aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid <dbphillipsnz@gmail.com>2014-09-18 22:08:41 +1200
committerDavid <dbphillipsnz@gmail.com>2014-09-18 22:08:41 +1200
commit2206b7f95651f7cc329efe19c496eb16ed077ae5 (patch)
treebea7abb1b62abf8bf293fa968448436c387c14d0
parent1b1d3f3ea1e5efa95fd1ef18db9e0af517393f95 (diff)
downloadparamano-2206b7f95651f7cc329efe19c496eb16ed077ae5.tar.xz
Added new files
-rw-r--r--paramano.c152
-rw-r--r--paramano.conf.src41
-rw-r--r--paramano.desktop7
-rw-r--r--paramano.h24
-rw-r--r--paramano_set.c160
-rw-r--r--paramano_set_interface.c50
-rw-r--r--paramano_set_interface.h25
7 files changed, 459 insertions, 0 deletions
diff --git a/paramano.c b/paramano.c
new file mode 100644
index 0000000..a048814
--- /dev/null
+++ b/paramano.c
@@ -0,0 +1,152 @@
+/************************************************************************
+ * 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/>. *
+ ************************************************************************/
+
+#include "paramano.h"
+
+#include "tray.h"
+#include "bat_tray.h"
+#include "getfreq.h"
+#include "getcore.h"
+#include "getgov.h"
+#include "config_file.h"
+#include "reload.h"
+#include "defaults.h"
+#include "common.h"
+
+#include <gtk/gtk.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, "");
+ bindtextdomain("trayfreq",LOCALEDIR);
+ textdomain("trayfreq");
+ debug("Set gettext up\n");
+
+ if(!gtk_init_check(&argc, &argv))
+ {
+ debug("Couldn't start gtk\n");
+ g_error( _("GTK Error: gtk_init_check returned FALSE.\nBailing.") );
+ return 1;
+ }
+
+ struct sigaction sig_act;
+ sig_act.sa_handler = reload_config;
+ sig_act.sa_flags = 0;
+ sigemptyset(&sig_act.sa_mask);
+
+ if (sigaction(SIGUSR1, &sig_act, NULL) == -1)
+ {
+ debug("Couldn't set sigaction for SIGUSR1\n");
+ }
+ config_init();
+ gc_init();
+ gg_init();
+ gf_init();
+ tray_init();
+ tray_show();
+
+ // Show battery tray only if we're supposed to
+ if(_DEFAULT_SHOW_BATTERY)
+ {
+ debug("Showing battery info this time around\n");
+ bat_tray_init();
+ bat_tray_show();
+ }
+
+ debug("Passing control to Gtk\n");
+
+ gtk_main();
+ debug("Exiting main()\n");
+ return 0;
+}
+
+
+/***********************************************************************
+ * Load config
+ **********************************************************************/
+void config_init()
+{
+ struct config_file config;
+ FILE* fd = NULL;
+ gboolean home_config_exists;
+ config.key_file = NULL;
+
+ config.file_name = g_strconcat(getenv("HOME"), "/.paramano.conf", NULL);
+
+
+ // Check if ~/.paramano.conf exists
+ if( (fd = fopen(config.file_name, "r")) )
+ {
+ // If file exists, close it, set param to TRUE
+ debug("Found '%s'\n",config.file_name);
+ fclose(fd);
+ home_config_exists = TRUE;
+ } else {
+ // If file didn't exist, it's not open (don't close it), free filename var, set param to FALSE
+ debug("Didn't find '%s'\n",config.file_name);
+ g_free(config.file_name);
+ home_config_exists = FALSE;
+ }
+
+ if(!home_config_exists)
+ config.file_name = g_strconcat(PARAMANO_CONF, NULL);
+
+ gboolean success = config_open(&config);
+ if(!success)
+ {
+ debug("Couldn't open '%s' for reading\n",config.file_name);
+ g_warning(_("Failed to open config files!\n"));
+ return;
+ }
+
+ // Reset defaults to default values
+ defaults_init();
+
+ _DEFAULT_GOV = config_get_key(&config, "governor", "default");
+ _DEFAULT_FREQ = config_get_key(&config, "frequency", "default");
+ _DEFAULT_BAT_GOV = config_get_key(&config, "battery", "governor");
+ _DEFAULT_AC_GOV = config_get_key(&config, "ac", "governor");
+
+ char* temp = config_get_key(&config, "battery", "show");
+ if (temp)
+ _DEFAULT_SHOW_BATTERY = ( temp[0] == '1' );
+
+ info("UID: %d GID: %d\n", getuid(), getgid());
+
+ temp = config_get_key(&config, "extra", "theme");
+ if (temp && strlen(temp) < sizeof(_DEFAULT_THEME) )
+ {
+ sprintf(_DEFAULT_THEME, "%s", temp);
+ }
+
+ debug("Using theme %s\n",_DEFAULT_THEME);
+
+ g_free(config.file_name);
+ config_close(&config);
+}
+
diff --git a/paramano.conf.src b/paramano.conf.src
new file mode 100644
index 0000000..2e3b3e7
--- /dev/null
+++ b/paramano.conf.src
@@ -0,0 +1,41 @@
+#
+# Begin PARAMANO_CONF
+#
+# Template/default config file for Paramano
+#
+
+[battery]
+## Uncomment to hide battery icon -- useful for desktop PCs
+## Set to anything (or leave unset/commented out) to show battery icon
+# show=0
+
+## Name of the governor to use for all cores when on battery power
+## Get a space-separated list of available governors from
+## /sys/devices/system/cpu/cpuX/cpufreq/scaling_available_governors
+# governor=powersave
+
+[ac]
+## Governor to use for all core while on AC power
+## Often set to a performance or non-power-saving governor
+# governor=ondemand
+
+[governor]
+## Governor to set all cores to when Paramano starts
+# default=ondemand
+
+
+[frequency]
+## The frequency to set the CPU to when Paramano starts
+## Get a space-separated list of available frequencies from
+## /sys/devices/system/cpu/cpuX/cpufreq/scaling_available_frequencies
+# default=800000
+
+
+[extra]
+## If you're arty, you can put together your own theme and point Paramano to it
+# theme=SHAREDIR/paramano/themes/some-theme/
+
+
+#
+# End PARAMANO_CONF
+#
diff --git a/paramano.desktop b/paramano.desktop
new file mode 100644
index 0000000..0905549
--- /dev/null
+++ b/paramano.desktop
@@ -0,0 +1,7 @@
+[Desktop Entry]
+Version=1.0
+Encoding=UTF-8
+Name=trayfreq
+GenericName=trayfreq
+Exec=/usr/bin/trayfreq
+Type=Application
diff --git a/paramano.h b/paramano.h
new file mode 100644
index 0000000..af1c8b0
--- /dev/null
+++ b/paramano.h
@@ -0,0 +1,24 @@
+/************************************************************************
+ * 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 TRAYFREQ_H
+#define TRAYFREQ_H
+
+void config_init();
+
+#endif
diff --git a/paramano_set.c b/paramano_set.c
new file mode 100644
index 0000000..02b0eef
--- /dev/null
+++ b/paramano_set.c
@@ -0,0 +1,160 @@
+/************************************************************************
+ * 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/>. *
+ ************************************************************************/
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <libintl.h>
+#include <locale.h>
+
+#include "getfreq.h"
+#include "getcore.h"
+#include "common.h"
+
+#define FILE_PATH_STRING_SIZE 100
+
+#define ARG_CORE 0x1
+#define ARG_GOV 0x2
+#define ARG_FREQ 0x4
+typedef struct {
+ char present;
+ char *core;
+ char *governor;
+ char *frequency;
+} argument_summary;
+
+char write_str_to_file(const char *file, const char *data, const char *core)
+{
+ FILE *fd;
+ char file_path[FILE_PATH_STRING_SIZE];
+
+ // Prepare file path
+ memset(file_path, '\0', sizeof(file_path));
+ sprintf(file_path, "/sys/devices/system/cpu/cpu%d/cpufreq/%s", atoi(core), file );
+
+ // Try to open file and write data to it
+ if ( (fd = fopen(file_path, "w")) != NULL )
+ {
+ debug("Writing '%s' to '%s'\n",data,file_path);
+ fprintf(fd, data);
+ fclose(fd);
+ return 1;
+ }
+
+ // Fallthrough: File couldn't be opened for writing
+ fprintf(stderr, _("FAILED: Couldn't open %s for writing\n") , file_path);
+ return 0;
+}
+
+
+#define set_freq_max(freq,core) write_str_to_file("scaling_max_freq",freq,core)
+#define set_freq_min(freq,core) write_str_to_file("scaling_min_freq",freq,core)
+#define set_speed(freq,core) write_str_to_file("scaling_setspeed",freq,core)
+#define set_gov(gov,core) write_str_to_file("scaling_governor",gov,core)
+
+void get_argument_summary(int argc, char **argv, argument_summary *argsum)
+{
+ int arg;
+
+ // Check for -{c,g,f} xyz
+ for ( arg = 1; arg < argc-1; arg+=2 )
+ {
+ // Can't have empty argument
+ if ( strlen(argv[arg+1]) != 0 )
+ {
+ if ( strcmp(argv[arg], "-c") == 0 )
+ {
+ // Found -c with an arg
+ debug("Found -c with arg '%s'\n",argv[arg+1]);
+ argsum->present |= ARG_CORE;
+ argsum->core = (char*)(argv[arg+1]);
+ continue;
+ }
+
+ if ( strcmp(argv[arg], "-f") == 0 )
+ {
+ // Found -f with an arg
+ debug("Found -f with arg '%s'\n",argv[arg+1]);
+ argsum->present |= ARG_FREQ;
+ argsum->frequency = (char*)(argv[arg+1]);
+ continue;
+ }
+
+ if ( strcmp(argv[arg], "-g") == 0 )
+ {
+ // Found -g with an arg
+ debug("Found -g with arg '%s'\n",argv[arg+1]);
+ argsum->present |= ARG_GOV;
+ argsum->governor = (char*)(argv[arg+1]);
+ //continue;
+ }
+ }
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ setlocale(LC_ALL,"");
+ bindtextdomain("trayfreq",LOCALEDIR);
+ textdomain("trayfreq");
+
+ debug("Set gettext up\n");
+
+ gc_init();
+ gf_init();
+
+ argument_summary args;
+ memset(&args, 0, sizeof(args));
+
+ // If unusual number of args, give up now
+ if (argc == 5)
+ {
+
+ debug ("Checking UID\n");
+ if (getuid() != 0)
+ fprintf(stderr,"Warning: running as UID %d, not 0\n",getuid() );
+
+ get_argument_summary(argc, argv, &args);
+
+ debug("Correct number of command line arguments\n");
+ debug("-c: %s -g: %s -f: %s\n", (args.present | ARG_CORE )? "Yes":"No",
+ (args.present | ARG_GOV )? "Yes":"No",
+ (args.present | ARG_FREQ )? "Yes":"No" );
+ debug("Core: %s\n",args.core);
+ debug("Gov : %s\n",args.governor);
+ debug("Freq: %s\n",args.frequency);
+
+ if ( args.present == ( ARG_CORE | ARG_GOV ) )
+ {
+ debug("Changing governor\n");
+ return set_gov(args.governor , args.core);
+ }
+
+ if ( args.present == ( ARG_CORE | ARG_FREQ ) )
+ {
+ debug("Changing frequency\n");
+ return set_gov("userspace", args.core) | set_speed(args.frequency, args.core);
+ }
+ }
+ // Fall through to here if no valid argument combination
+ debug("Fell through, showing command usage\n");
+ fprintf(stderr, _("%s {-f frequency|-g governor} -c core\n"), argv[0] );
+ return 1;
+}
diff --git a/paramano_set_interface.c b/paramano_set_interface.c
new file mode 100644
index 0000000..b7d1048
--- /dev/null
+++ b/paramano_set_interface.c
@@ -0,0 +1,50 @@
+/************************************************************************
+ * 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/>. *
+ ************************************************************************/
+
+#include "paramano_set_interface.h"
+
+#include "common.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+void si_gov(char* gov, int core)
+{
+ char cmd[1024];
+ if (getuid() != ROOT_UID)
+ {
+ sprintf(cmd, SUDO" -n "PARAMANO_SET" -g %s -c %i &",gov,core);
+ } else {
+ sprintf(cmd, PARAMANO_SET" -g %s -c %i",gov,core);
+ }
+ info("Running '%s'\n",cmd);
+ system(cmd);
+}
+
+void si_freq(int freq, int core)
+{
+ char cmd[1024];
+ if (getuid() != ROOT_UID)
+ {
+ sprintf(cmd, SUDO" -n "PARAMANO_SET" -f %i -c %i &",freq,core);
+ } else {
+ sprintf(cmd, PARAMANO_SET" -f %i -c %i",freq,core);
+ }
+ info("Running '%s'\n",cmd);
+ system(cmd);
+}
diff --git a/paramano_set_interface.h b/paramano_set_interface.h
new file mode 100644
index 0000000..bdbf652
--- /dev/null
+++ b/paramano_set_interface.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 *
+ * <http://www.gnu.org/licenses/>. *
+ ************************************************************************/
+
+#ifndef TRAYFREQ_SET_INTERFACE_H
+#define TRAYFREQ_SET_INTERFACE_H
+
+void si_gov(char* gov, int core);
+void si_freq(int freq, int core);
+
+#endif