Add OPEN_PLMENU option to parse command generated proplist style menus
[wmaker-crm.git] / src / screen.c
blobbc8f3faa7d9b791393da0a9c796dda4d4ca17cd0
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 SHAPE
32 #include <X11/extensions/shape.h>
33 #endif
34 #ifdef KEEP_XKB_LOCK_STATUS
35 #include <X11/XKBlib.h>
36 #endif /* KEEP_XKB_LOCK_STATUS */
37 #ifdef HAVE_XRANDR
38 #include <X11/extensions/Xrandr.h>
39 #endif
41 #include <wraster.h>
42 #include "WindowMaker.h"
43 #include "def_pixmaps.h"
44 #include "screen.h"
45 #include "texture.h"
46 #include "pixmap.h"
47 #include "menu.h"
48 #include "funcs.h"
49 #include "main.h"
50 #include "actions.h"
51 #include "properties.h"
52 #include "dock.h"
53 #include "resources.h"
54 #include "workspace.h"
55 #include "session.h"
56 #include "balloon.h"
57 #include "geomview.h"
58 #include "wmspec.h"
59 #include "rootmenu.h"
61 #include "xinerama.h"
63 #include <WINGs/WUtil.h>
65 #include "defaults.h"
67 #define EVENT_MASK (LeaveWindowMask|EnterWindowMask|PropertyChangeMask\
68 |SubstructureNotifyMask|PointerMotionMask \
69 |SubstructureRedirectMask|ButtonPressMask|ButtonReleaseMask\
70 |KeyPressMask|KeyReleaseMask)
72 /**** Global variables ****/
73 extern Cursor wCursor[WCUR_LAST];
74 extern WPreferences wPreferences;
75 extern Atom _XA_WINDOWMAKER_STATE;
76 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
78 extern int wScreenCount;
80 #ifdef KEEP_XKB_LOCK_STATUS
81 extern int wXkbSupported;
82 #endif
84 extern WDDomain *WDWindowMaker;
86 /**** Local ****/
87 #define STIPPLE_WIDTH 2
88 #define STIPPLE_HEIGHT 2
89 static char STIPPLE_DATA[] = { 0x02, 0x01 };
91 static int CantManageScreen = 0;
93 static WMPropList *dApplications = NULL;
94 static WMPropList *dWorkspace;
95 static WMPropList *dDock;
96 static WMPropList *dClip;
98 static void make_keys(void)
100 if (dApplications != NULL)
101 return;
103 dApplications = WMCreatePLString("Applications");
104 dWorkspace = WMCreatePLString("Workspace");
105 dDock = WMCreatePLString("Dock");
106 dClip = WMCreatePLString("Clip");
110 *----------------------------------------------------------------------
111 * alreadyRunningError--
112 * X error handler used to catch errors when trying to do
113 * XSelectInput() on the root window. These errors probably mean that
114 * there already is some other window manager running.
116 * Returns:
117 * Nothing, unless something really evil happens...
119 * Side effects:
120 * CantManageScreen is set to 1;
121 *----------------------------------------------------------------------
123 static int alreadyRunningError(Display * dpy, XErrorEvent * error)
125 CantManageScreen = 1;
126 return -1;
130 *----------------------------------------------------------------------
131 * allocButtonPixmaps--
132 * Allocate pixmaps used on window operation buttons (those in the
133 * titlebar). The pixmaps are linked to the program. If XPM is supported
134 * XPM pixmaps are used otherwise, equivalent bitmaps are used.
136 * Returns:
137 * Nothing
139 * Side effects:
140 * Allocates shared pixmaps for the screen. These pixmaps should
141 * not be freed by anybody.
142 *----------------------------------------------------------------------
144 static void allocButtonPixmaps(WScreen * scr)
146 WPixmap *pix;
148 /* create predefined pixmaps */
149 if (wPreferences.new_style == TS_NEXT) {
150 pix = wPixmapCreateFromXPMData(scr, NEXT_CLOSE_XPM);
151 } else {
152 pix = wPixmapCreateFromXPMData(scr, PRED_CLOSE_XPM);
154 if (pix)
155 pix->shared = 1;
156 scr->b_pixmaps[WBUT_CLOSE] = pix;
158 if (wPreferences.new_style == TS_NEXT) {
159 pix = wPixmapCreateFromXPMData(scr, NEXT_BROKEN_CLOSE_XPM);
160 } else {
161 pix = wPixmapCreateFromXPMData(scr, PRED_BROKEN_CLOSE_XPM);
163 if (pix)
164 pix->shared = 1;
165 scr->b_pixmaps[WBUT_BROKENCLOSE] = pix;
167 if (wPreferences.new_style == TS_NEXT) {
168 pix = wPixmapCreateFromXPMData(scr, NEXT_ICONIFY_XPM);
169 } else {
170 pix = wPixmapCreateFromXPMData(scr, PRED_ICONIFY_XPM);
172 if (pix)
173 pix->shared = 1;
174 scr->b_pixmaps[WBUT_ICONIFY] = pix;
175 #ifdef XKB_BUTTON_HINT
176 if (wPreferences.new_style == TS_NEXT) {
177 pix = wPixmapCreateFromXPMData(scr, NEXT_XKBGROUP1_XPM);
178 } else {
179 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP1_XPM);
181 if (pix)
182 pix->shared = 1;
183 scr->b_pixmaps[WBUT_XKBGROUP1] = pix;
184 if (wPreferences.new_style == TS_NEXT) {
185 pix = wPixmapCreateFromXPMData(scr, NEXT_XKBGROUP2_XPM);
186 } else {
187 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP2_XPM);
189 if (pix)
190 pix->shared = 1;
191 scr->b_pixmaps[WBUT_XKBGROUP2] = pix;
192 if (wPreferences.new_style == TS_NEXT) {
193 pix = wPixmapCreateFromXPMData(scr, NEXT_XKBGROUP3_XPM);
194 } else {
195 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP3_XPM);
197 if (pix)
198 pix->shared = 1;
199 scr->b_pixmaps[WBUT_XKBGROUP3] = pix;
200 if (wPreferences.new_style == TS_NEXT) {
201 pix = wPixmapCreateFromXPMData(scr, NEXT_XKBGROUP4_XPM);
202 } else {
203 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP4_XPM);
205 if (pix)
206 pix->shared = 1;
207 scr->b_pixmaps[WBUT_XKBGROUP4] = pix;
208 #endif
210 if (wPreferences.new_style == TS_NEXT) {
211 pix = wPixmapCreateFromXPMData(scr, NEXT_KILL_XPM);
212 } else {
213 pix = wPixmapCreateFromXPMData(scr, PRED_KILL_XPM);
215 if (pix)
216 pix->shared = 1;
217 scr->b_pixmaps[WBUT_KILL] = pix;
220 static void draw_dot(WScreen * scr, Drawable d, int x, int y, GC gc)
222 XSetForeground(dpy, gc, scr->black_pixel);
223 XDrawLine(dpy, d, gc, x, y, x + 1, y);
224 XDrawPoint(dpy, d, gc, x, y + 1);
225 XSetForeground(dpy, gc, scr->white_pixel);
226 XDrawLine(dpy, d, gc, x + 2, y, x + 2, y + 1);
227 XDrawPoint(dpy, d, gc, x + 1, y + 1);
230 static WPixmap *make3Dots(WScreen * scr)
232 WPixmap *wpix;
233 GC gc2, gc;
234 XGCValues gcv;
235 Pixmap pix, mask;
237 gc = scr->copy_gc;
238 pix = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size, wPreferences.icon_size, scr->w_depth);
239 XSetForeground(dpy, gc, scr->black_pixel);
240 XFillRectangle(dpy, pix, gc, 0, 0, wPreferences.icon_size, wPreferences.icon_size);
241 XSetForeground(dpy, gc, scr->white_pixel);
242 draw_dot(scr, pix, 4, wPreferences.icon_size - 6, gc);
243 draw_dot(scr, pix, 9, wPreferences.icon_size - 6, gc);
244 draw_dot(scr, pix, 14, wPreferences.icon_size - 6, gc);
246 mask = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size, wPreferences.icon_size, 1);
247 gcv.foreground = 0;
248 gcv.graphics_exposures = False;
249 gc2 = XCreateGC(dpy, mask, GCForeground | GCGraphicsExposures, &gcv);
250 XFillRectangle(dpy, mask, gc2, 0, 0, wPreferences.icon_size, wPreferences.icon_size);
251 XSetForeground(dpy, gc2, 1);
252 XFillRectangle(dpy, mask, gc2, 4, wPreferences.icon_size - 6, 3, 2);
253 XFillRectangle(dpy, mask, gc2, 9, wPreferences.icon_size - 6, 3, 2);
254 XFillRectangle(dpy, mask, gc2, 14, wPreferences.icon_size - 6, 3, 2);
256 XFreeGC(dpy, gc2);
258 wpix = wPixmapCreate(scr, pix, mask);
259 wpix->shared = 1;
261 return wpix;
264 static void allocGCs(WScreen * scr)
266 XGCValues gcv;
267 XColor color;
268 int gcm;
270 scr->stipple_bitmap = XCreateBitmapFromData(dpy, scr->w_win, STIPPLE_DATA, STIPPLE_WIDTH, STIPPLE_HEIGHT);
272 gcv.stipple = scr->stipple_bitmap;
273 gcv.foreground = scr->white_pixel;
274 gcv.fill_style = FillStippled;
275 gcv.graphics_exposures = False;
276 gcm = GCForeground | GCStipple | GCFillStyle | GCGraphicsExposures;
277 scr->stipple_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
279 /* selected icon border GCs */
280 gcv.function = GXcopy;
281 gcv.foreground = scr->white_pixel;
282 gcv.background = scr->black_pixel;
283 gcv.line_width = 1;
284 gcv.line_style = LineDoubleDash;
285 gcv.fill_style = FillSolid;
286 gcv.dash_offset = 0;
287 gcv.dashes = 4;
288 gcv.graphics_exposures = False;
290 gcm = GCFunction | GCGraphicsExposures;
291 gcm |= GCForeground | GCBackground;
292 gcm |= GCLineWidth | GCLineStyle;
293 gcm |= GCFillStyle;
294 gcm |= GCDashOffset | GCDashList;
296 scr->icon_select_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
298 scr->menu_title_color[0] = WMRetainColor(scr->white);
300 /* don't retain scr->black here because we may alter its alpha */
301 scr->mtext_color = WMCreateRGBColor(scr->wmscreen, 0, 0, 0, True);
302 scr->dtext_color = WMCreateRGBColor(scr->wmscreen, 0, 0, 0, True);
304 /* frame GC */
305 wGetColor(scr, DEF_FRAME_COLOR, &color);
306 gcv.function = GXxor;
307 /* this will raise the probability of the XORed color being different
308 * of the original color in PseudoColor when not all color cells are
309 * initialized */
310 if (DefaultVisual(dpy, scr->screen)->class == PseudoColor)
311 gcv.plane_mask = (1 << (scr->depth - 1)) | 1;
312 else
313 gcv.plane_mask = AllPlanes;
314 gcv.foreground = color.pixel;
315 if (gcv.foreground == 0)
316 gcv.foreground = 1;
317 gcv.line_width = DEF_FRAME_THICKNESS;
318 gcv.subwindow_mode = IncludeInferiors;
319 gcv.graphics_exposures = False;
320 scr->frame_gc = XCreateGC(dpy, scr->root_win, GCForeground | GCGraphicsExposures
321 | GCFunction | GCSubwindowMode | GCLineWidth | GCPlaneMask, &gcv);
323 /* line GC */
324 gcv.foreground = color.pixel;
326 if (gcv.foreground == 0)
327 /* XOR:ing with a zero is not going to be of much use, so
328 in that case, we somewhat arbitrarily xor with 17 instead. */
329 gcv.foreground = 17;
331 gcv.function = GXxor;
332 gcv.subwindow_mode = IncludeInferiors;
333 gcv.line_width = 1;
334 gcv.cap_style = CapRound;
335 gcv.graphics_exposures = False;
336 gcm = GCForeground | GCFunction | GCSubwindowMode | GCLineWidth | GCCapStyle | GCGraphicsExposures;
337 scr->line_gc = XCreateGC(dpy, scr->root_win, gcm, &gcv);
339 scr->line_pixel = gcv.foreground;
341 /* copy GC */
342 gcv.foreground = scr->white_pixel;
343 gcv.background = scr->black_pixel;
344 gcv.graphics_exposures = False;
345 scr->copy_gc = XCreateGC(dpy, scr->w_win, GCForeground | GCBackground | GCGraphicsExposures, &gcv);
347 /* misc drawing GC */
348 gcv.graphics_exposures = False;
349 gcm = GCGraphicsExposures;
350 scr->draw_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
352 assert(scr->stipple_bitmap != None);
354 /* mono GC */
355 scr->mono_gc = XCreateGC(dpy, scr->stipple_bitmap, gcm, &gcv);
358 static void createPixmaps(WScreen * scr)
360 WPixmap *pix;
362 /* load pixmaps */
363 pix = wPixmapCreateFromXBMData(scr, (char *)MENU_RADIO_INDICATOR_XBM_DATA,
364 (char *)MENU_RADIO_INDICATOR_XBM_DATA,
365 MENU_RADIO_INDICATOR_XBM_SIZE,
366 MENU_RADIO_INDICATOR_XBM_SIZE, scr->black_pixel, scr->white_pixel);
367 if (pix != NULL)
368 pix->shared = 1;
369 scr->menu_radio_indicator = pix;
371 pix = wPixmapCreateFromXBMData(scr, (char *)MENU_CHECK_INDICATOR_XBM_DATA,
372 (char *)MENU_CHECK_INDICATOR_XBM_DATA,
373 MENU_CHECK_INDICATOR_XBM_SIZE,
374 MENU_CHECK_INDICATOR_XBM_SIZE, scr->black_pixel, scr->white_pixel);
375 if (pix != NULL)
376 pix->shared = 1;
377 scr->menu_check_indicator = pix;
379 pix = wPixmapCreateFromXBMData(scr, (char *)MENU_MINI_INDICATOR_XBM_DATA,
380 (char *)MENU_MINI_INDICATOR_XBM_DATA,
381 MENU_MINI_INDICATOR_XBM_SIZE,
382 MENU_MINI_INDICATOR_XBM_SIZE, scr->black_pixel, scr->white_pixel);
383 if (pix != NULL)
384 pix->shared = 1;
385 scr->menu_mini_indicator = pix;
387 pix = wPixmapCreateFromXBMData(scr, (char *)MENU_HIDE_INDICATOR_XBM_DATA,
388 (char *)MENU_HIDE_INDICATOR_XBM_DATA,
389 MENU_HIDE_INDICATOR_XBM_SIZE,
390 MENU_HIDE_INDICATOR_XBM_SIZE, scr->black_pixel, scr->white_pixel);
391 if (pix != NULL)
392 pix->shared = 1;
393 scr->menu_hide_indicator = pix;
395 pix = wPixmapCreateFromXBMData(scr, (char *)MENU_SHADE_INDICATOR_XBM_DATA,
396 (char *)MENU_SHADE_INDICATOR_XBM_DATA,
397 MENU_SHADE_INDICATOR_XBM_SIZE,
398 MENU_SHADE_INDICATOR_XBM_SIZE, scr->black_pixel, scr->white_pixel);
399 if (pix != NULL)
400 pix->shared = 1;
401 scr->menu_shade_indicator = pix;
403 create_logo_image(scr);
405 scr->dock_dots = make3Dots(scr);
407 /* titlebar button pixmaps */
408 allocButtonPixmaps(scr);
411 void create_logo_image(WScreen *scr)
413 RImage *image = get_icon_image(scr, "Logo", "WMPanel", wPreferences.icon_size);
415 if (!image) {
416 wwarning(_("could not load logo image for panels: %s"), RMessageForError(RErrorCode));
417 } else {
418 WMSetApplicationIconImage(scr->wmscreen, image);
419 RReleaseImage(image);
424 *----------------------------------------------------------------------
425 * createInternalWindows--
426 * Creates some windows used internally by the program. One to
427 * receive input focus when no other window can get it and another
428 * to display window geometry information during window resize/move.
430 * Returns:
431 * Nothing
433 * Side effects:
434 * Windows are created and some colors are allocated for the
435 * window background.
436 *----------------------------------------------------------------------
438 static void createInternalWindows(WScreen * scr)
440 int vmask;
441 XSetWindowAttributes attribs;
443 /* InputOnly window to get the focus when no other window can get it */
444 vmask = CWEventMask | CWOverrideRedirect;
445 attribs.event_mask = KeyPressMask | FocusChangeMask;
446 attribs.override_redirect = True;
447 scr->no_focus_win = XCreateWindow(dpy, scr->root_win, -10, -10, 4, 4, 0, 0,
448 InputOnly, CopyFromParent, vmask, &attribs);
449 XSelectInput(dpy, scr->no_focus_win, KeyPressMask | KeyReleaseMask);
450 XMapWindow(dpy, scr->no_focus_win);
452 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
454 /* shadow window for dock buttons */
455 vmask = CWBorderPixel | CWBackPixmap | CWBackPixel | CWCursor | CWSaveUnder | CWOverrideRedirect;
456 attribs.border_pixel = scr->black_pixel;
457 attribs.save_under = True;
458 attribs.override_redirect = True;
459 attribs.background_pixmap = None;
460 attribs.background_pixel = scr->white_pixel;
461 attribs.cursor = wCursor[WCUR_DEFAULT];
462 vmask |= CWColormap;
463 attribs.colormap = scr->w_colormap;
464 scr->dock_shadow =
465 XCreateWindow(dpy, scr->root_win, 0, 0, wPreferences.icon_size,
466 wPreferences.icon_size, 0, scr->w_depth, CopyFromParent, scr->w_visual, vmask, &attribs);
468 /* workspace name balloon for clip */
469 vmask = CWBackPixel | CWSaveUnder | CWOverrideRedirect | CWColormap | CWBorderPixel;
470 attribs.save_under = True;
471 attribs.override_redirect = True;
472 attribs.colormap = scr->w_colormap;
473 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
474 attribs.border_pixel = 0; /* do not care */
475 scr->clip_balloon =
476 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
477 CopyFromParent, scr->w_visual, vmask, &attribs);
479 /* workspace name */
480 vmask = CWBackPixel | CWSaveUnder | CWOverrideRedirect | CWColormap | CWBorderPixel;
481 attribs.save_under = True;
482 attribs.override_redirect = True;
483 attribs.colormap = scr->w_colormap;
484 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
485 attribs.border_pixel = 0; /* do not care */
486 scr->workspace_name =
487 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
488 CopyFromParent, scr->w_visual, vmask, &attribs);
491 * If the window is clicked without having ButtonPress selected, the
492 * resulting event will have event.xbutton.window == root.
494 XSelectInput(dpy, scr->clip_balloon, ButtonPressMask);
498 *----------------------------------------------------------------------
499 * wScreenInit--
500 * Initializes the window manager for the given screen and
501 * allocates a WScreen descriptor for it. Many resources are allocated
502 * for the screen and the root window is setup appropriately.
504 * Returns:
505 * The WScreen descriptor for the screen.
507 * Side effects:
508 * Many resources are allocated and the IconSize property is
509 * set on the root window.
510 * The program can be aborted if some fatal error occurs.
512 * TODO: User specifiable visual.
513 *----------------------------------------------------------------------
515 WScreen *wScreenInit(int screen_number)
517 WScreen *scr;
518 XIconSize icon_size[1];
519 RContextAttributes rattr;
520 long event_mask;
521 XErrorHandler oldHandler;
522 int i;
524 scr = wmalloc(sizeof(WScreen));
526 scr->stacking_list = WMCreateTreeBag();
528 /* initialize globals */
529 scr->screen = screen_number;
530 scr->root_win = RootWindow(dpy, screen_number);
531 scr->depth = DefaultDepth(dpy, screen_number);
532 scr->colormap = DefaultColormap(dpy, screen_number);
534 scr->scr_width = WidthOfScreen(ScreenOfDisplay(dpy, screen_number));
535 scr->scr_height = HeightOfScreen(ScreenOfDisplay(dpy, screen_number));
537 wInitXinerama(scr);
539 scr->usableArea = (WArea *) wmalloc(sizeof(WArea) * wXineramaHeads(scr));
540 scr->totalUsableArea = (WArea *) wmalloc(sizeof(WArea) * wXineramaHeads(scr));
542 for (i = 0; i < wXineramaHeads(scr); ++i) {
543 WMRect rect = wGetRectForHead(scr, i);
544 scr->usableArea[i].x1 = scr->totalUsableArea[i].x1 = rect.pos.x;
545 scr->usableArea[i].y1 = scr->totalUsableArea[i].y1 = rect.pos.y;
546 scr->usableArea[i].x2 = scr->totalUsableArea[i].x2 = rect.pos.x + rect.size.width;
547 scr->usableArea[i].y2 = scr->totalUsableArea[i].y2 = rect.pos.y + rect.size.height;
550 scr->fakeGroupLeaders = WMCreateArray(16);
552 CantManageScreen = 0;
553 oldHandler = XSetErrorHandler((XErrorHandler) alreadyRunningError);
555 event_mask = EVENT_MASK;
557 if (wPreferences.disable_root_mouse) {
558 event_mask &= ~(ButtonPressMask | ButtonReleaseMask);
561 XSelectInput(dpy, scr->root_win, event_mask);
563 #ifdef KEEP_XKB_LOCK_STATUS
564 /* Only GroupLock doesn't work correctly in my system since right-alt
565 * can change mode while holding it too - ]d
567 if (wXkbSupported) {
568 XkbSelectEvents(dpy, XkbUseCoreKbd, XkbStateNotifyMask, XkbStateNotifyMask);
570 #endif /* KEEP_XKB_LOCK_STATUS */
572 #ifdef HAVE_XRANDR
573 if (has_randr)
574 XRRSelectInput(dpy, scr->root_win, RRScreenChangeNotifyMask);
575 #endif
577 XSync(dpy, False);
578 XSetErrorHandler(oldHandler);
580 if (CantManageScreen) {
581 wfree(scr);
582 return NULL;
585 XDefineCursor(dpy, scr->root_win, wCursor[WCUR_ROOT]);
587 /* screen descriptor for raster graphic library */
588 rattr.flags = RC_RenderMode | RC_ColorsPerChannel | RC_StandardColormap;
589 rattr.render_mode = wPreferences.no_dithering ? RBestMatchRendering : RDitheredRendering;
591 /* if the std colormap stuff works ok, this will be ignored */
592 rattr.colors_per_channel = wPreferences.cmap_size;
593 if (rattr.colors_per_channel < 2)
594 rattr.colors_per_channel = 2;
596 /* Use standard colormap */
597 rattr.standard_colormap_mode = RUseStdColormap;
599 if (getWVisualID(screen_number) >= 0) {
600 rattr.flags |= RC_VisualID;
601 rattr.visualid = getWVisualID(screen_number);
604 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
606 if (!scr->rcontext && RErrorCode == RERR_STDCMAPFAIL) {
607 wwarning("%s", RMessageForError(RErrorCode));
609 rattr.flags &= ~RC_StandardColormap;
610 rattr.standard_colormap_mode = RUseStdColormap;
612 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
615 if (!scr->rcontext) {
616 wwarning(_("could not initialize graphics library context: %s"), RMessageForError(RErrorCode));
617 wAbort(False);
618 } else {
619 char **formats;
620 int i = 0;
622 formats = RSupportedFileFormats();
623 if (formats) {
624 for (i = 0; formats[i] != NULL; i++) {
625 if (strcmp(formats[i], "TIFF") == 0) {
626 scr->flags.supports_tiff = 1;
627 break;
633 scr->w_win = scr->rcontext->drawable;
634 scr->w_visual = scr->rcontext->visual;
635 scr->w_depth = scr->rcontext->depth;
636 scr->w_colormap = scr->rcontext->cmap;
638 /* create screen descriptor for WINGs */
639 scr->wmscreen = WMCreateScreenWithRContext(dpy, screen_number, scr->rcontext);
641 if (!scr->wmscreen) {
642 wfatal(_("could not initialize WINGs widget set"));
643 return NULL;
646 scr->black = WMBlackColor(scr->wmscreen);
647 scr->white = WMWhiteColor(scr->wmscreen);
648 scr->gray = WMGrayColor(scr->wmscreen);
649 scr->darkGray = WMDarkGrayColor(scr->wmscreen);
651 scr->black_pixel = WMColorPixel(scr->black); /*scr->rcontext->black; */
652 scr->white_pixel = WMColorPixel(scr->white); /*scr->rcontext->white; */
653 scr->light_pixel = WMColorPixel(scr->gray);
654 scr->dark_pixel = WMColorPixel(scr->darkGray);
657 XColor xcol;
658 /* frame boder color */
659 wGetColor(scr, FRAME_BORDER_COLOR, &xcol);
660 scr->frame_border_pixel = xcol.pixel;
663 /* create GCs with default values */
664 allocGCs(scr);
666 /* for our window manager info notice board. Need to
667 * create before reading the defaults, because it will be used there.
669 scr->info_window = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, 0, 0);
671 /* read defaults for this screen */
672 wReadDefaults(scr, WDWindowMaker->dictionary);
674 createInternalWindows(scr);
676 wNETWMInitStuff(scr);
678 /* create initial workspace */
679 wWorkspaceNew(scr);
681 /* create shared pixmaps */
682 createPixmaps(scr);
684 /* set icon sizes we can accept from clients */
685 icon_size[0].min_width = 8;
686 icon_size[0].min_height = 8;
687 icon_size[0].max_width = wPreferences.icon_size - 4;
688 icon_size[0].max_height = wPreferences.icon_size - 4;
689 icon_size[0].width_inc = 1;
690 icon_size[0].height_inc = 1;
691 XSetIconSizes(dpy, scr->root_win, icon_size, 1);
693 /* setup WindowMaker protocols property in the root window */
694 PropSetWMakerProtocols(scr->root_win);
696 /* setup our noticeboard */
697 XChangeProperty(dpy, scr->info_window, _XA_WINDOWMAKER_NOTICEBOARD,
698 XA_WINDOW, 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
699 XChangeProperty(dpy, scr->root_win, _XA_WINDOWMAKER_NOTICEBOARD,
700 XA_WINDOW, 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
702 #ifdef BALLOON_TEXT
703 /* initialize balloon text stuff */
704 wBalloonInitialize(scr);
705 #endif
707 scr->info_text_font = WMBoldSystemFontOfSize(scr->wmscreen, 12);
709 scr->tech_draw_font = XLoadQueryFont(dpy, "-adobe-helvetica-bold-r-*-*-12-*-*-*-*-*-*-*");
710 if (!scr->tech_draw_font)
711 scr->tech_draw_font = XLoadQueryFont(dpy, "fixed");
713 scr->gview = WCreateGeometryView(scr->wmscreen);
714 WMRealizeWidget(scr->gview);
716 wScreenUpdateUsableArea(scr);
718 return scr;
721 void wScreenUpdateUsableArea(WScreen * scr)
724 * scr->totalUsableArea[] will become the usableArea used for Windowplacement,
725 * scr->usableArea[] will be used for iconplacement, hence no iconyard nor
726 * border.
729 WArea area;
730 int i, dock_head;
731 unsigned long best_area, tmp_area;
732 unsigned int size, position;
734 dock_head = scr->xine_info.primary_head;
735 best_area = 0;
736 size = wPreferences.workspace_border_size;
737 position = wPreferences.workspace_border_position;
739 if (scr->dock) {
740 WMRect rect;
741 rect.pos.x = scr->dock->x_pos;
742 rect.pos.y = scr->dock->y_pos;
743 rect.size.width = wPreferences.icon_size;
744 rect.size.height = wPreferences.icon_size;
745 dock_head = wGetHeadForRect(scr, rect);
748 for (i = 0; i < wXineramaHeads(scr); ++i) {
749 WMRect rect = wGetRectForHead(scr, i);
750 scr->totalUsableArea[i].x1 = rect.pos.x;
751 scr->totalUsableArea[i].y1 = rect.pos.y;
752 scr->totalUsableArea[i].x2 = rect.pos.x + rect.size.width;
753 scr->totalUsableArea[i].y2 = rect.pos.y + rect.size.height;
755 if (scr->dock && dock_head == i && (!scr->dock->lowered || wPreferences.no_window_over_dock)) {
756 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
758 if (scr->dock->on_right_side) {
759 scr->totalUsableArea[i].x2 -= offset;
760 } else {
761 scr->totalUsableArea[i].x1 += offset;
765 if (wNETWMGetUsableArea(scr, i, &area)) {
766 scr->totalUsableArea[i].x1 = WMAX(scr->totalUsableArea[i].x1, area.x1);
767 scr->totalUsableArea[i].y1 = WMAX(scr->totalUsableArea[i].y1, area.y1);
768 scr->totalUsableArea[i].x2 = WMIN(scr->totalUsableArea[i].x2, area.x2);
769 scr->totalUsableArea[i].y2 = WMIN(scr->totalUsableArea[i].y2, area.y2);
772 scr->usableArea[i] = scr->totalUsableArea[i];
774 #if 0
775 printf("usableArea[%d]: %d %d %d %d\n", i,
776 scr->usableArea[i].x1, scr->usableArea[i].x2, scr->usableArea[i].y1, scr->usableArea[i].y2);
777 #endif
778 if (wPreferences.no_window_over_icons) {
779 if (wPreferences.icon_yard & IY_VERT) {
780 if (wPreferences.icon_yard & IY_RIGHT) {
781 scr->totalUsableArea[i].x2 -= wPreferences.icon_size;
782 } else {
783 scr->totalUsableArea[i].x1 += wPreferences.icon_size;
785 } else {
786 if (wPreferences.icon_yard & IY_TOP) {
787 scr->totalUsableArea[i].y1 += wPreferences.icon_size;
788 } else {
789 scr->totalUsableArea[i].y2 -= wPreferences.icon_size;
794 if (scr->totalUsableArea[i].x2 - scr->totalUsableArea[i].x1 < rect.size.width / 2) {
795 scr->totalUsableArea[i].x1 = rect.pos.x;
796 scr->totalUsableArea[i].x2 = rect.pos.x + rect.size.width;
799 if (scr->totalUsableArea[i].y2 - scr->totalUsableArea[i].y1 < rect.size.height / 2) {
800 scr->totalUsableArea[i].y1 = rect.pos.y;
801 scr->totalUsableArea[i].y2 = rect.pos.y + rect.size.height;
804 tmp_area = (scr->totalUsableArea[i].x2 - scr->totalUsableArea[i].x1) *
805 (scr->totalUsableArea[i].y2 - scr->totalUsableArea[i].y1);
807 if (tmp_area > best_area) {
808 best_area = tmp_area;
809 area = scr->totalUsableArea[i];
812 if (size > 0 && position != WB_NONE) {
813 if (position & WB_LEFTRIGHT) {
814 scr->totalUsableArea[i].x1 += size;
815 scr->totalUsableArea[i].x2 -= size;
817 if (position & WB_TOPBOTTOM) {
818 scr->totalUsableArea[i].y1 += size;
819 scr->totalUsableArea[i].y2 -= size;
824 if (wPreferences.auto_arrange_icons)
825 wArrangeIcons(scr, True);
828 void wScreenRestoreState(WScreen * scr)
830 WMPropList *state;
831 char *path;
833 OpenRootMenu(scr, -10000, -10000, False);
834 wMenuUnmap(scr->root_menu);
836 make_keys();
838 if (wScreenCount == 1) {
839 path = wdefaultspathfordomain("WMState");
840 } else {
841 char buf[16];
842 snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
843 path = wdefaultspathfordomain(buf);
845 scr->session_state = WMReadPropListFromFile(path);
846 wfree(path);
847 if (!scr->session_state && wScreenCount > 1) {
848 path = wdefaultspathfordomain("WMState");
849 scr->session_state = WMReadPropListFromFile(path);
850 wfree(path);
853 if (!scr->session_state) {
854 scr->session_state = WMCreatePLDictionary(NULL, NULL);
857 if (!wPreferences.flags.nodock) {
858 state = WMGetFromPLDictionary(scr->session_state, dDock);
859 scr->dock = wDockRestoreState(scr, state, WM_DOCK);
862 if (!wPreferences.flags.noclip) {
863 state = WMGetFromPLDictionary(scr->session_state, dClip);
864 scr->clip_icon = wClipRestoreState(scr, state);
867 wWorkspaceRestoreState(scr);
869 wScreenUpdateUsableArea(scr);
872 void wScreenSaveState(WScreen * scr)
874 WWindow *wwin;
875 char *str;
876 WMPropList *old_state, *foo;
878 make_keys();
880 /* save state of windows */
881 wwin = scr->focused_window;
882 while (wwin) {
883 wWindowSaveState(wwin);
884 wwin = wwin->prev;
887 if (wPreferences.flags.noupdates)
888 return;
890 old_state = scr->session_state;
891 scr->session_state = WMCreatePLDictionary(NULL, NULL);
893 WMPLSetCaseSensitive(True);
895 /* save dock state to file */
896 if (!wPreferences.flags.nodock) {
897 wDockSaveState(scr, old_state);
898 } else {
899 if ((foo = WMGetFromPLDictionary(old_state, dDock)) != NULL) {
900 WMPutInPLDictionary(scr->session_state, dDock, foo);
903 if (!wPreferences.flags.noclip) {
904 wClipSaveState(scr);
905 } else {
906 if ((foo = WMGetFromPLDictionary(old_state, dClip)) != NULL) {
907 WMPutInPLDictionary(scr->session_state, dClip, foo);
911 wWorkspaceSaveState(scr, old_state);
913 if (wPreferences.save_session_on_exit) {
914 wSessionSaveState(scr);
915 } else {
916 if ((foo = WMGetFromPLDictionary(old_state, dApplications)) != NULL) {
917 WMPutInPLDictionary(scr->session_state, dApplications, foo);
919 if ((foo = WMGetFromPLDictionary(old_state, dWorkspace)) != NULL) {
920 WMPutInPLDictionary(scr->session_state, dWorkspace, foo);
924 /* clean up */
925 WMPLSetCaseSensitive(False);
927 wMenuSaveState(scr);
929 if (wScreenCount == 1) {
930 str = wdefaultspathfordomain("WMState");
931 } else {
932 char buf[16];
933 snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
934 str = wdefaultspathfordomain(buf);
936 if (!WMWritePropListToFile(scr->session_state, str)) {
937 werror(_("could not save session state in %s"), str);
939 wfree(str);
940 WMReleasePropList(old_state);
943 int wScreenBringInside(WScreen * scr, int *x, int *y, int width, int height)
945 int moved = 0;
946 int tol_w, tol_h;
948 * With respect to the head that contains most of the window.
950 int sx1, sy1, sx2, sy2;
952 WMRect rect;
953 int head, flags;
955 rect.pos.x = *x;
956 rect.pos.y = *y;
957 rect.size.width = width;
958 rect.size.height = height;
960 head = wGetRectPlacementInfo(scr, rect, &flags);
961 rect = wGetRectForHead(scr, head);
963 sx1 = rect.pos.x;
964 sy1 = rect.pos.y;
965 sx2 = sx1 + rect.size.width;
966 sy2 = sy1 + rect.size.height;
968 #if 0 /* NOTE: gives funky group movement */
969 if (flags & XFLAG_MULTIPLE) {
971 * since we span multiple heads, pull window totaly inside
973 if (*x < sx1)
974 *x = sx1, moved = 1;
975 else if (*x + width > sx2)
976 *x = sx2 - width, moved = 1;
978 if (*y < sy1)
979 *y = sy1, moved = 1;
980 else if (*y + height > sy2)
981 *y = sy2 - height, moved = 1;
983 return moved;
985 #endif
987 if (width > 20)
988 tol_w = width / 2;
989 else
990 tol_w = 20;
992 if (height > 20)
993 tol_h = height / 2;
994 else
995 tol_h = 20;
997 if (*x + width < sx1 + 10)
998 *x = sx1 - tol_w, moved = 1;
999 else if (*x >= sx2 - 10)
1000 *x = sx2 - tol_w - 1, moved = 1;
1002 if (*y < sy1 - height + 10)
1003 *y = sy1 - tol_h, moved = 1;
1004 else if (*y >= sy2 - 10)
1005 *y = sy2 - tol_h - 1, moved = 1;
1007 return moved;
1010 int wScreenKeepInside(WScreen * scr, int *x, int *y, int width, int height)
1012 int moved = 0;
1013 int sx1, sy1, sx2, sy2;
1014 WMRect rect;
1015 int head;
1017 rect.pos.x = *x;
1018 rect.pos.y = *y;
1019 rect.size.width = width;
1020 rect.size.height = height;
1022 head = wGetHeadForRect(scr, rect);
1023 rect = wGetRectForHead(scr, head);
1025 sx1 = rect.pos.x;
1026 sy1 = rect.pos.y;
1027 sx2 = sx1 + rect.size.width;
1028 sy2 = sy1 + rect.size.height;
1030 if (*x < sx1)
1031 *x = sx1, moved = 1;
1032 else if (*x + width > sx2)
1033 *x = sx2 - width, moved = 1;
1035 if (*y < sy1)
1036 *y = sy1, moved = 1;
1037 else if (*y + height > sy2)
1038 *y = sy2 - height, moved = 1;
1040 return moved;