diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | bat_tray.c | 72 | ||||
-rw-r--r-- | bool.h | 31 | ||||
-rw-r--r-- | common.c | 15 | ||||
-rw-r--r-- | common.h | 4 | ||||
-rw-r--r-- | defaults.c | 4 | ||||
-rw-r--r-- | defaults.h | 2 | ||||
-rw-r--r-- | getcore.c | 24 | ||||
-rw-r--r-- | getfreq.c | 53 | ||||
-rw-r--r-- | getgov.c | 49 | ||||
-rw-r--r-- | reload.c | 19 | ||||
-rw-r--r-- | tray.c | 64 | ||||
-rw-r--r-- | tray.h | 2 | ||||
-rw-r--r-- | trayfreq.c | 20 |
14 files changed, 222 insertions, 139 deletions
@@ -35,7 +35,6 @@ EXTRA_CFLAGS+= -DPREFIX=\"$(PREFIX)\" \ DEPS = bat_tray.h \ - bool.h \ common.h \ config_file.h \ defaults.h \ @@ -61,7 +60,6 @@ CFLAGS = -I/usr/include/gtk-2.0 \ LDFLAGS = -lgtk-3 \ -lgobject-2.0 \ -lglib-2.0 - ######################################################################## # Make entire suite all: trayfreq trayfreq-set lang trayfreq.conf @@ -27,9 +27,9 @@ static GtkStatusIcon* tray; -int _BAT_NUM; -char CHARGE_VALUE_PATH[512]; -char CHARGE_STATE_PATH[512]; +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 @@ -41,7 +41,7 @@ int get_bat_percent() #define TOOLTIP_TEXT_SIZE 128 -gchar tooltip_text[TOOLTIP_TEXT_SIZE]; +char tooltip_text[TOOLTIP_TEXT_SIZE]; /*********************************************************************** @@ -49,8 +49,8 @@ gchar tooltip_text[TOOLTIP_TEXT_SIZE]; **********************************************************************/ static gboolean update_tooltip(GtkStatusIcon* status_icon,gint x,gint y,gboolean keyboard_mode,GtkTooltip* tooltip,gpointer data) { - gchar msg[TOOLTIP_TEXT_SIZE]; - //memset(msg,0,TOOLTIP_TEXT_SIZE); + char msg[TOOLTIP_TEXT_SIZE]; + switch(get_battery_state()) { case STATE_DISCHARGING: @@ -77,45 +77,32 @@ static gboolean update_tooltip(GtkStatusIcon* status_icon,gint x,gint y,gboolean debug("Setting tooltip text to '%s'\n",msg); gtk_tooltip_set_text(tooltip, msg); - return TRUE; + return true; } /*********************************************************************** * Updates the battery tray icon based upon battery percent **********************************************************************/ -static gboolean update_icon(gpointer user_data) +gboolean update_icon(gpointer user_data) { - gchar* icon_file; - unsigned int percent = get_bat_percent(); - unsigned int adjusted_percent; - gchar adjusted_percent_string[4]; - - - // TO DO: do this with rounding and divisio etc - 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; - - debug("Rounded/adjusted percentage: %d\n",adjusted_percent); - sprintf(adjusted_percent_string, "%d", adjusted_percent); + 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-", adjusted_percent_string, ".png", NULL); + icon_file = g_strconcat(_DEFAULT_THEME, "/traybat-", percent_string, ".png", NULL); break; + case STATE_CHARGING: - icon_file = g_strconcat(_DEFAULT_THEME, "/traybat-", adjusted_percent_string, "-charging.png", NULL); + icon_file = g_strconcat(_DEFAULT_THEME, "/traybat-", percent_string, "-charging.png", NULL); break; default: @@ -125,24 +112,26 @@ static gboolean update_icon(gpointer user_data) debug("Setting tray icon to '%s'\n",icon_file); gtk_status_icon_set_from_file(tray, icon_file); - return TRUE; + 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(); + 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); + 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(); - gchar* icon_file = g_strconcat(_DEFAULT_THEME, "/traybat-charged.png", NULL); + 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); @@ -150,12 +139,19 @@ void bat_tray_init() } +/*********************************************************************** + * 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"); @@ -1,31 +0,0 @@ -/************************************************************************ - * 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/>. * - ************************************************************************/ -#ifndef BOOL_H -#define BOOL_H - - #include <stdbool.h> - - #ifndef TRUE - #define TRUE true - #endif - - #ifndef FALSE - #define FALSE false - #endif - -#endif @@ -23,7 +23,7 @@ #include <string.h> /* lots of functions */ /*********************************************************************** - * Gets integer value from first line in file + * Return integer value from first line in file **********************************************************************/ int get_int_value_from_file(const char* filename) { @@ -34,7 +34,7 @@ int get_int_value_from_file(const char* filename) if(!(fd = fopen(filename, "r"))) return -1; - if (fgets(buffer, 100, fd)) + if (fgets(buffer, sizeof(buffer), fd)) value = get_int(buffer); fclose(fd); @@ -51,18 +51,18 @@ bool file_has_line(const char *filename, const char *line) char buffer[512]; if (!(fd = fopen(filename, "r"))) - return FALSE; + return false; while (fgets(buffer, sizeof(buffer), fd) != NULL) { if(strstr(buffer, line) != NULL) { fclose(fd); - return TRUE; + return true; } } fclose(fd); - return FALSE; + return false; } @@ -76,9 +76,6 @@ int get_int(const char* string) first_num = strpbrk(string, "0123456789"); debug("first_num: '%s'\n",first_num); - if(first_num) - return atoi(first_num); - debug("first_num was 0, returning 1\n"); - return 1; + return atoi(first_num); } @@ -19,7 +19,7 @@ #ifndef COMMON_H #define COMMON_H -#include "bool.h" +#include <stdbool.h> int get_int_value_from_file(const char* filename); int get_int(const char* string); @@ -44,5 +44,5 @@ bool file_has_line(const char *filename, const char *line); #endif #define info(...) printf("INFO: "__FILE__":"STR_LINE" --- "__VA_ARGS__) - +#define FILE_PATH_SIZE 2048 #endif @@ -25,7 +25,7 @@ char* _DEFAULT_FREQ; char* _DEFAULT_PROG; char* _DEFAULT_BAT_GOV; char* _DEFAULT_AC_GOV; -bool _DEFAULT_SHOW_BATTERY = TRUE; +bool _DEFAULT_SHOW_BATTERY = true; char _DEFAULT_THEME[2048]; // to do : make dynamic with malloc void defaults_init() @@ -35,6 +35,6 @@ void defaults_init() _DEFAULT_PROG = NULL; _DEFAULT_BAT_GOV = NULL; _DEFAULT_AC_GOV = NULL; - _DEFAULT_SHOW_BATTERY = TRUE; + _DEFAULT_SHOW_BATTERY = true; sprintf (_DEFAULT_THEME, SHAREDIR"/trayfreq/themes/default"); } @@ -19,7 +19,7 @@ #ifndef DEFAULTS_H #define DEFAULTS_H -#include "bool.h" /* boolean types */ +#include <stdbool.h> /* boolean types */ #include <stdlib.h> /* NULL */ char* _DEFAULT_GOV; @@ -24,9 +24,13 @@ #include <stdlib.h> #include <glib.h> -unsigned int NUMBER_OF_CORES; +unsigned int cores; -static gboolean core_exists(unsigned int core) + +/*********************************************************************** + * Return true/false if specified core num exists + **********************************************************************/ +bool core_exists(unsigned int core) { FILE* fd; char path[80]; @@ -38,14 +42,22 @@ static gboolean core_exists(unsigned int core) return (gboolean)(fd = fopen(path, "r")); } + +/*********************************************************************** + * Initialise surrounding variables + **********************************************************************/ void gc_init() { - NUMBER_OF_CORES = 0; - while(core_exists(++NUMBER_OF_CORES)); - debug("Found %d cores\n",NUMBER_OF_CORES); + cores = 0; + while(core_exists(++cores)); + debug("Found %d cores\n",cores); } + +/*********************************************************************** + * Return number of cores + **********************************************************************/ int gc_number() { - return NUMBER_OF_CORES; + return cores; } @@ -26,45 +26,50 @@ #include <glib.h> /* [CORE][FREQUENCY NUMBER] */ -char AVAILABLE_FREQUENCIES[999][50][13]; -int NUMBER_OF_AVAILABLE_FREQUENCIES; +char freqs[999][50][13]; +int total_freqs; + +/*********************************************************************** + * Initialise surrounding variables, get available freqs etc + **********************************************************************/ void gf_init() { - // TO DO : get rid of magic constants gchar freq_string[500]; int i = 0; - int j = 0; - for(i = 0; i < gc_number(); ++i) + for(i = 0; i < gc_number(); i++) { - memset(freq_string, '\0', 500); + memset(freq_string, '\0', sizeof(freq_string) ); // Get available governor freqs. If no governor, try next cpu - if (gf_available(i, freq_string, 500) == -1) + if (gf_available(i, freq_string, sizeof(freq_string) ) == -1) { debug("Couldn't find gov on core %d\n",i); continue; } - /* go through every frequency in freq_string */ - j = 0; + // 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) { - memset(AVAILABLE_FREQUENCIES[i][j], '\0', 13); - memmove(AVAILABLE_FREQUENCIES[i][j], curr, end_of_curr - 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), " "); - ++j; + total_freqs++; } } - NUMBER_OF_AVAILABLE_FREQUENCIES = j; - debug("Found %d frequencies\n",j); + debug("Found %d frequencies\n",total_freqs); } +/*********************************************************************** + * Return current frequency for core + **********************************************************************/ int gf_current(int core) { FILE* fd; @@ -88,6 +93,10 @@ int gf_current(int core) return freq; } + +/*********************************************************************** + * Populate out with available frequencies for core + **********************************************************************/ int gf_available(int core, char* out, int size) { FILE* fd; @@ -107,6 +116,9 @@ int gf_available(int core, char* out, int size) 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) @@ -117,17 +129,26 @@ void gf_get_frequency_label(int freq, char* out) 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 AVAILABLE_FREQUENCIES[core][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 NUMBER_OF_AVAILABLE_FREQUENCIES; + return total_freqs; } @@ -27,45 +27,47 @@ #include <glib.h> /* [CORE][GOVERNOR NUMBER] */ -gchar AVAILABLE_GOVERNORS[999][50][13]; -gint NUMBER_OF_AVAILABLE_GOVERNORS; +char governors[999][50][13]; +int total_governors; +/*********************************************************************** + * Grab all available governors + **********************************************************************/ void gg_init() { gchar gov_string[500]; int i = 0; - int j = 0; + total_governors = 0; for(i = 0; i < gc_number(); ++i) { - memset(gov_string, '\0', 500); - gg_available(i, gov_string, 500); + memset(gov_string, '\0', sizeof(gov_string) ); + gg_available(i, gov_string, sizeof(gov_string) ); // go through every governor in gov_string - j = 0; - gchar* curr = &gov_string[0]; + gchar* curr = (char*)&gov_string; gchar* end_of_curr = g_strstr_len(curr, strlen(curr), " "); while(end_of_curr) { - memset(AVAILABLE_GOVERNORS[i][j], '\0', 13); - memmove(AVAILABLE_GOVERNORS[i][j], curr, end_of_curr - 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), " "); - ++j; + total_governors++; } } - NUMBER_OF_AVAILABLE_GOVERNORS = j; } + +/*********************************************************************** + * Populate out with current governor for core + **********************************************************************/ int gg_current(int core, char* out, int size) { FILE* fd; - char path[80]; - char corestr[4]; - - sprintf(corestr, "%i", core); + char path[FILE_PATH_SIZE]; - sprintf(path, "/sys/devices/system/cpu/cpu%s/cpufreq/scaling_governor", corestr); + sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", core); if(!(fd = fopen(path, "r"))) { @@ -84,6 +86,10 @@ int gg_current(int core, char* out, int size) return 0; } + +/*********************************************************************** + * Populate out with number of available cores + **********************************************************************/ int gg_available(int core, char* out, int size) { FILE* fd; @@ -103,12 +109,19 @@ int gg_available(int core, char* out, int size) return 0; } + +/*********************************************************************** + * Return pointer to name of gov + **********************************************************************/ char* gg_gov(int core, int index) { - return AVAILABLE_GOVERNORS[core][index]; + return governors[core][index]; } +/*********************************************************************** + * Return total number of governors + **********************************************************************/ int gg_number() { - return NUMBER_OF_AVAILABLE_GOVERNORS; + return total_governors; } @@ -24,17 +24,22 @@ #include "common.h" #include <stdio.h> + +/*********************************************************************** + * 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(); - if (_DEFAULT_SHOW_BATTERY) - { - //bat_tray_hide(); - bat_tray_show(); - } else { - bat_tray_hide(); - } + + // 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(); } @@ -43,6 +43,10 @@ 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"); @@ -56,6 +60,10 @@ static void freq_menu_item_toggled(GtkCheckMenuItem* item, gpointer data) } } + +/*********************************************************************** + * Handler for when governor is chosen + **********************************************************************/ static void gov_menu_item_toggled(GtkCheckMenuItem* item, gpointer data) { debug("[checking in]\n"); @@ -80,12 +88,20 @@ static gboolean governor_exists(gchar* governor) 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"); @@ -94,12 +110,19 @@ static void tray_clear_menu() 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(); @@ -170,6 +193,9 @@ static void tray_generate_menu() gtk_widget_show_all(menu); } +/*********************************************************************** + * Refresh the tooltip message + **********************************************************************/ static gboolean update_tooltip(GtkStatusIcon* status_icon,gint x,gint y,gboolean keyboard_mode,GtkTooltip* tooltip,gpointer data) { char msg[TOOLTIP_TEXT_SIZE]; @@ -198,6 +224,10 @@ static gboolean update_tooltip(GtkStatusIcon* status_icon,gint x,gint y,gboolean return TRUE; } + +/*********************************************************************** + * Handler for spawning the popup/context menu + **********************************************************************/ static void popup_menu(GtkStatusIcon* statuc_icon,guint button,guint activate_time,gpointer data) { debug("Spawning popup menu\n"); @@ -205,6 +235,10 @@ static void popup_menu(GtkStatusIcon* statuc_icon,guint button,guint activate_ti gtk_menu_popup(GTK_MENU(menu),NULL,NULL,gtk_status_icon_position_menu,tray,button,activate_time); } + +/*********************************************************************** + * Update the freq/gov tray icon + **********************************************************************/ static gboolean update_icon(gpointer user_data) { int i; @@ -236,6 +270,10 @@ static gboolean update_icon(gpointer user_data) return TRUE; } + +/*********************************************************************** + * Apply default gov/freq for current situation + **********************************************************************/ void tray_set_defaults() { // Set defaults @@ -258,6 +296,10 @@ void tray_set_defaults() } + +/*********************************************************************** + * Start the tray icon thingy + **********************************************************************/ void tray_init() { tray_set_defaults(); @@ -277,6 +319,10 @@ void tray_init() tray_init_menu(); } + +/*********************************************************************** + * Set the tooltip message + **********************************************************************/ void tray_set_tooltip(const char* msg) { debug("Setting up toolip var with text '%s'\n",msg); @@ -284,6 +330,9 @@ void tray_set_tooltip(const char* msg) memmove(tooltip_text, msg, strlen(msg)); } +/********************************************************************** + * Set icon based on current freq/governor + **********************************************************************/ void tray_update_icon_percent() { gulong max_frequency = gf_freqi(0, 0); @@ -294,6 +343,7 @@ void tray_update_icon_percent() adjusted_percent = 0; } else { // Percentages need to be {25,50,75,100}. Round to one of these numbers. + // TO DO: round instead of lots of ifs gint percent = (gf_current(0) * 100)/max_frequency; if(percent == 100) { adjusted_percent = 100; @@ -320,23 +370,37 @@ void tray_update_icon_percent() g_free(file); } + +/*********************************************************************** + * Show the tray + **********************************************************************/ void tray_show() { debug("Showing tray\n"); gtk_status_icon_set_visible(tray, TRUE); } +/*********************************************************************** + * Hide the tray + **********************************************************************/ void tray_hide() { debug("Hiding tray"); 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); @@ -19,7 +19,7 @@ #ifndef TRAY_H #define TRAY_H -#include "bool.h" +#include <stdbool.h> void tray_set_defaults(); void tray_init(); @@ -29,13 +29,17 @@ #include "common.h" #include <gtk/gtk.h> -#include <stdlib.h> -#include <unistd.h> -#include <stdio.h> -#include <libintl.h> -#include <locale.h> -#include <string.h> +#include <unistd.h> // getuid, getgid +#include <stdio.h> // printf, FILE, fopen, etc +#include <string.h> // strlen + +#include <libintl.h> // gettext +#include <locale.h> // LC_ALL etc + +/*********************************************************************** + * Main + **********************************************************************/ int main(int argc, char** argv) { setlocale(LC_ALL, ""); @@ -81,6 +85,10 @@ int main(int argc, char** argv) return 0; } + +/*********************************************************************** + * Load config + **********************************************************************/ void config_init() { struct config_file config; |