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