]> Dogcows Code - chaz/tint2/blob - src/util/window.c
caa293438e8cab0ab11a704048beaad5745b8dab
[chaz/tint2] / src / util / window.c
1 /**************************************************************************
2 *
3 * Tint2 : common windows function
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
28 #include <Imlib2.h>
29 #include <cairo.h>
30 #include <cairo-xlib.h>
31
32 #include "common.h"
33 #include "window.h"
34 #include "server.h"
35 #include "panel.h"
36 #include "taskbar.h"
37
38
39
40 void set_active (Window win)
41 {
42 send_event32 (win, server.atom._NET_ACTIVE_WINDOW, 2, CurrentTime, 0);
43 }
44
45
46 void set_desktop (int desktop)
47 {
48 send_event32 (server.root_win, server.atom._NET_CURRENT_DESKTOP, desktop, 0, 0);
49 }
50
51
52 void windows_set_desktop (Window win, int desktop)
53 {
54 send_event32 (win, server.atom._NET_WM_DESKTOP, desktop, 2, 0);
55 }
56
57
58 void set_close (Window win)
59 {
60 send_event32 (win, server.atom._NET_CLOSE_WINDOW, 0, 2, 0);
61 }
62
63
64 void window_toggle_shade (Window win)
65 {
66 send_event32 (win, server.atom._NET_WM_STATE, 2, server.atom._NET_WM_STATE_SHADED, 0);
67 }
68
69
70 void window_maximize_restore (Window win)
71 {
72 send_event32 (win, server.atom._NET_WM_STATE, 2, server.atom._NET_WM_STATE_MAXIMIZED_VERT, 0);
73 send_event32 (win, server.atom._NET_WM_STATE, 2, server.atom._NET_WM_STATE_MAXIMIZED_HORZ, 0);
74 }
75
76
77 int window_is_hidden (Window win)
78 {
79 Window window;
80 Atom *at;
81 int count, i;
82
83 at = server_get_property (win, server.atom._NET_WM_STATE, XA_ATOM, &count);
84 for (i = 0; i < count; i++) {
85 if (at[i] == server.atom._NET_WM_STATE_SKIP_TASKBAR) {
86 XFree(at);
87 return 1;
88 }
89 if (at[i] == server.atom._NET_WM_STATE_MODAL) {
90 // do not add modal windows if the transient window is already in the taskbar
91 if ( XGetTransientForHint(server.dsp, win, &window) && task_get_task(window) ) {
92 XFree(at);
93 return 1;
94 }
95 }
96 }
97 XFree(at);
98
99 at = server_get_property (win, server.atom._NET_WM_WINDOW_TYPE, XA_ATOM, &count);
100 for (i = 0; i < count; i++) {
101 if (at[i] == server.atom._NET_WM_WINDOW_TYPE_DOCK || at[i] == server.atom._NET_WM_WINDOW_TYPE_DESKTOP || at[i] == server.atom._NET_WM_WINDOW_TYPE_TOOLBAR || at[i] == server.atom._NET_WM_WINDOW_TYPE_MENU || at[i] == server.atom._NET_WM_WINDOW_TYPE_SPLASH) {
102 XFree(at);
103 return 1;
104 }
105 }
106 XFree(at);
107
108 for (i=0 ; i < nb_panel ; i++) {
109 if (panel1[i].main_win == win) {
110 return 1;
111 }
112 }
113
114 // specification
115 // Windows with neither _NET_WM_WINDOW_TYPE nor WM_TRANSIENT_FOR set
116 // MUST be taken as top-level window.
117 return 0;
118 }
119
120
121 int window_get_desktop (Window win)
122 {
123 return get_property32(win, server.atom._NET_WM_DESKTOP, XA_CARDINAL);
124 }
125
126
127 int window_get_monitor (Window win)
128 {
129 int i, x, y;
130 Window src;
131
132 XTranslateCoordinates(server.dsp, win, server.root_win, 0, 0, &x, &y, &src);
133 x += 2;
134 y += 2;
135 for (i = 0; i < server.nb_monitor; i++) {
136 if (x >= server.monitor[i].x && x <= (server.monitor[i].x + server.monitor[i].width))
137 if (y >= server.monitor[i].y && y <= (server.monitor[i].y + server.monitor[i].height))
138 break;
139 }
140
141 //printf("window %lx : ecran %d, (%d, %d)\n", win, i, x, y);
142 if (i == server.nb_monitor) return 0;
143 else return i;
144 }
145
146
147 int window_is_iconified (Window win)
148 {
149 if (IconicState == get_property32(win, server.atom.WM_STATE, server.atom.WM_STATE)) {
150 // openbox sets on shaded windows the IconicState, however we do not consider these windows iconified
151 Atom *at;
152 int count, i;
153 at = server_get_property (win, server.atom._NET_WM_STATE, XA_ATOM, &count);
154 for (i = 0; i < count; i++) {
155 if (at[i] == server.atom._NET_WM_STATE_SHADED) {
156 XFree(at);
157 return 0;
158 }
159 }
160 XFree(at);
161 return 1;
162 }
163 return 0;
164 }
165
166
167 int window_is_urgent (Window win)
168 {
169 Atom *at;
170 int count, i;
171
172 at = server_get_property (win, server.atom._NET_WM_STATE, XA_ATOM, &count);
173 for (i = 0; i < count; i++) {
174 if (at[i] == server.atom._NET_WM_STATE_DEMANDS_ATTENTION) {
175 XFree(at);
176 return 1;
177 }
178 }
179 XFree(at);
180 return 0;
181 }
182
183
184 int window_is_skip_taskbar (Window win)
185 {
186 Atom *at;
187 int count, i;
188
189 at = server_get_property(win, server.atom._NET_WM_STATE, XA_ATOM, &count);
190 for (i=0; i<count; i++) {
191 if (at[i] == server.atom._NET_WM_STATE_SKIP_TASKBAR) {
192 XFree(at);
193 return 1;
194 }
195 }
196 XFree(at);
197 return 0;
198 }
199
200
201 int server_get_number_of_desktop ()
202 {
203 return get_property32(server.root_win, server.atom._NET_NUMBER_OF_DESKTOPS, XA_CARDINAL);
204 }
205
206
207 int server_get_current_desktop ()
208 {
209 return get_property32(server.root_win, server.atom._NET_CURRENT_DESKTOP, XA_CARDINAL);
210 }
211
212
213 Window window_get_active ()
214 {
215 return get_property32(server.root_win, server.atom._NET_ACTIVE_WINDOW, XA_WINDOW);
216 }
217
218
219 int window_is_active (Window win)
220 {
221 return (win == get_property32(server.root_win, server.atom._NET_ACTIVE_WINDOW, XA_WINDOW));
222 }
223
224
225 int get_icon_count (long *data, int num)
226 {
227 int count, pos, w, h;
228
229 count = 0;
230 pos = 0;
231 while (pos < num) {
232 w = data[pos++];
233 h = data[pos++];
234 pos += w * h;
235 if (pos > num || w * h == 0) break;
236 count++;
237 }
238
239 return count;
240 }
241
242
243 long *get_best_icon (long *data, int icon_count, int num, int *iw, int *ih, int best_icon_size)
244 {
245 int width[icon_count], height[icon_count], pos, i, w, h;
246 long *icon_data[icon_count];
247
248 /* List up icons */
249 pos = 0;
250 i = icon_count;
251 while (i--) {
252 w = data[pos++];
253 h = data[pos++];
254 if (pos + w * h > num) break;
255
256 width[i] = w;
257 height[i] = h;
258 icon_data[i] = &data[pos];
259
260 pos += w * h;
261 }
262
263 /* Try to find exact size */
264 int icon_num = -1;
265 for (i = 0; i < icon_count; i++) {
266 if (width[i] == best_icon_size) {
267 icon_num = i;
268 break;
269 }
270 }
271
272 /* Take the biggest or whatever */
273 if (icon_num < 0) {
274 int highest = 0;
275 for (i = 0; i < icon_count; i++) {
276 if (width[i] > highest) {
277 icon_num = i;
278 highest = width[i];
279 }
280 }
281 }
282
283 *iw = width[icon_num];
284 *ih = height[icon_num];
285 return icon_data[icon_num];
286 }
287
288
289 void get_text_size(PangoFontDescription *font, int *height_ink, int *height, int panel_height, char *text, int len)
290 {
291 PangoRectangle rect_ink, rect;
292
293 Pixmap pmap = XCreatePixmap (server.dsp, server.root_win, panel_height, panel_height, server.depth);
294
295 cairo_surface_t *cs = cairo_xlib_surface_create (server.dsp, pmap, server.visual, panel_height, panel_height);
296 cairo_t *c = cairo_create (cs);
297
298 PangoLayout *layout = pango_cairo_create_layout (c);
299 pango_layout_set_font_description (layout, font);
300 pango_layout_set_text (layout, text, len);
301
302 pango_layout_get_pixel_extents(layout, &rect_ink, &rect);
303 *height_ink = rect_ink.height;
304 *height = rect.height;
305 //printf("dimension : %d - %d\n", rect_ink.height, rect.height);
306
307 g_object_unref (layout);
308 cairo_destroy (c);
309 cairo_surface_destroy (cs);
310 XFreePixmap (server.dsp, pmap);
311 }
312
313
This page took 0.048057 seconds and 3 git commands to generate.