- Fixed crashing bug in menu.c
[wmaker-crm.git] / src / screen.c
blob2c62f850f83329bc366c0605c7ebcfa8e2886cdc
1 /* screen.c - screen management
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997-2003 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 int gcm;
271 scr->stipple_bitmap =
272 XCreateBitmapFromData(dpy, scr->w_win, STIPPLE_DATA, STIPPLE_WIDTH,
273 STIPPLE_HEIGHT);
275 gcv.stipple = scr->stipple_bitmap;
276 gcv.foreground = scr->white_pixel;
277 gcv.fill_style = FillStippled;
278 gcv.graphics_exposures = False;
279 gcm = GCForeground|GCStipple|GCFillStyle|GCGraphicsExposures;
280 scr->stipple_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
283 /* selected icon border GCs */
284 gcv.function = GXcopy;
285 gcv.foreground = scr->white_pixel;
286 gcv.background = scr->black_pixel;
287 gcv.line_width = 1;
288 gcv.line_style = LineDoubleDash;
289 gcv.fill_style = FillSolid;
290 gcv.dash_offset = 0;
291 gcv.dashes = 4;
292 gcv.graphics_exposures = False;
294 gcm = GCFunction | GCGraphicsExposures;
295 gcm |= GCForeground | GCBackground;
296 gcm |= GCLineWidth | GCLineStyle;
297 gcm |= GCFillStyle;
298 gcm |= GCDashOffset | GCDashList;
300 scr->icon_select_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
302 scr->menu_title_color[0] = WMRetainColor(scr->white);
304 /* don't retain scr->black here because we may alter its alpha */
305 scr->mtext_color = WMCreateRGBColor(scr->wmscreen, 0, 0, 0, True);
306 scr->dtext_color = WMCreateRGBColor(scr->wmscreen, 0, 0, 0, True);
308 /* frame GC */
309 wGetColor(scr, DEF_FRAME_COLOR, &color);
310 gcv.function = GXxor;
311 /* this will raise the probability of the XORed color being different
312 * of the original color in PseudoColor when not all color cells are
313 * initialized */
314 if (DefaultVisual(dpy, scr->screen)->class==PseudoColor)
315 gcv.plane_mask = (1<<(scr->depth-1))|1;
316 else
317 gcv.plane_mask = AllPlanes;
318 gcv.foreground = color.pixel;
319 if (gcv.foreground == 0)
320 gcv.foreground = 1;
321 gcv.line_width = DEF_FRAME_THICKNESS;
322 gcv.subwindow_mode = IncludeInferiors;
323 gcv.graphics_exposures = False;
324 scr->frame_gc = XCreateGC(dpy, scr->root_win, GCForeground|GCGraphicsExposures
325 |GCFunction|GCSubwindowMode|GCLineWidth
326 |GCPlaneMask, &gcv);
328 /* line GC */
329 gcv.foreground = color.pixel;
331 if (gcv.foreground == 0)
332 /* XOR:ing with a zero is not going to be of much use, so
333 in that case, we somewhat arbitrarily xor with 17 instead. */
334 gcv.foreground = 17;
336 gcv.function = GXxor;
337 gcv.subwindow_mode = IncludeInferiors;
338 gcv.line_width = 1;
339 gcv.cap_style = CapRound;
340 gcv.graphics_exposures = False;
341 gcm = GCForeground|GCFunction|GCSubwindowMode|GCLineWidth|GCCapStyle
342 |GCGraphicsExposures;
343 scr->line_gc = XCreateGC(dpy, scr->root_win, gcm, &gcv);
345 scr->line_pixel = gcv.foreground;
347 /* copy GC */
348 gcv.foreground = scr->white_pixel;
349 gcv.background = scr->black_pixel;
350 gcv.graphics_exposures = False;
351 scr->copy_gc = XCreateGC(dpy, scr->w_win, GCForeground|GCBackground
352 |GCGraphicsExposures, &gcv);
354 /* misc drawing GC */
355 gcv.graphics_exposures = False;
356 gcm = GCGraphicsExposures;
357 scr->draw_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
359 assert (scr->stipple_bitmap!=None);
362 /* mono GC */
363 scr->mono_gc = XCreateGC(dpy, scr->stipple_bitmap, gcm, &gcv);
368 static void
369 createPixmaps(WScreen *scr)
371 WPixmap *pix;
372 RImage *image;
374 /* load pixmaps */
375 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_RADIO_INDICATOR_XBM_DATA,
376 (char*)MENU_RADIO_INDICATOR_XBM_DATA,
377 MENU_RADIO_INDICATOR_XBM_SIZE,
378 MENU_RADIO_INDICATOR_XBM_SIZE,
379 scr->black_pixel, scr->white_pixel);
380 if (pix!=NULL)
381 pix->shared = 1;
382 scr->menu_radio_indicator = pix;
385 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_CHECK_INDICATOR_XBM_DATA,
386 (char*)MENU_CHECK_INDICATOR_XBM_DATA,
387 MENU_CHECK_INDICATOR_XBM_SIZE,
388 MENU_CHECK_INDICATOR_XBM_SIZE,
389 scr->black_pixel, scr->white_pixel);
390 if (pix!=NULL)
391 pix->shared = 1;
392 scr->menu_check_indicator = pix;
394 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_MINI_INDICATOR_XBM_DATA,
395 (char*)MENU_MINI_INDICATOR_XBM_DATA,
396 MENU_MINI_INDICATOR_XBM_SIZE,
397 MENU_MINI_INDICATOR_XBM_SIZE,
398 scr->black_pixel, scr->white_pixel);
399 if (pix!=NULL)
400 pix->shared = 1;
401 scr->menu_mini_indicator = pix;
403 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_HIDE_INDICATOR_XBM_DATA,
404 (char*)MENU_HIDE_INDICATOR_XBM_DATA,
405 MENU_HIDE_INDICATOR_XBM_SIZE,
406 MENU_HIDE_INDICATOR_XBM_SIZE,
407 scr->black_pixel, scr->white_pixel);
408 if (pix!=NULL)
409 pix->shared = 1;
410 scr->menu_hide_indicator = pix;
412 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_SHADE_INDICATOR_XBM_DATA,
413 (char*)MENU_SHADE_INDICATOR_XBM_DATA,
414 MENU_SHADE_INDICATOR_XBM_SIZE,
415 MENU_SHADE_INDICATOR_XBM_SIZE,
416 scr->black_pixel, scr->white_pixel);
417 if (pix!=NULL)
418 pix->shared = 1;
419 scr->menu_shade_indicator = pix;
422 image = wDefaultGetImage(scr, "Logo", "WMPanel");
424 if (!image) {
425 wwarning(_("could not load logo image for panels: %s"),
426 RMessageForError(RErrorCode));
427 } else {
428 WMSetApplicationIconImage(scr->wmscreen, image);
429 RReleaseImage(image);
432 scr->dock_dots = make3Dots(scr);
434 /* titlebar button pixmaps */
435 allocButtonPixmaps(scr);
440 *----------------------------------------------------------------------
441 * createInternalWindows--
442 * Creates some windows used internally by the program. One to
443 * receive input focus when no other window can get it and another
444 * to display window geometry information during window resize/move.
446 * Returns:
447 * Nothing
449 * Side effects:
450 * Windows are created and some colors are allocated for the
451 * window background.
452 *----------------------------------------------------------------------
454 static void
455 createInternalWindows(WScreen *scr)
457 int vmask;
458 XSetWindowAttributes attribs;
460 /* InputOnly window to get the focus when no other window can get it */
461 vmask = CWEventMask|CWOverrideRedirect;
462 attribs.event_mask = KeyPressMask|FocusChangeMask;
463 attribs.override_redirect = True;
464 scr->no_focus_win=XCreateWindow(dpy,scr->root_win,-10, -10, 4, 4, 0, 0,
465 InputOnly,CopyFromParent, vmask, &attribs);
466 XSelectInput(dpy, scr->no_focus_win, KeyPressMask|KeyReleaseMask);
467 XMapWindow(dpy, scr->no_focus_win);
469 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
471 /* shadow window for dock buttons */
472 vmask = CWBorderPixel|CWBackPixmap|CWBackPixel|CWCursor|CWSaveUnder|CWOverrideRedirect;
473 attribs.border_pixel = scr->black_pixel;
474 attribs.save_under = True;
475 attribs.override_redirect = True;
476 attribs.background_pixmap = None;
477 attribs.background_pixel = scr->white_pixel;
478 attribs.cursor = wCursor[WCUR_DEFAULT];
479 vmask |= CWColormap;
480 attribs.colormap = scr->w_colormap;
481 scr->dock_shadow =
482 XCreateWindow(dpy, scr->root_win, 0, 0, wPreferences.icon_size,
483 wPreferences.icon_size, 0, scr->w_depth, CopyFromParent,
484 scr->w_visual, vmask, &attribs);
486 /* workspace name balloon for clip */
487 vmask = CWBackPixel|CWSaveUnder|CWOverrideRedirect|CWColormap
488 |CWBorderPixel;
489 attribs.save_under = True;
490 attribs.override_redirect = True;
491 attribs.colormap = scr->w_colormap;
492 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
493 attribs.border_pixel = 0; /* do not care */
494 scr->clip_balloon =
495 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
496 CopyFromParent, scr->w_visual, vmask, &attribs);
499 /* workspace name */
500 vmask = CWBackPixel|CWSaveUnder|CWOverrideRedirect|CWColormap
501 |CWBorderPixel;
502 attribs.save_under = True;
503 attribs.override_redirect = True;
504 attribs.colormap = scr->w_colormap;
505 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
506 attribs.border_pixel = 0; /* do not care */
507 scr->workspace_name =
508 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
509 CopyFromParent, scr->w_visual, vmask, &attribs);
512 * If the window is clicked without having ButtonPress selected, the
513 * resulting event will have event.xbutton.window == root.
515 XSelectInput(dpy, scr->clip_balloon, ButtonPressMask);
519 #if 0
520 static Bool
521 aquireManagerSelection(WScreen *scr)
523 char buffer[32];
524 XEvent ev;
525 Time timestamp;
527 snprintf(buffer, sizeof(buffer), "WM_S%i", scr->screen);
528 scr->managerAtom = XInternAtom(dpy, buffer, False);
530 /* for race-conditions... */
531 XGrabServer(dpy);
533 /* if there is another manager running, don't try to replace it
534 * (for now, at least) */
535 if (XGetSelectionOwner(dpy, scr->managerAtom) != None) {
536 XUngrabServer(dpy);
537 return False;
540 /* become the manager for this screen */
542 scr->managerWindow = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, 1, 1,
543 0, 0, 0);
545 XSelectInput(dpy, scr->managerWindow, PropertyChangeMask);
546 /* get a timestamp */
547 XChangeProperty(dpy, scr->managerWindow, scr->managerAtom,
548 XA_INTEGER, 32, PropModeAppend, NULL, 0);
549 while (1) {
550 XWindowEvent(dpy, scr->managerWindow, &ev);
551 if (ev.type == PropertyNotify) {
552 timestamp = ev.xproperty.time;
553 break;
556 XSelectInput(dpy, scr->managerWindow, NoEvents);
557 XDeleteProperty(dpy, scr->managerWindow, scr->managerAtom);
559 XSetSelectionOwner(dpy, scr->managerAtom, scr->managerWindow, CurrentTime);
561 XUngrabServer(dpy);
563 /* announce our arrival */
565 ev.xclient.type = ClientMessage;
566 ev.xclient.message_type = XInternAtom(dpy, "MANAGER", False);
567 ev.xclient.destination = scr->root_win;
568 ev.xclient.format = 32;
569 ev.xclient.data.l[0] = timestamp;
570 ev.xclient.data.l[1] = scr->managerAtom;
571 ev.xclient.data.l[2] = scr->managerWindow;
572 ev.xclient.data.l[3] = 0;
573 ev.xclient.data.l[4] = 0;
575 XSendEvent(dpy, scr->root_win, False, StructureNotify, &ev);
576 XSync(dpy, False);
578 return True;
580 #endif
583 *----------------------------------------------------------------------
584 * wScreenInit--
585 * Initializes the window manager for the given screen and
586 * allocates a WScreen descriptor for it. Many resources are allocated
587 * for the screen and the root window is setup appropriately.
589 * Returns:
590 * The WScreen descriptor for the screen.
592 * Side effects:
593 * Many resources are allocated and the IconSize property is
594 * set on the root window.
595 * The program can be aborted if some fatal error occurs.
597 * TODO: User specifiable visual.
598 *----------------------------------------------------------------------
600 WScreen*
601 wScreenInit(int screen_number)
603 WScreen *scr;
604 XIconSize icon_size[1];
605 RContextAttributes rattr;
606 extern int wVisualID;
607 long event_mask;
608 XErrorHandler oldHandler;
610 scr = wmalloc(sizeof(WScreen));
611 memset(scr, 0, sizeof(WScreen));
613 scr->stacking_list = WMCreateTreeBag();
615 /* initialize globals */
616 scr->screen = screen_number;
617 scr->root_win = RootWindow(dpy, screen_number);
618 scr->depth = DefaultDepth(dpy, screen_number);
619 scr->colormap = DefaultColormap(dpy, screen_number);
621 scr->scr_width = WidthOfScreen(ScreenOfDisplay(dpy, screen_number));
622 scr->scr_height = HeightOfScreen(ScreenOfDisplay(dpy, screen_number));
624 scr->usableArea.x2 = scr->scr_width;
625 scr->usableArea.y2 = scr->scr_height;
626 scr->totalUsableArea.x2 = scr->scr_width;
627 scr->totalUsableArea.y2 = scr->scr_height;
629 scr->fakeGroupLeaders = WMCreateArray(16);
631 #if 0
632 if (!aquireManagerSelection(scr)) {
633 wfree(scr);
635 return NULL;
637 #endif
638 CantManageScreen = 0;
639 oldHandler = XSetErrorHandler((XErrorHandler)alreadyRunningError);
641 event_mask = EVENT_MASK;
643 if (wPreferences.disable_root_mouse) {
644 event_mask &= ~(ButtonPressMask|ButtonReleaseMask);
647 XSelectInput(dpy, scr->root_win, event_mask);
649 #ifdef KEEP_XKB_LOCK_STATUS
650 /* Only GroupLock doesn't work correctly in my system since right-alt
651 * can change mode while holding it too - ]d
653 if (wXkbSupported) {
654 XkbSelectEvents(dpy,XkbUseCoreKbd,
655 XkbStateNotifyMask,
656 XkbStateNotifyMask);
658 #endif /* KEEP_XKB_LOCK_STATUS */
660 XSync(dpy, False);
661 XSetErrorHandler(oldHandler);
663 if (CantManageScreen) {
664 wfree(scr);
665 return NULL;
668 wInitXinerama(scr);
670 XDefineCursor(dpy, scr->root_win, wCursor[WCUR_ROOT]);
672 /* screen descriptor for raster graphic library */
673 rattr.flags = RC_RenderMode | RC_ColorsPerChannel | RC_StandardColormap;
674 rattr.render_mode = wPreferences.no_dithering
675 ? RBestMatchRendering
676 : RDitheredRendering;
678 /* if the std colormap stuff works ok, this will be ignored */
679 rattr.colors_per_channel = wPreferences.cmap_size;
680 if (rattr.colors_per_channel<2)
681 rattr.colors_per_channel = 2;
684 /* will only be accounted for in PseudoColor */
685 if (wPreferences.flags.create_stdcmap) {
686 rattr.standard_colormap_mode = RCreateStdColormap;
687 } else {
688 rattr.standard_colormap_mode = RUseStdColormap;
691 if (wVisualID>=0) {
692 rattr.flags |= RC_VisualID;
693 rattr.visualid = wVisualID;
696 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
698 if (!scr->rcontext && RErrorCode == RERR_STDCMAPFAIL) {
699 wwarning(RMessageForError(RErrorCode));
701 rattr.flags &= ~RC_StandardColormap;
702 rattr.standard_colormap_mode = RUseStdColormap;
704 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
707 if (!scr->rcontext) {
708 wwarning(_("could not initialize graphics library context: %s"),
709 RMessageForError(RErrorCode));
710 wAbort(False);
711 } else {
712 char **formats;
713 int i = 0;
715 formats = RSupportedFileFormats();
716 if (formats) {
717 for (i=0; formats[i]!=NULL; i++) {
718 if (strcmp(formats[i], "TIFF")==0) {
719 scr->flags.supports_tiff = 1;
720 break;
726 scr->w_win = scr->rcontext->drawable;
727 scr->w_visual = scr->rcontext->visual;
728 scr->w_depth = scr->rcontext->depth;
729 scr->w_colormap = scr->rcontext->cmap;
731 /* create screen descriptor for WINGs */
732 scr->wmscreen = WMCreateScreenWithRContext(dpy, screen_number,
733 scr->rcontext);
735 if (!scr->wmscreen) {
736 wfatal(_("could not initialize WINGs widget set"));
737 return NULL;
740 scr->black = WMBlackColor(scr->wmscreen);
741 scr->white = WMWhiteColor(scr->wmscreen);
742 scr->gray = WMGrayColor(scr->wmscreen);
743 scr->darkGray = WMDarkGrayColor(scr->wmscreen);
745 scr->black_pixel = WMColorPixel(scr->black); /*scr->rcontext->black;*/
746 scr->white_pixel = WMColorPixel(scr->white); /*scr->rcontext->white;*/
747 scr->light_pixel = WMColorPixel(scr->gray);
748 scr->dark_pixel = WMColorPixel(scr->darkGray);
751 XColor xcol;
752 /* frame boder color */
753 wGetColor(scr, FRAME_BORDER_COLOR, &xcol);
754 scr->frame_border_pixel = xcol.pixel;
757 /* create GCs with default values */
758 allocGCs(scr);
760 /* for our window manager info notice board. Need to
761 * create before reading the defaults, because it will be used there.
763 scr->info_window = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, 10, 10,
764 0, 0, 0);
766 /* read defaults for this screen */
767 wReadDefaults(scr, WDWindowMaker->dictionary);
769 createInternalWindows(scr);
771 #ifdef KWM_HINTS
772 wKWMInitStuff(scr);
773 #endif
775 #ifdef GNOME_STUFF
776 wGNOMEInitStuff(scr);
777 #endif
779 #ifdef OLWM_HINTS
780 wOLWMInitStuff(scr);
781 #endif
783 /* create initial workspace */
784 wWorkspaceNew(scr);
786 /* create shared pixmaps */
787 createPixmaps(scr);
789 /* set icon sizes we can accept from clients */
790 icon_size[0].min_width = 8;
791 icon_size[0].min_height = 8;
792 icon_size[0].max_width = wPreferences.icon_size-4;
793 icon_size[0].max_height = wPreferences.icon_size-4;
794 icon_size[0].width_inc = 1;
795 icon_size[0].height_inc = 1;
796 XSetIconSizes(dpy, scr->root_win, icon_size, 1);
798 /* setup WindowMaker protocols property in the root window*/
799 PropSetWMakerProtocols(scr->root_win);
801 /* setup our noticeboard */
802 XChangeProperty(dpy, scr->info_window, _XA_WINDOWMAKER_NOTICEBOARD,
803 XA_WINDOW, 32, PropModeReplace,
804 (unsigned char*)&scr->info_window, 1);
805 XChangeProperty(dpy, scr->root_win, _XA_WINDOWMAKER_NOTICEBOARD,
806 XA_WINDOW, 32, PropModeReplace,
807 (unsigned char*)&scr->info_window, 1);
810 #ifdef BALLOON_TEXT
811 /* initialize balloon text stuff */
812 wBalloonInitialize(scr);
813 #endif
815 scr->info_text_font = WMBoldSystemFontOfSize(scr->wmscreen, 12);
817 scr->tech_draw_font = WMCreateFontWithFlags(scr->wmscreen,
818 "BoldSystemFont-12",
819 WFNotAntialiased);
821 scr->gview = WCreateGeometryView(scr->wmscreen);
822 WMRealizeWidget(scr->gview);
824 wScreenUpdateUsableArea(scr);
826 return scr;
831 void
832 wScreenUpdateUsableArea(WScreen *scr)
834 scr->totalUsableArea = scr->usableArea;
837 if (scr->dock && (!scr->dock->lowered
838 || wPreferences.no_window_over_dock)) {
840 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
842 if (scr->dock->on_right_side) {
843 scr->totalUsableArea.x2 = WMIN(scr->totalUsableArea.x2,
844 scr->scr_width - offset);
845 } else {
846 scr->totalUsableArea.x1 = WMAX(scr->totalUsableArea.x1, offset);
850 if (wPreferences.no_window_over_icons) {
851 if (wPreferences.icon_yard & IY_VERT) {
853 if (!(wPreferences.icon_yard & IY_RIGHT)) {
854 scr->totalUsableArea.x1 += wPreferences.icon_size;
855 } else {
856 scr->totalUsableArea.x2 -= wPreferences.icon_size;
858 } else {
860 if (wPreferences.icon_yard & IY_TOP) {
861 scr->totalUsableArea.y1 += wPreferences.icon_size;
862 } else {
863 scr->totalUsableArea.y2 -= wPreferences.icon_size;
868 #ifdef KWM_HINTS
870 WArea area;
872 if (wKWMGetUsableArea(scr, &area)) {
873 scr->totalUsableArea.x1 = WMAX(scr->totalUsableArea.x1, area.x1);
874 scr->totalUsableArea.y1 = WMAX(scr->totalUsableArea.y1, area.y1);
875 scr->totalUsableArea.x2 = WMIN(scr->totalUsableArea.x2, area.x2);
876 scr->totalUsableArea.y2 = WMIN(scr->totalUsableArea.y2, area.y2);
879 #endif
881 #ifdef GNOME_STUFF
883 WReservedArea *area = scr->reservedAreas;
885 while (area) {
886 int th, bh;
887 int lw, rw;
888 int w, h;
890 w = area->area.x2 - area->area.x1;
891 h = area->area.y2 - area->area.y1;
893 th = area->area.y1;
894 bh = scr->scr_height - area->area.y2;
895 lw = area->area.x1;
896 rw = scr->scr_width - area->area.x2;
898 if (WMIN(th, bh) <= WMIN(lw, rw)) {
899 /* horizontal */
900 if (th < bh) {
901 /* on top */
902 if (scr->totalUsableArea.y1 < area->area.y2)
903 scr->totalUsableArea.y1 = area->area.y2;
904 } else {
905 /* on bottom */
906 if (scr->totalUsableArea.y2 > area->area.y1)
907 scr->totalUsableArea.y2 = area->area.y1;
909 } else {
910 /* vertical */
911 if (lw < rw) {
912 /* on left */
913 if (scr->totalUsableArea.x1 < area->area.x2)
914 scr->totalUsableArea.x1 = area->area.x2;
915 } else {
916 /* on right */
917 if (scr->totalUsableArea.x2 > area->area.x1)
918 scr->totalUsableArea.x2 = area->area.x1;
922 area = area->next;
925 #endif /* GNOME_STUFF */
927 if (scr->totalUsableArea.x2 - scr->totalUsableArea.x1 < scr->scr_width/2) {
928 scr->totalUsableArea.x2 = scr->usableArea.x2;
929 scr->totalUsableArea.x1 = scr->usableArea.x1;
931 if (scr->totalUsableArea.y2 - scr->totalUsableArea.y1 < scr->scr_height/2) {
932 scr->totalUsableArea.y2 = scr->usableArea.y2;
933 scr->totalUsableArea.y1 = scr->usableArea.y1;
936 #ifdef not_used
937 #ifdef KWM_HINTS
939 int i;
941 for (i = 0; i < scr->workspace_count; i++) {
942 wKWMSetUsableAreaHint(scr, i);
945 #endif
946 #endif
949 unsigned size = wPreferences.workspace_border_size;
950 unsigned position = wPreferences.workspace_border_position;
952 if (size>0 && position!=WB_NONE) {
953 if (position & WB_LEFTRIGHT) {
954 scr->totalUsableArea.x1 += size;
955 scr->totalUsableArea.x2 -= size;
957 if (position & WB_TOPBOTTOM) {
958 scr->totalUsableArea.y1 += size;
959 scr->totalUsableArea.y2 -= size;
967 void
968 wScreenRestoreState(WScreen *scr)
970 WMPropList *state;
971 char *path;
974 #ifndef LITE
975 OpenRootMenu(scr, -10000, -10000, False);
976 wMenuUnmap(scr->root_menu);
977 #endif
979 make_keys();
981 if (wScreenCount == 1) {
982 path = wdefaultspathfordomain("WMState");
983 } else {
984 char buf[16];
985 snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
986 path = wdefaultspathfordomain(buf);
988 scr->session_state = WMReadPropListFromFile(path);
989 wfree(path);
990 if (!scr->session_state && wScreenCount>1) {
991 path = wdefaultspathfordomain("WMState");
992 scr->session_state = WMReadPropListFromFile(path);
993 wfree(path);
996 if (!wPreferences.flags.noclip && scr->session_state) {
997 state = WMGetFromPLDictionary(scr->session_state, dClip);
998 scr->clip_icon = wClipRestoreState(scr, state);
1001 wWorkspaceRestoreState(scr);
1003 #ifdef VIRTUAL_DESKTOP
1005 * * create inputonly windows at the border of screen
1006 * */
1007 wWorkspaceManageEdge(scr);
1008 #endif
1010 if (!wPreferences.flags.nodock && scr->session_state) {
1011 state = WMGetFromPLDictionary(scr->session_state, dDock);
1012 scr->dock = wDockRestoreState(scr, state, WM_DOCK);
1015 wScreenUpdateUsableArea(scr);
1019 void
1020 wScreenSaveState(WScreen *scr)
1022 WWindow *wwin;
1023 char *str;
1024 WMPropList *old_state, *foo;
1025 CARD32 data[2];
1028 make_keys();
1030 /* Save current workspace, so can go back to it upon restart. */
1031 data[0] = scr->current_workspace;
1032 data[1] = WFLAGS_NONE;
1034 XChangeProperty(dpy, scr->root_win, _XA_WINDOWMAKER_STATE,
1035 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
1036 (unsigned char *) data, 2);
1038 /* save state of windows */
1039 wwin = scr->focused_window;
1040 while (wwin) {
1041 wWindowSaveState(wwin);
1042 wwin = wwin->prev;
1046 if (wPreferences.flags.noupdates)
1047 return;
1050 old_state = scr->session_state;
1051 scr->session_state = WMCreatePLDictionary(NULL, NULL, NULL);
1053 WMPLSetCaseSensitive(True);
1055 /* save dock state to file */
1056 if (!wPreferences.flags.nodock) {
1057 wDockSaveState(scr, old_state);
1058 } else {
1059 if ((foo = WMGetFromPLDictionary(old_state, dDock))!=NULL) {
1060 WMPutInPLDictionary(scr->session_state, dDock, foo);
1063 if (!wPreferences.flags.noclip) {
1064 wClipSaveState(scr);
1065 } else {
1066 if ((foo = WMGetFromPLDictionary(old_state, dClip))!=NULL) {
1067 WMPutInPLDictionary(scr->session_state, dClip, foo);
1071 wWorkspaceSaveState(scr, old_state);
1073 if (wPreferences.save_session_on_exit) {
1074 wSessionSaveState(scr);
1075 } else {
1076 if ((foo = WMGetFromPLDictionary(old_state, dApplications))!=NULL) {
1077 WMPutInPLDictionary(scr->session_state, dApplications, foo);
1079 if ((foo = WMGetFromPLDictionary(old_state, dWorkspace))!=NULL) {
1080 WMPutInPLDictionary(scr->session_state, dWorkspace, foo);
1084 /* clean up */
1085 WMPLSetCaseSensitive(False);
1087 wMenuSaveState(scr);
1089 if (wScreenCount == 1)
1090 str = wdefaultspathfordomain("WMState");
1091 else {
1092 char buf[16];
1093 snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
1094 str = wdefaultspathfordomain(buf);
1096 if (!WMWritePropListToFile(scr->session_state, str, True)) {
1097 wsyserror(_("could not save session state in %s"), str);
1099 wfree(str);
1100 WMReleasePropList(old_state);
1106 wScreenBringInside(WScreen *scr, int *x, int *y, int width, int height)
1108 int moved = 0;
1109 int tol_w, tol_h;
1111 * With respect to the head that contains most of the window.
1113 int sx1, sy1, sx2, sy2;
1115 WMRect rect;
1116 int head, flags;
1118 rect.pos.x = *x;
1119 rect.pos.y = *y;
1120 rect.size.width = width;
1121 rect.size.height = height;
1123 head = wGetRectPlacementInfo(scr, rect, &flags);
1124 rect = wGetRectForHead(scr, head);
1126 sx1 = rect.pos.x;
1127 sy1 = rect.pos.y;
1128 sx2 = sx1 + rect.size.width;
1129 sy2 = sy1 + rect.size.height;
1131 #if 0 /* NOTE: gives funky group movement */
1132 if (flags & XFLAG_MULTIPLE) {
1134 * since we span multiple heads, pull window totaly inside
1136 if (*x < sx1)
1137 *x = sx1, moved = 1;
1138 else if (*x + width > sx2)
1139 *x = sx2 - width, moved = 1;
1141 if (*y < sy1)
1142 *y = sy1, moved = 1;
1143 else if (*y + height > sy2)
1144 *y = sy2 - height, moved = 1;
1146 return moved;
1148 #endif
1150 if (width > 20)
1151 tol_w = width/2;
1152 else
1153 tol_w = 20;
1155 if (height > 20)
1156 tol_h = height/2;
1157 else
1158 tol_h = 20;
1160 if (*x + width < sx1 + 10)
1161 *x = sx1 - tol_w, moved = 1;
1162 else if (*x >= sx2 - 10)
1163 *x = sx2 - tol_w - 1, moved = 1;
1165 if (*y < sy1 - height + 10)
1166 *y = sy1 - tol_h, moved = 1;
1167 else if (*y >= sy2 - 10)
1168 *y = sy2 - tol_h - 1, moved = 1;
1170 return moved;
1176 wScreenKeepInside(WScreen *scr, int *x, int *y, int width, int height)
1178 int moved = 0;
1179 int sx1, sy1, sx2, sy2;
1180 WMRect rect;
1181 int head;
1183 rect.pos.x = *x;
1184 rect.pos.y = *y;
1185 rect.size.width = width;
1186 rect.size.height = height;
1188 head = wGetHeadForRect(scr, rect);
1189 rect = wGetRectForHead(scr, head);
1191 sx1 = rect.pos.x;
1192 sy1 = rect.pos.y;
1193 sx2 = sx1 + rect.size.width;
1194 sy2 = sy1 + rect.size.height;
1196 if (*x < sx1)
1197 *x = sx1, moved = 1;
1198 else if (*x + width > sx2)
1199 *x = sx2 - width, moved = 1;
1201 if (*y < sy1)
1202 *y = sy1, moved = 1;
1203 else if (*y + height > sy2)
1204 *y = sy2 - height, moved = 1;
1206 return moved;