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