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
)
42 if (!win
|| window_is_hidden (win
) || win
== window
.main_win
) return;
44 new_tsk
= malloc(sizeof(Task
));
47 new_tsk
->icon_data
= 0;
51 memcpy(&new_tsk
->area
, &g_task
.area
, sizeof(Area
));
52 memcpy(&new_tsk
->area_active
, &g_task
.area_active
, sizeof(Area
));
53 desktop
= window_get_desktop (new_tsk
->win
);
54 monitor
= window_get_monitor (new_tsk
->win
);
56 //if (panel.mode == MULTI_MONITOR) monitor = window_get_monitor (new_tsk->win);
58 //printf("task %s : desktop %d, monitor %d\n", new_tsk->title, desktop, monitor);
60 XSelectInput (server
.dsp
, new_tsk
->win
, PropertyChangeMask
|StructureNotifyMask
);
62 if (desktop
== 0xFFFFFFFF) {
63 if (new_tsk
->title
) XFree (new_tsk
->title
);
64 if (new_tsk
->icon_data
) XFree (new_tsk
->icon_data
);
66 fprintf(stderr
, "task on all desktop : ignored\n");
71 tskbar
= g_slist_nth_data(panel
.area
.list
, index(desktop
, monitor
));
72 new_tsk
->area
.parent
= tskbar
;
73 tskbar
->area
.list
= g_slist_append(tskbar
->area
.list
, new_tsk
);
75 if (resize_tasks (tskbar
))
76 redraw (&tskbar
->area
);
80 void remove_task (Task
*tsk
)
85 tskbar
= (Taskbar
*)tsk
->area
.parent
;
86 tskbar
->area
.list
= g_slist_remove(tskbar
->area
.list
, tsk
);
87 resize_tasks (tskbar
);
88 redraw (&tskbar
->area
);
90 if (tsk
->title
) XFree (tsk
->title
);
91 if (tsk
->icon_data
) XFree (tsk
->icon_data
);
92 XFreePixmap (server
.dsp
, tsk
->area
.pmap
);
93 XFreePixmap (server
.dsp
, tsk
->area_active
.pmap
);
98 void get_title(Task
*tsk
)
100 if (!g_task
.text
) return;
104 if (tsk
->title
) free(tsk
->title
);
106 name
= server_get_property (tsk
->win
, server
.atom
._NET_WM_VISIBLE_NAME
, server
.atom
.UTF8_STRING
, 0);
107 if (!name
|| !strlen(name
)) {
108 name
= server_get_property (tsk
->win
, server
.atom
._NET_WM_NAME
, server
.atom
.UTF8_STRING
, 0);
109 if (!name
|| !strlen(name
)) {
110 name
= server_get_property (tsk
->win
, server
.atom
.WM_NAME
, XA_STRING
, 0);
111 if (!name
|| !strlen(name
)) {
113 strcpy(name
, "Untitled");
118 // add space before title
119 title
= malloc(strlen(name
)+1);
120 if (g_task
.icon
) strcpy(title
, " ");
124 if (name
) XFree (name
);
129 void get_icon (Task
*tsk
)
131 if (!g_task
.icon
) return;
136 data
= server_get_property (tsk
->win
, server
.atom
._NET_WM_ICON
, XA_CARDINAL
, &num
);
141 tmp_data
= get_best_icon (data
, get_icon_count (data
, num
), num
, &w
, &h
, g_task
.icon_size1
);
144 tsk
->icon_height
= h
;
145 tsk
->icon_data
= malloc (w
* h
* sizeof (long));
146 memcpy (tsk
->icon_data
, tmp_data
, w
* h
* sizeof (long));
152 void draw_task_icon (Task
*tsk
, int text_width
, int active
)
154 if (tsk
->icon_data
== 0) get_icon (tsk
);
155 if (tsk
->icon_data
== 0) return;
159 if (active
) pmap
= &tsk
->area_active
.pmap
;
160 else pmap
= &tsk
->area
.pmap
;
164 if (g_task
.centered
) {
166 pos_x
= (tsk
->area
.width
- text_width
- g_task
.icon_size1
) / 2;
168 pos_x
= (tsk
->area
.width
- g_task
.icon_size1
) / 2;
170 else pos_x
= g_task
.area
.paddingx
+ g_task
.area
.border
.width
;
174 Imlib_Color_Modifier cmod
;
175 DATA8 red
[256], green
[256], blue
[256], alpha
[256];
177 // TODO: cpu improvement : compute only when icon changed
179 /* do we have 64bit? => long = 8bit */
180 if (sizeof(long) != 4) {
181 int length
= tsk
->icon_width
* tsk
->icon_height
;
182 data
= malloc(sizeof(DATA32
) * length
);
184 for (i
= 0; i
< length
; ++i
)
185 data
[i
] = tsk
->icon_data
[i
];
187 else data
= (DATA32
*) tsk
->icon_data
;
189 icon
= imlib_create_image_using_data (tsk
->icon_width
, tsk
->icon_height
, data
);
190 imlib_context_set_image (icon
);
191 imlib_context_set_drawable (*pmap
);
193 cmod
= imlib_create_color_modifier ();
194 imlib_context_set_color_modifier (cmod
);
195 imlib_image_set_has_alpha (1);
196 imlib_get_color_modifier_tables (red
, green
, blue
, alpha
);
199 if (active
) opacity
= 255*g_task
.font_active
.alpha
;
200 else opacity
= 255*g_task
.font
.alpha
;
201 for(i
= 127; i
< 256; i
++) alpha
[i
] = opacity
;
203 imlib_set_color_modifier_tables (red
, green
, blue
, alpha
);
205 //imlib_render_image_on_drawable (pos_x, pos_y);
206 imlib_render_image_on_drawable_at_size (pos_x
, g_task
.icon_posy
, g_task
.icon_size1
, g_task
.icon_size1
);
208 imlib_free_color_modifier ();
210 if (sizeof(long) != 4) free(data
);
214 void draw_task_title (cairo_t
*c
, Task
*tsk
, int active
)
217 config_color
*config_text
;
222 layout
= pango_cairo_create_layout (c
);
223 pango_layout_set_font_description (layout
, g_task
.font_desc
);
224 pango_layout_set_text (layout
, tsk
->title
, -1);
226 /* Drawing width and Cut text */
227 pango_layout_set_width (layout
, ((Taskbar
*)tsk
->area
.parent
)->text_width
* PANGO_SCALE
);
228 pango_layout_set_ellipsize (layout
, PANGO_ELLIPSIZE_END
);
231 if (g_task
.centered
) pango_layout_set_alignment (layout
, PANGO_ALIGN_CENTER
);
232 else pango_layout_set_alignment (layout
, PANGO_ALIGN_LEFT
);
234 pango_layout_get_pixel_size (layout
, &width
, &height
);
236 if (active
) config_text
= &g_task
.font_active
;
237 else config_text
= &g_task
.font
;
239 cairo_set_source_rgba (c
, config_text
->color
[0], config_text
->color
[1], config_text
->color
[2], config_text
->alpha
);
241 pango_cairo_update_layout (c
, layout
);
242 cairo_move_to (c
, g_task
.text_posx
, g_task
.text_posy
);
243 pango_cairo_show_layout (c
, layout
);
245 if (g_task
.font_shadow
) {
246 cairo_set_source_rgba (c
, 0.0, 0.0, 0.0, 0.5);
247 pango_cairo_update_layout (c
, layout
);
248 cairo_move_to (c
, g_task
.text_posx
+ 1, g_task
.text_posy
+ 1);
249 pango_cairo_show_layout (c
, layout
);
251 g_object_unref (layout
);
255 // icon use same opacity as text
256 draw_task_icon (tsk
, width
, active
);
261 int draw_foreground_task (void *obj
, cairo_t
*c
)
267 draw_task_title (c
, tsk
, 0);
270 if (tsk
->area_active
.pmap
) XFreePixmap (server
.dsp
, tsk
->area_active
.pmap
);
271 tsk
->area_active
.pmap
= server_create_pixmap (tsk
->area
.width
, tsk
->area
.height
);
273 // add layer of root pixmap
274 XCopyArea (server
.dsp
, server
.pmap
, tsk
->area_active
.pmap
, server
.gc
, tsk
->area
.posx
, tsk
->area
.posy
, tsk
->area
.width
, tsk
->area
.height
, 0, 0);
276 cs
= cairo_xlib_surface_create (server
.dsp
, tsk
->area_active
.pmap
, server
.visual
, tsk
->area
.width
, tsk
->area
.height
);
277 ca
= cairo_create (cs
);
280 draw_background (&tsk
->area_active
, ca
);
281 draw_task_title (ca
, tsk
, 1);
284 cairo_surface_destroy (cs
);