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