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