]> Dogcows Code - chaz/tint2/blob - src/taskbar/task.c
fixed : lost of active task on SIGUSR1
[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 "server.h"
33 #include "panel.h"
34 #include "tooltip.h"
35
36
37
38 Task *add_task (Window win)
39 {
40 if (!win) return 0;
41 if (window_is_hidden(win)) return 0;
42
43 int monitor;
44
45 Task new_tsk;
46 new_tsk.win = win;
47 new_tsk.desktop = window_get_desktop (win);
48 if (nb_panel > 1) {
49 monitor = window_get_monitor (win);
50 if (monitor >= nb_panel) monitor = 0;
51 }
52 else monitor = 0;
53 new_tsk.area.panel = &panel1[monitor];
54
55 // allocate only one title and one icon
56 // even with task_on_all_desktop and with task_on_all_panel
57 new_tsk.title = 0;
58 new_tsk.icon = new_tsk.icon_active = NULL;
59 get_title(&new_tsk);
60 get_icon(&new_tsk);
61
62 //printf("task %s : desktop %d, monitor %d\n", new_tsk->title, desktop, monitor);
63 XSelectInput (server.dsp, new_tsk.win, PropertyChangeMask|StructureNotifyMask);
64
65 Taskbar *tskbar;
66 Task *new_tsk2=0;
67 int i, j;
68 for (i=0 ; i < nb_panel ; i++) {
69 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
70 if (new_tsk.desktop != ALLDESKTOP && new_tsk.desktop != j) continue;
71 if (nb_panel > 1 && panel1[i].monitor != monitor) continue;
72
73 tskbar = &panel1[i].taskbar[j];
74 new_tsk2 = malloc(sizeof(Task));
75 memcpy(&new_tsk2->area, &panel1[i].g_task.area, sizeof(Area));
76 new_tsk2->area.parent = tskbar;
77 new_tsk2->win = new_tsk.win;
78 new_tsk2->desktop = new_tsk.desktop;
79 if (new_tsk2->desktop == ALLDESKTOP && server.desktop != j) {
80 // hide ALLDESKTOP task on non-current desktop
81 new_tsk2->area.on_screen = 0;
82 }
83 new_tsk2->title = new_tsk.title;
84 new_tsk2->icon = new_tsk.icon;
85 new_tsk2->icon_active = new_tsk.icon_active;
86 new_tsk2->icon_width = new_tsk.icon_width;
87 new_tsk2->icon_height = new_tsk.icon_height;
88 tskbar->area.list = g_slist_append(tskbar->area.list, new_tsk2);
89 tskbar->area.resize = 1;
90 //printf("add_task panel %d, desktop %d, task %s\n", i, j, new_tsk2->title);
91 }
92 }
93 return new_tsk2;
94 }
95
96
97 void remove_task (Task *tsk)
98 {
99 if (!tsk) return;
100
101 Window win = tsk->win;
102 int desktop = tsk->desktop;
103
104 if (g_tooltip.task == tsk) {
105 tooltip_hide();
106 alarm(0);
107 g_tooltip.task = 0;
108 }
109
110 // free title and icon just for the first task
111 // even with task_on_all_desktop and with task_on_all_panel
112 //printf("remove_task %s %d\n", tsk->title, tsk->desktop);
113 if (tsk->title)
114 free (tsk->title);
115 if (tsk->icon) {
116 imlib_context_set_image(tsk->icon);
117 imlib_free_image();
118 imlib_context_set_image(tsk->icon_active);
119 imlib_free_image();
120 tsk->icon = tsk->icon_active = NULL;
121 }
122
123 int i, j;
124 Task *tsk2;
125 Taskbar *tskbar;
126 for (i=0 ; i < nb_panel ; i++) {
127 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
128 if (desktop != ALLDESKTOP && desktop != j) continue;
129
130 GSList *l0;
131 tskbar = &panel1[i].taskbar[j];
132 for (l0 = tskbar->area.list; l0 ; ) {
133 tsk2 = l0->data;
134 l0 = l0->next;
135 if (win == tsk2->win) {
136 tskbar->area.list = g_slist_remove(tskbar->area.list, tsk2);
137 tskbar->area.resize = 1;
138
139 if (tsk2 == task_active)
140 task_active = 0;
141 if (tsk2 == task_drag)
142 task_drag = 0;
143
144 XFreePixmap (server.dsp, tsk2->area.pix.pmap);
145 XFreePixmap (server.dsp, tsk2->area.pix_active.pmap);
146 free(tsk2);
147 }
148 }
149 }
150 }
151 }
152
153
154 void get_title(Task *tsk)
155 {
156 Panel *panel = tsk->area.panel;
157 char *title, *name;
158
159 if (!panel->g_task.text && !g_tooltip.enabled) return;
160
161 if (g_tooltip.task == tsk) {
162 tooltip_hide();
163 alarm(0);
164 g_tooltip.task = 0;
165 }
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 tsk->area.redraw = 1;
187 if (tsk->title)
188 free(tsk->title);
189 tsk->title = title;
190 }
191
192
193 void get_icon (Task *tsk)
194 {
195 Panel *panel = tsk->area.panel;
196 if (!panel->g_task.icon) return;
197 int i;
198 Imlib_Image img = NULL;
199 XWMHints *hints = 0;
200 long *data = 0;
201
202 if (tsk->icon) {
203 imlib_context_set_image(tsk->icon);
204 imlib_free_image();
205 imlib_context_set_image(tsk->icon_active);
206 imlib_free_image();
207 tsk->icon = tsk->icon_active = NULL;
208 }
209 tsk->area.redraw = 1;
210
211 data = server_get_property (tsk->win, server.atom._NET_WM_ICON, XA_CARDINAL, &i);
212 if (data) {
213 // get ARGB icon
214 int w, h;
215 long *tmp_data;
216
217 tmp_data = get_best_icon (data, get_icon_count (data, i), i, &w, &h, panel->g_task.icon_size1);
218
219 #ifdef __x86_64__
220 DATA32 icon_data[w * h];
221 int length = w * h;
222 for (i = 0; i < length; ++i)
223 icon_data[i] = tmp_data[i];
224 img = imlib_create_image_using_copied_data (w, h, icon_data);
225 #else
226 img = imlib_create_image_using_data (w, h, (DATA32*)tmp_data);
227 #endif
228 }
229 else {
230 // get Pixmap icon
231 hints = XGetWMHints(server.dsp, tsk->win);
232 if (hints) {
233 if (hints->flags & IconPixmapHint && hints->icon_pixmap != 0) {
234 // get width, height and depth for the pixmap
235 Window root;
236 int icon_x, icon_y;
237 uint border_width, bpp;
238 uint w, h;
239
240 //printf(" get pixmap\n");
241 XGetGeometry(server.dsp, hints->icon_pixmap, &root, &icon_x, &icon_y, &w, &h, &border_width, &bpp);
242 imlib_context_set_drawable(hints->icon_pixmap);
243 img = imlib_create_image_from_drawable(hints->icon_mask, 0, 0, w, h, 0);
244 }
245 }
246 }
247 if (img == NULL) {
248 imlib_context_set_image(default_icon);
249 img = imlib_clone_image();
250 }
251
252 // transform icons
253 imlib_context_set_image(img);
254 imlib_image_set_has_alpha(1);
255 int w, h;
256 w = imlib_image_get_width();
257 h = imlib_image_get_height();
258 tsk->icon = imlib_create_cropped_scaled_image(0, 0, w, h, panel->g_task.icon_size1, panel->g_task.icon_size1);
259 imlib_free_image();
260
261 imlib_context_set_image(tsk->icon);
262 tsk->icon_width = imlib_image_get_width();
263 tsk->icon_height = imlib_image_get_height();
264 tsk->icon_active = imlib_clone_image();
265
266 DATA32 *data32;
267 if (panel->g_task.alpha != 100 || panel->g_task.saturation != 0 || panel->g_task.brightness != 0) {
268 data32 = imlib_image_get_data();
269 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);
270 imlib_image_put_back_data(data32);
271 }
272
273 if (panel->g_task.alpha_active != 100 || panel->g_task.saturation_active != 0 || panel->g_task.brightness_active != 0) {
274 imlib_context_set_image(tsk->icon_active);
275 data32 = imlib_image_get_data();
276 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);
277 imlib_image_put_back_data(data32);
278 }
279
280 if (hints)
281 XFree(hints);
282 if (data)
283 XFree (data);
284 }
285
286
287 void draw_task_icon (Task *tsk, int text_width, int active)
288 {
289 if (tsk->icon == NULL || tsk->icon_active == NULL) return;
290
291 // Find pos
292 int pos_x;
293 Panel *panel = (Panel*)tsk->area.panel;
294 if (panel->g_task.centered) {
295 if (panel->g_task.text)
296 pos_x = (tsk->area.width - text_width - panel->g_task.icon_size1) / 2;
297 else
298 pos_x = (tsk->area.width - panel->g_task.icon_size1) / 2;
299 }
300 else pos_x = panel->g_task.area.paddingxlr + panel->g_task.area.pix.border.width;
301
302 // Render
303 Pixmap *pmap;
304 if (active == 0) {
305 imlib_context_set_image (tsk->icon);
306 pmap = &tsk->area.pix.pmap;
307 }
308 else {
309 imlib_context_set_image (tsk->icon_active);
310 pmap = &tsk->area.pix_active.pmap;
311 }
312 imlib_context_set_drawable (*pmap);
313 imlib_render_image_on_drawable (pos_x, panel->g_task.icon_posy);
314 }
315
316
317 void draw_task (void *obj, cairo_t *c, int active)
318 {
319 Task *tsk = obj;
320 PangoLayout *layout;
321 config_color *config_text;
322 int width=0, height;
323 Panel *panel = (Panel*)tsk->area.panel;
324
325 if (panel->g_task.text) {
326 /* Layout */
327 layout = pango_cairo_create_layout (c);
328 pango_layout_set_font_description (layout, panel->g_task.font_desc);
329 pango_layout_set_text (layout, tsk->title, -1);
330
331 /* Drawing width and Cut text */
332 // pango use U+22EF or U+2026
333 pango_layout_set_width (layout, ((Taskbar*)tsk->area.parent)->text_width * PANGO_SCALE);
334 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
335
336 /* Center text */
337 if (panel->g_task.centered) pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
338 else pango_layout_set_alignment (layout, PANGO_ALIGN_LEFT);
339
340 pango_layout_get_pixel_size (layout, &width, &height);
341
342 if (active) config_text = &panel->g_task.font_active;
343 else config_text = &panel->g_task.font;
344
345 cairo_set_source_rgba (c, config_text->color[0], config_text->color[1], config_text->color[2], config_text->alpha);
346
347 pango_cairo_update_layout (c, layout);
348 cairo_move_to (c, panel->g_task.text_posx, panel->g_task.text_posy);
349 pango_cairo_show_layout (c, layout);
350
351 if (panel->g_task.font_shadow) {
352 cairo_set_source_rgba (c, 0.0, 0.0, 0.0, 0.5);
353 pango_cairo_update_layout (c, layout);
354 cairo_move_to (c, panel->g_task.text_posx + 1, panel->g_task.text_posy + 1);
355 pango_cairo_show_layout (c, layout);
356 }
357 g_object_unref (layout);
358 }
359
360 if (panel->g_task.icon) {
361 // icon use same opacity as text
362 draw_task_icon (tsk, width, active);
363 }
364 }
365
366
367 void active_task()
368 {
369 GSList *l0;
370 int i, j;
371 Task *tsk1, *tsk2;
372
373 if (task_active) {
374 for (i=0 ; i < nb_panel ; i++) {
375 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
376 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
377 tsk1 = l0->data;
378 tsk1->area.is_active = 0;
379 }
380 }
381 }
382 task_active = 0;
383 }
384
385 Window w1 = window_get_active ();
386 tsk2 = task_get_task(w1);
387 if (!tsk2) {
388 Window w2;
389 if (XGetTransientForHint(server.dsp, w1, &w2) != 0)
390 if (w2) tsk2 = task_get_task(w2);
391 }
392 if (task_urgent == tsk2) {
393 init_precision();
394 task_urgent = 0;
395 }
396 // put active state on all task (multi_desktop)
397 if (tsk2) {
398 for (i=0 ; i < nb_panel ; i++) {
399 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
400 for (l0 = panel1[i].taskbar[j].area.list; l0 ; l0 = l0->next) {
401 tsk1 = l0->data;
402 if (tsk1->win == tsk2->win) {
403 tsk1->area.is_active = 1;
404 }
405 }
406 }
407 }
408 task_active = tsk2;
409 }
410 }
411
This page took 0.053174 seconds and 5 git commands to generate.