wmaker: Replaced local 'extern' definition of wPreferences by proper header usage
[wmaker-crm.git] / src / wdefaults.c
blob09cf2c52605c0c1dc63a0ec29502c02b6e68adc8
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 "appicon.h"
40 #include "screen.h"
41 #include "workspace.h"
42 #include "defaults.h"
43 #include "icon.h"
44 #include "misc.h"
46 #define APPLY_VAL(value, flag, attrib) \
47 if (value) {attr->flag = getBool(attrib, value); \
48 if (mask) mask->flag = 1;}
50 /* Global stuff */
51 extern WDDomain *WDWindowAttributes;
53 /* Local stuff */
55 /* type converters */
56 static int getBool(WMPropList *, WMPropList *);
57 static char *getString(WMPropList *, WMPropList *);
58 static WMPropList *ANoTitlebar = NULL;
59 static WMPropList *ANoResizebar;
60 static WMPropList *ANoMiniaturizeButton;
61 static WMPropList *ANoMiniaturizable;
62 static WMPropList *ANoCloseButton;
63 static WMPropList *ANoBorder;
64 static WMPropList *ANoHideOthers;
65 static WMPropList *ANoMouseBindings;
66 static WMPropList *ANoKeyBindings;
67 static WMPropList *ANoAppIcon; /* app */
68 static WMPropList *AKeepOnTop;
69 static WMPropList *AKeepOnBottom;
70 static WMPropList *AOmnipresent;
71 static WMPropList *ASkipWindowList;
72 static WMPropList *ASkipSwitchPanel;
73 static WMPropList *AKeepInsideScreen;
74 static WMPropList *AUnfocusable;
75 static WMPropList *AAlwaysUserIcon;
76 static WMPropList *AStartMiniaturized;
77 static WMPropList *AStartMaximized;
78 static WMPropList *AStartHidden; /* app */
79 static WMPropList *ADontSaveSession; /* app */
80 static WMPropList *AEmulateAppIcon;
81 static WMPropList *AFocusAcrossWorkspace;
82 static WMPropList *AFullMaximize;
83 static WMPropList *ASharedAppIcon; /* app */
84 #ifdef XKB_BUTTON_HINT
85 static WMPropList *ANoLanguageButton;
86 #endif
87 static WMPropList *AStartWorkspace;
88 static WMPropList *AIcon;
89 static WMPropList *AnyWindow;
90 static WMPropList *No;
92 static void init_wdefaults(void)
94 AIcon = WMCreatePLString("Icon");
96 ANoTitlebar = WMCreatePLString("NoTitlebar");
97 ANoResizebar = WMCreatePLString("NoResizebar");
98 ANoMiniaturizeButton = WMCreatePLString("NoMiniaturizeButton");
99 ANoMiniaturizable = WMCreatePLString("NoMiniaturizable");
100 ANoCloseButton = WMCreatePLString("NoCloseButton");
101 ANoBorder = WMCreatePLString("NoBorder");
102 ANoHideOthers = WMCreatePLString("NoHideOthers");
103 ANoMouseBindings = WMCreatePLString("NoMouseBindings");
104 ANoKeyBindings = WMCreatePLString("NoKeyBindings");
105 ANoAppIcon = WMCreatePLString("NoAppIcon");
106 AKeepOnTop = WMCreatePLString("KeepOnTop");
107 AKeepOnBottom = WMCreatePLString("KeepOnBottom");
108 AOmnipresent = WMCreatePLString("Omnipresent");
109 ASkipWindowList = WMCreatePLString("SkipWindowList");
110 ASkipSwitchPanel = WMCreatePLString("SkipSwitchPanel");
111 AKeepInsideScreen = WMCreatePLString("KeepInsideScreen");
112 AUnfocusable = WMCreatePLString("Unfocusable");
113 AAlwaysUserIcon = WMCreatePLString("AlwaysUserIcon");
114 AStartMiniaturized = WMCreatePLString("StartMiniaturized");
115 AStartHidden = WMCreatePLString("StartHidden");
116 AStartMaximized = WMCreatePLString("StartMaximized");
117 ADontSaveSession = WMCreatePLString("DontSaveSession");
118 AEmulateAppIcon = WMCreatePLString("EmulateAppIcon");
119 AFocusAcrossWorkspace = WMCreatePLString("FocusAcrossWorkspace");
120 AFullMaximize = WMCreatePLString("FullMaximize");
121 ASharedAppIcon = WMCreatePLString("SharedAppIcon");
122 #ifdef XKB_BUTTON_HINT
123 ANoLanguageButton = WMCreatePLString("NoLanguageButton");
124 #endif
126 AStartWorkspace = WMCreatePLString("StartWorkspace");
128 AnyWindow = WMCreatePLString("*");
129 No = WMCreatePLString("No");
132 /* Returns the correct WMPropList, using instance+class or instance, or class, or default */
133 static WMPropList *get_value(WMPropList * dict_win, WMPropList * dict_class, WMPropList * dict_name,
134 WMPropList * dict_any, WMPropList * option, WMPropList * default_value,
135 Bool useGlobalDefault)
137 WMPropList *value;
139 if (dict_win) {
140 value = WMGetFromPLDictionary(dict_win, option);
141 if (value)
142 return value;
145 if (dict_name) {
146 value = WMGetFromPLDictionary(dict_name, option);
147 if (value)
148 return value;
151 if (dict_class) {
152 value = WMGetFromPLDictionary(dict_class, option);
153 if (value)
154 return value;
157 if (!useGlobalDefault)
158 return NULL;
160 if (dict_any) {
161 value = WMGetFromPLDictionary(dict_any, option);
162 if (value)
163 return value;
166 return default_value;
169 static WMPropList *get_value_from_instanceclass(const char *value)
171 WMPropList *key, *val = NULL;
173 if (!value)
174 return NULL;
176 key = WMCreatePLString(value);
178 WMPLSetCaseSensitive(True);
180 if (WDWindowAttributes->dictionary)
181 val = key ? WMGetFromPLDictionary(WDWindowAttributes->dictionary, key) : NULL;
183 if (key)
184 WMReleasePropList(key);
186 WMPLSetCaseSensitive(False);
188 return val;
192 *----------------------------------------------------------------------
193 * wDefaultFillAttributes--
194 * Retrieves attributes for the specified instance/class and
195 * fills attr with it. Values that are actually defined are also
196 * set in mask. If useGlobalDefault is True, the default for
197 * all windows ("*") will be used for when no values are found
198 * for that instance/class.
200 *----------------------------------------------------------------------
202 void wDefaultFillAttributes(const char *instance, const char *class,
203 WWindowAttributes *attr, WWindowAttributes *mask,
204 Bool useGlobalDefault)
206 WMPropList *value, *dw, *dc, *dn, *da;
207 char *buffer;
209 dw = dc = dn = da = NULL;
211 if (!ANoTitlebar)
212 init_wdefaults();
214 if (class && instance) {
215 buffer = StrConcatDot(instance, class);
216 dw = get_value_from_instanceclass(buffer);
217 wfree(buffer);
220 dn = get_value_from_instanceclass(instance);
221 dc = get_value_from_instanceclass(class);
223 WMPLSetCaseSensitive(True);
225 if ((WDWindowAttributes->dictionary) && (useGlobalDefault))
226 da = WMGetFromPLDictionary(WDWindowAttributes->dictionary, AnyWindow);
228 /* get the data */
229 value = get_value(dw, dc, dn, da, ANoTitlebar, No, useGlobalDefault);
230 APPLY_VAL(value, no_titlebar, ANoTitlebar);
232 value = get_value(dw, dc, dn, da, ANoResizebar, No, useGlobalDefault);
233 APPLY_VAL(value, no_resizebar, ANoResizebar);
235 value = get_value(dw, dc, dn, da, ANoMiniaturizeButton, No, useGlobalDefault);
236 APPLY_VAL(value, no_miniaturize_button, ANoMiniaturizeButton);
238 value = get_value(dw, dc, dn, da, ANoMiniaturizable, No, useGlobalDefault);
239 APPLY_VAL(value, no_miniaturizable, ANoMiniaturizable);
241 value = get_value(dw, dc, dn, da, ANoCloseButton, No, useGlobalDefault);
242 APPLY_VAL(value, no_close_button, ANoCloseButton);
244 value = get_value(dw, dc, dn, da, ANoBorder, No, useGlobalDefault);
245 APPLY_VAL(value, no_border, ANoBorder);
247 value = get_value(dw, dc, dn, da, ANoHideOthers, No, useGlobalDefault);
248 APPLY_VAL(value, no_hide_others, ANoHideOthers);
250 value = get_value(dw, dc, dn, da, ANoMouseBindings, No, useGlobalDefault);
251 APPLY_VAL(value, no_bind_mouse, ANoMouseBindings);
253 value = get_value(dw, dc, dn, da, ANoKeyBindings, No, useGlobalDefault);
254 APPLY_VAL(value, no_bind_keys, ANoKeyBindings);
256 value = get_value(dw, dc, dn, da, ANoAppIcon, No, useGlobalDefault);
257 APPLY_VAL(value, no_appicon, ANoAppIcon);
259 value = get_value(dw, dc, dn, da, ASharedAppIcon, No, useGlobalDefault);
260 APPLY_VAL(value, shared_appicon, ASharedAppIcon);
262 value = get_value(dw, dc, dn, da, AKeepOnTop, No, useGlobalDefault);
263 APPLY_VAL(value, floating, AKeepOnTop);
265 value = get_value(dw, dc, dn, da, AKeepOnBottom, No, useGlobalDefault);
266 APPLY_VAL(value, sunken, AKeepOnBottom);
268 value = get_value(dw, dc, dn, da, AOmnipresent, No, useGlobalDefault);
269 APPLY_VAL(value, omnipresent, AOmnipresent);
271 value = get_value(dw, dc, dn, da, ASkipWindowList, No, useGlobalDefault);
272 APPLY_VAL(value, skip_window_list, ASkipWindowList);
274 value = get_value(dw, dc, dn, da, ASkipSwitchPanel, No, useGlobalDefault);
275 APPLY_VAL(value, skip_switchpanel, ASkipSwitchPanel);
277 value = get_value(dw, dc, dn, da, AKeepInsideScreen, No, useGlobalDefault);
278 APPLY_VAL(value, dont_move_off, AKeepInsideScreen);
280 value = get_value(dw, dc, dn, da, AUnfocusable, No, useGlobalDefault);
281 APPLY_VAL(value, no_focusable, AUnfocusable);
283 value = get_value(dw, dc, dn, da, AAlwaysUserIcon, No, useGlobalDefault);
284 APPLY_VAL(value, always_user_icon, AAlwaysUserIcon);
286 value = get_value(dw, dc, dn, da, AStartMiniaturized, No, useGlobalDefault);
287 APPLY_VAL(value, start_miniaturized, AStartMiniaturized);
289 value = get_value(dw, dc, dn, da, AStartHidden, No, useGlobalDefault);
290 APPLY_VAL(value, start_hidden, AStartHidden);
292 value = get_value(dw, dc, dn, da, AStartMaximized, No, useGlobalDefault);
293 APPLY_VAL(value, start_maximized, AStartMaximized);
295 value = get_value(dw, dc, dn, da, ADontSaveSession, No, useGlobalDefault);
296 APPLY_VAL(value, dont_save_session, ADontSaveSession);
298 value = get_value(dw, dc, dn, da, AEmulateAppIcon, No, useGlobalDefault);
299 APPLY_VAL(value, emulate_appicon, AEmulateAppIcon);
301 value = get_value(dw, dc, dn, da, AFocusAcrossWorkspace, No, useGlobalDefault);
302 APPLY_VAL(value, focus_across_wksp, AFocusAcrossWorkspace);
304 value = get_value(dw, dc, dn, da, AFullMaximize, No, useGlobalDefault);
305 APPLY_VAL(value, full_maximize, AFullMaximize);
307 #ifdef XKB_BUTTON_HINT
308 value = get_value(dw, dc, dn, da, ANoLanguageButton, No, useGlobalDefault);
309 APPLY_VAL(value, no_language_button, ANoLanguageButton);
310 #endif
312 /* clean up */
313 WMPLSetCaseSensitive(False);
316 static WMPropList *get_generic_value(const char *instance, const char *class,
317 WMPropList *option, Bool default_icon)
319 WMPropList *value, *key, *dict;
321 value = NULL;
323 WMPLSetCaseSensitive(True);
325 /* Search the icon name using class and instance */
326 if (class && instance) {
327 char *buffer;
329 buffer = wmalloc(strlen(class) + strlen(instance) + 2);
330 sprintf(buffer, "%s.%s", instance, class);
331 key = WMCreatePLString(buffer);
332 wfree(buffer);
334 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, key);
335 WMReleasePropList(key);
337 if (dict)
338 value = WMGetFromPLDictionary(dict, option);
341 /* Search the icon name using instance */
342 if (!value && instance) {
343 key = WMCreatePLString(instance);
345 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, key);
346 WMReleasePropList(key);
348 if (dict)
349 value = WMGetFromPLDictionary(dict, option);
352 /* Search the icon name using class */
353 if (!value && class) {
354 key = WMCreatePLString(class);
356 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, key);
357 WMReleasePropList(key);
359 if (dict)
360 value = WMGetFromPLDictionary(dict, option);
363 /* Search the default icon name - See default_icon argument! */
364 if (!value && default_icon) {
365 /* AnyWindow is "*" - see wdefaults.c */
366 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, AnyWindow);
368 if (dict)
369 value = WMGetFromPLDictionary(dict, option);
372 WMPLSetCaseSensitive(False);
374 return value;
377 /* Get the file name of the image, using instance and class */
378 char *get_icon_filename(WScreen *scr, const char *winstance, const char *wclass, const char *command,
379 Bool default_icon)
381 char *file_name = NULL;
382 char *file_path = NULL;
384 /* Get the file name of the image, using instance and class */
385 file_name = wDefaultGetIconFile(winstance, wclass, default_icon);
387 /* Check if the file really exists in the disk */
388 if (file_name)
389 file_path = FindImage(wPreferences.icon_path, file_name);
391 /* If the specific icon filename is not found, and command is specified,
392 * then include the .app icons and re-do the search. */
393 if ((!file_name || !file_path ) && scr && command) {
394 wApplicationExtractDirPackIcon(scr, command, winstance, wclass);
395 file_name = wDefaultGetIconFile(winstance, wclass, False);
398 /* Get the full path for the image file */
399 if (file_name) {
400 file_path = FindImage(wPreferences.icon_path, file_name);
402 if (!file_path)
403 wwarning(_("icon \"%s\" doesn't exist, check your config files"), file_name);
405 /* FIXME: Here, if file_path don't exists, then the icon is in the
406 * "icon database" (WDWindowAttributes->dictionary), but the icon
407 * is not en disk. Therefore, we should remove it from the icon
408 * database. Is possible to do that using wDefaultChangeIcon() */
410 /* Don't wfree(file_name) here, because is a pointer to the icon
411 * dictionary (WDWindowAttributes->dictionary) value. */
414 if (!file_path && default_icon)
415 file_path = get_default_image_path();
417 return file_path;
420 /* This function returns the image picture for the file_name file */
421 RImage *get_rimage_from_file(WScreen *scr, const char *file_name, int max_size)
423 RImage *image = NULL;
425 if (!file_name)
426 return NULL;
428 image = RLoadImage(scr->rcontext, file_name, 0);
429 if (!image)
430 wwarning(_("error loading image file \"%s\": %s"), file_name,
431 RMessageForError(RErrorCode));
433 image = wIconValidateIconSize(image, max_size);
435 return image;
438 /* This function returns the default icon's full path
439 * If the path for an icon is not found, returns NULL */
440 char *get_default_image_path(void)
442 char *path = NULL, *file = NULL;
444 /* Get the default icon */
445 file = wDefaultGetIconFile(NULL, NULL, True);
446 if (file)
447 path = FindImage(wPreferences.icon_path, file);
449 return path;
452 /* This function creates the RImage using the default icon */
453 RImage *get_default_image(WScreen *scr)
455 RImage *image = NULL;
456 char *path = NULL;
458 /* Get the filename full path */
459 path = get_default_image_path();
460 if (!path)
461 return NULL;
463 /* Get the default icon */
464 image = get_rimage_from_file(scr, path, wPreferences.icon_size);
465 if (!image)
466 wwarning(_("could not find default icon \"%s\""), path);
468 /* Resize the icon to the wPreferences.icon_size size
469 * usually this function will return early, because size is right */
470 image = wIconValidateIconSize(image, wPreferences.icon_size);
472 return image;
475 RImage *get_icon_image(WScreen *scr, const char *winstance, const char *wclass, int max_size)
477 char *file_name = NULL;
479 /* Get the file name of the image, using instance and class */
480 file_name = get_icon_filename(scr, winstance, wclass, NULL, True);
482 return get_rimage_from_file(scr, file_name, max_size);
485 int wDefaultGetStartWorkspace(WScreen *scr, const char *instance, const char *class)
487 WMPropList *value;
488 int w;
489 char *tmp;
491 if (!ANoTitlebar)
492 init_wdefaults();
494 if (!WDWindowAttributes->dictionary)
495 return -1;
497 value = get_generic_value(instance, class, AStartWorkspace, True);
499 if (!value)
500 return -1;
502 tmp = getString(AStartWorkspace, value);
504 if (!tmp || strlen(tmp) == 0)
505 return -1;
507 /* Get the workspace number for the workspace name */
508 w = wGetWorkspaceNumber(scr, tmp);
510 return w;
513 /* Get the name of the Icon File. If default_icon is True, then, default value included */
514 char *wDefaultGetIconFile(const char *instance, const char *class, Bool default_icon)
516 WMPropList *value;
517 char *tmp;
519 if (!ANoTitlebar)
520 init_wdefaults();
522 if (!WDWindowAttributes || !WDWindowAttributes->dictionary)
523 return NULL;
525 value = get_generic_value(instance, class, AIcon, default_icon);
527 if (!value)
528 return NULL;
530 tmp = getString(AIcon, value);
532 return tmp;
535 void wDefaultChangeIcon(WScreen *scr, const char *instance, const char *class, const char *file)
537 WDDomain *db = WDWindowAttributes;
538 WMPropList *icon_value = NULL, *value, *attr, *key, *def_win, *def_icon = NULL;
539 WMPropList *dict = db->dictionary;
540 int same = 0;
542 if (!dict) {
543 dict = WMCreatePLDictionary(NULL, NULL);
544 if (dict)
545 db->dictionary = dict;
546 else
547 return;
550 WMPLSetCaseSensitive(True);
552 if (instance && class) {
553 char *buffer;
555 buffer = StrConcatDot(instance, class);
556 key = WMCreatePLString(buffer);
557 wfree(buffer);
558 } else if (instance) {
559 key = WMCreatePLString(instance);
560 } else if (class) {
561 key = WMCreatePLString(class);
562 } else {
563 key = WMRetainPropList(AnyWindow);
566 if (file) {
567 value = WMCreatePLString(file);
568 icon_value = WMCreatePLDictionary(AIcon, value, NULL);
569 WMReleasePropList(value);
571 if ((def_win = WMGetFromPLDictionary(dict, AnyWindow)) != NULL)
572 def_icon = WMGetFromPLDictionary(def_win, AIcon);
574 if (def_icon && !strcmp(WMGetFromPLString(def_icon), file))
575 same = 1;
578 if ((attr = WMGetFromPLDictionary(dict, key)) != NULL) {
579 if (WMIsPLDictionary(attr)) {
580 if (icon_value != NULL && !same)
581 WMMergePLDictionaries(attr, icon_value, False);
582 else
583 WMRemoveFromPLDictionary(attr, AIcon);
585 } else if (icon_value != NULL && !same) {
586 WMPutInPLDictionary(dict, key, icon_value);
589 if (!wPreferences.flags.noupdates)
590 UpdateDomainFile(db);
592 WMReleasePropList(key);
593 if (icon_value)
594 WMReleasePropList(icon_value);
596 WMPLSetCaseSensitive(False);
599 void wDefaultPurgeInfo(WScreen *scr, const char *instance, const char *class)
601 WMPropList *value, *key, *dict;
602 char *buffer;
604 if (!AIcon) { /* Unnecessary precaution */
605 init_wdefaults();
608 WMPLSetCaseSensitive(True);
610 buffer = wmalloc(strlen(class) + strlen(instance) + 2);
611 sprintf(buffer, "%s.%s", instance, class);
612 key = WMCreatePLString(buffer);
614 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, key);
616 if (dict) {
617 value = WMGetFromPLDictionary(dict, AIcon);
618 if (value) {
619 WMRemoveFromPLDictionary(dict, AIcon);
621 WMRemoveFromPLDictionary(WDWindowAttributes->dictionary, key);
622 UpdateDomainFile(WDWindowAttributes);
625 wfree(buffer);
626 WMReleasePropList(key);
627 WMPLSetCaseSensitive(False);
630 /* --------------------------- Local ----------------------- */
632 static int getBool(WMPropList * key, WMPropList * value)
634 char *val;
636 if (!WMIsPLString(value)) {
637 wwarning(_("Wrong option format for key \"%s\". Should be %s."),
638 WMGetFromPLString(key), "Boolean");
639 return 0;
641 val = WMGetFromPLString(value);
643 if ((val[1] == '\0' && (val[0] == 'y' || val[0] == 'Y' || val[0] == 'T' || val[0] == 't' || val[0] == '1'))
644 || (strcasecmp(val, "YES") == 0 || strcasecmp(val, "TRUE") == 0)) {
646 return 1;
647 } else if ((val[1] == '\0'
648 && (val[0] == 'n' || val[0] == 'N' || val[0] == 'F' || val[0] == 'f' || val[0] == '0'))
649 || (strcasecmp(val, "NO") == 0 || strcasecmp(val, "FALSE") == 0)) {
651 return 0;
652 } else {
653 wwarning(_("can't convert \"%s\" to boolean"), val);
654 /* We return False if we can't convert to BOOLEAN.
655 * This is because all options defaults to False.
656 * -1 is not checked and thus is interpreted as True,
657 * which is not good.*/
658 return 0;
662 /* WARNING: Do not free the value returned by this function!! */
663 static char *getString(WMPropList * key, WMPropList * value)
665 if (!WMIsPLString(value)) {
666 wwarning(_("Wrong option format for key \"%s\". Should be %s."), WMGetFromPLString(key), "String");
667 return NULL;
670 return WMGetFromPLString(value);