fixed some compilation issues with gcc-2.9x
[wmaker-crm.git] / src / wmspec.c
blob777f1cb03f3f61c09cf648c96f6abab865d6cb75
1 /* wmspec.c-- support for the wm-spec Hints
3 * Window Maker window manager
5 * Copyright (c) 1998-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
24 #include "wconfig.h"
26 #ifdef NETWM_HINTS
28 #include <X11/Xlib.h>
29 #include <X11/Xatom.h>
30 #include <string.h>
32 #include "WindowMaker.h"
33 #include "window.h"
34 #include "screen.h"
35 #include "workspace.h"
36 #include "framewin.h"
37 #include "actions.h"
38 #include "client.h"
39 #include "wmspec.h"
40 #include "icon.h"
41 #include "stacking.h"
42 #include "xinerama.h"
43 #include "properties.h"
46 #ifdef DEBUG_WMSPEC
47 #include <stdio.h>
48 #endif
50 /* Global variables */
51 extern Atom _XA_WM_DELETE_WINDOW;
52 extern Time LastTimestamp;
53 extern WPreferences wPreferences;
55 /* Root Window Properties */
56 static Atom net_supported;
57 static Atom net_client_list;
58 static Atom net_client_list_stacking;
59 static Atom net_number_of_desktops;
60 static Atom net_desktop_geometry;
61 static Atom net_desktop_viewport;
62 static Atom net_current_desktop;
63 static Atom net_desktop_names;
64 static Atom net_active_window;
65 static Atom net_workarea; /* XXX: not xinerama compatible */
66 static Atom net_supporting_wm_check;
67 static Atom net_virtual_roots; /* N/A */
68 static Atom net_desktop_layout; /* XXX */
69 static Atom net_showing_desktop;
71 /* Other Root Window Messages */
72 static Atom net_close_window;
73 static Atom net_moveresize_window; /* TODO */
74 static Atom net_wm_moveresize; /* TODO */
76 /* Application Window Properties */
77 static Atom net_wm_name;
78 static Atom net_wm_visible_name; /* TODO (unnecessary?) */
79 static Atom net_wm_icon_name;
80 static Atom net_wm_visible_icon_name; /* TODO (unnecessary?) */
81 static Atom net_wm_desktop;
82 static Atom net_wm_window_type;
83 static Atom net_wm_window_type_desktop;
84 static Atom net_wm_window_type_dock;
85 static Atom net_wm_window_type_toolbar;
86 static Atom net_wm_window_type_menu;
87 static Atom net_wm_window_type_utility;
88 static Atom net_wm_window_type_splash;
89 static Atom net_wm_window_type_dialog;
90 static Atom net_wm_window_type_normal;
91 static Atom net_wm_state;
92 static Atom net_wm_state_modal; /* XXX: what is this?!? */
93 static Atom net_wm_state_sticky;
94 static Atom net_wm_state_maximized_vert;
95 static Atom net_wm_state_maximized_horz;
96 static Atom net_wm_state_shaded;
97 static Atom net_wm_state_skip_taskbar;
98 static Atom net_wm_state_skip_pager;
99 static Atom net_wm_state_hidden;
100 static Atom net_wm_state_fullscreen;
101 static Atom net_wm_state_above;
102 static Atom net_wm_state_below;
103 static Atom net_wm_allowed_actions;
104 static Atom net_wm_action_move;
105 static Atom net_wm_action_resize;
106 static Atom net_wm_action_minimize;
107 static Atom net_wm_action_shade;
108 static Atom net_wm_action_stick;
109 static Atom net_wm_action_maximize_horz;
110 static Atom net_wm_action_maximize_vert;
111 static Atom net_wm_action_fullscreen;
112 static Atom net_wm_action_change_desktop;
113 static Atom net_wm_action_close;
114 static Atom net_wm_strut; /* XXX: see net_workarea */
115 static Atom net_wm_strut_partial; /* TODO: doesn't really fit into the current strut scheme */
116 static Atom net_wm_icon_geometry; /* FIXME: should work together with net_wm_handled_icons, gnome-panel-2.2.0.1 doesn't use _NET_WM_HANDLED_ICONS, thus present situation. */
117 static Atom net_wm_icon;
118 static Atom net_wm_pid; /* TODO */
119 static Atom net_wm_handled_icons; /* FIXME: see net_wm_icon_geometry */
121 /* Window Manager Protocols */
122 static Atom net_wm_ping; /* TODO */
124 static Atom utf8_string;
126 typedef struct {
127 char * name;
128 Atom * atom;
129 } atomitem_t;
131 static atomitem_t atomNames[] = {
132 { "_NET_SUPPORTED", &net_supported },
133 { "_NET_CLIENT_LIST", &net_client_list },
134 { "_NET_CLIENT_LIST_STACKING", &net_client_list_stacking },
135 { "_NET_NUMBER_OF_DESKTOPS", &net_number_of_desktops },
136 { "_NET_DESKTOP_GEOMETRY", &net_desktop_geometry },
137 { "_NET_DESKTOP_VIEWPORT", &net_desktop_viewport },
138 { "_NET_CURRENT_DESKTOP", &net_current_desktop },
139 { "_NET_DESKTOP_NAMES", &net_desktop_names },
140 { "_NET_ACTIVE_WINDOW", &net_active_window },
141 { "_NET_WORKAREA", &net_workarea },
142 { "_NET_SUPPORTING_WM_CHECK", &net_supporting_wm_check },
143 { "_NET_VIRTUAL_ROOTS", &net_virtual_roots },
144 { "_NET_DESKTOP_LAYOUT", &net_desktop_layout },
145 { "_NET_SHOWING_DESKTOP", &net_showing_desktop },
147 { "_NET_CLOSE_WINDOW", &net_close_window },
148 { "_NET_MOVERESIZE_WINDOW", &net_moveresize_window },
149 { "_NET_WM_MOVERESIZE", &net_wm_moveresize },
151 { "_NET_WM_NAME", &net_wm_name },
152 { "_NET_WM_VISIBLE_NAME", &net_wm_visible_name },
153 { "_NET_WM_ICON_NAME", &net_wm_icon_name },
154 { "_NET_WM_VISIBLE_ICON_NAME", &net_wm_visible_icon_name },
155 { "_NET_WM_DESKTOP", &net_wm_desktop },
156 { "_NET_WM_WINDOW_TYPE", &net_wm_window_type },
157 { "_NET_WM_WINDOW_TYPE_DESKTOP", &net_wm_window_type_desktop },
158 { "_NET_WM_WINDOW_TYPE_DOCK", &net_wm_window_type_dock },
159 { "_NET_WM_WINDOW_TYPE_TOOLBAR", &net_wm_window_type_toolbar },
160 { "_NET_WM_WINDOW_TYPE_MENU", &net_wm_window_type_menu },
161 { "_NET_WM_WINDOW_TYPE_UTILITY", &net_wm_window_type_utility },
162 { "_NET_WM_WINDOW_TYPE_SPLASH", &net_wm_window_type_splash },
163 { "_NET_WM_WINDOW_TYPE_DIALOG", &net_wm_window_type_dialog },
164 { "_NET_WM_WINDOW_TYPE_NORMAL", &net_wm_window_type_normal },
165 { "_NET_WM_STATE", &net_wm_state },
166 { "_NET_WM_STATE_MODAL", &net_wm_state_modal },
167 { "_NET_WM_STATE_STICKY", &net_wm_state_sticky },
168 { "_NET_WM_STATE_MAXIMIZED_VERT", &net_wm_state_maximized_vert },
169 { "_NET_WM_STATE_MAXIMIZED_HORZ", &net_wm_state_maximized_horz },
170 { "_NET_WM_STATE_SHADED", &net_wm_state_shaded },
171 { "_NET_WM_STATE_SKIP_TASKBAR", &net_wm_state_skip_taskbar },
172 { "_NET_WM_STATE_SKIP_PAGER", &net_wm_state_skip_pager },
173 { "_NET_WM_STATE_HIDDEN", &net_wm_state_hidden },
174 { "_NET_WM_STATE_FULLSCREEN", &net_wm_state_fullscreen },
175 { "_NET_WM_STATE_ABOVE", &net_wm_state_above },
176 { "_NET_WM_STATE_BELOW", &net_wm_state_below },
177 { "_NET_WM_ALLOWED_ACTIONS", &net_wm_allowed_actions },
178 { "_NET_WM_ACTION_MOVE", &net_wm_action_move },
179 { "_NET_WM_ACTION_RESIZE", &net_wm_action_resize },
180 { "_NET_WM_ACTION_MINIMIZE", &net_wm_action_minimize },
181 { "_NET_WM_ACTION_SHADE", &net_wm_action_shade },
182 { "_NET_WM_ACTION_STICK", &net_wm_action_stick },
183 { "_NET_WM_ACTION_MAXIMIZE_HORZ", &net_wm_action_maximize_horz },
184 { "_NET_WM_ACTION_MAXIMIZE_VERT", &net_wm_action_maximize_vert },
185 { "_NET_WM_ACTION_FULLSCREEN", &net_wm_action_fullscreen },
186 { "_NET_WM_ACTION_CHANGE_DESKTOP", &net_wm_action_change_desktop },
187 { "_NET_WM_ACTION_CLOSE", &net_wm_action_close },
188 { "_NET_WM_STRUT", &net_wm_strut },
189 { "_NET_WM_STRUT_PARTIAL", &net_wm_strut_partial },
190 { "_NET_WM_ICON_GEOMETRY", &net_wm_icon_geometry },
191 { "_NET_WM_ICON", &net_wm_icon },
192 { "_NET_WM_PID", &net_wm_pid },
193 { "_NET_WM_HANDLED_ICONS", &net_wm_handled_icons },
195 { "_NET_WM_PING", &net_wm_ping },
197 { "UTF8_STRING", &utf8_string },
200 #define atomNr (sizeof(atomNames)/sizeof(atomitem_t))
202 #define _NET_WM_STATE_REMOVE 0
203 #define _NET_WM_STATE_ADD 1
204 #define _NET_WM_STATE_TOGGLE 2
206 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
207 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
208 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
209 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
210 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
211 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
212 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
213 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
214 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
215 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
216 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
218 static void observer(void *self, WMNotification *notif);
219 static void wsobserver(void *self, WMNotification *notif);
221 static void updateClientList(WScreen *scr);
222 static void updateClientListStacking(WScreen *scr, WWindow *);
224 static void updateWorkspaceNames(WScreen *scr);
225 static void updateCurrentWorkspace(WScreen *scr);
226 static void updateWorkspaceCount(WScreen *scr);
228 typedef struct NetData {
229 WScreen *scr;
230 WReservedArea *strut;
231 WWindow **show_desktop;
232 } NetData;
235 static void
236 setSupportedHints(WScreen *scr)
238 Atom atom[atomNr];
239 int i = 0;
241 /* set supported hints list */
242 /* XXX: extend this !!! */
244 atom[i++] = net_client_list;
245 atom[i++] = net_client_list_stacking;
246 atom[i++] = net_number_of_desktops;
247 atom[i++] = net_desktop_geometry;
248 atom[i++] = net_desktop_viewport;
249 atom[i++] = net_current_desktop;
250 atom[i++] = net_desktop_names;
251 atom[i++] = net_active_window;
252 atom[i++] = net_workarea;
253 atom[i++] = net_supporting_wm_check;
254 atom[i++] = net_showing_desktop;
255 #if 0
256 atom[i++] = net_wm_moveresize;
257 #endif
258 atom[i++] = net_wm_desktop;
259 atom[i++] = net_wm_window_type;
260 atom[i++] = net_wm_window_type_desktop;
261 atom[i++] = net_wm_window_type_dock;
262 atom[i++] = net_wm_window_type_toolbar;
263 atom[i++] = net_wm_window_type_menu;
264 atom[i++] = net_wm_window_type_utility;
265 atom[i++] = net_wm_window_type_splash;
266 atom[i++] = net_wm_window_type_dialog;
267 atom[i++] = net_wm_window_type_normal;
269 atom[i++] = net_wm_state;
270 /* atom[i++] = net_wm_state_modal; */ /* XXX: not sure where/when to use it. */
271 atom[i++] = net_wm_state_sticky;
272 atom[i++] = net_wm_state_shaded;
273 atom[i++] = net_wm_state_maximized_horz;
274 atom[i++] = net_wm_state_maximized_vert;
275 atom[i++] = net_wm_state_skip_taskbar;
276 atom[i++] = net_wm_state_skip_pager;
277 atom[i++] = net_wm_state_hidden;
278 atom[i++] = net_wm_state_fullscreen;
279 atom[i++] = net_wm_state_above;
280 atom[i++] = net_wm_state_below;
282 atom[i++] = net_wm_allowed_actions;
283 atom[i++] = net_wm_action_move;
284 atom[i++] = net_wm_action_resize;
285 atom[i++] = net_wm_action_minimize;
286 atom[i++] = net_wm_action_shade;
287 atom[i++] = net_wm_action_stick;
288 atom[i++] = net_wm_action_maximize_horz;
289 atom[i++] = net_wm_action_maximize_vert;
290 atom[i++] = net_wm_action_fullscreen;
291 atom[i++] = net_wm_action_change_desktop;
292 atom[i++] = net_wm_action_close;
294 atom[i++] = net_wm_strut;
295 atom[i++] = net_wm_icon_geometry;
296 atom[i++] = net_wm_icon;
297 atom[i++] = net_wm_handled_icons;
299 atom[i++] = net_wm_name;
300 atom[i++] = net_wm_icon_name;
302 XChangeProperty(dpy, scr->root_win, net_supported, XA_ATOM, 32,
303 PropModeReplace, (unsigned char*)atom, i);
305 /* set supporting wm hint */
306 XChangeProperty(dpy, scr->root_win, net_supporting_wm_check, XA_WINDOW, 32,
307 PropModeReplace, (unsigned char*)&scr->info_window, 1);
309 XChangeProperty(dpy, scr->info_window, net_supporting_wm_check, XA_WINDOW,
310 32, PropModeReplace, (unsigned char*)&scr->info_window, 1);
314 void
315 wNETWMUpdateDesktop(WScreen *scr)
317 CARD32 *views, sizes[2];
318 int count, i;
320 if (scr->workspace_count==0)
321 return;
323 count = scr->workspace_count * 2;
324 views = wmalloc(sizeof(CARD32) * count);
325 /*memset(views, 0, sizeof(CARD32) * count);*/
327 #ifdef VIRTUAL_DESKTOP
328 sizes[0] = scr->workspaces[scr->current_workspace]->width;
329 sizes[1] = scr->workspaces[scr->current_workspace]->height;
330 #else
331 sizes[0] = scr->scr_width;
332 sizes[1] = scr->scr_height;
333 #endif
335 for (i=0; i<scr->workspace_count; i++) {
336 #ifdef VIRTUAL_DESKTOP
337 views[2*i + 0] = scr->workspaces[i]->view_x;
338 views[2*i + 1] = scr->workspaces[i]->view_y;
339 #else
340 views[2*i + 0] = 0;
341 views[2*i + 1] = 0;
342 #endif
345 XChangeProperty(dpy, scr->root_win, net_desktop_geometry, XA_CARDINAL, 32,
346 PropModeReplace, (unsigned char*)sizes, 2);
348 XChangeProperty(dpy, scr->root_win, net_desktop_viewport, XA_CARDINAL, 32,
349 PropModeReplace, (unsigned char*)views, count);
351 wfree(views);
356 wNETWMGetCurrentDesktopFromHint(WScreen *scr)
358 int count;
359 unsigned char *prop;
361 prop= PropGetCheckProperty(scr->root_win, net_current_desktop, XA_CARDINAL,
362 0, 1, &count);
363 if (prop)
365 int desktop= *(CARD32*)prop;
366 XFree(prop);
367 return desktop;
369 return -1;
373 static void
374 updateIconImage(WScreen *scr, WWindow *wwin)
376 Atom actual_type_return;
377 int actual_format_return;
378 unsigned long nitems_return, bytes_after_return;
379 unsigned char *prop_return;
380 int rc = XGetWindowProperty(dpy, wwin->client_win, net_wm_icon, 0, ~0, False,
381 XA_CARDINAL, &actual_type_return, &actual_format_return,
382 &nitems_return, &bytes_after_return, &prop_return);
384 if (rc==Success && prop_return) {
385 unsigned int *data = (unsigned int *)prop_return;
386 unsigned int pos = 0, len = 0, best_pos = 0, best_tmp = ~0;
387 unsigned int pref_size = wPreferences.icon_size;
388 unsigned int pref_sq = pref_size*pref_size;
389 unsigned int tmp;
390 char *src, *dst;
391 RImage *new_rimage;
393 do {
394 len = data[pos+0]*data[pos+1];
395 tmp = pref_sq-len;
396 if (tmp < best_tmp && tmp > 0) {
397 best_tmp = tmp;
398 best_pos = pos;
400 pos += 2+len;
401 } while (pos < nitems_return && len != 0);
403 new_rimage = RCreateImage(data[best_pos+0], data[best_pos+1], True);
404 len = data[best_pos+0] * data[best_pos+1];
405 src = (char*)&data[best_pos+2];
406 dst = new_rimage->data;
407 for (pos=0; pos<len; ++pos, src+=4, dst+=4) {
408 dst[0] = src[2]; /* R */
409 dst[1] = src[1]; /* G */
410 dst[2] = src[0]; /* B */
411 dst[3] = src[3]; /* A */
414 if (new_rimage) {
415 if (wwin->net_icon_image)
416 RReleaseImage(wwin->net_icon_image);
417 wwin->net_icon_image = new_rimage;
420 XFree(prop_return);
425 static void
426 updateShowDesktop(WScreen * scr, Bool show)
428 CARD32 foo;
430 foo = (show == True);
431 XChangeProperty(dpy, scr->root_win, net_showing_desktop, XA_CARDINAL, 32,
432 PropModeReplace, (unsigned char *)&foo, 1);
436 void
437 wNETWMShowingDesktop(WScreen *scr, Bool show)
439 if (show && scr->netdata->show_desktop == NULL) {
440 WWindow *tmp, **wins;
441 int i=0;
443 wins = (WWindow **)wmalloc(sizeof(WWindow *)*(scr->window_count+1));
445 tmp = scr->focused_window;
446 while (tmp) {
447 if (!tmp->flags.hidden && !tmp->flags.miniaturized &&
448 !WFLAGP(tmp, skip_window_list)) {
450 wins[i++] = tmp;
451 tmp->flags.skip_next_animation = 1;
452 tmp->flags.net_show_desktop = 1;
453 wIconifyWindow(tmp);
456 tmp = tmp->prev;
458 wins[i++] = NULL;
460 scr->netdata->show_desktop = wins;
461 updateShowDesktop(scr, True);
462 } else if (scr->netdata->show_desktop != NULL) {
463 /* FIXME: get rid of workspace flashing ! */
464 int ws = scr->current_workspace;
465 WWindow **tmp;
466 for (tmp = scr->netdata->show_desktop; *tmp; ++tmp) {
467 wDeiconifyWindow(*tmp);
468 (*tmp)->flags.net_show_desktop = 0;
470 if (ws != scr->current_workspace)
471 wWorkspaceChange(scr, ws);
472 wfree(scr->netdata->show_desktop);
473 scr->netdata->show_desktop = NULL;
474 updateShowDesktop(scr, False);
479 void
480 wNETWMInitStuff(WScreen *scr)
482 NetData *data;
483 int i;
485 #ifdef DEBUG_WMSPEC
486 printf( "wNETWMInitStuff\n");
487 #endif
489 #ifdef HAVE_XINTERNATOMS
491 Atom atoms[atomNr];
492 char * names[atomNr];
494 for (i = 0; i < atomNr; ++i) {
495 names[i] = atomNames[i].name;
497 XInternAtoms(dpy, &names[0], atomNr, False, atoms);
498 for (i = 0; i < atomNr; ++i) {
499 *atomNames[i].atom = atoms[i];
502 #else
503 for (i = 0; i < atomNr; i++) {
504 *atomNames[i].atom = XInternAtom(dpy, atomNames[i].name, False);
506 #endif
508 data = wmalloc(sizeof(NetData));
509 data->scr = scr;
510 data->strut = NULL;
511 data->show_desktop = NULL;
513 scr->netdata = data;
515 setSupportedHints(scr);
517 WMAddNotificationObserver(observer, data, WMNManaged, NULL);
518 WMAddNotificationObserver(observer, data, WMNUnmanaged, NULL);
519 WMAddNotificationObserver(observer, data, WMNChangedWorkspace, NULL);
520 WMAddNotificationObserver(observer, data, WMNChangedState, NULL);
521 WMAddNotificationObserver(observer, data, WMNChangedFocus, NULL);
522 WMAddNotificationObserver(observer, data, WMNChangedStacking, NULL);
523 WMAddNotificationObserver(observer, data, WMNChangedName, NULL);
525 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceCreated, NULL);
526 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceDestroyed, NULL);
527 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceChanged, NULL);
528 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceNameChanged, NULL);
530 updateClientList(scr);
531 updateClientListStacking(scr, NULL);
532 updateWorkspaceCount(scr);
533 updateWorkspaceNames(scr);
534 updateShowDesktop(scr, False);
536 wScreenUpdateUsableArea(scr);
540 void
541 wNETWMCleanup(WScreen *scr)
543 int i;
545 for (i= 0; i < atomNr; i++)
546 XDeleteProperty(dpy, scr->root_win, *atomNames[i].atom);
550 void
551 wNETWMUpdateActions(WWindow *wwin, Bool del)
553 Atom action[10]; /* nr of actions atoms defined */
554 int i = 0;
556 if (del) {
557 XDeleteProperty(dpy, wwin->client_win, net_wm_allowed_actions);
558 return;
561 if (IS_MOVABLE(wwin))
562 action[i++] = net_wm_action_move;
564 if (IS_RESIZABLE(wwin))
565 action[i++] = net_wm_action_resize;
567 if (!WFLAGP(wwin, no_miniaturizable))
568 action[i++] = net_wm_action_minimize;
570 if (!WFLAGP(wwin, no_shadeable))
571 action[i++] = net_wm_action_shade;
573 /* if (!WFLAGP(wwin, no_stickable)) */
574 action[i++] = net_wm_action_stick;
576 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_HORIZONTAL)) */
577 if (IS_RESIZABLE(wwin))
578 action[i++] = net_wm_action_maximize_horz;
580 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_VERTICAL)) */
581 if (IS_RESIZABLE(wwin))
582 action[i++] = net_wm_action_maximize_vert;
584 /* if (!WFLAGP(wwin, no_fullscreen)) */
585 action[i++] = net_wm_action_fullscreen;
587 /* if (!WFLAGP(wwin, no_change_desktop)) */
588 action[i++] = net_wm_action_change_desktop;
590 if (!WFLAGP(wwin, no_closable))
591 action[i++] = net_wm_action_close;
593 XChangeProperty(dpy, wwin->client_win, net_wm_allowed_actions,
594 XA_ATOM, 32, PropModeReplace, (unsigned char *)action, i);
598 void
599 wNETWMUpdateWorkarea(WScreen *scr, WArea usableArea)
601 CARD32 *area;
602 int count, i;
604 /* XXX: not Xinerama compatible,
605 xinerama gets the largest available */
607 if(!scr->netdata || scr->workspace_count==0)
608 return;
610 count = scr->workspace_count * 4;
611 area = wmalloc(sizeof(CARD32) * count);
612 for (i=0; i<scr->workspace_count; i++) {
613 area[4*i + 0] = usableArea.x1;
614 area[4*i + 1] = usableArea.y1;
615 area[4*i + 2] = usableArea.x2 - usableArea.x1;
616 area[4*i + 3] = usableArea.y2 - usableArea.y1;
619 XChangeProperty(dpy, scr->root_win, net_workarea, XA_CARDINAL, 32,
620 PropModeReplace, (unsigned char*)area, count);
622 wfree(area);
626 Bool
627 wNETWMGetUsableArea(WScreen *scr, int head, WArea *area)
629 WReservedArea *cur;
630 WMRect rect;
632 if(!scr->netdata || !scr->netdata->strut)
633 return False;
635 area->x1 = area->y1 = area->x2 = area->y2 = 0;
637 for(cur = scr->netdata->strut ; cur ; cur = cur->next) {
638 WWindow * wwin = wWindowFor(cur->window);
639 if (wWindowTouchesHead(wwin, head)) {
640 if(cur->area.x1 > area->x1)
641 area->x1 = cur->area.x1;
642 if(cur->area.y1 > area->y1)
643 area->y1 = cur->area.y1;
644 if(cur->area.x2 > area->x2)
645 area->x2 = cur->area.x2;
646 if(cur->area.y2 > area->y2)
647 area->y2 = cur->area.y2;
651 if (area->x1==0 && area->x2==0 &&
652 area->y1==0 && area->y2==0) return False;
654 rect = wGetRectForHead(scr, head);
656 area->x1 = rect.pos.x + area->x1;
657 area->x2 = rect.pos.x + rect.size.width - area->x2;
658 area->y1 = rect.pos.y + area->y1;
659 area->y2 = rect.pos.y + rect.size.height - area->y2;
661 return True;
665 static void
666 updateClientList(WScreen *scr)
668 WWindow *wwin;
669 Window *windows;
670 int count;
672 windows = (Window *)wmalloc(sizeof(Window)*(scr->window_count+1));
674 count = 0;
675 wwin = scr->focused_window;
676 while (wwin) {
677 windows[count++] = wwin->client_win;
678 wwin = wwin->prev;
680 XChangeProperty(dpy, scr->root_win, net_client_list, XA_WINDOW, 32,
681 PropModeReplace, (unsigned char*)windows, count);
683 wfree(windows);
684 XFlush(dpy);
688 static void
689 updateClientListStacking(WScreen *scr, WWindow *wwin_excl)
691 WWindow *wwin;
692 Window *client_list;
693 Window *client_list_reverse;
694 int client_count;
695 WCoreWindow *tmp;
696 WMBagIterator iter;
697 int i;
699 /* update client list */
700 i = scr->window_count + 1;
701 client_list = (Window*)wmalloc(sizeof(Window) * i);
702 client_list_reverse = (Window*)wmalloc(sizeof(Window) * i);
704 client_count = 0;
705 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
706 while (tmp) {
707 wwin = wWindowFor(tmp->window);
708 /* wwin_excl is a window to exclude from the list
709 (e.g. it's now unmanaged) */
710 if(wwin && (wwin != wwin_excl))
711 client_list[client_count++] = wwin->client_win;
712 tmp = tmp->stacking->under;
716 for(i = 0; i < client_count; i++) {
717 Window w = client_list[client_count - i - 1];
718 client_list_reverse[i] = w;
721 XChangeProperty(dpy, scr->root_win, net_client_list_stacking, XA_WINDOW, 32,
722 PropModeReplace, (unsigned char *)client_list_reverse, client_count);
724 wfree(client_list);
725 wfree(client_list_reverse);
727 XFlush(dpy);
731 static void
732 updateWorkspaceCount(WScreen *scr) /* changeable */
734 CARD32 count;
736 count = scr->workspace_count;
738 XChangeProperty(dpy, scr->root_win, net_number_of_desktops, XA_CARDINAL,
739 32, PropModeReplace, (unsigned char*)&count, 1);
743 static void
744 updateCurrentWorkspace(WScreen *scr) /* changeable */
746 CARD32 count;
748 count = scr->current_workspace;
750 XChangeProperty(dpy, scr->root_win, net_current_desktop, XA_CARDINAL, 32,
751 PropModeReplace, (unsigned char*)&count, 1);
755 static void
756 updateWorkspaceNames(WScreen *scr)
758 char buf[1024], *pos;
759 unsigned int i, len, curr_size;
761 pos = buf;
762 len = 0;
763 for(i = 0; i < scr->workspace_count; i++) {
764 curr_size = strlen(scr->workspaces[i]->name);
765 strcpy(pos, scr->workspaces[i]->name);
766 pos += (curr_size+1);
767 len += (curr_size+1);
770 XChangeProperty(dpy, scr->root_win, net_desktop_names, utf8_string, 8,
771 PropModeReplace, (unsigned char *)buf, len);
775 static void
776 updateFocusHint(WScreen *scr, WWindow *wwin) /* changeable */
778 Window window;
780 if (!scr->focused_window || !scr->focused_window->flags.focused)
781 window = None;
782 else
783 window = scr->focused_window->client_win;
785 XChangeProperty(dpy, scr->root_win, net_active_window, XA_WINDOW, 32,
786 PropModeReplace, (unsigned char*)&window, 1);
790 static void
791 updateWorkspaceHint(WWindow *wwin, Bool fake, Bool del)
793 CARD32 l;
795 if (del) {
796 XDeleteProperty(dpy, wwin->client_win, net_wm_desktop);
797 } else {
798 l = ((fake || IS_OMNIPRESENT(wwin)) ? -1 : wwin->frame->workspace);
799 XChangeProperty(dpy, wwin->client_win, net_wm_desktop, XA_CARDINAL,
800 32, PropModeReplace, (unsigned char*)&l, 1);
805 static void
806 updateStateHint(WWindow *wwin, Bool changedWorkspace, Bool del) /* changeable */
808 if (del) {
809 if (!wwin->flags.net_state_from_client) {
810 XDeleteProperty(dpy, wwin->client_win, net_wm_state);
812 } else {
813 Atom state[11]; /* nr of defined state atoms */
814 int i = 0;
816 if(changedWorkspace || (wPreferences.sticky_icons && !IS_OMNIPRESENT(wwin)))
817 updateWorkspaceHint(wwin, False, False);
819 if (IS_OMNIPRESENT(wwin))
820 state[i++] = net_wm_state_sticky;
821 if (wwin->flags.shaded)
822 state[i++] = net_wm_state_shaded;
823 if (wwin->flags.maximized & MAX_HORIZONTAL)
824 state[i++] = net_wm_state_maximized_horz;
825 if (wwin->flags.maximized & MAX_VERTICAL)
826 state[i++] = net_wm_state_maximized_vert;
827 if (WFLAGP(wwin, skip_window_list))
828 state[i++] = net_wm_state_skip_taskbar;
829 if (wwin->flags.net_skip_pager)
830 state[i++] = net_wm_state_skip_pager;
831 if ((wwin->flags.hidden || wwin->flags.miniaturized) && !wwin->flags.net_show_desktop){
832 state[i++] = net_wm_state_hidden;
833 state[i++] = net_wm_state_skip_pager;
835 if (wwin->flags.miniaturized && wPreferences.sticky_icons) {
836 if( !IS_OMNIPRESENT(wwin))
837 updateWorkspaceHint(wwin, True, False);
838 state[i++] = net_wm_state_sticky;
841 if (WFLAGP(wwin, sunken))
842 state[i++] = net_wm_state_below;
843 if (WFLAGP(wwin, floating))
844 state[i++] = net_wm_state_above;
845 if (wwin->flags.fullscreen)
846 state[i++] = net_wm_state_fullscreen;
848 XChangeProperty(dpy, wwin->client_win, net_wm_state, XA_ATOM, 32,
849 PropModeReplace, (unsigned char *)state, i);
854 static Bool
855 updateStrut(WWindow *wwin, Bool adding)
857 WScreen *scr;
858 WReservedArea *area;
859 Bool hasState = False;
861 scr = wwin->screen_ptr;
863 if (adding) {
864 Atom type_ret;
865 int fmt_ret;
866 unsigned long nitems_ret;
867 unsigned long bytes_after_ret;
868 long *data = 0;
870 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_strut, 0, 4, False,
871 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
872 &bytes_after_ret,
873 (unsigned char**)&data)==Success && data) {
875 area = (WReservedArea *)wmalloc(sizeof(WReservedArea));
876 area->area.x1 = data[0];
877 area->area.x2 = data[1];
878 area->area.y1 = data[2];
879 area->area.y2 = data[3];
881 area->window = wwin->client_win;
883 area->next = scr->netdata->strut;
884 scr->netdata->strut = area;
886 XFree(data);
887 hasState = True;
888 #ifdef VIRTUAL_DESKTOP
889 /* just in case wm_window_type didn't set it already */
890 wwin->client_flags.virtual_stick = 1;
891 #endif
894 } else {
895 /* deleting */
896 area = scr->netdata->strut;
898 if(area) {
899 if(area->window == wwin->client_win) {
900 scr->netdata->strut = area->next;
901 wfree(area);
902 hasState = True;
903 } else {
904 while(area->next && area->next->window != wwin->client_win)
905 area = area->next;
907 if(area->next) {
908 WReservedArea *next;
910 next = area->next->next;
911 wfree(area->next);
912 area->next = next;
914 hasState = True;
920 return hasState;
924 static int
925 getWindowLayer(WWindow * wwin)
927 int layer = WMNormalLevel;
929 if (wwin->type == net_wm_window_type_desktop) {
930 layer = WMDesktopLevel;
931 } else if (wwin->type == net_wm_window_type_dock) {
932 layer = WMDockLevel;
933 } else if (wwin->type == net_wm_window_type_toolbar) {
934 layer = WMMainMenuLevel;
935 } else if (wwin->type == net_wm_window_type_menu) {
936 layer = WMSubmenuLevel;
937 } else if (wwin->type == net_wm_window_type_utility) {
938 } else if (wwin->type == net_wm_window_type_splash) {
939 } else if (wwin->type == net_wm_window_type_dialog) {
940 if (wwin->transient_for) {
941 WWindow *parent = wWindowFor(wwin->transient_for);
942 if (parent && parent->flags.fullscreen) {
943 layer = WMFullscreenLevel;
946 /* //layer = WMPopUpLevel; // this seems a bad idea -Dan */
947 } else if (wwin->type == net_wm_window_type_normal) {
950 if (wwin->client_flags.sunken && WMSunkenLevel < layer)
951 layer = WMSunkenLevel;
952 if (wwin->client_flags.floating && WMFloatingLevel > layer)
953 layer = WMFloatingLevel;
955 return layer;
959 static void
960 doStateAtom(WWindow *wwin, Atom state, int set, Bool init)
963 if (state == net_wm_state_sticky) {
964 if (set == _NET_WM_STATE_TOGGLE) {
965 set = !IS_OMNIPRESENT(wwin);
967 if (set != wwin->flags.omnipresent) {
968 wWindowSetOmnipresent(wwin, set);
970 } else if (state == net_wm_state_shaded) {
971 if (set == _NET_WM_STATE_TOGGLE) {
972 set = !wwin->flags.shaded;
974 if (init) {
975 wwin->flags.shaded = set;
976 } else {
977 if (set) {
978 wShadeWindow(wwin);
979 } else {
980 wUnshadeWindow(wwin);
983 } else if (state == net_wm_state_skip_taskbar) {
984 if (set == _NET_WM_STATE_TOGGLE) {
985 set = !wwin->client_flags.skip_window_list;
987 wwin->client_flags.skip_window_list = set;
988 } else if (state == net_wm_state_skip_pager) {
989 if (set == _NET_WM_STATE_TOGGLE) {
990 set = !wwin->flags.net_skip_pager;
992 wwin->flags.net_skip_pager = set;
993 } else if (state == net_wm_state_maximized_vert) {
994 if (set == _NET_WM_STATE_TOGGLE) {
995 set = !(wwin->flags.maximized & MAX_VERTICAL);
997 if (init) {
998 wwin->flags.maximized = set;
999 } else {
1000 if (set) {
1001 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_VERTICAL);
1002 } else {
1003 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_VERTICAL);
1006 } else if (state == net_wm_state_maximized_horz) {
1007 if (set == _NET_WM_STATE_TOGGLE) {
1008 set = !(wwin->flags.maximized & MAX_HORIZONTAL);
1010 if (init) {
1011 wwin->flags.maximized = set;
1012 } else {
1013 if (set) {
1014 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_HORIZONTAL);
1015 } else {
1016 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_HORIZONTAL);
1019 } else if (state == net_wm_state_hidden) {
1020 if (set == _NET_WM_STATE_TOGGLE) {
1021 set = !(wwin->flags.miniaturized);
1023 if (init) {
1024 wwin->flags.miniaturized = set;
1025 } else {
1026 if (set) {
1027 wIconifyWindow(wwin);
1028 } else {
1029 wDeiconifyWindow(wwin);
1032 } else if (state == net_wm_state_fullscreen) {
1033 if (set == _NET_WM_STATE_TOGGLE) {
1034 set = !(wwin->flags.fullscreen);
1036 if (init) {
1037 wwin->flags.fullscreen = set;
1038 } else {
1039 if (set) {
1040 wFullscreenWindow(wwin);
1041 } else {
1042 wUnfullscreenWindow(wwin);
1045 } else if (state == net_wm_state_above) {
1046 if (set == _NET_WM_STATE_TOGGLE) {
1047 set = !(wwin->client_flags.floating);
1049 if (init) {
1050 wwin->client_flags.floating = set;
1051 } else {
1052 wwin->client_flags.floating = set;
1053 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1056 } else if (state == net_wm_state_below) {
1057 if (set == _NET_WM_STATE_TOGGLE) {
1058 set = !(wwin->client_flags.sunken);
1060 if (init) {
1061 wwin->client_flags.sunken = set;
1062 } else {
1063 wwin->client_flags.sunken = set;
1064 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1067 } else {
1068 #ifdef DEBUG_WMSPEC
1069 printf("doStateAtom unknown atom %s set %d\n", XGetAtomName(dpy, state), set);
1070 #endif
1075 static void
1076 removeIcon( WWindow * wwin)
1078 if (wwin->icon == NULL)
1079 return;
1080 if (wwin->flags.miniaturized && wwin->icon->mapped) {
1081 XUnmapWindow(dpy, wwin->icon->core->window);
1082 RemoveFromStackList(wwin->icon->core);
1083 wIconDestroy(wwin->icon);
1084 wwin->icon = NULL;
1089 static Bool
1090 handleWindowType(WWindow * wwin, Atom type, int *layer)
1092 Bool ret = True;
1094 if (type == net_wm_window_type_desktop) {
1095 wwin->client_flags.no_titlebar = 1;
1096 wwin->client_flags.no_resizable = 1;
1097 wwin->client_flags.no_miniaturizable = 1;
1098 wwin->client_flags.no_border = 1;
1099 wwin->client_flags.no_resizebar = 1;
1100 wwin->client_flags.no_shadeable = 1;
1101 wwin->client_flags.no_movable = 1;
1102 wwin->client_flags.omnipresent = 1;
1103 wwin->client_flags.skip_window_list = 1;
1104 wwin->client_flags.dont_move_off = 1;
1105 wwin->client_flags.no_appicon = 1;
1106 wwin->flags.net_skip_pager = 1;
1107 wwin->frame_x = 0;
1108 wwin->frame_y = 0;
1109 #ifdef VIRTUAL_DESKTOP
1110 wwin->client_flags.virtual_stick = 1;
1111 #endif
1112 } else if (type == net_wm_window_type_dock) {
1113 wwin->client_flags.no_titlebar = 1;
1114 wwin->client_flags.no_resizable = 1;
1115 wwin->client_flags.no_miniaturizable = 1;
1116 wwin->client_flags.no_border = 1; /* XXX: really not a single decoration. */
1117 wwin->client_flags.no_resizebar = 1;
1118 wwin->client_flags.no_shadeable = 1;
1119 wwin->client_flags.no_movable = 1;
1120 wwin->client_flags.omnipresent = 1;
1121 wwin->client_flags.skip_window_list = 1;
1122 wwin->client_flags.dont_move_off = 1;
1123 wwin->flags.net_skip_pager = 1;
1124 #ifdef VIRTUAL_DESKTOP
1125 wwin->client_flags.virtual_stick = 1;
1126 #endif
1127 } else if (type == net_wm_window_type_toolbar) {
1128 wwin->client_flags.no_titlebar = 1;
1129 wwin->client_flags.no_resizable = 1;
1130 wwin->client_flags.no_miniaturizable = 1;
1131 wwin->client_flags.no_resizebar = 1;
1132 wwin->client_flags.no_shadeable = 1;
1133 wwin->client_flags.skip_window_list = 1;
1134 wwin->client_flags.dont_move_off = 1;
1135 wwin->client_flags.no_appicon = 1;
1136 } else if (type == net_wm_window_type_menu) {
1137 wwin->client_flags.no_titlebar = 1;
1138 wwin->client_flags.no_resizable = 1;
1139 wwin->client_flags.no_miniaturizable = 1;
1140 wwin->client_flags.no_resizebar = 1;
1141 wwin->client_flags.no_shadeable = 1;
1142 wwin->client_flags.skip_window_list = 1;
1143 wwin->client_flags.dont_move_off = 1;
1144 wwin->client_flags.no_appicon = 1;
1145 } else if (type == net_wm_window_type_utility) {
1146 wwin->client_flags.no_appicon = 1;
1147 } else if (type == net_wm_window_type_splash) {
1148 wwin->client_flags.no_titlebar = 1;
1149 wwin->client_flags.no_resizable = 1;
1150 wwin->client_flags.no_miniaturizable = 1;
1151 wwin->client_flags.no_resizebar = 1;
1152 wwin->client_flags.no_shadeable = 1;
1153 wwin->client_flags.no_movable = 1;
1154 wwin->client_flags.skip_window_list = 1;
1155 wwin->client_flags.dont_move_off = 1;
1156 wwin->client_flags.no_appicon = 1;
1157 wwin->flags.net_skip_pager = 1;
1158 } else if (type == net_wm_window_type_dialog) {
1159 /* These also seem a bad idea in our context -Dan
1160 // wwin->client_flags.skip_window_list = 1;
1161 // wwin->client_flags.no_appicon = 1;
1163 } else if (type == net_wm_window_type_normal) {
1164 } else {
1165 ret = False;
1168 wwin->type = type;
1169 *layer = getWindowLayer(wwin);
1171 return ret;
1175 void
1176 wNETWMPositionSplash(WWindow *wwin, int *x, int *y, int width, int height)
1178 if (wwin->type == net_wm_window_type_splash) {
1179 WScreen * scr = wwin->screen_ptr;
1180 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1181 *x = rect.pos.x + (rect.size.width - width)/2;
1182 *y = rect.pos.y + (rect.size.height - height)/2;
1187 static void
1188 updateWindowType(WWindow * wwin)
1190 Atom type_ret;
1191 int fmt_ret;
1192 unsigned long nitems_ret;
1193 unsigned long bytes_after_ret;
1194 long *data = 0;
1195 int layer;
1196 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1,
1197 False, XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1198 &bytes_after_ret, (unsigned char **)&data)==Success
1199 && data) {
1201 int i;
1202 Atom * type = (Atom *)data;
1203 for (i=0; i<nitems_ret; ++i) {
1204 if (handleWindowType(wwin, type[i], &layer)) break;
1206 XFree(data);
1209 ChangeStackingLevel(wwin->frame->core, layer);
1210 wwin->frame->flags.need_texture_change = 1;
1211 wWindowConfigureBorders(wwin);
1212 wFrameWindowPaint(wwin->frame);
1213 wNETWMUpdateActions(wwin, False);
1217 Bool
1218 wNETWMCheckClientHints(WWindow *wwin, int *layer, int *workspace)
1220 Atom type_ret;
1221 int fmt_ret;
1222 unsigned long nitems_ret;
1223 unsigned long bytes_after_ret;
1224 long *data = 0;
1225 Bool hasState = False;
1226 int i;
1228 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_desktop, 0, 1, False,
1229 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1230 &bytes_after_ret,
1231 (unsigned char**)&data)==Success && data) {
1233 long desktop = *data;
1234 XFree(data);
1236 if(desktop == -1)
1237 wwin->client_flags.omnipresent = 1;
1238 else
1239 *workspace = desktop;
1241 hasState = True;
1244 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_state, 0, 1, False,
1245 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1246 &bytes_after_ret,
1247 (unsigned char**)&data)==Success && data) {
1249 Atom * state = (Atom *)data;
1250 for(i=0; i<nitems_ret; ++i) {
1251 doStateAtom(wwin, state[i], _NET_WM_STATE_ADD, True);
1253 XFree(data);
1254 hasState = True;
1257 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1, False,
1258 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1259 &bytes_after_ret,
1260 (unsigned char **)&data) == Success && data) {
1262 Atom * type = (Atom *)data;
1263 for (i=0; i<nitems_ret; ++i) {
1264 if (handleWindowType(wwin, type[i], layer)) break;
1266 XFree(data);
1267 hasState = True;
1270 wNETWMUpdateActions(wwin, False);
1271 updateStrut(wwin, False);
1272 if (updateStrut(wwin, True)) {
1273 hasState = True;
1275 wScreenUpdateUsableArea(wwin->screen_ptr);
1277 return hasState;
1281 static Bool
1282 updateNetIconInfo(WWindow *wwin) {
1284 Atom type_ret;
1285 int fmt_ret;
1286 unsigned long nitems_ret;
1287 unsigned long bytes_after_ret;
1288 long *data = 0;
1289 Bool hasState = False;
1290 Bool old_state = wwin->flags.net_handle_icon;
1292 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_handled_icons, 0, 1, False,
1293 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1294 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1295 long handled = *data;
1296 wwin->flags.net_handle_icon = (handled != 0);
1297 XFree(data);
1298 hasState = True;
1300 } else wwin->flags.net_handle_icon = False;
1302 if ( XGetWindowProperty(dpy, wwin->client_win, net_wm_icon_geometry, 0, 4, False,
1303 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1304 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1306 #ifdef NETWM_PROPER
1307 if (wwin->flags.net_handle_icon)
1308 #else
1309 wwin->flags.net_handle_icon = True;
1310 #endif
1312 wwin->icon_x = data[0];
1313 wwin->icon_y = data[1];
1314 wwin->icon_w = data[2];
1315 wwin->icon_h = data[3];
1318 XFree(data);
1319 hasState = True;
1321 } else wwin->flags.net_handle_icon = False;
1323 if (wwin->flags.miniaturized &&
1324 old_state != wwin->flags.net_handle_icon) {
1325 if (wwin->flags.net_handle_icon) {
1326 removeIcon(wwin);
1327 } else {
1328 wwin->flags.miniaturized = False;
1329 wwin->flags.skip_next_animation = True;
1330 wIconifyWindow(wwin);
1334 return hasState;
1338 Bool
1339 wNETWMCheckInitialClientState(WWindow *wwin)
1341 Bool hasState = False;
1343 #ifdef DEBUG_WMSPEC
1344 printf("CheckInitialClientState\n");
1345 #endif
1347 wNETWMShowingDesktop(wwin->screen_ptr, False);
1349 hasState |= updateNetIconInfo(wwin);
1351 updateIconImage(wwin->screen_ptr, wwin);
1353 return hasState;
1357 static void
1358 handleDesktopNames(XClientMessageEvent *event, WScreen *scr)
1360 unsigned long nitems_ret, bytes_after_ret;
1361 char *data, *names[32];
1362 int fmt_ret, i, n;
1363 Atom type_ret;
1365 if (XGetWindowProperty(dpy, scr->root_win, net_desktop_names, 0, 1, False,
1366 utf8_string, &type_ret, &fmt_ret, &nitems_ret,
1367 &bytes_after_ret,
1368 (unsigned char**)&data)!=Success) {
1369 return;
1372 if (data == 0)
1373 return;
1375 if (type_ret != utf8_string || fmt_ret != 8)
1376 return;
1378 n = 0;
1379 names[n] = data;
1380 for (i = 0; i < nitems_ret; i++) {
1381 if (data[i] == 0) {
1382 n++;
1383 names[n] = &data[i];
1384 } else if (*names[n] == 0) {
1385 names[n] = &data[i];
1386 wWorkspaceRename(scr, n, names[n]);
1392 Bool
1393 wNETWMProcessClientMessage(XClientMessageEvent *event)
1395 WScreen *scr;
1396 WWindow *wwin;
1397 Bool done = True;
1399 #ifdef DEBUG_WMSPEC
1400 printf("processClientMessage type %s\n", XGetAtomName(dpy, event->message_type));
1401 #endif
1403 scr = wScreenForWindow(event->window);
1404 if (scr) {
1405 /* generic client messages */
1406 if (event->message_type == net_current_desktop) {
1407 wWorkspaceChange(scr, event->data.l[0]);
1408 } else if(event->message_type == net_number_of_desktops) {
1409 long value;
1411 value = event->data.l[0];
1412 if(value > scr->workspace_count) {
1413 wWorkspaceMake(scr, value - scr->workspace_count);
1414 } else if(value < scr->workspace_count) {
1415 int i;
1416 Bool rebuild = False;
1418 for (i = scr->workspace_count-1; i >= value; i--) {
1419 if (!wWorkspaceDelete(scr, i)) {
1420 rebuild = True;
1421 break;
1425 if(rebuild) {
1426 updateWorkspaceCount(scr);
1429 } else if (event->message_type == net_showing_desktop) {
1430 wNETWMShowingDesktop(scr, event->data.l[0]);
1431 #ifdef VIRTUAL_DESKTOP
1432 } else if (event->message_type == net_desktop_viewport) {
1433 wWorkspaceSetViewport(scr, scr->current_workspace,
1434 event->data.l[0], event->data.l[1]);
1435 #endif
1436 } else if (event->message_type == net_desktop_names) {
1437 handleDesktopNames(event, scr);
1438 } else {
1439 done = False;
1442 if (done)
1443 return True;
1446 /* window specific client messages */
1448 wwin = wWindowFor(event->window);
1449 if (!wwin)
1450 return False;
1452 if (event->message_type == net_active_window) {
1453 wNETWMShowingDesktop(scr, False);
1454 wMakeWindowVisible(wwin);
1455 } else if (event->message_type == net_close_window) {
1456 if (!WFLAGP(wwin, no_closable)) {
1457 if (wwin->protocols.DELETE_WINDOW)
1458 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
1460 } else if (event->message_type == net_wm_state) {
1461 int maximized = wwin->flags.maximized;
1462 long set = event->data.l[0];
1464 #ifdef DEBUG_WMSPEC
1465 printf("net_wm_state set %d a1 %s a2 %s\n", set,
1466 XGetAtomName(dpy, event->data.l[1]),
1467 XGetAtomName(dpy, event->data.l[2]));
1468 #endif
1470 doStateAtom(wwin, (Atom)event->data.l[1], set, False);
1471 if(event->data.l[2])
1472 doStateAtom(wwin, (Atom)event->data.l[2], set, False);
1474 if(wwin->flags.maximized != maximized) {
1475 if(!wwin->flags.maximized) {
1476 wwin->flags.maximized = maximized;
1477 wUnmaximizeWindow(wwin);
1478 } else {
1479 wMaximizeWindow(wwin, wwin->flags.maximized);
1482 updateStateHint(wwin, False, False);
1483 } else if (event->message_type == net_wm_handled_icons ||
1484 event->message_type == net_wm_icon_geometry) {
1485 updateNetIconInfo(wwin);
1486 } else if (event->message_type == net_wm_desktop) {
1487 long desktop = event->data.l[0];
1488 if (desktop == -1) {
1489 wWindowSetOmnipresent(wwin, True);
1490 } else {
1491 if (IS_OMNIPRESENT(wwin))
1492 wWindowSetOmnipresent(wwin, False);
1493 wWindowChangeWorkspace(wwin, desktop);
1495 } else if (event->message_type == net_wm_icon) {
1496 updateIconImage(scr, wwin);
1497 } else {
1498 done = False;
1501 return done;
1505 Bool
1506 wNETWMCheckClientHintChange(WWindow *wwin, XPropertyEvent *event)
1508 Bool ret = True;
1510 if(event->atom == net_wm_strut) {
1511 updateStrut(wwin, False);
1512 updateStrut(wwin, True);
1513 wScreenUpdateUsableArea(wwin->screen_ptr);
1514 } else if (event->atom == net_wm_handled_icons ||
1515 event->atom == net_wm_icon_geometry) {
1516 updateNetIconInfo(wwin);
1517 } else if (event->atom == net_wm_window_type) {
1518 updateWindowType(wwin);
1519 } else if (event->atom == net_wm_name) {
1520 char *name= wNETWMGetWindowName(wwin->client_win);
1521 wWindowUpdateName(wwin, name);
1522 if (name) wfree(name);
1523 } else if (event->atom == net_wm_icon_name) {
1524 if (wwin->icon) {
1525 char *name= wNETWMGetIconName(wwin->client_win);
1526 wIconChangeTitle(wwin->icon, name);
1528 } else {
1529 ret = False;
1532 return ret;
1537 wNETWMGetPidForWindow(Window window)
1539 Atom type_ret;
1540 int fmt_ret;
1541 unsigned long nitems_ret;
1542 unsigned long bytes_after_ret;
1543 long *data = 0;
1544 int pid;
1546 if (XGetWindowProperty(dpy, window, net_wm_pid, 0, 1, False,
1547 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1548 &bytes_after_ret,
1549 (unsigned char**)&data)==Success && data) {
1551 pid = *data;
1552 XFree(data);
1553 } else {
1554 pid = 0;
1557 return pid;
1561 char*
1562 wNETWMGetWindowName(Window window)
1564 char *name;
1565 char *ret;
1566 int size;
1568 name= (char*)PropGetCheckProperty(window,
1569 net_wm_name, utf8_string, 0,
1570 0, &size);
1571 if (name) {
1572 ret= wstrndup(name, size);
1573 XFree(name);
1575 else
1576 ret= NULL;
1577 return ret;
1581 char*
1582 wNETWMGetIconName(Window window)
1584 char *name;
1585 char *ret;
1586 int size;
1588 name= (char*)PropGetCheckProperty(window,
1589 net_wm_icon_name, utf8_string, 0,
1590 0, &size);
1591 if (name) {
1592 ret= wstrndup(name, size);
1593 XFree(name);
1595 else
1596 ret= NULL;
1597 return ret;
1601 static void
1602 observer(void *self, WMNotification *notif)
1604 WWindow *wwin = (WWindow*)WMGetNotificationObject(notif);
1605 const char *name = WMGetNotificationName(notif);
1606 void *data = WMGetNotificationClientData(notif);
1607 NetData *ndata = (NetData*)self;
1610 if (strcmp(name, WMNManaged) == 0 && wwin) {
1611 updateClientList(wwin->screen_ptr);
1612 updateClientListStacking(wwin->screen_ptr, NULL);
1613 updateStateHint(wwin, True, False);
1615 updateStrut(wwin, False);
1616 updateStrut(wwin, True);
1617 wScreenUpdateUsableArea(wwin->screen_ptr);
1618 } else if (strcmp(name, WMNUnmanaged) == 0 && wwin) {
1619 updateClientList(wwin->screen_ptr);
1620 updateClientListStacking(wwin->screen_ptr, wwin);
1621 updateWorkspaceHint(wwin, False, True);
1622 updateStateHint(wwin, False, True);
1623 wNETWMUpdateActions(wwin, True);
1625 updateStrut(wwin, False);
1626 wScreenUpdateUsableArea(wwin->screen_ptr);
1627 } else if (strcmp(name, WMNResetStacking) == 0 && wwin) {
1628 updateClientListStacking(wwin->screen_ptr, NULL);
1629 updateStateHint(wwin, False, False);
1630 } else if (strcmp(name, WMNChangedStacking) == 0 && wwin) {
1631 updateClientListStacking(wwin->screen_ptr, NULL);
1632 updateStateHint(wwin, False, False);
1633 } else if (strcmp(name, WMNChangedFocus) == 0) {
1634 updateFocusHint(ndata->scr, wwin);
1635 } else if (strcmp(name, WMNChangedWorkspace) == 0 && wwin) {
1636 updateWorkspaceHint(wwin, False, False);
1637 updateStateHint(wwin, True, False);
1638 } else if (strcmp(name, WMNChangedState) == 0 && wwin) {
1639 updateStateHint(wwin, !strcmp(data, "omnipresent"), False);
1644 static void
1645 wsobserver(void *self, WMNotification *notif)
1647 WScreen *scr = (WScreen*)WMGetNotificationObject(notif);
1648 const char *name = WMGetNotificationName(notif);
1650 if (strcmp(name, WMNWorkspaceCreated) == 0) {
1651 updateWorkspaceCount(scr);
1652 updateWorkspaceNames(scr);
1653 } else if (strcmp(name, WMNWorkspaceDestroyed) == 0) {
1654 updateWorkspaceCount(scr);
1655 updateWorkspaceNames(scr);
1656 } else if (strcmp(name, WMNWorkspaceChanged) == 0) {
1657 updateCurrentWorkspace(scr);
1658 } else if (strcmp(name, WMNWorkspaceNameChanged) == 0) {
1659 updateWorkspaceNames(scr);
1663 #endif /* NETWM_HINTS */