Changeset 65

Show
Ignore:
Timestamp:
07/17/07 15:32:21 (1 year ago)
Author:
disq
Message:

maxed cpu notification

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • osso-statusbar-cpu/trunk/src/common.h

    r61 r65  
    2323#define GCONF_PATH_COMMAND_NAMES GCONF_PATH "/command_names" 
    2424#define GCONF_PATH_COMMANDS GCONF_PATH "/commands" 
     25#define GCONF_PATH_CPUMAX_ON GCONF_PATH "/cpumax_on" 
     26#define GCONF_PATH_MAXED_CPU_SECS GCONF_PATH "/maxed_cpu_secs" 
    2527 
    2628#define CPU_DEFAULT 0x1f6fff 
     
    2830#define CPU_ITEMS_DEFAULT 3 
    2931#define MEM_ITEMS_DEFAULT 3 
     32#define GCONF_PATH_MAXED_CPU_SECS_DEFAULT 10 
     33 
     34#define MAXED_CPU_NOTIFY_PCT 90 
    3035 
    3136#define _(x) x 
  • osso-statusbar-cpu/trunk/src/osso-applet-graph.c

    r61 r65  
    3737static void combo_toggled(GtkWidget *widget, GraphApplet *applet); 
    3838static void clock_toggled(GtkWidget *widget, GraphApplet *applet); 
     39static void cpumax_toggled(GtkWidget *widget, GraphApplet *applet); 
    3940static void mem_color_clicked(GtkWidget *widget, GraphApplet *applet); 
    4041static void cpu_color_clicked(GtkWidget *widget, GraphApplet *applet); 
     
    7677 
    7778        if (response == GTK_RESPONSE_ACCEPT) { 
     79                gconf_client_set_bool(applet.gcc, GCONF_PATH_CPUMAX_ON, 
     80                                applet.flags & CPUMAX_ON ? TRUE : FALSE, NULL); 
    7881                gconf_client_set_bool(applet.gcc, GCONF_PATH_CLOCK_ON, 
    7982                                applet.flags & CLOCK ? TRUE : FALSE, NULL); 
     
    119122        gboolean b_combine; 
    120123        gboolean b_clock; 
     124        gboolean b_notify_cpumax; 
    121125 
    122126        GSList *l_cmds; 
     
    157161                        GCONF_PATH_CLOCK_ON, NULL); 
    158162 
     163        b_notify_cpumax = gconf_client_get_bool(applet->gcc, 
     164                        GCONF_PATH_CPUMAX_ON, NULL); 
     165 
    159166        l_cmds = gconf_client_get_list(applet->gcc, 
    160167                        GCONF_PATH_COMMANDS, 
     
    171178                b_show_cpu = FALSE; 
    172179                b_show_mem = FALSE; 
    173                 applet->flags = CPU_ON | MEM_ON | b_clock * CLOCK
     180                applet->flags = CPU_ON | MEM_ON | b_clock * CLOCK | b_notify_cpumax * CPUMAX_ON
    174181        } else { 
    175182                applet->flags = b_show_cpu * CPU_ON | 
    176183                        b_show_mem * MEM_ON | 
    177                         b_clock * CLOCK; 
     184                        b_clock * CLOCK |  
     185                        b_notify_cpumax * CPUMAX_ON; 
    178186        } 
    179187        g_debug("cb: %d, cp: %d, me: %d", b_combine, b_show_cpu, b_show_mem); 
     
    281289                        HILDON_CAPTION_OPTIONAL); 
    282290 
     291        applet->cpumax_check = gtk_check_button_new(); 
     292        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(applet->cpumax_check), 
     293                                     b_notify_cpumax); 
     294        applet->cpumax_caption = hildon_caption_new(size_group, 
     295                        _("Notify if cpu is maxed"), applet->cpumax_check, NULL, 
     296                        HILDON_CAPTION_OPTIONAL); 
     297 
    283298        g_signal_connect_after(G_OBJECT(applet->combo_check), 
    284299                               "toggled", G_CALLBACK(combo_toggled), 
     
    289304                               applet); 
    290305 
     306        g_signal_connect_after(G_OBJECT(applet->cpumax_check), 
     307                               "toggled", G_CALLBACK(cpumax_toggled), 
     308                               applet); 
     309 
    291310        gtk_container_add(GTK_CONTAINER(dialog_vbox), 
    292311                          GTK_WIDGET(applet->combo_caption) 
     
    294313        gtk_container_add(GTK_CONTAINER(dialog_vbox), 
    295314                          GTK_WIDGET(gtk_hseparator_new())); 
     315        gtk_container_add(GTK_CONTAINER(dialog_vbox), 
     316                          GTK_WIDGET(applet->cpumax_caption) 
     317                ); 
    296318        gtk_container_add(GTK_CONTAINER(dialog_vbox), 
    297319                          GTK_WIDGET(applet->clock_caption) 
     
    400422        applet->flags &= ~CLOCK; 
    401423        applet->flags |= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)) * CLOCK; 
     424        gconf_client_clear_cache(applet->gcc); 
     425        while (gtk_events_pending()) 
     426                gtk_main_iteration(); 
     427} 
     428 
     429static void cpumax_toggled(GtkWidget *widget, GraphApplet *applet) 
     430{ 
     431        applet->flags &= ~CPUMAX_ON; 
     432        applet->flags |= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)) * CPUMAX_ON; 
    402433        gconf_client_clear_cache(applet->gcc); 
    403434        while (gtk_events_pending()) 
  • osso-statusbar-cpu/trunk/src/osso-applet-graph.h

    r3 r65  
    1212        COMBO = 1 << 2, 
    1313        CLOCK = 1 << 3, 
     14        CPUMAX_ON = 1 << 4, 
    1415} flag; 
    1516 
     
    2021        GtkWidget *clock_check; 
    2122        GtkWidget *clock_caption; 
     23        GtkWidget *cpumax_check; 
     24        GtkWidget *cpumax_caption; 
    2225 
    2326        GtkDialog *commands_dialog; 
  • osso-statusbar-cpu/trunk/src/osso-statusbar-combo.c

    r61 r65  
    2424#endif 
    2525 
     26#include <hildon-widgets/hildon-banner.h> 
     27 
    2628#include <stdio.h> 
    2729#include <string.h> 
     
    5355                                GConfEntry *entry, gpointer user_data); 
    5456static void memon_gconf_handler(GConfClient *gcc, 
     57                                guint con, 
     58                                GConfEntry *entry, gpointer user_data); 
     59static void cpumaxon_gconf_handler(GConfClient *gcc, 
    5560                                guint con, 
    5661                                GConfEntry *entry, gpointer user_data); 
     
    162167        info->show_mem = gconf_client_get_bool(info->gcc, 
    163168                        GCONF_PATH_MEM_ON, NULL); 
     169        info->notify_cpumax = gconf_client_get_bool(info->gcc, 
     170                        GCONF_PATH_CPUMAX_ON, NULL); 
    164171 
    165172        info->color_cpu = gc_get_int_fall(info->gcc, 
     
    169176        info->clock = gconf_client_get_bool(info->gcc, 
    170177                                            GCONF_PATH_CLOCK_ON, NULL); 
     178        info->maxed_cpu_secs = gc_get_int_fall(info->gcc, 
     179                                      GCONF_PATH_MAXED_CPU_SECS, GCONF_PATH_MAXED_CPU_SECS_DEFAULT); 
     180 
     181        if (info->maxed_cpu_secs < 5) { 
     182                info->maxed_cpu_secs = GCONF_PATH_MAXED_CPU_SECS_DEFAULT; 
     183                } 
    171184 
    172185        /* icon_pixbuf is owned by info->icon */ 
     
    183196#endif 
    184197 
     198        info->maxed_cpu_last_notification = 0; 
    185199        info->notify_id = 0; 
    186         if (info->show_cpu || info->show_mem) { 
     200        if (info->show_cpu || info->show_mem || info->notify_cpumax) { 
    187201                info->notify_id = g_timeout_add(2000, my_timeout, info); 
    188202        } 
     
    199213                        GCONF_PATH_MEM_ON, 
    200214                        memon_gconf_handler, 
     215                        info, NULL, NULL); 
     216        info->gc_notify_id_cpumax = gconf_client_notify_add(info->gcc, 
     217                        GCONF_PATH_CPUMAX_ON, 
     218                        cpumaxon_gconf_handler, 
    201219                        info, NULL, NULL); 
    202220 
     
    213231                                                     my_gconf_clk_handler, 
    214232                                                     info, NULL, NULL); 
     233        info->gc_notify_id_maxed_cpu_secs = gconf_client_notify_add(info->gcc, 
     234                                                     GCONF_PATH_MAXED_CPU_SECS, 
     235                                                     my_gconf_handler, 
     236                                                     info, NULL, NULL); 
    215237 
    216238        g_signal_connect(info->button, "toggled", G_CALLBACK(combo_popup), 
     
    256278        gconf_client_notify_remove(info->gcc, info->gc_notify_id_mem); 
    257279        gconf_client_notify_remove(info->gcc, info->gc_notify_id_clk); 
     280        gconf_client_notify_remove(info->gcc, info->gc_notify_id_cpumax); 
     281        gconf_client_notify_remove(info->gcc, info->gc_notify_id_maxed_cpu_secs); 
    258282        gconf_client_remove_dir(info->gcc, GCONF_PATH, NULL); 
    259283 
     
    296320        int x = info->show_cpu ? AREA_X + AREA_W / 2 : AREA_X; 
    297321         
    298         if (info->show_cpu) { 
     322        if (info->show_cpu || info->notify_cpumax) { 
    299323                FILE *foo = fopen("/proc/stat", "r"); 
    300324                fscanf(foo, "cpu %d %d %d %d", &user, &nice, &kern, &idle); 
     
    302326                total_change = user + nice + kern + idle - 
    303327                        (old_user + old_nice + old_kern + old_idle); 
     328 
     329                if (info->notify_cpumax) { 
     330 
     331                                int user_pct = (user - old_user)*100 / total_change; 
     332 
     333                                info->cpu_samples[info->cpu_cur_sample] = user_pct; 
     334                                info->cpu_cur_sample++; 
     335                                if (info->cpu_cur_sample >= CPU_NUM_SAMPLES) info->cpu_cur_sample = 0; 
     336                                 
     337                                gint samples_to_go_back = (int)(info->maxed_cpu_secs / 2); /* timeout is 2000ms */ 
     338                                if (samples_to_go_back >= CPU_NUM_SAMPLES) samples_to_go_back = CPU_NUM_SAMPLES; 
     339                                 
     340                                gint cpu_avg = 0, i; 
     341 
     342                                if (info->cpu_cur_sample==0) { /* condition one -- we have enough samples in the array, start from the end */ 
     343                                                for (i = CPU_NUM_SAMPLES - 1; i >= CPU_NUM_SAMPLES - samples_to_go_back; i-- ) cpu_avg += info->cpu_samples[i]; 
     344                                        } else if (info->cpu_cur_sample >= samples_to_go_back) { /* condition two -- we have enough samples in the first half of the array */ 
     345                                                for (i = info->cpu_cur_sample - 1; i >= info->cpu_cur_sample - samples_to_go_back; i-- ) cpu_avg += info->cpu_samples[i]; 
     346                                        } else { /* condition three - get some of the samples from the first half and some from the second half */ 
     347                                                for (i = info->cpu_cur_sample - 1; i >= 0; i-- ) cpu_avg += info->cpu_samples[i]; 
     348                                                int samples_left = samples_to_go_back - info->cpu_cur_sample; 
     349                                                for (i = CPU_NUM_SAMPLES - 1; i >= CPU_NUM_SAMPLES - samples_left; i-- ) cpu_avg += info->cpu_samples[i]; 
     350                                        } 
     351                                 
     352                                gint avg = (int)(cpu_avg / samples_to_go_back); 
     353 
     354                                if (avg >= MAXED_CPU_NOTIFY_PCT) { 
     355                                        guint ti = time(NULL); 
     356                                        if (info->maxed_cpu_last_notification == 0 || info->maxed_cpu_last_notification+CPUMAX_NOTIFY_FLOODPROTECTSECS < ti) { 
     357                                                info->maxed_cpu_last_notification = ti; 
     358                                                hildon_banner_show_information(info->icon, NULL, "Warning: Using excessive CPU"); 
     359                                                system("/usr/bin/play-sound /usr/share/sounds/ui-wrong_charger.wav"); 
     360                                                } 
     361                                        } 
     362 
     363                } 
     364 
    304365        } 
    305366        if (info->show_mem) { 
     
    334395        gtk_image_set_from_pixbuf(image, buf); 
    335396 
    336         if (info->show_cpu) { 
     397        if (info->show_cpu || info->notify_cpumax) { 
    337398                old_user = user; 
    338399                old_nice = nice; 
     
    360421                                g_timeout_add(2000, my_timeout, user_data); 
    361422                } 
    362         } else if (!info->show_mem && info->notify_id) { 
     423        } else if (!info->show_mem && !info->notify_cpumax && info->notify_id) { 
    363424                g_source_remove(info->notify_id); 
    364425                info->notify_id = 0; 
     
    383444                                g_timeout_add(2000, my_timeout, user_data); 
    384445                } 
    385         } else if (!info->show_cpu && info->notify_id) { 
     446        } else if (!info->show_cpu && !info->notify_cpumax && info->notify_id) { 
     447                g_source_remove(info->notify_id); 
     448                info->notify_id = 0; 
     449        } 
     450
     451 
     452static void cpumaxon_gconf_handler(GConfClient *gcc, 
     453                                guint con, 
     454                                GConfEntry *entry, gpointer user_data) 
     455
     456        OssoStatusbarCpuPrivate *info = user_data; 
     457 
     458        (void)gcc; 
     459        (void)con; 
     460 
     461        info->notify_cpumax = gconf_value_get_bool(entry->value); 
     462         
     463        if (info->notify_cpumax) { 
     464                if (!info->notify_id) { 
     465                        info->notify_id = 
     466                                g_timeout_add(2000, my_timeout, user_data); 
     467                } 
     468        } else if (!info->show_mem && !info->notify_cpumax && info->notify_id) { 
    386469                g_source_remove(info->notify_id); 
    387470                info->notify_id = 0; 
     
    402485        } else if (strcmp(entry->key, GCONF_PATH_MEM_COLOR) == 0) { 
    403486                info->color_mem =  
     487                        gconf_value_get_int(gconf_entry_get_value(entry)); 
     488        } else if (strcmp(entry->key, GCONF_PATH_MAXED_CPU_SECS) == 0) { 
     489                info->maxed_cpu_secs =  
    404490                        gconf_value_get_int(gconf_entry_get_value(entry)); 
    405491        } 
  • osso-statusbar-cpu/trunk/src/osso-statusbar-combo.h

    r61 r65  
    4444#endif 
    4545 
     46#define CPU_NUM_SAMPLES 32 
     47#define CPUMAX_NOTIFY_FLOODPROTECTSECS 10 
     48 
    4649typedef struct _OssoStatusbarCpuPrivate OssoStatusbarCpuPrivate; 
    4750 
     
    5760        guint gc_notify_id_mem; 
    5861        guint gc_notify_id_clk; 
     62        guint gc_notify_id_cpumax; 
     63        guint gc_notify_id_maxed_cpu_secs; 
    5964        gint color_cpu; 
    6065        gint color_mem; 
     
    6368        gboolean show_cpu; 
    6469        gboolean show_mem; 
     70        gboolean notify_cpumax; 
     71        gint maxed_cpu_secs; /* config value */ 
     72 
     73        gint cpu_samples[CPU_NUM_SAMPLES]; 
     74        gint cpu_cur_sample; 
     75        guint maxed_cpu_last_notification; 
    6576}; 
    6677