WindowMaker: New function wGetWorkspaceNumber
[wmaker-crm.git] / src / wdefaults.c
blobd89671bdd24c61941d4846ce15f38601858bd851
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 WDDomain *WDWindowAttributes;
49 /* Local stuff */
51 /* type converters */
52 static int getBool(WMPropList *, WMPropList *);
53 static char *getString(WMPropList *, WMPropList *);
54 static WMPropList *ANoTitlebar = NULL;
55 static WMPropList *ANoResizebar;
56 static WMPropList *ANoMiniaturizeButton;
57 static WMPropList *ANoMiniaturizable;
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 ANoMiniaturizable = WMCreatePLString("NoMiniaturizable");
96 ANoCloseButton = WMCreatePLString("NoCloseButton");
97 ANoBorder = WMCreatePLString("NoBorder");
98 ANoHideOthers = WMCreatePLString("NoHideOthers");
99 ANoMouseBindings = WMCreatePLString("NoMouseBindings");
100 ANoKeyBindings = WMCreatePLString("NoKeyBindings");
101 ANoAppIcon = WMCreatePLString("NoAppIcon");
102 AKeepOnTop = WMCreatePLString("KeepOnTop");
103 AKeepOnBottom = WMCreatePLString("KeepOnBottom");
104 AOmnipresent = WMCreatePLString("Omnipresent");
105 ASkipWindowList = WMCreatePLString("SkipWindowList");
106 ASkipSwitchPanel = WMCreatePLString("SkipSwitchPanel");
107 AKeepInsideScreen = WMCreatePLString("KeepInsideScreen");
108 AUnfocusable = WMCreatePLString("Unfocusable");
109 AAlwaysUserIcon = WMCreatePLString("AlwaysUserIcon");
110 AStartMiniaturized = WMCreatePLString("StartMiniaturized");
111 AStartHidden = WMCreatePLString("StartHidden");
112 AStartMaximized = WMCreatePLString("StartMaximized");
113 ADontSaveSession = WMCreatePLString("DontSaveSession");
114 AEmulateAppIcon = WMCreatePLString("EmulateAppIcon");
115 AFocusAcrossWorkspace = WMCreatePLString("FocusAcrossWorkspace");
116 AFullMaximize = WMCreatePLString("FullMaximize");
117 ASharedAppIcon = WMCreatePLString("SharedAppIcon");
118 #ifdef XKB_BUTTON_HINT
119 ANoLanguageButton = WMCreatePLString("NoLanguageButton");
120 #endif
122 AStartWorkspace = WMCreatePLString("StartWorkspace");
124 AnyWindow = WMCreatePLString("*");
125 No = WMCreatePLString("No");
128 static WMPropList *get_value(WMPropList * dict_win, WMPropList * dict_class, WMPropList * dict_name,
129 WMPropList * dict_any, WMPropList * option, WMPropList * default_value,
130 Bool useGlobalDefault)
132 WMPropList *value;
134 if (dict_win) {
135 value = WMGetFromPLDictionary(dict_win, option);
136 if (value)
137 return value;
140 if (dict_name) {
141 value = WMGetFromPLDictionary(dict_name, option);
142 if (value)
143 return value;
146 if (dict_class) {
147 value = WMGetFromPLDictionary(dict_class, option);
148 if (value)
149 return value;
152 if (!useGlobalDefault)
153 return NULL;
155 if (dict_any) {
156 value = WMGetFromPLDictionary(dict_any, option);
157 if (value)
158 return value;
161 return default_value;
165 *----------------------------------------------------------------------
166 * wDefaultFillAttributes--
167 * Retrieves attributes for the specified instance/class and
168 * fills attr with it. Values that are actually defined are also
169 * set in mask. If useGlobalDefault is True, the default for
170 * all windows ("*") will be used for when no values are found
171 * for that instance/class.
173 *----------------------------------------------------------------------
175 void
176 wDefaultFillAttributes(WScreen * scr, char *instance, char *class,
177 WWindowAttributes * attr, WWindowAttributes * mask, Bool useGlobalDefault)
179 WMPropList *value, *key1, *key2, *key3, *dw, *dc, *dn, *da;
181 if (class && instance) {
182 char *buffer;
184 buffer = wmalloc(strlen(class) + strlen(instance) + 2);
185 sprintf(buffer, "%s.%s", instance, class);
186 key1 = WMCreatePLString(buffer);
187 wfree(buffer);
188 } else {
189 key1 = NULL;
192 if (instance)
193 key2 = WMCreatePLString(instance);
194 else
195 key2 = NULL;
197 if (class)
198 key3 = WMCreatePLString(class);
199 else
200 key3 = NULL;
202 if (!ANoTitlebar)
203 init_wdefaults(scr);
205 WMPLSetCaseSensitive(True);
207 if (WDWindowAttributes->dictionary) {
208 dw = key1 ? WMGetFromPLDictionary(WDWindowAttributes->dictionary, key1) : NULL;
209 dn = key2 ? WMGetFromPLDictionary(WDWindowAttributes->dictionary, key2) : NULL;
210 dc = key3 ? WMGetFromPLDictionary(WDWindowAttributes->dictionary, key3) : NULL;
211 if (useGlobalDefault)
212 da = WMGetFromPLDictionary(WDWindowAttributes->dictionary, AnyWindow);
213 else
214 da = NULL;
215 } else {
216 dw = NULL;
217 dn = NULL;
218 dc = NULL;
219 da = NULL;
221 if (key1)
222 WMReleasePropList(key1);
223 if (key2)
224 WMReleasePropList(key2);
225 if (key3)
226 WMReleasePropList(key3);
228 #define APPLY_VAL(value, flag, attrib) \
229 if (value) {attr->flag = getBool(attrib, value); \
230 if (mask) mask->flag = 1;}
232 /* get the data */
233 value = get_value(dw, dc, dn, da, ANoTitlebar, No, useGlobalDefault);
234 APPLY_VAL(value, no_titlebar, ANoTitlebar);
236 value = get_value(dw, dc, dn, da, ANoResizebar, No, useGlobalDefault);
237 APPLY_VAL(value, no_resizebar, ANoResizebar);
239 value = get_value(dw, dc, dn, da, ANoMiniaturizeButton, No, useGlobalDefault);
240 APPLY_VAL(value, no_miniaturize_button, ANoMiniaturizeButton);
242 value = get_value(dw, dc, dn, da, ANoMiniaturizable, No, useGlobalDefault);
243 APPLY_VAL(value, no_miniaturizable, ANoMiniaturizable);
245 value = get_value(dw, dc, dn, da, ANoCloseButton, No, useGlobalDefault);
246 APPLY_VAL(value, no_close_button, ANoCloseButton);
248 value = get_value(dw, dc, dn, da, ANoBorder, No, useGlobalDefault);
249 APPLY_VAL(value, no_border, ANoBorder);
251 value = get_value(dw, dc, dn, da, ANoHideOthers, No, useGlobalDefault);
252 APPLY_VAL(value, no_hide_others, ANoHideOthers);
254 value = get_value(dw, dc, dn, da, ANoMouseBindings, No, useGlobalDefault);
255 APPLY_VAL(value, no_bind_mouse, ANoMouseBindings);
257 value = get_value(dw, dc, dn, da, ANoKeyBindings, No, useGlobalDefault);
258 APPLY_VAL(value, no_bind_keys, ANoKeyBindings);
260 value = get_value(dw, dc, dn, da, ANoAppIcon, No, useGlobalDefault);
261 APPLY_VAL(value, no_appicon, ANoAppIcon);
263 value = get_value(dw, dc, dn, da, ASharedAppIcon, No, useGlobalDefault);
264 APPLY_VAL(value, shared_appicon, ASharedAppIcon);
266 value = get_value(dw, dc, dn, da, AKeepOnTop, No, useGlobalDefault);
267 APPLY_VAL(value, floating, AKeepOnTop);
269 value = get_value(dw, dc, dn, da, AKeepOnBottom, No, useGlobalDefault);
270 APPLY_VAL(value, sunken, AKeepOnBottom);
272 value = get_value(dw, dc, dn, da, AOmnipresent, No, useGlobalDefault);
273 APPLY_VAL(value, omnipresent, AOmnipresent);
275 value = get_value(dw, dc, dn, da, ASkipWindowList, No, useGlobalDefault);
276 APPLY_VAL(value, skip_window_list, ASkipWindowList);
278 value = get_value(dw, dc, dn, da, ASkipSwitchPanel, No, useGlobalDefault);
279 APPLY_VAL(value, skip_switchpanel, ASkipSwitchPanel);
281 value = get_value(dw, dc, dn, da, AKeepInsideScreen, No, useGlobalDefault);
282 APPLY_VAL(value, dont_move_off, AKeepInsideScreen);
284 value = get_value(dw, dc, dn, da, AUnfocusable, No, useGlobalDefault);
285 APPLY_VAL(value, no_focusable, AUnfocusable);
287 value = get_value(dw, dc, dn, da, AAlwaysUserIcon, No, useGlobalDefault);
288 APPLY_VAL(value, always_user_icon, AAlwaysUserIcon);
290 value = get_value(dw, dc, dn, da, AStartMiniaturized, No, useGlobalDefault);
291 APPLY_VAL(value, start_miniaturized, AStartMiniaturized);
293 value = get_value(dw, dc, dn, da, AStartHidden, No, useGlobalDefault);
294 APPLY_VAL(value, start_hidden, AStartHidden);
296 value = get_value(dw, dc, dn, da, AStartMaximized, No, useGlobalDefault);
297 APPLY_VAL(value, start_maximized, AStartMaximized);
299 value = get_value(dw, dc, dn, da, ADontSaveSession, No, useGlobalDefault);
300 APPLY_VAL(value, dont_save_session, ADontSaveSession);
302 value = get_value(dw, dc, dn, da, AEmulateAppIcon, No, useGlobalDefault);
303 APPLY_VAL(value, emulate_appicon, AEmulateAppIcon);
305 value = get_value(dw, dc, dn, da, AFocusAcrossWorkspace, No, useGlobalDefault);
306 APPLY_VAL(value, focus_across_wksp, AFocusAcrossWorkspace);
308 value = get_value(dw, dc, dn, da, AFullMaximize, No, useGlobalDefault);
309 APPLY_VAL(value, full_maximize, AFullMaximize);
311 #ifdef XKB_BUTTON_HINT
312 value = get_value(dw, dc, dn, da, ANoLanguageButton, No, useGlobalDefault);
313 APPLY_VAL(value, no_language_button, ANoLanguageButton);
314 #endif
316 /* clean up */
317 WMPLSetCaseSensitive(False);
320 static WMPropList *get_generic_value(WScreen *scr, char *instance, char *class,
321 WMPropList *option, Bool noDefault)
323 WMPropList *value, *key, *dict;
325 value = NULL;
327 WMPLSetCaseSensitive(True);
329 /* Search the icon name using class and instance */
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);
345 /* Search the icon name using instance */
346 if (!value && instance) {
347 key = WMCreatePLString(instance);
349 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, key);
350 WMReleasePropList(key);
352 if (dict)
353 value = WMGetFromPLDictionary(dict, option);
356 /* Search the icon name using class */
357 if (!value && class) {
358 key = WMCreatePLString(class);
360 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, key);
361 WMReleasePropList(key);
363 if (dict)
364 value = WMGetFromPLDictionary(dict, option);
367 /* Search the default icon name - See noDefault argument! */
368 if (!value && !noDefault) {
369 /* AnyWindow is "*" - see wdefaults.c */
370 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, AnyWindow);
372 if (dict)
373 value = WMGetFromPLDictionary(dict, option);
376 WMPLSetCaseSensitive(False);
378 return value;
381 /* Get the name of the Icon File. If noDefault is False, then, default value included */
382 char *wDefaultGetIconFile(WScreen * scr, char *instance, char *class, Bool noDefault)
384 WMPropList *value;
385 char *tmp;
387 if (!ANoTitlebar)
388 init_wdefaults(scr);
390 if (!WDWindowAttributes->dictionary)
391 return NULL;
393 value = get_generic_value(scr, instance, class, AIcon, noDefault);
395 if (!value)
396 return NULL;
398 tmp = getString(AIcon, value);
400 return tmp;
403 RImage *wDefaultGetImage(WScreen * scr, char *winstance, char *wclass, int max_size)
405 char *file_name;
406 char *path;
407 RImage *image;
409 /* Get the file name of the image, using instance and class */
410 file_name = wDefaultGetIconFile(scr, winstance, wclass, False);
411 if (!file_name)
412 return NULL;
414 /* Search the file image in the icon paths */
415 path = FindImage(wPreferences.icon_path, file_name);
417 if (!path) {
418 wwarning(_("could not find icon file \"%s\""), file_name);
419 return NULL;
422 image = RLoadImage(scr->rcontext, path, 0);
423 if (!image)
424 wwarning(_("error loading image file \"%s\": %s"), path, RMessageForError(RErrorCode));
426 wfree(path);
428 image = wIconValidateIconSize(scr, image, max_size);
430 return image;
433 int wDefaultGetStartWorkspace(WScreen * scr, char *instance, char *class)
435 WMPropList *value;
436 int w;
437 char *tmp;
439 if (!ANoTitlebar)
440 init_wdefaults(scr);
442 if (!WDWindowAttributes->dictionary)
443 return -1;
445 value = get_generic_value(scr, instance, class, AStartWorkspace, False);
447 if (!value)
448 return -1;
450 tmp = getString(AStartWorkspace, value);
452 if (!tmp || strlen(tmp) == 0)
453 return -1;
455 /* Get the workspace number for the workspace name */
456 w = wGetWorkspaceNumber(scr, tmp);
458 wfree(value);
460 return w;
463 void wDefaultChangeIcon(WScreen * scr, char *instance, char *class, char *file)
465 WDDomain *db = WDWindowAttributes;
466 WMPropList *icon_value = NULL, *value, *attr, *key, *def_win, *def_icon = NULL;
467 WMPropList *dict = db->dictionary;
468 int same = 0;
470 if (!dict) {
471 dict = WMCreatePLDictionary(NULL, NULL);
472 if (dict)
473 db->dictionary = dict;
474 else
475 return;
478 WMPLSetCaseSensitive(True);
480 if (instance && class) {
481 char *buffer;
482 buffer = wmalloc(strlen(instance) + strlen(class) + 2);
483 sprintf(buffer, "%s.%s", instance, class);
484 key = WMCreatePLString(buffer);
485 wfree(buffer);
486 } else if (instance) {
487 key = WMCreatePLString(instance);
488 } else if (class) {
489 key = WMCreatePLString(class);
490 } else {
491 key = WMRetainPropList(AnyWindow);
494 if (file) {
495 value = WMCreatePLString(file);
496 icon_value = WMCreatePLDictionary(AIcon, value, NULL);
497 WMReleasePropList(value);
499 if ((def_win = WMGetFromPLDictionary(dict, AnyWindow)) != NULL)
500 def_icon = WMGetFromPLDictionary(def_win, AIcon);
502 if (def_icon && !strcmp(WMGetFromPLString(def_icon), file))
503 same = 1;
506 if ((attr = WMGetFromPLDictionary(dict, key)) != NULL) {
507 if (WMIsPLDictionary(attr)) {
508 if (icon_value != NULL && !same)
509 WMMergePLDictionaries(attr, icon_value, False);
510 else
511 WMRemoveFromPLDictionary(attr, AIcon);
513 } else if (icon_value != NULL && !same) {
514 WMPutInPLDictionary(dict, key, icon_value);
517 if (!wPreferences.flags.noupdates)
518 UpdateDomainFile(db);
520 WMReleasePropList(key);
521 if (icon_value)
522 WMReleasePropList(icon_value);
524 WMPLSetCaseSensitive(False);
527 /* --------------------------- Local ----------------------- */
529 static int getBool(WMPropList * key, WMPropList * value)
531 char *val;
533 if (!WMIsPLString(value)) {
534 wwarning(_("Wrong option format for key \"%s\". Should be %s."),
535 WMGetFromPLString(key), "Boolean");
536 return 0;
538 val = WMGetFromPLString(value);
540 if ((val[1] == '\0' && (val[0] == 'y' || val[0] == 'Y' || val[0] == 'T' || val[0] == 't' || val[0] == '1'))
541 || (strcasecmp(val, "YES") == 0 || strcasecmp(val, "TRUE") == 0)) {
543 return 1;
544 } else if ((val[1] == '\0'
545 && (val[0] == 'n' || val[0] == 'N' || val[0] == 'F' || val[0] == 'f' || val[0] == '0'))
546 || (strcasecmp(val, "NO") == 0 || strcasecmp(val, "FALSE") == 0)) {
548 return 0;
549 } else {
550 wwarning(_("can't convert \"%s\" to boolean"), val);
551 /* We return False if we can't convert to BOOLEAN.
552 * This is because all options defaults to False.
553 * -1 is not checked and thus is interpreted as True,
554 * which is not good.*/
555 return 0;
559 /* WARNING: Do not free the value returned by this function!! */
560 static char *getString(WMPropList * key, WMPropList * value)
562 if (!WMIsPLString(value)) {
563 wwarning(_("Wrong option format for key \"%s\". Should be %s."), WMGetFromPLString(key), "String");
564 return NULL;
567 return WMGetFromPLString(value);