WMaker: Fixed crash if the RContext creation fail on a Screen (Coverity #50066)
[wmaker-crm.git] / src / screen.c
blob51e2317818e084ee0a88a01b7b6bb3ba7f5c21bb
1 /* screen.c - screen management
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include <X11/Xlib.h>
29 #include <X11/Xutil.h>
30 #include <X11/Xatom.h>
31 #ifdef KEEP_XKB_LOCK_STATUS
32 #include <X11/XKBlib.h>
33 #endif /* KEEP_XKB_LOCK_STATUS */
34 #ifdef USE_RANDR
35 #include <X11/extensions/Xrandr.h>
36 #endif
38 #include <wraster.h>
39 #include "WindowMaker.h"
40 #include "def_pixmaps.h"
41 #include "screen.h"
42 #include "texture.h"
43 #include "pixmap.h"
44 #include "menu.h"
45 #include "window.h"
46 #include "main.h"
47 #include "actions.h"
48 #include "properties.h"
49 #include "dock.h"
50 #include "resources.h"
51 #include "workspace.h"
52 #include "session.h"
53 #include "balloon.h"
54 #include "geomview.h"
55 #include "wmspec.h"
56 #include "rootmenu.h"
58 #include "xinerama.h"
60 #include <WINGs/WUtil.h>
62 #include "defaults.h"
64 #define EVENT_MASK (LeaveWindowMask|EnterWindowMask|PropertyChangeMask\
65 |SubstructureNotifyMask|PointerMotionMask \
66 |SubstructureRedirectMask|ButtonPressMask|ButtonReleaseMask\
67 |KeyPressMask|KeyReleaseMask)
69 #define STIPPLE_WIDTH 2
70 #define STIPPLE_HEIGHT 2
71 static char STIPPLE_DATA[] = { 0x02, 0x01 };
73 static int CantManageScreen = 0;
75 static WMPropList *dApplications = NULL;
76 static WMPropList *dWorkspace;
77 static WMPropList *dDock;
78 static WMPropList *dClip;
79 static WMPropList *dDrawers = NULL;
81 static void make_keys(void)
83 if (dApplications != NULL)
84 return;
86 dApplications = WMCreatePLString("Applications");
87 dWorkspace = WMCreatePLString("Workspace");
88 dDock = WMCreatePLString("Dock");
89 dClip = WMCreatePLString("Clip");
90 dDrawers = WMCreatePLString("Drawers");
94 *----------------------------------------------------------------------
95 * alreadyRunningError--
96 * X error handler used to catch errors when trying to do
97 * XSelectInput() on the root window. These errors probably mean that
98 * there already is some other window manager running.
100 * Returns:
101 * Nothing, unless something really evil happens...
103 * Side effects:
104 * CantManageScreen is set to 1;
105 *----------------------------------------------------------------------
107 static int alreadyRunningError(Display * dpy, XErrorEvent * error)
109 /* Parameter not used, but tell the compiler that it is ok */
110 (void) dpy;
111 (void) error;
113 CantManageScreen = 1;
114 return -1;
118 *----------------------------------------------------------------------
119 * allocButtonPixmaps--
120 * Allocate pixmaps used on window operation buttons (those in the
121 * titlebar). The pixmaps are linked to the program. If XPM is supported
122 * XPM pixmaps are used otherwise, equivalent bitmaps are used.
124 * Returns:
125 * Nothing
127 * Side effects:
128 * Allocates shared pixmaps for the screen. These pixmaps should
129 * not be freed by anybody.
130 *----------------------------------------------------------------------
132 static void allocButtonPixmaps(WScreen * scr)
134 WPixmap *pix;
136 /* create predefined pixmaps */
137 if (wPreferences.new_style == TS_NEXT) {
138 pix = wPixmapCreateFromXPMData(scr, NEXT_CLOSE_XPM);
139 } else {
140 pix = wPixmapCreateFromXPMData(scr, PRED_CLOSE_XPM);
142 if (pix)
143 pix->shared = 1;
144 scr->b_pixmaps[WBUT_CLOSE] = pix;
146 if (wPreferences.new_style == TS_NEXT) {
147 pix = wPixmapCreateFromXPMData(scr, NEXT_BROKEN_CLOSE_XPM);
148 } else {
149 pix = wPixmapCreateFromXPMData(scr, PRED_BROKEN_CLOSE_XPM);
151 if (pix)
152 pix->shared = 1;
153 scr->b_pixmaps[WBUT_BROKENCLOSE] = pix;
155 if (wPreferences.new_style == TS_NEXT) {
156 pix = wPixmapCreateFromXPMData(scr, NEXT_ICONIFY_XPM);
157 } else {
158 pix = wPixmapCreateFromXPMData(scr, PRED_ICONIFY_XPM);
160 if (pix)
161 pix->shared = 1;
162 scr->b_pixmaps[WBUT_ICONIFY] = pix;
163 #ifdef XKB_BUTTON_HINT
164 if (wPreferences.new_style == TS_NEXT) {
165 pix = wPixmapCreateFromXPMData(scr, NEXT_XKBGROUP1_XPM);
166 } else {
167 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP1_XPM);
169 if (pix)
170 pix->shared = 1;
171 scr->b_pixmaps[WBUT_XKBGROUP1] = pix;
172 if (wPreferences.new_style == TS_NEXT) {
173 pix = wPixmapCreateFromXPMData(scr, NEXT_XKBGROUP2_XPM);
174 } else {
175 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP2_XPM);
177 if (pix)
178 pix->shared = 1;
179 scr->b_pixmaps[WBUT_XKBGROUP2] = pix;
180 if (wPreferences.new_style == TS_NEXT) {
181 pix = wPixmapCreateFromXPMData(scr, NEXT_XKBGROUP3_XPM);
182 } else {
183 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP3_XPM);
185 if (pix)
186 pix->shared = 1;
187 scr->b_pixmaps[WBUT_XKBGROUP3] = pix;
188 if (wPreferences.new_style == TS_NEXT) {
189 pix = wPixmapCreateFromXPMData(scr, NEXT_XKBGROUP4_XPM);
190 } else {
191 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP4_XPM);
193 if (pix)
194 pix->shared = 1;
195 scr->b_pixmaps[WBUT_XKBGROUP4] = pix;
196 #endif
198 if (wPreferences.new_style == TS_NEXT) {
199 pix = wPixmapCreateFromXPMData(scr, NEXT_KILL_XPM);
200 } else {
201 pix = wPixmapCreateFromXPMData(scr, PRED_KILL_XPM);
203 if (pix)
204 pix->shared = 1;
205 scr->b_pixmaps[WBUT_KILL] = pix;
208 static void draw_dot(WScreen * scr, Drawable d, int x, int y, GC gc)
210 XSetForeground(dpy, gc, scr->black_pixel);
211 XDrawLine(dpy, d, gc, x, y, x + 1, y);
212 XDrawPoint(dpy, d, gc, x, y + 1);
213 XSetForeground(dpy, gc, scr->white_pixel);
214 XDrawLine(dpy, d, gc, x + 2, y, x + 2, y + 1);
215 XDrawPoint(dpy, d, gc, x + 1, y + 1);
218 static WPixmap *make3Dots(WScreen * scr)
220 WPixmap *wpix;
221 GC gc2, gc;
222 XGCValues gcv;
223 Pixmap pix, mask;
225 gc = scr->copy_gc;
226 pix = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size, wPreferences.icon_size, scr->w_depth);
227 XSetForeground(dpy, gc, scr->black_pixel);
228 XFillRectangle(dpy, pix, gc, 0, 0, wPreferences.icon_size, wPreferences.icon_size);
229 XSetForeground(dpy, gc, scr->white_pixel);
230 draw_dot(scr, pix, 4, wPreferences.icon_size - 6, gc);
231 draw_dot(scr, pix, 9, wPreferences.icon_size - 6, gc);
232 draw_dot(scr, pix, 14, wPreferences.icon_size - 6, gc);
234 mask = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size, wPreferences.icon_size, 1);
235 gcv.foreground = 0;
236 gcv.graphics_exposures = False;
237 gc2 = XCreateGC(dpy, mask, GCForeground | GCGraphicsExposures, &gcv);
238 XFillRectangle(dpy, mask, gc2, 0, 0, wPreferences.icon_size, wPreferences.icon_size);
239 XSetForeground(dpy, gc2, 1);
240 XFillRectangle(dpy, mask, gc2, 4, wPreferences.icon_size - 6, 3, 2);
241 XFillRectangle(dpy, mask, gc2, 9, wPreferences.icon_size - 6, 3, 2);
242 XFillRectangle(dpy, mask, gc2, 14, wPreferences.icon_size - 6, 3, 2);
244 XFreeGC(dpy, gc2);
246 wpix = wPixmapCreate(pix, mask);
247 wpix->shared = 1;
249 return wpix;
252 static void allocGCs(WScreen * scr)
254 XGCValues gcv;
255 XColor color;
256 int gcm;
258 scr->stipple_bitmap = XCreateBitmapFromData(dpy, scr->w_win, STIPPLE_DATA, STIPPLE_WIDTH, STIPPLE_HEIGHT);
260 gcv.stipple = scr->stipple_bitmap;
261 gcv.foreground = scr->white_pixel;
262 gcv.fill_style = FillStippled;
263 gcv.graphics_exposures = False;
264 gcm = GCForeground | GCStipple | GCFillStyle | GCGraphicsExposures;
265 scr->stipple_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
267 /* selected icon border GCs */
268 gcv.function = GXcopy;
269 gcv.foreground = scr->white_pixel;
270 gcv.background = scr->black_pixel;
271 gcv.line_width = 1;
272 gcv.line_style = LineDoubleDash;
273 gcv.fill_style = FillSolid;
274 gcv.dash_offset = 0;
275 gcv.dashes = 4;
276 gcv.graphics_exposures = False;
278 gcm = GCFunction | GCGraphicsExposures;
279 gcm |= GCForeground | GCBackground;
280 gcm |= GCLineWidth | GCLineStyle;
281 gcm |= GCFillStyle;
282 gcm |= GCDashOffset | GCDashList;
284 scr->icon_select_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
286 scr->menu_title_color[0] = WMRetainColor(scr->white);
288 /* don't retain scr->black here because we may alter its alpha */
289 scr->mtext_color = WMCreateRGBColor(scr->wmscreen, 0, 0, 0, True);
290 scr->dtext_color = WMCreateRGBColor(scr->wmscreen, 0, 0, 0, True);
292 /* frame GC */
293 wGetColor(scr, DEF_FRAME_COLOR, &color);
294 gcv.function = GXxor;
295 /* this will raise the probability of the XORed color being different
296 * of the original color in PseudoColor when not all color cells are
297 * initialized */
298 if (DefaultVisual(dpy, scr->screen)->class == PseudoColor)
299 gcv.plane_mask = (1 << (scr->depth - 1)) | 1;
300 else
301 gcv.plane_mask = AllPlanes;
302 gcv.foreground = color.pixel;
303 if (gcv.foreground == 0)
304 gcv.foreground = 1;
305 gcv.line_width = DEF_FRAME_THICKNESS;
306 gcv.subwindow_mode = IncludeInferiors;
307 gcv.graphics_exposures = False;
308 scr->frame_gc = XCreateGC(dpy, scr->root_win, GCForeground | GCGraphicsExposures
309 | GCFunction | GCSubwindowMode | GCLineWidth | GCPlaneMask, &gcv);
311 /* line GC */
312 gcv.foreground = color.pixel;
314 if (gcv.foreground == 0)
315 /* XOR:ing with a zero is not going to be of much use, so
316 in that case, we somewhat arbitrarily xor with 17 instead. */
317 gcv.foreground = 17;
319 gcv.function = GXxor;
320 gcv.subwindow_mode = IncludeInferiors;
321 gcv.line_width = 1;
322 gcv.cap_style = CapRound;
323 gcv.graphics_exposures = False;
324 gcm = GCForeground | GCFunction | GCSubwindowMode | GCLineWidth | GCCapStyle | GCGraphicsExposures;
325 scr->line_gc = XCreateGC(dpy, scr->root_win, gcm, &gcv);
327 scr->line_pixel = gcv.foreground;
329 /* copy GC */
330 gcv.foreground = scr->white_pixel;
331 gcv.background = scr->black_pixel;
332 gcv.graphics_exposures = False;
333 scr->copy_gc = XCreateGC(dpy, scr->w_win, GCForeground | GCBackground | GCGraphicsExposures, &gcv);
335 /* misc drawing GC */
336 gcv.graphics_exposures = False;
337 gcm = GCGraphicsExposures;
338 scr->draw_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
340 assert(scr->stipple_bitmap != None);
342 /* mono GC */
343 scr->mono_gc = XCreateGC(dpy, scr->stipple_bitmap, gcm, &gcv);
346 static void createPixmaps(WScreen * scr)
348 WPixmap *pix;
350 /* load pixmaps */
351 pix = wPixmapCreateFromXBMData(scr, (char *)MENU_RADIO_INDICATOR_XBM_DATA,
352 (char *)MENU_RADIO_INDICATOR_XBM_DATA,
353 MENU_RADIO_INDICATOR_XBM_SIZE,
354 MENU_RADIO_INDICATOR_XBM_SIZE, scr->black_pixel, scr->white_pixel);
355 if (pix != NULL)
356 pix->shared = 1;
357 scr->menu_radio_indicator = pix;
359 pix = wPixmapCreateFromXBMData(scr, (char *)MENU_CHECK_INDICATOR_XBM_DATA,
360 (char *)MENU_CHECK_INDICATOR_XBM_DATA,
361 MENU_CHECK_INDICATOR_XBM_SIZE,
362 MENU_CHECK_INDICATOR_XBM_SIZE, scr->black_pixel, scr->white_pixel);
363 if (pix != NULL)
364 pix->shared = 1;
365 scr->menu_check_indicator = pix;
367 pix = wPixmapCreateFromXBMData(scr, (char *)MENU_MINI_INDICATOR_XBM_DATA,
368 (char *)MENU_MINI_INDICATOR_XBM_DATA,
369 MENU_MINI_INDICATOR_XBM_SIZE,
370 MENU_MINI_INDICATOR_XBM_SIZE, scr->black_pixel, scr->white_pixel);
371 if (pix != NULL)
372 pix->shared = 1;
373 scr->menu_mini_indicator = pix;
375 pix = wPixmapCreateFromXBMData(scr, (char *)MENU_HIDE_INDICATOR_XBM_DATA,
376 (char *)MENU_HIDE_INDICATOR_XBM_DATA,
377 MENU_HIDE_INDICATOR_XBM_SIZE,
378 MENU_HIDE_INDICATOR_XBM_SIZE, scr->black_pixel, scr->white_pixel);
379 if (pix != NULL)
380 pix->shared = 1;
381 scr->menu_hide_indicator = pix;
383 pix = wPixmapCreateFromXBMData(scr, (char *)MENU_SHADE_INDICATOR_XBM_DATA,
384 (char *)MENU_SHADE_INDICATOR_XBM_DATA,
385 MENU_SHADE_INDICATOR_XBM_SIZE,
386 MENU_SHADE_INDICATOR_XBM_SIZE, scr->black_pixel, scr->white_pixel);
387 if (pix != NULL)
388 pix->shared = 1;
389 scr->menu_shade_indicator = pix;
391 create_logo_image(scr);
393 scr->dock_dots = make3Dots(scr);
395 /* titlebar button pixmaps */
396 allocButtonPixmaps(scr);
399 void create_logo_image(WScreen *scr)
401 RImage *image = get_icon_image(scr, "Logo", "WMPanel", 128);
403 if (!image) {
404 wwarning(_("could not load logo image for panels: %s"), RMessageForError(RErrorCode));
405 } else {
406 WMSetApplicationIconImage(scr->wmscreen, image);
407 RReleaseImage(image);
412 *----------------------------------------------------------------------
413 * createInternalWindows--
414 * Creates some windows used internally by the program. One to
415 * receive input focus when no other window can get it and another
416 * to display window geometry information during window resize/move.
418 * Returns:
419 * Nothing
421 * Side effects:
422 * Windows are created and some colors are allocated for the
423 * window background.
424 *----------------------------------------------------------------------
426 static void createInternalWindows(WScreen * scr)
428 int vmask;
429 XSetWindowAttributes attribs;
431 /* InputOnly window to get the focus when no other window can get it */
432 vmask = CWEventMask | CWOverrideRedirect;
433 attribs.event_mask = KeyPressMask | FocusChangeMask;
434 attribs.override_redirect = True;
435 scr->no_focus_win = XCreateWindow(dpy, scr->root_win, -10, -10, 4, 4, 0, 0,
436 InputOnly, CopyFromParent, vmask, &attribs);
437 XSelectInput(dpy, scr->no_focus_win, KeyPressMask | KeyReleaseMask);
438 XMapWindow(dpy, scr->no_focus_win);
440 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
442 /* shadow window for dock buttons */
443 vmask = CWBorderPixel | CWBackPixmap | CWBackPixel | CWCursor | CWSaveUnder | CWOverrideRedirect;
444 attribs.border_pixel = scr->black_pixel;
445 attribs.save_under = True;
446 attribs.override_redirect = True;
447 attribs.background_pixmap = None;
448 attribs.background_pixel = scr->white_pixel;
449 attribs.cursor = wPreferences.cursor[WCUR_NORMAL];
450 vmask |= CWColormap;
451 attribs.colormap = scr->w_colormap;
452 scr->dock_shadow =
453 XCreateWindow(dpy, scr->root_win, 0, 0, wPreferences.icon_size,
454 wPreferences.icon_size, 0, scr->w_depth, CopyFromParent, scr->w_visual, vmask, &attribs);
456 /* workspace name */
457 vmask = CWBackPixel | CWSaveUnder | CWOverrideRedirect | CWColormap | CWBorderPixel;
458 attribs.save_under = True;
459 attribs.override_redirect = True;
460 attribs.colormap = scr->w_colormap;
461 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
462 attribs.border_pixel = 0; /* do not care */
463 scr->workspace_name =
464 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
465 CopyFromParent, scr->w_visual, vmask, &attribs);
469 *----------------------------------------------------------------------
470 * wScreenInit--
471 * Initializes the window manager for the given screen and
472 * allocates a WScreen descriptor for it. Many resources are allocated
473 * for the screen and the root window is setup appropriately.
475 * Returns:
476 * The WScreen descriptor for the screen.
478 * Side effects:
479 * Many resources are allocated and the IconSize property is
480 * set on the root window.
481 * The program can be aborted if some fatal error occurs.
483 * TODO: User specifiable visual.
484 *----------------------------------------------------------------------
486 WScreen *wScreenInit(int screen_number)
488 WScreen *scr;
489 XIconSize icon_size[1];
490 RContextAttributes rattr;
491 long event_mask;
492 XErrorHandler oldHandler;
493 int i;
495 scr = wmalloc(sizeof(WScreen));
497 scr->stacking_list = WMCreateTreeBag();
499 /* initialize globals */
500 scr->screen = screen_number;
501 scr->root_win = RootWindow(dpy, screen_number);
502 scr->depth = DefaultDepth(dpy, screen_number);
503 scr->colormap = DefaultColormap(dpy, screen_number);
505 scr->scr_width = WidthOfScreen(ScreenOfDisplay(dpy, screen_number));
506 scr->scr_height = HeightOfScreen(ScreenOfDisplay(dpy, screen_number));
508 wInitXinerama(scr);
510 scr->usableArea = (WArea *) wmalloc(sizeof(WArea) * wXineramaHeads(scr));
511 scr->totalUsableArea = (WArea *) wmalloc(sizeof(WArea) * wXineramaHeads(scr));
513 for (i = 0; i < wXineramaHeads(scr); ++i) {
514 WMRect rect = wGetRectForHead(scr, i);
515 scr->usableArea[i].x1 = scr->totalUsableArea[i].x1 = rect.pos.x;
516 scr->usableArea[i].y1 = scr->totalUsableArea[i].y1 = rect.pos.y;
517 scr->usableArea[i].x2 = scr->totalUsableArea[i].x2 = rect.pos.x + rect.size.width;
518 scr->usableArea[i].y2 = scr->totalUsableArea[i].y2 = rect.pos.y + rect.size.height;
521 scr->fakeGroupLeaders = WMCreateArray(16);
523 CantManageScreen = 0;
524 oldHandler = XSetErrorHandler(alreadyRunningError);
526 event_mask = EVENT_MASK;
527 XSelectInput(dpy, scr->root_win, event_mask);
529 #ifdef KEEP_XKB_LOCK_STATUS
530 /* Only GroupLock doesn't work correctly in my system since right-alt
531 * can change mode while holding it too - ]d
533 if (w_global.xext.xkb.supported) {
534 XkbSelectEvents(dpy, XkbUseCoreKbd, XkbStateNotifyMask, XkbStateNotifyMask);
536 #endif /* KEEP_XKB_LOCK_STATUS */
538 #ifdef USE_RANDR
539 if (w_global.xext.randr.supported)
540 XRRSelectInput(dpy, scr->root_win, RRScreenChangeNotifyMask);
541 #endif
543 XSync(dpy, False);
544 XSetErrorHandler(oldHandler);
546 if (CantManageScreen) {
547 wfree(scr);
548 return NULL;
551 XDefineCursor(dpy, scr->root_win, wPreferences.cursor[WCUR_ROOT]);
553 /* screen descriptor for raster graphic library */
554 rattr.flags = RC_RenderMode | RC_ColorsPerChannel | RC_StandardColormap;
555 rattr.render_mode = wPreferences.no_dithering ? RBestMatchRendering : RDitheredRendering;
557 /* if the std colormap stuff works ok, this will be ignored */
558 rattr.colors_per_channel = wPreferences.cmap_size;
559 if (rattr.colors_per_channel < 2)
560 rattr.colors_per_channel = 2;
562 /* Use standard colormap */
563 rattr.standard_colormap_mode = RUseStdColormap;
565 if (getWVisualID(screen_number) >= 0) {
566 rattr.flags |= RC_VisualID;
567 rattr.visualid = getWVisualID(screen_number);
570 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
572 if (!scr->rcontext && RErrorCode == RERR_STDCMAPFAIL) {
573 wwarning("%s", RMessageForError(RErrorCode));
575 rattr.flags &= ~RC_StandardColormap;
576 rattr.standard_colormap_mode = RUseStdColormap;
578 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
580 if (scr->rcontext == NULL) {
581 wfatal(_("can't create Context on screen %d, %s"),
582 screen_number, RMessageForError(RErrorCode));
583 goto abort_no_context;
586 scr->w_win = scr->rcontext->drawable;
587 scr->w_visual = scr->rcontext->visual;
588 scr->w_depth = scr->rcontext->depth;
589 scr->w_colormap = scr->rcontext->cmap;
591 /* create screen descriptor for WINGs */
592 scr->wmscreen = WMCreateScreenWithRContext(dpy, screen_number, scr->rcontext);
594 if (!scr->wmscreen) {
595 wfatal(_("could not initialize WINGs widget set"));
596 RDestroyContext(scr->rcontext);
597 abort_no_context:
598 WMFreeArray(scr->fakeGroupLeaders);
599 wfree(scr->totalUsableArea);
600 wfree(scr->usableArea);
601 WMFreeBag(scr->stacking_list);
602 wfree(scr);
603 return NULL;
606 scr->black = WMBlackColor(scr->wmscreen);
607 scr->white = WMWhiteColor(scr->wmscreen);
608 scr->gray = WMGrayColor(scr->wmscreen);
609 scr->darkGray = WMDarkGrayColor(scr->wmscreen);
611 scr->black_pixel = WMColorPixel(scr->black); /*scr->rcontext->black; */
612 scr->white_pixel = WMColorPixel(scr->white); /*scr->rcontext->white; */
613 scr->light_pixel = WMColorPixel(scr->gray);
614 scr->dark_pixel = WMColorPixel(scr->darkGray);
616 /* create GCs with default values */
617 allocGCs(scr);
619 /* for our window manager info notice board. Need to
620 * create before reading the defaults, because it will be used there.
622 scr->info_window = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, 0, 0);
624 /* read defaults for this screen */
625 wReadDefaults(scr, w_global.domain.wmaker->dictionary);
628 XColor xcol;
629 /* frame boder color */
630 wGetColor(scr, WMGetColorRGBDescription(scr->frame_border_color), &xcol);
631 scr->frame_border_pixel = xcol.pixel;
632 wGetColor(scr, WMGetColorRGBDescription(scr->frame_focused_border_color), &xcol);
633 scr->frame_focused_border_pixel = xcol.pixel;
634 wGetColor(scr, WMGetColorRGBDescription(scr->frame_selected_border_color), &xcol);
635 scr->frame_selected_border_pixel = xcol.pixel;
638 createInternalWindows(scr);
640 wNETWMInitStuff(scr);
642 /* create initial workspace */
643 wWorkspaceNew(scr);
645 /* create shared pixmaps */
646 createPixmaps(scr);
648 /* set icon sizes we can accept from clients */
649 icon_size[0].min_width = 8;
650 icon_size[0].min_height = 8;
651 icon_size[0].max_width = wPreferences.icon_size - 4;
652 icon_size[0].max_height = wPreferences.icon_size - 4;
653 icon_size[0].width_inc = 1;
654 icon_size[0].height_inc = 1;
655 XSetIconSizes(dpy, scr->root_win, icon_size, 1);
657 /* setup WindowMaker protocols property in the root window */
658 PropSetWMakerProtocols(scr->root_win);
660 /* setup our noticeboard */
661 XChangeProperty(dpy, scr->info_window, w_global.atom.wmaker.noticeboard,
662 XA_WINDOW, 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
663 XChangeProperty(dpy, scr->root_win, w_global.atom.wmaker.noticeboard,
664 XA_WINDOW, 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
666 #ifdef BALLOON_TEXT
667 /* initialize balloon text stuff */
668 wBalloonInitialize(scr);
669 #endif
671 scr->info_text_font = WMBoldSystemFontOfSize(scr->wmscreen, 12);
673 scr->tech_draw_font = XLoadQueryFont(dpy, "-adobe-helvetica-bold-r-*-*-12-*-*-*-*-*-*-*");
674 if (!scr->tech_draw_font)
675 scr->tech_draw_font = XLoadQueryFont(dpy, "fixed");
677 scr->gview = WCreateGeometryView(scr->wmscreen);
678 WMRealizeWidget(scr->gview);
680 wScreenUpdateUsableArea(scr);
682 return scr;
685 void wScreenUpdateUsableArea(WScreen *scr)
688 * scr->totalUsableArea[] will become the usableArea used for Windowplacement,
689 * scr->usableArea[] will be used for iconplacement, hence no iconyard nor
690 * border.
693 WArea area;
694 int i;
695 unsigned long tmp_area, best_area = 0;
696 unsigned int size = wPreferences.workspace_border_size;
697 unsigned int position = wPreferences.workspace_border_position;
699 for (i = 0; i < wXineramaHeads(scr); ++i) {
700 WMRect rect = wGetRectForHead(scr, i);
701 scr->totalUsableArea[i].x1 = rect.pos.x;
702 scr->totalUsableArea[i].y1 = rect.pos.y;
703 scr->totalUsableArea[i].x2 = rect.pos.x + rect.size.width;
704 scr->totalUsableArea[i].y2 = rect.pos.y + rect.size.height;
706 if (wNETWMGetUsableArea(scr, i, &area)) {
707 scr->totalUsableArea[i].x1 = WMAX(scr->totalUsableArea[i].x1, area.x1);
708 scr->totalUsableArea[i].y1 = WMAX(scr->totalUsableArea[i].y1, area.y1);
709 scr->totalUsableArea[i].x2 = WMIN(scr->totalUsableArea[i].x2, area.x2);
710 scr->totalUsableArea[i].y2 = WMIN(scr->totalUsableArea[i].y2, area.y2);
713 scr->usableArea[i] = scr->totalUsableArea[i];
715 if (wPreferences.no_window_over_icons) {
716 if (wPreferences.icon_yard & IY_VERT) {
717 if (wPreferences.icon_yard & IY_RIGHT)
718 scr->totalUsableArea[i].x2 -= wPreferences.icon_size;
719 else
720 scr->totalUsableArea[i].x1 += wPreferences.icon_size;
721 } else {
722 if (wPreferences.icon_yard & IY_TOP)
723 scr->totalUsableArea[i].y1 += wPreferences.icon_size;
724 else
725 scr->totalUsableArea[i].y2 -= wPreferences.icon_size;
729 if (scr->totalUsableArea[i].x2 - scr->totalUsableArea[i].x1 < rect.size.width / 2) {
730 scr->totalUsableArea[i].x1 = rect.pos.x;
731 scr->totalUsableArea[i].x2 = rect.pos.x + rect.size.width;
734 if (scr->totalUsableArea[i].y2 - scr->totalUsableArea[i].y1 < rect.size.height / 2) {
735 scr->totalUsableArea[i].y1 = rect.pos.y;
736 scr->totalUsableArea[i].y2 = rect.pos.y + rect.size.height;
739 tmp_area = (scr->totalUsableArea[i].x2 - scr->totalUsableArea[i].x1) *
740 (scr->totalUsableArea[i].y2 - scr->totalUsableArea[i].y1);
742 if (tmp_area > best_area) {
743 best_area = tmp_area;
744 area = scr->totalUsableArea[i];
747 if (size > 0 && position != WB_NONE) {
748 if (position & WB_LEFTRIGHT) {
749 scr->totalUsableArea[i].x1 += size;
750 scr->totalUsableArea[i].x2 -= size;
752 if (position & WB_TOPBOTTOM) {
753 scr->totalUsableArea[i].y1 += size;
754 scr->totalUsableArea[i].y2 -= size;
759 if (wPreferences.auto_arrange_icons)
760 wArrangeIcons(scr, True);
763 void wScreenRestoreState(WScreen * scr)
765 WMPropList *state;
766 char *path;
768 OpenRootMenu(scr, -10000, -10000, False);
769 wMenuUnmap(scr->root_menu);
771 make_keys();
773 if (w_global.screen_count == 1) {
774 path = wdefaultspathfordomain("WMState");
775 } else {
776 char buf[16];
777 snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
778 path = wdefaultspathfordomain(buf);
780 scr->session_state = WMReadPropListFromFile(path);
781 wfree(path);
782 if (!scr->session_state && w_global.screen_count > 1) {
783 path = wdefaultspathfordomain("WMState");
784 scr->session_state = WMReadPropListFromFile(path);
785 wfree(path);
788 if (!scr->session_state)
789 scr->session_state = WMCreatePLDictionary(NULL, NULL);
791 if (!wPreferences.flags.nodock) {
792 state = WMGetFromPLDictionary(scr->session_state, dDock);
793 scr->dock = wDockRestoreState(scr, state, WM_DOCK);
796 if (!wPreferences.flags.noclip) {
797 state = WMGetFromPLDictionary(scr->session_state, dClip);
798 scr->clip_icon = wClipRestoreState(scr, state);
801 if (!wPreferences.flags.nodrawer) {
802 if (!scr->dock->on_right_side) {
803 /* Drawer tile was created early in wScreenInit() -> wReadDefaults(). At
804 * that time, scr->dock was NULL and the tile was created as if we were on
805 * the right side. If we aren't, redo it now. */
806 assert(scr->drawer_tile);
807 RReleaseImage(scr->drawer_tile);
808 scr->drawer_tile = wDrawerMakeTile(scr, scr->icon_tile);
810 wDrawersRestoreState(scr);
813 wWorkspaceRestoreState(scr);
814 wScreenUpdateUsableArea(scr);
817 void wScreenSaveState(WScreen * scr)
819 WWindow *wwin;
820 char *str;
821 WMPropList *old_state, *foo;
823 make_keys();
825 /* save state of windows */
826 wwin = scr->focused_window;
827 while (wwin) {
828 wWindowSaveState(wwin);
829 wwin = wwin->prev;
832 if (wPreferences.flags.noupdates)
833 return;
835 old_state = scr->session_state;
836 scr->session_state = WMCreatePLDictionary(NULL, NULL);
838 WMPLSetCaseSensitive(True);
840 /* save dock state to file */
841 if (!wPreferences.flags.nodock) {
842 wDockSaveState(scr, old_state);
843 } else {
844 if ((foo = WMGetFromPLDictionary(old_state, dDock)) != NULL) {
845 WMPutInPLDictionary(scr->session_state, dDock, foo);
848 if (!wPreferences.flags.noclip) {
849 wClipSaveState(scr);
850 } else {
851 if ((foo = WMGetFromPLDictionary(old_state, dClip)) != NULL) {
852 WMPutInPLDictionary(scr->session_state, dClip, foo);
856 wWorkspaceSaveState(scr, old_state);
858 if (!wPreferences.flags.nodrawer) {
859 wDrawersSaveState(scr);
860 } else {
861 if ((foo = WMGetFromPLDictionary(old_state, dDrawers)) != NULL) {
862 WMPutInPLDictionary(scr->session_state, dDrawers, foo);
867 if (wPreferences.save_session_on_exit) {
868 wSessionSaveState(scr);
869 } else {
870 if ((foo = WMGetFromPLDictionary(old_state, dApplications)) != NULL) {
871 WMPutInPLDictionary(scr->session_state, dApplications, foo);
873 if ((foo = WMGetFromPLDictionary(old_state, dWorkspace)) != NULL) {
874 WMPutInPLDictionary(scr->session_state, dWorkspace, foo);
878 /* clean up */
879 WMPLSetCaseSensitive(False);
881 wMenuSaveState(scr);
883 if (w_global.screen_count == 1) {
884 str = wdefaultspathfordomain("WMState");
885 } else {
886 char buf[16];
887 snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
888 str = wdefaultspathfordomain(buf);
890 if (!WMWritePropListToFile(scr->session_state, str)) {
891 werror(_("could not save session state in %s"), str);
893 wfree(str);
894 WMReleasePropList(old_state);
897 int wScreenBringInside(WScreen * scr, int *x, int *y, int width, int height)
899 int moved = 0;
900 int tol_w, tol_h;
902 * With respect to the head that contains most of the window.
904 int sx1, sy1, sx2, sy2;
906 WMRect rect;
907 int head, flags;
909 rect.pos.x = *x;
910 rect.pos.y = *y;
911 rect.size.width = width;
912 rect.size.height = height;
914 head = wGetRectPlacementInfo(scr, rect, &flags);
915 rect = wGetRectForHead(scr, head);
917 sx1 = rect.pos.x;
918 sy1 = rect.pos.y;
919 sx2 = sx1 + rect.size.width;
920 sy2 = sy1 + rect.size.height;
922 #if 0 /* NOTE: gives funky group movement */
923 if (flags & XFLAG_MULTIPLE) {
925 * since we span multiple heads, pull window totaly inside
927 if (*x < sx1)
928 *x = sx1, moved = 1;
929 else if (*x + width > sx2)
930 *x = sx2 - width, moved = 1;
932 if (*y < sy1)
933 *y = sy1, moved = 1;
934 else if (*y + height > sy2)
935 *y = sy2 - height, moved = 1;
937 return moved;
939 #endif
941 if (width > 20)
942 tol_w = width / 2;
943 else
944 tol_w = 20;
946 if (height > 20)
947 tol_h = height / 2;
948 else
949 tol_h = 20;
951 if (*x + width < sx1 + 10)
952 *x = sx1 - tol_w, moved = 1;
953 else if (*x >= sx2 - 10)
954 *x = sx2 - tol_w - 1, moved = 1;
956 if (*y < sy1 - height + 10)
957 *y = sy1 - tol_h, moved = 1;
958 else if (*y >= sy2 - 10)
959 *y = sy2 - tol_h - 1, moved = 1;
961 return moved;
964 int wScreenKeepInside(WScreen * scr, int *x, int *y, int width, int height)
966 int moved = 0;
967 int sx1, sy1, sx2, sy2;
968 WMRect rect;
969 int head;
971 rect.pos.x = *x;
972 rect.pos.y = *y;
973 rect.size.width = width;
974 rect.size.height = height;
976 head = wGetHeadForRect(scr, rect);
977 rect = wGetRectForHead(scr, head);
979 sx1 = rect.pos.x;
980 sy1 = rect.pos.y;
981 sx2 = sx1 + rect.size.width;
982 sy2 = sy1 + rect.size.height;
984 if (*x < sx1)
985 *x = sx1, moved = 1;
986 else if (*x + width > sx2)
987 *x = sx2 - width, moved = 1;
989 if (*y < sy1)
990 *y = sy1, moved = 1;
991 else if (*y + height > sy2)
992 *y = sy2 - height, moved = 1;
994 return moved;