]> Dogcows Code - chaz/tint2/blob - src/clock/clock.c
fixed issue 98
[chaz/tint2] / src / clock / clock.c
1 /**************************************************************************
2 *
3 * Tint2 : clock
4 *
5 * Copyright (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
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 <unistd.h>
26
27 #include "window.h"
28 #include "server.h"
29 #include "taskbar.h"
30 #include "panel.h"
31 #include "area.h"
32 #include "clock.h"
33
34
35 char *time1_format;
36 char *time2_format;
37 char *clock_lclick_command;
38 char *clock_rclick_command;
39 struct timeval time_clock;
40 int time_precision;
41 PangoFontDescription *time1_font_desc;
42 PangoFontDescription *time2_font_desc;
43 static char buf_time[40];
44 static char buf_date[40];
45
46
47 void init_precision()
48 {
49 if (!time1_format) time_precision = 60;
50 else if (strchr(time1_format, 'S')) time_precision = 1;
51 else if (strchr(time1_format, 'T')) time_precision = 1;
52 else if (strchr(time1_format, 'r')) time_precision = 1;
53 else time_precision = 60;
54 }
55
56
57 void init_clock()
58 {
59 Panel *panel;
60 Clock *clock;
61 int i, time_height, time_height_ink, date_height, date_height_ink;
62
63 init_precision();
64 // update clock to force update (-time_precision)
65 struct timeval stv;
66 gettimeofday(&stv, 0);
67 time_clock.tv_sec = stv.tv_sec - time_precision;
68 time_clock.tv_sec -= time_clock.tv_sec % time_precision;
69
70 for (i=0 ; i < nb_panel ; i++) {
71 panel = &panel1[i];
72 clock = &panel->clock;
73
74 clock->area.parent = panel;
75 clock->area.panel = panel;
76 clock->area._draw_foreground = draw_clock;
77 clock->area._resize = resize_clock;
78
79 if (!clock->area.on_screen) continue;
80
81 clock->area.posy = panel->area.pix.border.width + panel->area.paddingy;
82 clock->area.height = panel->area.height - (2 * clock->area.posy);
83 clock->area.resize = 1;
84 clock->area.redraw = 1;
85
86 strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec));
87 if (time2_format)
88 strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
89
90 get_text_size(time1_font_desc, &time_height_ink, &time_height, panel->area.height, buf_time, strlen(buf_time));
91 clock->time1_posy = (clock->area.height - time_height) / 2;
92
93 if (time2_format) {
94 get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
95
96 clock->time1_posy -= ((date_height_ink + 2) / 2);
97 clock->time2_posy = clock->time1_posy + time_height + 2 - (time_height - time_height_ink)/2 - (date_height - date_height_ink)/2;
98 }
99 }
100 }
101
102
103 void draw_clock (void *obj, cairo_t *c, int active)
104 {
105 Clock *clock = obj;
106 PangoLayout *layout;
107
108 //printf(" draw_clock : %s en (%d, %d)\n", buf_time, clock->area.posx, clock->area.width);
109 layout = pango_cairo_create_layout (c);
110
111 // draw layout
112 pango_layout_set_font_description (layout, time1_font_desc);
113 pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
114 pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
115 pango_layout_set_text (layout, buf_time, strlen(buf_time));
116
117 cairo_set_source_rgba (c, clock->font.color[0], clock->font.color[1], clock->font.color[2], clock->font.alpha);
118
119 pango_cairo_update_layout (c, layout);
120 cairo_move_to (c, 0, clock->time1_posy);
121 pango_cairo_show_layout (c, layout);
122
123 if (time2_format) {
124 pango_layout_set_font_description (layout, time2_font_desc);
125 pango_layout_set_indent(layout, 0);
126 pango_layout_set_text (layout, buf_date, strlen(buf_date));
127 pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
128
129 pango_cairo_update_layout (c, layout);
130 cairo_move_to (c, 0, clock->time2_posy);
131 pango_cairo_show_layout (c, layout);
132 }
133
134 g_object_unref (layout);
135 }
136
137
138 void resize_clock (void *obj)
139 {
140 Clock *clock = obj;
141 PangoLayout *layout;
142 int time_width, date_width, new_width;
143
144 time_width = date_width = 0;
145 clock->area.redraw = 1;
146
147 strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec));
148 if (time2_format)
149 strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
150
151 //printf(" resize_clock\n");
152 cairo_surface_t *cs;
153 cairo_t *c;
154 Pixmap pmap;
155 pmap = XCreatePixmap (server.dsp, server.root_win, clock->area.width, clock->area.height, server.depth);
156
157 cs = cairo_xlib_surface_create (server.dsp, pmap, server.visual, clock->area.width, clock->area.height);
158 c = cairo_create (cs);
159 layout = pango_cairo_create_layout (c);
160
161 // check width
162 pango_layout_set_font_description (layout, time1_font_desc);
163 pango_layout_set_indent(layout, 0);
164 pango_layout_set_text (layout, buf_time, strlen(buf_time));
165 pango_layout_get_pixel_size (layout, &time_width, NULL);
166 if (time2_format) {
167 pango_layout_set_font_description (layout, time2_font_desc);
168 pango_layout_set_indent(layout, 0);
169 pango_layout_set_text (layout, buf_date, strlen(buf_date));
170 pango_layout_get_pixel_size (layout, &date_width, NULL);
171 }
172
173 if (time_width > date_width) new_width = time_width;
174 else new_width = date_width;
175 new_width += (2*clock->area.paddingxlr) + (2*clock->area.pix.border.width);
176
177 if (new_width > clock->area.width || new_width < (clock->area.width-6)) {
178 Panel *panel = ((Area*)obj)->panel;
179
180 printf("clock_width %d, new_width %d\n", clock->area.width, new_width);
181 // resize clock
182 // we try to limit the number of resize
183 clock->area.width = new_width + 1;
184 clock->area.posx = panel->area.width - clock->area.width - panel->area.paddingxlr - panel->area.pix.border.width;
185
186 // resize other objects on panel
187 panel->area.resize = 1;
188 panel->battery.area.resize = 1;
189 systray.area.resize = 1;
190 panel_refresh = 1;
191 }
192
193 g_object_unref (layout);
194 cairo_destroy (c);
195 cairo_surface_destroy (cs);
196 XFreePixmap (server.dsp, pmap);
197 }
198
199
200 void clock_action(int button)
201 {
202 char *command = 0;
203 switch (button) {
204 case 1:
205 command = clock_lclick_command;
206 break;
207 case 3:
208 command = clock_rclick_command;
209 break;
210 }
211 if (command) {
212 pid_t pid;
213 pid = fork();
214 if (pid == 0) {
215 execl("/bin/sh", "/bin/sh", "-c", command, NULL);
216 _exit(0);
217 }
218 }
219 }
220
This page took 0.047085 seconds and 5 git commands to generate.