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 void add_task (Window win
)
40 if (window_is_hidden(win
)) return;
46 new_tsk
.area
.panel
= &panel1
[0];
47 new_tsk
.desktop
= window_get_desktop (win
);
48 if (panel_mode
== SINGLE_MONITOR
) monitor
= window_get_monitor (win
);
51 // allocate only one title and one icon
52 // even with task_on_all_desktop and with task_on_all_panel
54 new_tsk
.icon_data
= 0;
58 //printf("task %s : desktop %d, monitor %d\n", new_tsk->title, desktop, monitor);
59 XSelectInput (server
.dsp
, new_tsk
.win
, PropertyChangeMask
|StructureNotifyMask
);
64 for (i
=0 ; i
< nb_panel
; i
++) {
65 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
66 if (new_tsk
.desktop
!= ALLDESKTOP
&& new_tsk
.desktop
!= j
) continue;
67 if (panel_mode
== SINGLE_MONITOR
&& panel1
[i
].monitor
!= monitor
) continue;
69 tskbar
= &panel1
[i
].taskbar
[j
];
70 new_tsk2
= malloc(sizeof(Task
));
71 memcpy(&new_tsk2
->area
, &panel1
[i
].g_task
.area
, sizeof(Area
));
72 new_tsk2
->area
.parent
= tskbar
;
73 new_tsk2
->win
= new_tsk
.win
;
74 new_tsk2
->desktop
= new_tsk
.desktop
;
75 new_tsk2
->title
= new_tsk
.title
;
76 new_tsk2
->icon_data
= new_tsk
.icon_data
;
77 new_tsk2
->icon_width
= new_tsk
.icon_width
;
78 new_tsk2
->icon_height
= new_tsk
.icon_height
;
79 tskbar
->area
.list
= g_slist_append(tskbar
->area
.list
, new_tsk2
);
81 //printf("add_task panel %d, desktop %d, task %s\n", i, j, new_tsk2->title);
82 // set_resize (&tskbar->area);
83 if (resize_tasks (tskbar
))
84 set_redraw (&tskbar
->area
);
90 void remove_task (Task
*tsk
)
94 Window win
= tsk
->win
;
95 int desktop
= tsk
->desktop
;
97 // free title and icon just for the first task
98 // even with task_on_all_desktop and with task_on_all_panel
99 //printf("remove_task %s %d\n", tsk->title, tsk->desktop);
100 //printf("remove_task %s \n", tsk->title);
104 free (tsk
->icon_data
);
109 for (i
=0 ; i
< nb_panel
; i
++) {
110 for (j
=0 ; j
< panel1
[i
].nb_desktop
; j
++) {
111 if (desktop
!= ALLDESKTOP
&& desktop
!= j
) continue;
114 tskbar
= &panel1
[i
].taskbar
[j
];
115 for (l0
= tskbar
->area
.list
; l0
; ) {
118 if (win
== tsk2
->win
) {
119 tskbar
->area
.list
= g_slist_remove(tskbar
->area
.list
, tsk2
);
120 set_resize (&tskbar
->area
);
121 set_redraw (&tskbar
->area
);
123 if (tsk2
== task_active
)
125 if (tsk2
== task_drag
)
128 XFreePixmap (server
.dsp
, tsk2
->area
.pix
.pmap
);
129 XFreePixmap (server
.dsp
, tsk2
->area
.pix_active
.pmap
);
139 void get_title(Task
*tsk
)
141 Panel
*panel
= tsk
->area
.panel
;
144 if (!panel
->g_task
.text
) return;
146 name
= server_get_property (tsk
->win
, server
.atom
._NET_WM_VISIBLE_NAME
, server
.atom
.UTF8_STRING
, 0);
147 if (!name
|| !strlen(name
)) {
148 name
= server_get_property (tsk
->win
, server
.atom
._NET_WM_NAME
, server
.atom
.UTF8_STRING
, 0);
149 if (!name
|| !strlen(name
)) {
150 name
= server_get_property (tsk
->win
, server
.atom
.WM_NAME
, XA_STRING
, 0);
151 if (!name
|| !strlen(name
)) {
153 strcpy(name
, "Untitled");
158 // add space before title
159 title
= malloc(strlen(name
)+2);
160 if (panel
->g_task
.icon
) strcpy(title
, " ");
163 if (name
) XFree (name
);
165 tsk
->area
.redraw
= 1;
172 void get_icon (Task
*tsk
)
174 Panel
*panel
= tsk
->area
.panel
;
175 if (!panel
->g_task
.icon
) return;
177 if (tsk
->icon_data
) {
178 free (tsk
->icon_data
);
181 tsk
->area
.redraw
= 1;
185 data
= server_get_property (tsk
->win
, server
.atom
._NET_WM_ICON
, XA_CARDINAL
, &num
);
190 tmp_data
= get_best_icon (data
, get_icon_count (data
, num
), num
, &w
, &h
, panel
->g_task
.icon_size1
);
193 tsk
->icon_height
= h
;
194 tsk
->icon_data
= malloc (w
* h
* sizeof (long));
195 memcpy (tsk
->icon_data
, tmp_data
, w
* h
* sizeof (long));
201 XWMHints
*hints
= XGetWMHints(server
.dsp
, tsk
->win
);
203 if (hints
->flags
& IconPixmapHint
&& hints
->icon_pixmap
!= 0) {
204 // get width, height and depth for the pixmap
207 uint border_width
, bpp
;
208 uint icon_width
, icon_height
;
210 XGetGeometry(server
.dsp
, hints
->icon_pixmap
, &root
, &icon_x
, &icon_y
, &icon_width
, &icon_height
, &border_width
, &bpp
);
212 //printf(" get_pixmap\n");
214 imlib_context_set_drawable(hints
->icon_pixmap
);
215 img
= imlib_create_image_from_drawable(hints
->icon_mask
, 0, 0, icon_width
, icon_height
, 0);
216 imlib_context_set_image(img
);
217 unsigned int *data
= imlib_image_get_data();
221 tsk
->icon_width
= imlib_image_get_width();
222 tsk
->icon_height
= imlib_image_get_height();
223 tsk
->icon_data
= malloc (tsk
->icon_width
* tsk
->icon_height
* sizeof (long));
224 memcpy (tsk
->icon_data
, data
, tsk
->icon_width
* tsk
->icon_height
* sizeof (long));
233 void draw_task_icon (Task
*tsk
, int text_width
, int active
)
235 if (tsk
->icon_data
== 0) return;
237 Pixmap
*pmap
= (active
== 0) ? (&tsk
->area
.pix
.pmap
) : (&tsk
->area
.pix_active
.pmap
);
241 Panel
*panel
= (Panel
*)tsk
->area
.panel
;
242 if (panel
->g_task
.centered
) {
243 if (panel
->g_task
.text
)
244 pos_x
= (tsk
->area
.width
- text_width
- panel
->g_task
.icon_size1
) / 2;
246 pos_x
= (tsk
->area
.width
- panel
->g_task
.icon_size1
) / 2;
248 else pos_x
= panel
->g_task
.area
.paddingxlr
+ panel
->g_task
.area
.pix
.border
.width
;
252 Imlib_Color_Modifier cmod
;
253 DATA8 red
[256], green
[256], blue
[256], alpha
[256];
255 // TODO: cpu improvement : compute only when icon changed
257 /* do we have 64bit? => long = 8bit */
258 if (sizeof(long) != 4) {
259 int length
= tsk
->icon_width
* tsk
->icon_height
;
260 data
= malloc(sizeof(DATA32
) * length
);
262 for (i
= 0; i
< length
; ++i
)
263 data
[i
] = tsk
->icon_data
[i
];
265 else data
= (DATA32
*) tsk
->icon_data
;
267 icon
= imlib_create_image_using_data (tsk
->icon_width
, tsk
->icon_height
, data
);
268 imlib_context_set_image (icon
);
269 imlib_context_set_drawable (*pmap
);
271 cmod
= imlib_create_color_modifier ();
272 imlib_context_set_color_modifier (cmod
);
273 imlib_image_set_has_alpha (1);
274 imlib_get_color_modifier_tables (red
, green
, blue
, alpha
);
277 opacity
= (active
== 0) ? (255*panel
->g_task
.font
.alpha
) : (255*panel
->g_task
.font_active
.alpha
);
278 for (i
= 127; i
< 256; i
++) alpha
[i
] = opacity
;
280 imlib_set_color_modifier_tables (red
, green
, blue
, alpha
);
282 //imlib_render_image_on_drawable (pos_x, pos_y);
283 imlib_render_image_on_drawable_at_size (pos_x
, panel
->g_task
.icon_posy
, panel
->g_task
.icon_size1
, panel
->g_task
.icon_size1
);
285 imlib_free_color_modifier ();
287 if (sizeof(long) != 4) free(data
);
291 void draw_foreground_task (void *obj
, cairo_t
*c
, int active
)
295 config_color
*config_text
;
297 Panel
*panel
= (Panel
*)tsk
->area
.panel
;
299 if (panel
->g_task
.text
) {
301 layout
= pango_cairo_create_layout (c
);
302 pango_layout_set_font_description (layout
, panel
->g_task
.font_desc
);
303 pango_layout_set_text (layout
, tsk
->title
, -1);
305 /* Drawing width and Cut text */
306 pango_layout_set_width (layout
, ((Taskbar
*)tsk
->area
.parent
)->text_width
* PANGO_SCALE
);
307 pango_layout_set_ellipsize (layout
, PANGO_ELLIPSIZE_END
);
310 if (panel
->g_task
.centered
) pango_layout_set_alignment (layout
, PANGO_ALIGN_CENTER
);
311 else pango_layout_set_alignment (layout
, PANGO_ALIGN_LEFT
);
313 pango_layout_get_pixel_size (layout
, &width
, &height
);
315 if (active
) config_text
= &panel
->g_task
.font_active
;
316 else config_text
= &panel
->g_task
.font
;
318 cairo_set_source_rgba (c
, config_text
->color
[0], config_text
->color
[1], config_text
->color
[2], config_text
->alpha
);
320 pango_cairo_update_layout (c
, layout
);
321 cairo_move_to (c
, panel
->g_task
.text_posx
, panel
->g_task
.text_posy
);
322 pango_cairo_show_layout (c
, layout
);
324 if (panel
->g_task
.font_shadow
) {
325 cairo_set_source_rgba (c
, 0.0, 0.0, 0.0, 0.5);
326 pango_cairo_update_layout (c
, layout
);
327 cairo_move_to (c
, panel
->g_task
.text_posx
+ 1, panel
->g_task
.text_posy
+ 1);
328 pango_cairo_show_layout (c
, layout
);
330 g_object_unref (layout
);
333 if (panel
->g_task
.icon
) {
334 // icon use same opacity as text
335 draw_task_icon (tsk
, width
, active
);