]>
Dogcows Code - chaz/tint2/blob - src/taskbar/task.c
1 /**************************************************************************
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
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.
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 **************************************************************************/
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
38 static const struct timeout
* urgent_timeout
= 0;
39 static GSList
* urgent_list
= 0;
41 const char* task_get_tooltip(void* obj
)
48 Task
*add_task (Window win
)
51 if (window_is_hidden(win
)) return 0;
57 new_tsk
.desktop
= window_get_desktop (win
);
59 monitor
= window_get_monitor (win
);
60 if (monitor
>= nb_panel
) monitor
= 0;
63 new_tsk
.area
.panel
= &panel1
[monitor
];
65 // allocate only one title and one icon
66 // even with task_on_all_desktop and with task_on_all_panel
68 new_tsk
.icon
= new_tsk
.icon_active
= NULL
;
72 //printf("task %s : desktop %d, monitor %d\n", new_tsk->title, desktop, monitor);
73 XSelectInput (server
.dsp
, new_tsk
.win
, PropertyChangeMask
|StructureNotifyMask
);
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;
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;
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);
105 if (window_is_urgent(win
))
106 add_urgent(new_tsk2
);
111 void remove_task (Task
*tsk
)
115 Window win
= tsk
->win
;
116 int desktop
= tsk
->desktop
;
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);
124 imlib_context_set_image(tsk
->icon
);
126 imlib_context_set_image(tsk
->icon_active
);
128 tsk
->icon
= tsk
->icon_active
= NULL
;
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;
139 tskbar
= &panel1
[i
].taskbar
[j
];
140 for (l0
= tskbar
->area
.list
; l0
; ) {
143 if (win
== tsk2
->win
) {
144 tskbar
->area
.list
= g_slist_remove(tskbar
->area
.list
, tsk2
);
145 tskbar
->area
.resize
= 1;
147 if (tsk2
== task_active
)
149 if (tsk2
== task_drag
)
151 if (g_slist_find(urgent_list
, tsk2
))
154 XFreePixmap (server
.dsp
, tsk2
->area
.pix
.pmap
);
155 XFreePixmap (server
.dsp
, tsk2
->area
.pix_active
.pmap
);
164 void get_title(Task
*tsk
)
166 Panel
*panel
= tsk
->area
.panel
;
169 if (!panel
->g_task
.text
&& !g_tooltip
.enabled
) return;
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
)) {
178 strcpy(name
, "Untitled");
183 // add space before title
184 title
= malloc(strlen(name
)+2);
185 if (panel
->g_task
.icon
) strcpy(title
, " ");
188 if (name
) XFree (name
);
190 tsk
->area
.redraw
= 1;
197 void get_icon (Task
*tsk
)
199 Panel
*panel
= tsk
->area
.panel
;
200 if (!panel
->g_task
.icon
) return;
202 Imlib_Image img
= NULL
;
207 imlib_context_set_image(tsk
->icon
);
209 imlib_context_set_image(tsk
->icon_active
);
211 tsk
->icon
= tsk
->icon_active
= NULL
;
213 tsk
->area
.redraw
= 1;
215 data
= server_get_property (tsk
->win
, server
.atom
._NET_WM_ICON
, XA_CARDINAL
, &i
);
221 tmp_data
= get_best_icon (data
, get_icon_count (data
, i
), i
, &w
, &h
, panel
->g_task
.icon_size1
);
224 DATA32 icon_data
[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
);
230 img
= imlib_create_image_using_data (w
, h
, (DATA32
*)tmp_data
);
235 hints
= XGetWMHints(server
.dsp
, tsk
->win
);
237 if (hints
->flags
& IconPixmapHint
&& hints
->icon_pixmap
!= 0) {
238 // get width, height and depth for the pixmap
241 uint border_width
, bpp
;
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);
252 imlib_context_set_image(default_icon
);
253 img
= imlib_clone_image();
257 imlib_context_set_image(img
);
258 imlib_image_set_has_alpha(1);
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
);
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();
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
);
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
);
291 void draw_task_icon (Task
*tsk
, int text_width
, int active
)
293 if (tsk
->icon
== NULL
|| tsk
->icon_active
== NULL
) return;
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;
302 pos_x
= (tsk
->area
.width
- panel
->g_task
.icon_size1
) / 2;
304 else pos_x
= panel
->g_task
.area
.paddingxlr
+ panel
->g_task
.area
.pix
.border
.width
;
309 imlib_context_set_image (tsk
->icon
);
310 pmap
= &tsk
->area
.pix
.pmap
;
313 imlib_context_set_image (tsk
->icon_active
);
314 pmap
= &tsk
->area
.pix_active
.pmap
;
316 imlib_context_set_drawable (*pmap
);
317 imlib_render_image_on_drawable (pos_x
, panel
->g_task
.icon_posy
);
321 void draw_task (void *obj
, cairo_t
*c
, int active
)
325 config_color
*config_text
;
327 Panel
*panel
= (Panel
*)tsk
->area
.panel
;
329 if (panel
->g_task
.text
) {
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);
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
);
341 if (panel
->g_task
.centered
) pango_layout_set_alignment (layout
, PANGO_ALIGN_CENTER
);
342 else pango_layout_set_alignment (layout
, PANGO_ALIGN_LEFT
);
344 pango_layout_get_pixel_size (layout
, &width
, &height
);
346 if (active
) config_text
= &panel
->g_task
.font_active
;
347 else config_text
= &panel
->g_task
.font
;
349 cairo_set_source_rgba (c
, config_text
->color
[0], config_text
->color
[1], config_text
->color
[2], config_text
->alpha
);
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
);
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
);
361 g_object_unref (layout
);
364 if (panel
->g_task
.icon
) {
365 // icon use same opacity as text
366 draw_task_icon (tsk
, width
, active
);
371 Task
*next_task(Task
*tsk
)
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
) {
382 if (l0
->next
== NULL
) l0
= panel1
[i
].taskbar
[j
].area
.list
;
393 Task
*prev_task(Task
*tsk
)
399 for (i
=0 ; i
< nb_panel
; i
++) {
400 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
402 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
405 if (l0
== panel1
[i
].taskbar
[j
].area
.list
) {
406 l0
= g_slist_last ( l0
);
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
) {
431 tsk1
->area
.is_active
= 0;
438 Window w1
= window_get_active ();
439 //printf("Change active task %ld\n", w1);
441 tsk2
= task_get_task(w1
);
444 if (XGetTransientForHint(server
.dsp
, w1
, &w2
) != 0)
445 if (w2
) tsk2
= task_get_task(w2
);
447 if ( g_slist_find(urgent_list
, tsk2
) ) {
450 // put active state on all task (multi_desktop)
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
) {
456 if (tsk1
->win
== tsk2
->win
) {
457 tsk1
->area
.is_active
= 1;
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
;
477 urgent_task
= urgent_task
->next
;
483 void add_urgent(Task
*tsk
)
488 // some programs set urgency hint although they are active
489 if ( task_active
&& task_active
->win
== tsk
->win
)
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
;
498 tsk2
->urgent_tick
= 0;
500 if (g_slist_find(urgent_list
, tsk2
))
501 urgent_add
= g_slist_remove(urgent_add
, tsk2
);
504 // not yet in the list, so we have to add it
505 urgent_list
= g_slist_concat(urgent_add
, urgent_list
);
507 if (urgent_timeout
== 0)
508 urgent_timeout
= add_timeout(10, 1000, blink_urgent
);
512 void del_urgent(Task
*tsk
)
517 GSList
* urgent_del
= task_get_tasks(tsk
->win
);
518 GSList
* it
= urgent_del
;
520 urgent_list
= g_slist_remove(urgent_list
, it
->data
);
524 stop_timeout(urgent_timeout
);
This page took 0.060199 seconds and 4 git commands to generate.