Change to the linux kernel coding style
[wmaker-crm.git] / src / wdefaults.c
blobd82bb49a5751d9579bc89cdea81d2b332e4099c6
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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.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 */
47 extern WPreferences wPreferences;
49 extern WMPropList *wAttributeDomainName;
51 extern WDDomain *WDWindowAttributes;
53 /* Local stuff */
55 /* type converters */
56 static int getBool(WMPropList *, WMPropList *);
58 static char *getString(WMPropList *, WMPropList *);
60 static WMPropList *ANoTitlebar = NULL;
61 static WMPropList *ANoResizebar;
62 static WMPropList *ANoMiniaturizeButton;
63 static WMPropList *ANoCloseButton;
64 static WMPropList *ANoBorder;
65 static WMPropList *ANoHideOthers;
66 static WMPropList *ANoMouseBindings;
67 static WMPropList *ANoKeyBindings;
68 static WMPropList *ANoAppIcon; /* app */
69 static WMPropList *AKeepOnTop;
70 static WMPropList *AKeepOnBottom;
71 static WMPropList *AOmnipresent;
72 static WMPropList *ASkipWindowList;
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 *AFullMaximize;
82 static WMPropList *ASharedAppIcon; /* app */
83 #ifdef XKB_BUTTON_HINT
84 static WMPropList *ANoLanguageButton;
85 #endif
87 static WMPropList *AStartWorkspace;
89 static WMPropList *AIcon;
91 static WMPropList *AnyWindow;
92 static WMPropList *No;
94 static void init_wdefaults(WScreen * scr)
96 AIcon = WMCreatePLString("Icon");
98 ANoTitlebar = WMCreatePLString("NoTitlebar");
99 ANoResizebar = WMCreatePLString("NoResizebar");
100 ANoMiniaturizeButton = WMCreatePLString("NoMiniaturizeButton");
101 ANoCloseButton = WMCreatePLString("NoCloseButton");
102 ANoBorder = WMCreatePLString("NoBorder");
103 ANoHideOthers = WMCreatePLString("NoHideOthers");
104 ANoMouseBindings = WMCreatePLString("NoMouseBindings");
105 ANoKeyBindings = WMCreatePLString("NoKeyBindings");
106 ANoAppIcon = WMCreatePLString("NoAppIcon");
107 AKeepOnTop = WMCreatePLString("KeepOnTop");
108 AKeepOnBottom = WMCreatePLString("KeepOnBottom");
109 AOmnipresent = WMCreatePLString("Omnipresent");
110 ASkipWindowList = WMCreatePLString("SkipWindowList");
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 AFullMaximize = WMCreatePLString("FullMaximize");
120 ASharedAppIcon = WMCreatePLString("SharedAppIcon");
121 #ifdef XKB_BUTTON_HINT
122 ANoLanguageButton = WMCreatePLString("NoLanguageButton");
123 #endif
125 AStartWorkspace = WMCreatePLString("StartWorkspace");
127 AnyWindow = WMCreatePLString("*");
128 No = WMCreatePLString("No");
130 if (!scr->wattribs) {
131 scr->wattribs = PLGetDomain(wAttributeDomainName);
132 } */
135 static WMPropList *get_value(WMPropList * dict_win, WMPropList * dict_class, WMPropList * dict_name,
136 WMPropList * dict_any, WMPropList * option, WMPropList * default_value,
137 Bool useGlobalDefault)
139 WMPropList *value;
141 if (dict_win) {
142 value = WMGetFromPLDictionary(dict_win, option);
143 if (value)
144 return value;
147 if (dict_name) {
148 value = WMGetFromPLDictionary(dict_name, option);
149 if (value)
150 return value;
153 if (dict_class) {
154 value = WMGetFromPLDictionary(dict_class, option);
155 if (value)
156 return value;
159 if (!useGlobalDefault)
160 return NULL;
162 if (dict_any) {
163 value = WMGetFromPLDictionary(dict_any, option);
164 if (value)
165 return value;
168 return default_value;
172 *----------------------------------------------------------------------
173 * wDefaultFillAttributes--
174 * Retrieves attributes for the specified instance/class and
175 * fills attr with it. Values that are actually defined are also
176 * set in mask. If useGlobalDefault is True, the default for
177 * all windows ("*") will be used for when no values are found
178 * for that instance/class.
180 *----------------------------------------------------------------------
182 void
183 wDefaultFillAttributes(WScreen * scr, char *instance, char *class,
184 WWindowAttributes * attr, WWindowAttributes * mask, Bool useGlobalDefault)
186 WMPropList *value, *key1, *key2, *key3, *dw, *dc, *dn, *da;
188 if (class && instance) {
189 char *buffer;
191 buffer = wmalloc(strlen(class) + strlen(instance) + 2);
192 sprintf(buffer, "%s.%s", instance, class);
193 key1 = WMCreatePLString(buffer);
194 wfree(buffer);
195 } else {
196 key1 = NULL;
199 if (instance)
200 key2 = WMCreatePLString(instance);
201 else
202 key2 = NULL;
204 if (class)
205 key3 = WMCreatePLString(class);
206 else
207 key3 = NULL;
209 if (!ANoTitlebar) {
210 init_wdefaults(scr);
213 WMPLSetCaseSensitive(True);
215 if (WDWindowAttributes->dictionary) {
216 dw = key1 ? WMGetFromPLDictionary(WDWindowAttributes->dictionary, key1) : NULL;
217 dn = key2 ? WMGetFromPLDictionary(WDWindowAttributes->dictionary, key2) : NULL;
218 dc = key3 ? WMGetFromPLDictionary(WDWindowAttributes->dictionary, key3) : NULL;
219 if (useGlobalDefault)
220 da = WMGetFromPLDictionary(WDWindowAttributes->dictionary, AnyWindow);
221 else
222 da = NULL;
223 } else {
224 dw = NULL;
225 dn = NULL;
226 dc = NULL;
227 da = NULL;
229 if (key1)
230 WMReleasePropList(key1);
231 if (key2)
232 WMReleasePropList(key2);
233 if (key3)
234 WMReleasePropList(key3);
236 #define APPLY_VAL(value, flag, attrib) \
237 if (value) {attr->flag = getBool(attrib, value); \
238 if (mask) mask->flag = 1;}
240 /* get the data */
241 value = get_value(dw, dc, dn, da, ANoTitlebar, No, useGlobalDefault);
242 APPLY_VAL(value, no_titlebar, ANoTitlebar);
244 value = get_value(dw, dc, dn, da, ANoResizebar, No, useGlobalDefault);
245 APPLY_VAL(value, no_resizebar, ANoResizebar);
247 value = get_value(dw, dc, dn, da, ANoMiniaturizeButton, No, useGlobalDefault);
248 APPLY_VAL(value, no_miniaturize_button, ANoMiniaturizeButton);
250 value = get_value(dw, dc, dn, da, ANoCloseButton, No, useGlobalDefault);
251 APPLY_VAL(value, no_close_button, ANoCloseButton);
253 value = get_value(dw, dc, dn, da, ANoBorder, No, useGlobalDefault);
254 APPLY_VAL(value, no_border, ANoBorder);
256 value = get_value(dw, dc, dn, da, ANoHideOthers, No, useGlobalDefault);
257 APPLY_VAL(value, no_hide_others, ANoHideOthers);
259 value = get_value(dw, dc, dn, da, ANoMouseBindings, No, useGlobalDefault);
260 APPLY_VAL(value, no_bind_mouse, ANoMouseBindings);
262 value = get_value(dw, dc, dn, da, ANoKeyBindings, No, useGlobalDefault);
263 APPLY_VAL(value, no_bind_keys, ANoKeyBindings);
265 value = get_value(dw, dc, dn, da, ANoAppIcon, No, useGlobalDefault);
266 APPLY_VAL(value, no_appicon, ANoAppIcon);
268 value = get_value(dw, dc, dn, da, ASharedAppIcon, No, useGlobalDefault);
269 APPLY_VAL(value, shared_appicon, ASharedAppIcon);
271 value = get_value(dw, dc, dn, da, AKeepOnTop, No, useGlobalDefault);
272 APPLY_VAL(value, floating, AKeepOnTop);
274 value = get_value(dw, dc, dn, da, AKeepOnBottom, No, useGlobalDefault);
275 APPLY_VAL(value, sunken, AKeepOnBottom);
277 value = get_value(dw, dc, dn, da, AOmnipresent, No, useGlobalDefault);
278 APPLY_VAL(value, omnipresent, AOmnipresent);
280 value = get_value(dw, dc, dn, da, ASkipWindowList, No, useGlobalDefault);
281 APPLY_VAL(value, skip_window_list, ASkipWindowList);
283 value = get_value(dw, dc, dn, da, AKeepInsideScreen, No, useGlobalDefault);
284 APPLY_VAL(value, dont_move_off, AKeepInsideScreen);
286 value = get_value(dw, dc, dn, da, AUnfocusable, No, useGlobalDefault);
287 APPLY_VAL(value, no_focusable, AUnfocusable);
289 value = get_value(dw, dc, dn, da, AAlwaysUserIcon, No, useGlobalDefault);
290 APPLY_VAL(value, always_user_icon, AAlwaysUserIcon);
292 value = get_value(dw, dc, dn, da, AStartMiniaturized, No, useGlobalDefault);
293 APPLY_VAL(value, start_miniaturized, AStartMiniaturized);
295 value = get_value(dw, dc, dn, da, AStartHidden, No, useGlobalDefault);
296 APPLY_VAL(value, start_hidden, AStartHidden);
298 value = get_value(dw, dc, dn, da, AStartMaximized, No, useGlobalDefault);
299 APPLY_VAL(value, start_maximized, AStartMaximized);
301 value = get_value(dw, dc, dn, da, ADontSaveSession, No, useGlobalDefault);
302 APPLY_VAL(value, dont_save_session, ADontSaveSession);
304 value = get_value(dw, dc, dn, da, AEmulateAppIcon, No, useGlobalDefault);
305 APPLY_VAL(value, emulate_appicon, AEmulateAppIcon);
307 value = get_value(dw, dc, dn, da, AFullMaximize, No, useGlobalDefault);
308 APPLY_VAL(value, full_maximize, AFullMaximize);
310 #ifdef XKB_BUTTON_HINT
311 value = get_value(dw, dc, dn, da, ANoLanguageButton, No, useGlobalDefault);
312 APPLY_VAL(value, no_language_button, ANoLanguageButton);
313 #endif
315 /* clean up */
316 WMPLSetCaseSensitive(False);
319 WMPropList *get_generic_value(WScreen * scr, char *instance, char *class, WMPropList * option, Bool noDefault)
321 WMPropList *value, *key, *dict;
323 value = NULL;
325 WMPLSetCaseSensitive(True);
327 if (class && instance) {
328 char *buffer;
330 buffer = wmalloc(strlen(class) + strlen(instance) + 2);
331 sprintf(buffer, "%s.%s", instance, class);
332 key = WMCreatePLString(buffer);
333 wfree(buffer);
335 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, key);
336 WMReleasePropList(key);
338 if (dict) {
339 value = WMGetFromPLDictionary(dict, option);
343 if (!value && instance) {
344 key = WMCreatePLString(instance);
346 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, key);
347 WMReleasePropList(key);
348 if (dict) {
349 value = WMGetFromPLDictionary(dict, option);
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);
364 if (!value && !noDefault) {
365 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, AnyWindow);
367 if (dict) {
368 value = WMGetFromPLDictionary(dict, option);
372 WMPLSetCaseSensitive(False);
374 return value;
377 char *wDefaultGetIconFile(WScreen * scr, char *instance, char *class, Bool noDefault)
379 WMPropList *value;
380 char *tmp;
382 if (!ANoTitlebar) {
383 init_wdefaults(scr);
386 if (!WDWindowAttributes->dictionary)
387 return NULL;
389 value = get_generic_value(scr, instance, class, AIcon, noDefault);
391 if (!value)
392 return NULL;
394 tmp = getString(AIcon, value);
396 return tmp;
399 RImage *wDefaultGetImage(WScreen * scr, char *winstance, char *wclass)
401 char *file_name;
402 char *path;
403 RImage *image;
405 file_name = wDefaultGetIconFile(scr, winstance, wclass, False);
406 if (!file_name)
407 return NULL;
409 path = FindImage(wPreferences.icon_path, file_name);
411 if (!path) {
412 wwarning(_("could not find icon file \"%s\""), file_name);
413 return NULL;
416 image = RLoadImage(scr->rcontext, path, 0);
417 if (!image) {
418 wwarning(_("error loading image file \"%s\""), path, RMessageForError(RErrorCode));
420 wfree(path);
422 image = wIconValidateIconSize(scr, image);
424 return image;
427 int wDefaultGetStartWorkspace(WScreen * scr, char *instance, char *class)
429 WMPropList *value;
430 int w, i;
431 char *tmp;
433 if (!ANoTitlebar) {
434 init_wdefaults(scr);
437 if (!WDWindowAttributes->dictionary)
438 return -1;
440 value = get_generic_value(scr, instance, class, AStartWorkspace, False);
442 if (!value)
443 return -1;
445 tmp = getString(AStartWorkspace, value);
447 if (!tmp || strlen(tmp) == 0)
448 return -1;
450 if (sscanf(tmp, "%i", &w) != 1) {
451 w = -1;
452 for (i = 0; i < scr->workspace_count; i++) {
453 if (strcmp(scr->workspaces[i]->name, tmp) == 0) {
454 w = i;
455 break;
458 } else {
459 w--;
462 return w;
465 void wDefaultChangeIcon(WScreen * scr, char *instance, char *class, char *file)
467 WDDomain *db = WDWindowAttributes;
468 WMPropList *icon_value = NULL, *value, *attr, *key, *def_win, *def_icon = NULL;
469 WMPropList *dict = db->dictionary;
470 int same = 0;
472 if (!dict) {
473 dict = WMCreatePLDictionary(NULL, NULL);
474 if (dict) {
475 db->dictionary = dict;
476 } else {
477 return;
481 WMPLSetCaseSensitive(True);
483 if (instance && class) {
484 char *buffer;
485 buffer = wmalloc(strlen(instance) + strlen(class) + 2);
486 sprintf(buffer, "%s.%s", instance, class);
487 key = WMCreatePLString(buffer);
488 wfree(buffer);
489 } else if (instance) {
490 key = WMCreatePLString(instance);
491 } else if (class) {
492 key = WMCreatePLString(class);
493 } else {
494 key = WMRetainPropList(AnyWindow);
497 if (file) {
498 value = WMCreatePLString(file);
499 icon_value = WMCreatePLDictionary(AIcon, value, NULL);
500 WMReleasePropList(value);
502 if ((def_win = WMGetFromPLDictionary(dict, AnyWindow)) != NULL) {
503 def_icon = WMGetFromPLDictionary(def_win, AIcon);
506 if (def_icon && !strcmp(WMGetFromPLString(def_icon), file))
507 same = 1;
510 if ((attr = WMGetFromPLDictionary(dict, key)) != NULL) {
511 if (WMIsPLDictionary(attr)) {
512 if (icon_value != NULL && !same)
513 WMMergePLDictionaries(attr, icon_value, False);
514 else
515 WMRemoveFromPLDictionary(attr, AIcon);
517 } else if (icon_value != NULL && !same) {
518 WMPutInPLDictionary(dict, key, icon_value);
520 if (!wPreferences.flags.noupdates) {
521 UpdateDomainFile(db);
524 WMReleasePropList(key);
525 if (icon_value)
526 WMReleasePropList(icon_value);
528 WMPLSetCaseSensitive(False);
531 /* --------------------------- Local ----------------------- */
533 static int getBool(WMPropList * key, WMPropList * value)
535 char *val;
537 if (!WMIsPLString(value)) {
538 wwarning(_("Wrong option format for key \"%s\". Should be %s."),
539 WMGetFromPLString(key), "Boolean");
540 return 0;
542 val = WMGetFromPLString(value);
544 if ((val[1] == '\0' && (val[0] == 'y' || val[0] == 'Y' || val[0] == 'T' || val[0] == 't' || val[0] == '1'))
545 || (strcasecmp(val, "YES") == 0 || strcasecmp(val, "TRUE") == 0)) {
547 return 1;
548 } else if ((val[1] == '\0'
549 && (val[0] == 'n' || val[0] == 'N' || val[0] == 'F' || val[0] == 'f' || val[0] == '0'))
550 || (strcasecmp(val, "NO") == 0 || strcasecmp(val, "FALSE") == 0)) {
552 return 0;
553 } else {
554 wwarning(_("can't convert \"%s\" to boolean"), val);
555 /* We return False if we can't convert to BOOLEAN.
556 * This is because all options defaults to False.
557 * -1 is not checked and thus is interpreted as True,
558 * which is not good.*/
559 return 0;
563 /* WARNING: Do not free the value returned by this function!! */
564 static char *getString(WMPropList * key, WMPropList * value)
566 if (!WMIsPLString(value)) {
567 wwarning(_("Wrong option format for key \"%s\". Should be %s."), WMGetFromPLString(key), "String");
568 return NULL;
571 return WMGetFromPLString(value);