]> Dogcows Code - chaz/tint2/commitdiff
issue 158 : dissociate object initialization from panel initialization
authorThierry Lorthiois <lorthiois@bbsoft.fr>
Sun, 18 Oct 2009 17:54:09 +0000 (17:54 +0000)
committerThierry Lorthiois <lorthiois@bbsoft.fr>
Sun, 18 Oct 2009 17:54:09 +0000 (17:54 +0000)
12 files changed:
src/battery/battery.c
src/battery/battery.h
src/clock/clock.c
src/clock/clock.h
src/config.c
src/config.h
src/panel.c
src/panel.h
src/server.c
src/systray/systraybar.c
src/systray/systraybar.h
src/tint.c

index 7e47bd32699a8f2ee9a740b3ae099372d2f2f723..d5bf0a98851b870961beb299b2410254979b7e93 100644 (file)
@@ -26,9 +26,9 @@
 
 #include "window.h"
 #include "server.h"
-#include "taskbar.h"
-#include "panel.h"
 #include "area.h"
+#include "panel.h"
+#include "taskbar.h"
 #include "battery.h"
 #include "clock.h"
 
@@ -95,77 +95,77 @@ void init_battery()
                g_free(path1);
        }
 
+       if (battery_dir)
+               g_free(battery_dir);
+}
+
+
+void init_battery_panel(void *p)
+{
+       Panel *panel = (Panel*)p;
+       Battery *battery = &panel->battery;
        FILE *fp;
-       Panel *panel;
-       Battery *battery;
-       int i, bat_percentage_height, bat_percentage_height_ink, bat_time_height, bat_time_height_ink;
-
-       for (i=0 ; i < nb_panel ; i++) {
-               panel = &panel1[i];
-               battery = &panel->battery;
-
-               if (battery_dir == 0) battery->area.on_screen = 0;
-               if (!battery->area.on_screen) continue;
-
-               battery->area.parent = panel;
-               battery->area.panel = panel;
-               battery->area._draw_foreground = draw_battery;
-               battery->area._resize = resize_battery;
-               battery->area.resize = 1;
-               battery->area.redraw = 1;
-
-               if((fp = fopen(path_energy_now, "r")) == NULL) {
-                       fprintf(stderr, "ERROR: battery applet can't open energy_now\n");
-                       panel->battery.area.on_screen = 0;
-                       continue;
-               }
-               fclose(fp);
-               if((fp = fopen(path_energy_full, "r")) == NULL) {
-                       fprintf(stderr, "ERROR: battery applet can't open energy_full\n");
-                       panel->battery.area.on_screen = 0;
-                       continue;
-               }
-               fclose(fp);
-               if((fp = fopen(path_current_now, "r")) == NULL) {
-                       fprintf(stderr, "ERROR: battery applet can't open current_now\n");
-                       panel->battery.area.on_screen = 0;
-                       continue;
-               }
-               fclose(fp);
-               if((fp = fopen(path_status, "r")) == NULL) {
-                       fprintf(stderr, "ERROR: battery applet can't open status");
-                       panel->battery.area.on_screen = 0;
-                       continue;
-               }
-               fclose(fp);
+       int bat_percentage_height, bat_percentage_height_ink, bat_time_height, bat_time_height_ink;
+       char *battery_dir = 0;
 
-               update_battery(&battery_state);
-               snprintf(buf_bat_percentage, sizeof(buf_bat_percentage), "%d%%", battery_state.percentage);
-               snprintf(buf_bat_time, sizeof(buf_bat_time), "%02d:%02d", battery_state.time.hours, battery_state.time.minutes);
+       if (battery_dir == 0) battery->area.on_screen = 0;
+       if (!battery->area.on_screen) return;
 
-               get_text_size(bat1_font_desc, &bat_percentage_height_ink, &bat_percentage_height, panel->area.height, buf_bat_percentage, strlen(buf_bat_percentage));
-               get_text_size(bat2_font_desc, &bat_time_height_ink, &bat_time_height, panel->area.height, buf_bat_time, strlen(buf_bat_time));
+       battery->area.parent = p;
+       battery->area.panel = p;
+       battery->area._draw_foreground = draw_battery;
+       battery->area._resize = resize_battery;
+       battery->area.resize = 1;
+       battery->area.redraw = 1;
 
-               if (panel_horizontal) {
-                       // panel horizonal => fixed height and posy
-                       battery->area.posy = panel->area.pix.border.width + panel->area.paddingy;
-                       battery->area.height = panel->area.height - (2 * battery->area.posy);
-               }
-               else {
-                       // panel vertical => fixed width, height, posy and posx
-                       battery->area.posy = panel->clock.area.posy + panel->clock.area.height + panel->area.paddingx;
-                       battery->area.height = (2 * battery->area.paddingxlr) + (bat_time_height + bat_percentage_height);
-                       battery->area.posx = panel->area.pix.border.width + panel->area.paddingy;
-                       battery->area.width = panel->area.width - (2 * panel->area.pix.border.width) - (2 * panel->area.paddingy);
-               }
+       if((fp = fopen(path_energy_now, "r")) == NULL) {
+               fprintf(stderr, "ERROR: battery applet can't open energy_now\n");
+               panel->battery.area.on_screen = 0;
+               return;
+       }
+       fclose(fp);
+       if((fp = fopen(path_energy_full, "r")) == NULL) {
+               fprintf(stderr, "ERROR: battery applet can't open energy_full\n");
+               panel->battery.area.on_screen = 0;
+               return;
+       }
+       fclose(fp);
+       if((fp = fopen(path_current_now, "r")) == NULL) {
+               fprintf(stderr, "ERROR: battery applet can't open current_now\n");
+               panel->battery.area.on_screen = 0;
+               return;
+       }
+       fclose(fp);
+       if((fp = fopen(path_status, "r")) == NULL) {
+               fprintf(stderr, "ERROR: battery applet can't open status");
+               panel->battery.area.on_screen = 0;
+               return;
+       }
+       fclose(fp);
 
-               battery->bat1_posy = (battery->area.height - bat_percentage_height) / 2;
-               battery->bat1_posy -= ((bat_time_height_ink + 2) / 2);
-               battery->bat2_posy = battery->bat1_posy + bat_percentage_height + 2 - (bat_percentage_height - bat_percentage_height_ink)/2 - (bat_time_height - bat_time_height_ink)/2;
+       update_battery(&battery_state);
+       snprintf(buf_bat_percentage, sizeof(buf_bat_percentage), "%d%%", battery_state.percentage);
+       snprintf(buf_bat_time, sizeof(buf_bat_time), "%02d:%02d", battery_state.time.hours, battery_state.time.minutes);
+
+       get_text_size(bat1_font_desc, &bat_percentage_height_ink, &bat_percentage_height, panel->area.height, buf_bat_percentage, strlen(buf_bat_percentage));
+       get_text_size(bat2_font_desc, &bat_time_height_ink, &bat_time_height, panel->area.height, buf_bat_time, strlen(buf_bat_time));
+
+       if (panel_horizontal) {
+               // panel horizonal => fixed height and posy
+               battery->area.posy = panel->area.pix.border.width + panel->area.paddingy;
+               battery->area.height = panel->area.height - (2 * battery->area.posy);
+       }
+       else {
+               // panel vertical => fixed width, height, posy and posx
+               battery->area.posy = panel->clock.area.posy + panel->clock.area.height + panel->area.paddingx;
+               battery->area.height = (2 * battery->area.paddingxlr) + (bat_time_height + bat_percentage_height);
+               battery->area.posx = panel->area.pix.border.width + panel->area.paddingy;
+               battery->area.width = panel->area.width - (2 * panel->area.pix.border.width) - (2 * panel->area.paddingy);
        }
 
-       if (battery_dir)
-               g_free(battery_dir);
+       battery->bat1_posy = (battery->area.height - bat_percentage_height) / 2;
+       battery->bat1_posy -= ((bat_time_height_ink + 2) / 2);
+       battery->bat2_posy = battery->bat1_posy + bat_percentage_height + 2 - (bat_percentage_height - bat_percentage_height_ink)/2 - (bat_time_height - bat_time_height_ink)/2;
 }
 
 
index 71280e1510b69f52bc429d558fbab0fa825ccd39..5fb1fdf13abfb82ae66684d90d63110ee93da189 100644 (file)
@@ -17,6 +17,7 @@
 #include "common.h"
 #include "area.h"
 
+
 typedef struct Battery {
        // always start with area
        Area area;
@@ -57,6 +58,7 @@ extern char *path_energy_now, *path_energy_full, *path_current_now, *path_status
 void update_battery();
 
 void init_battery();
+void init_battery_panel(void *panel);
 
 void draw_battery(void *obj, cairo_t *c, int active);
 
index b7a3966c71f586d75612583425f8fbbe08493564..cff5e619bcffe110478b856c533acceaab140abc 100644 (file)
@@ -26,9 +26,9 @@
 
 #include "window.h"
 #include "server.h"
-#include "taskbar.h"
-#include "panel.h"
 #include "area.h"
+#include "panel.h"
+#include "taskbar.h"
 #include "clock.h"
 
 
@@ -56,58 +56,56 @@ void init_precision()
 
 void init_clock()
 {
-       Panel *panel;
-       Clock *clock;
-       int i, time_height, time_height_ink, date_height, date_height_ink;
-
        init_precision();
+
        // update clock to force update (-time_precision)
        struct timeval stv;
        gettimeofday(&stv, 0);
        time_clock.tv_sec = stv.tv_sec - time_precision;
        time_clock.tv_sec -= time_clock.tv_sec % time_precision;
+}
 
-       for (i=0 ; i < nb_panel ; i++) {
-               panel = &panel1[i];
-               clock = &panel->clock;
 
-               if (!clock->area.on_screen) continue;
+void init_clock_panel(void *p)
+{
+       Panel *panel =(Panel*)p;
+       Clock *clock = &panel->clock;
+       int i, time_height, time_height_ink, date_height, date_height_ink;
 
-               clock->area.parent = panel;
-               clock->area.panel = panel;
-               clock->area._draw_foreground = draw_clock;
-               clock->area._resize = resize_clock;
-               clock->area.resize = 1;
-               clock->area.redraw = 1;
+       clock->area.parent = p;
+       clock->area.panel = p;
+       clock->area._draw_foreground = draw_clock;
+       clock->area._resize = resize_clock;
+       clock->area.resize = 1;
+       clock->area.redraw = 1;
 
-               strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec));
-               get_text_size(time1_font_desc, &time_height_ink, &time_height, panel->area.height, buf_time, strlen(buf_time));
-               if (time2_format) {
-                       strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
-                       get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
-               }
+       strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec));
+       get_text_size(time1_font_desc, &time_height_ink, &time_height, panel->area.height, buf_time, strlen(buf_time));
+       if (time2_format) {
+               strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
+               get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
+       }
 
-               if (panel_horizontal) {
-                       // panel horizonal => fixed height and posy
-                       clock->area.posy = panel->area.pix.border.width + panel->area.paddingy;
-                       clock->area.height = panel->area.height - (2 * clock->area.posy);
-               }
-               else {
-                       // panel vertical => fixed width, height, posy and posx
-                       clock->area.posy = panel->area.pix.border.width + panel->area.paddingxlr;
-                       clock->area.height = (2 * clock->area.paddingxlr) + (time_height + date_height);
-                       clock->area.posx = panel->area.pix.border.width + panel->area.paddingy;
-                       clock->area.width = panel->area.width - (2 * panel->area.pix.border.width) - (2 * panel->area.paddingy);
-               }
+       if (panel_horizontal) {
+               // panel horizonal => fixed height and posy
+               clock->area.posy = panel->area.pix.border.width + panel->area.paddingy;
+               clock->area.height = panel->area.height - (2 * clock->area.posy);
+       }
+       else {
+               // panel vertical => fixed width, height, posy and posx
+               clock->area.posy = panel->area.pix.border.width + panel->area.paddingxlr;
+               clock->area.height = (2 * clock->area.paddingxlr) + (time_height + date_height);
+               clock->area.posx = panel->area.pix.border.width + panel->area.paddingy;
+               clock->area.width = panel->area.width - (2 * panel->area.pix.border.width) - (2 * panel->area.paddingy);
+       }
 
-               clock->time1_posy = (clock->area.height - time_height) / 2;
-               if (time2_format) {
-                       strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
-                       get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
+       clock->time1_posy = (clock->area.height - time_height) / 2;
+       if (time2_format) {
+               strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
+               get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
 
-                       clock->time1_posy -= ((date_height_ink + 2) / 2);
-                       clock->time2_posy = clock->time1_posy + time_height + 2 - (time_height - time_height_ink)/2 - (date_height - date_height_ink)/2;
-               }
+               clock->time1_posy -= ((date_height_ink + 2) / 2);
+               clock->time2_posy = clock->time1_posy + time_height + 2 - (time_height - time_height_ink)/2 - (date_height - date_height_ink)/2;
        }
 }
 
index f521de45020ff9c970c88e4a46f29abba7b52597..b91c796dcf986159b576ca32570b86ceeb592159 100644 (file)
@@ -36,6 +36,7 @@ extern char *clock_rclick_command;
 
 // initialize clock : y position, precision, ...
 void init_clock();
+void init_clock_panel(void *panel);
 void init_precision();
 
 void draw_clock (void *obj, cairo_t *c, int active);
index 6d9e7d0cc4e65bba7dc08820bf653e6b32b237af..e281b8a006f6f0d5713a71d59041b896cd28f9f8 100644 (file)
 
 #include "common.h"
 #include "server.h"
+#include "panel.h"
 #include "task.h"
 #include "taskbar.h"
 #include "systraybar.h"
 #include "clock.h"
-#include "panel.h"
 #include "config.h"
 #include "window.h"
 #include "tooltip.h"
@@ -748,19 +748,15 @@ void config_finish ()
 
        // TODO: user can configure layout => ordered objects in panel.area.list
        // clock and systray before taskbar because resize(clock) can resize others object ??
-       init_panel();
+       init_tooltip();
        init_clock();
 #ifdef ENABLE_BATTERY
        init_battery();
 #endif
        init_systray();
-       init_taskbar();
-       init_tooltip();
-       visible_object();
+       init_panel();
 
        cleanup_config();
-
-       task_refresh_tasklist();
 }
 
 
index ca851853d0e7c410b903c939087b7d5f776eb319..42aafcf46f844b504188b341ac370d6ede934d3b 100644 (file)
@@ -20,3 +20,4 @@ void config_finish ();
 void save_config ();
 
 #endif
+
index 8a6af12713d1a6af7ccf3a907d2f233a48b48720..c28db61c73e08727b1d4285752188a92037eb9fa 100644 (file)
@@ -59,8 +59,8 @@ int  max_tick_urgent;
 // panel's initial config
 Panel panel_config;
 // panels (one panel per monitor)
-Panel *panel1 = 0;
-int  nb_panel;
+Panel *panel1 = NULL;
+int  nb_panel = 0;
 
 Imlib_Image default_icon = NULL;
 
@@ -68,20 +68,40 @@ Imlib_Image default_icon = NULL;
 
 void init_panel()
 {
-       int i;
-       Panel *p;
+       int i, old_nb_panel;
+       Panel *new_panel, *p;
+
+       cleanup_taskbar();
+       for (i=0 ; i < nb_panel ; i++) {
+               free_area(&panel1[i].area);
+       }
 
        // alloc panels (one monitor or all monitors)
+       old_nb_panel = nb_panel;
        if (panel_config.monitor >= 0)
                nb_panel = 1;
        else
                nb_panel = server.nb_monitor;
-       panel1 = malloc(nb_panel * sizeof(Panel));
+       fprintf(stderr, "tint2 : nb monitor %d, nb desktop %d\n", nb_panel, server.nb_desktop);
+
+/*     if (nb_panel < old_nb_panel) {
+               // freed old panels
+               for (i=nb_panel ; i < old_nb_panel ; i++) {
+               }
+       }*/
+
+       if (nb_panel != old_nb_panel)
+               new_panel = realloc(panel1, nb_panel * sizeof(Panel));
+       else
+               new_panel = panel1;
 
        for (i=0 ; i < nb_panel ; i++) {
-               p = &panel1[i];
+               p = &new_panel[i];
 
-               memcpy(p, &panel_config, sizeof(Panel));
+               if (i >= old_nb_panel) {
+                       // new panel
+                       memcpy(p, &panel_config, sizeof(Panel));
+               }
                p->monitor = i;
                p->area.parent = p;
                p->area.panel = p;
@@ -91,17 +111,24 @@ void init_panel()
                p->g_taskbar.parent = p;
                p->g_taskbar.panel = p;
                p->g_task.area.panel = p;
+               init_panel_size_and_position(p);
 
                // add childs
-               if (p->clock.area.on_screen)
+               if (p->clock.area.on_screen) {
+                       init_clock_panel(p);
                        p->area.list = g_slist_append(p->area.list, &p->clock);
+               }
 #ifdef ENABLE_BATTERY
-               if (p->battery.area.on_screen)
+               if (p->battery.area.on_screen) {
+                       init_battery_panel(p);
                        p->area.list = g_slist_append(p->area.list, &p->battery);
+               }
 #endif
                // systray only on first panel
-               if (systray.area.on_screen && i == 0)
+               if (systray.area.on_screen && i == 0) {
+                       init_systray_panel(p);
                        p->area.list = g_slist_append(p->area.list, &systray);
+               }
 
                // full width mode
                if (!p->initial_width) {
@@ -109,22 +136,35 @@ void init_panel()
                        p->pourcentx = 1;
                }
 
-               init_panel_size_and_position(p);
-
-               // Catch some events
-               long event_mask = ExposureMask|ButtonPressMask|ButtonReleaseMask;
-               if (g_tooltip.enabled)
-                       event_mask |= PointerMotionMask|LeaveWindowMask;
-               XSetWindowAttributes att = { ParentRelative, 0L, 0, 0L, 0, 0, Always, 0L, 0L, False, event_mask, NoEventMask, False, 0, 0 };
-               if (p->main_win) XDestroyWindow(server.dsp, p->main_win);
-               p->main_win = XCreateWindow(server.dsp, server.root_win, p->posx, p->posy, p->area.width, p->area.height, 0, server.depth, InputOutput, CopyFromParent, CWEventMask, &att);
-
-               set_panel_properties(p);
-               set_panel_background(p);
-
-               XMapWindow (server.dsp, p->main_win);
+               if (i >= old_nb_panel) {
+                       // new panel
+                       //printf("new window\n");
+
+                       // Catch some events
+                       long event_mask = ExposureMask|ButtonPressMask|ButtonReleaseMask;
+                       if (g_tooltip.enabled)
+                               event_mask |= PointerMotionMask|LeaveWindowMask;
+                       XSetWindowAttributes att = { ParentRelative, 0L, 0, 0L, 0, 0, Always, 0L, 0L, False, event_mask, NoEventMask, False, 0, 0 };
+                       if (p->main_win) XDestroyWindow(server.dsp, p->main_win);
+                       p->main_win = XCreateWindow(server.dsp, server.root_win, p->posx, p->posy, p->area.width, p->area.height, 0, server.depth, InputOutput, CopyFromParent, CWEventMask, &att);
+
+                       set_panel_properties(p);
+                       set_panel_background(p);
+                       XMapWindow (server.dsp, p->main_win);
+               }
+               else {
+                       // old panel
+                       //printf("move old window\n");
+                       XMoveResizeWindow(server.dsp, p->main_win, p->posx, p->posy, p->area.width, p->area.height);
+                       set_panel_background(p);
+               }
        }
+
+       panel1 = new_panel;
        panel_refresh = 1;
+       init_taskbar();
+       visible_object();
+       task_refresh_tasklist();
 }
 
 
index 1e7da9e83b7decb3b458015376a1d6c459180bc0..a74b080cea360f7a63eaa7cfe2f76cfcfd8e4f51 100644 (file)
@@ -105,7 +105,10 @@ extern Panel panel_config;
 extern Panel *panel1;
 extern int  nb_panel;
 
+// realloc panels according to number of monitor
+// it's used on initial config and when new monitor added
 void init_panel();
+
 void init_panel_size_and_position(Panel *panel);
 void cleanup_panel();
 void resize_panel(void *obj);
index 9b09010506e2bc53323303ecb0df5eaae8d20a44..c501ec1e6eecec09328b6ae669c005535953a815 100644 (file)
@@ -290,7 +290,6 @@ void get_desktops()
                server.nb_desktop = 1;
                fprintf(stderr, "warning : WM doesn't respect NETWM specs. tint2 default to 1 desktop.\n");
        }
-       fprintf(stderr, "tint2 : nb monitor %d, nb desktop %d\n", server.nb_monitor, server.nb_desktop);
 }
 
 
index d9e3411e5d4fcff7bf4f6190b95f8f5c5ed57819..16dd079c57ce25060d414623bdd650e636a13900 100644 (file)
@@ -47,24 +47,24 @@ int refresh_systray;
 
 void init_systray()
 {
-       Panel *panel = &panel1[0];
-
        if (systray.area.on_screen)
                systray.area.on_screen = init_net();
 
        if (!systray.area.on_screen)
                return;
 
-       systray.area.parent = panel;
-       systray.area.panel = panel;
        systray.area._draw_foreground = draw_systray;
        systray.area._resize = resize_systray;
        systray.area.resize = 1;
        systray.area.redraw = 1;
        refresh_systray = 0;
+}
+
+
+void init_systray_panel(void *p)
+{
+       Panel *panel =(Panel*)p;
 
-       // configure systray
-       // draw only one systray (even with multi panel)
        if (panel_horizontal) {
                systray.area.posy = panel->area.pix.border.width + panel->area.paddingy;
                systray.area.height = panel->area.height - (2 * systray.area.posy);
@@ -73,6 +73,8 @@ void init_systray()
                systray.area.posx = panel->area.pix.border.width + panel->area.paddingy;
                systray.area.width = panel->area.width - (2 * panel->area.pix.border.width) - (2 * panel->area.paddingy);
        }
+       systray.area.parent = p;
+       systray.area.panel = p;
 }
 
 
index c1f2324c223449f822d75512424729faa00b4d67..778b89261e883224372fce672b8dee9522bcc0ba 100644 (file)
@@ -44,6 +44,7 @@ extern int refresh_systray;
 
 
 void init_systray();
+void init_systray_panel(void *p);
 void cleanup_systray();
 void draw_systray(void *obj, cairo_t *c, int active);
 void resize_systray(void *obj);
index 260d93cdecdb610c9e448ca0b8c7570b2d47d285..70b07c8521e383fc60030128bc444667d1963522 100644 (file)
@@ -636,33 +636,8 @@ void event_configure_notify (Window win)
 {
        // change in root window (xrandr)
        if (win == server.root_win) {
-               int i, old_nb_panel = nb_panel;
-
                get_monitors();
-               if (panel_config.monitor >= 0)
-                       nb_panel = 1;
-               else
-                       nb_panel = server.nb_monitor;
-
-               if (old_nb_panel != nb_panel) {
-                       // changed number of panel
-                       printf("changed number of panel\n");
-                       //realloc(panel1, nb_panel * sizeof(Panel));
-               }
-               for (i=0 ; i < nb_panel && i < old_nb_panel ; i++) {
-                       Panel *panel = &panel1[i];
-
-                       init_panel_size_and_position(panel);
-                       XMoveResizeWindow(server.dsp, panel->main_win, panel->posx, panel->posy, panel->area.width, panel->area.height);
-                       set_panel_background(panel);
-
-                       // force the resize of childs
-                       GSList *l0;
-                       panel->area.resize = 1;
-                       for (l0 = panel->area.list; l0 ; l0 = l0->next)
-                               ((Area*)l0->data)->resize = 1;
-               }
-               panel_refresh = 1;
+               init_panel();
                return;
        }
 
This page took 0.040786 seconds and 4 git commands to generate.