]> Dogcows Code - chaz/tint2/blob - src/taskbar/task.c
Fix for incorrect task cycling when windows visible on all desktops are present
[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) from Omega distribution
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 timeout* urgent_timeout;
39 GSList* urgent_list;
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 if (nb_panel > 1) {
55 monitor = window_get_monitor (win);
56 if (monitor >= nb_panel) monitor = 0;
57 }
58 else monitor = 0;
59
60 Task new_tsk;
61 new_tsk.win = win;
62 new_tsk.desktop = window_get_desktop (win);
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 new_tsk.state_pix[k] = 0;
73 }
74 get_title(&new_tsk);
75 get_icon(&new_tsk);
76
77 //printf("task %s : desktop %d, monitor %d\n", new_tsk->title, desktop, monitor);
78 XSelectInput (server.dsp, new_tsk.win, PropertyChangeMask|StructureNotifyMask);
79
80 GPtrArray* task_group = g_ptr_array_new();
81 Taskbar *tskbar;
82 Task *new_tsk2=0;
83 int j;
84 for (j=0 ; j < panel1[monitor].nb_desktop ; j++) {
85 if (new_tsk.desktop != ALLDESKTOP && new_tsk.desktop != j) continue;
86
87 tskbar = &panel1[monitor].taskbar[j];
88 new_tsk2 = malloc(sizeof(Task));
89 memcpy(&new_tsk2->area, &panel1[monitor].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 new_tsk2->current_state = -1; // to update the current state later in set_task_state...
94 if (new_tsk2->desktop == ALLDESKTOP && server.desktop != j) {
95 // hide ALLDESKTOP task on non-current desktop
96 new_tsk2->area.on_screen = 0;
97 }
98 new_tsk2->title = new_tsk.title;
99 if (panel1[monitor].g_task.tooltip_enabled)
100 new_tsk2->area._get_tooltip_text = task_get_tooltip;
101 for (k=0; k<TASK_STATE_COUNT; ++k) {
102 new_tsk2->icon[k] = new_tsk.icon[k];
103 new_tsk2->state_pix[k] = 0;
104 }
105 new_tsk2->icon_width = new_tsk.icon_width;
106 new_tsk2->icon_height = new_tsk.icon_height;
107 tskbar->area.list = g_slist_append(tskbar->area.list, new_tsk2);
108 tskbar->area.resize = 1;
109 g_ptr_array_add(task_group, new_tsk2);
110 //printf("add_task panel %d, desktop %d, task %s\n", i, j, new_tsk2->title);
111 }
112 Window* key = malloc(sizeof(Window));
113 *key = new_tsk.win;
114 g_hash_table_insert(win_to_task_table, key, task_group);
115 set_task_state(new_tsk2, new_tsk.current_state);
116
117 if (window_is_urgent(win))
118 add_urgent(new_tsk2);
119
120 return new_tsk2;
121 }
122
123
124 void remove_task (Task *tsk)
125 {
126 if (!tsk) return;
127
128 Window win = tsk->win;
129
130 // free title and icon just for the first task
131 // even with task_on_all_desktop and with task_on_all_panel
132 //printf("remove_task %s %d\n", tsk->title, tsk->desktop);
133 if (tsk->title)
134 free (tsk->title);
135 int k;
136 for (k=0; k<TASK_STATE_COUNT; ++k) {
137 if (tsk->icon[k]) {
138 imlib_context_set_image(tsk->icon[k]);
139 imlib_free_image();
140 tsk->icon[k] = 0;
141 if (tsk->state_pix[k]) XFreePixmap(server.dsp, tsk->state_pix[k]);
142 }
143 }
144
145 int i;
146 Task *tsk2;
147 Taskbar *tskbar;
148 GPtrArray* task_group = g_hash_table_lookup(win_to_task_table, &win);
149 for (i=0; i<task_group->len; ++i) {
150 tsk2 = g_ptr_array_index(task_group, i);
151 tskbar = tsk2->area.parent;
152 tskbar->area.list = g_slist_remove(tskbar->area.list, tsk2);
153 tskbar->area.resize = 1;
154 if (tsk2 == task_active) task_active = 0;
155 if (tsk2 == task_drag) task_drag = 0;
156 if (g_slist_find(urgent_list, tsk2)) del_urgent(tsk2);
157 free(tsk2);
158 }
159 g_hash_table_remove(win_to_task_table, &win);
160 }
161
162
163 int get_title(Task *tsk)
164 {
165 Panel *panel = tsk->area.panel;
166 char *title, *name;
167
168 if (!panel->g_task.text && !panel->g_task.tooltip_enabled) return 0;
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 if (tsk->title) {
190 // check unecessary title change
191 if (strcmp(tsk->title, title) == 0) {
192 free(title);
193 return 0;
194 }
195 else
196 free(tsk->title);
197 }
198
199 tsk->title = title;
200 GPtrArray* task_group = task_get_tasks(tsk->win);
201 if (task_group) {
202 int i;
203 for (i=0; i<task_group->len; ++i) {
204 Task* tsk2 = g_ptr_array_index(task_group, i);
205 tsk2->title = tsk->title;
206 set_task_redraw(tsk2);
207 }
208 }
209 return 1;
210 }
211
212
213 void get_icon (Task *tsk)
214 {
215 Panel *panel = tsk->area.panel;
216 if (!panel->g_task.icon) return;
217 int i;
218 Imlib_Image img = NULL;
219 XWMHints *hints = 0;
220 gulong *data = 0;
221
222 int k;
223 for (k=0; k<TASK_STATE_COUNT; ++k) {
224 if (tsk->icon[k]) {
225 imlib_context_set_image(tsk->icon[k]);
226 imlib_free_image();
227 tsk->icon[k] = 0;
228 }
229 }
230
231 data = server_get_property (tsk->win, server.atom._NET_WM_ICON, XA_CARDINAL, &i);
232 if (data) {
233 // get ARGB icon
234 int w, h;
235 gulong *tmp_data;
236
237 tmp_data = get_best_icon (data, get_icon_count (data, i), i, &w, &h, panel->g_task.icon_size1);
238 #ifdef __x86_64__
239 DATA32 icon_data[w * h];
240 int length = w * h;
241 for (i = 0; i < length; ++i)
242 icon_data[i] = tmp_data[i];
243 img = imlib_create_image_using_copied_data (w, h, icon_data);
244 #else
245 img = imlib_create_image_using_data (w, h, (DATA32*)tmp_data);
246 #endif
247 }
248 else {
249 // get Pixmap icon
250 hints = XGetWMHints(server.dsp, tsk->win);
251 if (hints) {
252 if (hints->flags & IconPixmapHint && hints->icon_pixmap != 0) {
253 // get width, height and depth for the pixmap
254 Window root;
255 int icon_x, icon_y;
256 uint border_width, bpp;
257 uint w, h;
258
259 //printf(" get pixmap\n");
260 XGetGeometry(server.dsp, hints->icon_pixmap, &root, &icon_x, &icon_y, &w, &h, &border_width, &bpp);
261 imlib_context_set_drawable(hints->icon_pixmap);
262 img = imlib_create_image_from_drawable(hints->icon_mask, 0, 0, w, h, 0);
263 }
264 }
265 }
266 if (img == NULL) {
267 imlib_context_set_image(default_icon);
268 img = imlib_clone_image();
269 }
270
271 // transform icons
272 imlib_context_set_image(img);
273 imlib_image_set_has_alpha(1);
274 int w, h;
275 w = imlib_image_get_width();
276 h = imlib_image_get_height();
277 Imlib_Image orig_image = imlib_create_cropped_scaled_image(0, 0, w, h, panel->g_task.icon_size1, panel->g_task.icon_size1);
278 imlib_free_image();
279
280 imlib_context_set_image(orig_image);
281 tsk->icon_width = imlib_image_get_width();
282 tsk->icon_height = imlib_image_get_height();
283 for (k=0; k<TASK_STATE_COUNT; ++k) {
284 imlib_context_set_image(orig_image);
285 tsk->icon[k] = imlib_clone_image();
286 imlib_context_set_image(tsk->icon[k]);
287 DATA32 *data32;
288 if (panel->g_task.alpha[k] != 100 || panel->g_task.saturation[k] != 0 || panel->g_task.brightness[k] != 0) {
289 data32 = imlib_image_get_data();
290 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);
291 imlib_image_put_back_data(data32);
292 }
293 }
294 imlib_context_set_image(orig_image);
295 imlib_free_image();
296
297 if (hints)
298 XFree(hints);
299 if (data)
300 XFree (data);
301
302 GPtrArray* task_group = task_get_tasks(tsk->win);
303 if (task_group) {
304 for (i=0; i<task_group->len; ++i) {
305 Task* tsk2 = g_ptr_array_index(task_group, i);
306 tsk2->icon_width = tsk->icon_width;
307 tsk2->icon_height = tsk->icon_height;
308 int k;
309 for (k=0; k<TASK_STATE_COUNT; ++k)
310 tsk2->icon[k] = tsk->icon[k];
311 set_task_redraw(tsk2);
312 }
313 }
314 }
315
316
317 void draw_task_icon (Task *tsk, int text_width)
318 {
319 if (tsk->icon[tsk->current_state] == 0) return;
320
321 // Find pos
322 int pos_x;
323 Panel *panel = (Panel*)tsk->area.panel;
324 if (panel->g_task.centered) {
325 if (panel->g_task.text)
326 pos_x = (tsk->area.width - text_width - panel->g_task.icon_size1) / 2;
327 else
328 pos_x = (tsk->area.width - panel->g_task.icon_size1) / 2;
329 }
330 else pos_x = panel->g_task.area.paddingxlr + tsk->area.bg->border.width;
331
332 // Render
333 imlib_context_set_image (tsk->icon[tsk->current_state]);
334 if (server.real_transparency) {
335 render_image(tsk->area.pix, pos_x, panel->g_task.icon_posy, imlib_image_get_width(), imlib_image_get_height() );
336 }
337 else {
338 imlib_context_set_drawable(tsk->area.pix);
339 imlib_render_image_on_drawable (pos_x, panel->g_task.icon_posy);
340 }
341 }
342
343
344 void draw_task (void *obj, cairo_t *c)
345 {
346 Task *tsk = obj;
347 tsk->state_pix[tsk->current_state] = tsk->area.pix;
348 PangoLayout *layout;
349 Color *config_text;
350 int width=0, height;
351 Panel *panel = (Panel*)tsk->area.panel;
352 //printf("draw_task %d %d\n", tsk->area.posx, tsk->area.posy);
353
354 if (panel->g_task.text) {
355 /* Layout */
356 layout = pango_cairo_create_layout (c);
357 pango_layout_set_font_description (layout, panel->g_task.font_desc);
358 pango_layout_set_text(layout, tsk->title, -1);
359
360 /* Drawing width and Cut text */
361 // pango use U+22EF or U+2026
362 pango_layout_set_width(layout, ((Taskbar*)tsk->area.parent)->text_width * PANGO_SCALE);
363 pango_layout_set_height(layout, panel->g_task.text_height * PANGO_SCALE);
364 pango_layout_set_wrap(layout, PANGO_WRAP_CHAR);
365 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
366
367 /* Center text */
368 if (panel->g_task.centered) pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
369 else pango_layout_set_alignment (layout, PANGO_ALIGN_LEFT);
370
371 pango_layout_get_pixel_size (layout, &width, &height);
372
373 config_text = &panel->g_task.font[tsk->current_state];
374 cairo_set_source_rgba (c, config_text->color[0], config_text->color[1], config_text->color[2], config_text->alpha);
375
376 pango_cairo_update_layout (c, layout);
377 double text_posy = (panel->g_task.area.height - height) / 2.0;
378 cairo_move_to (c, panel->g_task.text_posx, text_posy);
379 pango_cairo_show_layout (c, layout);
380
381 if (panel->g_task.font_shadow) {
382 cairo_set_source_rgba (c, 0.0, 0.0, 0.0, 0.5);
383 pango_cairo_update_layout (c, layout);
384 cairo_move_to (c, panel->g_task.text_posx + 1, text_posy + 1);
385 pango_cairo_show_layout (c, layout);
386 }
387 g_object_unref (layout);
388 }
389
390 if (panel->g_task.icon) {
391 draw_task_icon (tsk, width);
392 }
393 }
394
395
396 void on_change_task (void *obj)
397 {
398 Task *tsk = obj;
399 Panel *panel = (Panel*)tsk->area.panel;
400
401 long value[] = { panel->posx+tsk->area.posx, panel->posy+tsk->area.posy, tsk->area.width, tsk->area.height };
402 XChangeProperty (server.dsp, tsk->win, server.atom._NET_WM_ICON_GEOMETRY, XA_CARDINAL, 32, PropModeReplace, (unsigned char*)value, 4);
403
404 // reset Pixmap when position/size changed
405 set_task_redraw(tsk);
406 }
407
408 // Given a pointer to the active task (active_task) and a pointer
409 // to the task that is currently under the mouse (current_task),
410 // return a pointer to the active task that is on the same desktop
411 // as current_task. Normally this is simply active_task, except when
412 // it is set to appear on all desktops. In that case we search for
413 // another Task on current_task's taskbar, with the same window as
414 // active_task.
415 Task *find_active_task(Task *current_task, Task *active_task)
416 {
417 if (active_task == 0)
418 return current_task;
419 if (active_task->desktop != ALLDESKTOP)
420 return active_task;
421 if (current_task == 0)
422 return active_task;
423
424 GSList *l0;
425 Task *tsk;
426 Taskbar* tskbar = current_task->area.parent;
427
428 l0 = tskbar->area.list;
429 if (taskbarname_enabled) l0 = l0->next;
430 for (; l0 ; l0 = l0->next) {
431 tsk = l0->data;
432 if (tsk->win == active_task->win)
433 return tsk;
434 }
435 return active_task;
436 }
437
438 Task *next_task(Task *tsk)
439 {
440 if (tsk == 0)
441 return 0;
442
443 GSList *l0, *lfirst_tsk;
444 Task *tsk1;
445 Taskbar* tskbar = tsk->area.parent;
446
447 l0 = tskbar->area.list;
448 if (taskbarname_enabled) l0 = l0->next;
449 lfirst_tsk = l0;
450 for (; l0 ; l0 = l0->next) {
451 tsk1 = l0->data;
452 if (tsk1 == tsk) {
453 if (l0->next == 0) l0 = lfirst_tsk;
454 else l0 = l0->next;
455 return l0->data;
456 }
457 }
458
459 return 0;
460 }
461
462 Task *prev_task(Task *tsk)
463 {
464 if (tsk == 0)
465 return 0;
466
467 GSList *l0, *lfirst_tsk;
468 Task *tsk1, *tsk2;
469 Taskbar* tskbar = tsk->area.parent;
470
471 tsk2 = 0;
472 l0 = tskbar->area.list;
473 if (taskbarname_enabled) l0 = l0->next;
474 lfirst_tsk = l0;
475 for (; l0 ; l0 = l0->next) {
476 tsk1 = l0->data;
477 if (tsk1 == tsk) {
478 if (l0 == lfirst_tsk) {
479 l0 = g_slist_last ( l0 );
480 tsk2 = l0->data;
481 }
482 return tsk2;
483 }
484 tsk2 = tsk1;
485 }
486
487 return 0;
488 }
489
490
491 void active_task()
492 {
493 if (task_active) {
494 set_task_state(task_active, window_is_iconified(task_active->win) ? TASK_ICONIFIED : TASK_NORMAL);
495 task_active = 0;
496 }
497
498 Window w1 = window_get_active();
499 //printf("Change active task %ld\n", w1);
500
501 if (w1) {
502 if (!task_get_tasks(w1)) {
503 Window w2;
504 while (XGetTransientForHint(server.dsp, w1, &w2))
505 w1 = w2;
506 }
507 set_task_state((task_active = task_get_task(w1)), TASK_ACTIVE);
508 }
509 }
510
511
512 void set_task_state(Task *tsk, int state)
513 {
514 if (tsk == 0 || state < 0 || state >= TASK_STATE_COUNT)
515 return;
516
517 if (tsk->current_state != state) {
518 GPtrArray* task_group = task_get_tasks(tsk->win);
519 if (task_group) {
520 int i;
521 for (i=0; i<task_group->len; ++i) {
522 Task* tsk1 = g_ptr_array_index(task_group, i);
523 tsk1->current_state = state;
524 tsk1->area.bg = panel1[0].g_task.background[state];
525 tsk1->area.pix = tsk1->state_pix[state];
526 if (tsk1->state_pix[state] == 0)
527 tsk1->area.redraw = 1;
528 if (state == TASK_ACTIVE && g_slist_find(urgent_list, tsk1))
529 del_urgent(tsk1);
530 }
531 panel_refresh = 1;
532 }
533 }
534 }
535
536
537 void set_task_redraw(Task* tsk)
538 {
539 int k;
540 for (k=0; k<TASK_STATE_COUNT; ++k) {
541 if (tsk->state_pix[k]) XFreePixmap(server.dsp, tsk->state_pix[k]);
542 tsk->state_pix[k] = 0;
543 }
544 tsk->area.pix = 0;
545 tsk->area.redraw = 1;
546 }
547
548
549 void blink_urgent(void* arg)
550 {
551 GSList* urgent_task = urgent_list;
552 while (urgent_task) {
553 Task* t = urgent_task->data;
554 if ( t->urgent_tick < max_tick_urgent) {
555 if (t->urgent_tick++ % 2)
556 set_task_state(t, TASK_URGENT);
557 else
558 set_task_state(t, window_is_iconified(t->win) ? TASK_ICONIFIED : TASK_NORMAL);
559 }
560 urgent_task = urgent_task->next;
561 }
562 panel_refresh = 1;
563 }
564
565
566 void add_urgent(Task *tsk)
567 {
568 if (!tsk)
569 return;
570
571 // some programs set urgency hint although they are active
572 if ( task_active && task_active->win == tsk->win )
573 return;
574
575 tsk = task_get_task(tsk->win); // always add the first tsk for a task group (omnipresent windows)
576 tsk->urgent_tick = 0;
577 if (g_slist_find(urgent_list, tsk))
578 return;
579
580 // not yet in the list, so we have to add it
581 urgent_list = g_slist_prepend(urgent_list, tsk);
582
583 if (urgent_timeout == 0)
584 urgent_timeout = add_timeout(10, 1000, blink_urgent, 0);
585 }
586
587
588 void del_urgent(Task *tsk)
589 {
590 urgent_list = g_slist_remove(urgent_list, tsk);
591 if (urgent_list == 0) {
592 stop_timeout(urgent_timeout);
593 urgent_timeout = 0;
594 }
595 }
This page took 0.059841 seconds and 4 git commands to generate.