]> Dogcows Code - chaz/tint2/blob - src/taskbar/task.c
*fix* use XFlush to really make use of the tooltip timeouts and do not rely on some...
[chaz/tint2] / src / taskbar / task.c
1 /**************************************************************************
2 *
3 * Tint2 : task
4 *
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 **************************************************************************/
20
21 #include <X11/Xlib.h>
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <glib.h>
28 #include <unistd.h>
29
30 #include "window.h"
31 #include "task.h"
32 #include "server.h"
33 #include "panel.h"
34 #include "tooltip.h"
35 #include "timer.h"
36
37 static int urgent_timer = 0;
38
39 Task *add_task (Window win)
40 {
41 if (!win) return 0;
42 if (window_is_hidden(win)) return 0;
43
44 int monitor;
45
46 Task new_tsk;
47 new_tsk.win = win;
48 new_tsk.desktop = window_get_desktop (win);
49 if (nb_panel > 1) {
50 monitor = window_get_monitor (win);
51 if (monitor >= nb_panel) monitor = 0;
52 }
53 else monitor = 0;
54 new_tsk.area.panel = &panel1[monitor];
55
56 // allocate only one title and one icon
57 // even with task_on_all_desktop and with task_on_all_panel
58 new_tsk.title = 0;
59 new_tsk.icon = new_tsk.icon_active = NULL;
60 get_title(&new_tsk);
61 get_icon(&new_tsk);
62
63 //printf("task %s : desktop %d, monitor %d\n", new_tsk->title, desktop, monitor);
64 XSelectInput (server.dsp, new_tsk.win, PropertyChangeMask|StructureNotifyMask);
65
66 Taskbar *tskbar;
67 Task *new_tsk2=0;
68 int i, j;
69 for (i=0 ; i < nb_panel ; i++) {
70 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
71 if (new_tsk.desktop != ALLDESKTOP && new_tsk.desktop != j) continue;
72 if (nb_panel > 1 && panel1[i].monitor != monitor) continue;
73
74 tskbar = &panel1[i].taskbar[j];
75 new_tsk2 = malloc(sizeof(Task));
76 memcpy(&new_tsk2->area, &panel1[i].g_task.area, sizeof(Area));
77 new_tsk2->area.parent = tskbar;
78 new_tsk2->win = new_tsk.win;
79 new_tsk2->desktop = new_tsk.desktop;
80 if (new_tsk2->desktop == ALLDESKTOP && server.desktop != j) {
81 // hide ALLDESKTOP task on non-current desktop
82 new_tsk2->area.on_screen = 0;
83 }
84 new_tsk2->title = new_tsk.title;
85 new_tsk2->icon = new_tsk.icon;
86 new_tsk2->icon_active = new_tsk.icon_active;
87 new_tsk2->icon_width = new_tsk.icon_width;
88 new_tsk2->icon_height = new_tsk.icon_height;
89 tskbar->area.list = g_slist_append(tskbar->area.list, new_tsk2);
90 tskbar->area.resize = 1;
91 //printf("add_task panel %d, desktop %d, task %s\n", i, j, new_tsk2->title);
92 }
93 }
94 return new_tsk2;
95 }
96
97
98 void remove_task (Task *tsk)
99 {
100 if (!tsk) return;
101
102 Window win = tsk->win;
103 int desktop = tsk->desktop;
104
105 if (g_tooltip.task == tsk) {
106 tooltip_hide();
107 alarm(0);
108 g_tooltip.task = 0;
109 }
110
111 // free title and icon just for the first task
112 // even with task_on_all_desktop and with task_on_all_panel
113 //printf("remove_task %s %d\n", tsk->title, tsk->desktop);
114 if (tsk->title)
115 free (tsk->title);
116 if (tsk->icon) {
117 imlib_context_set_image(tsk->icon);
118 imlib_free_image();
119 imlib_context_set_image(tsk->icon_active);
120 imlib_free_image();
121 tsk->icon = tsk->icon_active = NULL;
122 }
123
124 int i, j;
125 Task *tsk2;
126 Taskbar *tskbar;
127 for (i=0 ; i < nb_panel ; i++) {
128 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
129 if (desktop != ALLDESKTOP && desktop != j) continue;
130
131 GSList *l0;
132 tskbar = &panel1[i].taskbar[j];
133 for (l0 = tskbar->area.list; l0 ; ) {
134 tsk2 = l0->data;
135 l0 = l0->next;
136 if (win == tsk2->win) {
137 tskbar->area.list = g_slist_remove(tskbar->area.list, tsk2);
138 tskbar->area.resize = 1;
139
140 if (tsk2 == task_active)
141 task_active = 0;
142 if (tsk2 == task_drag)
143 task_drag = 0;
144
145 XFreePixmap (server.dsp, tsk2->area.pix.pmap);
146 XFreePixmap (server.dsp, tsk2->area.pix_active.pmap);
147 free(tsk2);
148 }
149 }
150 }
151 }
152 }
153
154
155 void get_title(Task *tsk)
156 {
157 Panel *panel = tsk->area.panel;
158 char *title, *name;
159
160 if (!panel->g_task.text && !g_tooltip.enabled) return;
161
162 if (g_tooltip.task == tsk) {
163 tooltip_hide();
164 alarm(0);
165 g_tooltip.task = 0;
166 }
167
168 name = server_get_property (tsk->win, server.atom._NET_WM_VISIBLE_NAME, server.atom.UTF8_STRING, 0);
169 if (!name || !strlen(name)) {
170 name = server_get_property (tsk->win, server.atom._NET_WM_NAME, server.atom.UTF8_STRING, 0);
171 if (!name || !strlen(name)) {
172 name = server_get_property (tsk->win, server.atom.WM_NAME, XA_STRING, 0);
173 if (!name || !strlen(name)) {
174 name = malloc(10);
175 strcpy(name, "Untitled");
176 }
177 }
178 }
179
180 // add space before title
181 title = malloc(strlen(name)+2);
182 if (panel->g_task.icon) strcpy(title, " ");
183 else title[0] = 0;
184 strcat(title, name);
185 if (name) XFree (name);
186
187 tsk->area.redraw = 1;
188 if (tsk->title)
189 free(tsk->title);
190 tsk->title = title;
191 }
192
193
194 void get_icon (Task *tsk)
195 {
196 Panel *panel = tsk->area.panel;
197 if (!panel->g_task.icon) return;
198 int i;
199 Imlib_Image img = NULL;
200 XWMHints *hints = 0;
201 long *data = 0;
202
203 if (tsk->icon) {
204 imlib_context_set_image(tsk->icon);
205 imlib_free_image();
206 imlib_context_set_image(tsk->icon_active);
207 imlib_free_image();
208 tsk->icon = tsk->icon_active = NULL;
209 }
210 tsk->area.redraw = 1;
211
212 data = server_get_property (tsk->win, server.atom._NET_WM_ICON, XA_CARDINAL, &i);
213 if (data) {
214 // get ARGB icon
215 int w, h;
216 long *tmp_data;
217
218 tmp_data = get_best_icon (data, get_icon_count (data, i), i, &w, &h, panel->g_task.icon_size1);
219
220 #ifdef __x86_64__
221 DATA32 icon_data[w * h];
222 int length = w * h;
223 for (i = 0; i < length; ++i)
224 icon_data[i] = tmp_data[i];
225 img = imlib_create_image_using_copied_data (w, h, icon_data);
226 #else
227 img = imlib_create_image_using_data (w, h, (DATA32*)tmp_data);
228 #endif
229 }
230 else {
231 // get Pixmap icon
232 hints = XGetWMHints(server.dsp, tsk->win);
233 if (hints) {
234 if (hints->flags & IconPixmapHint && hints->icon_pixmap != 0) {
235 // get width, height and depth for the pixmap
236 Window root;
237 int icon_x, icon_y;
238 uint border_width, bpp;
239 uint w, h;
240
241 //printf(" get pixmap\n");
242 XGetGeometry(server.dsp, hints->icon_pixmap, &root, &icon_x, &icon_y, &w, &h, &border_width, &bpp);
243 imlib_context_set_drawable(hints->icon_pixmap);
244 img = imlib_create_image_from_drawable(hints->icon_mask, 0, 0, w, h, 0);
245 }
246 }
247 }
248 if (img == NULL) {
249 imlib_context_set_image(default_icon);
250 img = imlib_clone_image();
251 }
252
253 // transform icons
254 imlib_context_set_image(img);
255 imlib_image_set_has_alpha(1);
256 int w, h;
257 w = imlib_image_get_width();
258 h = imlib_image_get_height();
259 tsk->icon = imlib_create_cropped_scaled_image(0, 0, w, h, panel->g_task.icon_size1, panel->g_task.icon_size1);
260 imlib_free_image();
261
262 imlib_context_set_image(tsk->icon);
263 tsk->icon_width = imlib_image_get_width();
264 tsk->icon_height = imlib_image_get_height();
265 tsk->icon_active = imlib_clone_image();
266
267 DATA32 *data32;
268 if (panel->g_task.alpha != 100 || panel->g_task.saturation != 0 || panel->g_task.brightness != 0) {
269 data32 = imlib_image_get_data();
270 adjust_asb(data32, tsk->icon_width, tsk->icon_height, panel->g_task.alpha, (float)panel->g_task.saturation/100, (float)panel->g_task.brightness/100);
271 imlib_image_put_back_data(data32);
272 }
273
274 if (panel->g_task.alpha_active != 100 || panel->g_task.saturation_active != 0 || panel->g_task.brightness_active != 0) {
275 imlib_context_set_image(tsk->icon_active);
276 data32 = imlib_image_get_data();
277 adjust_asb(data32, tsk->icon_width, tsk->icon_height, panel->g_task.alpha_active, (float)panel->g_task.saturation_active/100, (float)panel->g_task.brightness_active/100);
278 imlib_image_put_back_data(data32);
279 }
280
281 if (hints)
282 XFree(hints);
283 if (data)
284 XFree (data);
285 }
286
287
288 void draw_task_icon (Task *tsk, int text_width, int active)
289 {
290 if (tsk->icon == NULL || tsk->icon_active == NULL) return;
291
292 // Find pos
293 int pos_x;
294 Panel *panel = (Panel*)tsk->area.panel;
295 if (panel->g_task.centered) {
296 if (panel->g_task.text)
297 pos_x = (tsk->area.width - text_width - panel->g_task.icon_size1) / 2;
298 else
299 pos_x = (tsk->area.width - panel->g_task.icon_size1) / 2;
300 }
301 else pos_x = panel->g_task.area.paddingxlr + panel->g_task.area.pix.border.width;
302
303 // Render
304 Pixmap *pmap;
305 if (active == 0) {
306 imlib_context_set_image (tsk->icon);
307 pmap = &tsk->area.pix.pmap;
308 }
309 else {
310 imlib_context_set_image (tsk->icon_active);
311 pmap = &tsk->area.pix_active.pmap;
312 }
313 imlib_context_set_drawable (*pmap);
314 imlib_render_image_on_drawable (pos_x, panel->g_task.icon_posy);
315 }
316
317
318 void draw_task (void *obj, cairo_t *c, int active)
319 {
320 Task *tsk = obj;
321 PangoLayout *layout;
322 config_color *config_text;
323 int width=0, height;
324 Panel *panel = (Panel*)tsk->area.panel;
325
326 if (panel->g_task.text) {
327 /* Layout */
328 layout = pango_cairo_create_layout (c);
329 pango_layout_set_font_description (layout, panel->g_task.font_desc);
330 pango_layout_set_text (layout, tsk->title, -1);
331
332 /* Drawing width and Cut text */
333 // pango use U+22EF or U+2026
334 pango_layout_set_width (layout, ((Taskbar*)tsk->area.parent)->text_width * PANGO_SCALE);
335 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
336
337 /* Center text */
338 if (panel->g_task.centered) pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
339 else pango_layout_set_alignment (layout, PANGO_ALIGN_LEFT);
340
341 pango_layout_get_pixel_size (layout, &width, &height);
342
343 if (active) config_text = &panel->g_task.font_active;
344 else config_text = &panel->g_task.font;
345
346 cairo_set_source_rgba (c, config_text->color[0], config_text->color[1], config_text->color[2], config_text->alpha);
347
348 pango_cairo_update_layout (c, layout);
349 cairo_move_to (c, panel->g_task.text_posx, panel->g_task.text_posy);
350 pango_cairo_show_layout (c, layout);
351
352 if (panel->g_task.font_shadow) {
353 cairo_set_source_rgba (c, 0.0, 0.0, 0.0, 0.5);
354 pango_cairo_update_layout (c, layout);
355 cairo_move_to (c, panel->g_task.text_posx + 1, panel->g_task.text_posy + 1);
356 pango_cairo_show_layout (c, layout);
357 }
358 g_object_unref (layout);
359 }
360
361 if (panel->g_task.icon) {
362 // icon use same opacity as text
363 draw_task_icon (tsk, width, active);
364 }
365 }
366
367
368 void active_task()
369 {
370 GSList *l0;
371 int i, j;
372 Task *tsk1, *tsk2;
373
374 if (task_active) {
375 for (i=0 ; i < nb_panel ; i++) {
376 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
377 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
378 tsk1 = l0->data;
379 tsk1->area.is_active = 0;
380 }
381 }
382 }
383 task_active = 0;
384 }
385
386 Window w1 = window_get_active ();
387 //printf("Change active task %ld\n", w1);
388
389 tsk2 = task_get_task(w1);
390 if (!tsk2) {
391 Window w2;
392 if (XGetTransientForHint(server.dsp, w1, &w2) != 0)
393 if (w2) tsk2 = task_get_task(w2);
394 }
395 if ( is_urgent(tsk2) ) {
396 del_urgent(tsk2);
397 }
398 // put active state on all task (multi_desktop)
399 if (tsk2) {
400 for (i=0 ; i < nb_panel ; i++) {
401 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
402 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
403 tsk1 = l0->data;
404 if (tsk1->win == tsk2->win) {
405 tsk1->area.is_active = 1;
406 }
407 }
408 }
409 }
410 task_active = tsk2;
411 }
412 }
413
414
415 void blink_urgent()
416 {
417 GSList* urgent_task = urgent_list;
418 while (urgent_task) {
419 Task_urgent* t = urgent_task->data;
420 if ( t->tick < max_tick_urgent) {
421 t->tsk->area.is_active = !t->tsk->area.is_active;
422 t->tsk->area.redraw = 1;
423 t->tick++;
424 }
425 urgent_task = urgent_task->next;
426 }
427 panel_refresh = 1;
428 }
429
430
431 void add_urgent(Task *tsk)
432 {
433 // first check if task is already in the list and reset the counter
434 GSList* urgent_task = urgent_list;
435 while (urgent_task) {
436 Task_urgent* t = urgent_task->data;
437 if (t->tsk == tsk) {
438 t->tick = 0;
439 return;
440 }
441 urgent_task = urgent_task->next;
442 }
443
444 // not yet in the list, so we have to add it
445 Task_urgent* t = malloc(sizeof(Task_urgent));
446 if (!t)
447 return;
448 t->tsk = tsk;
449 t->tick = 0;
450 urgent_list = g_slist_prepend(urgent_list, t);
451
452 if (urgent_timer == 0)
453 urgent_timer = install_timer(0, 1000000, 1, 0, blink_urgent);
454 else
455 reset_timer(urgent_timer, 0, 1000000, 1, 0);
456 }
457
458
459 void del_urgent(Task *tsk)
460 {
461 GSList* urgent_task = urgent_list;
462 while (urgent_task) {
463 Task_urgent* t = urgent_task->data;
464 if (t->tsk == tsk) {
465 urgent_list = g_slist_remove(urgent_list, t);
466 free(t);
467 if (!urgent_list)
468 reset_timer(urgent_timer, 0, 0, 0, 0);
469 return;
470 }
471 urgent_task = urgent_task->next;
472 }
473 }
474
475 int is_urgent(Task *tsk)
476 {
477 GSList* urgent_task = urgent_list;
478 while (urgent_task) {
479 Task_urgent* t = urgent_task->data;
480 if (t->tsk == tsk)
481 return 1;
482 urgent_task = urgent_task->next;
483 }
484 return 0;
485 }
This page took 0.056511 seconds and 5 git commands to generate.