]>
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 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
];
64 new_tsk
.current_state
= window_is_iconified(win
) ? TASK_ICONIFIED
: TASK_NORMAL
;
66 // allocate only one title and one icon
67 // even with task_on_all_desktop and with task_on_all_panel
70 for (k
=0; k
<TASK_STATE_COUNT
; ++k
)
75 //printf("task %s : desktop %d, monitor %d\n", new_tsk->title, desktop, monitor);
76 XSelectInput (server
.dsp
, new_tsk
.win
, PropertyChangeMask
|StructureNotifyMask
);
81 // QUESTION: Do we need to iterate over nb_panel (we know the monitor, why not taking panel1[monitor]???
82 for (i
=0 ; i
< nb_panel
; i
++) {
83 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
84 if (new_tsk
.desktop
!= ALLDESKTOP
&& new_tsk
.desktop
!= j
) continue;
85 if (nb_panel
> 1 && panel1
[i
].monitor
!= monitor
) continue;
87 tskbar
= &panel1
[i
].taskbar
[j
];
88 new_tsk2
= malloc(sizeof(Task
));
89 memcpy(&new_tsk2
->area
, &panel1
[i
].g_task
.area
, sizeof(Area
));
90 new_tsk2
->area
.parent
= tskbar
;
91 new_tsk2
->win
= new_tsk
.win
;
92 new_tsk2
->desktop
= new_tsk
.desktop
;
93 set_task_state(new_tsk2
, new_tsk
.current_state
);
94 if (new_tsk2
->desktop
== ALLDESKTOP
&& server
.desktop
!= j
) {
95 // hide ALLDESKTOP task on non-current desktop
96 new_tsk2
->area
.on_screen
= 0;
98 new_tsk2
->title
= new_tsk
.title
;
99 new_tsk2
->area
._get_tooltip_text
= task_get_tooltip
;
100 for (k
=0; k
<TASK_STATE_COUNT
; ++k
)
101 new_tsk2
->icon
[k
] = new_tsk
.icon
[k
];
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 //printf("add_task panel %d, desktop %d, task %s\n", i, j, new_tsk2->title);
109 if (window_is_urgent(win
))
110 add_urgent(new_tsk2
);
115 void remove_task (Task
*tsk
)
119 Window win
= tsk
->win
;
120 int desktop
= tsk
->desktop
;
122 // free title and icon just for the first task
123 // even with task_on_all_desktop and with task_on_all_panel
124 //printf("remove_task %s %d\n", tsk->title, tsk->desktop);
128 for (k
=0; k
<TASK_STATE_COUNT
; ++k
) {
130 imlib_context_set_image(tsk
->icon
[k
]);
139 for (i
=0 ; i
< nb_panel
; i
++) {
140 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
141 if (desktop
!= ALLDESKTOP
&& desktop
!= j
) continue;
144 tskbar
= &panel1
[i
].taskbar
[j
];
145 for (l0
= tskbar
->area
.list
; l0
; ) {
148 if (win
== tsk2
->win
) {
149 tskbar
->area
.list
= g_slist_remove(tskbar
->area
.list
, tsk2
);
150 tskbar
->area
.resize
= 1;
152 if (tsk2
== task_active
)
154 if (tsk2
== task_drag
)
156 if (g_slist_find(urgent_list
, tsk2
))
159 XFreePixmap (server
.dsp
, tsk2
->area
.pix
);
168 void get_title(Task
*tsk
)
170 Panel
*panel
= tsk
->area
.panel
;
173 if (!panel
->g_task
.text
&& !g_tooltip
.enabled
) return;
175 name
= server_get_property (tsk
->win
, server
.atom
._NET_WM_VISIBLE_NAME
, server
.atom
.UTF8_STRING
, 0);
176 if (!name
|| !strlen(name
)) {
177 name
= server_get_property (tsk
->win
, server
.atom
._NET_WM_NAME
, server
.atom
.UTF8_STRING
, 0);
178 if (!name
|| !strlen(name
)) {
179 name
= server_get_property (tsk
->win
, server
.atom
.WM_NAME
, XA_STRING
, 0);
180 if (!name
|| !strlen(name
)) {
182 strcpy(name
, "Untitled");
187 // add space before title
188 title
= malloc(strlen(name
)+2);
189 if (panel
->g_task
.icon
) strcpy(title
, " ");
192 if (name
) XFree (name
);
194 tsk
->area
.redraw
= 1;
201 void get_icon (Task
*tsk
)
203 Panel
*panel
= tsk
->area
.panel
;
204 if (!panel
->g_task
.icon
) return;
206 Imlib_Image img
= NULL
;
211 for (k
=0; k
<TASK_STATE_COUNT
; ++k
) {
213 imlib_context_set_image(tsk
->icon
[k
]);
218 tsk
->area
.redraw
= 1;
220 data
= server_get_property (tsk
->win
, server
.atom
._NET_WM_ICON
, XA_CARDINAL
, &i
);
226 tmp_data
= get_best_icon (data
, get_icon_count (data
, i
), i
, &w
, &h
, panel
->g_task
.icon_size1
);
229 DATA32 icon_data
[w
* h
];
231 for (i
= 0; i
< length
; ++i
)
232 icon_data
[i
] = tmp_data
[i
];
233 img
= imlib_create_image_using_copied_data (w
, h
, icon_data
);
235 img
= imlib_create_image_using_data (w
, h
, (DATA32
*)tmp_data
);
240 hints
= XGetWMHints(server
.dsp
, tsk
->win
);
242 if (hints
->flags
& IconPixmapHint
&& hints
->icon_pixmap
!= 0) {
243 // get width, height and depth for the pixmap
246 uint border_width
, bpp
;
249 //printf(" get pixmap\n");
250 XGetGeometry(server
.dsp
, hints
->icon_pixmap
, &root
, &icon_x
, &icon_y
, &w
, &h
, &border_width
, &bpp
);
251 imlib_context_set_drawable(hints
->icon_pixmap
);
252 img
= imlib_create_image_from_drawable(hints
->icon_mask
, 0, 0, w
, h
, 0);
257 imlib_context_set_image(default_icon
);
258 img
= imlib_clone_image();
262 imlib_context_set_image(img
);
263 imlib_image_set_has_alpha(1);
265 w
= imlib_image_get_width();
266 h
= imlib_image_get_height();
267 Imlib_Image orig_image
= imlib_create_cropped_scaled_image(0, 0, w
, h
, panel
->g_task
.icon_size1
, panel
->g_task
.icon_size1
);
270 imlib_context_set_image(orig_image
);
271 tsk
->icon_width
= imlib_image_get_width();
272 tsk
->icon_height
= imlib_image_get_height();
273 for (k
=0; k
<TASK_STATE_COUNT
; ++k
) {
274 imlib_context_set_image(orig_image
);
275 tsk
->icon
[k
] = imlib_clone_image();
276 imlib_context_set_image(tsk
->icon
[k
]);
278 if (panel
->g_task
.alpha
[k
] != 100 || panel
->g_task
.saturation
[k
] != 0 || panel
->g_task
.brightness
[k
] != 0) {
279 data32
= imlib_image_get_data();
280 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);
281 imlib_image_put_back_data(data32
);
284 imlib_context_set_image(orig_image
);
294 void draw_task_icon (Task
*tsk
, int text_width
)
296 if (tsk
->icon
[tsk
->current_state
] == 0) return;
300 Panel
*panel
= (Panel
*)tsk
->area
.panel
;
301 if (panel
->g_task
.centered
) {
302 if (panel
->g_task
.text
)
303 pos_x
= (tsk
->area
.width
- text_width
- panel
->g_task
.icon_size1
) / 2;
305 pos_x
= (tsk
->area
.width
- panel
->g_task
.icon_size1
) / 2;
307 else pos_x
= panel
->g_task
.area
.paddingxlr
+ tsk
->area
.bg
->border
.width
;
310 imlib_context_set_image (tsk
->icon
[tsk
->current_state
]);
311 if (real_transparency
) {
312 render_image(tsk
->area
.pix
, pos_x
, panel
->g_task
.icon_posy
, imlib_image_get_width(), imlib_image_get_height() );
315 imlib_context_set_drawable(tsk
->area
.pix
);
316 imlib_render_image_on_drawable (pos_x
, panel
->g_task
.icon_posy
);
321 void draw_task (void *obj
, cairo_t
*c
)
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 config_text
= &panel
->g_task
.font
[tsk
->current_state
];
348 cairo_set_source_rgba (c
, config_text
->color
[0], config_text
->color
[1], config_text
->color
[2], config_text
->alpha
);
350 pango_cairo_update_layout (c
, layout
);
351 cairo_move_to (c
, panel
->g_task
.text_posx
, panel
->g_task
.text_posy
);
352 pango_cairo_show_layout (c
, layout
);
354 if (panel
->g_task
.font_shadow
) {
355 cairo_set_source_rgba (c
, 0.0, 0.0, 0.0, 0.5);
356 pango_cairo_update_layout (c
, layout
);
357 cairo_move_to (c
, panel
->g_task
.text_posx
+ 1, panel
->g_task
.text_posy
+ 1);
358 pango_cairo_show_layout (c
, layout
);
360 g_object_unref (layout
);
363 if (panel
->g_task
.icon
) {
364 // icon use same opacity as text
365 draw_task_icon (tsk
, width
);
370 Task
*next_task(Task
*tsk
)
376 for (i
=0 ; i
< nb_panel
; i
++) {
377 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
378 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
381 if (l0
->next
== NULL
) l0
= panel1
[i
].taskbar
[j
].area
.list
;
392 Task
*prev_task(Task
*tsk
)
398 for (i
=0 ; i
< nb_panel
; i
++) {
399 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
401 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
404 if (l0
== panel1
[i
].taskbar
[j
].area
.list
) {
405 l0
= g_slist_last ( l0
);
426 for (i
=0 ; i
< nb_panel
; i
++) {
427 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
428 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
430 if (tsk1
->win
== task_active
->win
)
431 set_task_state(tsk1
, window_is_iconified(tsk1
->win
) ? TASK_ICONIFIED
: TASK_NORMAL
);
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
);
448 if ( g_slist_find(urgent_list
, tsk2
) )
451 // put active state on all task (multi_desktop)
453 for (i
=0 ; i
< nb_panel
; i
++) {
454 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
455 for (l0
= panel1
[i
].taskbar
[j
].area
.list
; l0
; l0
= l0
->next
) {
457 if (tsk1
->win
== tsk2
->win
)
458 set_task_state(tsk1
, TASK_ACTIVE
);
467 void set_task_state(Task
*tsk
, int state
)
469 tsk
->current_state
= state
;
470 tsk
->area
.bg
= panel1
[0].g_task
.background
[state
];
471 tsk
->area
.redraw
= 1;
475 void blink_urgent(void* arg
)
477 GSList
* urgent_task
= urgent_list
;
478 while (urgent_task
) {
479 Task
* t
= urgent_task
->data
;
480 if ( t
->urgent_tick
< max_tick_urgent
) {
481 if (t
->urgent_tick
++ % 2)
482 set_task_state(t
, TASK_URGENT
);
484 set_task_state(t
, window_is_iconified(t
->win
) ? TASK_ICONIFIED
: TASK_NORMAL
);
486 urgent_task
= urgent_task
->next
;
492 void add_urgent(Task
*tsk
)
497 // some programs set urgency hint although they are active
498 if ( task_active
&& task_active
->win
== tsk
->win
)
501 // reset counter to 0, remove tasks which are already in the list
502 GSList
* urgent_add
= task_get_tasks(tsk
->win
);
503 GSList
* it
= urgent_add
;
507 tsk2
->urgent_tick
= 0;
509 if (g_slist_find(urgent_list
, tsk2
))
510 urgent_add
= g_slist_remove(urgent_add
, tsk2
);
513 // not yet in the list, so we have to add it
514 urgent_list
= g_slist_concat(urgent_add
, urgent_list
);
516 if (urgent_timeout
== 0)
517 urgent_timeout
= add_timeout(10, 1000, blink_urgent
, 0);
521 void del_urgent(Task
*tsk
)
526 GSList
* urgent_del
= task_get_tasks(tsk
->win
);
527 GSList
* it
= urgent_del
;
529 urgent_list
= g_slist_remove(urgent_list
, it
->data
);
532 g_slist_free(urgent_del
);
534 stop_timeout(urgent_timeout
);
This page took 0.065679 seconds and 4 git commands to generate.