RandR misc.
[wmaker-crm.git] / src / screen.c
blob91ef63bc33836f46b222179b696319cdb046bdf3
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", wPreferences.icon_size);
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;
528 if (wPreferences.disable_root_mouse) {
529 event_mask &= ~(ButtonPressMask | ButtonReleaseMask);
532 XSelectInput(dpy, scr->root_win, event_mask);
534 #ifdef KEEP_XKB_LOCK_STATUS
535 /* Only GroupLock doesn't work correctly in my system since right-alt
536 * can change mode while holding it too - ]d
538 if (w_global.xext.xkb.supported) {
539 XkbSelectEvents(dpy, XkbUseCoreKbd, XkbStateNotifyMask, XkbStateNotifyMask);
541 #endif /* KEEP_XKB_LOCK_STATUS */
543 #ifdef USE_RANDR
544 if (w_global.xext.randr.supported)
545 XRRSelectInput(dpy, scr->root_win, RRScreenChangeNotifyMask);
546 #endif
548 XSync(dpy, False);
549 XSetErrorHandler(oldHandler);
551 if (CantManageScreen) {
552 wfree(scr);
553 return NULL;
556 XDefineCursor(dpy, scr->root_win, wPreferences.cursor[WCUR_ROOT]);
558 /* screen descriptor for raster graphic library */
559 rattr.flags = RC_RenderMode | RC_ColorsPerChannel | RC_StandardColormap;
560 rattr.render_mode = wPreferences.no_dithering ? RBestMatchRendering : RDitheredRendering;
562 /* if the std colormap stuff works ok, this will be ignored */
563 rattr.colors_per_channel = wPreferences.cmap_size;
564 if (rattr.colors_per_channel < 2)
565 rattr.colors_per_channel = 2;
567 /* Use standard colormap */
568 rattr.standard_colormap_mode = RUseStdColormap;
570 if (getWVisualID(screen_number) >= 0) {
571 rattr.flags |= RC_VisualID;
572 rattr.visualid = getWVisualID(screen_number);
575 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
577 if (!scr->rcontext && RErrorCode == RERR_STDCMAPFAIL) {
578 wwarning("%s", RMessageForError(RErrorCode));
580 rattr.flags &= ~RC_StandardColormap;
581 rattr.standard_colormap_mode = RUseStdColormap;
583 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
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 return NULL;
599 scr->black = WMBlackColor(scr->wmscreen);
600 scr->white = WMWhiteColor(scr->wmscreen);
601 scr->gray = WMGrayColor(scr->wmscreen);
602 scr->darkGray = WMDarkGrayColor(scr->wmscreen);
604 scr->black_pixel = WMColorPixel(scr->black); /*scr->rcontext->black; */
605 scr->white_pixel = WMColorPixel(scr->white); /*scr->rcontext->white; */
606 scr->light_pixel = WMColorPixel(scr->gray);
607 scr->dark_pixel = WMColorPixel(scr->darkGray);
609 /* create GCs with default values */
610 allocGCs(scr);
612 /* for our window manager info notice board. Need to
613 * create before reading the defaults, because it will be used there.
615 scr->info_window = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, 0, 0);
617 /* read defaults for this screen */
618 wReadDefaults(scr, w_global.domain.wmaker->dictionary);
621 XColor xcol;
622 /* frame boder color */
623 wGetColor(scr, WMGetColorRGBDescription(scr->frame_border_color), &xcol);
624 scr->frame_border_pixel = xcol.pixel;
625 wGetColor(scr, WMGetColorRGBDescription(scr->frame_selected_border_color), &xcol);
626 scr->frame_selected_border_pixel = xcol.pixel;
629 createInternalWindows(scr);
631 wNETWMInitStuff(scr);
633 /* create initial workspace */
634 wWorkspaceNew(scr);
636 /* create shared pixmaps */
637 createPixmaps(scr);
639 /* set icon sizes we can accept from clients */
640 icon_size[0].min_width = 8;
641 icon_size[0].min_height = 8;
642 icon_size[0].max_width = wPreferences.icon_size - 4;
643 icon_size[0].max_height = wPreferences.icon_size - 4;
644 icon_size[0].width_inc = 1;
645 icon_size[0].height_inc = 1;
646 XSetIconSizes(dpy, scr->root_win, icon_size, 1);
648 /* setup WindowMaker protocols property in the root window */
649 PropSetWMakerProtocols(scr->root_win);
651 /* setup our noticeboard */
652 XChangeProperty(dpy, scr->info_window, w_global.atom.wmaker.noticeboard,
653 XA_WINDOW, 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
654 XChangeProperty(dpy, scr->root_win, w_global.atom.wmaker.noticeboard,
655 XA_WINDOW, 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
657 #ifdef BALLOON_TEXT
658 /* initialize balloon text stuff */
659 wBalloonInitialize(scr);
660 #endif
662 scr->info_text_font = WMBoldSystemFontOfSize(scr->wmscreen, 12);
664 scr->tech_draw_font = XLoadQueryFont(dpy, "-adobe-helvetica-bold-r-*-*-12-*-*-*-*-*-*-*");
665 if (!scr->tech_draw_font)
666 scr->tech_draw_font = XLoadQueryFont(dpy, "fixed");
668 scr->gview = WCreateGeometryView(scr->wmscreen);
669 WMRealizeWidget(scr->gview);
671 wScreenUpdateUsableArea(scr);
673 return scr;
676 void wScreenUpdateUsableArea(WScreen *scr)
679 * scr->totalUsableArea[] will become the usableArea used for Windowplacement,
680 * scr->usableArea[] will be used for iconplacement, hence no iconyard nor
681 * border.
684 WArea area;
685 int i;
686 unsigned long tmp_area, best_area = 0;
687 unsigned int size = wPreferences.workspace_border_size;
688 unsigned int position = wPreferences.workspace_border_position;
690 for (i = 0; i < wXineramaHeads(scr); ++i) {
691 WMRect rect = wGetRectForHead(scr, i);
692 scr->totalUsableArea[i].x1 = rect.pos.x;
693 scr->totalUsableArea[i].y1 = rect.pos.y;
694 scr->totalUsableArea[i].x2 = rect.pos.x + rect.size.width;
695 scr->totalUsableArea[i].y2 = rect.pos.y + rect.size.height;
697 if (wNETWMGetUsableArea(scr, i, &area)) {
698 scr->totalUsableArea[i].x1 = WMAX(scr->totalUsableArea[i].x1, area.x1);
699 scr->totalUsableArea[i].y1 = WMAX(scr->totalUsableArea[i].y1, area.y1);
700 scr->totalUsableArea[i].x2 = WMIN(scr->totalUsableArea[i].x2, area.x2);
701 scr->totalUsableArea[i].y2 = WMIN(scr->totalUsableArea[i].y2, area.y2);
704 scr->usableArea[i] = scr->totalUsableArea[i];
706 if (wPreferences.no_window_over_icons) {
707 if (wPreferences.icon_yard & IY_VERT) {
708 if (wPreferences.icon_yard & IY_RIGHT)
709 scr->totalUsableArea[i].x2 -= wPreferences.icon_size;
710 else
711 scr->totalUsableArea[i].x1 += wPreferences.icon_size;
712 } else {
713 if (wPreferences.icon_yard & IY_TOP)
714 scr->totalUsableArea[i].y1 += wPreferences.icon_size;
715 else
716 scr->totalUsableArea[i].y2 -= wPreferences.icon_size;
720 if (scr->totalUsableArea[i].x2 - scr->totalUsableArea[i].x1 < rect.size.width / 2) {
721 scr->totalUsableArea[i].x1 = rect.pos.x;
722 scr->totalUsableArea[i].x2 = rect.pos.x + rect.size.width;
725 if (scr->totalUsableArea[i].y2 - scr->totalUsableArea[i].y1 < rect.size.height / 2) {
726 scr->totalUsableArea[i].y1 = rect.pos.y;
727 scr->totalUsableArea[i].y2 = rect.pos.y + rect.size.height;
730 tmp_area = (scr->totalUsableArea[i].x2 - scr->totalUsableArea[i].x1) *
731 (scr->totalUsableArea[i].y2 - scr->totalUsableArea[i].y1);
733 if (tmp_area > best_area) {
734 best_area = tmp_area;
735 area = scr->totalUsableArea[i];
738 if (size > 0 && position != WB_NONE) {
739 if (position & WB_LEFTRIGHT) {
740 scr->totalUsableArea[i].x1 += size;
741 scr->totalUsableArea[i].x2 -= size;
743 if (position & WB_TOPBOTTOM) {
744 scr->totalUsableArea[i].y1 += size;
745 scr->totalUsableArea[i].y2 -= size;
750 if (wPreferences.auto_arrange_icons)
751 wArrangeIcons(scr, True);
754 void wScreenRestoreState(WScreen * scr)
756 WMPropList *state;
757 char *path;
759 OpenRootMenu(scr, -10000, -10000, False);
760 wMenuUnmap(scr->root_menu);
762 make_keys();
764 if (w_global.screen_count == 1) {
765 path = wdefaultspathfordomain("WMState");
766 } else {
767 char buf[16];
768 snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
769 path = wdefaultspathfordomain(buf);
772 w_global.session_state = WMReadPropListFromFile(path);
773 wfree(path);
774 if (!w_global.session_state && w_global.screen_count > 1) {
775 path = wdefaultspathfordomain("WMState");
776 w_global.session_state = WMReadPropListFromFile(path);
777 wfree(path);
780 if (!w_global.session_state)
781 w_global.session_state = WMCreatePLDictionary(NULL, NULL);
783 if (!wPreferences.flags.nodock) {
784 state = WMGetFromPLDictionary(w_global.session_state, dDock);
785 scr->dock = wDockRestoreState(scr, state, WM_DOCK);
788 if (!wPreferences.flags.noclip) {
789 state = WMGetFromPLDictionary(w_global.session_state, dClip);
790 w_global.clip.icon = wClipRestoreState(scr, state);
793 if (!wPreferences.flags.nodrawer) {
794 if (!scr->dock->on_right_side) {
795 /* Drawer tile was created early in wScreenInit() -> wReadDefaults(). At
796 * that time, scr->dock was NULL and the tile was created as if we were on
797 * the right side. If we aren't, redo it now. */
798 assert(scr->drawer_tile);
799 RReleaseImage(scr->drawer_tile);
800 scr->drawer_tile = wDrawerMakeTile(scr, scr->icon_tile);
802 wDrawersRestoreState(scr);
805 wWorkspaceRestoreState(scr);
806 wScreenUpdateUsableArea(scr);
809 void wScreenSaveState(WScreen * scr)
811 WWindow *wwin;
812 char *str;
813 WMPropList *old_state, *foo;
815 make_keys();
817 /* save state of windows */
818 wwin = scr->focused_window;
819 while (wwin) {
820 wWindowSaveState(wwin);
821 wwin = wwin->prev;
824 if (wPreferences.flags.noupdates)
825 return;
827 old_state = w_global.session_state;
828 w_global.session_state = WMCreatePLDictionary(NULL, NULL);
830 WMPLSetCaseSensitive(True);
832 /* save dock state to file */
833 if (!wPreferences.flags.nodock) {
834 wDockSaveState(scr, old_state);
835 } else {
836 if ((foo = WMGetFromPLDictionary(old_state, dDock)) != NULL) {
837 WMPutInPLDictionary(w_global.session_state, dDock, foo);
840 if (!wPreferences.flags.noclip) {
841 wClipSaveState();
842 } else {
843 if ((foo = WMGetFromPLDictionary(old_state, dClip)) != NULL) {
844 WMPutInPLDictionary(w_global.session_state, dClip, foo);
848 wWorkspaceSaveState(old_state);
850 if (!wPreferences.flags.nodrawer) {
851 wDrawersSaveState(scr);
852 } else {
853 if ((foo = WMGetFromPLDictionary(old_state, dDrawers)) != NULL) {
854 WMPutInPLDictionary(w_global.session_state, dDrawers, foo);
859 if (wPreferences.save_session_on_exit) {
860 wSessionSaveState(scr);
861 } else {
862 if ((foo = WMGetFromPLDictionary(old_state, dApplications)) != NULL) {
863 WMPutInPLDictionary(w_global.session_state, dApplications, foo);
865 if ((foo = WMGetFromPLDictionary(old_state, dWorkspace)) != NULL) {
866 WMPutInPLDictionary(w_global.session_state, dWorkspace, foo);
870 /* clean up */
871 WMPLSetCaseSensitive(False);
873 wMenuSaveState(scr);
875 if (w_global.screen_count == 1) {
876 str = wdefaultspathfordomain("WMState");
877 } else {
878 char buf[16];
879 snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
880 str = wdefaultspathfordomain(buf);
882 if (!WMWritePropListToFile(w_global.session_state, str)) {
883 werror(_("could not save session state in %s"), str);
885 wfree(str);
886 WMReleasePropList(old_state);
889 int wScreenBringInside(WScreen * scr, int *x, int *y, int width, int height)
891 int moved = 0;
892 int tol_w, tol_h;
894 * With respect to the head that contains most of the window.
896 int sx1, sy1, sx2, sy2;
898 WMRect rect;
899 int head, flags;
901 rect.pos.x = *x;
902 rect.pos.y = *y;
903 rect.size.width = width;
904 rect.size.height = height;
906 head = wGetRectPlacementInfo(scr, rect, &flags);
907 rect = wGetRectForHead(scr, head);
909 sx1 = rect.pos.x;
910 sy1 = rect.pos.y;
911 sx2 = sx1 + rect.size.width;
912 sy2 = sy1 + rect.size.height;
914 #if 0 /* NOTE: gives funky group movement */
915 if (flags & XFLAG_MULTIPLE) {
917 * since we span multiple heads, pull window totaly inside
919 if (*x < sx1)
920 *x = sx1, moved = 1;
921 else if (*x + width > sx2)
922 *x = sx2 - width, moved = 1;
924 if (*y < sy1)
925 *y = sy1, moved = 1;
926 else if (*y + height > sy2)
927 *y = sy2 - height, moved = 1;
929 return moved;
931 #endif
933 if (width > 20)
934 tol_w = width / 2;
935 else
936 tol_w = 20;
938 if (height > 20)
939 tol_h = height / 2;
940 else
941 tol_h = 20;
943 if (*x + width < sx1 + 10)
944 *x = sx1 - tol_w, moved = 1;
945 else if (*x >= sx2 - 10)
946 *x = sx2 - tol_w - 1, moved = 1;
948 if (*y < sy1 - height + 10)
949 *y = sy1 - tol_h, moved = 1;
950 else if (*y >= sy2 - 10)
951 *y = sy2 - tol_h - 1, moved = 1;
953 return moved;
956 int wScreenKeepInside(WScreen * scr, int *x, int *y, int width, int height)
958 int moved = 0;
959 int sx1, sy1, sx2, sy2;
960 WMRect rect;
961 int head;
963 rect.pos.x = *x;
964 rect.pos.y = *y;
965 rect.size.width = width;
966 rect.size.height = height;
968 head = wGetHeadForRect(scr, rect);
969 rect = wGetRectForHead(scr, head);
971 sx1 = rect.pos.x;
972 sy1 = rect.pos.y;
973 sx2 = sx1 + rect.size.width;
974 sy2 = sy1 + rect.size.height;
976 if (*x < sx1)
977 *x = sx1, moved = 1;
978 else if (*x + width > sx2)
979 *x = sx2 - width, moved = 1;
981 if (*y < sy1)
982 *y = sy1, moved = 1;
983 else if (*y + height > sy2)
984 *y = sy2 - height, moved = 1;
986 return moved;