From ce5d47962ac628521a658f20b3f48aee7d041fe3 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 20 Jul 2014 13:01:10 +1200 Subject: Removed licence symlink all together --- Makefile | 148 ++++++++++++ README.md | 75 ++++++ bat_tray.c | 214 +++++++++++++++++ bat_tray.h | 34 +++ common.c | 81 +++++++ common.h | 48 ++++ config_file.c | 53 +++++ config_file.h | 34 +++ defaults.c | 40 ++++ defaults.h | 36 +++ getcore.c | 63 +++++ getcore.h | 25 ++ getfreq.c | 154 ++++++++++++ getfreq.h | 31 +++ getgov.c | 127 ++++++++++ getgov.h | 28 +++ lc/fr.po | 68 ++++++ reload.c | 45 ++++ reload.h | 23 ++ themes/default/cpufreq-0.png | Bin 0 -> 939 bytes themes/default/cpufreq-100.png | Bin 0 -> 1057 bytes themes/default/cpufreq-25.png | Bin 0 -> 942 bytes themes/default/cpufreq-50.png | Bin 0 -> 988 bytes themes/default/cpufreq-75.png | Bin 0 -> 1014 bytes themes/default/traybat-0-charging.png | Bin 0 -> 1270 bytes themes/default/traybat-0.png | Bin 0 -> 922 bytes themes/default/traybat-100-charging.png | Bin 0 -> 1261 bytes themes/default/traybat-100.png | Bin 0 -> 839 bytes themes/default/traybat-20-charging.png | Bin 0 -> 1297 bytes themes/default/traybat-20.png | Bin 0 -> 923 bytes themes/default/traybat-40-charging.png | Bin 0 -> 1288 bytes themes/default/traybat-40.png | Bin 0 -> 925 bytes themes/default/traybat-60-charging.png | Bin 0 -> 1305 bytes themes/default/traybat-60.png | Bin 0 -> 912 bytes themes/default/traybat-80-charging.png | Bin 0 -> 1280 bytes themes/default/traybat-80.png | Bin 0 -> 872 bytes themes/default/traybat-charged.png | Bin 0 -> 1913 bytes tray.c | 406 ++++++++++++++++++++++++++++++++ tray.h | 33 +++ trayfreq.c | 152 ++++++++++++ trayfreq.conf.src | 41 ++++ trayfreq.desktop | 7 + trayfreq.h | 24 ++ trayfreq_set.c | 160 +++++++++++++ trayfreq_set_interface.c | 50 ++++ trayfreq_set_interface.h | 25 ++ 46 files changed, 2225 insertions(+) create mode 100644 Makefile create mode 100644 README.md create mode 100644 bat_tray.c create mode 100644 bat_tray.h create mode 100644 common.c create mode 100644 common.h create mode 100644 config_file.c create mode 100644 config_file.h create mode 100644 defaults.c create mode 100644 defaults.h create mode 100644 getcore.c create mode 100644 getcore.h create mode 100644 getfreq.c create mode 100644 getfreq.h create mode 100644 getgov.c create mode 100644 getgov.h create mode 100644 lc/fr.po create mode 100644 reload.c create mode 100644 reload.h create mode 100644 themes/default/cpufreq-0.png create mode 100644 themes/default/cpufreq-100.png create mode 100644 themes/default/cpufreq-25.png create mode 100644 themes/default/cpufreq-50.png create mode 100644 themes/default/cpufreq-75.png create mode 100644 themes/default/traybat-0-charging.png create mode 100644 themes/default/traybat-0.png create mode 100644 themes/default/traybat-100-charging.png create mode 100644 themes/default/traybat-100.png create mode 100644 themes/default/traybat-20-charging.png create mode 100644 themes/default/traybat-20.png create mode 100644 themes/default/traybat-40-charging.png create mode 100644 themes/default/traybat-40.png create mode 100644 themes/default/traybat-60-charging.png create mode 100644 themes/default/traybat-60.png create mode 100644 themes/default/traybat-80-charging.png create mode 100644 themes/default/traybat-80.png create mode 100644 themes/default/traybat-charged.png create mode 100644 tray.c create mode 100644 tray.h create mode 100644 trayfreq.c create mode 100644 trayfreq.conf.src create mode 100644 trayfreq.desktop create mode 100644 trayfreq.h create mode 100644 trayfreq_set.c create mode 100644 trayfreq_set_interface.c create mode 100644 trayfreq_set_interface.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..589df84 --- /dev/null +++ b/Makefile @@ -0,0 +1,148 @@ +# Tabsize: 4 + +# Build utils +MAKE = make +CC = gcc + +# Directories +PREFIX=/usr +BINDIR=$(PREFIX)/bin +SYSCONFDIR=/etc +SHAREDIR=$(PREFIX)/share +LOCALEDIR=$(SHAREDIR)/locale + +# External program/support programs +SUDO=$(BINDIR)/sudo +TRAYFREQ_SET=$(BINDIR)/trayfreq-set + +# Misc +TRAYFREQ_CONF=$(SYSCONFDIR)/trayfreq.conf +ROOT_UID=0 + + +ifdef DEBUG + EXTRA_CFLAGS+=-DDEBUG +endif + +EXTRA_CFLAGS+= -DPREFIX=\"$(PREFIX)\" \ + -DBINDIR=\"$(BINDIR)\" \ + -DSUDO=\"$(SUDO)\" \ + -DTRAYFREQ_SET=\"$(TRAYFREQ_SET)\" \ + -DTRAYFREQ_CONF=\"$(TRAYFREQ_CONF)\" \ + -DLOCALEDIR=\"$(LOCALEDIR)\" \ + -DSHAREDIR=\"$(SHAREDIR)\" \ + -DROOT_UID=$(ROOT_UID) + + +DEPS = bat_tray.h \ + common.h \ + config_file.h \ + defaults.h \ + getcore.h \ + getfreq.h \ + getgov.h \ + reload.h \ + trayfreq.h \ + tray.h \ + trayfreq_set_interface.h + +CFLAGS = -I/usr/include/gtk-2.0 \ + -I/usr/lib/gtk-2.0/include \ + -I/usr/include/pango-1.0 \ + -I/usr/include/cairo \ + -I/usr/include/gdk-pixbuf-2.0 \ + -I/usr/include/atk-1.0 \ + -I/usr/include/glib-2.0 \ + -I/usr/lib/glib-2.0/include \ + -Wall \ + -D_=gettext + +LDFLAGS = -lgtk-3 \ + -lgobject-2.0 \ + -lglib-2.0 +######################################################################## +# Make entire suite +all: trayfreq trayfreq-set lang trayfreq.conf +######################################################################## + + +######################################################################## +# Make main trayfreq system tray program +trayfreq: bat_tray.o \ + common.o \ + config_file.o \ + defaults.o \ + getcore.o \ + getfreq.o \ + getgov.o \ + reload.o \ + trayfreq.o \ + tray.o \ + trayfreq_set_interface.o + $(CC) -o $@ $? $(LDFLAGS) + +######################################################################## +# Make trayfreq-set utility +trayfreq-set: \ + trayfreq_set.o \ + getcore.o \ + getfreq.o \ + getgov.o + + $(CC) -o $@ $? $(LDFLAGS) +######################################################################## + + +######################################################################## +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) $(EXTRA_CFLAGS) +######################################################################## + + +######################################################################## +# Make language file(s) +lang: + msgfmt -c -o lc/fr.mo lc/fr.po +######################################################################## + + +######################################################################## +# Prepare template config file +trayfreq.conf: + sed -e 's:SHAREDIR:$(SHAREDIR):g' \ + -e 's:TRAYFREQ_CONF:$(TRAYFREQ_CONF):g' \ + trayfreq.conf.src > trayfreq.conf +######################################################################## + + +######################################################################## +# Strip all symbols from binaries +strip: + strip -s trayfreq trayfreq-set + +######################################################################## + + +######################################################################## +# Remove generated files +clean: + rm -f trayfreq trayfreq-set *.o lc/*.mo trayfreq.conf +######################################################################## + + +######################################################################## +# Install entire suite +install: + mkdir -p $(DESTDIR)/$(SHAREDIR)/trayfreq/ + cp -r themes $(DESTDIR)/$(SHAREDIR)/trayfreq/ + + install -Dm 644 lc/fr.mo $(DESTDIR)/$(LOCALEDIR)/fr/LC_MESSAGES/trayfreq.mo + + install -Dm 644 trayfreq.conf $(DESTDIR)/etc/trayfreq.conf + install -Dm 644 trayfreq.desktop $(DESTDIR)/etc/xdg/autostart/trayfreq.desktop + + install -Dm 755 trayfreq $(DESTDIR)/$(BINDIR)/trayfreq + install -Dm 755 trayfreq-set $(DESTDIR)/$(BINDIR)/trayfreq-set + + ln -s ../../../etc/trayfreq.conf $(DESTDIR)/$(SHAREDIR)/trayfreq/trayfreq.conf +######################################################################## diff --git a/README.md b/README.md new file mode 100644 index 0000000..2072b48 --- /dev/null +++ b/README.md @@ -0,0 +1,75 @@ +What? +===== + +This is a fork of trayfreq, intended to work on modern Linux systems. +The original trayfreq wasn't compatible with Archlinux, due to the migration away from a `/proc/acpi` directory. +I forked the project and got it working again. + +If you're using ArchLinux, just jump over to the [AUR page for this software](https://aur.archlinux.org/packages/trayfreq-archlinux) and install it that way. + +Despite the software's name, it should work on other distros as well. + + +Runtime Dependencies +-------------------- + +* gtk3 +* sudo + +Building +======== +Due to KISS, I don't tend to use ./configure scripts for relatively small programs. +Instead, it's up to the user to check they have the right libraries. +The sort of parameters you might pass to a ./configure can instead be passed to make. + +Here's a list: + +* BINDIR to override default binary directory (`/usr/bin`) for trayfreq and trayfreq-set +* PREFIX to prefix all paths with something +* LOCALE_DIR to override default of `/usr/share/locale` +* SUDO to override default sudo program `/usr/bin/sudo` +* MAKE to override default make program `make` +* CC to override default compiler `gcc` +* TRAYFREQ_SET to override default trayfreq-set program of `PREFIX/BINDIR/trayfreq-set` +* DESTDIR (only for `make install` -- see below) + +If you'd like a huge amount of debug information on the tty every time you run trayfreq or trayfreq-set, then set DEBUG to something, eg: + + make all DEBUG=yeah + + +Extreme example for nutty system with binaries at `/exec` and sudo `/exec/sodu`: + + make all BINDIR=/exec SUDO=/exec/sodu + + +Installation +============ +A plain `make install` should work if you're installing straight to `/`, i.e. you're not building a package or anything. +If you *are* building a package or installing to a special dir, then you might want something like this: + + make install DESTDIR=/path/to/package/temp/dir + +This will prefix **all** paths with `/path/to/package/temp/dir` while installing. + +Autostarting +------------ +`make install` will copy a .desktop file to /etc/xdg/autostart, meaning that all xdg-compliant window managers should start it automatically when Bob logs in. +Bob's WM starts these programs as root, thus he is fully able to change the governor. + +Alice uses a non-xdg-compliant WM (e.g. dwm) so trayfreq is run as the user `alice`. +She doesn't have permission to write to the files under `/sys/` so trayfreq can't change the governor. +Alice needs to make trayfreq run `trayfreq-set` through sudo. +This can be done in the configuration file. +Then, she also needs to give herself passwordless permission to run `trayfreq-set` through sudo: + + + # ... (/etc/sudoers + alice ALL = NOPASSWD: /usr/bin/trayfreq-set + # ... + +Simple. + +Screenshots 'n' Stuff +===================== +See the [official page](http://batchbin.ueuo.com/projects/trayfreq-archlinux) for screenshots diff --git a/bat_tray.c b/bat_tray.c new file mode 100644 index 0000000..ac8fc94 --- /dev/null +++ b/bat_tray.c @@ -0,0 +1,214 @@ +/************************************************************************ + * 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 * + * . * + ************************************************************************/ + + +#include "bat_tray.h" + +#include +#include +#include "common.h" +#include "defaults.h" + + +static GtkStatusIcon* tray; + +int bat_num; // Shortcoming: we only detect one battery +char CHARGE_VALUE_PATH[FILE_PATH_SIZE]; +char CHARGE_STATE_PATH[FILE_PATH_SIZE]; + +/*********************************************************************** + * Return the battery level percentage + **********************************************************************/ +int get_bat_percent() +{ + return get_int_value_from_file(CHARGE_VALUE_PATH); +} + + +#define TOOLTIP_TEXT_SIZE 128 +char tooltip_text[TOOLTIP_TEXT_SIZE]; + + +/*********************************************************************** + * 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[TOOLTIP_TEXT_SIZE]; + + switch(get_battery_state()) + { + case STATE_DISCHARGING: + debug("Discharging\n"); + sprintf(msg, _("Discharging (%d%%)"), get_bat_percent()); + break; + + case STATE_CHARGING: + debug("Charging\n"); + sprintf(msg, _("Charging (%d%%)"), get_bat_percent()); + break; + + case STATE_CHARGED: + debug("Charged\n"); + sprintf(msg, _("Fully charged") ); + break; + + default: + debug("Unknown\n"); + sprintf(msg, _("Unknown status") ); + break; + } + + debug("Setting tooltip text to '%s'\n",msg); + gtk_tooltip_set_text(tooltip, msg); + + return true; +} + + +/*********************************************************************** + * Updates the battery tray icon based upon battery percent + **********************************************************************/ +gboolean update_icon(gpointer user_data) +{ + char *icon_file; + char percent_string[4]; // worst case scenario 100 + \0 + 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); + sprintf(percent_string, "%d", rounded); + + switch ( get_battery_state() ) + { + case STATE_DISCHARGING: + icon_file = g_strconcat(_DEFAULT_THEME, "/traybat-", percent_string, ".png", NULL); + break; + + case STATE_CHARGING: + icon_file = g_strconcat(_DEFAULT_THEME, "/traybat-", percent_string, "-charging.png", NULL); + break; + + default: + icon_file = g_strconcat(_DEFAULT_THEME, "/traybat-charged.png", NULL); + break; + } + + debug("Setting tray icon to '%s'\n",icon_file); + gtk_status_icon_set_from_file(tray, icon_file); + return true; +} + + + +/*********************************************************************** + * Initialise the tray and related variables + **********************************************************************/ +void bat_tray_init() +{ + // Get the battery number, store it for later + bat_num = get_bat_num(); + + // Set up battery info filenames/paths + sprintf(CHARGE_VALUE_PATH, "/sys/class/power_supply/BAT%d/capacity", bat_num); + sprintf(CHARGE_STATE_PATH, "/sys/class/power_supply/BAT%d/status", bat_num); + + debug("Spawning new status icon\n"); + tray = gtk_status_icon_new(); + char* icon_file = g_strconcat(_DEFAULT_THEME, "/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); + g_timeout_add(5000, update_icon, NULL); +} + + +/*********************************************************************** + * 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; + gchar file[40]; + unsigned int i; + for(i = 0; i < 3; i++) + { + sprintf(file, "/sys/class/power_supply/BAT%d/present", i); + debug("Attempting to open '%s'\n",file); + if( (fd = fopen(file, "r")) ) + { + debug("Found battery %d\n",i); + if (fgetc(fd) == '1') + { + debug("Battery %d is present\n",i); + fclose(fd); + return i; + } + } + } + debug("Fallthrough: couldn't find battery\n"); + return -1; +} diff --git a/bat_tray.h b/bat_tray.h new file mode 100644 index 0000000..e459a11 --- /dev/null +++ b/bat_tray.h @@ -0,0 +1,34 @@ +/************************************************************************ + * 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 * + * . * + ************************************************************************/ + +#ifndef BAT_TRAY_H +#define BAT_TRAY_H + +#define STATE_CHARGING 0 +#define STATE_DISCHARGING 1 +#define STATE_CHARGED 2 +#define STATE_FULL STATE_CHARGED +#define STATE_UNKNOWN 3 + +void bat_tray_init(); +void bat_tray_show(); +void bat_tray_hide(); +int get_battery_state(); +int get_bat_num(); + +#endif /* ifndef BAT_TRAY_H */ diff --git a/common.c b/common.c new file mode 100644 index 0000000..3bf7dd1 --- /dev/null +++ b/common.c @@ -0,0 +1,81 @@ +/************************************************************************ + * 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 * + * . * + ************************************************************************/ + +#include "common.h" + +#include /* fopen, fgets, printf */ +#include /* atoi */ +#include /* lots of functions */ + +/*********************************************************************** + * Return integer value from first line in file + **********************************************************************/ +int get_int_value_from_file(const char* filename) +{ + FILE* fd; + char buffer[512]; + int value; + + if(!(fd = fopen(filename, "r"))) + return -1; + + if (fgets(buffer, sizeof(buffer), fd)) + value = get_int(buffer); + + fclose(fd); + return value; +} + + +/*********************************************************************** + * Return true/false if a file has specified line of text + **********************************************************************/ +bool file_has_line(const char *filename, const char *line) +{ + FILE* fd; + char buffer[512]; + + if (!(fd = fopen(filename, "r"))) + return false; + + while (fgets(buffer, sizeof(buffer), fd) != NULL) + { + if(strstr(buffer, line) != NULL) + { + fclose(fd); + return true; + } + } + fclose(fd); + return false; +} + + +/*********************************************************************** + * Fetches first number (ie the 35 of "35 123") from a + * string + **********************************************************************/ +int get_int(const char* string) +{ + char* first_num; + + first_num = strpbrk(string, "0123456789"); + debug("first_num: '%s'\n",first_num); + + return atoi(first_num); +} diff --git a/common.h b/common.h new file mode 100644 index 0000000..ca1be0f --- /dev/null +++ b/common.h @@ -0,0 +1,48 @@ +/************************************************************************ + * 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 * + * . * + ************************************************************************/ + +#ifndef COMMON_H +#define COMMON_H + +#include + +int get_int_value_from_file(const char* filename); +int get_int(const char* string); +bool file_has_line(const char *filename, const char *line); + +#include +// Stringification of line number +#define STRING2(x) #x +#define STRING(x) STRING2(x) +#define STR_LINE STRING(__LINE__) +// + + +#ifdef DEBUG +/* + Make debug macros into empty space if debug mode's not enabled. + Otherwise, make debug macro calls into appropriate printf()s +*/ + #define debug(...) printf("DEBUG: "__FILE__":"STR_LINE" --- "__VA_ARGS__) +#else + #define debug(...); +#endif + +#define info(...) printf("INFO: "__FILE__":"STR_LINE" --- "__VA_ARGS__) +#define FILE_PATH_SIZE 2048 +#endif diff --git a/config_file.c b/config_file.c new file mode 100644 index 0000000..169b82a --- /dev/null +++ b/config_file.c @@ -0,0 +1,53 @@ +/************************************************************************ + * 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 * + * . * + ************************************************************************/ + +#include "config_file.h" +#include "common.h" + +gboolean config_open(struct config_file* config_file) +{ + if(config_file->key_file) + { + debug("Freeing config_file->keyfile\n"); + g_key_file_free(config_file->key_file); + } + + debug("Creating new config_file->key_file\n"); + config_file->key_file = g_key_file_new(); + gboolean success = g_key_file_load_from_file( + config_file->key_file, + config_file->file_name, + G_KEY_FILE_NONE, + NULL); + debug("Returning %s\n",success? "TRUE" : "FALSE"); + return success; +} + +void config_close(struct config_file* config_file) +{ + debug("Freeing key_file with %s value\n",config_file->key_file == NULL? "NULL":"non-NULL"); + g_key_file_free(config_file->key_file); +} + +gchar* config_get_key(struct config_file* config_file, const gchar* group_name, const gchar* key_name) +{ + return g_key_file_get_value(config_file->key_file, + group_name, + key_name, + NULL); +} diff --git a/config_file.h b/config_file.h new file mode 100644 index 0000000..d8e150b --- /dev/null +++ b/config_file.h @@ -0,0 +1,34 @@ +/************************************************************************ + * 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 * + * . * + ************************************************************************/ + +#ifndef CONFIG_FILE_H +#define CONFIG_FILE_H + +#include + +struct config_file +{ + GKeyFile* key_file; + gchar* file_name; +}; + +gboolean config_open(struct config_file* config_file); +void config_close(struct config_file* config_file); +gchar* config_get_key(struct config_file* config_file, const gchar* group_name, const gchar* key_name); + +#endif /* ifndef CONFIG_FILE_H */ diff --git a/defaults.c b/defaults.c new file mode 100644 index 0000000..627b702 --- /dev/null +++ b/defaults.c @@ -0,0 +1,40 @@ +/************************************************************************ + * 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 * + * . * + ************************************************************************/ + +#include "defaults.h" + +#include + +char* _DEFAULT_GOV; +char* _DEFAULT_FREQ; +char* _DEFAULT_PROG; +char* _DEFAULT_BAT_GOV; +char* _DEFAULT_AC_GOV; +bool _DEFAULT_SHOW_BATTERY = true; +char _DEFAULT_THEME[2048]; // to do : make dynamic with malloc + +void defaults_init() +{ + _DEFAULT_GOV = NULL; + _DEFAULT_FREQ = NULL; + _DEFAULT_PROG = NULL; + _DEFAULT_BAT_GOV = NULL; + _DEFAULT_AC_GOV = NULL; + _DEFAULT_SHOW_BATTERY = true; + sprintf (_DEFAULT_THEME, SHAREDIR"/trayfreq/themes/default"); +} diff --git a/defaults.h b/defaults.h new file mode 100644 index 0000000..0963b8d --- /dev/null +++ b/defaults.h @@ -0,0 +1,36 @@ +/************************************************************************ + * 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 * + * . * + ************************************************************************/ + +#ifndef DEFAULTS_H +#define DEFAULTS_H + +#include /* boolean types */ +#include /* NULL */ + +char* _DEFAULT_GOV; +char* _DEFAULT_FREQ; +char* _DEFAULT_PROG; +char* _DEFAULT_BAT_GOV; +char* _DEFAULT_AC_GOV; +bool _DEFAULT_SHOW_BATTERY; +char _DEFAULT_THEME[2048]; + + +void defaults_init(); + +#endif /* ifndef DEFAULTS_H */ diff --git a/getcore.c b/getcore.c new file mode 100644 index 0000000..7ef5c91 --- /dev/null +++ b/getcore.c @@ -0,0 +1,63 @@ +/************************************************************************ + * 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 * + * . * + ************************************************************************/ + +#include "getcore.h" + +#include "common.h" + +#include +#include +#include + +unsigned int cores; + + +/*********************************************************************** + * Return true/false if specified core num exists + **********************************************************************/ +bool core_exists(unsigned int core) +{ + FILE* fd; + char path[80]; + char corestr[4]; + + sprintf(corestr, "%d", core); + sprintf(path, "/sys/devices/system/cpu/cpu%s/cpufreq/scaling_cur_freq", corestr); + debug("Checking if core %d exists by opening '%s'\n",core,path); + return (gboolean)(fd = fopen(path, "r")); +} + + +/*********************************************************************** + * Initialise surrounding variables + **********************************************************************/ +void gc_init() +{ + cores = 0; + while(core_exists(++cores)); + debug("Found %d cores\n",cores); +} + + +/*********************************************************************** + * Return number of cores + **********************************************************************/ +int gc_number() +{ + return cores; +} diff --git a/getcore.h b/getcore.h new file mode 100644 index 0000000..fc1fc8d --- /dev/null +++ b/getcore.h @@ -0,0 +1,25 @@ +/************************************************************************ + * 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 * + * . * + ************************************************************************/ + +#ifndef GETCORE_H +#define GETCORE_H + +void gc_init(); +int gc_number(); + +#endif diff --git a/getfreq.c b/getfreq.c new file mode 100644 index 0000000..e94bf8c --- /dev/null +++ b/getfreq.c @@ -0,0 +1,154 @@ +/************************************************************************ + * 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 * + * . * + ************************************************************************/ + +#include "getfreq.h" +#include "getcore.h" +#include "common.h" + +#include +#include +#include +#include + +/* [CORE][FREQUENCY NUMBER] */ +char freqs[999][50][13]; +int total_freqs; + + +/*********************************************************************** + * Initialise surrounding variables, get available freqs etc + **********************************************************************/ +void gf_init() +{ + gchar freq_string[500]; + + int i = 0; + for(i = 0; i < gc_number(); i++) + { + memset(freq_string, '\0', sizeof(freq_string) ); + + // Get available governor freqs. If no governor, try next cpu + if (gf_available(i, freq_string, sizeof(freq_string) ) == -1) + { + debug("Couldn't find gov on core %d\n",i); + continue; + } + + // freq_string is a space separated list of freqs so + // iterate over each frequency in freq_string + gchar* curr = &freq_string[0]; + gchar* end_of_curr = g_strstr_len(curr, strlen(curr), " "); + while(end_of_curr) + { + // TO DO : get rid of magic constants + memset(freqs[i][total_freqs], '\0', 13); // TO DO: get rid of magic constant 13 + memmove(freqs[i][total_freqs], curr, end_of_curr - curr); + + curr = end_of_curr+1; + end_of_curr = g_strstr_len(curr, strlen(curr), " "); + total_freqs++; + } + } + debug("Found %d frequencies\n",total_freqs); +} + +/*********************************************************************** + * Return current frequency for core + **********************************************************************/ +int gf_current(int core) +{ + FILE* fd; + char buff[13]; + char path[80]; + int freq; + + sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", core); + + if(!(fd = fopen(path, "r"))) + { + debug("Couldn't open '%s'\n",path); + return -1; + } + + fgets(buff, 13, fd); + + freq = atoi(buff); + fclose(fd); + debug("Found freq %d on core %d\n",freq,core); + return freq; +} + + +/*********************************************************************** + * Populate out with available frequencies for core + **********************************************************************/ +int gf_available(int core, char* out, int size) +{ + FILE* fd; + char path[80]; + + sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_available_frequencies", core); + + if(!(fd = fopen(path, "r"))) + { + debug("Couldn't open '%s'\n",path); + return -1; + } + + fgets(out, size, fd); + + fclose(fd); + return 0; +} + +/*********************************************************************** + * Populate out with a formatted, units-added freq label for freq + **********************************************************************/ +void gf_get_frequency_label(int freq, char* out) +{ + if(freq >= 1000000) // >= 1 million KHz (1GHz) + sprintf(out, "%.2f GHz", ((float)freq/1000000) ); + else + sprintf(out, "%.2d MHz", freq/1000); + + debug("Prepared freq label '%s' for freq %d\n",out,freq); +} + +/*********************************************************************** + * Return freq value at index for core, as a string + **********************************************************************/ +char* gf_freqa(int core, int index) +{ + return freqs[core][index]; +} + +/*********************************************************************** + * Return freq value at index for core, as an int + **********************************************************************/ +int gf_freqi(int core, int index) +{ + return atoi(gf_freqa(core, index)); +} + +/*********************************************************************** + * Return total number of frequencies + **********************************************************************/ +int gf_number() +{ + return total_freqs; +} diff --git a/getfreq.h b/getfreq.h new file mode 100644 index 0000000..8df9eca --- /dev/null +++ b/getfreq.h @@ -0,0 +1,31 @@ +/************************************************************************ + * 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 * + * . * + ************************************************************************/ + +#ifndef GETFREQ_H +#define GETFREQ_H + +void gf_init(); +int gf_current(int core); +int gf_available(int core, char* out, int size); +void gf_get_frequency_label(int freq, char* out); +char* gf_freqa(int core, int index); +int gf_freqi(int core, int index); +int gf_number(); + + +#endif diff --git a/getgov.c b/getgov.c new file mode 100644 index 0000000..197df1c --- /dev/null +++ b/getgov.c @@ -0,0 +1,127 @@ +/************************************************************************ + * 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 * + * . * + ************************************************************************/ + +#include "string.h" +#include "getgov.h" +#include "getcore.h" +#include "common.h" + +#include +#include +#include +#include + +/* [CORE][GOVERNOR NUMBER] */ +char governors[999][50][13]; +int total_governors; + +/*********************************************************************** + * Grab all available governors + **********************************************************************/ +void gg_init() +{ + gchar gov_string[500]; + int i = 0; + total_governors = 0; + for(i = 0; i < gc_number(); ++i) + { + memset(gov_string, '\0', sizeof(gov_string) ); + gg_available(i, gov_string, sizeof(gov_string) ); + + // go through every governor in gov_string + gchar* curr = (char*)&gov_string; + gchar* end_of_curr = g_strstr_len(curr, strlen(curr), " "); + while(end_of_curr) + { + memset(governors[i][total_governors], '\0', 13); + memmove(governors[i][total_governors], curr, end_of_curr - curr); + + curr = end_of_curr+1; + end_of_curr = g_strstr_len(curr, strlen(curr), " "); + total_governors++; + } + } +} + + +/*********************************************************************** + * Populate out with current governor for core + **********************************************************************/ +int gg_current(int core, char* out, int size) +{ + FILE* fd; + char path[FILE_PATH_SIZE]; + + sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", core); + + if(!(fd = fopen(path, "r"))) + { + debug("Couldn't open '%s'\n",path); + return -1; + } + + fgets(out, size, fd); + // Chomp + gchar* newline = g_strrstr(out, "\n"); + *newline = '\0'; + + debug("Current gov for core %d is '%s'\n",core,out); + + fclose(fd); + return 0; +} + + +/*********************************************************************** + * Populate out with number of available cores + **********************************************************************/ +int gg_available(int core, char* out, int size) +{ + FILE* fd; + char path[80]; + + sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_available_governors", core); + + if(!(fd = fopen(path, "r"))) + { + debug("Couldn't open '%s'\n",path); + return -1; + } + + fgets(out, size, fd); + + fclose(fd); + return 0; +} + + +/*********************************************************************** + * Return pointer to name of gov + **********************************************************************/ +char* gg_gov(int core, int index) +{ + return governors[core][index]; +} + +/*********************************************************************** + * Return total number of governors + **********************************************************************/ +int gg_number() +{ + return total_governors; +} diff --git a/getgov.h b/getgov.h new file mode 100644 index 0000000..fca341d --- /dev/null +++ b/getgov.h @@ -0,0 +1,28 @@ +/************************************************************************ + * 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 * + * . * + ************************************************************************/ + +#ifndef GETGOV_H +#define GETGOV_H + +void gg_init(); +int gg_current(int core, char* out, int size); +int gg_available(int core, char* out, int size); +char* gg_gov(int core, int index); +int gg_number(); + +#endif diff --git a/lc/fr.po b/lc/fr.po new file mode 100644 index 0000000..fdcecc5 --- /dev/null +++ b/lc/fr.po @@ -0,0 +1,68 @@ +# French translation of strings for trayfreq-archlinux. +# Copyright (C) David Phillips +# This file is distributed under the same license as the trayfreq-archlinux package. +# David Phillips , 2014. +# +msgid "" +msgstr "" +"Project-Id-Version: trayfreq-archlinux 0.5\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2014-05-01 13:02+1200\n" +"PO-Revision-Date: 2014-05-01 13:08+1200\n" +"Last-Translator: David Phillips \n" +"Language-Team: French \n" +"Language: fr\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#: tray.c:192 +#, c-format +msgid "Governor: %s\n" +msgstr "Profil: %s\n" + +#: tray.c:199 +#, c-format +msgid "CPU%i: %s%s" +msgstr "CPU%i: %s%s" + +#: trayfreq.c:69 +msgid "Failed to open config files!\n" +msgstr "Pouvais pas ouvrier les fichiers config !\n" + +#: trayfreq.c:105 +msgid "" +"GTK Error: gtk_init_check returned FALSE.\n" +"Bailing." +msgstr "Erreur: gtk_init_check a retourné FALSE.\n" +"Je part" + +#: bat_tray/bat_tray.c:57 +#, c-format +msgid "Discharging (%d%%)" +msgstr "En décharge (%d%%)" + +#: bat_tray/bat_tray.c:62 +#, c-format +msgid "Charging (%d%%)" +msgstr "En charge (%d%%)" + +#: bat_tray/bat_tray.c:67 +#, c-format +msgid "Fully charged" +msgstr "Complétement chargée" + +#: bat_tray/bat_tray.c:72 +#, c-format +msgid "Unknown status" +msgstr "Statut inconnu" + +#: trayfreq_set/trayfreq_set.c:65 +#, c-format +msgid "FAILED: Couldn't open %s for writing\n" +msgstr "ERREUR: Peut pas écrire à %s\n" + +#: trayfreq_set/trayfreq_set.c:161 +#, c-format +msgid "%s {-f frequency|-g governor} -c core\n" +msgstr "%s {-f fréquence|-g profil} -c core\n" diff --git a/reload.c b/reload.c new file mode 100644 index 0000000..6d31586 --- /dev/null +++ b/reload.c @@ -0,0 +1,45 @@ +/************************************************************************ + * 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 * + * . * + ************************************************************************/ + +#include "reload.h" +#include "bat_tray.h" +#include "tray.h" +#include "trayfreq.h" +#include "defaults.h" +#include "common.h" +#include + + +/*********************************************************************** + * Catches the USR1 sig. Reloads the configuration files and applies any + * new changes/config etc + ************************************************************************/ +void reload_config() +{ + debug("Reloading config\n"); + + // Reload config + config_init(); + + // Hide battery icon if told to + _DEFAULT_SHOW_BATTERY? bat_tray_show() : bat_tray_hide(); + + // Update governor and frequency defaults + debug("Re-init freq tray\n"); + tray_set_defaults(); +} diff --git a/reload.h b/reload.h new file mode 100644 index 0000000..b0e0c4d --- /dev/null +++ b/reload.h @@ -0,0 +1,23 @@ +/************************************************************************ + * 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 * + * . * + ************************************************************************/ + +#ifndef RELOAD_H +#define RELOAD_H + +void reload_config(); +#endif diff --git a/themes/default/cpufreq-0.png b/themes/default/cpufreq-0.png new file mode 100644 index 0000000..9a1b3b4 Binary files /dev/null and b/themes/default/cpufreq-0.png differ diff --git a/themes/default/cpufreq-100.png b/themes/default/cpufreq-100.png new file mode 100644 index 0000000..6b63453 Binary files /dev/null and b/themes/default/cpufreq-100.png differ diff --git a/themes/default/cpufreq-25.png b/themes/default/cpufreq-25.png new file mode 100644 index 0000000..ae2acf6 Binary files /dev/null and b/themes/default/cpufreq-25.png differ diff --git a/themes/default/cpufreq-50.png b/themes/default/cpufreq-50.png new file mode 100644 index 0000000..03ab5e3 Binary files /dev/null and b/themes/default/cpufreq-50.png differ diff --git a/themes/default/cpufreq-75.png b/themes/default/cpufreq-75.png new file mode 100644 index 0000000..49e9342 Binary files /dev/null and b/themes/default/cpufreq-75.png differ diff --git a/themes/default/traybat-0-charging.png b/themes/default/traybat-0-charging.png new file mode 100644 index 0000000..9255e66 Binary files /dev/null and b/themes/default/traybat-0-charging.png differ diff --git a/themes/default/traybat-0.png b/themes/default/traybat-0.png new file mode 100644 index 0000000..2554a6a Binary files /dev/null and b/themes/default/traybat-0.png differ diff --git a/themes/default/traybat-100-charging.png b/themes/default/traybat-100-charging.png new file mode 100644 index 0000000..bae3a43 Binary files /dev/null and b/themes/default/traybat-100-charging.png differ diff --git a/themes/default/traybat-100.png b/themes/default/traybat-100.png new file mode 100644 index 0000000..fe4ba7f Binary files /dev/null and b/themes/default/traybat-100.png differ diff --git a/themes/default/traybat-20-charging.png b/themes/default/traybat-20-charging.png new file mode 100644 index 0000000..aabd778 Binary files /dev/null and b/themes/default/traybat-20-charging.png differ diff --git a/themes/default/traybat-20.png b/themes/default/traybat-20.png new file mode 100644 index 0000000..83e7065 Binary files /dev/null and b/themes/default/traybat-20.png differ diff --git a/themes/default/traybat-40-charging.png b/themes/default/traybat-40-charging.png new file mode 100644 index 0000000..c016a28 Binary files /dev/null and b/themes/default/traybat-40-charging.png differ diff --git a/themes/default/traybat-40.png b/themes/default/traybat-40.png new file mode 100644 index 0000000..3245106 Binary files /dev/null and b/themes/default/traybat-40.png differ diff --git a/themes/default/traybat-60-charging.png b/themes/default/traybat-60-charging.png new file mode 100644 index 0000000..1667458 Binary files /dev/null and b/themes/default/traybat-60-charging.png differ diff --git a/themes/default/traybat-60.png b/themes/default/traybat-60.png new file mode 100644 index 0000000..aa25803 Binary files /dev/null and b/themes/default/traybat-60.png differ diff --git a/themes/default/traybat-80-charging.png b/themes/default/traybat-80-charging.png new file mode 100644 index 0000000..b9c6991 Binary files /dev/null and b/themes/default/traybat-80-charging.png differ diff --git a/themes/default/traybat-80.png b/themes/default/traybat-80.png new file mode 100644 index 0000000..e649be0 Binary files /dev/null and b/themes/default/traybat-80.png differ diff --git a/themes/default/traybat-charged.png b/themes/default/traybat-charged.png new file mode 100644 index 0000000..1e61940 Binary files /dev/null and b/themes/default/traybat-charged.png differ diff --git a/tray.c b/tray.c new file mode 100644 index 0000000..38a55ea --- /dev/null +++ b/tray.c @@ -0,0 +1,406 @@ +/************************************************************************ + * 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 * + * . * + ************************************************************************/ + +#include "tray.h" + +#include "getfreq.h" +#include "getcore.h" +#include "getgov.h" +#include "trayfreq_set_interface.h" +#include "bat_tray.h" +#include "defaults.h" +#include "common.h" + +#include +#include +#include +#include +#include +#include + + +#define TOOLTIP_TEXT_SIZE 500 + +GtkStatusIcon* tray; +char tooltip_text[TOOLTIP_TEXT_SIZE]; + +GtkWidget* menu; +GSList* menu_items; +GtkWidget* checked_menu_item; + + +/*********************************************************************** + * Handler for when freq is chosen + **********************************************************************/ +static void freq_menu_item_toggled(GtkCheckMenuItem* item, gpointer data) +{ + debug("[checking in]\n"); + if(gtk_check_menu_item_get_active(item)) + { + checked_menu_item = GTK_WIDGET(item); + gint freq = GPOINTER_TO_INT(data); + 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) +{ + debug("[checking in]\n"); + if(gtk_check_menu_item_get_active(item)) + { + checked_menu_item = GTK_WIDGET(item); + char* gov = (char*)data; + int i = 0; + for(i = 0; i < gc_number(); ++i) + si_gov(gov, i); + } +} +/* +static gboolean governor_exists(gchar* governor) +{ + int i = 0; + for(i = 0; i < gf_number(); ++i) + { + if(g_strcmp0(governor, gg_gov(0, i)) == 0) + return TRUE; + } + return FALSE; +}*/ + + +/*********************************************************************** + * Destroy a menu item + **********************************************************************/ +static void remove_menu_item(GtkWidget* menu_item, gpointer data) +{ + debug("Destroying menu_item\n"); + gtk_widget_destroy(menu_item); +} + + +/*********************************************************************** + * Remove all items in menu + **********************************************************************/ +static void tray_clear_menu() +{ + debug("Clearing the menu\n"); + 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() +{ + debug("Spawning new 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[20]; + 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) + { + memset(label, '\0', 20); + gf_get_frequency_label(gf_freqi(0, i), label); + debug("Got freq label '%s', i=%d\n",label,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) + { + debug("This freq is current freq, ticking radio button\n"); + gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE); + } + + debug("Setting connection/callback\n"); + g_signal_connect(G_OBJECT(item), "toggled", GTK_SIGNAL_FUNC(freq_menu_item_toggled), GINT_TO_POINTER(gf_freqi(0, i))); + + debug("Adding item to menu\n"); + gtk_menu_shell_append(GTK_MENU_SHELL(menu), item); + } + + // Add a seperator + debug("Adding separator\n"); + 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) + { + debug("Gov is userspace, not adding\n"); + 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) + { + debug("Governor is current one, ticking radio button\n"); + gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),