Partial fix for focus stealing
[wmaker-crm.git] / src / wdefaults.c
blob94487d0d70e1fdacea3f0537f3a72bf97dfa4ef9
1 /* wdefaults.c - window specific defaults
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 <unistd.h>
27 #include <string.h>
28 #include <strings.h>
29 #include <ctype.h>
31 #include <X11/Xlib.h>
32 #include <X11/Xutil.h>
33 #include <X11/keysym.h>
35 #include <wraster.h>
37 #include "WindowMaker.h"
38 #include "window.h"
39 #include "screen.h"
40 #include "funcs.h"
41 #include "workspace.h"
42 #include "defaults.h"
43 #include "icon.h"
45 /* Global stuff */
46 extern WPreferences wPreferences;
47 extern WMPropList *wAttributeDomainName;
48 extern WDDomain *WDWindowAttributes;
50 /* Local stuff */
52 /* type converters */
53 static int getBool(WMPropList *, WMPropList *);
54 static char *getString(WMPropList *, WMPropList *);
55 static WMPropList *ANoTitlebar = NULL;
56 static WMPropList *ANoResizebar;
57 static WMPropList *ANoMiniaturizeButton;
58 static WMPropList *ANoCloseButton;
59 static WMPropList *ANoBorder;
60 static WMPropList *ANoHideOthers;
61 static WMPropList *ANoMouseBindings;
62 static WMPropList *ANoKeyBindings;
63 static WMPropList *ANoAppIcon; /* app */
64 static WMPropList *AKeepOnTop;
65 static WMPropList *AKeepOnBottom;
66 static WMPropList *AOmnipresent;
67 static WMPropList *ASkipWindowList;
68 static WMPropList *ASkipSwitchPanel;
69 static WMPropList *AKeepInsideScreen;
70 static WMPropList *AUnfocusable;
71 static WMPropList *AAlwaysUserIcon;
72 static WMPropList *AStartMiniaturized;
73 static WMPropList *AStartMaximized;
74 static WMPropList *AStartHidden; /* app */
75 static WMPropList *ADontSaveSession; /* app */
76 static WMPropList *AEmulateAppIcon;
77 static WMPropList *AFocusAcrossWorkspace;
78 static WMPropList *AFullMaximize;
79 static WMPropList *ASharedAppIcon; /* app */
80 #ifdef XKB_BUTTON_HINT
81 static WMPropList *ANoLanguageButton;
82 #endif
83 static WMPropList *AStartWorkspace;
84 static WMPropList *AIcon;
85 static WMPropList *AnyWindow;
86 static WMPropList *No;
88 static void init_wdefaults(WScreen * scr)
90 AIcon = WMCreatePLString("Icon");
92 ANoTitlebar = WMCreatePLString("NoTitlebar");
93 ANoResizebar = WMCreatePLString("NoResizebar");
94 ANoMiniaturizeButton = WMCreatePLString("NoMiniaturizeButton");
95 ANoCloseButton = WMCreatePLString("NoCloseButton");
96 ANoBorder = WMCreatePLString("NoBorder");
97 ANoHideOthers = WMCreatePLString("NoHideOthers");
98 ANoMouseBindings = WMCreatePLString("NoMouseBindings");
99 ANoKeyBindings = WMCreatePLString("NoKeyBindings");
100 ANoAppIcon = WMCreatePLString("NoAppIcon");
101 AKeepOnTop = WMCreatePLString("KeepOnTop");
102 AKeepOnBottom = WMCreatePLString("KeepOnBottom");
103 AOmnipresent = WMCreatePLString("Omnipresent");
104 ASkipWindowList = WMCreatePLString("SkipWindowList");
105 ASkipSwitchPanel = WMCreatePLString("SkipSwitchPanel");
106 AKeepInsideScreen = WMCreatePLString("KeepInsideScreen");
107 AUnfocusable = WMCreatePLString("Unfocusable");
108 AAlwaysUserIcon = WMCreatePLString("AlwaysUserIcon");
109 AStartMiniaturized = WMCreatePLString("StartMiniaturized");
110 AStartHidden = WMCreatePLString("StartHidden");
111 AStartMaximized = WMCreatePLString("StartMaximized");
112 ADontSaveSession = WMCreatePLString("DontSaveSession");
113 AEmulateAppIcon = WMCreatePLString("EmulateAppIcon");
114 AFocusAcrossWorkspace = WMCreatePLString("FocusAcrossWorkspace");
115 AFullMaximize = WMCreatePLString("FullMaximize");
116 ASharedAppIcon = WMCreatePLString("SharedAppIcon");
117 #ifdef XKB_BUTTON_HINT
118 ANoLanguageButton = WMCreatePLString("NoLanguageButton");
119 #endif
121 AStartWorkspace = WMCreatePLString("StartWorkspace");
123 AnyWindow = WMCreatePLString("*");
124 No = WMCreatePLString("No");
126 if (!scr->wattribs) {
127 scr->wattribs = PLGetDomain(wAttributeDomainName);
128 } */
131 static WMPropList *get_value(WMPropList * dict_win, WMPropList * dict_class, WMPropList * dict_name,
132 WMPropList * dict_any, WMPropList * option, WMPropList * default_value,
133 Bool useGlobalDefault)
135 WMPropList *value;
137 if (dict_win) {
138 value = WMGetFromPLDictionary(dict_win, option);
139 if (value)
140 return value;
143 if (dict_name) {
144 value = WMGetFromPLDictionary(dict_name, option);
145 if (value)
146 return value;
149 if (dict_class) {
150 value = WMGetFromPLDictionary(dict_class, option);
151 if (value)
152 return value;
155 if (!useGlobalDefault)
156 return NULL;
158 if (dict_any) {
159 value = WMGetFromPLDictionary(dict_any, option);
160 if (value)
161 return value;
164 return default_value;
168 *----------------------------------------------------------------------
169 * wDefaultFillAttributes--
170 * Retrieves attributes for the specified instance/class and
171 * fills attr with it. Values that are actually defined are also
172 * set in mask. If useGlobalDefault is True, the default for
173 * all windows ("*") will be used for when no values are found
174 * for that instance/class.
176 *----------------------------------------------------------------------
178 void
179 wDefaultFillAttributes(WScreen * scr, char *instance, char *class,
180 WWindowAttributes * attr, WWindowAttributes * mask, Bool useGlobalDefault)
182 WMPropList *value, *key1, *key2, *key3, *dw, *dc, *dn, *da;
184 if (class && instance) {
185 char *buffer;
187 buffer = wmalloc(strlen(class) + strlen(instance) + 2);
188 sprintf(buffer, "%s.%s", instance, class);
189 key1 = WMCreatePLString(buffer);
190 wfree(buffer);
191 } else {
192 key1 = NULL;
195 if (instance)
196 key2 = WMCreatePLString(instance);
197 else
198 key2 = NULL;
200 if (class)
201 key3 = WMCreatePLString(class);
202 else
203 key3 = NULL;
205 if (!ANoTitlebar) {
206 init_wdefaults(scr);
209 WMPLSetCaseSensitive(True);
211 if (WDWindowAttributes->dictionary) {
212 dw = key1 ? WMGetFromPLDictionary(WDWindowAttributes->dictionary, key1) : NULL;
213 dn = key2 ? WMGetFromPLDictionary(WDWindowAttributes->dictionary, key2) : NULL;
214 dc = key3 ? WMGetFromPLDictionary(WDWindowAttributes->dictionary, key3) : NULL;
215 if (useGlobalDefault)
216 da = WMGetFromPLDictionary(WDWindowAttributes->dictionary, AnyWindow);
217 else
218 da = NULL;
219 } else {
220 dw = NULL;
221 dn = NULL;
222 dc = NULL;
223 da = NULL;
225 if (key1)
226 WMReleasePropList(key1);
227 if (key2)
228 WMReleasePropList(key2);
229 if (key3)
230 WMReleasePropList(key3);
232 #define APPLY_VAL(value, flag, attrib) \
233 if (value) {attr->flag = getBool(attrib, value); \
234 if (mask) mask->flag = 1;}
236 /* get the data */
237 value = get_value(dw, dc, dn, da, ANoTitlebar, No, useGlobalDefault);
238 APPLY_VAL(value, no_titlebar, ANoTitlebar);
240 value = get_value(dw, dc, dn, da, ANoResizebar, No, useGlobalDefault);
241 APPLY_VAL(value, no_resizebar, ANoResizebar);
243 value = get_value(dw, dc, dn, da, ANoMiniaturizeButton, No, useGlobalDefault);
244 APPLY_VAL(value, no_miniaturize_button, ANoMiniaturizeButton);
246 value = get_value(dw, dc, dn, da, ANoCloseButton, No, useGlobalDefault);
247 APPLY_VAL(value, no_close_button, ANoCloseButton);
249 value = get_value(dw, dc, dn, da, ANoBorder, No, useGlobalDefault);
250 APPLY_VAL(value, no_border, ANoBorder);
252 value = get_value(dw, dc, dn, da, ANoHideOthers, No, useGlobalDefault);
253 APPLY_VAL(value, no_hide_others, ANoHideOthers);
255 value = get_value(dw, dc, dn, da, ANoMouseBindings, No, useGlobalDefault);
256 APPLY_VAL(value, no_bind_mouse, ANoMouseBindings);
258 value = get_value(dw, dc, dn, da, ANoKeyBindings, No, useGlobalDefault);
259 APPLY_VAL(value, no_bind_keys, ANoKeyBindings);
261 value = get_value(dw, dc, dn, da, ANoAppIcon, No, useGlobalDefault);
262 APPLY_VAL(value, no_appicon, ANoAppIcon);
264 value = get_value(dw, dc, dn, da, ASharedAppIcon, No, useGlobalDefault);
265 APPLY_VAL(value, shared_appicon, ASharedAppIcon);
267 value = get_value(dw, dc, dn, da, AKeepOnTop, No, useGlobalDefault);
268 APPLY_VAL(value, floating, AKeepOnTop);
270 value = get_value(dw, dc, dn, da, AKeepOnBottom, No, useGlobalDefault);
271 APPLY_VAL(value, sunken, AKeepOnBottom);
273 value = get_value(dw, dc, dn, da, AOmnipresent, No, useGlobalDefault);
274 APPLY_VAL(value, omnipresent, AOmnipresent);
276 value = get_value(dw, dc, dn, da, ASkipWindowList, No, useGlobalDefault);
277 APPLY_VAL(value, skip_window_list, ASkipWindowList);
279 value = get_value(dw, dc, dn, da, ASkipSwitchPanel, No, useGlobalDefault);
280 APPLY_VAL(value, skip_switchpanel, ASkipSwitchPanel);
282 value = get_value(dw, dc, dn, da, AKeepInsideScreen, No, useGlobalDefault);
283 APPLY_VAL(value, dont_move_off, AKeepInsideScreen);
285 value = get_value(dw, dc, dn, da, AUnfocusable, No, useGlobalDefault);
286 APPLY_VAL(value, no_focusable, AUnfocusable);
288 value = get_value(dw, dc, dn, da, AAlwaysUserIcon, No, useGlobalDefault);
289 APPLY_VAL(value, always_user_icon, AAlwaysUserIcon);
291 value = get_value(dw, dc, dn, da, AStartMiniaturized, No, useGlobalDefault);
292 APPLY_VAL(value, start_miniaturized, AStartMiniaturized);
294 value = get_value(dw, dc, dn, da, AStartHidden, No, useGlobalDefault);
295 APPLY_VAL(value, start_hidden, AStartHidden);
297 value = get_value(dw, dc, dn, da, AStartMaximized, No, useGlobalDefault);
298 APPLY_VAL(value, start_maximized, AStartMaximized);
300 value = get_value(dw, dc, dn, da, ADontSaveSession, No, useGlobalDefault);
301 APPLY_VAL(value, dont_save_session, ADontSaveSession);
303 value = get_value(dw, dc, dn, da, AEmulateAppIcon, No, useGlobalDefault);
304 APPLY_VAL(value, emulate_appicon, AEmulateAppIcon);
306 value = get_value(dw, dc, dn, da, AFocusAcrossWorkspace, No, useGlobalDefault);
307 APPLY_VAL(value, focus_across_wksp, AFocusAcrossWorkspace);
309 value = get_value(dw, dc, dn, da, AFullMaximize, No, useGlobalDefault);
310 APPLY_VAL(value, full_maximize, AFullMaximize);
312 #ifdef XKB_BUTTON_HINT
313 value = get_value(dw, dc, dn, da, ANoLanguageButton, No, useGlobalDefault);
314 APPLY_VAL(value, no_language_button, ANoLanguageButton);
315 #endif
317 /* clean up */
318 WMPLSetCaseSensitive(False);
321 static WMPropList *get_generic_value(WScreen *scr, char *instance, char *class,
322 WMPropList *option, Bool noDefault)
324 WMPropList *value, *key, *dict;
326 value = NULL;
328 WMPLSetCaseSensitive(True);
330 if (class && instance) {
331 char *buffer;
333 buffer = wmalloc(strlen(class) + strlen(instance) + 2);
334 sprintf(buffer, "%s.%s", instance, class);
335 key = WMCreatePLString(buffer);
336 wfree(buffer);
338 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, key);
339 WMReleasePropList(key);
341 if (dict) {
342 value = WMGetFromPLDictionary(dict, option);
346 if (!value && instance) {
347 key = WMCreatePLString(instance);
349 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, key);
350 WMReleasePropList(key);
351 if (dict) {
352 value = WMGetFromPLDictionary(dict, option);
356 if (!value && class) {
357 key = WMCreatePLString(class);
359 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, key);
360 WMReleasePropList(key);
362 if (dict) {
363 value = WMGetFromPLDictionary(dict, option);
367 if (!value && !noDefault) {
368 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, AnyWindow);
370 if (dict) {
371 value = WMGetFromPLDictionary(dict, option);
375 WMPLSetCaseSensitive(False);
377 return value;
380 char *wDefaultGetIconFile(WScreen * scr, char *instance, char *class, Bool noDefault)
382 WMPropList *value;
383 char *tmp;
385 if (!ANoTitlebar) {
386 init_wdefaults(scr);
389 if (!WDWindowAttributes->dictionary)
390 return NULL;
392 value = get_generic_value(scr, instance, class, AIcon, noDefault);
394 if (!value)
395 return NULL;
397 tmp = getString(AIcon, value);
399 return tmp;
402 RImage *wDefaultGetImage(WScreen * scr, char *winstance, char *wclass)
404 char *file_name;
405 char *path;
406 RImage *image;
408 file_name = wDefaultGetIconFile(scr, winstance, wclass, False);
409 if (!file_name)
410 return NULL;
412 path = FindImage(wPreferences.icon_path, file_name);
414 if (!path) {
415 wwarning(_("could not find icon file \"%s\""), file_name);
416 return NULL;
419 image = RLoadImage(scr->rcontext, path, 0);
420 if (!image) {
421 wwarning(_("error loading image file \"%s\": %s"), path, RMessageForError(RErrorCode));
423 wfree(path);
425 image = wIconValidateIconSize(scr, image);
427 return image;
430 int wDefaultGetStartWorkspace(WScreen * scr, char *instance, char *class)
432 WMPropList *value;
433 int w, i;
434 char *tmp;
436 if (!ANoTitlebar) {
437 init_wdefaults(scr);
440 if (!WDWindowAttributes->dictionary)
441 return -1;
443 value = get_generic_value(scr, instance, class, AStartWorkspace, False);
445 if (!value)
446 return -1;
448 tmp = getString(AStartWorkspace, value);
450 if (!tmp || strlen(tmp) == 0)
451 return -1;
453 if (sscanf(tmp, "%i", &w) != 1) {
454 w = -1;
455 for (i = 0; i < scr->workspace_count; i++) {
456 if (strcmp(scr->workspaces[i]->name, tmp) == 0) {
457 w = i;
458 break;
461 } else {
462 w--;
465 return w;
468 void wDefaultChangeIcon(WScreen * scr, char *instance, char *class, char *file)
470 WDDomain *db = WDWindowAttributes;
471 WMPropList *icon_value = NULL, *value, *attr, *key, *def_win, *def_icon = NULL;
472 WMPropList *dict = db->dictionary;
473 int same = 0;
475 if (!dict) {
476 dict = WMCreatePLDictionary(NULL, NULL);
477 if (dict) {
478 db->dictionary = dict;
479 } else {
480 return;
484 WMPLSetCaseSensitive(True);
486 if (instance && class) {
487 char *buffer;
488 buffer = wmalloc(strlen(instance) + strlen(class) + 2);
489 sprintf(buffer, "%s.%s", instance, class);
490 key = WMCreatePLString(buffer);
491 wfree(buffer);
492 } else if (instance) {
493 key = WMCreatePLString(instance);
494 } else if (class) {
495 key = WMCreatePLString(class);
496 } else {
497 key = WMRetainPropList(AnyWindow);
500 if (file) {
501 value = WMCreatePLString(file);
502 icon_value = WMCreatePLDictionary(AIcon, value, NULL);
503 WMReleasePropList(value);
505 if ((def_win = WMGetFromPLDictionary(dict, AnyWindow)) != NULL) {
506 def_icon = WMGetFromPLDictionary(def_win, AIcon);
509 if (def_icon && !strcmp(WMGetFromPLString(def_icon), file))
510 same = 1;
513 if ((attr = WMGetFromPLDictionary(dict, key)) != NULL) {
514 if (WMIsPLDictionary(attr)) {
515 if (icon_value != NULL && !same)
516 WMMergePLDictionaries(attr, icon_value, False);
517 else
518 WMRemoveFromPLDictionary(attr, AIcon);
520 } else if (icon_value != NULL && !same) {
521 WMPutInPLDictionary(dict, key, icon_value);
523 if (!wPreferences.flags.noupdates) {
524 UpdateDomainFile(db);
527 WMReleasePropList(key);
528 if (icon_value)
529 WMReleasePropList(icon_value);
531 WMPLSetCaseSensitive(False);
534 /* --------------------------- Local ----------------------- */
536 static int getBool(WMPropList * key, WMPropList * value)
538 char *val;
540 if (!WMIsPLString(value)) {
541 wwarning(_("Wrong option format for key \"%s\". Should be %s."),
542 WMGetFromPLString(key), "Boolean");
543 return 0;
545 val = WMGetFromPLString(value);
547 if ((val[1] == '\0' && (val[0] == 'y' || val[0] == 'Y' || val[0] == 'T' || val[0] == 't' || val[0] == '1'))
548 || (strcasecmp(val, "YES") == 0 || strcasecmp(val, "TRUE") == 0)) {
550 return 1;
551 } else if ((val[1] == '\0'
552 && (val[0] == 'n' || val[0] == 'N' || val[0] == 'F' || val[0] == 'f' || val[0] == '0'))
553 || (strcasecmp(val, "NO") == 0 || strcasecmp(val, "FALSE") == 0)) {
555 return 0;
556 } else {
557 wwarning(_("can't convert \"%s\" to boolean"), val);
558 /* We return False if we can't convert to BOOLEAN.
559 * This is because all options defaults to False.
560 * -1 is not checked and thus is interpreted as True,
561 * which is not good.*/
562 return 0;
566 /* WARNING: Do not free the value returned by this function!! */
567 static char *getString(WMPropList * key, WMPropList * value)
569 if (!WMIsPLString(value)) {
570 wwarning(_("Wrong option format for key \"%s\". Should be %s."), WMGetFromPLString(key), "String");
571 return NULL;
574 return WMGetFromPLString(value);