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