aboutsummaryrefslogtreecommitdiff
path: root/src/bat_tray.c
blob: c4b7d6104d44299f9d731ef7ccdfa77c97a4a695 (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
/************************************************************************
 * This file is part of trayfreq.                                       *
 *                                                                      *
 * trayfreq 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 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. If not, see <http://www.gnu.org/licenses/>.     *
 ************************************************************************/

#include "bat_tray.h"
#include "getbat.h"
#include "utilities.h"

#include <gtk/gtk.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

static GtkStatusIcon* tray;
#define TOOLTIP_TEXT_SIZE 500
gchar tooltip_text[TOOLTIP_TEXT_SIZE];

void bat_tray_set_tooltip(const gchar* msg)
{
	memset(tooltip_text, '\0', TOOLTIP_TEXT_SIZE);
	memmove(tooltip_text, msg, strlen(msg));
}

static gboolean update_tooltip(GtkStatusIcon* status_icon,gint x,gint y,gboolean keyboard_mode,GtkTooltip* tooltip,gpointer data)
{
	gchar msg[500];
	memset(msg, '\0', 500);

	//printf("dis: %i, char: %i, full: %i\n", gb_discharging(), gb_charging(), gb_charged());

	if(gb_discharging())
	{
		//gchar time[50];
		//memset(time, '\0', 50);
		sprintf(msg, "Discharging (%i%% left)", gb_percent());
	} else if(gb_charging()) {
		//gchar time[50];
		//memset(time, '\0', 50);
		sprintf(msg, "Charging (%i%%)", gb_percent());
	} else if(gb_charged()) {
		sprintf(msg, "Fully Charged\nAC Plugged In");
	} else {
		sprintf(msg, "Unknown Status");
	}

	bat_tray_set_tooltip(msg);
	gtk_tooltip_set_text(tooltip, tooltip_text);

	return TRUE;
}

void bat_tray_update_icon_percent()
{
	gchar* file;
	int percent = gb_percent();
	int adjusted_percent;
	gchar adjusted_percent_string[4];
	memset(adjusted_percent_string, '\0', 4);

	if(percent > 90)
		adjusted_percent=100;
	else if(percent > 70)
		adjusted_percent=80;
	else if(percent > 50)
		adjusted_percent=60;
	else if(percent > 30)
		adjusted_percent=40;
	else if(percent > 10)
		adjusted_percent=20;
	else
		adjusted_percent=0;

	sprintf(adjusted_percent_string, "%i", adjusted_percent);

	if(gb_discharging())
	{
		file = g_strconcat(util_get_prefix(), "/share/trayfreq/traybat-", adjusted_percent_string, ".png", NULL);
	} else if(gb_charging())
	{
		file = g_strconcat(util_get_prefix(), "/share/trayfreq/traybat-", adjusted_percent_string, "-charging.png", NULL);
	} else {
		file = g_strconcat(util_get_prefix(), "/share/trayfreq/traybat-charged.png", NULL);
	}

	gtk_status_icon_set_from_file(tray, file);
}

static gboolean update_icon(gpointer user_data)
{
	bat_tray_update_icon_percent();
	return TRUE;
}

void bat_tray_init()
{
	tray = gtk_status_icon_new();
	gchar* icon_file = g_strconcat(util_get_prefix(), "/share/trayfreq/traybat-charged.png", NULL);
	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(update_tooltip), NULL);
	gtk_timeout_add(5000, update_icon, NULL);
}


void bat_tray_show()
{
	gtk_status_icon_set_visible(tray, TRUE);
}

void bat_tray_hide()
{
	gtk_status_icon_set_visible(tray, FALSE);
}