[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[E-devel] battery-module: run an command on alarm



Hi,
i have written a patch with which the battery-module can run an command
on alarm.
E.g when the battery drops under 10 min remaining time then you
can initiate the shutdown of the system.

the command is currently executet via the system-fn call. If it is
there a better methode to do it then let me know it.

Regards

Stephan Wezel

-- 
Ein Ring, sie zu knechten, sie alle zu finden,
Ins Dunkel zu treiben und ewig zu binden
Im Lande Mordor, wo die Schatten drohn.
? battery_run_command_on_alarm.patch
Index: e_mod_config.c
===================================================================
RCS file: /var/cvs/e/e17/apps/e/src/modules/battery/e_mod_config.c,v
retrieving revision 1.17
diff -u -r1.17 e_mod_config.c
--- e_mod_config.c	20 Apr 2006 11:30:24 -0000	1.17
+++ e_mod_config.c	21 Jun 2006 22:09:51 -0000
@@ -6,6 +6,9 @@
    int show_alert;   
    double poll_time;   
    int alarm_time;
+   char *command_on_critical;
+   int command_on_critical_time;
+   int run_command;
 };
 
 /* Protos */
@@ -15,6 +18,7 @@
 static int           _basic_apply_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
 static Evas_Object   *_advanced_create_widgets(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata);
 static int           _advanced_apply_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
+static void          onRunCommandCheckChange(void *data, Evas_Object *obj);
 
 void
 _config_battery_module(void) 
@@ -47,6 +51,13 @@
      cfdata->show_alert = 1;
    else 
      cfdata->show_alert = 0;
+   cfdata->command_on_critical = strdup(battery_config->command_on_critical);
+   cfdata->command_on_critical_time = battery_config->command_on_critical_time;
+   if(cfdata->command_on_critical_time > 0)
+       cfdata->run_command = 1;
+   else
+       cfdata->run_command = 0;
+   
 }
 
 static void *
@@ -96,7 +107,7 @@
 static Evas_Object *
 _advanced_create_widgets(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata) 
 {
-   Evas_Object *o, *of, *ob;
+   Evas_Object *o, *of, *ob, *run_command_check, *run_command_entry;
    
    /* Use Sliders for both cfg options */
    o = e_widget_list_add(evas, 0, 0);
@@ -117,10 +128,38 @@
    ob = e_widget_slider_add(evas, 1, 0, _("%1.0f minutes"), 1, 60, 1, 0, NULL, &(cfdata->alarm_time), 200);
    e_widget_frametable_object_append(of, ob, 0, 5, 1, 1, 1, 0, 1, 0);
 
+   run_command_check = e_widget_check_add(evas, _("Run Command on Alert"), &(cfdata->run_command));
+   if (cfdata->run_command)
+      e_widget_check_checked_set(run_command_check, 1);
+   e_widget_frametable_object_append(of, run_command_check, 0, 6, 1, 1, 1, 0, 1, 1);
+   
+   ob = e_widget_label_add(evas, _("Run Command when battery is down to:"));
+   e_widget_frametable_object_append(of, ob, 0, 7, 1, 1, 1, 0, 1, 1);
+   
+   ob = e_widget_slider_add(evas, 1, 0, _("%1.0f minutes"), 1, 60*3, 1, 0, NULL, &(cfdata->command_on_critical_time), 200);
+   e_widget_frametable_object_append(of, ob, 0, 8, 1, 1, 1, 0, 1, 0);
+   
+   ob = e_widget_label_add(evas, _("Command to run:"));
+   e_widget_frametable_object_append(of, ob, 0, 9, 1, 1, 1, 0, 1, 1);
+   
+   run_command_entry = e_widget_entry_add(evas, &cfdata->command_on_critical);
+   e_widget_on_change_hook_set(run_command_check, onRunCommandCheckChange, run_command_entry);
+   e_widget_disabled_set(run_command_entry, !cfdata->run_command);
+   e_widget_min_size_set(run_command_entry, 150, 1);
+   e_widget_frametable_object_append(of, run_command_entry, 0, 10, 1, 1, 1, 0, 1, 1);
+   
    e_widget_list_object_append(o, of, 1, 1, 0.5);
    return o;
 }
 
+static void
+onRunCommandCheckChange(void *data, Evas_Object *obj)
+{
+   int checked = e_widget_check_checked_get(obj);
+
+   e_widget_disabled_set(data, !checked);
+}
+
 static int 
 _advanced_apply_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata) 
 {
@@ -130,6 +169,15 @@
      battery_config->alarm = cfdata->alarm_time;
    else 
      battery_config->alarm = 0;
+   if(battery_config->command_on_critical) free(battery_config->command_on_critical);
+   battery_config->command_on_critical = cfdata->command_on_critical;
+   
+   if(cfdata->run_command)
+       battery_config->command_on_critical_time = cfdata->command_on_critical_time;
+   else
+       battery_config->command_on_critical_time = 0;
+   
+   
    _battery_config_updated();
    e_config_save_queue();
    return 1;
Index: e_mod_main.c
===================================================================
RCS file: /var/cvs/e/e17/apps/e/src/modules/battery/e_mod_main.c,v
retrieving revision 1.80
diff -u -r1.80 e_mod_main.c
--- e_mod_main.c	7 Jun 2006 23:30:13 -0000	1.80
+++ e_mod_main.c	21 Jun 2006 22:09:52 -0000
@@ -150,7 +150,7 @@
 /***************************************************************************/
 
 /***************************************************************************/
-/**/
+/***/
 static void
 _button_cb_mouse_down(void *data, Evas *e, Evas_Object *obj, void *event_info)
 {
@@ -286,6 +286,12 @@
 		    {
 		       if (battery_config->battery_prev_ac != 0)
 			 edje_object_signal_emit(inst->o_battery, "discharge", "");
+               if(ret->critical && !battery_config->critical_triggered)
+               {
+                   if(strlen(battery_config->command_on_critical) >0)
+                       system(battery_config->command_on_critical);
+                   battery_config->critical_triggered = 1;
+               }
 		       if (ret->alarm)
 			 {
 			    if (!battery_config->alarm_triggered)
@@ -530,6 +536,7 @@
           {
 	     stat->state = BATTERY_STATE_CHARGING;
 	     battery_config->alarm_triggered = 0;
+         battery_config->critical_triggered = 0;
           }
 	else if (discharging)
 	  {
@@ -538,6 +545,8 @@
 	       {
 		  if (((hours * 60) + minutes) <= battery_config->alarm)
 		    stat->alarm = 1;
+          if (((hours * 60) + minutes) <= battery_config->command_on_critical_time)
+		    stat->critical = 1;
 	       }
 	  }
 	if (level_unknown)
@@ -663,6 +672,8 @@
 	  {
 	     if (((hours * 60) + minutes) <= battery_config->alarm)
 	       stat->alarm = 1;
+         if (((hours * 60) + minutes) <= battery_config->command_on_critical_time)
+	       stat->critical = 1;
 	  }
      }
 
@@ -842,6 +853,7 @@
           {
 	     stat->state = BATTERY_STATE_CHARGING;
 	     battery_config->alarm_triggered = 0;
+         battery_config->critical_triggered = 0;
           }
 	else if (discharging)
 	  {
@@ -850,6 +862,8 @@
 	       {
 		  if (((hours * 60) + minutes) <= battery_config->alarm)
 		    stat->alarm = 1;
+          if (((hours * 60) + minutes) <= battery_config->command_on_critical_time)
+	       stat->critical = 1;
 	       }
 	  }
 	stat->level = (double)charge / (double)max_charge;
@@ -997,7 +1011,8 @@
         if (state == BATTERY_STATE_CHARGING)
           {
 	     stat->state = BATTERY_STATE_CHARGING;
-	     battery_config->alarm_triggered = 0;
+         battery_config->alarm_triggered = 0;
+	     battery_config->critical_triggered = 0;
           }
 	else if (state == BATTERY_STATE_DISCHARGING)
 	  {
@@ -1006,6 +1021,8 @@
 	       {
 		  if (((hours * 60) + minutes) <= battery_config->alarm)
 		    stat->alarm = 1;
+          if (((hours * 60) + minutes) <= battery_config->command_on_critical_time)
+	       stat->critical = 1;
 	       }
 	  }
 	if (!level)
@@ -1129,6 +1146,8 @@
 	  {
 	     if (((hours * 60) + minutes) <= battery_config->alarm)
 	       stat->alarm = 1;
+         if (((hours * 60) + minutes) <= battery_config->command_on_critical_time)
+	       stat->critical = 1;
 	  }
      }
    
@@ -1376,6 +1395,8 @@
 #define D conf_edd
    E_CONFIG_VAL(D, T, poll_time, DOUBLE);
    E_CONFIG_VAL(D, T, alarm, INT);
+   E_CONFIG_VAL(D, T, command_on_critical_time, INT);
+   E_CONFIG_VAL(D, T, command_on_critical, STR);
 
    battery_config = e_config_domain_load("module.battery", conf_edd);
    if (!battery_config)
@@ -1383,9 +1404,12 @@
        battery_config = E_NEW(Config, 1);
        battery_config->poll_time = 30.0;
        battery_config->alarm = 30;
+       battery_config->command_on_critical_time = 20;
+       battery_config->command_on_critical = strdup("");
      }
    E_CONFIG_LIMIT(battery_config->poll_time, 0.5, 1000.0);
    E_CONFIG_LIMIT(battery_config->alarm, 0, 60);
+   E_CONFIG_LIMIT(battery_config->command_on_critical_time, 0, 60*3);
    
    battery_config->battery_check_mode = CHECK_NONE;
    battery_config->battery_prev_drain = 1;
Index: e_mod_main.h
===================================================================
RCS file: /var/cvs/e/e17/apps/e/src/modules/battery/e_mod_main.h,v
retrieving revision 1.25
diff -u -r1.25 e_mod_main.h
--- e_mod_main.h	6 Jun 2006 15:19:36 -0000	1.25
+++ e_mod_main.h	21 Jun 2006 22:09:52 -0000
@@ -17,12 +17,15 @@
    /* saved * loaded config values */
    double           poll_time;
    int              alarm;
+   int              command_on_critical_time;
+   char             *command_on_critical;
    /* just config state */
    E_Module        *module;
    E_Config_Dialog *config_dialog;
    Evas_List       *instances;
    E_Menu          *menu;
    int              alarm_triggered;
+   int              critical_triggered;
    int              battery_check_mode;
    Ecore_Timer     *battery_check_timer;
    int              battery_prev_drain;
@@ -45,6 +48,9 @@
 {
    /* Low battery */
    unsigned char alarm;
+   /* critical battery */
+   unsigned char critical;
+   
    /* Is there a battery? */
    unsigned char has_battery;
    /* charging, discharging, none */