]> Dogcows Code - chaz/tint2/blob - src/clock/clock.c
ec17a9fad6659477022a0ca76412cad02b9af8e0
[chaz/tint2] / src / clock / clock.c
1 /**************************************************************************
2 *
3 * Tint2 : clock
4 *
5 * Copyright (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr) from Omega distribution
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 **************************************************************************/
19
20 #include <string.h>
21 #include <stdio.h>
22 #include <cairo.h>
23 #include <cairo-xlib.h>
24 #include <pango/pangocairo.h>
25 #include <stdlib.h>
26
27 #include "window.h"
28 #include "server.h"
29 #include "panel.h"
30 #include "clock.h"
31 #include "timer.h"
32 #include "common.h"
33
34
35 char *time1_format;
36 char *time1_timezone;
37 char *time2_format;
38 char *time2_timezone;
39 char *time_tooltip_format;
40 char *time_tooltip_timezone;
41 char *clock_lclick_command;
42 char *clock_rclick_command;
43 struct timeval time_clock;
44 PangoFontDescription *time1_font_desc;
45 PangoFontDescription *time2_font_desc;
46 static char buf_time[256];
47 static char buf_date[256];
48 static char buf_tooltip[512];
49 int clock_enabled;
50 static timeout* clock_timeout;
51
52
53 void default_clock()
54 {
55 clock_enabled = 0;
56 clock_timeout = 0;
57 time1_format = 0;
58 time1_timezone = 0;
59 time2_format = 0;
60 time2_timezone = 0;
61 time_tooltip_format = 0;
62 time_tooltip_timezone = 0;
63 clock_lclick_command = 0;
64 clock_rclick_command = 0;
65 time1_font_desc = 0;
66 time2_font_desc = 0;
67 }
68
69 void cleanup_clock()
70 {
71 if (time1_font_desc) pango_font_description_free(time1_font_desc);
72 if (time2_font_desc) pango_font_description_free(time2_font_desc);
73 if (time1_format) g_free(time1_format);
74 if (time2_format) g_free(time2_format);
75 if (time_tooltip_format) g_free(time_tooltip_format);
76 if (time1_timezone) g_free(time1_timezone);
77 if (time2_timezone) g_free(time2_timezone);
78 if (time_tooltip_timezone) g_free(time_tooltip_timezone);
79 if (clock_lclick_command) g_free(clock_lclick_command);
80 if (clock_rclick_command) g_free(clock_rclick_command);
81 if (clock_timeout) stop_timeout(clock_timeout);
82 }
83
84
85 void update_clocks_sec(void* arg)
86 {
87 gettimeofday(&time_clock, 0);
88 int i;
89 if (time1_format) {
90 for (i=0 ; i < nb_panel ; i++)
91 panel1[i].clock.area.resize = 1;
92 }
93 panel_refresh = 1;
94 }
95
96 void update_clocks_min(void* arg)
97 {
98 // remember old_sec because after suspend/hibernate the clock should be updated directly, and not
99 // on next minute change
100 time_t old_sec = time_clock.tv_sec;
101 gettimeofday(&time_clock, 0);
102 if (time_clock.tv_sec % 60 == 0 || time_clock.tv_sec - old_sec > 60) {
103 int i;
104 if (time1_format) {
105 for (i=0 ; i < nb_panel ; i++)
106 panel1[i].clock.area.resize = 1;
107 }
108 panel_refresh = 1;
109 }
110 }
111
112 struct tm* clock_gettime_for_tz(const char* timezone) {
113 if (timezone) {
114 const char* old_tz = getenv("TZ");
115 setenv("TZ", timezone, 1);
116 struct tm* result = localtime(&time_clock.tv_sec);
117 if (old_tz) setenv("TZ", old_tz, 1);
118 else unsetenv("TZ");
119 return result;
120 }
121 else return localtime(&time_clock.tv_sec);
122 }
123
124 const char* clock_get_tooltip(void* obj)
125 {
126 strftime(buf_tooltip, sizeof(buf_tooltip), time_tooltip_format, clock_gettime_for_tz(time_tooltip_timezone));
127 return buf_tooltip;
128 }
129
130
131 void init_clock()
132 {
133 if(time1_format && clock_timeout==0) {
134 if (strchr(time1_format, 'S') || strchr(time1_format, 'T') || strchr(time1_format, 'r'))
135 clock_timeout = add_timeout(10, 1000, update_clocks_sec, 0);
136 else
137 clock_timeout = add_timeout(10, 1000, update_clocks_min, 0);
138 }
139 }
140
141
142 void init_clock_panel(void *p)
143 {
144 Panel *panel =(Panel*)p;
145 Clock *clock = &panel->clock;
146
147 clock->area.parent = p;
148 clock->area.panel = p;
149 clock->area._draw_foreground = draw_clock;
150 clock->area.size_mode = SIZE_BY_CONTENT;
151 clock->area._resize = resize_clock;
152 // check consistency
153 if (time1_format == 0)
154 return;
155
156 clock->area.resize = 1;
157 clock->area.on_screen = 1;
158
159 if (time_tooltip_format) {
160 clock->area._get_tooltip_text = clock_get_tooltip;
161 strftime(buf_tooltip, sizeof(buf_tooltip), time_tooltip_format, clock_gettime_for_tz(time_tooltip_timezone));
162 }
163 }
164
165
166 void draw_clock (void *obj, cairo_t *c)
167 {
168 Clock *clock = obj;
169 PangoLayout *layout;
170
171 layout = pango_cairo_create_layout (c);
172
173 // draw layout
174 pango_layout_set_font_description (layout, time1_font_desc);
175 pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
176 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
177 pango_layout_set_text (layout, buf_time, strlen(buf_time));
178
179 cairo_set_source_rgba (c, clock->font.color[0], clock->font.color[1], clock->font.color[2], clock->font.alpha);
180
181 pango_cairo_update_layout (c, layout);
182 cairo_move_to (c, 0, clock->time1_posy);
183 pango_cairo_show_layout (c, layout);
184
185 if (time2_format) {
186 pango_layout_set_font_description (layout, time2_font_desc);
187 pango_layout_set_indent(layout, 0);
188 pango_layout_set_text (layout, buf_date, strlen(buf_date));
189 pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
190
191 pango_cairo_update_layout (c, layout);
192 cairo_move_to (c, 0, clock->time2_posy);
193 pango_cairo_show_layout (c, layout);
194 }
195
196 g_object_unref (layout);
197 }
198
199
200 int resize_clock (void *obj)
201 {
202 Clock *clock = obj;
203 Panel *panel = clock->area.panel;
204 int time_height_ink, time_height, time_width, date_height_ink, date_height, date_width, ret = 0;
205
206 clock->area.redraw = 1;
207
208 date_height = date_width = 0;
209 strftime(buf_time, sizeof(buf_time), time1_format, clock_gettime_for_tz(time1_timezone));
210 get_text_size2(time1_font_desc, &time_height_ink, &time_height, &time_width, panel->area.height, panel->area.width, buf_time, strlen(buf_time));
211 if (time2_format) {
212 strftime(buf_date, sizeof(buf_date), time2_format, clock_gettime_for_tz(time2_timezone));
213 get_text_size2(time2_font_desc, &date_height_ink, &date_height, &date_width, panel->area.height, panel->area.width, buf_date, strlen(buf_date));
214 }
215
216 if (panel_horizontal) {
217 int new_size = (time_width > date_width) ? time_width : date_width;
218 new_size += (2*clock->area.paddingxlr) + (2*clock->area.bg->border.width);
219 if (new_size > clock->area.width || new_size < (clock->area.width-6)) {
220 // we try to limit the number of resize
221 clock->area.width = new_size + 1;
222 clock->time1_posy = (clock->area.height - time_height) / 2;
223 if (time2_format) {
224 clock->time1_posy -= (date_height)/2;
225 clock->time2_posy = clock->time1_posy + time_height;
226 }
227 ret = 1;
228 }
229 }
230 else {
231 int new_size = time_height + date_height + (2 * (clock->area.paddingxlr + clock->area.bg->border.width));
232 if (new_size != clock->area.height) {
233 // we try to limit the number of resize
234 clock->area.height = new_size;
235 clock->time1_posy = (clock->area.height - time_height) / 2;
236 if (time2_format) {
237 clock->time1_posy -= (date_height)/2;
238 clock->time2_posy = clock->time1_posy + time_height;
239 }
240 ret = 1;
241 }
242 }
243
244 return ret;
245 }
246
247
248 void clock_action(int button)
249 {
250 char *command = 0;
251 switch (button) {
252 case 1:
253 command = clock_lclick_command;
254 break;
255 case 3:
256 command = clock_rclick_command;
257 break;
258 }
259 tint_exec(command);
260 }
261
This page took 0.046761 seconds and 3 git commands to generate.