Update for 0.51.0
[wmaker-crm.git] / src / client.c
blobf2eda11d227fed44ea6799ac758cc7f2af6f84b2
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 XUnmapWindow(dpy, wwin->client_win);
91 XSetWindowBorderWidth(dpy, wwin->client_win, wwin->old_border_width);
92 XReparentWindow(dpy, wwin->client_win, wwin->screen_ptr->root_win,
93 wwin->frame_x, wwin->frame_y);
94 if (wwin->flags.mapped)
95 XMapWindow(dpy, wwin->client_win);
97 /* don't let the window get iconified after restart */
99 if (wwin->flags.shaded)
100 wClientSetState(wwin, NormalState, None);
106 *----------------------------------------------------------------------
107 * wClientSetState--
108 * Set the state of the client window to one of the window
109 * states defined in ICCCM (Iconic, Withdrawn, Normal)
111 * Side effects:
112 * The WM_STATE property of the window is updated as well as the
113 * WWindow.state variable.
114 *----------------------------------------------------------------------
116 void
117 wClientSetState(WWindow *wwin, int state, Window icon_win)
119 CARD32 data[2];
121 wwin->state = state;
123 data[0] = (unsigned long) state;
124 data[1] = (unsigned long) icon_win;
126 XChangeProperty(dpy, wwin->client_win, _XA_WM_STATE, _XA_WM_STATE, 32,
127 PropModeReplace, (unsigned char *) data, 2);
131 void
132 wClientGetGravityOffsets(WWindow *wwin, int *ofs_x, int *ofs_y)
134 switch (wwin->normal_hints->win_gravity) {
135 case ForgetGravity:
136 case CenterGravity:
137 case StaticGravity:
138 *ofs_x = 0;
139 *ofs_y = 0;
140 break;
141 case NorthWestGravity:
142 *ofs_x = -1;
143 *ofs_y = -1;
144 break;
145 case NorthGravity:
146 *ofs_x = 0;
147 *ofs_y = -1;
148 break;
149 case NorthEastGravity:
150 *ofs_x = 1;
151 *ofs_y = -1;
152 break;
153 case WestGravity:
154 *ofs_x = -1;
155 *ofs_y = 0;
156 break;
157 case EastGravity:
158 *ofs_x = 1;
159 *ofs_y = 0;
160 break;
161 case SouthWestGravity:
162 *ofs_x = -1;
163 *ofs_y = 1;
164 break;
165 case SouthGravity:
166 *ofs_x = 0;
167 *ofs_y = 1;
168 break;
169 case SouthEastGravity:
170 *ofs_x = 1;
171 *ofs_y = 1;
172 break;
178 void
179 wClientConfigure(WWindow *wwin, XConfigureRequestEvent *xcre)
181 XWindowChanges xwc;
182 int nx, ny, nwidth, nheight;
183 int ofs_x, ofs_y;
185 if (wwin==NULL) {
187 * configure a window that was not mapped by us
189 xwc.x = xcre->x;
190 xwc.y = xcre->y;
191 xwc.width = xcre->width;
192 xwc.height = xcre->height;
193 xwc.border_width = xcre->border_width;
194 xwc.stack_mode = xcre->detail;
195 xwc.sibling = xcre->above;
196 XConfigureWindow(dpy, xcre->window, xcre->value_mask, &xwc);
197 return;
199 #ifdef SHAPE
200 if (wShapeSupported) {
201 int junk;
202 unsigned int ujunk;
203 int b_shaped;
205 XShapeSelectInput(dpy, wwin->client_win, ShapeNotifyMask);
206 XShapeQueryExtents(dpy, wwin->client_win, &b_shaped, &junk, &junk,
207 &ujunk, &ujunk, &junk, &junk, &junk, &ujunk,
208 &ujunk);
209 wwin->flags.shaped = b_shaped;
211 #endif
212 if (xcre->value_mask & CWStackMode) {
213 WObjDescriptor *desc;
214 WWindow *sibling;
216 if ((xcre->value_mask & CWSibling) &&
217 (XFindContext(dpy, xcre->above, wWinContext,
218 (XPointer *)&desc) == XCSUCCESS)
219 && (desc->parent_type==WCLASS_WINDOW)) {
220 sibling=desc->parent;
221 xwc.sibling = sibling->frame->core->window;
222 } else {
223 xwc.sibling = xcre->above;
225 xwc.stack_mode = xcre->detail;
226 XConfigureWindow(dpy, wwin->frame->core->window,
227 xcre->value_mask & (CWSibling | CWStackMode), &xwc);
228 /* fix stacking order */
229 RemakeStackList(wwin->screen_ptr);
232 wClientGetGravityOffsets(wwin, &ofs_x, &ofs_y);
234 if (xcre->value_mask & CWBorderWidth) {
235 wwin->old_border_width = xcre->border_width;
238 if (!wwin->flags.shaded) {
239 /* If the window is shaded, wrong height will be set for the window */
240 if (xcre->value_mask & CWX)
241 nx = xcre->x - FRAME_BORDER_WIDTH;
242 else
243 nx = wwin->frame_x;
245 if (xcre->value_mask & CWY)
246 ny = xcre->y - ((ofs_y < 0) ? 0 : wwin->frame->top_width)
247 - FRAME_BORDER_WIDTH;
248 else
249 ny = wwin->frame_y;
251 if (xcre->value_mask & CWWidth)
252 nwidth = xcre->width;
253 else
254 nwidth = wwin->frame->core->width;
256 if (xcre->value_mask & CWHeight)
257 nheight = xcre->height;
258 else
259 nheight = wwin->frame->core->height - wwin->frame->top_width - wwin->frame->bottom_width;
261 wWindowConfigure(wwin, nx, ny, nwidth, nheight);
262 wwin->old_geometry.x = nx;
263 wwin->old_geometry.y = ny;
264 wwin->old_geometry.width = nwidth;
265 wwin->old_geometry.height = nheight;
270 void
271 wClientSendProtocol(WWindow *wwin, Atom protocol, Time time)
273 XEvent event;
275 event.xclient.type = ClientMessage;
276 event.xclient.message_type = _XA_WM_PROTOCOLS;
277 event.xclient.format = 32;
278 event.xclient.display = dpy;
279 event.xclient.window = wwin->client_win;
280 event.xclient.data.l[0] = protocol;
281 event.xclient.data.l[1] = time;
282 event.xclient.data.l[2] = 0;
283 event.xclient.data.l[3] = 0;
284 XSendEvent(dpy, wwin->client_win, False, NoEventMask, &event);
285 XSync(dpy, False);
290 void
291 wClientKill(WWindow *wwin)
293 XKillClient(dpy, wwin->client_win);
294 XFlush(dpy);
298 *----------------------------------------------------------------------
299 * wClientCheckProperty--
300 * Handles PropertyNotify'es, verifying which property was
301 * changed and updating internal state according to that, like redrawing
302 * the icon title when it is changed.
304 * Side effects:
305 * Depends on the changed property.
307 * TODO: CLIENT_LEADER, _GNUSTEP_WM_ATTR
308 *----------------------------------------------------------------------
310 void
311 wClientCheckProperty(WWindow *wwin, XPropertyEvent *event)
313 XWindowAttributes attribs;
314 XWMHints *new_hints;
315 int i, g1, g2;
316 char *tmp;
318 switch (event->atom) {
319 case XA_WM_NAME:
320 /* window title was changed */
321 if (wwin->frame) {
322 if (!wFetchName(dpy, wwin->client_win, &tmp)) {
323 /* the hint was removed */
324 tmp = wstrdup(DEF_WINDOW_TITLE);
325 #ifdef KWM_HINTS
326 wKWMSendEventMessage(wwin, WKWMChangedClient);
327 #endif
329 if (wFrameWindowChangeTitle(wwin->frame, tmp)) {
330 /* only update the menu if the title has actually changed */
331 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE);
332 #ifdef KWM_HINTS
333 wKWMSendEventMessage(wwin, WKWMChangedClient);
334 #endif
336 if (tmp)
337 free(tmp);
339 break;
341 case XA_WM_ICON_NAME:
342 #ifdef KWM_HINTS
343 wKWMSendEventMessage(wwin, WKWMChangedClient);
344 #endif
345 if (!wwin->icon)
346 break;
347 else {
348 char *new_title;
350 /* icon title was changed */
351 wGetIconName(dpy, wwin->client_win, &new_title);
352 wIconChangeTitle(wwin->icon, new_title);
354 break;
356 case XA_WM_COMMAND:
357 if (wwin->main_window!=None) {
358 WApplication *wapp = wApplicationOf(wwin->main_window);
359 char **argv;
360 int argc;
362 if (wapp && wapp->app_icon) {
363 if (wapp->app_icon->command!=NULL)
364 free(wapp->app_icon->command);
366 if (XGetCommand(dpy, wwin->main_window, &argv, &argc)) {
367 if (argc > 0 && argv != NULL)
368 wapp->app_icon->command = FlattenStringList(argv,argc);
369 if (argv) {
370 XFreeStringList(argv);
375 break;
377 case XA_WM_HINTS:
378 /* WM_HINTS */
380 new_hints = XGetWMHints(dpy, wwin->client_win);
382 /* group leader update
384 * This means that the window is setting the leader after
385 * it was mapped, changing leaders or removing the leader.
387 * Valid state transitions are:
389 * _1 __2
390 * / \ / \
391 * v | v |
392 * (GC) (GC')
393 * / ^ / ^
394 * 3| |4 5| |6
395 * | | | |
396 * v / v /
397 * (G'C) (G'C')
399 * Where G is the window_group hint, C is CLIENT_LEADER property
400 * and ' indicates the hint is unset.
402 * 1,2 - change group leader to new value of window_group
403 * 3 - change leader to value of CLIENT_LEADER
404 * 4 - change leader to value of window_group
405 * 5 - destroy application
406 * 6 - create application
408 if (new_hints && (new_hints->flags & WindowGroupHint)
409 && new_hints->window_group!=None) {
410 g2 = 1;
411 } else {
412 g2 = 0;
414 if (wwin->wm_hints && (wwin->wm_hints->flags & WindowGroupHint)
415 && wwin->wm_hints->window_group!=None) {
416 g1 = 1;
417 } else {
418 g1 = 0;
421 if (wwin->client_leader) {
422 if (g1 && g2
423 && wwin->wm_hints->window_group!=new_hints->window_group) {
424 i = 1;
425 } else if (g1 && !g2) {
426 i = 3;
427 } else if (!g1 && g2) {
428 i = 4;
429 } else {
430 i = 0;
432 } else {
433 if (g1 && g2
434 && wwin->wm_hints->window_group!=new_hints->window_group) {
435 i = 2;
436 } else if (g1 && !g2) {
437 i = 5;
438 } else if (!g1 && g2) {
439 i = 6;
440 } else {
441 i = 0;
445 if (wwin->wm_hints)
446 XFree(wwin->wm_hints);
448 wwin->wm_hints = new_hints;
450 /* do action according to state transition */
451 switch (i) {
452 /* 3 - change leader to value of CLIENT_LEADER */
453 case 3:
454 wApplicationDestroy(wApplicationOf(wwin->main_window));
455 wwin->main_window = wwin->client_leader;
456 wwin->group_id = None;
457 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
458 break;
460 /* 1,2,4 - change leader to new value of window_group */
461 case 1:
462 case 2:
463 case 4:
464 wApplicationDestroy(wApplicationOf(wwin->main_window));
465 wwin->main_window = new_hints->window_group;
466 wwin->group_id = wwin->main_window;
467 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
468 break;
470 /* 5 - destroy application */
471 case 5:
472 wApplicationDestroy(wApplicationOf(wwin->main_window));
473 wwin->main_window = None;
474 wwin->group_id = None;
475 break;
477 /* 6 - create application */
478 case 6:
479 wwin->main_window = new_hints->window_group;
480 wwin->group_id = wwin->main_window;
481 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
482 break;
484 #ifdef DEBUG
485 if (i) {
486 printf("window leader update caused state transition %i\n",i);
488 #endif
490 if (wwin->wm_hints) {
491 /* update icon */
492 if ((wwin->wm_hints->flags & IconPixmapHint)
493 || (wwin->wm_hints->flags & IconWindowHint)) {
494 WApplication *wapp;
496 if (wwin->flags.miniaturized) {
497 wIconUpdate(wwin->icon);
499 wapp = wApplicationOf(wwin->main_window);
500 if (wapp && wapp->app_icon) {
501 wIconUpdate(wapp->app_icon->icon);
503 #ifdef KWM_HINTS
504 wKWMSendEventMessage(wwin, WKWMIconChange);
505 #endif
508 if (wwin->wm_hints->flags & UrgencyHint)
509 wwin->flags.urgent = 1;
510 else
511 wwin->flags.urgent = 0;
512 } else {
513 wwin->group_id = None;
515 break;
517 case XA_WM_NORMAL_HINTS:
518 /* normal (geometry) hints */
520 int foo;
521 unsigned bar;
523 XGetWindowAttributes(dpy, wwin->client_win, &attribs);
524 wClientGetNormalHints(wwin, &attribs, False, &foo, &foo,
525 &bar, &bar);
526 /* TODO: should we check for consistency of the current
527 * size against the new geometry hints? */
529 break;
531 case XA_WM_TRANSIENT_FOR:
533 Window new_owner;
534 WWindow *owner;
536 if (!XGetTransientForHint(dpy, wwin->client_win, &new_owner)) {
537 new_owner = None;
538 } else {
539 if (new_owner==0 || new_owner == wwin->client_win) {
540 new_owner = wwin->screen_ptr->root_win;
543 if (new_owner!=wwin->transient_for) {
544 owner = wWindowFor(wwin->transient_for);
545 if (owner) {
546 if (owner->flags.semi_focused) {
547 owner->flags.semi_focused = 0;
548 if ((owner->flags.mapped || owner->flags.shaded)
549 && owner->frame)
550 wFrameWindowPaint(owner->frame);
553 owner = wWindowFor(new_owner);
554 if (owner) {
555 if (!owner->flags.semi_focused) {
556 owner->flags.semi_focused = 1;
557 if ((owner->flags.mapped || owner->flags.shaded)
558 && owner->frame)
559 wFrameWindowPaint(owner->frame);
562 wwin->transient_for = new_owner;
563 if (new_owner==None) {
564 if (WFLAGP(wwin, no_miniaturizable)) {
565 WSETUFLAG(wwin, no_miniaturizable, 0);
566 WSETUFLAG(wwin, no_miniaturize_button, 0);
567 if (wwin->frame)
568 wWindowConfigureBorders(wwin);
570 } else if (!WFLAGP(wwin, no_miniaturizable)) {
571 WSETUFLAG(wwin, no_miniaturizable, 1);
572 WSETUFLAG(wwin, no_miniaturize_button, 1);
573 if (wwin->frame)
574 wWindowConfigureBorders(wwin);
578 break;
580 default:
581 if (event->atom==_XA_WM_PROTOCOLS) {
583 PropGetProtocols(wwin->client_win, &wwin->protocols);
585 WSETUFLAG(wwin, kill_close, !wwin->protocols.DELETE_WINDOW);
587 if (wwin->frame)
588 wWindowUpdateButtonImages(wwin);
590 } else if (event->atom==_XA_WM_COLORMAP_WINDOWS) {
592 GetColormapWindows(wwin);
593 wColormapInstallForWindow(wwin->screen_ptr, wwin);
595 } else if (event->atom==_XA_WINDOWMAKER_MENU) {
596 WApplication *wapp;
598 wapp = wApplicationOf(wwin->main_window);
599 if (wapp) {
600 if (wapp->menu) {
601 /* update menu */
602 /* TODO: remake appmenu update */
603 wAppMenuDestroy(wapp->menu);
605 wapp->menu = wAppMenuGet(wwin->screen_ptr, wwin->main_window);
606 /* make the appmenu be mapped */
607 wSetFocusTo(wwin->screen_ptr, NULL);
608 wSetFocusTo(wwin->screen_ptr,
609 wwin->screen_ptr->focused_window);
611 } else if (event->atom==_XA_GNUSTEP_WM_ATTR) {
612 GNUstepWMAttributes *attr;
614 PropGetGNUstepWMAttr(wwin->client_win, &attr);
616 wWindowUpdateGNUstepAttr(wwin, attr);
618 XFree(attr);
619 } else {
620 #ifdef KWM_HINTS
621 Bool done;
623 done = wKWMCheckClientHintChange(wwin, event);
624 #endif /* KWM_HINTS */
631 *----------------------------------------------------------------------
632 * wClientGetNormalHints--
633 * Get size (normal) hints and a default geometry for the client
634 * window. The hints are also checked for inconsistency. If geometry is
635 * True, the returned data will account for client specified initial
636 * geometry.
638 * Side effects:
639 * normal_hints is filled with valid data.
640 *----------------------------------------------------------------------
642 void
643 wClientGetNormalHints(WWindow *wwin, XWindowAttributes *wattribs, Bool geometry,
644 int *x, int *y, unsigned *width, unsigned *height)
646 int pre_icccm=0;
648 /* find a position for the window */
649 if (!wwin->normal_hints)
650 wwin->normal_hints = XAllocSizeHints();
652 if (!PropGetNormalHints(wwin->client_win, wwin->normal_hints, &pre_icccm)) {
653 wwin->normal_hints->flags = 0;
655 *x = wattribs->x;
656 *y = wattribs->y;
658 *width = wattribs->width;
659 *height = wattribs->height;
661 if (!(wwin->normal_hints->flags & PWinGravity)) {
662 wwin->normal_hints->win_gravity = NorthWestGravity;
664 if (!(wwin->normal_hints->flags & PMinSize)) {
665 wwin->normal_hints->min_width = MIN_WINDOW_SIZE;
666 wwin->normal_hints->min_height = MIN_WINDOW_SIZE;
668 if (!(wwin->normal_hints->flags & PBaseSize)) {
669 wwin->normal_hints->base_width = 0;
670 wwin->normal_hints->base_height = 0;
672 if (!(wwin->normal_hints->flags & PMaxSize)) {
673 wwin->normal_hints->max_width = wwin->screen_ptr->scr_width*2;
674 wwin->normal_hints->max_height = wwin->screen_ptr->scr_height*2;
677 /* some buggy apps set weird hints.. */
678 if (wwin->normal_hints->max_width < wwin->normal_hints->min_width)
679 wwin->normal_hints->max_width = wwin->normal_hints->min_width;
681 if (wwin->normal_hints->max_height < wwin->normal_hints->min_height)
682 wwin->normal_hints->max_height = wwin->normal_hints->min_height;
684 if (!(wwin->normal_hints->flags & PResizeInc)) {
685 wwin->normal_hints->width_inc = 1;
686 wwin->normal_hints->height_inc = 1;
687 } else {
688 if (wwin->normal_hints->width_inc <= 0)
689 wwin->normal_hints->width_inc = 1;
690 if (wwin->normal_hints->height_inc <= 0)
691 wwin->normal_hints->height_inc = 1;
694 if (wwin->normal_hints->flags & PAspect) {
695 if (wwin->normal_hints->min_aspect.x < 1)
696 wwin->normal_hints->min_aspect.x = 1;
697 if (wwin->normal_hints->min_aspect.y < 1)
698 wwin->normal_hints->min_aspect.y = 1;
700 if (wwin->normal_hints->max_aspect.x < 1)
701 wwin->normal_hints->max_aspect.x = 1;
702 if (wwin->normal_hints->max_aspect.y < 1)
703 wwin->normal_hints->max_aspect.y = 1;
706 if (wwin->normal_hints->min_width <= 0)
707 wwin->normal_hints->min_width = MIN_WINDOW_SIZE;
709 if (wwin->normal_hints->min_height <= 0)
710 wwin->normal_hints->min_height = MIN_WINDOW_SIZE;
712 if (wwin->normal_hints->min_height > wwin->normal_hints->max_height) {
713 wwin->normal_hints->min_height = wwin->normal_hints->max_height;
715 if (wwin->normal_hints->min_width > wwin->normal_hints->max_width) {
716 wwin->normal_hints->min_width = wwin->normal_hints->max_width;
719 /* pre ICCCM (old) client */
720 if (pre_icccm && !wwin->screen_ptr->flags.startup && geometry) {
721 #ifdef DEBUG
722 printf("PRE ICCCM\n");
723 #endif
724 #ifdef IGNORE_PPOSITION
725 wwin->normal_hints->flags &= ~PPosition;
726 #endif
727 if (wwin->normal_hints->flags & (USPosition|PPosition)) {
728 *x = wwin->normal_hints->x;
729 *y = wwin->normal_hints->y;
731 if (wwin->normal_hints->flags & (USSize|PSize)) {
732 *width = wwin->normal_hints->width;
733 *height = wwin->normal_hints->height;
739 void
740 GetColormapWindows(WWindow *wwin)
742 if (wwin->cmap_windows) {
743 XFree(wwin->cmap_windows);
746 wwin->cmap_windows = NULL;
747 wwin->cmap_window_no = 0;
749 if (XGetWMColormapWindows(dpy, wwin->client_win, &(wwin->cmap_windows),
750 &(wwin->cmap_window_no))!=Success
751 || !wwin->cmap_windows) {
752 wwin->cmap_window_no = 0;
753 wwin->cmap_windows = NULL;