paranoid signal stuff
[wmaker-crm.git] / src / gnome.c
blob2082d63b5fbc7a87757a5e824782cb752478615f
1 /* gnome.c-- support for the GNOME Hints
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1998, 1999 Alfredo K. Kojima
6 *
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 * According to the author of this thing, it should not be taken seriously.
25 * IMHO, there are lot's of weirdnesses and it's quite unelegant. I'd
26 * rather not support it, but here it goes anyway.
29 #include "wconfig.h"
31 #ifdef GNOME_STUFF
33 #include <X11/Xlib.h>
34 #include <X11/Xutil.h>
35 #include <X11/Xatom.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
42 #include "WindowMaker.h"
43 #include "screen.h"
44 #include "wcore.h"
45 #include "framewin.h"
46 #include "window.h"
47 #include "workspace.h"
48 #include "funcs.h"
49 #include "actions.h"
50 #include "stacking.h"
52 #include "gnome.h"
59 #define WIN_HINTS_SKIP_FOCUS (1<<0) /*"alt-tab" skips this win*/
60 #define WIN_HINTS_SKIP_WINLIST (1<<1) /*do not show in window list*/
61 #define WIN_HINTS_SKIP_TASKBAR (1<<2) /*do not show on taskbar*/
62 #define WIN_HINTS_GROUP_TRANSIENT (1<<3) /*Reserved - definition is unclear*/
63 #define WIN_HINTS_FOCUS_ON_CLICK (1<<4) /*app only accepts focus if clicked*/
64 #define WIN_HINTS_DO_NOT_COVER (1<<5) /* attempt to not cover this window */
67 #define WIN_STATE_STICKY (1<<0) /*everyone knows sticky*/
68 #define WIN_STATE_MINIMIZED (1<<1) /*Reserved - definition is unclear*/
69 #define WIN_STATE_MAXIMIZED_VERT (1<<2) /*window in maximized V state*/
70 #define WIN_STATE_MAXIMIZED_HORIZ (1<<3) /*window in maximized H state*/
71 #define WIN_STATE_HIDDEN (1<<4) /*not on taskbar but window visible*/
72 #define WIN_STATE_SHADED (1<<5) /*shaded (MacOS / Afterstep style)*/
73 /* these are bogus states defined in "the spec" */
74 #define WIN_STATE_HID_WORKSPACE (1<<6) /*not on current desktop*/
75 #define WIN_STATE_HID_TRANSIENT (1<<7) /*owner of transient is hidden*/
76 #define WIN_STATE_FIXED_POSITION (1<<8) /*window is fixed in position even*/
77 #define WIN_STATE_ARRANGE_IGNORE (1<<9) /*ignore for auto arranging*/
80 #define WIN_LAYER_DESKTOP 0
81 #define WIN_LAYER_BELOW 2
82 #define WIN_LAYER_NORMAL 4
83 #define WIN_LAYER_ONTOP 6
84 #define WIN_LAYER_DOCK 8
85 #define WIN_LAYER_ABOVE_DOCK 10
86 #define WIN_LAYER_MENU 12
90 static Atom _XA_WIN_SUPPORTING_WM_CHECK = 0;
91 static Atom _XA_WIN_PROTOCOLS;
92 static Atom _XA_WIN_LAYER;
93 static Atom _XA_WIN_STATE;
94 static Atom _XA_WIN_HINTS;
95 static Atom _XA_WIN_APP_STATE;
96 static Atom _XA_WIN_EXPANDED_SIZE;
97 static Atom _XA_WIN_ICONS;
98 static Atom _XA_WIN_WORKSPACE;
99 static Atom _XA_WIN_WORKSPACE_COUNT;
100 static Atom _XA_WIN_WORKSPACE_NAMES;
101 static Atom _XA_WIN_CLIENT_LIST;
102 static Atom _XA_WIN_DESKTOP_BUTTON_PROXY;
105 void
106 wGNOMEInitStuff(WScreen *scr)
108 Atom supportedStuff[10];
109 int count;
111 if (!_XA_WIN_SUPPORTING_WM_CHECK) {
113 _XA_WIN_SUPPORTING_WM_CHECK =
114 XInternAtom(dpy, "_WIN_SUPPORTING_WM_CHECK", False);
116 _XA_WIN_PROTOCOLS = XInternAtom(dpy, "_WIN_PROTOCOLS", False);
118 _XA_WIN_LAYER = XInternAtom(dpy, "_WIN_LAYER", False);
120 _XA_WIN_STATE = XInternAtom(dpy, "_WIN_STATE", False);
122 _XA_WIN_HINTS = XInternAtom(dpy, "_WIN_HINTS", False);
124 _XA_WIN_APP_STATE = XInternAtom(dpy, "_WIN_APP_STATE", False);
126 _XA_WIN_EXPANDED_SIZE = XInternAtom(dpy, "_WIN_EXPANDED_SIZE", False);
128 _XA_WIN_ICONS = XInternAtom(dpy, "_WIN_ICONS", False);
130 _XA_WIN_WORKSPACE = XInternAtom(dpy, "_WIN_WORKSPACE", False);
132 _XA_WIN_WORKSPACE_COUNT =
133 XInternAtom(dpy, "_WIN_WORKSPACE_COUNT", False);
135 _XA_WIN_WORKSPACE_NAMES =
136 XInternAtom(dpy, "_WIN_WORKSPACE_NAMES", False);
138 _XA_WIN_CLIENT_LIST = XInternAtom(dpy, "_WIN_CLIENT_LIST", False);
140 _XA_WIN_DESKTOP_BUTTON_PROXY =
141 XInternAtom(dpy, "_WIN_DESKTOP_BUTTON_PROXY", False);
144 /* I'd rather use the ICCCM 2.0 mechanisms, but
145 * since some people prefer to reinvent the wheel instead of
146 * conforming to standards... */
148 /* setup the "We're compliant, you idiot!" hint */
150 /* why XA_CARDINAL instead of XA_WINDOW? */
151 XChangeProperty(dpy, scr->root_win, _XA_WIN_SUPPORTING_WM_CHECK,
152 XA_CARDINAL, 32, PropModeReplace,
153 (unsigned char*)&scr->no_focus_win, 1);
155 XChangeProperty(dpy, scr->no_focus_win, _XA_WIN_SUPPORTING_WM_CHECK,
156 XA_CARDINAL, 32, PropModeReplace,
157 (unsigned char*)&scr->no_focus_win, 1);
160 /* setup the "desktop button proxy" thing */
161 XChangeProperty(dpy, scr->root_win, _XA_WIN_DESKTOP_BUTTON_PROXY,
162 XA_CARDINAL, 32, PropModeReplace,
163 (unsigned char*)&scr->no_focus_win, 1);
164 XChangeProperty(dpy, scr->no_focus_win, _XA_WIN_DESKTOP_BUTTON_PROXY,
165 XA_CARDINAL, 32, PropModeReplace,
166 (unsigned char*)&scr->no_focus_win, 1);
169 /* setup the list of supported protocols */
170 count = 0;
171 supportedStuff[count++] = _XA_WIN_LAYER;
172 supportedStuff[count++] = _XA_WIN_STATE;
173 supportedStuff[count++] = _XA_WIN_HINTS;
174 supportedStuff[count++] = _XA_WIN_APP_STATE;
175 supportedStuff[count++] = _XA_WIN_EXPANDED_SIZE;
176 supportedStuff[count++] = _XA_WIN_ICONS;
177 supportedStuff[count++] = _XA_WIN_WORKSPACE;
178 supportedStuff[count++] = _XA_WIN_WORKSPACE_COUNT;
179 supportedStuff[count++] = _XA_WIN_WORKSPACE_NAMES;
180 supportedStuff[count++] = _XA_WIN_CLIENT_LIST;
182 XChangeProperty(dpy, scr->root_win, _XA_WIN_PROTOCOLS, XA_ATOM, 32,
183 PropModeReplace, (unsigned char*)supportedStuff, count);
185 XFlush(dpy);
189 void
190 wGNOMEUpdateClientListHint(WScreen *scr)
192 WWindow *wwin;
193 Window *windows;
194 int count;
196 windows = malloc(sizeof(Window)*scr->window_count);
197 if (!windows) {
198 wwarning(_("out of memory while updating GNOME hints"));
199 return;
202 count = 0;
203 wwin = scr->focused_window;
204 while (wwin) {
205 if (!wwin->flags.internal_window) {
207 windows[count++] = wwin->client_win;
210 wwin = wwin->prev;
213 XChangeProperty(dpy, scr->root_win, _XA_WIN_CLIENT_LIST, XA_CARDINAL, 32,
214 PropModeReplace, (unsigned char *)windows, count);
216 wfree(windows);
217 XFlush(dpy);
221 void
222 wGNOMEUpdateWorkspaceHints(WScreen *scr)
224 long val;
226 val = scr->workspace_count;
228 XChangeProperty(dpy, scr->root_win, _XA_WIN_WORKSPACE_COUNT, XA_CARDINAL,
229 32, PropModeReplace, (unsigned char*)&val, 1);
231 wGNOMEUpdateWorkspaceNamesHint(scr);
235 void
236 wGNOMEUpdateWorkspaceNamesHint(WScreen *scr)
238 char *wsNames[MAX_WORKSPACES];
239 XTextProperty textProp;
240 int i;
242 for (i = 0; i < scr->workspace_count; i++) {
243 wsNames[i] = scr->workspaces[i]->name;
246 if (XStringListToTextProperty(wsNames, scr->workspace_count, &textProp)) {
247 XSetTextProperty(dpy, scr->root_win, &textProp,
248 _XA_WIN_WORKSPACE_NAMES);
249 XFree(textProp.value);
254 void
255 wGNOMEUpdateCurrentWorkspaceHint(WScreen *scr)
257 long val;
259 val = scr->current_workspace;
261 XChangeProperty(dpy, scr->root_win, _XA_WIN_WORKSPACE, XA_CARDINAL,
262 32, PropModeReplace, (unsigned char*)&val, 1);
266 static int
267 getWindowLevel(int layer)
269 int level;
271 if (layer <= WIN_LAYER_DESKTOP)
272 level = WMDesktopLevel;
273 else if (layer <= WIN_LAYER_BELOW)
274 level = WMSunkenLevel;
275 else if (layer <= WIN_LAYER_NORMAL)
276 level = WMNormalLevel;
277 else if (layer <= WIN_LAYER_ONTOP)
278 level = WMFloatingLevel;
279 else if (layer <= WIN_LAYER_DOCK)
280 level = WMDockLevel;
281 else if (layer <= WIN_LAYER_ABOVE_DOCK)
282 level = WMSubmenuLevel;
283 else if (layer <= WIN_LAYER_MENU)
284 level = WMMainMenuLevel;
285 else
286 level = WMOuterSpaceLevel;
288 return level;
292 Bool
293 wGNOMECheckClientHints(WWindow *wwin, int *layer, int *workspace)
295 Atom type_ret;
296 int fmt_ret;
297 unsigned long nitems_ret;
298 unsigned long bytes_after_ret;
299 long flags, val, *data = 0;
300 Bool hasHints = False;
302 /* hints */
304 if (XGetWindowProperty(dpy, wwin->client_win, _XA_WIN_HINTS, 0, 1, False,
305 /* should be XA_INTEGER, but spec is broken */
306 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
307 &bytes_after_ret,
308 (unsigned char**)&data)==Success && data) {
309 flags = *data;
311 XFree(data);
313 if (flags & (WIN_HINTS_SKIP_FOCUS|WIN_HINTS_SKIP_WINLIST
314 |WIN_HINTS_SKIP_TASKBAR)) {
315 wwin->client_flags.skip_window_list = 1;
318 /* client reserved area, for the panel */
319 if (flags & (WIN_HINTS_DO_NOT_COVER)) {
320 WReservedArea *area;
322 area = malloc(sizeof(WReservedArea));
323 if (!area) {
324 wwarning(_("out of memory while updating GNOME hints"));
325 } else {
326 XWindowAttributes wattribs;
328 XGetWindowAttributes(dpy, wwin->client_win, &wattribs);
329 wClientGetNormalHints(wwin, &wattribs, False,
330 &area->area.x1, &area->area.y1,
331 &area->area.x2, &area->area.y2);
332 area->area.x2 = area->area.x2 + area->area.x1;
333 area->area.y2 = area->area.y2 + area->area.y1;
335 area->window = wwin->client_win;
337 area->next = wwin->screen_ptr->reservedAreas;
338 wwin->screen_ptr->reservedAreas = area;
340 wScreenUpdateUsableArea(wwin->screen_ptr);
343 hasHints = True;
346 /* layer */
347 if (XGetWindowProperty(dpy, wwin->client_win, _XA_WIN_LAYER, 0, 1, False,
348 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
349 &bytes_after_ret,
350 (unsigned char**)&data)==Success && data) {
351 val = *data;
353 XFree(data);
355 *layer = getWindowLevel(val);
356 hasHints = True;
359 /* workspace */
360 if (XGetWindowProperty(dpy, wwin->client_win, _XA_WIN_WORKSPACE, 0, 1,
361 False, XA_CARDINAL, &type_ret, &fmt_ret,
362 &nitems_ret, &bytes_after_ret,
363 (unsigned char**)&data)==Success && data) {
364 val = *data;
366 XFree(data);
368 if (val > 0)
369 *workspace = val;
370 hasHints = True;
373 /* reserved area */
374 if (XGetWindowProperty(dpy, wwin->client_win, _XA_WIN_EXPANDED_SIZE, 0, 1,
375 False, XA_CARDINAL, &type_ret, &fmt_ret,
376 &nitems_ret, &bytes_after_ret,
377 (unsigned char**)&data)==Success && data) {
378 WReservedArea *area;
380 area = malloc(sizeof(WReservedArea));
381 if (!area) {
382 wwarning(_("out of memory while updating GNOME hints"));
383 } else {
384 area->area.x1 = data[0];
385 area->area.y1 = data[1];
386 area->area.x2 = data[2] - data[0];
387 area->area.y2 = data[3] - data[1];
388 XFree(data);
390 area->window = wwin->client_win;
393 area->next = wwin->screen_ptr->reservedAreas;
394 wwin->screen_ptr->reservedAreas = area;
396 wScreenUpdateUsableArea(wwin->screen_ptr);
397 hasHints = True;
400 return hasHints;
404 Bool
405 wGNOMECheckInitialClientState(WWindow *wwin)
407 Atom type_ret;
408 int fmt_ret;
409 unsigned long nitems_ret;
410 unsigned long bytes_after_ret;
411 long flags, *data = 0;
413 if (XGetWindowProperty(dpy, wwin->client_win, _XA_WIN_STATE, 0, 1, False,
414 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
415 &bytes_after_ret,
416 (unsigned char**)&data)!=Success || !data)
417 return False;
419 flags = *data;
421 XFree(data);
423 if (flags & WIN_STATE_STICKY)
424 wwin->client_flags.omnipresent = 1;
426 if (flags & (WIN_STATE_MAXIMIZED_VERT|WIN_STATE_MAXIMIZED_HORIZ)) {
428 if (flags & WIN_STATE_MAXIMIZED_VERT)
429 wwin->flags.maximized |= MAX_VERTICAL;
431 if (flags & WIN_STATE_MAXIMIZED_HORIZ)
432 wwin->flags.maximized |= MAX_HORIZONTAL;
435 if (flags & WIN_STATE_SHADED)
436 wwin->flags.shaded = 1;
438 return True;
442 void
443 wGNOMEUpdateClientStateHint(WWindow *wwin, Bool changedWorkspace)
445 long val;
446 long flags = 0;
448 if (changedWorkspace) {
449 val = wwin->frame->workspace;
451 XChangeProperty(dpy, wwin->client_win, _XA_WIN_WORKSPACE, XA_CARDINAL,
452 32, PropModeReplace, (unsigned char*)&val, 1);
454 if (val != wwin->screen_ptr->current_workspace)
455 flags |= WIN_STATE_HID_WORKSPACE;
458 if (IS_OMNIPRESENT(wwin))
459 flags |= WIN_STATE_STICKY;
461 if (wwin->flags.miniaturized)
462 flags |= WIN_STATE_MINIMIZED;
464 if (wwin->flags.maximized & MAX_VERTICAL)
465 flags |= WIN_STATE_MAXIMIZED_VERT;
467 if (wwin->flags.maximized & MAX_HORIZONTAL)
468 flags |= WIN_STATE_MAXIMIZED_HORIZ;
470 if (wwin->flags.shaded)
471 flags |= WIN_STATE_SHADED;
473 if (wwin->transient_for != None) {
474 WWindow *owner = wWindowFor(wwin->transient_for);
476 if (owner && !owner->flags.mapped)
477 flags |= WIN_STATE_HID_TRANSIENT;
480 /* ? */
481 if (wwin->flags.hidden)
482 flags |= WIN_STATE_HIDDEN;
484 XChangeProperty(dpy, wwin->client_win, _XA_WIN_STATE, XA_CARDINAL,
485 32, PropModeReplace, (unsigned char*)&flags, 1);
489 Bool
490 wGNOMEProcessClientMessage(XClientMessageEvent *event)
492 WScreen *scr;
493 WWindow *wwin;
494 Bool done = True;
496 scr = wScreenForWindow(event->window);
497 if (scr) {
498 /* generic client messages */
499 if (event->message_type == _XA_WIN_WORKSPACE) {
500 wWorkspaceChange(scr, event->data.l[0]);
501 } else {
502 done = False;
505 if (done)
506 return True;
509 /* window specific client messages */
511 wwin = wWindowFor(event->window);
512 if (!wwin)
513 return False;
515 if (event->message_type == _XA_WIN_LAYER) {
516 int level = getWindowLevel(event->data.l[0]);
518 if (WINDOW_LEVEL(wwin) != level) {
519 ChangeStackingLevel(wwin->frame->core, level);
521 } else if (event->message_type == _XA_WIN_STATE) {
522 int flags, mask;
523 Bool updateWindowList = False;
524 int maximize = 0;
526 mask = event->data.l[0];
527 flags = event->data.l[1];
529 if (mask & WIN_STATE_STICKY) {
530 if ((flags & WIN_STATE_STICKY) != WFLAGP(wwin, omnipresent)) {
531 wwin->client_flags.omnipresent = (flags & WIN_STATE_STICKY)!=0;
532 wGNOMEUpdateClientStateHint(wwin, False);
533 updateWindowList = True;
537 if (mask & WIN_STATE_MAXIMIZED_VERT) {
538 if (flags & WIN_STATE_MAXIMIZED_VERT)
539 maximize = MAX_VERTICAL;
540 else
541 maximize = 0;
542 } else {
543 maximize = wwin->flags.maximized & MAX_VERTICAL;
546 if (mask & WIN_STATE_MAXIMIZED_HORIZ) {
547 if (flags & WIN_STATE_MAXIMIZED_HORIZ)
548 maximize |= MAX_HORIZONTAL;
549 else
550 maximize |= 0;
551 } else {
552 maximize |= wwin->flags.maximized & MAX_HORIZONTAL;
555 if (maximize != wwin->flags.maximized) {
556 #define both (MAX_HORIZONTAL|MAX_VERTICAL)
557 if (!(maximize & both) && (wwin->flags.maximized & both)) {
558 wUnmaximizeWindow(wwin);
560 if ((maximize & both) && !(wwin->flags.maximized & both)) {
561 wMaximizeWindow(wwin, maximize);
563 updateWindowList = False;
564 #undef both
567 if (mask & WIN_STATE_SHADED) {
568 if ((flags & WIN_STATE_SHADED) != wwin->flags.shaded) {
569 if (wwin->flags.shaded)
570 wUnshadeWindow(wwin);
571 else
572 wShadeWindow(wwin);
573 updateWindowList = False;
577 if (updateWindowList) {
578 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
580 } else if (event->message_type == _XA_WIN_WORKSPACE) {
582 if (event->data.l[0] != wwin->frame->workspace) {
583 wWindowChangeWorkspace(wwin, event->data.l[0]);
585 } else {
586 done = False;
589 return done;
593 Bool
594 wGNOMEProxyizeButtonEvent(WScreen *scr, XEvent *event)
596 if (event->type == ButtonPress)
597 XUngrabPointer(dpy, CurrentTime);
598 XSendEvent(dpy, scr->no_focus_win, False, SubstructureNotifyMask, event);
600 return True;
604 void
605 wGNOMERemoveClient(WWindow *wwin)
607 int flag = 0;
608 WReservedArea *area;
610 wGNOMEUpdateClientListHint(wwin->screen_ptr);
612 area = wwin->screen_ptr->reservedAreas;
614 if (area) {
615 if (area->window == wwin->client_win) {
616 wwin->screen_ptr->reservedAreas = area->next;
617 wfree(area);
618 flag = 1;
619 } else {
620 while (area->next && area->next->window != wwin->client_win)
621 area = area->next;
623 if (area->next) {
624 WReservedArea *next;
626 next = area->next->next;
627 wfree(area->next);
628 area->next = next;
630 flag = 1;
635 if (flag) {
636 wScreenUpdateUsableArea(wwin->screen_ptr);
641 #endif /* GNOME_STUFF */