VirtualBox

source: vbox/trunk/src/VBox/RDP/client-1.8.4/ewmhints.c@ 78352

Last change on this file since 78352 was 55123, checked in by vboxsync, 10 years ago

rdesktop 1.8.3 modified for VBox

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.9 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 <[email protected]> for Cendio AB
8 Copyright 2007 Pierre Ossman <[email protected]> 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 unsigned long *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 = (unsigned long *) 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 /* In EWMH, HIDDEN overrides MAXIMIZED_VERT/MAXIMIZED_HORZ */
239 if (hidden)
240 return SEAMLESSRDP_MINIMIZED;
241 else if (maximized_vert && maximized_horz)
242 return SEAMLESSRDP_MAXIMIZED;
243 else
244 return SEAMLESSRDP_NORMAL;
245}
246
247static int
248ewmh_modify_state(Window wnd, int add, Atom atom1, Atom atom2)
249{
250 Status status;
251 XEvent xevent;
252
253 int result;
254 unsigned long nitems;
255 unsigned char *props;
256 uint32 state = WithdrawnState;
257
258 /* The spec states that the window manager must respect any
259 _NET_WM_STATE attributes on a withdrawn window. In order words, we
260 modify the attributes directly for withdrawn windows and ask the WM
261 to do it for active windows. */
262 result = get_property_value(wnd, "WM_STATE", 64, &nitems, &props, 1);
263 if ((result >= 0) && nitems)
264 {
265 state = *(uint32 *) props;
266 XFree(props);
267 }
268
269 if (state == WithdrawnState)
270 {
271 if (add)
272 {
273 Atom atoms[2];
274
275 atoms[0] = atom1;
276 nitems = 1;
277 if (atom2)
278 {
279 atoms[1] = atom2;
280 nitems = 2;
281 }
282
283 XChangeProperty(g_display, wnd, g_net_wm_state_atom, XA_ATOM,
284 32, PropModeAppend, (unsigned char *) atoms, nitems);
285 }
286 else
287 {
288 Atom *atoms;
289 int i;
290
291 if (get_property_value(wnd, "_NET_WM_STATE", 64, &nitems, &props, 1) < 0)
292 return 0;
293
294 atoms = (Atom *) props;
295
296 for (i = 0; i < nitems; i++)
297 {
298 if ((atoms[i] == atom1) || (atom2 && (atoms[i] == atom2)))
299 {
300 if (i != (nitems - 1))
301 memmove(&atoms[i], &atoms[i + 1],
302 sizeof(Atom) * (nitems - i - 1));
303 nitems--;
304 i--;
305 }
306 }
307
308 XChangeProperty(g_display, wnd, g_net_wm_state_atom, XA_ATOM,
309 32, PropModeReplace, (unsigned char *) atoms, nitems);
310
311 XFree(props);
312 }
313
314 return 0;
315 }
316
317 xevent.type = ClientMessage;
318 xevent.xclient.window = wnd;
319 xevent.xclient.message_type = g_net_wm_state_atom;
320 xevent.xclient.format = 32;
321 if (add)
322 xevent.xclient.data.l[0] = _NET_WM_STATE_ADD;
323 else
324 xevent.xclient.data.l[0] = _NET_WM_STATE_REMOVE;
325 xevent.xclient.data.l[1] = atom1;
326 xevent.xclient.data.l[2] = atom2;
327 xevent.xclient.data.l[3] = 0;
328 xevent.xclient.data.l[4] = 0;
329 status = XSendEvent(g_display, DefaultRootWindow(g_display), False,
330 SubstructureNotifyMask | SubstructureRedirectMask, &xevent);
331 if (!status)
332 return -1;
333
334 return 0;
335}
336
337/*
338 Set the window state: normal/minimized/maximized.
339 Returns -1 on failure.
340*/
341int
342ewmh_change_state(Window wnd, int state)
343{
344 /*
345 * Deal with the max atoms
346 */
347 if (state == SEAMLESSRDP_MAXIMIZED)
348 {
349 if (ewmh_modify_state
350 (wnd, 1, g_net_wm_state_maximized_vert_atom,
351 g_net_wm_state_maximized_horz_atom) < 0)
352 return -1;
353 }
354 else
355 {
356 if (ewmh_modify_state
357 (wnd, 0, g_net_wm_state_maximized_vert_atom,
358 g_net_wm_state_maximized_horz_atom) < 0)
359 return -1;
360 }
361
362 return 0;
363}
364
365
366int
367ewmh_get_window_desktop(Window wnd)
368{
369 unsigned long nitems_return;
370 unsigned char *prop_return;
371 int desktop;
372
373 if (get_property_value(wnd, "_NET_WM_DESKTOP", 1, &nitems_return, &prop_return, 0) < 0)
374 return (-1);
375
376 if (nitems_return != 1)
377 {
378 fprintf(stderr, "_NET_WM_DESKTOP has bad length\n");
379 return (-1);
380 }
381
382 desktop = *prop_return;
383 XFree(prop_return);
384 return desktop;
385}
386
387
388int
389ewmh_move_to_desktop(Window wnd, unsigned int desktop)
390{
391 Status status;
392 XEvent xevent;
393
394 xevent.type = ClientMessage;
395 xevent.xclient.window = wnd;
396 xevent.xclient.message_type = g_net_wm_desktop_atom;
397 xevent.xclient.format = 32;
398 xevent.xclient.data.l[0] = desktop;
399 xevent.xclient.data.l[1] = 0;
400 xevent.xclient.data.l[2] = 0;
401 xevent.xclient.data.l[3] = 0;
402 xevent.xclient.data.l[4] = 0;
403 status = XSendEvent(g_display, DefaultRootWindow(g_display), False,
404 SubstructureNotifyMask | SubstructureRedirectMask, &xevent);
405 if (!status)
406 return -1;
407
408 return 0;
409}
410
411void
412ewmh_set_wm_name(Window wnd, const char *title)
413{
414 int len;
415
416 len = strlen(title);
417 XChangeProperty(g_display, wnd, g_net_wm_name_atom, g_utf8_string_atom,
418 8, PropModeReplace, (unsigned char *) title, len);
419}
420
421
422int
423ewmh_set_window_popup(Window wnd)
424{
425 if (ewmh_modify_state
426 (wnd, 1, g_net_wm_state_skip_taskbar_atom, g_net_wm_state_skip_pager_atom) < 0)
427 return -1;
428 return 0;
429}
430
431int
432ewmh_set_window_modal(Window wnd)
433{
434 if (ewmh_modify_state(wnd, 1, g_net_wm_state_modal_atom, 0) < 0)
435 return -1;
436 return 0;
437}
438
439void
440ewmh_set_icon(Window wnd, int width, int height, const char *rgba_data)
441{
442 unsigned long nitems, i;
443 unsigned char *props;
444 unsigned long *cur_set, *new_set;
445 unsigned long *icon;
446
447 cur_set = NULL;
448 new_set = NULL;
449
450 if (get_property_value(wnd, "_NET_WM_ICON", 10000, &nitems, &props, 1) >= 0)
451 {
452 cur_set = (unsigned long *) props;
453
454 for (i = 0; i < nitems;)
455 {
456 if (cur_set[i] == width && cur_set[i + 1] == height)
457 break;
458
459 i += 2 + cur_set[i] * cur_set[i + 1];
460 }
461
462 if (i != nitems)
463 icon = cur_set + i;
464 else
465 {
466 new_set = xmalloc((nitems + width * height + 2) * sizeof(unsigned long));
467 memcpy(new_set, cur_set, nitems * sizeof(unsigned long));
468 icon = new_set + nitems;
469 nitems += width * height + 2;
470 }
471 }
472 else
473 {
474 new_set = xmalloc((width * height + 2) * sizeof(unsigned long));
475 icon = new_set;
476 nitems = width * height + 2;
477 }
478
479 icon[0] = width;
480 icon[1] = height;
481
482 /* Convert RGBA -> ARGB */
483 for (i = 0; i < width * height; i++)
484 {
485 icon[i + 2] =
486 rgba_data[i * 4 + 3] << 24 |
487 ((rgba_data[i * 4 + 0] << 16) & 0x00FF0000) |
488 ((rgba_data[i * 4 + 1] << 8) & 0x0000FF00) |
489 ((rgba_data[i * 4 + 2] << 0) & 0x000000FF);
490 }
491
492 XChangeProperty(g_display, wnd, g_net_wm_icon_atom, XA_CARDINAL, 32,
493 PropModeReplace, (unsigned char *) (new_set ? new_set : cur_set), nitems);
494
495 if (cur_set)
496 XFree(cur_set);
497 if (new_set)
498 xfree(new_set);
499}
500
501void
502ewmh_del_icon(Window wnd, int width, int height)
503{
504 unsigned long nitems, i, icon_size;
505 unsigned char *props;
506 unsigned long *cur_set, *new_set;
507
508 cur_set = NULL;
509 new_set = NULL;
510
511 if (get_property_value(wnd, "_NET_WM_ICON", 10000, &nitems, &props, 1) < 0)
512 return;
513
514 cur_set = (unsigned long *) props;
515
516 for (i = 0; i < nitems;)
517 {
518 if (cur_set[i] == width && cur_set[i + 1] == height)
519 break;
520
521 i += 2 + cur_set[i] * cur_set[i + 1];
522 }
523
524 if (i == nitems)
525 goto out;
526
527 icon_size = width * height + 2;
528 new_set = xmalloc((nitems - icon_size) * sizeof(unsigned long));
529
530 if (i != 0)
531 memcpy(new_set, cur_set, i * sizeof(unsigned long));
532 if (i != nitems - icon_size)
533 memcpy(new_set + i, cur_set + i + icon_size,
534 (nitems - (i + icon_size)) * sizeof(unsigned long));
535
536 nitems -= icon_size;
537
538 XChangeProperty(g_display, wnd, g_net_wm_icon_atom, XA_CARDINAL, 32,
539 PropModeReplace, (unsigned char *) new_set, nitems);
540
541 xfree(new_set);
542
543 out:
544 XFree(cur_set);
545}
546
547int
548ewmh_set_window_above(Window wnd)
549{
550 if (ewmh_modify_state(wnd, 1, g_net_wm_state_above_atom, 0) < 0)
551 return -1;
552 return 0;
553}
554
555RD_BOOL
556ewmh_is_window_above(Window w)
557{
558 unsigned long nitems_return;
559 unsigned char *prop_return;
560 unsigned long *return_words;
561 unsigned long item;
562 RD_BOOL above;
563
564 above = False;
565
566 if (get_property_value(w, "_NET_WM_STATE", 64, &nitems_return, &prop_return, 0) < 0)
567 return False;
568
569 return_words = (unsigned long *) prop_return;
570
571 for (item = 0; item < nitems_return; item++)
572 {
573 if (return_words[item] == g_net_wm_state_above_atom)
574 above = True;
575 }
576
577 XFree(prop_return);
578
579 return above;
580}
581
582#endif /* MAKE_PROTO */
583
584
585#if 0
586
587/* FIXME: _NET_MOVERESIZE_WINDOW is for pagers, not for
588 applications. We should implement _NET_WM_MOVERESIZE instead */
589
590int
591ewmh_net_moveresize_window(Window wnd, int x, int y, int width, int height)
592{
593 Status status;
594 XEvent xevent;
595 Atom moveresize;
596
597 moveresize = XInternAtom(g_display, "_NET_MOVERESIZE_WINDOW", False);
598 if (!moveresize)
599 {
600 return -1;
601 }
602
603 xevent.type = ClientMessage;
604 xevent.xclient.window = wnd;
605 xevent.xclient.message_type = moveresize;
606 xevent.xclient.format = 32;
607 xevent.xclient.data.l[0] = StaticGravity | (1 << 8) | (1 << 9) | (1 << 10) | (1 << 11);
608 xevent.xclient.data.l[1] = x;
609 xevent.xclient.data.l[2] = y;
610 xevent.xclient.data.l[3] = width;
611 xevent.xclient.data.l[4] = height;
612
613 status = XSendEvent(g_display, DefaultRootWindow(g_display), False,
614 SubstructureNotifyMask | SubstructureRedirectMask, &xevent);
615 if (!status)
616 return -1;
617 return 0;
618}
619
620#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