Add option to merge the workspace-switching functionality into the dock
[wmaker-crm.git] / src / screen.c
blobaa4662b5b36c795620a8ebefa61546c69f1b052e
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;
97 static WMPropList *dDrawers = NULL;
99 static void make_keys(void)
101 if (dApplications != NULL)
102 return;
104 dApplications = WMCreatePLString("Applications");
105 dWorkspace = WMCreatePLString("Workspace");
106 dDock = WMCreatePLString("Dock");
107 dClip = WMCreatePLString("Clip");
108 dDrawers = WMCreatePLString("Drawers");
112 *----------------------------------------------------------------------
113 * alreadyRunningError--
114 * X error handler used to catch errors when trying to do
115 * XSelectInput() on the root window. These errors probably mean that
116 * there already is some other window manager running.
118 * Returns:
119 * Nothing, unless something really evil happens...
121 * Side effects:
122 * CantManageScreen is set to 1;
123 *----------------------------------------------------------------------
125 static int alreadyRunningError(Display * dpy, XErrorEvent * error)
127 CantManageScreen = 1;
128 return -1;
132 *----------------------------------------------------------------------
133 * allocButtonPixmaps--
134 * Allocate pixmaps used on window operation buttons (those in the
135 * titlebar). The pixmaps are linked to the program. If XPM is supported
136 * XPM pixmaps are used otherwise, equivalent bitmaps are used.
138 * Returns:
139 * Nothing
141 * Side effects:
142 * Allocates shared pixmaps for the screen. These pixmaps should
143 * not be freed by anybody.
144 *----------------------------------------------------------------------
146 static void allocButtonPixmaps(WScreen * scr)
148 WPixmap *pix;
150 /* create predefined pixmaps */
151 if (wPreferences.new_style == TS_NEXT) {
152 pix = wPixmapCreateFromXPMData(scr, NEXT_CLOSE_XPM);
153 } else {
154 pix = wPixmapCreateFromXPMData(scr, PRED_CLOSE_XPM);
156 if (pix)
157 pix->shared = 1;
158 scr->b_pixmaps[WBUT_CLOSE] = pix;
160 if (wPreferences.new_style == TS_NEXT) {
161 pix = wPixmapCreateFromXPMData(scr, NEXT_BROKEN_CLOSE_XPM);
162 } else {
163 pix = wPixmapCreateFromXPMData(scr, PRED_BROKEN_CLOSE_XPM);
165 if (pix)
166 pix->shared = 1;
167 scr->b_pixmaps[WBUT_BROKENCLOSE] = pix;
169 if (wPreferences.new_style == TS_NEXT) {
170 pix = wPixmapCreateFromXPMData(scr, NEXT_ICONIFY_XPM);
171 } else {
172 pix = wPixmapCreateFromXPMData(scr, PRED_ICONIFY_XPM);
174 if (pix)
175 pix->shared = 1;
176 scr->b_pixmaps[WBUT_ICONIFY] = pix;
177 #ifdef XKB_BUTTON_HINT
178 if (wPreferences.new_style == TS_NEXT) {
179 pix = wPixmapCreateFromXPMData(scr, NEXT_XKBGROUP1_XPM);
180 } else {
181 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP1_XPM);
183 if (pix)
184 pix->shared = 1;
185 scr->b_pixmaps[WBUT_XKBGROUP1] = pix;
186 if (wPreferences.new_style == TS_NEXT) {
187 pix = wPixmapCreateFromXPMData(scr, NEXT_XKBGROUP2_XPM);
188 } else {
189 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP2_XPM);
191 if (pix)
192 pix->shared = 1;
193 scr->b_pixmaps[WBUT_XKBGROUP2] = pix;
194 if (wPreferences.new_style == TS_NEXT) {
195 pix = wPixmapCreateFromXPMData(scr, NEXT_XKBGROUP3_XPM);
196 } else {
197 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP3_XPM);
199 if (pix)
200 pix->shared = 1;
201 scr->b_pixmaps[WBUT_XKBGROUP3] = pix;
202 if (wPreferences.new_style == TS_NEXT) {
203 pix = wPixmapCreateFromXPMData(scr, NEXT_XKBGROUP4_XPM);
204 } else {
205 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP4_XPM);
207 if (pix)
208 pix->shared = 1;
209 scr->b_pixmaps[WBUT_XKBGROUP4] = pix;
210 #endif
212 if (wPreferences.new_style == TS_NEXT) {
213 pix = wPixmapCreateFromXPMData(scr, NEXT_KILL_XPM);
214 } else {
215 pix = wPixmapCreateFromXPMData(scr, PRED_KILL_XPM);
217 if (pix)
218 pix->shared = 1;
219 scr->b_pixmaps[WBUT_KILL] = pix;
222 static void draw_dot(WScreen * scr, Drawable d, int x, int y, GC gc)
224 XSetForeground(dpy, gc, scr->black_pixel);
225 XDrawLine(dpy, d, gc, x, y, x + 1, y);
226 XDrawPoint(dpy, d, gc, x, y + 1);
227 XSetForeground(dpy, gc, scr->white_pixel);
228 XDrawLine(dpy, d, gc, x + 2, y, x + 2, y + 1);
229 XDrawPoint(dpy, d, gc, x + 1, y + 1);
232 static WPixmap *make3Dots(WScreen * scr)
234 WPixmap *wpix;
235 GC gc2, gc;
236 XGCValues gcv;
237 Pixmap pix, mask;
239 gc = scr->copy_gc;
240 pix = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size, wPreferences.icon_size, scr->w_depth);
241 XSetForeground(dpy, gc, scr->black_pixel);
242 XFillRectangle(dpy, pix, gc, 0, 0, wPreferences.icon_size, wPreferences.icon_size);
243 XSetForeground(dpy, gc, scr->white_pixel);
244 draw_dot(scr, pix, 4, wPreferences.icon_size - 6, gc);
245 draw_dot(scr, pix, 9, wPreferences.icon_size - 6, gc);
246 draw_dot(scr, pix, 14, wPreferences.icon_size - 6, gc);
248 mask = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size, wPreferences.icon_size, 1);
249 gcv.foreground = 0;
250 gcv.graphics_exposures = False;
251 gc2 = XCreateGC(dpy, mask, GCForeground | GCGraphicsExposures, &gcv);
252 XFillRectangle(dpy, mask, gc2, 0, 0, wPreferences.icon_size, wPreferences.icon_size);
253 XSetForeground(dpy, gc2, 1);
254 XFillRectangle(dpy, mask, gc2, 4, wPreferences.icon_size - 6, 3, 2);
255 XFillRectangle(dpy, mask, gc2, 9, wPreferences.icon_size - 6, 3, 2);
256 XFillRectangle(dpy, mask, gc2, 14, wPreferences.icon_size - 6, 3, 2);
258 XFreeGC(dpy, gc2);
260 wpix = wPixmapCreate(scr, pix, mask);
261 wpix->shared = 1;
263 return wpix;
266 static void allocGCs(WScreen * scr)
268 XGCValues gcv;
269 XColor color;
270 int gcm;
272 scr->stipple_bitmap = XCreateBitmapFromData(dpy, scr->w_win, STIPPLE_DATA, STIPPLE_WIDTH, STIPPLE_HEIGHT);
274 gcv.stipple = scr->stipple_bitmap;
275 gcv.foreground = scr->white_pixel;
276 gcv.fill_style = FillStippled;
277 gcv.graphics_exposures = False;
278 gcm = GCForeground | GCStipple | GCFillStyle | GCGraphicsExposures;
279 scr->stipple_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
281 /* selected icon border GCs */
282 gcv.function = GXcopy;
283 gcv.foreground = scr->white_pixel;
284 gcv.background = scr->black_pixel;
285 gcv.line_width = 1;
286 gcv.line_style = LineDoubleDash;
287 gcv.fill_style = FillSolid;
288 gcv.dash_offset = 0;
289 gcv.dashes = 4;
290 gcv.graphics_exposures = False;
292 gcm = GCFunction | GCGraphicsExposures;
293 gcm |= GCForeground | GCBackground;
294 gcm |= GCLineWidth | GCLineStyle;
295 gcm |= GCFillStyle;
296 gcm |= GCDashOffset | GCDashList;
298 scr->icon_select_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
300 scr->menu_title_color[0] = WMRetainColor(scr->white);
302 /* don't retain scr->black here because we may alter its alpha */
303 scr->mtext_color = WMCreateRGBColor(scr->wmscreen, 0, 0, 0, True);
304 scr->dtext_color = WMCreateRGBColor(scr->wmscreen, 0, 0, 0, True);
306 /* frame GC */
307 wGetColor(scr, DEF_FRAME_COLOR, &color);
308 gcv.function = GXxor;
309 /* this will raise the probability of the XORed color being different
310 * of the original color in PseudoColor when not all color cells are
311 * initialized */
312 if (DefaultVisual(dpy, scr->screen)->class == PseudoColor)
313 gcv.plane_mask = (1 << (scr->depth - 1)) | 1;
314 else
315 gcv.plane_mask = AllPlanes;
316 gcv.foreground = color.pixel;
317 if (gcv.foreground == 0)
318 gcv.foreground = 1;
319 gcv.line_width = DEF_FRAME_THICKNESS;
320 gcv.subwindow_mode = IncludeInferiors;
321 gcv.graphics_exposures = False;
322 scr->frame_gc = XCreateGC(dpy, scr->root_win, GCForeground | GCGraphicsExposures
323 | GCFunction | GCSubwindowMode | GCLineWidth | GCPlaneMask, &gcv);
325 /* line GC */
326 gcv.foreground = color.pixel;
328 if (gcv.foreground == 0)
329 /* XOR:ing with a zero is not going to be of much use, so
330 in that case, we somewhat arbitrarily xor with 17 instead. */
331 gcv.foreground = 17;
333 gcv.function = GXxor;
334 gcv.subwindow_mode = IncludeInferiors;
335 gcv.line_width = 1;
336 gcv.cap_style = CapRound;
337 gcv.graphics_exposures = False;
338 gcm = GCForeground | GCFunction | GCSubwindowMode | GCLineWidth | GCCapStyle | GCGraphicsExposures;
339 scr->line_gc = XCreateGC(dpy, scr->root_win, gcm, &gcv);
341 scr->line_pixel = gcv.foreground;
343 /* copy GC */
344 gcv.foreground = scr->white_pixel;
345 gcv.background = scr->black_pixel;
346 gcv.graphics_exposures = False;
347 scr->copy_gc = XCreateGC(dpy, scr->w_win, GCForeground | GCBackground | GCGraphicsExposures, &gcv);
349 /* misc drawing GC */
350 gcv.graphics_exposures = False;
351 gcm = GCGraphicsExposures;
352 scr->draw_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
354 assert(scr->stipple_bitmap != None);
356 /* mono GC */
357 scr->mono_gc = XCreateGC(dpy, scr->stipple_bitmap, gcm, &gcv);
360 static void createPixmaps(WScreen * scr)
362 WPixmap *pix;
364 /* load pixmaps */
365 pix = wPixmapCreateFromXBMData(scr, (char *)MENU_RADIO_INDICATOR_XBM_DATA,
366 (char *)MENU_RADIO_INDICATOR_XBM_DATA,
367 MENU_RADIO_INDICATOR_XBM_SIZE,
368 MENU_RADIO_INDICATOR_XBM_SIZE, scr->black_pixel, scr->white_pixel);
369 if (pix != NULL)
370 pix->shared = 1;
371 scr->menu_radio_indicator = pix;
373 pix = wPixmapCreateFromXBMData(scr, (char *)MENU_CHECK_INDICATOR_XBM_DATA,
374 (char *)MENU_CHECK_INDICATOR_XBM_DATA,
375 MENU_CHECK_INDICATOR_XBM_SIZE,
376 MENU_CHECK_INDICATOR_XBM_SIZE, scr->black_pixel, scr->white_pixel);
377 if (pix != NULL)
378 pix->shared = 1;
379 scr->menu_check_indicator = pix;
381 pix = wPixmapCreateFromXBMData(scr, (char *)MENU_MINI_INDICATOR_XBM_DATA,
382 (char *)MENU_MINI_INDICATOR_XBM_DATA,
383 MENU_MINI_INDICATOR_XBM_SIZE,
384 MENU_MINI_INDICATOR_XBM_SIZE, scr->black_pixel, scr->white_pixel);
385 if (pix != NULL)
386 pix->shared = 1;
387 scr->menu_mini_indicator = pix;
389 pix = wPixmapCreateFromXBMData(scr, (char *)MENU_HIDE_INDICATOR_XBM_DATA,
390 (char *)MENU_HIDE_INDICATOR_XBM_DATA,
391 MENU_HIDE_INDICATOR_XBM_SIZE,
392 MENU_HIDE_INDICATOR_XBM_SIZE, scr->black_pixel, scr->white_pixel);
393 if (pix != NULL)
394 pix->shared = 1;
395 scr->menu_hide_indicator = pix;
397 pix = wPixmapCreateFromXBMData(scr, (char *)MENU_SHADE_INDICATOR_XBM_DATA,
398 (char *)MENU_SHADE_INDICATOR_XBM_DATA,
399 MENU_SHADE_INDICATOR_XBM_SIZE,
400 MENU_SHADE_INDICATOR_XBM_SIZE, scr->black_pixel, scr->white_pixel);
401 if (pix != NULL)
402 pix->shared = 1;
403 scr->menu_shade_indicator = pix;
405 create_logo_image(scr);
407 scr->dock_dots = make3Dots(scr);
409 /* titlebar button pixmaps */
410 allocButtonPixmaps(scr);
413 void create_logo_image(WScreen *scr)
415 RImage *image = get_icon_image(scr, "Logo", "WMPanel", wPreferences.icon_size);
417 if (!image) {
418 wwarning(_("could not load logo image for panels: %s"), RMessageForError(RErrorCode));
419 } else {
420 WMSetApplicationIconImage(scr->wmscreen, image);
421 RReleaseImage(image);
426 *----------------------------------------------------------------------
427 * createInternalWindows--
428 * Creates some windows used internally by the program. One to
429 * receive input focus when no other window can get it and another
430 * to display window geometry information during window resize/move.
432 * Returns:
433 * Nothing
435 * Side effects:
436 * Windows are created and some colors are allocated for the
437 * window background.
438 *----------------------------------------------------------------------
440 static void createInternalWindows(WScreen * scr)
442 int vmask;
443 XSetWindowAttributes attribs;
445 /* InputOnly window to get the focus when no other window can get it */
446 vmask = CWEventMask | CWOverrideRedirect;
447 attribs.event_mask = KeyPressMask | FocusChangeMask;
448 attribs.override_redirect = True;
449 scr->no_focus_win = XCreateWindow(dpy, scr->root_win, -10, -10, 4, 4, 0, 0,
450 InputOnly, CopyFromParent, vmask, &attribs);
451 XSelectInput(dpy, scr->no_focus_win, KeyPressMask | KeyReleaseMask);
452 XMapWindow(dpy, scr->no_focus_win);
454 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
456 /* shadow window for dock buttons */
457 vmask = CWBorderPixel | CWBackPixmap | CWBackPixel | CWCursor | CWSaveUnder | CWOverrideRedirect;
458 attribs.border_pixel = scr->black_pixel;
459 attribs.save_under = True;
460 attribs.override_redirect = True;
461 attribs.background_pixmap = None;
462 attribs.background_pixel = scr->white_pixel;
463 attribs.cursor = wCursor[WCUR_DEFAULT];
464 vmask |= CWColormap;
465 attribs.colormap = scr->w_colormap;
466 scr->dock_shadow =
467 XCreateWindow(dpy, scr->root_win, 0, 0, wPreferences.icon_size,
468 wPreferences.icon_size, 0, scr->w_depth, CopyFromParent, scr->w_visual, vmask, &attribs);
470 /* workspace name balloon for clip */
471 vmask = CWBackPixel | CWSaveUnder | CWOverrideRedirect | CWColormap | CWBorderPixel;
472 attribs.save_under = True;
473 attribs.override_redirect = True;
474 attribs.colormap = scr->w_colormap;
475 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
476 attribs.border_pixel = 0; /* do not care */
477 scr->clip_balloon =
478 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
479 CopyFromParent, scr->w_visual, vmask, &attribs);
481 /* workspace name */
482 vmask = CWBackPixel | CWSaveUnder | CWOverrideRedirect | CWColormap | CWBorderPixel;
483 attribs.save_under = True;
484 attribs.override_redirect = True;
485 attribs.colormap = scr->w_colormap;
486 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
487 attribs.border_pixel = 0; /* do not care */
488 scr->workspace_name =
489 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
490 CopyFromParent, scr->w_visual, vmask, &attribs);
493 * If the window is clicked without having ButtonPress selected, the
494 * resulting event will have event.xbutton.window == root.
496 XSelectInput(dpy, scr->clip_balloon, ButtonPressMask);
500 *----------------------------------------------------------------------
501 * wScreenInit--
502 * Initializes the window manager for the given screen and
503 * allocates a WScreen descriptor for it. Many resources are allocated
504 * for the screen and the root window is setup appropriately.
506 * Returns:
507 * The WScreen descriptor for the screen.
509 * Side effects:
510 * Many resources are allocated and the IconSize property is
511 * set on the root window.
512 * The program can be aborted if some fatal error occurs.
514 * TODO: User specifiable visual.
515 *----------------------------------------------------------------------
517 WScreen *wScreenInit(int screen_number)
519 WScreen *scr;
520 XIconSize icon_size[1];
521 RContextAttributes rattr;
522 long event_mask;
523 XErrorHandler oldHandler;
524 int i;
526 scr = wmalloc(sizeof(WScreen));
528 scr->stacking_list = WMCreateTreeBag();
530 /* initialize globals */
531 scr->screen = screen_number;
532 scr->root_win = RootWindow(dpy, screen_number);
533 scr->depth = DefaultDepth(dpy, screen_number);
534 scr->colormap = DefaultColormap(dpy, screen_number);
536 scr->scr_width = WidthOfScreen(ScreenOfDisplay(dpy, screen_number));
537 scr->scr_height = HeightOfScreen(ScreenOfDisplay(dpy, screen_number));
539 wInitXinerama(scr);
541 scr->usableArea = (WArea *) wmalloc(sizeof(WArea) * wXineramaHeads(scr));
542 scr->totalUsableArea = (WArea *) wmalloc(sizeof(WArea) * wXineramaHeads(scr));
544 for (i = 0; i < wXineramaHeads(scr); ++i) {
545 WMRect rect = wGetRectForHead(scr, i);
546 scr->usableArea[i].x1 = scr->totalUsableArea[i].x1 = rect.pos.x;
547 scr->usableArea[i].y1 = scr->totalUsableArea[i].y1 = rect.pos.y;
548 scr->usableArea[i].x2 = scr->totalUsableArea[i].x2 = rect.pos.x + rect.size.width;
549 scr->usableArea[i].y2 = scr->totalUsableArea[i].y2 = rect.pos.y + rect.size.height;
552 scr->fakeGroupLeaders = WMCreateArray(16);
554 CantManageScreen = 0;
555 oldHandler = XSetErrorHandler((XErrorHandler) alreadyRunningError);
557 event_mask = EVENT_MASK;
559 if (wPreferences.disable_root_mouse) {
560 event_mask &= ~(ButtonPressMask | ButtonReleaseMask);
563 XSelectInput(dpy, scr->root_win, event_mask);
565 #ifdef KEEP_XKB_LOCK_STATUS
566 /* Only GroupLock doesn't work correctly in my system since right-alt
567 * can change mode while holding it too - ]d
569 if (wXkbSupported) {
570 XkbSelectEvents(dpy, XkbUseCoreKbd, XkbStateNotifyMask, XkbStateNotifyMask);
572 #endif /* KEEP_XKB_LOCK_STATUS */
574 #ifdef HAVE_XRANDR
575 if (has_randr)
576 XRRSelectInput(dpy, scr->root_win, RRScreenChangeNotifyMask);
577 #endif
579 XSync(dpy, False);
580 XSetErrorHandler(oldHandler);
582 if (CantManageScreen) {
583 wfree(scr);
584 return NULL;
587 XDefineCursor(dpy, scr->root_win, wCursor[WCUR_ROOT]);
589 /* screen descriptor for raster graphic library */
590 rattr.flags = RC_RenderMode | RC_ColorsPerChannel | RC_StandardColormap;
591 rattr.render_mode = wPreferences.no_dithering ? RBestMatchRendering : RDitheredRendering;
593 /* if the std colormap stuff works ok, this will be ignored */
594 rattr.colors_per_channel = wPreferences.cmap_size;
595 if (rattr.colors_per_channel < 2)
596 rattr.colors_per_channel = 2;
598 /* Use standard colormap */
599 rattr.standard_colormap_mode = RUseStdColormap;
601 if (getWVisualID(screen_number) >= 0) {
602 rattr.flags |= RC_VisualID;
603 rattr.visualid = getWVisualID(screen_number);
606 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
608 if (!scr->rcontext && RErrorCode == RERR_STDCMAPFAIL) {
609 wwarning("%s", RMessageForError(RErrorCode));
611 rattr.flags &= ~RC_StandardColormap;
612 rattr.standard_colormap_mode = RUseStdColormap;
614 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
617 if (!scr->rcontext) {
618 wwarning(_("could not initialize graphics library context: %s"), RMessageForError(RErrorCode));
619 wAbort(False);
620 } else {
621 char **formats;
622 int i = 0;
624 formats = RSupportedFileFormats();
625 if (formats) {
626 for (i = 0; formats[i] != NULL; i++) {
627 if (strcmp(formats[i], "TIFF") == 0) {
628 scr->flags.supports_tiff = 1;
629 break;
635 scr->w_win = scr->rcontext->drawable;
636 scr->w_visual = scr->rcontext->visual;
637 scr->w_depth = scr->rcontext->depth;
638 scr->w_colormap = scr->rcontext->cmap;
640 /* create screen descriptor for WINGs */
641 scr->wmscreen = WMCreateScreenWithRContext(dpy, screen_number, scr->rcontext);
643 if (!scr->wmscreen) {
644 wfatal(_("could not initialize WINGs widget set"));
645 return NULL;
648 scr->black = WMBlackColor(scr->wmscreen);
649 scr->white = WMWhiteColor(scr->wmscreen);
650 scr->gray = WMGrayColor(scr->wmscreen);
651 scr->darkGray = WMDarkGrayColor(scr->wmscreen);
653 scr->black_pixel = WMColorPixel(scr->black); /*scr->rcontext->black; */
654 scr->white_pixel = WMColorPixel(scr->white); /*scr->rcontext->white; */
655 scr->light_pixel = WMColorPixel(scr->gray);
656 scr->dark_pixel = WMColorPixel(scr->darkGray);
659 XColor xcol;
660 /* frame boder color */
661 wGetColor(scr, FRAME_BORDER_COLOR, &xcol);
662 scr->frame_border_pixel = xcol.pixel;
665 /* create GCs with default values */
666 allocGCs(scr);
668 /* for our window manager info notice board. Need to
669 * create before reading the defaults, because it will be used there.
671 scr->info_window = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, 0, 0);
673 /* read defaults for this screen */
674 wReadDefaults(scr, WDWindowMaker->dictionary);
676 createInternalWindows(scr);
678 wNETWMInitStuff(scr);
680 /* create initial workspace */
681 wWorkspaceNew(scr);
683 /* create shared pixmaps */
684 createPixmaps(scr);
686 /* set icon sizes we can accept from clients */
687 icon_size[0].min_width = 8;
688 icon_size[0].min_height = 8;
689 icon_size[0].max_width = wPreferences.icon_size - 4;
690 icon_size[0].max_height = wPreferences.icon_size - 4;
691 icon_size[0].width_inc = 1;
692 icon_size[0].height_inc = 1;
693 XSetIconSizes(dpy, scr->root_win, icon_size, 1);
695 /* setup WindowMaker protocols property in the root window */
696 PropSetWMakerProtocols(scr->root_win);
698 /* setup our noticeboard */
699 XChangeProperty(dpy, scr->info_window, _XA_WINDOWMAKER_NOTICEBOARD,
700 XA_WINDOW, 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
701 XChangeProperty(dpy, scr->root_win, _XA_WINDOWMAKER_NOTICEBOARD,
702 XA_WINDOW, 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
704 #ifdef BALLOON_TEXT
705 /* initialize balloon text stuff */
706 wBalloonInitialize(scr);
707 #endif
709 scr->info_text_font = WMBoldSystemFontOfSize(scr->wmscreen, 12);
711 scr->tech_draw_font = XLoadQueryFont(dpy, "-adobe-helvetica-bold-r-*-*-12-*-*-*-*-*-*-*");
712 if (!scr->tech_draw_font)
713 scr->tech_draw_font = XLoadQueryFont(dpy, "fixed");
715 scr->gview = WCreateGeometryView(scr->wmscreen);
716 WMRealizeWidget(scr->gview);
718 wScreenUpdateUsableArea(scr);
720 return scr;
723 void wScreenUpdateUsableArea(WScreen * scr)
726 * scr->totalUsableArea[] will become the usableArea used for Windowplacement,
727 * scr->usableArea[] will be used for iconplacement, hence no iconyard nor
728 * border.
731 WArea area;
732 int i, dock_head;
733 unsigned long best_area, tmp_area;
734 unsigned int size, position;
736 dock_head = scr->xine_info.primary_head;
737 best_area = 0;
738 size = wPreferences.workspace_border_size;
739 position = wPreferences.workspace_border_position;
741 if (scr->dock) {
742 WMRect rect;
743 rect.pos.x = scr->dock->x_pos;
744 rect.pos.y = scr->dock->y_pos;
745 rect.size.width = wPreferences.icon_size;
746 rect.size.height = wPreferences.icon_size;
747 dock_head = wGetHeadForRect(scr, rect);
750 for (i = 0; i < wXineramaHeads(scr); ++i) {
751 WMRect rect = wGetRectForHead(scr, i);
752 scr->totalUsableArea[i].x1 = rect.pos.x;
753 scr->totalUsableArea[i].y1 = rect.pos.y;
754 scr->totalUsableArea[i].x2 = rect.pos.x + rect.size.width;
755 scr->totalUsableArea[i].y2 = rect.pos.y + rect.size.height;
757 if (scr->dock && dock_head == i && (!scr->dock->lowered || wPreferences.no_window_over_dock)) {
758 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
760 if (scr->dock->on_right_side) {
761 scr->totalUsableArea[i].x2 -= offset;
762 } else {
763 scr->totalUsableArea[i].x1 += offset;
767 if (wNETWMGetUsableArea(scr, i, &area)) {
768 scr->totalUsableArea[i].x1 = WMAX(scr->totalUsableArea[i].x1, area.x1);
769 scr->totalUsableArea[i].y1 = WMAX(scr->totalUsableArea[i].y1, area.y1);
770 scr->totalUsableArea[i].x2 = WMIN(scr->totalUsableArea[i].x2, area.x2);
771 scr->totalUsableArea[i].y2 = WMIN(scr->totalUsableArea[i].y2, area.y2);
774 scr->usableArea[i] = scr->totalUsableArea[i];
776 #if 0
777 printf("usableArea[%d]: %d %d %d %d\n", i,
778 scr->usableArea[i].x1, scr->usableArea[i].x2, scr->usableArea[i].y1, scr->usableArea[i].y2);
779 #endif
780 if (wPreferences.no_window_over_icons) {
781 if (wPreferences.icon_yard & IY_VERT) {
782 if (wPreferences.icon_yard & IY_RIGHT) {
783 scr->totalUsableArea[i].x2 -= wPreferences.icon_size;
784 } else {
785 scr->totalUsableArea[i].x1 += wPreferences.icon_size;
787 } else {
788 if (wPreferences.icon_yard & IY_TOP) {
789 scr->totalUsableArea[i].y1 += wPreferences.icon_size;
790 } else {
791 scr->totalUsableArea[i].y2 -= wPreferences.icon_size;
796 if (scr->totalUsableArea[i].x2 - scr->totalUsableArea[i].x1 < rect.size.width / 2) {
797 scr->totalUsableArea[i].x1 = rect.pos.x;
798 scr->totalUsableArea[i].x2 = rect.pos.x + rect.size.width;
801 if (scr->totalUsableArea[i].y2 - scr->totalUsableArea[i].y1 < rect.size.height / 2) {
802 scr->totalUsableArea[i].y1 = rect.pos.y;
803 scr->totalUsableArea[i].y2 = rect.pos.y + rect.size.height;
806 tmp_area = (scr->totalUsableArea[i].x2 - scr->totalUsableArea[i].x1) *
807 (scr->totalUsableArea[i].y2 - scr->totalUsableArea[i].y1);
809 if (tmp_area > best_area) {
810 best_area = tmp_area;
811 area = scr->totalUsableArea[i];
814 if (size > 0 && position != WB_NONE) {
815 if (position & WB_LEFTRIGHT) {
816 scr->totalUsableArea[i].x1 += size;
817 scr->totalUsableArea[i].x2 -= size;
819 if (position & WB_TOPBOTTOM) {
820 scr->totalUsableArea[i].y1 += size;
821 scr->totalUsableArea[i].y2 -= size;
826 if (wPreferences.auto_arrange_icons)
827 wArrangeIcons(scr, True);
830 void wScreenRestoreState(WScreen * scr)
832 WMPropList *state;
833 char *path;
835 OpenRootMenu(scr, -10000, -10000, False);
836 wMenuUnmap(scr->root_menu);
838 make_keys();
840 if (wScreenCount == 1) {
841 path = wdefaultspathfordomain("WMState");
842 } else {
843 char buf[16];
844 snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
845 path = wdefaultspathfordomain(buf);
847 scr->session_state = WMReadPropListFromFile(path);
848 wfree(path);
849 if (!scr->session_state && wScreenCount > 1) {
850 path = wdefaultspathfordomain("WMState");
851 scr->session_state = WMReadPropListFromFile(path);
852 wfree(path);
855 if (!scr->session_state) {
856 scr->session_state = WMCreatePLDictionary(NULL, NULL);
859 if (!wPreferences.flags.nodock) {
860 state = WMGetFromPLDictionary(scr->session_state, dDock);
861 scr->dock = wDockRestoreState(scr, state, WM_DOCK);
862 /* If clip_merged_in_dock, setting scr->clip_icon is done by
863 * wDockRestoreState()->wDockCreate()->mainIconCreate() */
866 if (!wPreferences.flags.noclip) {
867 state = WMGetFromPLDictionary(scr->session_state, dClip);
868 scr->clip_icon = wClipRestoreState(scr, state);
871 if (!wPreferences.flags.nodrawer) {
872 wDrawersRestoreState(scr);
875 wWorkspaceRestoreState(scr);
877 wScreenUpdateUsableArea(scr);
880 void wScreenSaveState(WScreen * scr)
882 WWindow *wwin;
883 char *str;
884 WMPropList *old_state, *foo;
886 make_keys();
888 /* save state of windows */
889 wwin = scr->focused_window;
890 while (wwin) {
891 wWindowSaveState(wwin);
892 wwin = wwin->prev;
895 if (wPreferences.flags.noupdates)
896 return;
898 old_state = scr->session_state;
899 scr->session_state = WMCreatePLDictionary(NULL, NULL);
901 WMPLSetCaseSensitive(True);
903 /* save dock state to file */
904 if (!wPreferences.flags.nodock) {
905 wDockSaveState(scr, old_state);
906 } else {
907 if ((foo = WMGetFromPLDictionary(old_state, dDock)) != NULL) {
908 WMPutInPLDictionary(scr->session_state, dDock, foo);
911 if (!wPreferences.flags.noclip) {
912 wClipSaveState(scr);
913 } else {
914 if ((foo = WMGetFromPLDictionary(old_state, dClip)) != NULL) {
915 WMPutInPLDictionary(scr->session_state, dClip, foo);
919 wWorkspaceSaveState(scr, old_state);
921 if (!wPreferences.flags.nodrawer) {
922 wDrawersSaveState(scr);
923 } else {
924 if ((foo = WMGetFromPLDictionary(old_state, dDrawers)) != NULL) {
925 WMPutInPLDictionary(scr->session_state, dDrawers, foo);
930 if (wPreferences.save_session_on_exit) {
931 wSessionSaveState(scr);
932 } else {
933 if ((foo = WMGetFromPLDictionary(old_state, dApplications)) != NULL) {
934 WMPutInPLDictionary(scr->session_state, dApplications, foo);
936 if ((foo = WMGetFromPLDictionary(old_state, dWorkspace)) != NULL) {
937 WMPutInPLDictionary(scr->session_state, dWorkspace, foo);
941 /* clean up */
942 WMPLSetCaseSensitive(False);
944 wMenuSaveState(scr);
946 if (wScreenCount == 1) {
947 str = wdefaultspathfordomain("WMState");
948 } else {
949 char buf[16];
950 snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
951 str = wdefaultspathfordomain(buf);
953 if (!WMWritePropListToFile(scr->session_state, str)) {
954 werror(_("could not save session state in %s"), str);
956 wfree(str);
957 WMReleasePropList(old_state);
960 int wScreenBringInside(WScreen * scr, int *x, int *y, int width, int height)
962 int moved = 0;
963 int tol_w, tol_h;
965 * With respect to the head that contains most of the window.
967 int sx1, sy1, sx2, sy2;
969 WMRect rect;
970 int head, flags;
972 rect.pos.x = *x;
973 rect.pos.y = *y;
974 rect.size.width = width;
975 rect.size.height = height;
977 head = wGetRectPlacementInfo(scr, rect, &flags);
978 rect = wGetRectForHead(scr, head);
980 sx1 = rect.pos.x;
981 sy1 = rect.pos.y;
982 sx2 = sx1 + rect.size.width;
983 sy2 = sy1 + rect.size.height;
985 #if 0 /* NOTE: gives funky group movement */
986 if (flags & XFLAG_MULTIPLE) {
988 * since we span multiple heads, pull window totaly inside
990 if (*x < sx1)
991 *x = sx1, moved = 1;
992 else if (*x + width > sx2)
993 *x = sx2 - width, moved = 1;
995 if (*y < sy1)
996 *y = sy1, moved = 1;
997 else if (*y + height > sy2)
998 *y = sy2 - height, moved = 1;
1000 return moved;
1002 #endif
1004 if (width > 20)
1005 tol_w = width / 2;
1006 else
1007 tol_w = 20;
1009 if (height > 20)
1010 tol_h = height / 2;
1011 else
1012 tol_h = 20;
1014 if (*x + width < sx1 + 10)
1015 *x = sx1 - tol_w, moved = 1;
1016 else if (*x >= sx2 - 10)
1017 *x = sx2 - tol_w - 1, moved = 1;
1019 if (*y < sy1 - height + 10)
1020 *y = sy1 - tol_h, moved = 1;
1021 else if (*y >= sy2 - 10)
1022 *y = sy2 - tol_h - 1, moved = 1;
1024 return moved;
1027 int wScreenKeepInside(WScreen * scr, int *x, int *y, int width, int height)
1029 int moved = 0;
1030 int sx1, sy1, sx2, sy2;
1031 WMRect rect;
1032 int head;
1034 rect.pos.x = *x;
1035 rect.pos.y = *y;
1036 rect.size.width = width;
1037 rect.size.height = height;
1039 head = wGetHeadForRect(scr, rect);
1040 rect = wGetRectForHead(scr, head);
1042 sx1 = rect.pos.x;
1043 sy1 = rect.pos.y;
1044 sx2 = sx1 + rect.size.width;
1045 sy2 = sy1 + rect.size.height;
1047 if (*x < sx1)
1048 *x = sx1, moved = 1;
1049 else if (*x + width > sx2)
1050 *x = sx2 - width, moved = 1;
1052 if (*y < sy1)
1053 *y = sy1, moved = 1;
1054 else if (*y + height > sy2)
1055 *y = sy2 - height, moved = 1;
1057 return moved;