]> Dogcows Code - chaz/tint2/blob - src/taskbar/task.c
tint2 looks good for me. if you see bugs, report it.
[chaz/tint2] / src / taskbar / task.c
1 /**************************************************************************
2 *
3 * Tint2 : task
4 *
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
7 *
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.
11 *
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 **************************************************************************/
20
21 #include <X11/Xlib.h>
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <glib.h>
28 #include <Imlib2.h>
29
30 #include "window.h"
31 #include "task.h"
32 #include "server.h"
33 #include "panel.h"
34
35
36
37 void add_task (Window win)
38 {
39 if (!win) return;
40 if (window_is_hidden(win)) return;
41
42 int monitor;
43
44 Task new_tsk;
45 new_tsk.win = win;
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);
49 else monitor = 0;
50
51 // allocate only one title and one icon
52 // even with task_on_all_desktop and with task_on_all_panel
53 new_tsk.title = 0;
54 new_tsk.icon_data = 0;
55 get_title(&new_tsk);
56 get_icon(&new_tsk);
57
58 //printf("task %s : desktop %d, monitor %d\n", new_tsk->title, desktop, monitor);
59 XSelectInput (server.dsp, new_tsk.win, PropertyChangeMask|StructureNotifyMask);
60
61 Taskbar *tskbar;
62 Task *new_tsk2;
63 int i, j;
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;
68
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);
80 tskbar->area.resize = 1;
81 //printf("add_task panel %d, desktop %d, task %s\n", i, j, new_tsk2->title);
82 }
83 }
84 }
85
86
87 void remove_task (Task *tsk)
88 {
89 if (!tsk) return;
90
91 Window win = tsk->win;
92 int desktop = tsk->desktop;
93
94 // free title and icon just for the first task
95 // even with task_on_all_desktop and with task_on_all_panel
96 //printf("remove_task %s %d\n", tsk->title, tsk->desktop);
97 if (tsk->title)
98 free (tsk->title);
99 if (tsk->icon_data)
100 free (tsk->icon_data);
101
102 int i, j;
103 Task *tsk2;
104 Taskbar *tskbar;
105 for (i=0 ; i < nb_panel ; i++) {
106 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
107 if (desktop != ALLDESKTOP && desktop != j) continue;
108
109 GSList *l0;
110 tskbar = &panel1[i].taskbar[j];
111 for (l0 = tskbar->area.list; l0 ; ) {
112 tsk2 = l0->data;
113 l0 = l0->next;
114 if (win == tsk2->win) {
115 tskbar->area.list = g_slist_remove(tskbar->area.list, tsk2);
116 tskbar->area.resize = 1;
117
118 if (tsk2 == task_active)
119 task_active = 0;
120 if (tsk2 == task_drag)
121 task_drag = 0;
122
123 XFreePixmap (server.dsp, tsk2->area.pix.pmap);
124 XFreePixmap (server.dsp, tsk2->area.pix_active.pmap);
125 free(tsk2);
126 }
127 }
128 }
129 }
130
131 }
132
133
134 void get_title(Task *tsk)
135 {
136 Panel *panel = tsk->area.panel;
137 char *title, *name;
138
139 if (!panel->g_task.text) return;
140
141 name = server_get_property (tsk->win, server.atom._NET_WM_VISIBLE_NAME, server.atom.UTF8_STRING, 0);
142 if (!name || !strlen(name)) {
143 name = server_get_property (tsk->win, server.atom._NET_WM_NAME, server.atom.UTF8_STRING, 0);
144 if (!name || !strlen(name)) {
145 name = server_get_property (tsk->win, server.atom.WM_NAME, XA_STRING, 0);
146 if (!name || !strlen(name)) {
147 name = malloc(10);
148 strcpy(name, "Untitled");
149 }
150 }
151 }
152
153 // add space before title
154 title = malloc(strlen(name)+2);
155 if (panel->g_task.icon) strcpy(title, " ");
156 else title[0] = 0;
157 strcat(title, name);
158 if (name) XFree (name);
159
160 tsk->area.redraw = 1;
161 if (tsk->title)
162 free(tsk->title);
163 tsk->title = title;
164 }
165
166
167 void get_icon (Task *tsk)
168 {
169 Panel *panel = tsk->area.panel;
170 if (!panel->g_task.icon) return;
171
172 if (tsk->icon_data) {
173 free (tsk->icon_data);
174 tsk->icon_data = 0;
175 }
176 tsk->area.redraw = 1;
177
178 long *data;
179 int num;
180 data = server_get_property (tsk->win, server.atom._NET_WM_ICON, XA_CARDINAL, &num);
181 if (data) {
182 // get ARGB icon
183 int w, h;
184 long *tmp_data;
185 tmp_data = get_best_icon (data, get_icon_count (data, num), num, &w, &h, panel->g_task.icon_size1);
186
187 tsk->icon_width = w;
188 tsk->icon_height = h;
189 tsk->icon_data = malloc (w * h * sizeof (long));
190 memcpy (tsk->icon_data, tmp_data, w * h * sizeof (long));
191
192 XFree (data);
193 }
194 else {
195 // get Pixmap icon
196 XWMHints *hints = XGetWMHints(server.dsp, tsk->win);
197 if (hints) {
198 if (hints->flags & IconPixmapHint && hints->icon_pixmap != 0) {
199 // get width, height and depth for the pixmap
200 Window root;
201 int icon_x, icon_y;
202 uint border_width, bpp;
203 uint icon_width, icon_height;
204
205 XGetGeometry(server.dsp, hints->icon_pixmap, &root, &icon_x, &icon_y, &icon_width, &icon_height, &border_width, &bpp);
206
207 //printf(" get_pixmap\n");
208 Imlib_Image img;
209 imlib_context_set_drawable(hints->icon_pixmap);
210 img = imlib_create_image_from_drawable(hints->icon_mask, 0, 0, icon_width, icon_height, 0);
211 imlib_context_set_image(img);
212 unsigned int *data = imlib_image_get_data();
213 if (!data) {
214 return;
215 }
216 tsk->icon_width = imlib_image_get_width();
217 tsk->icon_height = imlib_image_get_height();
218 tsk->icon_data = malloc (tsk->icon_width * tsk->icon_height * sizeof (long));
219 memcpy (tsk->icon_data, data, tsk->icon_width * tsk->icon_height * sizeof (long));
220 imlib_free_image();
221 }
222 XFree(hints);
223 }
224 }
225 }
226
227
228 void draw_task_icon (Task *tsk, int text_width, int active)
229 {
230 if (tsk->icon_data == 0) return;
231
232 Pixmap *pmap = (active == 0) ? (&tsk->area.pix.pmap) : (&tsk->area.pix_active.pmap);
233
234 /* Find pos */
235 int pos_x;
236 Panel *panel = (Panel*)tsk->area.panel;
237 if (panel->g_task.centered) {
238 if (panel->g_task.text)
239 pos_x = (tsk->area.width - text_width - panel->g_task.icon_size1) / 2;
240 else
241 pos_x = (tsk->area.width - panel->g_task.icon_size1) / 2;
242 }
243 else pos_x = panel->g_task.area.paddingxlr + panel->g_task.area.pix.border.width;
244
245 /* Render */
246 Imlib_Image icon;
247 Imlib_Color_Modifier cmod;
248 DATA8 red[256], green[256], blue[256], alpha[256];
249
250 // TODO: cpu improvement : compute only when icon changed
251 DATA32 *data;
252 /* do we have 64bit? => long = 8bit */
253 if (sizeof(long) != 4) {
254 int length = tsk->icon_width * tsk->icon_height;
255 data = malloc(sizeof(DATA32) * length);
256 int i;
257 for (i = 0; i < length; ++i)
258 data[i] = tsk->icon_data[i];
259 }
260 else data = (DATA32 *) tsk->icon_data;
261
262 icon = imlib_create_image_using_data (tsk->icon_width, tsk->icon_height, data);
263 imlib_context_set_image (icon);
264 imlib_context_set_drawable (*pmap);
265
266 cmod = imlib_create_color_modifier ();
267 imlib_context_set_color_modifier (cmod);
268 imlib_image_set_has_alpha (1);
269 imlib_get_color_modifier_tables (red, green, blue, alpha);
270
271 int i, opacity;
272 opacity = (active == 0) ? (255*panel->g_task.font.alpha) : (255*panel->g_task.font_active.alpha);
273 for (i = 127; i < 256; i++) alpha[i] = opacity;
274
275 imlib_set_color_modifier_tables (red, green, blue, alpha);
276
277 //imlib_render_image_on_drawable (pos_x, pos_y);
278 imlib_render_image_on_drawable_at_size (pos_x, panel->g_task.icon_posy, panel->g_task.icon_size1, panel->g_task.icon_size1);
279
280 imlib_free_color_modifier ();
281 imlib_free_image ();
282 if (sizeof(long) != 4) free(data);
283 }
284
285
286 void draw_foreground_task (void *obj, cairo_t *c, int active)
287 {
288 Task *tsk = obj;
289 PangoLayout *layout;
290 config_color *config_text;
291 int width, height;
292 Panel *panel = (Panel*)tsk->area.panel;
293
294 if (panel->g_task.text) {
295 /* Layout */
296 layout = pango_cairo_create_layout (c);
297 pango_layout_set_font_description (layout, panel->g_task.font_desc);
298 pango_layout_set_text (layout, tsk->title, -1);
299
300 /* Drawing width and Cut text */
301 pango_layout_set_width (layout, ((Taskbar*)tsk->area.parent)->text_width * PANGO_SCALE);
302 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
303
304 /* Center text */
305 if (panel->g_task.centered) pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
306 else pango_layout_set_alignment (layout, PANGO_ALIGN_LEFT);
307
308 pango_layout_get_pixel_size (layout, &width, &height);
309
310 if (active) config_text = &panel->g_task.font_active;
311 else config_text = &panel->g_task.font;
312
313 cairo_set_source_rgba (c, config_text->color[0], config_text->color[1], config_text->color[2], config_text->alpha);
314
315 pango_cairo_update_layout (c, layout);
316 cairo_move_to (c, panel->g_task.text_posx, panel->g_task.text_posy);
317 pango_cairo_show_layout (c, layout);
318
319 if (panel->g_task.font_shadow) {
320 cairo_set_source_rgba (c, 0.0, 0.0, 0.0, 0.5);
321 pango_cairo_update_layout (c, layout);
322 cairo_move_to (c, panel->g_task.text_posx + 1, panel->g_task.text_posy + 1);
323 pango_cairo_show_layout (c, layout);
324 }
325 g_object_unref (layout);
326 }
327
328 if (panel->g_task.icon) {
329 // icon use same opacity as text
330 draw_task_icon (tsk, width, active);
331 }
332 }
333
This page took 0.047958 seconds and 5 git commands to generate.