]>
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>
37 static int urgent_timer
= 0;
39 const char* task_get_tooltip(void* obj
)
46 Task
*add_task (Window win
)
49 if (window_is_hidden(win
)) return 0;
55 new_tsk
.desktop
= window_get_desktop (win
);
57 monitor
= window_get_monitor (win
);
58 if (monitor
>= nb_panel
) monitor
= 0;
61 new_tsk
.area
.panel
= &panel1
[monitor
];
63 // allocate only one title and one icon
64 // even with task_on_all_desktop and with task_on_all_panel
66 new_tsk
.icon
= new_tsk
.icon_active
= NULL
;
70 //printf("task %s : desktop %d, monitor %d\n", new_tsk->title, desktop, monitor);
71 XSelectInput (server
.dsp
, new_tsk
.win
, PropertyChangeMask
|StructureNotifyMask
);
76 for (i
=0 ; i
< nb_panel
; i
++) {
77 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
78 if (new_tsk
.desktop
!= ALLDESKTOP
&& new_tsk
.desktop
!= j
) continue;
79 if (nb_panel
> 1 && panel1
[i
].monitor
!= monitor
) continue;
81 tskbar
= &panel1
[i
].taskbar
[j
];
82 new_tsk2
= malloc(sizeof(Task
));
83 memcpy(&new_tsk2
->area
, &panel1
[i
].g_task
.area
, sizeof(Area
));
84 new_tsk2
->area
.parent
= tskbar
;
85 new_tsk2
->win
= new_tsk
.win
;
86 new_tsk2
->desktop
= new_tsk
.desktop
;
87 if (new_tsk2
->desktop
== ALLDESKTOP
&& server
.desktop
!= j
) {
88 // hide ALLDESKTOP task on non-current desktop
89 new_tsk2
->area
.on_screen
= 0;
91 new_tsk2
->title
= new_tsk
.title
;
92 new_tsk2
->area
._get_tooltip_text
= task_get_tooltip
;
93 new_tsk2
->icon
= new_tsk
.icon
;
94 new_tsk2
->icon_active
= new_tsk
.icon_active
;
95 new_tsk2
->icon_width
= new_tsk
.icon_width
;
96 new_tsk2
->icon_height
= new_tsk
.icon_height
;
97 tskbar
->area
.list
= g_slist_append(tskbar
->area
.list
, new_tsk2
);
98 tskbar
->area
.resize
= 1;
99 //printf("add_task panel %d, desktop %d, task %s\n", i, j, new_tsk2->title);
106 void remove_task (Task
*tsk
)
110 Window win
= tsk
->win
;
111 int desktop
= tsk
->desktop
;
113 // free title and icon just for the first task
114 // even with task_on_all_desktop and with task_on_all_panel
115 //printf("remove_task %s %d\n", tsk->title, tsk->desktop);
119 imlib_context_set_image(tsk
->icon
);
121 imlib_context_set_image(tsk
->icon_active
);
123 tsk
->icon
= tsk
->icon_active
= NULL
;
129 for (i
=0 ; i
< nb_panel
; i
++) {
130 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
131 if (desktop
!= ALLDESKTOP
&& desktop
!= j
) continue;
134 tskbar
= &panel1
[i
].taskbar
[j
];
135 for (l0
= tskbar
->area
.list
; l0
; ) {
138 if (win
== tsk2
->win
) {
139 tskbar
->area
.list
= g_slist_remove(tskbar
->area
.list
, tsk2
);
140 tskbar
->area
.resize
= 1;
142 if (tsk2
== task_active
)
144 if (tsk2
== task_drag
)
149 XFreePixmap (server
.dsp
, tsk2
->area
.pix
.pmap
);
150 XFreePixmap (server
.dsp
, tsk2
->area
.pix_active
.pmap
);
159 void get_title(Task
*tsk
)
161 Panel
*panel
= tsk
->area
.panel
;
164 if (!panel
->g_task
.text
&& !g_tooltip
.enabled
) return;
166 name
= server_get_property (tsk
->win
, server
.atom
._NET_WM_VISIBLE_NAME
, server
.atom
.UTF8_STRING
, 0);
167 if (!name
|| !strlen(name
)) {
168 name
= server_get_property (tsk
->win
, server
.atom
._NET_WM_NAME
, server
.atom
.UTF8_STRING
, 0);
169 if (!name
|| !strlen(name
)) {
170 name
= server_get_property (tsk
->win
, server
.atom
.WM_NAME
, XA_STRING
, 0);
171 if (!name
|| !strlen(name
)) {
173 strcpy(name
, "Untitled");
178 // add space before title
179 title
= malloc(strlen(name
)+2);
180 if (panel
->g_task
.icon
) strcpy(title
, " ");
183 if (name
) XFree (name
);
185 tsk
->area
.redraw
= 1;
192 void get_icon (Task
*tsk
)
194 Panel
*panel
= tsk
->area
.panel
;
195 if (!panel
->g_task
.icon
) return;
197 Imlib_Image img
= NULL
;
202 imlib_context_set_image(tsk
->icon
);
204 imlib_context_set_image(tsk
->icon_active
);
206 tsk
->icon
= tsk
->icon_active
= NULL
;
208 tsk
->area
.redraw
= 1;
210 data
= server_get_property (tsk
->win
, server
.atom
._NET_WM_ICON
, XA_CARDINAL
, &i
);
216 tmp_data
= get_best_icon (data
, get_icon_count (data
, i
), i
, &w
, &h
, panel
->g_task
.icon_size1
);
219 DATA32 icon_data
[w
* h
];
221 for (i
= 0; i
< length
; ++i
)
222 icon_data
[i
] = tmp_data
[i
];
223 img
= imlib_create_image_using_copied_data (w
, h
, icon_data
);
225 img
= imlib_create_image_using_data (w
, h
, (DATA32
*)tmp_data
);
230 hints
= XGetWMHints(server
.dsp
, tsk
->win
);
232 if (hints
->flags
& IconPixmapHint
&& hints
->icon_pixmap
!= 0) {
233 // get width, height and depth for the pixmap
236 uint border_width
, bpp
;
239 //printf(" get pixmap\n");
240 XGetGeometry(server
.dsp
, hints
->icon_pixmap
, &root
, &icon_x
, &icon_y
, &w
, &h
, &border_width
, &bpp
);
241 imlib_context_set_drawable(hints
->icon_pixmap
);
242 img
= imlib_create_image_from_drawable(hints
->icon_mask
, 0, 0, w
, h
, 0);
247 imlib_context_set_image(default_icon
);
248 img
= imlib_clone_image();
252 imlib_context_set_image(img
);
253 imlib_image_set_has_alpha(1);
255 w
= imlib_image_get_width();
256 h
= imlib_image_get_height();
257 tsk
->icon
= imlib_create_cropped_scaled_image(0, 0, w
, h
, panel
->g_task
.icon_size1
, panel
->g_task
.icon_size1
);
260 imlib_context_set_image(tsk
->icon
);
261 tsk
->icon_width
= imlib_image_get_width();
262 tsk
->icon_height
= imlib_image_get_height();
263 tsk
->icon_active
= imlib_clone_image();
266 if (panel
->g_task
.alpha
!= 100 || panel
->g_task
.saturation
!= 0 || panel
->g_task
.brightness
!= 0) {
267 data32
= imlib_image_get_data();
268 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);
269 imlib_image_put_back_data(data32
);
272 if (panel
->g_task
.alpha_active
!= 100 || panel
->g_task
.saturation_active
!= 0 || panel
->g_task
.brightness_active
!= 0) {
273 imlib_context_set_image(tsk
->icon_active
);
274 data32
= imlib_image_get_data();
275 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);
276 imlib_image_put_back_data(data32
);
286 void draw_task_icon (Task
*tsk
, int text_width
, int active
)
288 if (tsk
->icon
== NULL
|| tsk
->icon_active
== NULL
) return;
292 Panel
*panel
= (Panel
*)tsk
->area
.panel
;
293 if (panel
->g_task
.centered
) {
294 if (panel
->g_task
.text
)
295 pos_x
= (tsk
->area
.width
- text_width
- panel
->g_task
.icon_size1
) / 2;
297 pos_x
= (tsk
->area
.width
- panel
->g_task
.icon_size1
) / 2;
299 else pos_x
= panel
->g_task
.area
.paddingxlr
+ panel
->g_task
.area
.pix
.border
.width
;
304 imlib_context_set_image (tsk
->icon
);
305 pmap
= &tsk
->area
.pix
.pmap
;
308 imlib_context_set_image (tsk
->icon_active
);
309 pmap
= &tsk
->area
.pix_active
.pmap
;
311 imlib_context_set_drawable (*pmap
);
312 imlib_render_image_on_drawable (pos_x
, panel
->g_task
.icon_posy
);
316 void draw_task (void *obj
, cairo_t
*c
, int active
)
320 config_color
*config_text
;
322 Panel
*panel
= (Panel
*)tsk
->area
.panel
;
324 if (panel
->g_task
.text
) {
326 layout
= pango_cairo_create_layout (c
);
327 pango_layout_set_font_description (layout
, panel
->g_task
.font_desc
);
328 pango_layout_set_text (layout
, tsk
->title
, -1);
330 /* Drawing width and Cut text */
331 // pango use U+22EF or U+2026
332 pango_layout_set_width (layout
, ((Taskbar
*)tsk
->area
.parent
)->text_width
* PANGO_SCALE
);
333 pango_layout_set_ellipsize (layout
, PANGO_ELLIPSIZE_END
);
336 if (panel
->g_task
.centered
) pango_layout_set_alignment (layout
, PANGO_ALIGN_CENTER
);
337 else pango_layout_set_alignment (layout
, PANGO_ALIGN_LEFT
);
339 pango_layout_get_pixel_size (layout
, &width
, &height
);
341 if (active
) config_text
= &panel
->g_task
.font_active
;
342 else config_text
= &panel
->g_task
.font
;
344 cairo_set_source_rgba (c
, config_text
->color
[0], config_text
->color
[1], config_text
->color
[2], config_text
->alpha
);
346 pango_cairo_update_layout (c
, layout
);
347 cairo_move_to (c
, panel
->g_task
.text_posx
, panel
->g_task
.text_posy
);
348 pango_cairo_show_layout (c
, layout
);
350 if (panel
->g_task
.font_shadow
) {
351 cairo_set_source_rgba (c
, 0.0, 0.0, 0.0, 0.5);
352 pango_cairo_update_layout (c
, layout
);
353 cairo_move_to (c
, panel
->g_task
.text_posx
+ 1, panel
->g_task
.text_posy
+ 1);
354 pango_cairo_show_layout (c
, layout
);
356 g_object_unref (layout
);
359 if (panel
->g_task
.icon
) {
360 // icon use same opacity as text
361 draw_task_icon (tsk
, width
, active
);
366 Task
*next_task(Task
*tsk
)
372 for (i
=0 ; i
< nb_panel
; i
++) {
373 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
374 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
377 if (l0
->next
== NULL
) l0
= panel1
[i
].taskbar
[j
].area
.list
;
388 Task
*prev_task(Task
*tsk
)
394 for (i
=0 ; i
< nb_panel
; i
++) {
395 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
397 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
400 if (l0
== panel1
[i
].taskbar
[j
].area
.list
) {
401 l0
= g_slist_last ( l0
);
422 for (i
=0 ; i
< nb_panel
; i
++) {
423 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
424 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
426 tsk1
->area
.is_active
= 0;
433 Window w1
= window_get_active ();
434 //printf("Change active task %ld\n", w1);
436 tsk2
= task_get_task(w1
);
439 if (XGetTransientForHint(server
.dsp
, w1
, &w2
) != 0)
440 if (w2
) tsk2
= task_get_task(w2
);
442 if ( is_urgent(tsk2
) ) {
445 // put active state on all task (multi_desktop)
447 for (i
=0 ; i
< nb_panel
; i
++) {
448 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
449 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
451 if (tsk1
->win
== tsk2
->win
) {
452 tsk1
->area
.is_active
= 1;
464 GSList
* urgent_task
= urgent_list
;
465 while (urgent_task
) {
466 Task_urgent
* t
= urgent_task
->data
;
467 if ( t
->tick
< max_tick_urgent
) {
468 t
->tsk
->area
.is_active
= !t
->tsk
->area
.is_active
;
469 t
->tsk
->area
.redraw
= 1;
472 urgent_task
= urgent_task
->next
;
478 void add_urgent(Task
*tsk
)
483 // some programs set urgency hint although they are active
484 if ( task_active
&& task_active
->win
== tsk
->win
)
487 // first check if task is already in the list and reset the counter
488 GSList
* urgent_task
= urgent_list
;
489 while (urgent_task
) {
490 Task_urgent
* t
= urgent_task
->data
;
495 urgent_task
= urgent_task
->next
;
498 // not yet in the list, so we have to add it
499 Task_urgent
* t
= malloc(sizeof(Task_urgent
));
504 urgent_list
= g_slist_prepend(urgent_list
, t
);
506 if (urgent_timer
== 0)
507 urgent_timer
= install_timer(0, 1000000, 1, 0, blink_urgent
);
509 reset_timer(urgent_timer
, 0, 1000000, 1, 0);
513 void del_urgent(Task
*tsk
)
515 GSList
* urgent_task
= urgent_list
;
516 while (urgent_task
) {
517 Task_urgent
* t
= urgent_task
->data
;
519 urgent_list
= g_slist_remove(urgent_list
, t
);
522 reset_timer(urgent_timer
, 0, 0, 0, 0);
525 urgent_task
= urgent_task
->next
;
529 int is_urgent(Task
*tsk
)
531 GSList
* urgent_task
= urgent_list
;
532 while (urgent_task
) {
533 Task_urgent
* t
= urgent_task
->data
;
536 urgent_task
= urgent_task
->next
;
This page took 0.055612 seconds and 4 git commands to generate.