From: Andreas Fink Date: Wed, 30 Dec 2009 11:27:29 +0000 (+0000) Subject: *add* clock supports timezones X-Git-Url: https://git.brokenzipper.com/gitweb?a=commitdiff_plain;h=9becd8bd3b513a0bbfe778b2ef88f6d598fdf03a;p=chaz%2Ftint2 *add* clock supports timezones *add* first preparations for real transparency (tooltip works already, but panel does not. Therefore it is disabled in the source code) --- diff --git a/src/clock/clock.c b/src/clock/clock.c index 876536c..115ba9f 100644 --- a/src/clock/clock.c +++ b/src/clock/clock.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "window.h" #include "server.h" @@ -35,8 +36,11 @@ char *time1_format=0; +char *time1_timezone=0; char *time2_format=0; +char *time2_timezone=0; char *time_tooltip_format=0; +char *time_tooltip_timezone=0; char *clock_lclick_command=0; char *clock_rclick_command=0; struct timeval time_clock; @@ -60,10 +64,21 @@ void update_clocks() panel_refresh = 1; } +struct tm* clock_gettime_for_tz(const char* timezone) { + if (timezone) { + const char* old_tz = getenv("TZ"); + setenv("TZ", timezone, 1); + struct tm* result = localtime(&time_clock.tv_sec); + if (old_tz) setenv("TZ", old_tz, 1); + else unsetenv("TZ"); + return result; + } + else return localtime(&time_clock.tv_sec); +} const char* clock_get_tooltip(void* obj) { - strftime(buf_tooltip, sizeof(buf_tooltip), time_tooltip_format, localtime(&time_clock.tv_sec)); + strftime(buf_tooltip, sizeof(buf_tooltip), time_tooltip_format, clock_gettime_for_tz(time_tooltip_timezone)); return buf_tooltip; } @@ -93,10 +108,10 @@ void init_clock_panel(void *p) clock->area.redraw = 1; clock->area.on_screen = 1; - strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec)); + strftime(buf_time, sizeof(buf_time), time1_format, clock_gettime_for_tz(time1_timezone)); 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)); + strftime(buf_date, sizeof(buf_date), time2_format, clock_gettime_for_tz(time2_timezone)); get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date)); } @@ -115,7 +130,7 @@ void init_clock_panel(void *p) 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)); + strftime(buf_date, sizeof(buf_date), time2_format, clock_gettime_for_tz(time2_timezone)); 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); @@ -124,7 +139,7 @@ void init_clock_panel(void *p) if (time_tooltip_format) { clock->area._get_tooltip_text = clock_get_tooltip; - strftime(buf_tooltip, sizeof(buf_tooltip), time_tooltip_format, localtime(&time_clock.tv_sec)); + strftime(buf_tooltip, sizeof(buf_tooltip), time_tooltip_format, clock_gettime_for_tz(time_tooltip_timezone)); } } @@ -136,18 +151,17 @@ void cleanup_clock() pango_font_description_free(time1_font_desc); if (time2_font_desc) pango_font_description_free(time2_font_desc); - if (time1_format) - g_free(time1_format); - if (time2_format) - g_free(time2_format); - if (time_tooltip_format) - g_free(time_tooltip_format); - if (clock_lclick_command) - g_free(clock_lclick_command); - if (clock_rclick_command) - g_free(clock_rclick_command); + g_free(time1_format); + g_free(time2_format); + g_free(time_tooltip_format); + g_free(time1_timezone); + g_free(time2_timezone); + g_free(time_tooltip_timezone); + g_free(clock_lclick_command); + g_free(clock_rclick_command); time1_font_desc = time2_font_desc = 0; - time1_format = time2_format = 0; + time1_format = time2_format = time_tooltip_format = 0; + time1_timezone = time2_timezone = time_tooltip_timezone = 0; clock_lclick_command = clock_rclick_command = 0; } @@ -194,9 +208,9 @@ void resize_clock (void *obj) clock->area.redraw = 1; time_width = date_width = 0; - strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec)); + strftime(buf_time, sizeof(buf_time), time1_format, clock_gettime_for_tz(time1_timezone)); if (time2_format) - strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec)); + strftime(buf_date, sizeof(buf_date), time2_format, clock_gettime_for_tz(time2_timezone)); // vertical panel doen't adjust width if (!panel_horizontal) return; diff --git a/src/clock/clock.h b/src/clock/clock.h index 750a12e..d9d16fb 100644 --- a/src/clock/clock.h +++ b/src/clock/clock.h @@ -25,8 +25,11 @@ typedef struct Clock { extern char *time1_format; +extern char *time1_timezone; extern char *time2_format; +extern char *time2_timezone; extern char *time_tooltip_format; +extern char *time_tooltip_timezone; extern PangoFontDescription *time1_font_desc; extern PangoFontDescription *time2_font_desc; extern char *clock_lclick_command; diff --git a/src/config.c b/src/config.c index fe3f9fe..cbb9d66 100644 --- a/src/config.c +++ b/src/config.c @@ -366,6 +366,14 @@ void add_entry (char *key, char *value) else if (strcmp (key, "time1_font") == 0) { time1_font_desc = pango_font_description_from_string (value); } + else if (strcmp(key, "time1_timezone") == 0) { + if (strlen(value) > 0) + time1_timezone = strdup(value); + } + else if (strcmp(key, "time2_timezone") == 0) { + if (strlen(value) > 0) + time2_timezone = strdup(value); + } else if (strcmp (key, "time2_font") == 0) { time2_font_desc = pango_font_description_from_string (value); } @@ -391,6 +399,10 @@ void add_entry (char *key, char *value) if (strlen(value) > 0) time_tooltip_format = strdup (value); } + else if (strcmp(key, "clock_tooltip_timezone") == 0) { + if (strlen(value) > 0) + time_tooltip_timezone = strdup(value); + } else if (strcmp(key, "clock_lclick_command") == 0) { if (strlen(value) > 0) clock_lclick_command = strdup(value); diff --git a/src/panel.c b/src/panel.c index 6da20c5..e2bbf9f 100644 --- a/src/panel.c +++ b/src/panel.c @@ -151,8 +151,9 @@ void init_panel() 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 }; - 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); + XSetWindowAttributes att = { .event_mask=event_mask, .colormap=server.colormap, .background_pixel=0, .border_pixel=0 }; + unsigned long mask = CWEventMask|CWColormap|CWBackPixel|CWBorderPixel; + p->main_win = XCreateWindow(server.dsp, server.root_win, p->posx, p->posy, p->area.width, p->area.height, 0, server.depth, InputOutput, server.visual, mask, &att); } else { // old panel diff --git a/src/server.c b/src/server.c index 511eca5..475c0b6 100644 --- a/src/server.c +++ b/src/server.c @@ -18,6 +18,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. **************************************************************************/ +#include + #include #include #include @@ -94,6 +96,7 @@ void server_init_atoms () void cleanup_server() { if (name_trayer) free(name_trayer); + XFreeColormap(server.dsp, server.colormap); } @@ -299,3 +302,38 @@ void get_desktops() } +void server_init_visual() +{ + int real_transparency = 0; + XVisualInfo *xvi; + XVisualInfo templ = { .screen=server.screen, .depth=32, .class=TrueColor }; + int nvi; + xvi = XGetVisualInfo(server.dsp, VisualScreenMask|VisualDepthMask|VisualClassMask, &templ, &nvi); + + Visual *visual = 0; + if (xvi) { + int i; + XRenderPictFormat *format; + for (i = 0; i < nvi; i++) { + format = XRenderFindVisualFormat(server.dsp, xvi[i].visual); + if (format->type == PictTypeDirect && format->direct.alphaMask) { + visual = xvi[i].visual; + break; + } + } + } + XFree (xvi); + + if (visual && real_transparency) { + printf("real transparency on... depth: %d\n", server.depth); + server.depth = 32; + server.colormap = XCreateColormap(server.dsp, server.root_win, visual, AllocNone); + server.visual = visual; + } + else { + server.depth = DefaultDepth(server.dsp, server.screen); + printf("real transparency off.... depth: %d\n", server.depth); + server.colormap = DefaultColormap(server.dsp, server.screen); + server.visual = DefaultVisual(server.dsp, server.screen); + } +} diff --git a/src/server.h b/src/server.h index aba1132..2769ff8 100644 --- a/src/server.h +++ b/src/server.h @@ -95,6 +95,7 @@ typedef struct // root background Pixmap root_pmap; GC gc; + Colormap colormap; Global_atom atom; } Server_global; @@ -110,6 +111,7 @@ void server_refresh_root_pixmap (); void server_refresh_main_pixmap (); void server_catch_error (Display *d, XErrorEvent *ev); void server_init_atoms (); +void server_init_visual(); // detect root background void get_root_pixmap(); diff --git a/src/tint.c b/src/tint.c index cdb72a8..cd0b2b8 100644 --- a/src/tint.c +++ b/src/tint.c @@ -101,17 +101,15 @@ void init (int argc, char *argv[]) server_init_atoms (); server.screen = DefaultScreen (server.dsp); server.root_win = RootWindow(server.dsp, server.screen); - server.depth = DefaultDepth (server.dsp, server.screen); - server.visual = DefaultVisual (server.dsp, server.screen); server.desktop = server_get_current_desktop (); XGCValues gcv; server.gc = XCreateGC (server.dsp, server.root_win, (unsigned long)0, &gcv); - + server_init_visual(); XSetErrorHandler ((XErrorHandler) server_catch_error); imlib_context_set_display (server.dsp); imlib_context_set_visual (server.visual); - imlib_context_set_colormap (DefaultColormap (server.dsp, server.screen)); + imlib_context_set_colormap (server.colormap); /* Catch events */ XSelectInput (server.dsp, server.root_win, PropertyChangeMask|StructureNotifyMask); diff --git a/src/tooltip/tooltip.c b/src/tooltip/tooltip.c index 7bfd709..e63c1eb 100644 --- a/src/tooltip/tooltip.c +++ b/src/tooltip/tooltip.c @@ -62,8 +62,12 @@ void init_tooltip() XSetWindowAttributes attr; attr.override_redirect = True; attr.event_mask = StructureNotifyMask; + attr.colormap = server.colormap; + attr.background_pixel = 0; + attr.border_pixel = 0; + unsigned long mask = CWEventMask|CWColormap|CWBorderPixel|CWBackPixel|CWOverrideRedirect; if (g_tooltip.window) XDestroyWindow(server.dsp, g_tooltip.window); - g_tooltip.window = XCreateWindow(server.dsp, server.root_win, 0, 0, 100, 20, 0, server.depth, InputOutput, CopyFromParent, CWOverrideRedirect|CWEventMask, &attr); + g_tooltip.window = XCreateWindow(server.dsp, server.root_win, 0, 0, 100, 20, 0, server.depth, InputOutput, server.visual, mask, &attr); } @@ -212,7 +216,7 @@ void tooltip_update() c = cairo_create(cs); Color bc = g_tooltip.background_color; cairo_rectangle(c, 0, 0, width, height); - cairo_set_source_rgb(c, bc.color[0], bc.color[1], bc.color[2]); + cairo_set_source_rgba(c, bc.color[0], bc.color[1], bc.color[2], bc.alpha); cairo_fill(c); Border b = g_tooltip.border; cairo_set_source_rgba(c, b.color[0], b.color[1], b.color[2], b.alpha);