WPrefs appearance stuff update (shows preview)
[wmaker-crm.git] / src / client.c
blob9052b1e2eb02edc8b37e4c5343daa428fee7a05c
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
21 #include "wconfig.h"
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25 #include <X11/Xatom.h>
26 #ifdef SHAPE
27 #include <X11/extensions/shape.h>
28 #endif
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
34 #include "WindowMaker.h"
35 #include "wcore.h"
36 #include "framewin.h"
37 #include "window.h"
38 #include "properties.h"
39 #include "actions.h"
40 #include "icon.h"
41 #include "client.h"
42 #include "funcs.h"
43 #include "stacking.h"
44 #include "appicon.h"
45 #include "appmenu.h"
46 #ifdef KWM_HINTS
47 #include "kwm.h"
48 #endif
50 /****** Global Variables ******/
52 /* contexts */
53 extern XContext wWinContext;
55 extern Atom _XA_WM_STATE;
56 extern Atom _XA_WM_PROTOCOLS;
57 extern Atom _XA_WM_COLORMAP_WINDOWS;
59 extern Atom _XA_WINDOWMAKER_MENU;
61 extern Atom _XA_GNUSTEP_WM_ATTR;
62 extern Atom _XA_GNUSTEP_WM_RESIZEBAR;
64 #ifdef SHAPE
65 extern Bool wShapeSupported;
66 #endif
70 *--------------------------------------------------------------------
71 * wClientRestore--
72 * Reparent the window back to the root window.
74 *--------------------------------------------------------------------
76 void
77 wClientRestore(WWindow *wwin)
79 #if 0
80 int gx, gy;
82 wClientGetGravityOffsets(wwin, &gx, &gy);
83 /* set the positio of the frame on screen */
84 wwin->frame_x -= gx * FRAME_BORDER_WIDTH;
85 wwin->frame_y -= gy * FRAME_BORDER_WIDTH;
86 /* if gravity is to the south, account for the border sizes */
87 if (gy > 0)
88 wwin->frame_y += (wwin->frame->top_width + wwin->frame->bottom_width);
89 #endif
90 XSetWindowBorderWidth(dpy, wwin->client_win, wwin->old_border_width);
91 XReparentWindow(dpy, wwin->client_win, wwin->screen_ptr->root_win,
92 wwin->frame_x, wwin->frame_y);
94 /* don't let the window get iconified after restart */
96 if (wwin->flags.shaded)
97 wClientSetState(wwin, NormalState, None);
103 *----------------------------------------------------------------------
104 * wClientSetState--
105 * Set the state of the client window to one of the window
106 * states defined in ICCCM (Iconic, Withdrawn, Normal)
108 * Side effects:
109 * The WM_STATE property of the window is updated as well as the
110 * WWindow.state variable.
111 *----------------------------------------------------------------------
113 void
114 wClientSetState(WWindow *wwin, int state, Window icon_win)
116 CARD32 data[2];
118 wwin->state = state;
120 data[0] = (unsigned long) state;
121 data[1] = (unsigned long) icon_win;
123 XChangeProperty(dpy, wwin->client_win, _XA_WM_STATE, _XA_WM_STATE, 32,
124 PropModeReplace, (unsigned char *) data, 2);
128 void
129 wClientGetGravityOffsets(WWindow *wwin, int *ofs_x, int *ofs_y)
131 switch (wwin->normal_hints->win_gravity) {
132 case ForgetGravity:
133 case CenterGravity:
134 case StaticGravity:
135 *ofs_x = 0;
136 *ofs_y = 0;
137 break;
138 case NorthWestGravity:
139 *ofs_x = -1;
140 *ofs_y = -1;
141 break;
142 case NorthGravity:
143 *ofs_x = 0;
144 *ofs_y = -1;
145 break;
146 case NorthEastGravity:
147 *ofs_x = 1;
148 *ofs_y = -1;
149 break;
150 case WestGravity:
151 *ofs_x = -1;
152 *ofs_y = 0;
153 break;
154 case EastGravity:
155 *ofs_x = 1;
156 *ofs_y = 0;
157 break;
158 case SouthWestGravity:
159 *ofs_x = -1;
160 *ofs_y = 1;
161 break;
162 case SouthGravity:
163 *ofs_x = 0;
164 *ofs_y = 1;
165 break;
166 case SouthEastGravity:
167 *ofs_x = 1;
168 *ofs_y = 1;
169 break;
175 void
176 wClientConfigure(WWindow *wwin, XConfigureRequestEvent *xcre)
178 XWindowChanges xwc;
179 int nx, ny, nwidth, nheight;
180 int ofs_x, ofs_y;
182 if (wwin==NULL) {
184 * configure a window that was not mapped by us
186 xwc.x = xcre->x;
187 xwc.y = xcre->y;
188 xwc.width = xcre->width;
189 xwc.height = xcre->height;
190 xwc.border_width = xcre->border_width;
191 xwc.stack_mode = xcre->detail;
192 xwc.sibling = xcre->above;
193 XConfigureWindow(dpy, xcre->window, xcre->value_mask, &xwc);
194 return;
196 #ifdef SHAPE
197 if (wShapeSupported) {
198 int junk;
199 unsigned int ujunk;
200 int b_shaped;
202 XShapeSelectInput(dpy, wwin->client_win, ShapeNotifyMask);
203 XShapeQueryExtents(dpy, wwin->client_win, &b_shaped, &junk, &junk,
204 &ujunk, &ujunk, &junk, &junk, &junk, &ujunk,
205 &ujunk);
206 wwin->flags.shaped = b_shaped;
208 #endif
209 if (xcre->value_mask & CWStackMode) {
210 WObjDescriptor *desc;
211 WWindow *sibling;
213 if ((xcre->value_mask & CWSibling) &&
214 (XFindContext(dpy, xcre->above, wWinContext,
215 (XPointer *)&desc) == XCSUCCESS)
216 && (desc->parent_type==WCLASS_WINDOW)) {
217 sibling=desc->parent;
218 xwc.sibling = sibling->frame->core->window;
219 } else {
220 xwc.sibling = xcre->above;
222 xwc.stack_mode = xcre->detail;
223 XConfigureWindow(dpy, wwin->frame->core->window,
224 xcre->value_mask & (CWSibling | CWStackMode), &xwc);
225 /* fix stacking order */
226 RemakeStackList(wwin->screen_ptr);
229 wClientGetGravityOffsets(wwin, &ofs_x, &ofs_y);
231 if (xcre->value_mask & CWBorderWidth) {
232 wwin->old_border_width = xcre->border_width;
235 if (!wwin->flags.shaded) {
236 /* If the window is shaded, wrong height will be set for the window */
237 if (xcre->value_mask & CWX)
238 nx = xcre->x - FRAME_BORDER_WIDTH;
239 else
240 nx = wwin->frame_x;
242 if (xcre->value_mask & CWY)
243 ny = xcre->y - ((ofs_y < 0) ? 0 : wwin->frame->top_width)
244 - FRAME_BORDER_WIDTH;
245 else
246 ny = wwin->frame_y;
248 if (xcre->value_mask & CWWidth)
249 nwidth = xcre->width;
250 else
251 nwidth = wwin->frame->core->width;
253 if (xcre->value_mask & CWHeight)
254 nheight = xcre->height;
255 else
256 nheight = wwin->frame->core->height - wwin->frame->top_width - wwin->frame->bottom_width;
258 wWindowConfigure(wwin, nx, ny, nwidth, nheight);
259 wwin->old_geometry.x = nx;
260 wwin->old_geometry.y = ny;
261 wwin->old_geometry.width = nwidth;
262 wwin->old_geometry.height = nheight;
267 void
268 wClientSendProtocol(WWindow *wwin, Atom protocol, Time time)
270 XEvent event;
272 event.xclient.type = ClientMessage;
273 event.xclient.message_type = _XA_WM_PROTOCOLS;
274 event.xclient.format = 32;
275 event.xclient.display = dpy;
276 event.xclient.window = wwin->client_win;
277 event.xclient.data.l[0] = protocol;
278 event.xclient.data.l[1] = time;
279 event.xclient.data.l[2] = 0;
280 event.xclient.data.l[3] = 0;
281 XSendEvent(dpy, wwin->client_win, False, NoEventMask, &event);
282 XSync(dpy, False);
287 void
288 wClientKill(WWindow *wwin)
290 XKillClient(dpy, wwin->client_win);
292 XFlush(dpy);
296 *----------------------------------------------------------------------
297 * wClientCheckProperty--
298 * Handles PropertyNotify'es, verifying which property was
299 * changed and updating internal state according to that, like redrawing
300 * the icon title when it is changed.
302 * Side effects:
303 * Depends on the changed property.
305 * TODO: CLIENT_LEADER, _GNUSTEP_WM_ATTR
306 *----------------------------------------------------------------------
308 void
309 wClientCheckProperty(WWindow *wwin, XPropertyEvent *event)
311 XWindowAttributes attribs;
312 XWMHints *new_hints;
313 int i, g1, g2;
314 char *tmp;
316 switch (event->atom) {
317 case XA_WM_NAME:
318 /* window title was changed */
319 if (wwin->frame) {
320 if (!wFetchName(dpy, wwin->client_win, &tmp)) {
321 /* the hint was removed */
322 tmp = wstrdup(DEF_WINDOW_TITLE);
323 #ifdef KWM_HINTS
324 wKWMSendEventMessage(wwin, WKWMChangedClient);
325 #endif
327 if (wFrameWindowChangeTitle(wwin->frame, tmp)) {
328 /* only update the menu if the title has actually changed */
329 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE);
330 #ifdef KWM_HINTS
331 wKWMSendEventMessage(wwin, WKWMChangedClient);
332 #endif
334 if (tmp)
335 free(tmp);
337 break;
339 case XA_WM_ICON_NAME:
340 #ifdef KWM_HINTS
341 wKWMSendEventMessage(wwin, WKWMChangedClient);
342 #endif
343 if (!wwin->icon)
344 break;
345 else {
346 char *new_title;
348 /* icon title was changed */
349 wGetIconName(dpy, wwin->client_win, &new_title);
350 wIconChangeTitle(wwin->icon, new_title);
352 break;
354 case XA_WM_COMMAND:
355 if (wwin->main_window!=None) {
356 WApplication *wapp = wApplicationOf(wwin->main_window);
357 char **argv;
358 int argc;
360 if (wapp && wapp->app_icon) {
361 if (wapp->app_icon->command!=NULL)
362 free(wapp->app_icon->command);
364 if (XGetCommand(dpy, wwin->main_window, &argv, &argc)) {
365 if (argc > 0 && argv != NULL)
366 wapp->app_icon->command = FlattenStringList(argv,argc);
367 if (argv) {
368 XFreeStringList(argv);
373 break;
375 case XA_WM_HINTS:
376 /* WM_HINTS */
378 new_hints = XGetWMHints(dpy, wwin->client_win);
380 /* group leader update
382 * This means that the window is setting the leader after
383 * it was mapped, changing leaders or removing the leader.
385 * Valid state transitions are:
387 * _1 __2
388 * / \ / \
389 * v | v |
390 * (GC) (GC')
391 * / ^ / ^
392 * 3| |4 5| |6
393 * | | | |
394 * v / v /
395 * (G'C) (G'C')
397 * Where G is the window_group hint, C is CLIENT_LEADER property
398 * and ' indicates the hint is unset.
400 * 1,2 - change group leader to new value of window_group
401 * 3 - change leader to value of CLIENT_LEADER
402 * 4 - change leader to value of window_group
403 * 5 - destroy application
404 * 6 - create application
406 if (new_hints && (new_hints->flags & WindowGroupHint)
407 && new_hints->window_group!=None) {
408 g2 = 1;
409 } else {
410 g2 = 0;
412 if (wwin->wm_hints && (wwin->wm_hints->flags & WindowGroupHint)
413 && wwin->wm_hints->window_group!=None) {
414 g1 = 1;
415 } else {
416 g1 = 0;
419 if (wwin->client_leader) {
420 if (g1 && g2
421 && wwin->wm_hints->window_group!=new_hints->window_group) {
422 i = 1;
423 } else if (g1 && !g2) {
424 i = 3;
425 } else if (!g1 && g2) {
426 i = 4;
427 } else {
428 i = 0;
430 } else {
431 if (g1 && g2
432 && wwin->wm_hints->window_group!=new_hints->window_group) {
433 i = 2;
434 } else if (g1 && !g2) {
435 i = 5;
436 } else if (!g1 && g2) {
437 i = 6;
438 } else {
439 i = 0;
443 if (wwin->wm_hints)
444 XFree(wwin->wm_hints);
446 wwin->wm_hints = new_hints;
448 /* do action according to state transition */
449 switch (i) {
450 /* 3 - change leader to value of CLIENT_LEADER */
451 case 3:
452 wApplicationDestroy(wApplicationOf(wwin->main_window));
453 wwin->main_window = wwin->client_leader;
454 wwin->group_id = None;
455 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
456 break;
458 /* 1,2,4 - change leader to new value of window_group */
459 case 1:
460 case 2:
461 case 4:
462 wApplicationDestroy(wApplicationOf(wwin->main_window));
463 wwin->main_window = new_hints->window_group;
464 wwin->group_id = wwin->main_window;
465 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
466 break;
468 /* 5 - destroy application */
469 case 5:
470 wApplicationDestroy(wApplicationOf(wwin->main_window));
471 wwin->main_window = None;
472 wwin->group_id = None;
473 break;
475 /* 6 - create application */
476 case 6:
477 wwin->main_window = new_hints->window_group;
478 wwin->group_id = wwin->main_window;
479 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
480 break;
482 #ifdef DEBUG
483 if (i) {
484 printf("window leader update caused state transition %i\n",i);
486 #endif
488 if (wwin->wm_hints) {
489 /* update icon */
490 if ((wwin->wm_hints->flags & IconPixmapHint)
491 || (wwin->wm_hints->flags & IconWindowHint)) {
492 WApplication *wapp;
494 if (wwin->flags.miniaturized) {
495 wIconUpdate(wwin->icon);
497 wapp = wApplicationOf(wwin->main_window);
498 if (wapp && wapp->app_icon) {
499 wIconUpdate(wapp->app_icon->icon);
501 #ifdef KWM_HINTS
502 wKWMSendEventMessage(wwin, WKWMIconChange);
503 #endif
506 if (wwin->wm_hints->flags & UrgencyHint)
507 wwin->flags.urgent = 1;
508 else
509 wwin->flags.urgent = 0;
510 } else {
511 wwin->group_id = None;
513 break;
515 case XA_WM_NORMAL_HINTS:
516 /* normal (geometry) hints */
518 int foo;
519 unsigned bar;
521 XGetWindowAttributes(dpy, wwin->client_win, &attribs);
522 wClientGetNormalHints(wwin, &attribs, False, &foo, &foo,
523 &bar, &bar);
524 /* TODO: should we check for consistency of the current
525 * size against the new geometry hints? */
527 break;
529 case XA_WM_TRANSIENT_FOR:
531 Window new_owner;
532 WWindow *owner;
534 if (!XGetTransientForHint(dpy, wwin->client_win, &new_owner)) {
535 new_owner = None;
536 } else {
537 if (new_owner==0 || new_owner == wwin->client_win) {
538 new_owner = wwin->screen_ptr->root_win;
541 if (new_owner!=wwin->transient_for) {
542 owner = wWindowFor(wwin->transient_for);
543 if (owner) {
544 if (owner->flags.semi_focused) {
545 owner->flags.semi_focused = 0;
546 if ((owner->flags.mapped || owner->flags.shaded)
547 && owner->frame)
548 wFrameWindowPaint(owner->frame);
551 owner = wWindowFor(new_owner);
552 if (owner) {
553 if (!owner->flags.semi_focused) {
554 owner->flags.semi_focused = 1;
555 if ((owner->flags.mapped || owner->flags.shaded)
556 && owner->frame)
557 wFrameWindowPaint(owner->frame);
560 wwin->transient_for = new_owner;
561 if (new_owner==None) {
562 if (WFLAGP(wwin, no_miniaturizable)) {
563 WSETUFLAG(wwin, no_miniaturizable, 0);
564 WSETUFLAG(wwin, no_miniaturize_button, 0);
565 if (wwin->frame)
566 wWindowConfigureBorders(wwin);
568 } else if (!WFLAGP(wwin, no_miniaturizable)) {
569 WSETUFLAG(wwin, no_miniaturizable, 1);
570 WSETUFLAG(wwin, no_miniaturize_button, 1);
571 if (wwin->frame)
572 wWindowConfigureBorders(wwin);
576 break;
578 default:
579 if (event->atom==_XA_WM_PROTOCOLS) {
581 PropGetProtocols(wwin->client_win, &wwin->protocols);
583 WSETUFLAG(wwin, kill_close, !wwin->protocols.DELETE_WINDOW);
585 if (wwin->frame)
586 wWindowUpdateButtonImages(wwin);
588 } else if (event->atom==_XA_WM_COLORMAP_WINDOWS) {
590 GetColormapWindows(wwin);
591 wColormapInstallForWindow(wwin->screen_ptr, wwin);
593 } else if (event->atom==_XA_WINDOWMAKER_MENU) {
594 WApplication *wapp;
596 wapp = wApplicationOf(wwin->main_window);
597 if (wapp) {
598 if (wapp->menu) {
599 /* update menu */
600 /* TODO: remake appmenu update */
601 wAppMenuDestroy(wapp->menu);
603 wapp->menu = wAppMenuGet(wwin->screen_ptr, wwin->main_window);
604 /* make the appmenu be mapped */
605 wSetFocusTo(wwin->screen_ptr, NULL);
606 wSetFocusTo(wwin->screen_ptr,
607 wwin->screen_ptr->focused_window);
609 } else if (event->atom==_XA_GNUSTEP_WM_ATTR) {
610 GNUstepWMAttributes *attr;
612 PropGetGNUstepWMAttr(wwin->client_win, &attr);
614 wWindowUpdateGNUstepAttr(wwin, attr);
616 XFree(attr);
617 } else {
618 #ifdef KWM_HINTS
619 Bool done;
621 done = wKWMCheckClientHintChange(wwin, event);
622 #endif /* KWM_HINTS */
629 *----------------------------------------------------------------------
630 * wClientGetNormalHints--
631 * Get size (normal) hints and a default geometry for the client
632 * window. The hints are also checked for inconsistency. If geometry is
633 * True, the returned data will account for client specified initial
634 * geometry.
636 * Side effects:
637 * normal_hints is filled with valid data.
638 *----------------------------------------------------------------------
640 void
641 wClientGetNormalHints(WWindow *wwin, XWindowAttributes *wattribs, Bool geometry,
642 int *x, int *y, unsigned *width, unsigned *height)
644 int pre_icccm = 0; /* not used */
646 /* find a position for the window */
647 if (!wwin->normal_hints)
648 wwin->normal_hints = XAllocSizeHints();
650 if (!PropGetNormalHints(wwin->client_win, wwin->normal_hints, &pre_icccm)) {
651 wwin->normal_hints->flags = 0;
653 *x = wattribs->x;
654 *y = wattribs->y;
656 *width = wattribs->width;
657 *height = wattribs->height;
659 if (!(wwin->normal_hints->flags & PWinGravity)) {
660 wwin->normal_hints->win_gravity = NorthWestGravity;
662 if (!(wwin->normal_hints->flags & PMinSize)) {
663 wwin->normal_hints->min_width = MIN_WINDOW_SIZE;
664 wwin->normal_hints->min_height = MIN_WINDOW_SIZE;
666 if (!(wwin->normal_hints->flags & PBaseSize)) {
667 wwin->normal_hints->base_width = 0;
668 wwin->normal_hints->base_height = 0;
670 if (!(wwin->normal_hints->flags & PMaxSize)) {
671 wwin->normal_hints->max_width = wwin->screen_ptr->scr_width*2;
672 wwin->normal_hints->max_height = wwin->screen_ptr->scr_height*2;
675 /* some buggy apps set weird hints.. */
676 if (wwin->normal_hints->max_width < wwin->normal_hints->min_width)
677 wwin->normal_hints->max_width = wwin->normal_hints->min_width;
679 if (wwin->normal_hints->max_height < wwin->normal_hints->min_height)
680 wwin->normal_hints->max_height = wwin->normal_hints->min_height;
682 if (!(wwin->normal_hints->flags & PResizeInc)) {
683 wwin->normal_hints->width_inc = 1;
684 wwin->normal_hints->height_inc = 1;
685 } else {
686 if (wwin->normal_hints->width_inc <= 0)
687 wwin->normal_hints->width_inc = 1;
688 if (wwin->normal_hints->height_inc <= 0)
689 wwin->normal_hints->height_inc = 1;
692 if (wwin->normal_hints->flags & PAspect) {
693 if (wwin->normal_hints->min_aspect.x < 1)
694 wwin->normal_hints->min_aspect.x = 1;
695 if (wwin->normal_hints->min_aspect.y < 1)
696 wwin->normal_hints->min_aspect.y = 1;
698 if (wwin->normal_hints->max_aspect.x < 1)
699 wwin->normal_hints->max_aspect.x = 1;
700 if (wwin->normal_hints->max_aspect.y < 1)
701 wwin->normal_hints->max_aspect.y = 1;
704 if (wwin->normal_hints->min_width <= 0)
705 wwin->normal_hints->min_width = MIN_WINDOW_SIZE;
707 if (wwin->normal_hints->min_height <= 0)
708 wwin->normal_hints->min_height = MIN_WINDOW_SIZE;
710 if (wwin->normal_hints->min_height > wwin->normal_hints->max_height) {
711 wwin->normal_hints->min_height = wwin->normal_hints->max_height;
713 if (wwin->normal_hints->min_width > wwin->normal_hints->max_width) {
714 wwin->normal_hints->min_width = wwin->normal_hints->max_width;
717 if (/*pre_icccm && */!wwin->screen_ptr->flags.startup && geometry) {
718 #ifdef DEBUG
719 printf("PRE ICCCM\n");
720 #endif
721 #ifdef IGNORE_PPOSITION
722 wwin->normal_hints->flags &= ~PPosition;
723 #endif
724 if (wwin->normal_hints->flags & (USPosition|PPosition)) {
725 puts("QWEWQEWQ");
726 *x = wwin->normal_hints->x;
727 *y = wwin->normal_hints->y;
729 if (wwin->normal_hints->flags & (USSize|PSize)) {
730 *width = wwin->normal_hints->width;
731 *height = wwin->normal_hints->height;
737 void
738 GetColormapWindows(WWindow *wwin)
740 if (wwin->cmap_windows) {
741 XFree(wwin->cmap_windows);
744 wwin->cmap_windows = NULL;
745 wwin->cmap_window_no = 0;
747 if (!XGetWMColormapWindows(dpy, wwin->client_win, &(wwin->cmap_windows),
748 &(wwin->cmap_window_no))
749 || !wwin->cmap_windows) {
750 wwin->cmap_window_no = 0;
751 wwin->cmap_windows = NULL;