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