aboutsummaryrefslogtreecommitdiff
path: root/trayfreq_set/trayfreq_set.c
blob: a9de8c12bcd71e04ff8fc1d90e3e42165ceace4b (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
146
147
148
149
150
151
152
153
154
155
156
157
/************************************************************************
 * 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/>.                                      *
 ************************************************************************/

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

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

char file_path[100];


void prepare_path(const char* file, const char* core)
{
	// Sanatise core to make sure there's not monkey business going on -- it's only an integer
	int core_i = atoi(core);

	sprintf(file_path, "/sys/devices/system/cpu/cpu%d/cpufreq/%s", core_i, file);
}

void file_open_error()
{
	printf( _("FAILED: Couldn't open %s for writing\n") , file_path);
}

void set_freq_max(const char* freq, const char* core)
{

	prepare_path("scaling_max_freq",core);
	FILE* fd = fopen(file_path, "w");
	if (fd)
	{
		fprintf(fd, freq);
		fclose(fd);
	} else {
		file_open_error();
	}
}

void set_freq_min(char* freq, char* core)
{
	prepare_path("scaling_min_freq",core);

	FILE* fd = fopen(file_path, "w");
	if (fd)
	{
		fprintf(fd, freq);
		fclose(fd);
	} else {
		file_open_error();
	}
}

static void set_speed(char* freq, char* core)
{
	prepare_path("scaling_setspeed",core);

	FILE* fd = fopen(file_path, "w");
	if (fd)
	{
		fprintf(fd, freq);
		fclose(fd);
	} else {
		file_open_error();
	}
}

void set_gov(char* gov, char* core)
{
	prepare_path("scaling_governor",core);

	FILE* fd = fopen(file_path, "w");
	if (fd)
	{
		fprintf(fd, gov);
		fclose(fd);
	} else {
		file_open_error();
	}
}

void set_freq(char* freq, char* core)
{
	set_gov("userspace", core);
	set_speed(freq, core);
}

int main(int argc, char *argv[])
{
	setlocale(LC_ALL,"");
	bindtextdomain("trayfreq","/usr/share/locale");
	textdomain("trayfreq");

	gc_init();
	gf_init();

	if(!argv[1])
	{
		printf( _("Use -g to set the governor or -f to set the frequency\n") );
	} else if(strcmp(argv[1], "-g") == 0) {
		if(!argv[2])
			printf( _("Pass the governor with -g\n") );
		else
		{
			if(!argv[3])
				printf( _("Pass the core with -c\n") );
			else if(strcmp(argv[3], "-c") == 0)
			{
				if(!argv[4])
					printf( _("Pass the core with -c\n") );
				else
					set_gov(argv[2], argv[4]);
			} else
				printf( _("Pass the core with -c\n") );
		}
	} else if(strcmp(argv[1], "-f") == 0) {
		if(!argv[2])
		{
			printf( _("Pass the frequency with -f\n") );
		} else {
			if(!argv[3])
			{
				printf( _("Pass the core with -c\n") );
			} else if(strcmp(argv[3], "-c") == 0)
			{
				if(!argv[4])
					printf( _("Pass the core with -c\n") );
				else
					set_freq(argv[2], argv[4]);
			} else {
				printf( _("Pass the core with -c\n") );
			}
		}
	} else {
		printf( _("Use -g to set the governor or -f to set the frequency\n") );
	}
	return 0;
}