aboutsummaryrefslogtreecommitdiff
path: root/trayfreq_set/trayfreq_set.c
blob: 4f288057875905e2ce873baed900eba24970a5fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/************************************************************************
 * This file is part of trayfreq-archlinux.                             *
 *                                                                      *
 * trayfreq-archlinux is free software; you can redistribute it and/or  *
 * modify it under the terms of the GNU General Public License as       *
 * published by the Free Software Foundation; either version 3 of the   *
 * License, or (at your option) any later version.                      *
 *                                                                      *
 * trayfreq-archlinux is distributed in the hope that it will be useful,*
 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
 * GNU General Public License for more details.                         *
 *                                                                      *
 * You should have received a copy of the GNU General Public License    *
 * along with trayfreq-archlinux. If not, see                           *
 * <http://www.gnu.org/licenses/>.                                      *
 ************************************************************************/


// <TO DO> : move a lot of this to a .h

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libintl.h>
#include <locale.h>

#include "../freq_tray/getfreq.h"
#include "../freq_tray/getcore.h"

#define FILE_PATH_STRING_SIZE 100

#define ARG_CORE	0x1
#define ARG_GOV		0x2
#define ARG_FREQ	0x4
typedef struct  {
	char present;
	char *core;
	char *governor;
	char *frequency;
} argument_summary;

// </TO DO>

char write_str_to_file(const char *file, const char *data, const char *core)
{
	FILE *fd;
	char file_path[FILE_PATH_STRING_SIZE];

	// Prepare file path
	memset(file_path, '\0', sizeof(file_path));
	sprintf(file_path, "/sys/devices/system/cpu/cpu%d/cpufreq/%s", atoi(core), file );

	// Try to open file and write data to it
	if ( !(fd = fopen(file_path, "w")) )
	{
		fprintf(fd, data);
		fclose(fd);
		return 1;
	}

	// Fallthrough: File couldn't be opened for writing
	printf( _("FAILED: Couldn't open %s for writing\n") , file_path);
	return 0;
}


#define set_freq_max(freq,core)		write_str_to_file("scaling_max_freq",freq,core)
#define set_freq_min(freq,core)		write_str_to_file("scaling_min_freq",freq,core)
#define set_speed(freq,core)		write_str_to_file("scaling_setspeed",freq,core)
#define set_gov(gov,core)			write_str_to_file("scaling_governor",gov,core)
#define set_freq(freq,core)			set_gov("userspace",core);set_speed(freq,core)

void get_argument_summary(int argc, char **argv, argument_summary *argsum)
{
	int arg;

	// Check for -{c,g,f} xyz
	for ( arg = 1; arg < argc-1; arg+=2 )
	{
		// Can't have empty argument
		if ( strlen(argv[arg+1]) != 0 )
		{
			if ( strcmp(argv[arg], "-c") == 0 )
			{
				// Found -c with an arg
				argsum->present |= ARG_CORE;
				argsum->core = (char*)(argv[arg+1]);
				continue;
			}

			if ( strcmp(argv[arg], "-f") == 0 )
			{
				// Found -f with an arg
				argsum->present |= ARG_FREQ;
				argsum->frequency = (char*)(argv[arg+1]);
				continue;
			}

			if ( strcmp(argv[arg], "-g") == 0 )
			{
				// Found -g with an arg
				argsum->present |= ARG_GOV;
				argsum->governor = (char*)(argv[arg+1]);
				//continue;
			}
		}
	}
}

int main(int argc, char *argv[])
{
	setlocale(LC_ALL,"");

	// TO DO: Not portable
	bindtextdomain("trayfreq","/usr/share/locale");

	// TO DO: Needs to be #defined
	textdomain("trayfreq");

	gc_init();
	gf_init();

	argument_summary args;
	memset(&args, 0, sizeof(args));

	// If unusual number of args, give up now
	if (argc == 5)
	{
		get_argument_summary(argc, argv, &args);
		
		if ( args.present == ( ARG_CORE | ARG_GOV ) )
		{
			return set_gov(args.governor , args.core);
		}

		if ( args.present == ( ARG_CORE | ARG_FREQ ) )
		{
			return set_freq(args.frequency , args.core);
		}
	}
	// Fall through to here if no valid argument combination
	fprintf(stderr, _("%s {-f frequency|-g governor} -c core\n"), argv[0] );
	return 1;
}