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