aboutsummaryrefslogtreecommitdiff
path: root/tray.c
blob: d4eed312875bc1a8ff3398e84dc9b84a7fc2b56d (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/************************************************************************
 * This file is part of Paramano.                                       *
 *                                                                      *
 * Paramano 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.                      *
 *                                                                      *
 * Paramano 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 Paramano. If not, see                                     *
 * <http://www.gnu.org/licenses/>.                                      *
 ************************************************************************/

#include "tray.h"
#include "getcore.h"
#include "getfreq.h"
#include "getgov.h"
#include "bat_tray.h"
#include "defaults.h"
#include "paramano_set_interface.h"

#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <libintl.h>
#include <gtk/gtk.h>

static GtkStatusIcon* tray;
static char tooltip_text[1024];
static GtkWidget* menu;
static GSList* menu_items;
static GtkWidget* checked_menu_item;


/***********************************************************************
 * Handler for when freq is chosen
 **********************************************************************/
static void freq_menu_item_toggled(GtkCheckMenuItem* item, gpointer data)
{
	if(gtk_check_menu_item_get_active(item))
	{
		checked_menu_item = GTK_WIDGET(item);
		gint freq = GPOINTER_TO_INT(data);
		unsigned int i = 0;
		for(i = 0; i < gc_number(); i++)
			si_freq(freq, i);
	}
}


/***********************************************************************
 * Handler for when governor is chosen
 **********************************************************************/
static void gov_menu_item_toggled(GtkCheckMenuItem* item, gpointer data)
{
	if(gtk_check_menu_item_get_active(item))
	{
		checked_menu_item = GTK_WIDGET(item);
		char* gov = (char*)data;
		unsigned int i = 0;
		for(i = 0; i < gc_number(); i++)
			si_gov(gov, i);
	}
}

/***********************************************************************
 * Destroy a menu item
 **********************************************************************/
static void remove_menu_item(GtkWidget* menu_item, gpointer data)
{
	gtk_widget_destroy(menu_item);
}


/***********************************************************************
 * Remove all items in menu
 **********************************************************************/
static void tray_clear_menu()
{
	GtkContainer* cont = GTK_CONTAINER(menu);
	gtk_container_foreach(cont, remove_menu_item, NULL);
	menu_items = NULL;
}


/***********************************************************************
 * Create new instance of gtk_tray_new for gov/freq tray
 **********************************************************************/
static void tray_init_menu()
{
	menu = gtk_menu_new();
}

/***********************************************************************
 * Populate gtk menu object with all the entries
 **********************************************************************/
static void tray_generate_menu()
{
	tray_clear_menu();
	gg_init();

	char label[1024];
	unsigned int i = 0;

	char current_governor[20];
	memset(current_governor, '\0', sizeof(current_governor) );
	gg_current(0, current_governor, sizeof(current_governor) );

	gint current_frequency = gf_current(0);

	/* Add available frequencies */
	for(i = 0; i < gf_number(); i++)
	{
		gf_get_frequency_label(label, sizeof(label), gf_freqi(0, i));

		GtkWidget* item = gtk_radio_menu_item_new_with_label(menu_items, label);

		menu_items = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM (item));

		if(g_strcmp0(current_governor, "userspace") == 0 && gf_freqi(0, i) == current_frequency)
			gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);

		g_signal_connect(G_OBJECT(item), "toggled", GTK_SIGNAL_FUNC(freq_menu_item_toggled), GINT_TO_POINTER(gf_freqi(0, i)));

		gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
	}

	/* Add a seperator */
	GtkWidget* seperator = gtk_separator_menu_item_new();
	gtk_menu_append(menu, seperator);

	/* Add available governors */
	for(i = 0; i < gg_number(); i++)
	{
		if(g_strcmp0(gg_gov(0, i), "userspace") == 0)
			continue;

		GtkWidget* item = gtk_radio_menu_item_new_with_label(menu_items, gg_gov(0, i));
		menu_items = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM (item));

		if(g_strcmp0(gg_gov(0, i), current_governor) == 0)
			gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);

		g_signal_connect(G_OBJECT(item), "toggled", GTK_SIGNAL_FUNC(gov_menu_item_toggled), gg_gov(0, i));

		gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
	}
	gtk_widget_show_all(menu);
}

/***********************************************************************
 * Tell gtk what the tooltip message should be
 **********************************************************************/
static gboolean show_tooltip(GtkStatusIcon* status_icon, gint x, gint y, gboolean keyboard_mode,GtkTooltip* tooltip, gpointer data)
{
	gtk_tooltip_set_text(tooltip, tooltip_text);
	return true;
}


/***********************************************************************
 * Work out what the tooltip message would be now, store it away
 **********************************************************************/
static void update_tooltip_cache()
{
	char msg[10240], label[1024];
	char current_governor[20]; /* FIXME magic */
	unsigned int i = 0;
	unsigned int offset = 0;

	memset(current_governor, '\0', sizeof(current_governor) );

	gg_current(0, current_governor, sizeof(current_governor) );

	offset = snprintf(msg, sizeof(msg), _("Governor: %s\n"), current_governor);

	for(i = 0; i < gc_number(); i++)
	{
		gf_get_frequency_label(label, sizeof(label), gf_current(i));
		offset += snprintf(msg+offset, sizeof(msg)-offset, _("CPU%d: %s%s"), i, label, i == gc_number()-1 ? "" : "\n");
	}

	strncpy(tooltip_text, msg, sizeof(tooltip_text));
	tooltip_text[sizeof(tooltip_text)-1] = '\0';
}


/***********************************************************************
 * Handler for spawning the popup/context menu
 **********************************************************************/
static void popup_menu(GtkStatusIcon* statuc_icon,guint button,guint activate_time,gpointer data)
{
	tray_generate_menu();
	gtk_menu_popup(GTK_MENU(menu),NULL,NULL,gtk_status_icon_position_menu,tray,button,activate_time);
}


/**********************************************************************
 * Set icon based on current freq/governor
 **********************************************************************/
static void update_icon()
{
	char file[1024];
	int max_frequency = gf_freqi(0, 0);
	int adjusted_percent, percent;

	/* If max_frequency is 0, we don't want to divide by it,
	 * so give up, call it a day, and have a simple icon */
	if (max_frequency == 0)
	{
		adjusted_percent = 0;
	} else {
		/* Percentages need to be {25,50,75,100}. Round to one of these numbers.
		 * TO DO: round/truncate instead of lots of ifs */
		percent = (gf_current(0) * 100)/max_frequency;
		if(percent == 100) {
			adjusted_percent = 100;
		} else if(percent >= 65.5) {
			adjusted_percent = 75;
		} else if(percent >= 37.5) {
			adjusted_percent = 50;
		} else if(percent >= 12.5) {
			adjusted_percent = 25;
		} else {
			adjusted_percent = 0;
		}
	}

	snprintf(file, sizeof(file), "%s/cpu-%d.png", DEFAULT_THEME,adjusted_percent);
	gtk_status_icon_set_from_file(tray, file);
}


/***********************************************************************
 * Update the freq/gov tray icon
 * Also updates the cached tooltip text
 **********************************************************************/
static gboolean update()
{
	unsigned int i;
	switch ( get_battery_state() )
	{
		case STATE_DISCHARGING:
			if(DEFAULT_BAT_GOV)
			{
				for(i = 0; i < gc_number(); i++)
					si_gov(DEFAULT_BAT_GOV, i);
			}
			break;

		case STATE_CHARGING:
		case STATE_FULL:
			if(DEFAULT_AC_GOV)
			{
				for(i = 0; i < gc_number(); i++)
					si_gov(DEFAULT_AC_GOV, i);
			}

			break;
	}

	update_tooltip_cache();
	update_icon();
	return true;
}


/***********************************************************************
 * Apply default gov/freq for current situation
 **********************************************************************/
void tray_set_defaults()
{
	unsigned int i = 0;
	if(DEFAULT_GOV)
	{
		for(i = 0; i < gc_number(); i++)
			si_gov(DEFAULT_GOV, i);

	} else {
		for(i = 0; i < gc_number(); i++)
			si_gov("ondemand", i);
	}

	if(DEFAULT_FREQ)
	{
		for(i = 0; i < gc_number(); i++)
			si_freq(atoi(DEFAULT_FREQ), i);
	}

}


/***********************************************************************
 * Start the tray icon thingy
 **********************************************************************/
void tray_init()
{
	tray_set_defaults();
	tray = gtk_status_icon_new();
	char* icon_file = g_strconcat(DEFAULT_THEME, "/cpu-0.png", NULL);

	/* Force something useful to be in the cached tooltip text */
	update_tooltip_cache();

	gtk_status_icon_set_from_file(tray, icon_file);
	gtk_status_icon_set_has_tooltip(tray, TRUE);

	g_signal_connect(G_OBJECT(tray), "query-tooltip", GTK_SIGNAL_FUNC(show_tooltip), NULL);
	g_signal_connect(G_OBJECT(tray), "popup-menu", GTK_SIGNAL_FUNC(popup_menu), NULL);

	g_timeout_add(1000, update, NULL);

	/* Force meaningful tooltip cached text and force meaningful icon */
	update();

	tray_init_menu();
}


/***********************************************************************
 * Show the tray
 **********************************************************************/
void tray_show()
{
	gtk_status_icon_set_visible(tray, TRUE);
}

/***********************************************************************
 * Hide the tray
 **********************************************************************/
void tray_hide()
{
	gtk_status_icon_set_visible(tray, FALSE);
}


/***********************************************************************
 * Return bool for visiblity of tray
 **********************************************************************/
bool tray_visible()
{
	return gtk_status_icon_get_visible(tray);
}

/***********************************************************************
 * Return bool for embeddedness of tray
 **********************************************************************/
bool tray_embedded()
{
	return gtk_status_icon_is_embedded(tray);
}