]> Dogcows Code - chaz/tint2/blob - src/taskbar/task.c
*add* autohide
[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 if (real_transparency) {
317 render_image(*pmap, pos_x, panel->g_task.icon_posy, imlib_image_get_width(), imlib_image_get_height() );
318 }
319 else {
320 imlib_context_set_drawable (*pmap);
321 imlib_render_image_on_drawable (pos_x, panel->g_task.icon_posy);
322 }
323 }
324
325
326 void draw_task (void *obj, cairo_t *c, int active)
327 {
328 Task *tsk = obj;
329 PangoLayout *layout;
330 config_color *config_text;
331 int width=0, height;
332 Panel *panel = (Panel*)tsk->area.panel;
333
334 if (panel->g_task.text) {
335 /* Layout */
336 layout = pango_cairo_create_layout (c);
337 pango_layout_set_font_description (layout, panel->g_task.font_desc);
338 pango_layout_set_text (layout, tsk->title, -1);
339
340 /* Drawing width and Cut text */
341 // pango use U+22EF or U+2026
342 pango_layout_set_width (layout, ((Taskbar*)tsk->area.parent)->text_width * PANGO_SCALE);
343 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
344
345 /* Center text */
346 if (panel->g_task.centered) pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
347 else pango_layout_set_alignment (layout, PANGO_ALIGN_LEFT);
348
349 pango_layout_get_pixel_size (layout, &width, &height);
350
351 if (active) config_text = &panel->g_task.font_active;
352 else config_text = &panel->g_task.font;
353
354 cairo_set_source_rgba (c, config_text->color[0], config_text->color[1], config_text->color[2], config_text->alpha);
355
356 pango_cairo_update_layout (c, layout);
357 cairo_move_to (c, panel->g_task.text_posx, panel->g_task.text_posy);
358 pango_cairo_show_layout (c, layout);
359
360 if (panel->g_task.font_shadow) {
361 cairo_set_source_rgba (c, 0.0, 0.0, 0.0, 0.5);
362 pango_cairo_update_layout (c, layout);
363 cairo_move_to (c, panel->g_task.text_posx + 1, panel->g_task.text_posy + 1);
364 pango_cairo_show_layout (c, layout);
365 }
366 g_object_unref (layout);
367 }
368
369 if (panel->g_task.icon) {
370 // icon use same opacity as text
371 draw_task_icon (tsk, width, active);
372 }
373 }
374
375
376 Task *next_task(Task *tsk)
377 {
378 GSList *l0;
379 int i, j;
380 Task *tsk1;
381
382 for (i=0 ; i < nb_panel ; i++) {
383 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
384 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
385 tsk1 = l0->data;
386 if (tsk1 == tsk) {
387 if (l0->next == NULL) l0 = panel1[i].taskbar[j].area.list;
388 else l0 = l0->next;
389 return l0->data;
390 }
391 }
392 }
393 }
394
395 return NULL;
396 }
397
398 Task *prev_task(Task *tsk)
399 {
400 GSList *l0;
401 int i, j;
402 Task *tsk1, *tsk2;
403
404 for (i=0 ; i < nb_panel ; i++) {
405 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
406 tsk2 = NULL;
407 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
408 tsk1 = l0->data;
409 if (tsk1 == tsk) {
410 if (l0 == panel1[i].taskbar[j].area.list) {
411 l0 = g_slist_last ( l0 );
412 tsk2 = l0->data;
413 }
414 return tsk2;
415 }
416 tsk2 = tsk1;
417 }
418 }
419 }
420
421 return NULL;
422 }
423
424
425 void active_task()
426 {
427 GSList *l0;
428 int i, j;
429 Task *tsk1, *tsk2;
430
431 if (task_active) {
432 for (i=0 ; i < nb_panel ; i++) {
433 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
434 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
435 tsk1 = l0->data;
436 tsk1->area.is_active = 0;
437 }
438 }
439 }
440 task_active = 0;
441 }
442
443 Window w1 = window_get_active ();
444 //printf("Change active task %ld\n", w1);
445
446 tsk2 = task_get_task(w1);
447 if (!tsk2) {
448 Window w2;
449 if (XGetTransientForHint(server.dsp, w1, &w2) != 0)
450 if (w2) tsk2 = task_get_task(w2);
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 tsk1->area.is_active = 1;
463 }
464 }
465 }
466 }
467 task_active = tsk2;
468 }
469 }
470
471
472 void blink_urgent(void* arg)
473 {
474 GSList* urgent_task = urgent_list;
475 while (urgent_task) {
476 Task* t = urgent_task->data;
477 if ( t->urgent_tick < max_tick_urgent) {
478 t->area.is_active = !t->area.is_active;
479 t->area.redraw = 1;
480 t->urgent_tick++;
481 }
482 urgent_task = urgent_task->next;
483 }
484 panel_refresh = 1;
485 }
486
487
488 void add_urgent(Task *tsk)
489 {
490 if (!tsk)
491 return;
492
493 // some programs set urgency hint although they are active
494 if ( task_active && task_active->win == tsk->win )
495 return;
496
497 // reset counter to 0, remove tasks which are already in the list
498 GSList* urgent_add = task_get_tasks(tsk->win);
499 GSList* it = urgent_add;
500 Task* tsk2;
501 while (it) {
502 tsk2 = it->data;
503 tsk2->urgent_tick = 0;
504 it = it->next;
505 if (g_slist_find(urgent_list, tsk2))
506 urgent_add = g_slist_remove(urgent_add, tsk2);
507 }
508
509 // not yet in the list, so we have to add it
510 urgent_list = g_slist_concat(urgent_add, urgent_list);
511
512 if (urgent_timeout == 0)
513 urgent_timeout = add_timeout(10, 1000, blink_urgent, 0);
514 }
515
516
517 void del_urgent(Task *tsk)
518 {
519 if (!tsk)
520 return;
521
522 GSList* urgent_del = task_get_tasks(tsk->win);
523 GSList* it = urgent_del;
524 while(it) {
525 urgent_list = g_slist_remove(urgent_list, it->data);
526 it = it->next;
527 }
528 if (!urgent_list) {
529 stop_timeout(urgent_timeout);
530 urgent_timeout = 0;
531 }
532 }
This page took 0.055383 seconds and 5 git commands to generate.