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