aboutsummaryrefslogtreecommitdiff
path: root/bat_tray.c
blob: 2de7ed2d855cfc0f587659783740d2310a3c8a96 (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
/************************************************************************
 * 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 "paramano.h"

static GtkStatusIcon* tray;
int bat_num; // Shortcoming: we only detect one battery
char *CHARGE_VALUE_PATH, *CHARGE_STATE_PATH;

/***********************************************************************
 * Return the battery level percentage
 **********************************************************************/
int get_bat_percent()
{
	return get_int_value_from_file(CHARGE_VALUE_PATH);
}

/***********************************************************************
 * Return number of seconds until battery is fully charged/discharged
 **********************************************************************/
int get_bat_seconds_left()
{
	int charge_now, charge_full, current_now;
	char* file;

	// Read current current value
	asprintf(&file, POWERDIR"BAT%d/current_now",bat_num);
	current_now = get_int_value_from_file(file);
	free(file);

	// Return -1 if zero current (infinite charge time)
	if (current_now == 0)
		return -1;

	asprintf(&file, POWERDIR"BAT%d/charge_now",bat_num);
	charge_now = get_int_value_from_file(file);
	free(file);

	switch(get_battery_state())
	{
		case STATE_CHARGING:
			// We need to know full charge to calculate this one
			asprintf(&file, POWERDIR"BAT%d/charge_full",bat_num);
			charge_full = get_int_value_from_file(file);
			free(file);

			return (3600*(charge_full - charge_now))/current_now;
			break;
		case STATE_DISCHARGING:
			return (3600*charge_now)/current_now;
			break;
		default:
			return -1;
	}
}


/***********************************************************************
 * Updates the battery tray tooltip text
 **********************************************************************/
static gboolean update_tooltip(GtkStatusIcon* status_icon,gint x,gint y,gboolean keyboard_mode,GtkTooltip* tooltip,gpointer data)
{
	char* msg;
	int seconds_left = get_bat_seconds_left();

	char* time_left;

	if (seconds_left != -1)
	{
		asprintf(&time_left, _("%02d:%02d:%02d left"), (int)(seconds_left/3600), (int)((seconds_left%3600)/60), seconds_left%60);
	} else {
		asprintf(&time_left, _("Unknown time left"));
	}

	switch(get_battery_state())
	{
		case STATE_DISCHARGING:
			debug("Discharging\n");
			asprintf(&msg, _("Discharging (%d%%)\n%s"), get_bat_percent(), time_left);
			break;

		case STATE_CHARGING:
			debug("Charging\n");
			asprintf(&msg, _("Charging (%d%%)\n%s"), get_bat_percent(), time_left);
			break;

		case STATE_CHARGED:
			debug("Charged\n");
			asprintf(&msg, _("Fully charged") );
			break;

		default:
			debug("Unknown\n");
			asprintf(&msg, _("Unknown status") );
			break;
	}
	debug("Setting tooltip text to '%s'\n",msg);
	gtk_tooltip_set_text(tooltip, msg);

	free(time_left);
	free(msg);

	return true;
}


/***********************************************************************
 * Updates the battery tray icon based upon battery percent
 **********************************************************************/
static gboolean update_icon(gpointer user_data)
{
	char *icon_file;
	unsigned int rounded;

	rounded = 20* (int)((get_bat_percent()+10)/20); // Round percentage to 0, 20, 40, 60, 80 or 100

	debug("Rounded/adjusted percentage: %d\n",rounded);

	switch ( get_battery_state() )
	{
		case STATE_DISCHARGING:
			asprintf(&icon_file,"%s/bat-%d.png",DEFAULT_THEME,rounded);
			break;

		case STATE_CHARGING:
			asprintf(&icon_file,"%s/bat-%d-charging.png",DEFAULT_THEME,rounded);
			break;

		default:
			asprintf(&icon_file,"%s/bat-charged.png",DEFAULT_THEME);
			break;
	}

	debug("Setting tray icon to '%s'\n",icon_file);
	gtk_status_icon_set_from_file(tray, icon_file);

	free(icon_file);

	return true;
}


/***********************************************************************
 * Initialise the tray and related variables
 **********************************************************************/
void bat_tray_init()
{
	char *icon_file;

	// Get the battery number, store it for later
	bat_num = get_bat_num();

	// Set up battery info filenames/paths
	asprintf(&CHARGE_VALUE_PATH, "/sys/class/power_supply/BAT%d/capacity", bat_num);
	asprintf(&CHARGE_STATE_PATH, "/sys/class/power_supply/BAT%d/status", bat_num);

	debug("Spawning new status icon\n");
	tray = gtk_status_icon_new();
	asprintf(&icon_file,"%s/bat-charged.png",DEFAULT_THEME);
	gtk_status_icon_set_from_file(tray, icon_file);
	free(icon_file);
	gtk_status_icon_set_has_tooltip (tray, TRUE);
	g_signal_connect(G_OBJECT(tray), "query-tooltip", GTK_SIGNAL_FUNC(update_tooltip), NULL);
	g_timeout_add(5000, update_icon, NULL);
}


/***********************************************************************
 * Free memory allocated in bat_tray_init()
 **********************************************************************/
void bat_tray_end()
{
	free(CHARGE_VALUE_PATH);
	free(CHARGE_STATE_PATH);
}


/***********************************************************************
 * Show the battery tray
 **********************************************************************/
void bat_tray_show()
{
	debug("Showing tray\n");
	gtk_status_icon_set_visible(tray, TRUE);
}


/***********************************************************************
 * Hide the battery tray
 **********************************************************************/
void bat_tray_hide()
{
	debug("Hiding tray\n");
	gtk_status_icon_set_visible(tray, FALSE);
}


/***********************************************************************
 * Return the battery state
 **********************************************************************/
int get_battery_state()
{
	if (file_has_line(CHARGE_STATE_PATH, "Discharging"))
	{
		debug("Battery discharging\n");
		return STATE_DISCHARGING;
	}

	if (file_has_line(CHARGE_STATE_PATH, "Full"))
	{
		debug("Battery full\n");
		return STATE_CHARGED;
	}

	if (file_has_line(CHARGE_STATE_PATH, "Charging"))
	{
		debug("Battery charging\n");
		return STATE_CHARGING;
	}
	debug("Fallthrough: unknown status\n");
	return STATE_UNKNOWN;
}


/***********************************************************************
 * Get the number of the first (who has more than one?) battery
 * Returns -1 if no battery present
 **********************************************************************/
int get_bat_num()
{
	FILE* fd;
	char* file;
	unsigned int i;
	for(i = 0; i < 10; i++)
	{
		asprintf(&file, "/sys/class/power_supply/BAT%d/present", i);
		if( (fd = check_for_file(file)) )
		{
			debug("Found battery %d\n",i);
			if (fgetc(fd) == '1')
			{
				debug("Battery %d is present\n",i);
				fclose(fd);
				free(file);
				return i;
			}
		}
	}
	debug("Fallthrough: couldn't find battery\n");
	free(file);
	return -1;
}