fix for soemthing wrong with piped menus
[wmaker-crm.git] / src / screen.c
blob3ec878a4e17a0e808636272807fc0141b331d073
1 /* screen.c - screen management
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997-2002 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.
23 #include "wconfig.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include <X11/Xlib.h>
30 #include <X11/Xutil.h>
31 #include <X11/Xatom.h>
32 #ifdef SHAPE
33 #include <X11/extensions/shape.h>
34 #endif
35 #ifdef KEEP_XKB_LOCK_STATUS
36 #include <X11/XKBlib.h>
37 #endif /* KEEP_XKB_LOCK_STATUS */
39 #include <wraster.h>
41 #include "WindowMaker.h"
42 #include "def_pixmaps.h"
43 #include "screen.h"
44 #include "texture.h"
45 #include "pixmap.h"
46 #include "menu.h"
47 #include "funcs.h"
48 #include "actions.h"
49 #include "properties.h"
50 #include "dock.h"
51 #include "resources.h"
52 #include "workspace.h"
53 #include "session.h"
54 #include "balloon.h"
55 #include "geomview.h"
56 #ifdef KWM_HINTS
57 # include "kwm.h"
58 #endif
59 #ifdef GNOME_STUFF
60 # include "gnome.h"
61 #endif
62 #ifdef OLWM_HINTS
63 # include "openlook.h"
64 #endif
66 #include "xinerama.h"
68 #include <WINGs/WUtil.h>
70 #include "defaults.h"
73 #ifdef LITE
74 #define EVENT_MASK (LeaveWindowMask|EnterWindowMask|PropertyChangeMask\
75 |SubstructureNotifyMask|PointerMotionMask \
76 |SubstructureRedirectMask|KeyPressMask|KeyReleaseMask)
77 #else
78 #define EVENT_MASK (LeaveWindowMask|EnterWindowMask|PropertyChangeMask\
79 |SubstructureNotifyMask|PointerMotionMask \
80 |SubstructureRedirectMask|ButtonPressMask|ButtonReleaseMask\
81 |KeyPressMask|KeyReleaseMask)
82 #endif
84 /**** Global variables ****/
86 extern Cursor wCursor[WCUR_LAST];
87 extern WPreferences wPreferences;
88 extern Atom _XA_WINDOWMAKER_STATE;
89 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
92 extern int wScreenCount;
94 #ifdef KEEP_XKB_LOCK_STATUS
95 extern int wXkbSupported;
96 #endif
98 extern WDDomain *WDWindowMaker;
101 /**** Local ****/
103 #define STIPPLE_WIDTH 2
104 #define STIPPLE_HEIGHT 2
105 static char STIPPLE_DATA[] = {0x02, 0x01};
107 static int CantManageScreen = 0;
109 static WMPropList *dApplications = NULL;
110 static WMPropList *dWorkspace;
111 static WMPropList *dDock;
112 static WMPropList *dClip;
115 static void
116 make_keys()
118 if (dApplications!=NULL)
119 return;
121 dApplications = WMCreatePLString("Applications");
122 dWorkspace = WMCreatePLString("Workspace");
123 dDock = WMCreatePLString("Dock");
124 dClip = WMCreatePLString("Clip");
129 *----------------------------------------------------------------------
130 * alreadyRunningError--
131 * X error handler used to catch errors when trying to do
132 * XSelectInput() on the root window. These errors probably mean that
133 * there already is some other window manager running.
135 * Returns:
136 * Nothing, unless something really evil happens...
138 * Side effects:
139 * CantManageScreen is set to 1;
140 *----------------------------------------------------------------------
142 static int
143 alreadyRunningError(Display *dpy, XErrorEvent *error)
145 CantManageScreen = 1;
146 return -1;
151 *----------------------------------------------------------------------
152 * allocButtonPixmaps--
153 * Allocate pixmaps used on window operation buttons (those in the
154 * titlebar). The pixmaps are linked to the program. If XPM is supported
155 * XPM pixmaps are used otherwise, equivalent bitmaps are used.
157 * Returns:
158 * Nothing
160 * Side effects:
161 * Allocates shared pixmaps for the screen. These pixmaps should
162 * not be freed by anybody.
163 *----------------------------------------------------------------------
165 static void
166 allocButtonPixmaps(WScreen *scr)
168 WPixmap *pix;
170 /* create predefined pixmaps */
171 pix = wPixmapCreateFromXPMData(scr, PRED_CLOSE_XPM);
172 if (pix)
173 pix->shared = 1;
174 scr->b_pixmaps[WBUT_CLOSE] = pix;
176 pix = wPixmapCreateFromXPMData(scr, PRED_BROKEN_CLOSE_XPM);
177 if (pix)
178 pix->shared = 1;
179 scr->b_pixmaps[WBUT_BROKENCLOSE] = pix;
181 pix = wPixmapCreateFromXPMData(scr, PRED_ICONIFY_XPM);
182 if (pix)
183 pix->shared = 1;
184 scr->b_pixmaps[WBUT_ICONIFY] = pix;
185 #ifdef XKB_BUTTON_HINT
186 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP1_XPM);
187 if (pix)
188 pix->shared = 1;
189 scr->b_pixmaps[WBUT_XKBGROUP1] = pix;
190 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP2_XPM);
191 if (pix)
192 pix->shared = 1;
193 scr->b_pixmaps[WBUT_XKBGROUP2] = pix;
194 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP3_XPM);
195 if (pix)
196 pix->shared = 1;
197 scr->b_pixmaps[WBUT_XKBGROUP3] = pix;
198 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP4_XPM);
199 if (pix)
200 pix->shared = 1;
201 scr->b_pixmaps[WBUT_XKBGROUP4] = pix;
202 #endif
205 pix = wPixmapCreateFromXPMData(scr, PRED_KILL_XPM);
206 if (pix)
207 pix->shared = 1;
208 scr->b_pixmaps[WBUT_KILL] = pix;
212 static void
213 draw_dot(WScreen *scr, Drawable d, int x, int y, GC gc)
215 XSetForeground(dpy, gc, scr->black_pixel);
216 XDrawLine(dpy, d, gc, x, y, x+1, y);
217 XDrawPoint(dpy, d, gc, x, y+1);
218 XSetForeground(dpy, gc, scr->white_pixel);
219 XDrawLine(dpy, d, gc, x+2, y, x+2, y+1);
220 XDrawPoint(dpy, d, gc, x+1, y+1);
224 static WPixmap*
225 make3Dots(WScreen *scr)
227 WPixmap *wpix;
228 GC gc2, gc;
229 XGCValues gcv;
230 Pixmap pix, mask;
232 gc = scr->copy_gc;
233 pix = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
234 wPreferences.icon_size, scr->w_depth);
235 XSetForeground(dpy, gc, scr->black_pixel);
236 XFillRectangle(dpy, pix, gc, 0, 0, wPreferences.icon_size,
237 wPreferences.icon_size);
238 XSetForeground(dpy, gc, scr->white_pixel);
239 draw_dot(scr, pix, 4, wPreferences.icon_size-6, gc);
240 draw_dot(scr, pix, 9, wPreferences.icon_size-6, gc);
241 draw_dot(scr, pix, 14, wPreferences.icon_size-6, gc);
243 mask = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
244 wPreferences.icon_size, 1);
245 gcv.foreground = 0;
246 gcv.graphics_exposures = False;
247 gc2 = XCreateGC(dpy, mask, GCForeground|GCGraphicsExposures, &gcv);
248 XFillRectangle(dpy, mask, gc2, 0, 0, wPreferences.icon_size,
249 wPreferences.icon_size);
250 XSetForeground(dpy, gc2, 1);
251 XFillRectangle(dpy, mask, gc2, 4, wPreferences.icon_size-6, 3, 2);
252 XFillRectangle(dpy, mask, gc2, 9, wPreferences.icon_size-6, 3, 2);
253 XFillRectangle(dpy, mask, gc2, 14, wPreferences.icon_size-6, 3, 2);
255 XFreeGC(dpy, gc2);
257 wpix = wPixmapCreate(scr, pix, mask);
258 wpix->shared = 1;
260 return wpix;
264 static void
265 allocGCs(WScreen *scr)
267 XGCValues gcv;
268 XColor color;
269 unsigned long mtextcolor;
270 int gcm;
272 scr->stipple_bitmap =
273 XCreateBitmapFromData(dpy, scr->w_win, STIPPLE_DATA, STIPPLE_WIDTH,
274 STIPPLE_HEIGHT);
276 gcv.stipple = scr->stipple_bitmap;
277 gcv.foreground = scr->white_pixel;
278 gcv.fill_style = FillStippled;
279 gcv.graphics_exposures = False;
280 gcm = GCForeground|GCStipple|GCFillStyle|GCGraphicsExposures;
281 scr->stipple_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
284 /* selected icon border GCs */
285 gcv.function = GXcopy;
286 gcv.foreground = scr->white_pixel;
287 gcv.background = scr->black_pixel;
288 gcv.line_width = 1;
289 gcv.line_style = LineDoubleDash;
290 gcv.fill_style = FillSolid;
291 gcv.dash_offset = 0;
292 gcv.dashes = 4;
293 gcv.graphics_exposures = False;
295 gcm = GCFunction | GCGraphicsExposures;
296 gcm |= GCForeground | GCBackground;
297 gcm |= GCLineWidth | GCLineStyle;
298 gcm |= GCFillStyle;
299 gcm |= GCDashOffset | GCDashList;
301 scr->icon_select_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
303 gcm = GCForeground|GCGraphicsExposures;
305 scr->menu_title_pixel[0] = scr->white_pixel;
306 gcv.foreground = scr->white_pixel;
307 gcv.graphics_exposures = False;
308 scr->menu_title_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
310 scr->mtext_pixel = scr->black_pixel;
311 mtextcolor = gcv.foreground = scr->black_pixel;
312 scr->menu_entry_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
314 /* selected menu entry GC */
315 gcm = GCForeground|GCBackground|GCGraphicsExposures;
316 gcv.foreground = scr->white_pixel;
317 gcv.background = scr->white_pixel;
318 gcv.graphics_exposures = False;
319 scr->select_menu_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
321 /* disabled menu entry GC */
322 scr->dtext_pixel = scr->black_pixel;
323 gcm = GCForeground|GCBackground|GCStipple|GCGraphicsExposures;
324 gcv.stipple = scr->stipple_bitmap;
325 gcv.graphics_exposures = False;
326 scr->disabled_menu_entry_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
328 /* frame GC */
329 wGetColor(scr, DEF_FRAME_COLOR, &color);
330 gcv.function = GXxor;
331 /* this will raise the probability of the XORed color being different
332 * of the original color in PseudoColor when not all color cells are
333 * initialized */
334 if (DefaultVisual(dpy, scr->screen)->class==PseudoColor)
335 gcv.plane_mask = (1<<(scr->depth-1))|1;
336 else
337 gcv.plane_mask = AllPlanes;
338 gcv.foreground = color.pixel;
339 if (gcv.foreground == 0)
340 gcv.foreground = 1;
341 gcv.line_width = DEF_FRAME_THICKNESS;
342 gcv.subwindow_mode = IncludeInferiors;
343 gcv.graphics_exposures = False;
344 scr->frame_gc = XCreateGC(dpy, scr->root_win, GCForeground|GCGraphicsExposures
345 |GCFunction|GCSubwindowMode|GCLineWidth
346 |GCPlaneMask, &gcv);
348 /* line GC */
349 gcv.foreground = color.pixel;
351 if (gcv.foreground == 0)
352 /* XOR:ing with a zero is not going to be of much use, so
353 in that case, we somewhat arbitrarily xor with 17 instead. */
354 gcv.foreground = 17;
356 gcv.function = GXxor;
357 gcv.subwindow_mode = IncludeInferiors;
358 gcv.line_width = 1;
359 gcv.cap_style = CapRound;
360 gcv.graphics_exposures = False;
361 gcm = GCForeground|GCFunction|GCSubwindowMode|GCLineWidth|GCCapStyle
362 |GCGraphicsExposures;
363 scr->line_gc = XCreateGC(dpy, scr->root_win, gcm, &gcv);
365 scr->line_pixel = gcv.foreground;
367 /* copy GC */
368 gcv.foreground = scr->white_pixel;
369 gcv.background = scr->black_pixel;
370 gcv.graphics_exposures = False;
371 scr->copy_gc = XCreateGC(dpy, scr->w_win, GCForeground|GCBackground
372 |GCGraphicsExposures, &gcv);
374 /* window title text GC */
375 gcv.graphics_exposures = False;
376 scr->window_title_gc = XCreateGC(dpy, scr->w_win,GCGraphicsExposures,&gcv);
378 /* icon title GC */
379 scr->icon_title_gc = XCreateGC(dpy, scr->w_win, GCGraphicsExposures, &gcv);
381 /* clip title GC */
382 scr->clip_title_gc = XCreateGC(dpy, scr->w_win, GCGraphicsExposures, &gcv);
384 /* move/size display GC */
385 gcv.graphics_exposures = False;
386 gcm = GCGraphicsExposures;
387 scr->info_text_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
389 /* misc drawing GC */
390 gcv.graphics_exposures = False;
391 gcm = GCGraphicsExposures;
392 scr->draw_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
394 assert (scr->stipple_bitmap!=None);
397 /* mono GC */
398 scr->mono_gc = XCreateGC(dpy, scr->stipple_bitmap, gcm, &gcv);
403 static void
404 createPixmaps(WScreen *scr)
406 WPixmap *pix;
407 RImage *image;
409 /* load pixmaps */
410 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_RADIO_INDICATOR_XBM_DATA,
411 (char*)MENU_RADIO_INDICATOR_XBM_DATA,
412 MENU_RADIO_INDICATOR_XBM_SIZE,
413 MENU_RADIO_INDICATOR_XBM_SIZE,
414 scr->black_pixel, scr->white_pixel);
415 if (pix!=NULL)
416 pix->shared = 1;
417 scr->menu_radio_indicator = pix;
420 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_CHECK_INDICATOR_XBM_DATA,
421 (char*)MENU_CHECK_INDICATOR_XBM_DATA,
422 MENU_CHECK_INDICATOR_XBM_SIZE,
423 MENU_CHECK_INDICATOR_XBM_SIZE,
424 scr->black_pixel, scr->white_pixel);
425 if (pix!=NULL)
426 pix->shared = 1;
427 scr->menu_check_indicator = pix;
429 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_MINI_INDICATOR_XBM_DATA,
430 (char*)MENU_MINI_INDICATOR_XBM_DATA,
431 MENU_MINI_INDICATOR_XBM_SIZE,
432 MENU_MINI_INDICATOR_XBM_SIZE,
433 scr->black_pixel, scr->white_pixel);
434 if (pix!=NULL)
435 pix->shared = 1;
436 scr->menu_mini_indicator = pix;
438 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_HIDE_INDICATOR_XBM_DATA,
439 (char*)MENU_HIDE_INDICATOR_XBM_DATA,
440 MENU_HIDE_INDICATOR_XBM_SIZE,
441 MENU_HIDE_INDICATOR_XBM_SIZE,
442 scr->black_pixel, scr->white_pixel);
443 if (pix!=NULL)
444 pix->shared = 1;
445 scr->menu_hide_indicator = pix;
447 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_SHADE_INDICATOR_XBM_DATA,
448 (char*)MENU_SHADE_INDICATOR_XBM_DATA,
449 MENU_SHADE_INDICATOR_XBM_SIZE,
450 MENU_SHADE_INDICATOR_XBM_SIZE,
451 scr->black_pixel, scr->white_pixel);
452 if (pix!=NULL)
453 pix->shared = 1;
454 scr->menu_shade_indicator = pix;
457 image = wDefaultGetImage(scr, "Logo", "WMPanel");
459 if (!image) {
460 wwarning(_("could not load logo image for panels: %s"),
461 RMessageForError(RErrorCode));
462 } else {
463 WMSetApplicationIconImage(scr->wmscreen, image);
464 RReleaseImage(image);
467 scr->dock_dots = make3Dots(scr);
469 /* titlebar button pixmaps */
470 allocButtonPixmaps(scr);
475 *----------------------------------------------------------------------
476 * createInternalWindows--
477 * Creates some windows used internally by the program. One to
478 * receive input focus when no other window can get it and another
479 * to display window geometry information during window resize/move.
481 * Returns:
482 * Nothing
484 * Side effects:
485 * Windows are created and some colors are allocated for the
486 * window background.
487 *----------------------------------------------------------------------
489 static void
490 createInternalWindows(WScreen *scr)
492 int vmask;
493 XSetWindowAttributes attribs;
495 /* InputOnly window to get the focus when no other window can get it */
496 vmask = CWEventMask|CWOverrideRedirect;
497 attribs.event_mask = KeyPressMask|FocusChangeMask;
498 attribs.override_redirect = True;
499 scr->no_focus_win=XCreateWindow(dpy,scr->root_win,-10, -10, 4, 4, 0, 0,
500 InputOnly,CopyFromParent, vmask, &attribs);
501 XSelectInput(dpy, scr->no_focus_win, KeyPressMask|KeyReleaseMask);
502 XMapWindow(dpy, scr->no_focus_win);
504 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
506 /* shadow window for dock buttons */
507 vmask = CWBorderPixel|CWBackPixmap|CWBackPixel|CWCursor|CWSaveUnder|CWOverrideRedirect;
508 attribs.border_pixel = scr->black_pixel;
509 attribs.save_under = True;
510 attribs.override_redirect = True;
511 attribs.background_pixmap = None;
512 attribs.background_pixel = scr->white_pixel;
513 attribs.cursor = wCursor[WCUR_DEFAULT];
514 vmask |= CWColormap;
515 attribs.colormap = scr->w_colormap;
516 scr->dock_shadow =
517 XCreateWindow(dpy, scr->root_win, 0, 0, wPreferences.icon_size,
518 wPreferences.icon_size, 0, scr->w_depth, CopyFromParent,
519 scr->w_visual, vmask, &attribs);
521 /* workspace name balloon for clip */
522 vmask = CWBackPixel|CWSaveUnder|CWOverrideRedirect|CWColormap
523 |CWBorderPixel;
524 attribs.save_under = True;
525 attribs.override_redirect = True;
526 attribs.colormap = scr->w_colormap;
527 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
528 attribs.border_pixel = 0; /* do not care */
529 scr->clip_balloon =
530 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
531 CopyFromParent, scr->w_visual, vmask, &attribs);
534 /* workspace name */
535 vmask = CWBackPixel|CWSaveUnder|CWOverrideRedirect|CWColormap
536 |CWBorderPixel;
537 attribs.save_under = True;
538 attribs.override_redirect = True;
539 attribs.colormap = scr->w_colormap;
540 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
541 attribs.border_pixel = 0; /* do not care */
542 scr->workspace_name =
543 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
544 CopyFromParent, scr->w_visual, vmask, &attribs);
547 * If the window is clicked without having ButtonPress selected, the
548 * resulting event will have event.xbutton.window == root.
550 XSelectInput(dpy, scr->clip_balloon, ButtonPressMask);
554 #if 0
555 static Bool
556 aquireManagerSelection(WScreen *scr)
558 char buffer[32];
559 XEvent ev;
560 Time timestamp;
562 snprintf(buffer, sizeof(buffer), "WM_S%i", scr->screen);
563 scr->managerAtom = XInternAtom(dpy, buffer, False);
565 /* for race-conditions... */
566 XGrabServer(dpy);
568 /* if there is another manager running, don't try to replace it
569 * (for now, at least) */
570 if (XGetSelectionOwner(dpy, scr->managerAtom) != None) {
571 XUngrabServer(dpy);
572 return False;
575 /* become the manager for this screen */
577 scr->managerWindow = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, 1, 1,
578 0, 0, 0);
580 XSelectInput(dpy, scr->managerWindow, PropertyChangeMask);
581 /* get a timestamp */
582 XChangeProperty(dpy, scr->managerWindow, scr->managerAtom,
583 XA_INTEGER, 32, PropModeAppend, NULL, 0);
584 while (1) {
585 XWindowEvent(dpy, scr->managerWindow, &ev);
586 if (ev.type == PropertyNotify) {
587 timestamp = ev.xproperty.time;
588 break;
591 XSelectInput(dpy, scr->managerWindow, NoEvents);
592 XDeleteProperty(dpy, scr->managerWindow, scr->managerAtom);
594 XSetSelectionOwner(dpy, scr->managerAtom, scr->managerWindow, CurrentTime);
596 XUngrabServer(dpy);
598 /* announce our arrival */
600 ev.xclient.type = ClientMessage;
601 ev.xclient.message_type = XInternAtom(dpy, "MANAGER", False);
602 ev.xclient.destination = scr->root_win;
603 ev.xclient.format = 32;
604 ev.xclient.data.l[0] = timestamp;
605 ev.xclient.data.l[1] = scr->managerAtom;
606 ev.xclient.data.l[2] = scr->managerWindow;
607 ev.xclient.data.l[3] = 0;
608 ev.xclient.data.l[4] = 0;
610 XSendEvent(dpy, scr->root_win, False, StructureNotify, &ev);
611 XSync(dpy, False);
613 return True;
615 #endif
618 *----------------------------------------------------------------------
619 * wScreenInit--
620 * Initializes the window manager for the given screen and
621 * allocates a WScreen descriptor for it. Many resources are allocated
622 * for the screen and the root window is setup appropriately.
624 * Returns:
625 * The WScreen descriptor for the screen.
627 * Side effects:
628 * Many resources are allocated and the IconSize property is
629 * set on the root window.
630 * The program can be aborted if some fatal error occurs.
632 * TODO: User specifiable visual.
633 *----------------------------------------------------------------------
635 WScreen*
636 wScreenInit(int screen_number)
638 WScreen *scr;
639 XIconSize icon_size[1];
640 RContextAttributes rattr;
641 extern int wVisualID;
642 long event_mask;
643 WMColor *color;
644 XErrorHandler oldHandler;
646 scr = wmalloc(sizeof(WScreen));
647 memset(scr, 0, sizeof(WScreen));
649 scr->stacking_list = WMCreateTreeBag();
651 /* initialize globals */
652 scr->screen = screen_number;
653 scr->root_win = RootWindow(dpy, screen_number);
654 scr->depth = DefaultDepth(dpy, screen_number);
655 scr->colormap = DefaultColormap(dpy, screen_number);
657 scr->scr_width = WidthOfScreen(ScreenOfDisplay(dpy, screen_number));
658 scr->scr_height = HeightOfScreen(ScreenOfDisplay(dpy, screen_number));
660 scr->usableArea.x2 = scr->scr_width;
661 scr->usableArea.y2 = scr->scr_height;
662 scr->totalUsableArea.x2 = scr->scr_width;
663 scr->totalUsableArea.y2 = scr->scr_height;
665 scr->fakeGroupLeaders = WMCreateArray(16);
667 #if 0
668 if (!aquireManagerSelection(scr)) {
669 wfree(scr);
671 return NULL;
673 #endif
674 CantManageScreen = 0;
675 oldHandler = XSetErrorHandler((XErrorHandler)alreadyRunningError);
677 event_mask = EVENT_MASK;
679 if (wPreferences.disable_root_mouse) {
680 event_mask &= ~(ButtonPressMask|ButtonReleaseMask);
683 XSelectInput(dpy, scr->root_win, event_mask);
685 #ifdef KEEP_XKB_LOCK_STATUS
686 /* Only GroupLock doesn't work correctly in my system since right-alt
687 * can change mode while holding it too - ]d
689 if (wXkbSupported) {
690 XkbSelectEvents(dpy,XkbUseCoreKbd,
691 XkbStateNotifyMask,
692 XkbStateNotifyMask);
694 #endif /* KEEP_XKB_LOCK_STATUS */
696 XSync(dpy, False);
697 XSetErrorHandler(oldHandler);
699 if (CantManageScreen) {
700 wfree(scr);
701 return NULL;
704 #ifdef XINERAMA
705 wInitXinerama(scr);
706 #endif
709 XDefineCursor(dpy, scr->root_win, wCursor[WCUR_ROOT]);
711 /* screen descriptor for raster graphic library */
712 rattr.flags = RC_RenderMode | RC_ColorsPerChannel | RC_StandardColormap;
713 rattr.render_mode = wPreferences.no_dithering
714 ? RBestMatchRendering
715 : RDitheredRendering;
717 /* if the std colormap stuff works ok, this will be ignored */
718 rattr.colors_per_channel = wPreferences.cmap_size;
719 if (rattr.colors_per_channel<2)
720 rattr.colors_per_channel = 2;
723 /* will only be accounted for in PseudoColor */
724 if (wPreferences.flags.create_stdcmap) {
725 rattr.standard_colormap_mode = RCreateStdColormap;
726 } else {
727 rattr.standard_colormap_mode = RUseStdColormap;
730 if (wVisualID>=0) {
731 rattr.flags |= RC_VisualID;
732 rattr.visualid = wVisualID;
735 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
737 if (!scr->rcontext && RErrorCode == RERR_STDCMAPFAIL) {
738 wwarning(RMessageForError(RErrorCode));
740 rattr.flags &= ~RC_StandardColormap;
741 rattr.standard_colormap_mode = RUseStdColormap;
743 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
746 if (!scr->rcontext) {
747 wwarning(_("could not initialize graphics library context: %s"),
748 RMessageForError(RErrorCode));
749 wAbort(False);
750 } else {
751 char **formats;
752 int i = 0;
754 formats = RSupportedFileFormats();
755 if (formats) {
756 for (i=0; formats[i]!=NULL; i++) {
757 if (strcmp(formats[i], "TIFF")==0) {
758 scr->flags.supports_tiff = 1;
759 break;
765 scr->w_win = scr->rcontext->drawable;
766 scr->w_visual = scr->rcontext->visual;
767 scr->w_depth = scr->rcontext->depth;
768 scr->w_colormap = scr->rcontext->cmap;
770 scr->black_pixel = scr->rcontext->black;
771 scr->white_pixel = scr->rcontext->white;
773 /* create screen descriptor for WINGs */
774 scr->wmscreen = WMCreateScreenWithRContext(dpy, screen_number,
775 scr->rcontext);
777 if (!scr->wmscreen) {
778 wfatal(_("could not do initialization of WINGs widget set"));
780 return NULL;
783 color = WMGrayColor(scr->wmscreen);
784 scr->light_pixel = WMColorPixel(color);
785 WMReleaseColor(color);
787 color = WMDarkGrayColor(scr->wmscreen);
788 scr->dark_pixel = WMColorPixel(color);
789 WMReleaseColor(color);
792 XColor xcol;
793 /* frame boder color */
794 wGetColor(scr, FRAME_BORDER_COLOR, &xcol);
795 scr->frame_border_pixel = xcol.pixel;
798 /* create GCs with default values */
799 allocGCs(scr);
802 /* for our window manager info notice board. Need to
803 * create before reading the defaults, because it will be used there.
805 scr->info_window = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, 10, 10,
806 0, 0, 0);
808 /* read defaults for this screen */
809 wReadDefaults(scr, WDWindowMaker->dictionary);
811 createInternalWindows(scr);
813 #ifdef KWM_HINTS
814 wKWMInitStuff(scr);
815 #endif
817 #ifdef GNOME_STUFF
818 wGNOMEInitStuff(scr);
819 #endif
821 #ifdef OLWM_HINTS
822 wOLWMInitStuff(scr);
823 #endif
825 /* create initial workspace */
826 wWorkspaceNew(scr);
828 /* create shared pixmaps */
829 createPixmaps(scr);
831 /* set icon sizes we can accept from clients */
832 icon_size[0].min_width = 8;
833 icon_size[0].min_height = 8;
834 icon_size[0].max_width = wPreferences.icon_size-4;
835 icon_size[0].max_height = wPreferences.icon_size-4;
836 icon_size[0].width_inc = 1;
837 icon_size[0].height_inc = 1;
838 XSetIconSizes(dpy, scr->root_win, icon_size, 1);
840 /* setup WindowMaker protocols property in the root window*/
841 PropSetWMakerProtocols(scr->root_win);
843 /* setup our noticeboard */
844 XChangeProperty(dpy, scr->info_window, _XA_WINDOWMAKER_NOTICEBOARD,
845 XA_WINDOW, 32, PropModeReplace,
846 (unsigned char*)&scr->info_window, 1);
847 XChangeProperty(dpy, scr->root_win, _XA_WINDOWMAKER_NOTICEBOARD,
848 XA_WINDOW, 32, PropModeReplace,
849 (unsigned char*)&scr->info_window, 1);
852 #ifdef BALLOON_TEXT
853 /* initialize balloon text stuff */
854 wBalloonInitialize(scr);
855 #endif
857 scr->info_text_font = WMBoldSystemFontOfSize(scr->wmscreen, 12);
860 scr->gview = WCreateGeometryView(scr->wmscreen);
861 WMRealizeWidget(scr->gview);
863 wScreenUpdateUsableArea(scr);
865 return scr;
870 static WArea subtractRectangle(WArea area, WArea rect)
872 WArea result = area;
878 void
879 wScreenUpdateUsableArea(WScreen *scr)
881 WReservedArea *area;
883 scr->totalUsableArea = scr->usableArea;
886 if (scr->dock && (!scr->dock->lowered
887 || wPreferences.no_window_over_dock)) {
889 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
891 if (scr->dock->on_right_side) {
892 scr->totalUsableArea.x2 = WMIN(scr->totalUsableArea.x2,
893 scr->scr_width - offset);
894 } else {
895 scr->totalUsableArea.x1 = WMAX(scr->totalUsableArea.x1, offset);
899 if (wPreferences.no_window_over_icons) {
900 if (wPreferences.icon_yard & IY_VERT) {
902 if (!(wPreferences.icon_yard & IY_RIGHT)) {
903 scr->totalUsableArea.x1 += wPreferences.icon_size;
904 } else {
905 scr->totalUsableArea.x2 -= wPreferences.icon_size;
907 } else {
909 if (wPreferences.icon_yard & IY_TOP) {
910 scr->totalUsableArea.y1 += wPreferences.icon_size;
911 } else {
912 scr->totalUsableArea.y2 -= wPreferences.icon_size;
917 #ifdef KWM_HINTS
919 WArea area;
921 if (wKWMGetUsableArea(scr, &area)) {
922 scr->totalUsableArea.x1 = WMAX(scr->totalUsableArea.x1, area.x1);
923 scr->totalUsableArea.y1 = WMAX(scr->totalUsableArea.y1, area.y1);
924 scr->totalUsableArea.x2 = WMIN(scr->totalUsableArea.x2, area.x2);
925 scr->totalUsableArea.y2 = WMIN(scr->totalUsableArea.y2, area.y2);
928 #endif
930 #ifdef GNOME_STUFF
931 area = scr->reservedAreas;
933 while (area) {
934 int th, bh;
935 int lw, rw;
936 int w, h;
938 w = area->area.x2 - area->area.x1;
939 h = area->area.y2 - area->area.y1;
941 th = area->area.y1;
942 bh = scr->scr_height - area->area.y2;
943 lw = area->area.x1;
944 rw = scr->scr_width - area->area.x2;
946 if (WMIN(th, bh) <= WMIN(lw, rw)) {
947 /* horizontal */
948 if (th < bh) {
949 /* on top */
950 if (scr->totalUsableArea.y1 < area->area.y2)
951 scr->totalUsableArea.y1 = area->area.y2;
952 } else {
953 /* on bottom */
954 if (scr->totalUsableArea.y2 > area->area.y1)
955 scr->totalUsableArea.y2 = area->area.y1;
957 } else {
958 /* vertical */
959 if (lw < rw) {
960 /* on left */
961 if (scr->totalUsableArea.x1 < area->area.x2)
962 scr->totalUsableArea.x1 = area->area.x2;
963 } else {
964 /* on right */
965 if (scr->totalUsableArea.x2 > area->area.x1)
966 scr->totalUsableArea.x2 = area->area.x1;
970 area = area->next;
972 #endif /* GNOME_STUFF */
974 if (scr->totalUsableArea.x2 - scr->totalUsableArea.x1 < scr->scr_width/2) {
975 scr->totalUsableArea.x2 = scr->usableArea.x2;
976 scr->totalUsableArea.x1 = scr->usableArea.x1;
978 if (scr->totalUsableArea.y2 - scr->totalUsableArea.y1 < scr->scr_height/2) {
979 scr->totalUsableArea.y2 = scr->usableArea.y2;
980 scr->totalUsableArea.y1 = scr->usableArea.y1;
983 #ifdef not_used
984 #ifdef KWM_HINTS
986 int i;
988 for (i = 0; i < scr->workspace_count; i++) {
989 wKWMSetUsableAreaHint(scr, i);
992 #endif
993 #endif
996 unsigned size = wPreferences.workspace_border_size;
997 unsigned position = wPreferences.workspace_border_position;
999 if (size>0 && position!=WB_NONE) {
1000 if (position & WB_LEFTRIGHT) {
1001 scr->totalUsableArea.x1 += size;
1002 scr->totalUsableArea.x2 -= size;
1004 if (position & WB_TOPBOTTOM) {
1005 scr->totalUsableArea.y1 += size;
1006 scr->totalUsableArea.y2 -= size;
1014 void
1015 wScreenRestoreState(WScreen *scr)
1017 WMPropList *state;
1018 char *path;
1021 #ifndef LITE
1022 OpenRootMenu(scr, -10000, -10000, False);
1023 wMenuUnmap(scr->root_menu);
1024 #endif
1026 make_keys();
1028 if (wScreenCount == 1) {
1029 path = wdefaultspathfordomain("WMState");
1030 } else {
1031 char buf[16];
1032 snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
1033 path = wdefaultspathfordomain(buf);
1035 scr->session_state = WMReadPropListFromFile(path);
1036 wfree(path);
1037 if (!scr->session_state && wScreenCount>1) {
1038 path = wdefaultspathfordomain("WMState");
1039 scr->session_state = WMReadPropListFromFile(path);
1040 wfree(path);
1043 if (!wPreferences.flags.noclip && scr->session_state) {
1044 state = WMGetFromPLDictionary(scr->session_state, dClip);
1045 scr->clip_icon = wClipRestoreState(scr, state);
1048 wWorkspaceRestoreState(scr);
1050 #ifdef VIRTUAL_DESKTOP
1052 * * create inputonly windows at the border of screen
1053 * */
1054 wWorkspaceManageEdge(scr);
1055 #endif
1057 if (!wPreferences.flags.nodock && scr->session_state) {
1058 state = WMGetFromPLDictionary(scr->session_state, dDock);
1059 scr->dock = wDockRestoreState(scr, state, WM_DOCK);
1062 wScreenUpdateUsableArea(scr);
1066 void
1067 wScreenSaveState(WScreen *scr)
1069 WWindow *wwin;
1070 char *str;
1071 WMPropList *old_state, *foo;
1072 CARD32 data[2];
1075 make_keys();
1077 /* Save current workspace, so can go back to it upon restart. */
1078 data[0] = scr->current_workspace;
1079 data[1] = WFLAGS_NONE;
1081 XChangeProperty(dpy, scr->root_win, _XA_WINDOWMAKER_STATE,
1082 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
1083 (unsigned char *) data, 2);
1085 /* save state of windows */
1086 wwin = scr->focused_window;
1087 while (wwin) {
1088 wWindowSaveState(wwin);
1089 wwin = wwin->prev;
1093 if (wPreferences.flags.noupdates)
1094 return;
1097 old_state = scr->session_state;
1098 scr->session_state = WMCreatePLDictionary(NULL, NULL, NULL);
1100 WMPLSetCaseSensitive(True);
1102 /* save dock state to file */
1103 if (!wPreferences.flags.nodock) {
1104 wDockSaveState(scr, old_state);
1105 } else {
1106 if ((foo = WMGetFromPLDictionary(old_state, dDock))!=NULL) {
1107 WMPutInPLDictionary(scr->session_state, dDock, foo);
1110 if (!wPreferences.flags.noclip) {
1111 wClipSaveState(scr);
1112 } else {
1113 if ((foo = WMGetFromPLDictionary(old_state, dClip))!=NULL) {
1114 WMPutInPLDictionary(scr->session_state, dClip, foo);
1118 wWorkspaceSaveState(scr, old_state);
1120 if (wPreferences.save_session_on_exit) {
1121 wSessionSaveState(scr);
1122 } else {
1123 if ((foo = WMGetFromPLDictionary(old_state, dApplications))!=NULL) {
1124 WMPutInPLDictionary(scr->session_state, dApplications, foo);
1126 if ((foo = WMGetFromPLDictionary(old_state, dWorkspace))!=NULL) {
1127 WMPutInPLDictionary(scr->session_state, dWorkspace, foo);
1131 /* clean up */
1132 WMPLSetCaseSensitive(False);
1134 wMenuSaveState(scr);
1136 if (wScreenCount == 1)
1137 str = wdefaultspathfordomain("WMState");
1138 else {
1139 char buf[16];
1140 snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
1141 str = wdefaultspathfordomain(buf);
1143 if (!WMWritePropListToFile(scr->session_state, str, True)) {
1144 wsyserror(_("could not save session state in %s"), str);
1146 wfree(str);
1147 WMReleasePropList(old_state);
1153 wScreenBringInside(WScreen *scr, int *x, int *y, int width, int height)
1155 int moved = 0;
1156 int tol_w, tol_h;
1158 if (width > 20)
1159 tol_w = width/2;
1160 else
1161 tol_w = 20;
1163 if (height > 20)
1164 tol_h = height/2;
1165 else
1166 tol_h = 20;
1168 if (*x+width < 10)
1169 *x = -tol_w, moved = 1;
1170 else if (*x >= scr->scr_width - 10)
1171 *x = scr->scr_width - tol_w - 1, moved = 1;
1173 if (*y < -height + 10)
1174 *y = -tol_h, moved = 1;
1175 else if (*y >= scr->scr_height - 10)
1176 *y = scr->scr_height - tol_h - 1, moved = 1;
1178 return moved;