]> Dogcows Code - chaz/tint2/blob - src/taskbar/task.c
lower cpu use with icon. replace HUE by ALPHA on icon (see task_icon_asb).
[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 = new_tsk.icon_active = NULL;
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 = new_tsk.icon;
77 new_tsk2->icon_active = new_tsk.icon_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) {
102 imlib_context_set_image(tsk->icon);
103 imlib_free_image();
104 imlib_context_set_image(tsk->icon_active);
105 imlib_free_image();
106 tsk->icon = tsk->icon_active = NULL;
107 }
108
109 int i, j;
110 Task *tsk2;
111 Taskbar *tskbar;
112 for (i=0 ; i < nb_panel ; i++) {
113 for (j=0 ; j < panel1[i].nb_desktop ; j++) {
114 if (desktop != ALLDESKTOP && desktop != j) continue;
115
116 GSList *l0;
117 tskbar = &panel1[i].taskbar[j];
118 for (l0 = tskbar->area.list; l0 ; ) {
119 tsk2 = l0->data;
120 l0 = l0->next;
121 if (win == tsk2->win) {
122 tskbar->area.list = g_slist_remove(tskbar->area.list, tsk2);
123 tskbar->area.resize = 1;
124
125 if (tsk2 == task_active)
126 task_active = 0;
127 if (tsk2 == task_drag)
128 task_drag = 0;
129
130 XFreePixmap (server.dsp, tsk2->area.pix.pmap);
131 XFreePixmap (server.dsp, tsk2->area.pix_active.pmap);
132 free(tsk2);
133 }
134 }
135 }
136 }
137 }
138
139
140 void get_title(Task *tsk)
141 {
142 Panel *panel = tsk->area.panel;
143 char *title, *name;
144
145 if (!panel->g_task.text && !g_tooltip.enabled) return;
146
147 name = server_get_property (tsk->win, server.atom._NET_WM_VISIBLE_NAME, server.atom.UTF8_STRING, 0);
148 if (!name || !strlen(name)) {
149 name = server_get_property (tsk->win, server.atom._NET_WM_NAME, server.atom.UTF8_STRING, 0);
150 if (!name || !strlen(name)) {
151 name = server_get_property (tsk->win, server.atom.WM_NAME, XA_STRING, 0);
152 if (!name || !strlen(name)) {
153 name = malloc(10);
154 strcpy(name, "Untitled");
155 }
156 }
157 }
158
159 // add space before title
160 title = malloc(strlen(name)+2);
161 if (panel->g_task.icon) strcpy(title, " ");
162 else title[0] = 0;
163 strcat(title, name);
164 if (name) XFree (name);
165
166 tsk->area.redraw = 1;
167 if (tsk->title)
168 free(tsk->title);
169 tsk->title = title;
170 }
171
172
173 void get_icon (Task *tsk)
174 {
175 Panel *panel = tsk->area.panel;
176 if (!panel->g_task.icon) return;
177
178 if (tsk->icon) {
179 imlib_context_set_image(tsk->icon);
180 imlib_free_image();
181 imlib_context_set_image(tsk->icon_active);
182 imlib_free_image();
183 tsk->icon = tsk->icon_active = NULL;
184 }
185 tsk->area.redraw = 1;
186
187 int i;
188 Imlib_Image img = NULL;
189 long *data = server_get_property (tsk->win, server.atom._NET_WM_ICON, XA_CARDINAL, &i);
190 if (data) {
191 // get ARGB icon
192 int w, h;
193 long *tmp_data;
194
195 tmp_data = get_best_icon (data, get_icon_count (data, i), i, &w, &h, panel->g_task.icon_size1);
196
197 #ifdef __x86_64__
198 DATA32 *icon_data = malloc (w * h * sizeof (DATA32));
199 int length = w * h;
200 for (i = 0; i < length; ++i)
201 icon_data[i] = tmp_data[i];
202 img = imlib_create_image_using_data (w, h, icon_data);
203 #else
204 img = imlib_create_image_using_data (w, h, (DATA32*)tmp_data);
205 #endif
206 imlib_context_set_image(img);
207 imlib_image_set_has_alpha(1);
208 XFree (data);
209 }
210 else {
211 // get Pixmap icon
212 XWMHints *hints = XGetWMHints(server.dsp, tsk->win);
213 if (hints) {
214 if (hints->flags & IconPixmapHint && hints->icon_pixmap != 0) {
215 // get width, height and depth for the pixmap
216 Window root;
217 int icon_x, icon_y;
218 uint border_width, bpp;
219 uint w, h;
220
221 //printf(" get pixmap\n");
222 XGetGeometry(server.dsp, hints->icon_pixmap, &root, &icon_x, &icon_y, &w, &h, &border_width, &bpp);
223 imlib_context_set_drawable(hints->icon_pixmap);
224 img = imlib_create_image_from_drawable(hints->icon_mask, 0, 0, w, h, 0);
225 }
226 }
227 XFree(hints);
228 }
229 if (img == NULL) {
230 imlib_context_set_image(default_icon);
231 img = imlib_clone_image();
232 }
233
234 // transform icons
235 imlib_context_set_image(img);
236 tsk->icon = imlib_create_cropped_scaled_image(0, 0, imlib_image_get_width(), imlib_image_get_height(), panel->g_task.icon_size1, panel->g_task.icon_size1);
237 imlib_free_image();
238
239 imlib_context_set_image(tsk->icon);
240 tsk->icon_width = imlib_image_get_width();
241 tsk->icon_height = imlib_image_get_height();
242 tsk->icon_active = imlib_clone_image();
243
244 DATA32 *data32;
245 if (panel->g_task.alpha != 100 || panel->g_task.saturation != 0 || panel->g_task.brightness != 0) {
246 data32 = imlib_image_get_data();
247 adjust_asb(data32, tsk->icon_width, tsk->icon_height, panel->g_task.alpha, (float)panel->g_task.saturation/100, (float)panel->g_task.brightness/100);
248 imlib_image_put_back_data(data32);
249 }
250
251 if (panel->g_task.alpha_active != 100 || panel->g_task.saturation_active != 0 || panel->g_task.brightness_active != 0) {
252 imlib_context_set_image(tsk->icon_active);
253 data32 = imlib_image_get_data();
254 adjust_asb(data32, tsk->icon_width, tsk->icon_height, panel->g_task.alpha_active, (float)panel->g_task.saturation_active/100, (float)panel->g_task.brightness_active/100);
255 imlib_image_put_back_data(data32);
256 }
257 }
258
259
260 void draw_task_icon (Task *tsk, int text_width, int active)
261 {
262 if (tsk->icon == NULL || tsk->icon_active == NULL) return;
263
264 // Find pos
265 int pos_x;
266 Panel *panel = (Panel*)tsk->area.panel;
267 if (panel->g_task.centered) {
268 if (panel->g_task.text)
269 pos_x = (tsk->area.width - text_width - panel->g_task.icon_size1) / 2;
270 else
271 pos_x = (tsk->area.width - panel->g_task.icon_size1) / 2;
272 }
273 else pos_x = panel->g_task.area.paddingxlr + panel->g_task.area.pix.border.width;
274
275 // Render
276 Pixmap *pmap;
277 if (active == 0) {
278 imlib_context_set_image (tsk->icon);
279 pmap = &tsk->area.pix.pmap;
280 }
281 else {
282 imlib_context_set_image (tsk->icon_active);
283 pmap = &tsk->area.pix_active.pmap;
284 }
285 imlib_context_set_drawable (*pmap);
286 imlib_render_image_on_drawable (pos_x, panel->g_task.icon_posy);
287 }
288
289
290 void draw_task (void *obj, cairo_t *c, int active)
291 {
292 Task *tsk = obj;
293 PangoLayout *layout;
294 config_color *config_text;
295 int width=0, height;
296 Panel *panel = (Panel*)tsk->area.panel;
297
298 if (panel->g_task.text) {
299 /* Layout */
300 layout = pango_cairo_create_layout (c);
301 pango_layout_set_font_description (layout, panel->g_task.font_desc);
302 pango_layout_set_text (layout, tsk->title, -1);
303
304 /* Drawing width and Cut text */
305 // pango use U+22EF or U+2026
306 pango_layout_set_width (layout, ((Taskbar*)tsk->area.parent)->text_width * PANGO_SCALE);
307 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
308
309 /* Center text */
310 if (panel->g_task.centered) pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
311 else pango_layout_set_alignment (layout, PANGO_ALIGN_LEFT);
312
313 pango_layout_get_pixel_size (layout, &width, &height);
314
315 if (active) config_text = &panel->g_task.font_active;
316 else config_text = &panel->g_task.font;
317
318 cairo_set_source_rgba (c, config_text->color[0], config_text->color[1], config_text->color[2], config_text->alpha);
319
320 pango_cairo_update_layout (c, layout);
321 cairo_move_to (c, panel->g_task.text_posx, panel->g_task.text_posy);
322 pango_cairo_show_layout (c, layout);
323
324 if (panel->g_task.font_shadow) {
325 cairo_set_source_rgba (c, 0.0, 0.0, 0.0, 0.5);
326 pango_cairo_update_layout (c, layout);
327 cairo_move_to (c, panel->g_task.text_posx + 1, panel->g_task.text_posy + 1);
328 pango_cairo_show_layout (c, layout);
329 }
330 g_object_unref (layout);
331 }
332
333 if (panel->g_task.icon) {
334 // icon use same opacity as text
335 draw_task_icon (tsk, width, active);
336 }
337 }
338
339
This page took 0.048916 seconds and 5 git commands to generate.