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