]> Dogcows Code - chaz/tint2/blob - src/taskbar/task.c
*fix* better internal urgent task handling
[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 "taskbar.h"
33 #include "server.h"
34 #include "panel.h"
35 #include "tooltip.h"
36 #include "timer.h"
37
38 static const struct timeout* urgent_timeout = 0;
39 static GSList* urgent_list = 0;
40
41 const char* task_get_tooltip(void* obj)
42 {
43 Task* t = obj;
44 return t->title;
45 }
46
47
48 Task *add_task (Window win)
49 {
50 if (!win) return 0;
51 if (window_is_hidden(win)) return 0;
52
53 int monitor;
54
55 Task new_tsk;
56 new_tsk.win = win;
57 new_tsk.desktop = window_get_desktop (win);
58 if (nb_panel > 1) {
59 monitor = window_get_monitor (win);
60 if (monitor >= nb_panel) monitor = 0;
61 }
62 else monitor = 0;
63 new_tsk.area.panel = &panel1[monitor];
64
65 // allocate only one title and one icon
66 // even with task_on_all_desktop and with task_on_all_panel
67 new_tsk.title = 0;
68 new_tsk.icon = new_tsk.icon_active = NULL;
69 get_title(&new_tsk);
70 get_icon(&new_tsk);
71
72 //printf("task %s : desktop %d, monitor %d\n", new_tsk->title, desktop, monitor);
73 XSelectInput (server.dsp, new_tsk.win, PropertyChangeMask|StructureNotifyMask);
74
75 Taskbar *tskbar;
76 Task *new_tsk2=0;
77 int i, j;
78 for (i=0 ; i < nb_panel ; i++) {
79 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
80 if (new_tsk.desktop != ALLDESKTOP && new_tsk.desktop != j) continue;
81 if (nb_panel > 1 && panel1[i].monitor != monitor) continue;
82
83 tskbar = &panel1[i].taskbar[j];
84 new_tsk2 = malloc(sizeof(Task));
85 memcpy(&new_tsk2->area, &panel1[i].g_task.area, sizeof(Area));
86 new_tsk2->area.parent = tskbar;
87 new_tsk2->win = new_tsk.win;
88 new_tsk2->desktop = new_tsk.desktop;
89 if (new_tsk2->desktop == ALLDESKTOP && server.desktop != j) {
90 // hide ALLDESKTOP task on non-current desktop
91 new_tsk2->area.on_screen = 0;
92 }
93 new_tsk2->title = new_tsk.title;
94 new_tsk2->area._get_tooltip_text = task_get_tooltip;
95 new_tsk2->icon = new_tsk.icon;
96 new_tsk2->icon_active = new_tsk.icon_active;
97 new_tsk2->icon_width = new_tsk.icon_width;
98 new_tsk2->icon_height = new_tsk.icon_height;
99 tskbar->area.list = g_slist_append(tskbar->area.list, new_tsk2);
100 tskbar->area.resize = 1;
101 //printf("add_task panel %d, desktop %d, task %s\n", i, j, new_tsk2->title);
102 }
103 }
104 if (window_is_urgent(win))
105 add_urgent(new_tsk2);
106 return new_tsk2;
107 }
108
109
110 void remove_task (Task *tsk)
111 {
112 if (!tsk) return;
113
114 Window win = tsk->win;
115 int desktop = tsk->desktop;
116
117 // free title and icon just for the first task
118 // even with task_on_all_desktop and with task_on_all_panel
119 //printf("remove_task %s %d\n", tsk->title, tsk->desktop);
120 if (tsk->title)
121 free (tsk->title);
122 if (tsk->icon) {
123 imlib_context_set_image(tsk->icon);
124 imlib_free_image();
125 imlib_context_set_image(tsk->icon_active);
126 imlib_free_image();
127 tsk->icon = tsk->icon_active = NULL;
128 }
129
130 int i, j;
131 Task *tsk2;
132 Taskbar *tskbar;
133 for (i=0 ; i < nb_panel ; i++) {
134 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
135 if (desktop != ALLDESKTOP && desktop != j) continue;
136
137 GSList *l0;
138 tskbar = &panel1[i].taskbar[j];
139 for (l0 = tskbar->area.list; l0 ; ) {
140 tsk2 = l0->data;
141 l0 = l0->next;
142 if (win == tsk2->win) {
143 tskbar->area.list = g_slist_remove(tskbar->area.list, tsk2);
144 tskbar->area.resize = 1;
145
146 if (tsk2 == task_active)
147 task_active = 0;
148 if (tsk2 == task_drag)
149 task_drag = 0;
150 if (g_slist_find(urgent_list, tsk2))
151 del_urgent(tsk2);
152
153 XFreePixmap (server.dsp, tsk2->area.pix.pmap);
154 XFreePixmap (server.dsp, tsk2->area.pix_active.pmap);
155 free(tsk2);
156 }
157 }
158 }
159 }
160 }
161
162
163 void get_title(Task *tsk)
164 {
165 Panel *panel = tsk->area.panel;
166 char *title, *name;
167
168 if (!panel->g_task.text && !g_tooltip.enabled) return;
169
170 name = server_get_property (tsk->win, server.atom._NET_WM_VISIBLE_NAME, server.atom.UTF8_STRING, 0);
171 if (!name || !strlen(name)) {
172 name = server_get_property (tsk->win, server.atom._NET_WM_NAME, server.atom.UTF8_STRING, 0);
173 if (!name || !strlen(name)) {
174 name = server_get_property (tsk->win, server.atom.WM_NAME, XA_STRING, 0);
175 if (!name || !strlen(name)) {
176 name = malloc(10);
177 strcpy(name, "Untitled");
178 }
179 }
180 }
181
182 // add space before title
183 title = malloc(strlen(name)+2);
184 if (panel->g_task.icon) strcpy(title, " ");
185 else title[0] = 0;
186 strcat(title, name);
187 if (name) XFree (name);
188
189 tsk->area.redraw = 1;
190 if (tsk->title)
191 free(tsk->title);
192 tsk->title = title;
193 }
194
195
196 void get_icon (Task *tsk)
197 {
198 Panel *panel = tsk->area.panel;
199 if (!panel->g_task.icon) return;
200 int i;
201 Imlib_Image img = NULL;
202 XWMHints *hints = 0;
203 long *data = 0;
204
205 if (tsk->icon) {
206 imlib_context_set_image(tsk->icon);
207 imlib_free_image();
208 imlib_context_set_image(tsk->icon_active);
209 imlib_free_image();
210 tsk->icon = tsk->icon_active = NULL;
211 }
212 tsk->area.redraw = 1;
213
214 data = server_get_property (tsk->win, server.atom._NET_WM_ICON, XA_CARDINAL, &i);
215 if (data) {
216 // get ARGB icon
217 int w, h;
218 long *tmp_data;
219
220 tmp_data = get_best_icon (data, get_icon_count (data, i), i, &w, &h, panel->g_task.icon_size1);
221
222 #ifdef __x86_64__
223 DATA32 icon_data[w * h];
224 int length = w * h;
225 for (i = 0; i < length; ++i)
226 icon_data[i] = tmp_data[i];
227 img = imlib_create_image_using_copied_data (w, h, icon_data);
228 #else
229 img = imlib_create_image_using_data (w, h, (DATA32*)tmp_data);
230 #endif
231 }
232 else {
233 // get Pixmap icon
234 hints = XGetWMHints(server.dsp, tsk->win);
235 if (hints) {
236 if (hints->flags & IconPixmapHint && hints->icon_pixmap != 0) {
237 // get width, height and depth for the pixmap
238 Window root;
239 int icon_x, icon_y;
240 uint border_width, bpp;
241 uint w, h;
242
243 //printf(" get pixmap\n");
244 XGetGeometry(server.dsp, hints->icon_pixmap, &root, &icon_x, &icon_y, &w, &h, &border_width, &bpp);
245 imlib_context_set_drawable(hints->icon_pixmap);
246 img = imlib_create_image_from_drawable(hints->icon_mask, 0, 0, w, h, 0);
247 }
248 }
249 }
250 if (img == NULL) {
251 imlib_context_set_image(default_icon);
252 img = imlib_clone_image();
253 }
254
255 // transform icons
256 imlib_context_set_image(img);
257 imlib_image_set_has_alpha(1);
258 int w, h;
259 w = imlib_image_get_width();
260 h = imlib_image_get_height();
261 tsk->icon = imlib_create_cropped_scaled_image(0, 0, w, h, panel->g_task.icon_size1, panel->g_task.icon_size1);
262 imlib_free_image();
263
264 imlib_context_set_image(tsk->icon);
265 tsk->icon_width = imlib_image_get_width();
266 tsk->icon_height = imlib_image_get_height();
267 tsk->icon_active = imlib_clone_image();
268
269 DATA32 *data32;
270 if (panel->g_task.alpha != 100 || panel->g_task.saturation != 0 || panel->g_task.brightness != 0) {
271 data32 = imlib_image_get_data();
272 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);
273 imlib_image_put_back_data(data32);
274 }
275
276 if (panel->g_task.alpha_active != 100 || panel->g_task.saturation_active != 0 || panel->g_task.brightness_active != 0) {
277 imlib_context_set_image(tsk->icon_active);
278 data32 = imlib_image_get_data();
279 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);
280 imlib_image_put_back_data(data32);
281 }
282
283 if (hints)
284 XFree(hints);
285 if (data)
286 XFree (data);
287 }
288
289
290 void draw_task_icon (Task *tsk, int text_width, int active)
291 {
292 if (tsk->icon == NULL || tsk->icon_active == NULL) return;
293
294 // Find pos
295 int pos_x;
296 Panel *panel = (Panel*)tsk->area.panel;
297 if (panel->g_task.centered) {
298 if (panel->g_task.text)
299 pos_x = (tsk->area.width - text_width - panel->g_task.icon_size1) / 2;
300 else
301 pos_x = (tsk->area.width - panel->g_task.icon_size1) / 2;
302 }
303 else pos_x = panel->g_task.area.paddingxlr + panel->g_task.area.pix.border.width;
304
305 // Render
306 Pixmap *pmap;
307 if (active == 0) {
308 imlib_context_set_image (tsk->icon);
309 pmap = &tsk->area.pix.pmap;
310 }
311 else {
312 imlib_context_set_image (tsk->icon_active);
313 pmap = &tsk->area.pix_active.pmap;
314 }
315 imlib_context_set_drawable (*pmap);
316 imlib_render_image_on_drawable (pos_x, panel->g_task.icon_posy);
317 }
318
319
320 void draw_task (void *obj, cairo_t *c, int active)
321 {
322 Task *tsk = obj;
323 PangoLayout *layout;
324 config_color *config_text;
325 int width=0, height;
326 Panel *panel = (Panel*)tsk->area.panel;
327
328 if (panel->g_task.text) {
329 /* Layout */
330 layout = pango_cairo_create_layout (c);
331 pango_layout_set_font_description (layout, panel->g_task.font_desc);
332 pango_layout_set_text (layout, tsk->title, -1);
333
334 /* Drawing width and Cut text */
335 // pango use U+22EF or U+2026
336 pango_layout_set_width (layout, ((Taskbar*)tsk->area.parent)->text_width * PANGO_SCALE);
337 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
338
339 /* Center text */
340 if (panel->g_task.centered) pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
341 else pango_layout_set_alignment (layout, PANGO_ALIGN_LEFT);
342
343 pango_layout_get_pixel_size (layout, &width, &height);
344
345 if (active) config_text = &panel->g_task.font_active;
346 else config_text = &panel->g_task.font;
347
348 cairo_set_source_rgba (c, config_text->color[0], config_text->color[1], config_text->color[2], config_text->alpha);
349
350 pango_cairo_update_layout (c, layout);
351 cairo_move_to (c, panel->g_task.text_posx, panel->g_task.text_posy);
352 pango_cairo_show_layout (c, layout);
353
354 if (panel->g_task.font_shadow) {
355 cairo_set_source_rgba (c, 0.0, 0.0, 0.0, 0.5);
356 pango_cairo_update_layout (c, layout);
357 cairo_move_to (c, panel->g_task.text_posx + 1, panel->g_task.text_posy + 1);
358 pango_cairo_show_layout (c, layout);
359 }
360 g_object_unref (layout);
361 }
362
363 if (panel->g_task.icon) {
364 // icon use same opacity as text
365 draw_task_icon (tsk, width, active);
366 }
367 }
368
369
370 Task *next_task(Task *tsk)
371 {
372 GSList *l0;
373 int i, j;
374 Task *tsk1;
375
376 for (i=0 ; i < nb_panel ; i++) {
377 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
378 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
379 tsk1 = l0->data;
380 if (tsk1 == tsk) {
381 if (l0->next == NULL) l0 = panel1[i].taskbar[j].area.list;
382 else l0 = l0->next;
383 return l0->data;
384 }
385 }
386 }
387 }
388
389 return NULL;
390 }
391
392 Task *prev_task(Task *tsk)
393 {
394 GSList *l0;
395 int i, j;
396 Task *tsk1, *tsk2;
397
398 for (i=0 ; i < nb_panel ; i++) {
399 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
400 tsk2 = NULL;
401 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
402 tsk1 = l0->data;
403 if (tsk1 == tsk) {
404 if (l0 == panel1[i].taskbar[j].area.list) {
405 l0 = g_slist_last ( l0 );
406 tsk2 = l0->data;
407 }
408 return tsk2;
409 }
410 tsk2 = tsk1;
411 }
412 }
413 }
414
415 return NULL;
416 }
417
418
419 void active_task()
420 {
421 GSList *l0;
422 int i, j;
423 Task *tsk1, *tsk2;
424
425 if (task_active) {
426 for (i=0 ; i < nb_panel ; i++) {
427 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
428 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
429 tsk1 = l0->data;
430 tsk1->area.is_active = 0;
431 }
432 }
433 }
434 task_active = 0;
435 }
436
437 Window w1 = window_get_active ();
438 //printf("Change active task %ld\n", w1);
439
440 tsk2 = task_get_task(w1);
441 if (!tsk2) {
442 Window w2;
443 if (XGetTransientForHint(server.dsp, w1, &w2) != 0)
444 if (w2) tsk2 = task_get_task(w2);
445 }
446 if ( g_slist_find(urgent_list, tsk2) ) {
447 del_urgent(tsk2);
448 }
449 // put active state on all task (multi_desktop)
450 if (tsk2) {
451 for (i=0 ; i < nb_panel ; i++) {
452 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
453 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
454 tsk1 = l0->data;
455 if (tsk1->win == tsk2->win) {
456 tsk1->area.is_active = 1;
457 }
458 }
459 }
460 }
461 task_active = tsk2;
462 }
463 }
464
465
466 void blink_urgent()
467 {
468 GSList* urgent_task = urgent_list;
469 while (urgent_task) {
470 Task* t = urgent_task->data;
471 if ( t->urgent_tick < max_tick_urgent) {
472 t->area.is_active = !t->area.is_active;
473 t->area.redraw = 1;
474 t->urgent_tick++;
475 }
476 urgent_task = urgent_task->next;
477 }
478 panel_refresh = 1;
479 }
480
481
482 void add_urgent(Task *tsk)
483 {
484 if (!tsk)
485 return;
486
487 // some programs set urgency hint although they are active
488 if ( task_active && task_active->win == tsk->win )
489 return;
490
491 // reset counter to 0, remove tasks which are already in the list
492 GSList* urgent_add = task_get_tasks(tsk->win);
493 GSList* it = urgent_add;
494 Task* tsk2;
495 while (it) {
496 tsk2 = it->data;
497 tsk2->urgent_tick = 0;
498 it = it->next;
499 if (g_slist_find(urgent_list, tsk2))
500 urgent_add = g_slist_remove(urgent_add, tsk2);
501 }
502
503 // not yet in the list, so we have to add it
504 urgent_list = g_slist_concat(urgent_add, urgent_list);
505
506 if (urgent_timeout == 0)
507 urgent_timeout = add_timeout(10, 1000, blink_urgent);
508 }
509
510
511 void del_urgent(Task *tsk)
512 {
513 if (!tsk)
514 return;
515
516 GSList* urgent_del = task_get_tasks(tsk->win);
517 GSList* it = urgent_del;
518 while(it) {
519 urgent_list = g_slist_remove(urgent_list, it->data);
520 it = it->next;
521 }
522 if (!urgent_list) {
523 stop_timeout(urgent_timeout);
524 urgent_timeout = 0;
525 }
526 }
This page took 0.058058 seconds and 5 git commands to generate.