]> Dogcows Code - chaz/tint2/blob - src/taskbar/task.c
fixed segfault with icon
[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 Task *new_tsk;
40 int desktop, monitor;
41
42 if (!win || window_is_hidden (win) || win == window.main_win) return;
43
44 new_tsk = malloc(sizeof(Task));
45 new_tsk->win = win;
46 new_tsk->title = 0;
47 new_tsk->icon_data = 0;
48
49 get_icon(new_tsk);
50 get_title(new_tsk);
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);
55
56 //if (panel.mode == MULTI_MONITOR) monitor = window_get_monitor (new_tsk->win);
57 //else monitor = 0;
58 //printf("task %s : desktop %d, monitor %d\n", new_tsk->title, desktop, monitor);
59
60 XSelectInput (server.dsp, new_tsk->win, PropertyChangeMask|StructureNotifyMask);
61
62 if (desktop == 0xFFFFFFFF) {
63 if (new_tsk->title) {
64 free (new_tsk->title);
65 new_tsk->title = 0;
66 }
67 if (new_tsk->icon_data) {
68 XFree (new_tsk->icon_data);
69 new_tsk->icon_data = 0;
70 }
71 free(new_tsk);
72 fprintf(stderr, "task on all desktop : ignored\n");
73 return;
74 }
75
76 Taskbar *tskbar;
77 tskbar = &panel.taskbar[index(desktop, monitor)];
78 new_tsk->area.parent = tskbar;
79 tskbar->area.list = g_slist_append(tskbar->area.list, new_tsk);
80
81 if (resize_tasks (tskbar))
82 redraw (&tskbar->area);
83 }
84
85
86 void remove_task (Task *tsk)
87 {
88 if (!tsk) return;
89
90 Taskbar *tskbar;
91 tskbar = (Taskbar*)tsk->area.parent;
92 tskbar->area.list = g_slist_remove(tskbar->area.list, tsk);
93 resize_tasks (tskbar);
94 redraw (&tskbar->area);
95
96 if (tsk->title) {
97 free (tsk->title);
98 tsk->title = 0;
99 }
100 if (tsk->icon_data) {
101 XFree (tsk->icon_data);
102 tsk->icon_data = 0;
103 }
104 XFreePixmap (server.dsp, tsk->area.pmap);
105 XFreePixmap (server.dsp, tsk->area_active.pmap);
106 free(tsk);
107 }
108
109
110 void get_title(Task *tsk)
111 {
112 if (!g_task.text) return;
113
114 char *title, *name;
115
116 name = server_get_property (tsk->win, server.atom._NET_WM_VISIBLE_NAME, server.atom.UTF8_STRING, 0);
117 if (!name || !strlen(name)) {
118 name = server_get_property (tsk->win, server.atom._NET_WM_NAME, server.atom.UTF8_STRING, 0);
119 if (!name || !strlen(name)) {
120 name = server_get_property (tsk->win, server.atom.WM_NAME, XA_STRING, 0);
121 if (!name || !strlen(name)) {
122 name = malloc(10);
123 strcpy(name, "Untitled");
124 }
125 }
126 }
127
128 // add space before title
129 title = malloc(strlen(name)+2);
130 if (g_task.icon) strcpy(title, " ");
131 else title[0] = 0;
132 strcat(title, name);
133 if (name) XFree (name);
134
135 if (tsk->title)
136 free(tsk->title);
137 tsk->title = title;
138 }
139
140
141 void get_icon (Task *tsk)
142 {
143 if (!g_task.icon) return;
144
145 long *data;
146 int num;
147
148 data = server_get_property (tsk->win, server.atom._NET_WM_ICON, XA_CARDINAL, &num);
149 if (!data) return;
150
151 int w, h;
152 long *tmp_data;
153 tmp_data = get_best_icon (data, get_icon_count (data, num), num, &w, &h, g_task.icon_size1);
154
155 tsk->icon_width = w;
156 tsk->icon_height = h;
157 tsk->icon_data = malloc (w * h * sizeof (long));
158 memcpy (tsk->icon_data, tmp_data, w * h * sizeof (long));
159
160 XFree (data);
161 }
162
163
164 void draw_task_icon (Task *tsk, int text_width, int active)
165 {
166 if (tsk->icon_data == 0) get_icon (tsk);
167 if (tsk->icon_data == 0) return;
168
169 Pixmap *pmap;
170
171 if (active) pmap = &tsk->area_active.pmap;
172 else pmap = &tsk->area.pmap;
173
174 /* Find pos */
175 int pos_x;
176 if (g_task.centered) {
177 if (g_task.text)
178 pos_x = (tsk->area.width - text_width - g_task.icon_size1) / 2;
179 else
180 pos_x = (tsk->area.width - g_task.icon_size1) / 2;
181 }
182 else pos_x = g_task.area.paddingx + g_task.area.border.width;
183
184 /* Render */
185 Imlib_Image icon;
186 Imlib_Color_Modifier cmod;
187 DATA8 red[256], green[256], blue[256], alpha[256];
188
189 // TODO: cpu improvement : compute only when icon changed
190 DATA32 *data;
191 /* do we have 64bit? => long = 8bit */
192 if (sizeof(long) != 4) {
193 int length = tsk->icon_width * tsk->icon_height;
194 data = malloc(sizeof(DATA32) * length);
195 int i;
196 for (i = 0; i < length; ++i)
197 data[i] = tsk->icon_data[i];
198 }
199 else data = (DATA32 *) tsk->icon_data;
200
201 icon = imlib_create_image_using_data (tsk->icon_width, tsk->icon_height, data);
202 imlib_context_set_image (icon);
203 imlib_context_set_drawable (*pmap);
204
205 cmod = imlib_create_color_modifier ();
206 imlib_context_set_color_modifier (cmod);
207 imlib_image_set_has_alpha (1);
208 imlib_get_color_modifier_tables (red, green, blue, alpha);
209
210 int i, opacity;
211 if (active) opacity = 255*g_task.font_active.alpha;
212 else opacity = 255*g_task.font.alpha;
213 for(i = 127; i < 256; i++) alpha[i] = opacity;
214
215 imlib_set_color_modifier_tables (red, green, blue, alpha);
216
217 //imlib_render_image_on_drawable (pos_x, pos_y);
218 imlib_render_image_on_drawable_at_size (pos_x, g_task.icon_posy, g_task.icon_size1, g_task.icon_size1);
219
220 imlib_free_color_modifier ();
221 imlib_free_image ();
222 if (sizeof(long) != 4) free(data);
223 }
224
225
226 void draw_task_title (cairo_t *c, Task *tsk, int active)
227 {
228 PangoLayout *layout;
229 config_color *config_text;
230 int width, height;
231
232 if (g_task.text) {
233 /* Layout */
234 layout = pango_cairo_create_layout (c);
235 pango_layout_set_font_description (layout, g_task.font_desc);
236 pango_layout_set_text (layout, tsk->title, -1);
237
238 /* Drawing width and Cut text */
239 pango_layout_set_width (layout, ((Taskbar*)tsk->area.parent)->text_width * PANGO_SCALE);
240 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
241
242 /* Center text */
243 if (g_task.centered) pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
244 else pango_layout_set_alignment (layout, PANGO_ALIGN_LEFT);
245
246 pango_layout_get_pixel_size (layout, &width, &height);
247
248 if (active) config_text = &g_task.font_active;
249 else config_text = &g_task.font;
250
251 cairo_set_source_rgba (c, config_text->color[0], config_text->color[1], config_text->color[2], config_text->alpha);
252
253 pango_cairo_update_layout (c, layout);
254 cairo_move_to (c, g_task.text_posx, g_task.text_posy);
255 pango_cairo_show_layout (c, layout);
256
257 if (g_task.font_shadow) {
258 cairo_set_source_rgba (c, 0.0, 0.0, 0.0, 0.5);
259 pango_cairo_update_layout (c, layout);
260 cairo_move_to (c, g_task.text_posx + 1, g_task.text_posy + 1);
261 pango_cairo_show_layout (c, layout);
262 }
263 g_object_unref (layout);
264 }
265
266 if (g_task.icon) {
267 // icon use same opacity as text
268 draw_task_icon (tsk, width, active);
269 }
270 }
271
272
273 int draw_foreground_task (void *obj, cairo_t *c)
274 {
275 Task *tsk = obj;
276 cairo_surface_t *cs;
277 cairo_t *ca;
278 //printf(" draw_foreground_task\n");
279
280 draw_task_title (c, tsk, 0);
281
282 // draw active pmap
283 if (tsk->area_active.pmap) XFreePixmap (server.dsp, tsk->area_active.pmap);
284 tsk->area_active.pmap = server_create_pixmap (tsk->area.width, tsk->area.height);
285
286 // add layer of root pixmap
287 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);
288
289 cs = cairo_xlib_surface_create (server.dsp, tsk->area_active.pmap, server.visual, tsk->area.width, tsk->area.height);
290 ca = cairo_create (cs);
291
292 // redraw task
293 draw_background (&tsk->area_active, ca);
294 draw_task_title (ca, tsk, 1);
295
296 cairo_destroy (ca);
297 cairo_surface_destroy (cs);
298 return 0;
299 }
300
This page took 0.049286 seconds and 5 git commands to generate.