VirtualBox

source: vbox/trunk/src/VBox/RDP/client/ewmhints.c@ 50926

Last change on this file since 50926 was 37224, checked in by vboxsync, 14 years ago

RDP/client: fix OSE

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.3 KB
Line 
1/* -*- c-basic-offset: 8 -*-
2 rdesktop: A Remote Desktop Protocol client.
3
4 Support functions for Extended Window Manager Hints,
5 http://www.freedesktop.org/wiki/Standards_2fwm_2dspec
6
7 Copyright 2005-2011 Peter Astrand <astrand@cendio.se> for Cendio AB
8 Copyright 2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
9
10 This program is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24/*
25 * Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
26 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
27 * the General Public License version 2 (GPLv2) at this time for any software where
28 * a choice of GPL license versions is made available with the language indicating
29 * that GPLv2 or any later version may be used, or where a choice of which version
30 * of the GPL is applied is otherwise unspecified.
31 */
32
33#include <X11/Xlib.h>
34#include <X11/Xatom.h>
35#include <X11/Xutil.h>
36#include "rdesktop.h"
37
38#define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
39#define _NET_WM_STATE_ADD 1 /* add/set property */
40#define _NET_WM_STATE_TOGGLE 2 /* toggle property */
41
42extern Display *g_display;
43
44static Atom g_net_wm_state_maximized_vert_atom, g_net_wm_state_maximized_horz_atom,
45 g_net_wm_state_hidden_atom, g_net_wm_name_atom, g_utf8_string_atom,
46 g_net_wm_state_skip_taskbar_atom, g_net_wm_state_skip_pager_atom,
47 g_net_wm_state_modal_atom, g_net_wm_icon_atom, g_net_wm_state_above_atom;
48
49Atom g_net_wm_state_atom, g_net_wm_desktop_atom;
50
51/*
52 Get window property value (32 bit format)
53 Returns zero on success, -1 on error
54*/
55static int
56get_property_value(Window wnd, char *propname, long max_length,
57 unsigned long *nitems_return, unsigned char **prop_return, int nowarn)
58{
59 int result;
60 Atom property;
61 Atom actual_type_return;
62 int actual_format_return;
63 unsigned long bytes_after_return;
64
65 property = XInternAtom(g_display, propname, True);
66 if (property == None)
67 {
68 fprintf(stderr, "Atom %s does not exist\n", propname);
69 return (-1);
70 }
71
72 result = XGetWindowProperty(g_display, wnd, property, 0, /* long_offset */
73 max_length, /* long_length */
74 False, /* delete */
75 AnyPropertyType, /* req_type */
76 &actual_type_return,
77 &actual_format_return,
78 nitems_return, &bytes_after_return, prop_return);
79
80 if (result != Success)
81 {
82 fprintf(stderr, "XGetWindowProperty failed\n");
83 return (-1);
84 }
85
86 if (actual_type_return == None || actual_format_return == 0)
87 {
88 if (!nowarn)
89 fprintf(stderr, "Window is missing property %s\n", propname);
90 return (-1);
91 }
92
93 if (bytes_after_return)
94 {
95 fprintf(stderr, "%s is too big for me\n", propname);
96 return (-1);
97 }
98
99 if (actual_format_return != 32)
100 {
101 fprintf(stderr, "%s has bad format\n", propname);
102 return (-1);
103 }
104
105 return (0);
106}
107
108/*
109 Get current desktop number
110 Returns -1 on error
111*/
112static int
113get_current_desktop(void)
114{
115 unsigned long nitems_return;
116 unsigned char *prop_return;
117 int current_desktop;
118
119 if (get_property_value
120 (DefaultRootWindow(g_display), "_NET_CURRENT_DESKTOP", 1, &nitems_return,
121 &prop_return, 0) < 0)
122 return (-1);
123
124 if (nitems_return != 1)
125 {
126 fprintf(stderr, "_NET_CURRENT_DESKTOP has bad length\n");
127 return (-1);
128 }
129
130 current_desktop = *prop_return;
131 XFree(prop_return);
132 return current_desktop;
133}
134
135/*
136 Get workarea geometry
137 Returns zero on success, -1 on error
138 */
139
140int
141get_current_workarea(uint32 * x, uint32 * y, uint32 * width, uint32 * height)
142{
143 int current_desktop;
144 unsigned long nitems_return;
145 unsigned char *prop_return;
146 long *return_words;
147 const uint32 net_workarea_x_offset = 0;
148 const uint32 net_workarea_y_offset = 1;
149 const uint32 net_workarea_width_offset = 2;
150 const uint32 net_workarea_height_offset = 3;
151 const uint32 max_prop_length = 32 * 4; /* Max 32 desktops */
152
153 if (get_property_value
154 (DefaultRootWindow(g_display), "_NET_WORKAREA", max_prop_length, &nitems_return,
155 &prop_return, 0) < 0)
156 return (-1);
157
158 if (nitems_return % 4)
159 {
160 fprintf(stderr, "_NET_WORKAREA has odd length\n");
161 return (-1);
162 }
163
164 current_desktop = get_current_desktop();
165
166 if (current_desktop < 0)
167 return -1;
168
169 return_words = (long *) prop_return;
170
171 *x = return_words[current_desktop * 4 + net_workarea_x_offset];
172 *y = return_words[current_desktop * 4 + net_workarea_y_offset];
173 *width = return_words[current_desktop * 4 + net_workarea_width_offset];
174 *height = return_words[current_desktop * 4 + net_workarea_height_offset];
175
176 XFree(prop_return);
177
178 return (0);
179
180}
181
182
183
184void
185ewmh_init()
186{
187 /* FIXME: Use XInternAtoms */
188 g_net_wm_state_maximized_vert_atom =
189 XInternAtom(g_display, "_NET_WM_STATE_MAXIMIZED_VERT", False);
190 g_net_wm_state_maximized_horz_atom =
191 XInternAtom(g_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False);
192 g_net_wm_state_hidden_atom = XInternAtom(g_display, "_NET_WM_STATE_HIDDEN", False);
193 g_net_wm_state_skip_taskbar_atom =
194 XInternAtom(g_display, "_NET_WM_STATE_SKIP_TASKBAR", False);
195 g_net_wm_state_skip_pager_atom = XInternAtom(g_display, "_NET_WM_STATE_SKIP_PAGER", False);
196 g_net_wm_state_modal_atom = XInternAtom(g_display, "_NET_WM_STATE_MODAL", False);
197 g_net_wm_state_above_atom = XInternAtom(g_display, "_NET_WM_STATE_ABOVE", False);
198 g_net_wm_state_atom = XInternAtom(g_display, "_NET_WM_STATE", False);
199 g_net_wm_desktop_atom = XInternAtom(g_display, "_NET_WM_DESKTOP", False);
200 g_net_wm_name_atom = XInternAtom(g_display, "_NET_WM_NAME", False);
201 g_net_wm_icon_atom = XInternAtom(g_display, "_NET_WM_ICON", False);
202 g_utf8_string_atom = XInternAtom(g_display, "UTF8_STRING", False);
203}
204
205
206/*
207 Get the window state: normal/minimized/maximized.
208*/
209#ifndef MAKE_PROTO
210int
211ewmh_get_window_state(Window w)
212{
213 unsigned long nitems_return;
214 unsigned char *prop_return;
215 uint32 *return_words;
216 unsigned long item;
217 RD_BOOL maximized_vert, maximized_horz, hidden;
218
219 maximized_vert = maximized_horz = hidden = False;
220
221 if (get_property_value(w, "_NET_WM_STATE", 64, &nitems_return, &prop_return, 0) < 0)
222 return SEAMLESSRDP_NORMAL;
223
224 return_words = (uint32 *) prop_return;
225
226 for (item = 0; item < nitems_return; item++)
227 {
228 if (return_words[item] == g_net_wm_state_maximized_vert_atom)
229 maximized_vert = True;
230 if (return_words[item] == g_net_wm_state_maximized_horz_atom)
231 maximized_horz = True;
232 if (return_words[item] == g_net_wm_state_hidden_atom)
233 hidden = True;
234 }
235
236 XFree(prop_return);
237
238 if (maximized_vert && maximized_horz)
239 return SEAMLESSRDP_MAXIMIZED;
240 else if (hidden)
241 return SEAMLESSRDP_MINIMIZED;
242 else
243 return SEAMLESSRDP_NORMAL;
244}
245
246static int
247ewmh_modify_state(Window wnd, int add, Atom atom1, Atom atom2)
248{
249 Status status;
250 XEvent xevent;
251
252 int result;
253 unsigned long nitems;
254 unsigned char *props;
255 uint32 state = WithdrawnState;
256
257 /* The spec states that the window manager must respect any
258 _NET_WM_STATE attributes on a withdrawn window. In order words, we
259 modify the attributes directly for withdrawn windows and ask the WM
260 to do it for active windows. */
261 result = get_property_value(wnd, "WM_STATE", 64, &nitems, &props, 1);
262 if ((result >= 0) && nitems)
263 {
264 state = *(uint32 *) props;
265 XFree(props);
266 }
267
268 if (state == WithdrawnState)
269 {
270 if (add)
271 {
272 Atom atoms[2];
273
274 atoms[0] = atom1;
275 nitems = 1;
276 if (atom2)
277 {
278 atoms[1] = atom2;
279 nitems = 2;
280 }
281
282 XChangeProperty(g_display, wnd, g_net_wm_state_atom, XA_ATOM,
283 32, PropModeAppend, (unsigned char *) atoms, nitems);
284 }
285 else
286 {
287 Atom *atoms;
288 int i;
289
290 if (get_property_value(wnd, "_NET_WM_STATE", 64, &nitems, &props, 1) < 0)
291 return 0;
292
293 atoms = (Atom *) props;
294
295 for (i = 0; i < nitems; i++)
296 {
297 if ((atoms[i] == atom1) || (atom2 && (atoms[i] == atom2)))
298 {
299 if (i != (nitems - 1))
300 memmove(&atoms[i], &atoms[i + 1],
301 sizeof(Atom) * (nitems - i - 1));
302 nitems--;
303 i--;
304 }
305 }
306
307 XChangeProperty(g_display, wnd, g_net_wm_state_atom, XA_ATOM,
308 32, PropModeReplace, (unsigned char *) atoms, nitems);
309
310 XFree(props);
311 }
312
313 return 0;
314 }
315
316 xevent.type = ClientMessage;
317 xevent.xclient.window = wnd;
318 xevent.xclient.message_type = g_net_wm_state_atom;
319 xevent.xclient.format = 32;
320 if (add)
321 xevent.xclient.data.l[0] = _NET_WM_STATE_ADD;
322 else
323 xevent.xclient.data.l[0] = _NET_WM_STATE_REMOVE;
324 xevent.xclient.data.l[1] = atom1;
325 xevent.xclient.data.l[2] = atom2;
326 xevent.xclient.data.l[3] = 0;
327 xevent.xclient.data.l[4] = 0;
328 status = XSendEvent(g_display, DefaultRootWindow(g_display), False,
329 SubstructureNotifyMask | SubstructureRedirectMask, &xevent);
330 if (!status)
331 return -1;
332
333 return 0;
334}
335
336/*
337 Set the window state: normal/minimized/maximized.
338 Returns -1 on failure.
339*/
340int
341ewmh_change_state(Window wnd, int state)
342{
343 /*
344 * Deal with the max atoms
345 */
346 if (state == SEAMLESSRDP_MAXIMIZED)
347 {
348 if (ewmh_modify_state
349 (wnd, 1, g_net_wm_state_maximized_vert_atom,
350 g_net_wm_state_maximized_horz_atom) < 0)
351 return -1;
352 }
353 else
354 {
355 if (ewmh_modify_state
356 (wnd, 0, g_net_wm_state_maximized_vert_atom,
357 g_net_wm_state_maximized_horz_atom) < 0)
358 return -1;
359 }
360
361 return 0;
362}
363
364
365int
366ewmh_get_window_desktop(Window wnd)
367{
368 unsigned long nitems_return;
369 unsigned char *prop_return;
370 int desktop;
371
372 if (get_property_value(wnd, "_NET_WM_DESKTOP", 1, &nitems_return, &prop_return, 0) < 0)
373 return (-1);
374
375 if (nitems_return != 1)
376 {
377 fprintf(stderr, "_NET_WM_DESKTOP has bad length\n");
378 return (-1);
379 }
380
381 desktop = *prop_return;
382 XFree(prop_return);
383 return desktop;
384}
385
386
387int
388ewmh_move_to_desktop(Window wnd, unsigned int desktop)
389{
390 Status status;
391 XEvent xevent;
392
393 xevent.type = ClientMessage;
394 xevent.xclient.window = wnd;
395 xevent.xclient.message_type = g_net_wm_desktop_atom;
396 xevent.xclient.format = 32;
397 xevent.xclient.data.l[0] = desktop;
398 xevent.xclient.data.l[1] = 0;
399 xevent.xclient.data.l[2] = 0;
400 xevent.xclient.data.l[3] = 0;
401 xevent.xclient.data.l[4] = 0;
402 status = XSendEvent(g_display, DefaultRootWindow(g_display), False,
403 SubstructureNotifyMask | SubstructureRedirectMask, &xevent);
404 if (!status)
405 return -1;
406
407 return 0;
408}
409
410void
411ewmh_set_wm_name(Window wnd, const char *title)
412{
413 int len;
414
415 len = strlen(title);
416 XChangeProperty(g_display, wnd, g_net_wm_name_atom, g_utf8_string_atom,
417 8, PropModeReplace, (unsigned char *) title, len);
418}
419
420
421int
422ewmh_set_window_popup(Window wnd)
423{
424 if (ewmh_modify_state
425 (wnd, 1, g_net_wm_state_skip_taskbar_atom, g_net_wm_state_skip_pager_atom) < 0)
426 return -1;
427 return 0;
428}
429
430int
431ewmh_set_window_modal(Window wnd)
432{
433 if (ewmh_modify_state(wnd, 1, g_net_wm_state_modal_atom, 0) < 0)
434 return -1;
435 return 0;
436}
437
438void
439ewmh_set_icon(Window wnd, int width, int height, const char *rgba_data)
440{
441 unsigned long nitems, i;
442 unsigned char *props;
443 unsigned long *cur_set, *new_set;
444 unsigned long *icon;
445
446 cur_set = NULL;
447 new_set = NULL;
448
449 if (get_property_value(wnd, "_NET_WM_ICON", 10000, &nitems, &props, 1) >= 0)
450 {
451 cur_set = (unsigned long *) props;
452
453 for (i = 0; i < nitems;)
454 {
455 if (cur_set[i] == width && cur_set[i + 1] == height)
456 break;
457
458 i += 2 + cur_set[i] * cur_set[i + 1];
459 }
460
461 if (i != nitems)
462 icon = cur_set + i;
463 else
464 {
465 new_set = xmalloc((nitems + width * height + 2) * sizeof(unsigned long));
466 memcpy(new_set, cur_set, nitems * sizeof(unsigned long));
467 icon = new_set + nitems;
468 nitems += width * height + 2;
469 }
470 }
471 else
472 {
473 new_set = xmalloc((width * height + 2) * sizeof(unsigned long));
474 icon = new_set;
475 nitems = width * height + 2;
476 }
477
478 icon[0] = width;
479 icon[1] = height;
480
481 /* Convert RGBA -> ARGB */
482 for (i = 0; i < width * height; i++)
483 {
484 icon[i + 2] =
485 rgba_data[i * 4 + 3] << 24 |
486 ((rgba_data[i * 4 + 0] << 16) & 0x00FF0000) |
487 ((rgba_data[i * 4 + 1] << 8) & 0x0000FF00) |
488 ((rgba_data[i * 4 + 2] << 0) & 0x000000FF);
489 }
490
491 XChangeProperty(g_display, wnd, g_net_wm_icon_atom, XA_CARDINAL, 32,
492 PropModeReplace, (unsigned char *) (new_set ? new_set : cur_set), nitems);
493
494 if (cur_set)
495 XFree(cur_set);
496 if (new_set)
497 xfree(new_set);
498}
499
500void
501ewmh_del_icon(Window wnd, int width, int height)
502{
503 unsigned long nitems, i, icon_size;
504 unsigned char *props;
505 unsigned long *cur_set, *new_set;
506
507 cur_set = NULL;
508 new_set = NULL;
509
510 if (get_property_value(wnd, "_NET_WM_ICON", 10000, &nitems, &props, 1) < 0)
511 return;
512
513 cur_set = (unsigned long *) props;
514
515 for (i = 0; i < nitems;)
516 {
517 if (cur_set[i] == width && cur_set[i + 1] == height)
518 break;
519
520 i += 2 + cur_set[i] * cur_set[i + 1];
521 }
522
523 if (i == nitems)
524 goto out;
525
526 icon_size = width * height + 2;
527 new_set = xmalloc((nitems - icon_size) * sizeof(unsigned long));
528
529 if (i != 0)
530 memcpy(new_set, cur_set, i * sizeof(unsigned long));
531 if (i != nitems - icon_size)
532 memcpy(new_set + i, cur_set + i + icon_size,
533 (nitems - (i + icon_size)) * sizeof(unsigned long));
534
535 nitems -= icon_size;
536
537 XChangeProperty(g_display, wnd, g_net_wm_icon_atom, XA_CARDINAL, 32,
538 PropModeReplace, (unsigned char *) new_set, nitems);
539
540 xfree(new_set);
541
542 out:
543 XFree(cur_set);
544}
545
546int
547ewmh_set_window_above(Window wnd)
548{
549 if (ewmh_modify_state(wnd, 1, g_net_wm_state_above_atom, 0) < 0)
550 return -1;
551 return 0;
552}
553
554#endif /* MAKE_PROTO */
555
556
557#if 0
558
559/* FIXME: _NET_MOVERESIZE_WINDOW is for pagers, not for
560 applications. We should implement _NET_WM_MOVERESIZE instead */
561
562int
563ewmh_net_moveresize_window(Window wnd, int x, int y, int width, int height)
564{
565 Status status;
566 XEvent xevent;
567 Atom moveresize;
568
569 moveresize = XInternAtom(g_display, "_NET_MOVERESIZE_WINDOW", False);
570 if (!moveresize)
571 {
572 return -1;
573 }
574
575 xevent.type = ClientMessage;
576 xevent.xclient.window = wnd;
577 xevent.xclient.message_type = moveresize;
578 xevent.xclient.format = 32;
579 xevent.xclient.data.l[0] = StaticGravity | (1 << 8) | (1 << 9) | (1 << 10) | (1 << 11);
580 xevent.xclient.data.l[1] = x;
581 xevent.xclient.data.l[2] = y;
582 xevent.xclient.data.l[3] = width;
583 xevent.xclient.data.l[4] = height;
584
585 status = XSendEvent(g_display, DefaultRootWindow(g_display), False,
586 SubstructureNotifyMask | SubstructureRedirectMask, &xevent);
587 if (!status)
588 return -1;
589 return 0;
590}
591
592#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette