Update for 0.51.2-pre2
[wmaker-crm.git] / src / wdefaults.c
blob80f5a49072e2620192eee8fe2ef6e2f0930ea490
1 /* wdefaults.c - window specific defaults
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 *
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>
38 #include "WindowMaker.h"
39 #include "window.h"
40 #include "screen.h"
41 #include "funcs.h"
42 #include "workspace.h"
43 #include "defaults.h"
44 #include "icon.h"
47 /* Global stuff */
49 extern WPreferences wPreferences;
51 extern proplist_t wAttributeDomainName;
53 extern WDDomain *WDWindowAttributes;
56 /* Local stuff */
59 /* type converters */
60 static int getBool(proplist_t, proplist_t);
62 static char* getString(proplist_t, proplist_t);
65 static proplist_t ANoTitlebar = NULL;
66 static proplist_t ANoResizebar;
67 static proplist_t ANoMiniaturizeButton;
68 static proplist_t ANoCloseButton;
69 static proplist_t ANoHideOthers;
70 static proplist_t ANoMouseBindings;
71 static proplist_t ANoKeyBindings;
72 static proplist_t ANoAppIcon; /* app */
73 static proplist_t AKeepOnTop;
74 static proplist_t AKeepOnBottom;
75 static proplist_t AOmnipresent;
76 static proplist_t ASkipWindowList;
77 static proplist_t AKeepInsideScreen;
78 static proplist_t AUnfocusable;
79 static proplist_t AAlwaysUserIcon;
80 static proplist_t AStartMiniaturized;
81 static proplist_t AStartMaximized;
82 static proplist_t AStartHidden; /* app */
83 static proplist_t ADontSaveSession; /* app */
84 static proplist_t AEmulateAppIcon;
85 static proplist_t AFullMaximize;
87 static proplist_t AStartWorkspace;
89 static proplist_t AIcon;
92 static proplist_t AnyWindow;
93 static proplist_t No;
96 static void
97 init_wdefaults(WScreen *scr)
99 AIcon = PLMakeString("Icon");
101 ANoTitlebar = PLMakeString("NoTitlebar");
102 ANoResizebar = PLMakeString("NoResizebar");
103 ANoMiniaturizeButton = PLMakeString("NoMiniaturizeButton");
104 ANoCloseButton = PLMakeString("NoCloseButton");
105 ANoHideOthers = PLMakeString("NoHideOthers");
106 ANoMouseBindings = PLMakeString("NoMouseBindings");
107 ANoKeyBindings = PLMakeString("NoKeyBindings");
108 ANoAppIcon = PLMakeString("NoAppIcon");
109 AKeepOnTop = PLMakeString("KeepOnTop");
110 AKeepOnBottom = PLMakeString("KeepOnBottom");
111 AOmnipresent = PLMakeString("Omnipresent");
112 ASkipWindowList = PLMakeString("SkipWindowList");
113 AKeepInsideScreen = PLMakeString("KeepInsideScreen");
114 AUnfocusable = PLMakeString("Unfocusable");
115 AAlwaysUserIcon = PLMakeString("AlwaysUserIcon");
116 AStartMiniaturized = PLMakeString("StartMiniaturized");
117 AStartHidden = PLMakeString("StartHidden");
118 AStartMaximized = PLMakeString("StartMaximized");
119 ADontSaveSession = PLMakeString("DontSaveSession");
120 AEmulateAppIcon = PLMakeString("EmulateAppIcon");
121 AFullMaximize = PLMakeString("FullMaximize");
123 AStartWorkspace = PLMakeString("StartWorkspace");
125 AnyWindow = PLMakeString("*");
126 No = PLMakeString("No");
128 if (!scr->wattribs) {
129 scr->wattribs = PLGetDomain(wAttributeDomainName);
135 static proplist_t
136 get_value(proplist_t dict_win, proplist_t dict_class, proplist_t dict_name,
137 proplist_t dict_any, proplist_t option, proplist_t default_value,
138 Bool useGlobalDefault)
140 proplist_t value;
143 if (dict_win) {
144 value = PLGetDictionaryEntry(dict_win, option);
145 if (value)
146 return value;
149 if (dict_name) {
150 value = PLGetDictionaryEntry(dict_name, option);
151 if (value)
152 return value;
155 if (dict_class) {
156 value = PLGetDictionaryEntry(dict_class, option);
157 if (value)
158 return value;
161 if (!useGlobalDefault)
162 return NULL;
164 if (dict_any) {
165 value = PLGetDictionaryEntry(dict_any, option);
166 if (value)
167 return value;
170 return default_value;
175 *----------------------------------------------------------------------
176 * wDefaultFillAttributes--
177 * Retrieves attributes for the specified instance/class and
178 * fills attr with it. Values that are actually defined are also
179 * set in mask. If useGlobalDefault is True, the default for
180 * all windows ("*") will be used for when no values are found
181 * for that instance/class.
183 *----------------------------------------------------------------------
185 void
186 wDefaultFillAttributes(WScreen *scr, char *instance, char *class,
187 WWindowAttributes *attr,
188 WWindowAttributes *mask,
189 Bool useGlobalDefault)
191 proplist_t value;
192 proplist_t key1, key2, key3;
193 proplist_t dw, dc, dn, da;
194 char buffer[256];
197 if (class && instance)
198 key1 = PLMakeString(strcat(strcat(strcpy(buffer,instance),"."),class));
199 else
200 key1 = NULL;
202 if (instance)
203 key2 = PLMakeString(instance);
204 else
205 key2 = NULL;
207 if (class)
208 key3 = PLMakeString(class);
209 else
210 key3 = NULL;
212 if (!ANoTitlebar) {
213 init_wdefaults(scr);
216 PLSetStringCmpHook(NULL);
218 if (WDWindowAttributes->dictionary) {
219 dw = key1 ? PLGetDictionaryEntry(WDWindowAttributes->dictionary, key1) : NULL;
220 dn = key2 ? PLGetDictionaryEntry(WDWindowAttributes->dictionary, key2) : NULL;
221 dc = key3 ? PLGetDictionaryEntry(WDWindowAttributes->dictionary, key3) : NULL;
222 if (useGlobalDefault)
223 da = PLGetDictionaryEntry(WDWindowAttributes->dictionary, AnyWindow);
224 else
225 da = NULL;
226 } else {
227 dw = NULL;
228 dn = NULL;
229 dc = NULL;
230 da = NULL;
232 if (key1)
233 PLRelease(key1);
234 if (key2)
235 PLRelease(key2);
236 if (key3)
237 PLRelease(key3);
239 #define APPLY_VAL(value, flag, attrib) \
240 if (value) {attr->flag = getBool(attrib, value); \
241 if (mask) mask->flag = 1;}
243 /* get the data */
244 value = get_value(dw, dc, dn, da, ANoTitlebar, No, useGlobalDefault);
245 APPLY_VAL(value, no_titlebar, ANoTitlebar);
247 value = get_value(dw, dc, dn, da, ANoResizebar, No, useGlobalDefault);
248 APPLY_VAL(value, no_resizebar, ANoResizebar);
250 value = get_value(dw, dc, dn, da, ANoMiniaturizeButton, No, useGlobalDefault);
251 APPLY_VAL(value, no_miniaturize_button, ANoMiniaturizeButton);
253 value = get_value(dw, dc, dn, da, ANoCloseButton, No, useGlobalDefault);
254 APPLY_VAL(value, no_close_button, ANoCloseButton);
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, AKeepOnTop, No, useGlobalDefault);
269 APPLY_VAL(value, floating, AKeepOnTop);
271 value = get_value(dw, dc, dn, da, AKeepOnBottom, No, useGlobalDefault);
272 APPLY_VAL(value, sunken, AKeepOnBottom);
274 value = get_value(dw, dc, dn, da, AOmnipresent, No, useGlobalDefault);
275 APPLY_VAL(value, omnipresent, AOmnipresent);
277 value = get_value(dw, dc, dn, da, ASkipWindowList, No, useGlobalDefault);
278 APPLY_VAL(value, skip_window_list, ASkipWindowList);
280 value = get_value(dw, dc, dn, da, AKeepInsideScreen, No, useGlobalDefault);
281 APPLY_VAL(value, dont_move_off, AKeepInsideScreen);
283 value = get_value(dw, dc, dn, da, AUnfocusable, No, useGlobalDefault);
284 APPLY_VAL(value, no_focusable, AUnfocusable);
286 value = get_value(dw, dc, dn, da, AAlwaysUserIcon, No, useGlobalDefault);
287 APPLY_VAL(value, always_user_icon, AAlwaysUserIcon);
289 value = get_value(dw, dc, dn, da, AStartMiniaturized, No, useGlobalDefault);
290 APPLY_VAL(value, start_miniaturized, AStartMiniaturized);
292 value = get_value(dw, dc, dn, da, AStartHidden, No, useGlobalDefault);
293 APPLY_VAL(value, start_hidden, AStartHidden);
295 value = get_value(dw, dc, dn, da, AStartMaximized, No, useGlobalDefault);
296 APPLY_VAL(value, start_maximized, AStartMaximized);
298 value = get_value(dw, dc, dn, da, ADontSaveSession, No, useGlobalDefault);
299 APPLY_VAL(value, dont_save_session, ADontSaveSession);
301 value = get_value(dw, dc, dn, da, AEmulateAppIcon, No, useGlobalDefault);
302 APPLY_VAL(value, emulate_appicon, AEmulateAppIcon);
304 value = get_value(dw, dc, dn, da, AFullMaximize, No, useGlobalDefault);
305 APPLY_VAL(value, emulate_appicon, AFullMaximize);
307 /* clean up */
308 PLSetStringCmpHook(StringCompareHook);
313 proplist_t
314 get_generic_value(WScreen *scr, char *instance, char *class, proplist_t option,
315 Bool noDefault)
317 proplist_t value, key, dict;
318 char buffer[256];
320 value = NULL;
322 PLSetStringCmpHook(NULL);
324 if (class && instance) {
325 key = PLMakeString(strcat(strcat(strcpy(buffer,instance),"."),class));
327 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, key);
328 PLRelease(key);
330 if (dict) {
331 value = PLGetDictionaryEntry(dict, option);
335 if (!value && instance) {
336 key = PLMakeString(instance);
338 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, key);
339 PLRelease(key);
341 if (dict) {
342 value = PLGetDictionaryEntry(dict, option);
346 if (!value && class) {
347 key = PLMakeString(class);
349 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, key);
350 PLRelease(key);
352 if (dict) {
353 value = PLGetDictionaryEntry(dict, option);
357 if (!value && !noDefault) {
358 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, AnyWindow);
360 if (dict) {
361 value = PLGetDictionaryEntry(dict, option);
365 PLSetStringCmpHook(StringCompareHook);
367 return value;
371 char*
372 wDefaultGetIconFile(WScreen *scr, char *instance, char *class,
373 Bool noDefault)
375 proplist_t value;
376 char *tmp;
378 if (!ANoTitlebar) {
379 init_wdefaults(scr);
382 if (!WDWindowAttributes->dictionary)
383 return NULL;
385 value = get_generic_value(scr, instance, class, AIcon, noDefault);
387 if (!value)
388 return NULL;
390 tmp = getString(AIcon, value);
392 return tmp;
396 RImage*
397 wDefaultGetImage(WScreen *scr, char *winstance, char *wclass)
399 char *file_name;
400 char *path;
401 RImage *image;
403 file_name = wDefaultGetIconFile(scr, winstance, wclass, False);
404 if (!file_name)
405 return NULL;
407 path = FindImage(wPreferences.icon_path, file_name);
409 if (!path) {
410 wwarning(_("could not find icon file \"%s\""), file_name);
411 return NULL;
414 image = RLoadImage(scr->rcontext, path, 0);
415 if (!image) {
416 wwarning(_("error loading image file \"%s\""), path, RMessageForError(RErrorCode));
418 free(path);
420 image = wIconValidateIconSize(scr, image);
422 return image;
427 wDefaultGetStartWorkspace(WScreen *scr, char *instance, char *class)
429 proplist_t 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,
441 False);
443 if (!value)
444 return -1;
446 tmp = getString(AStartWorkspace, value);
448 if (!tmp || strlen(tmp)==0)
449 return -1;
451 if (sscanf(tmp, "%i", &w)!=1) {
452 w = -1;
453 for (i=0; i < scr->workspace_count; i++) {
454 if (strcmp(scr->workspaces[i]->name, tmp)==0) {
455 w = i;
456 break;
459 } else {
460 w--;
463 return w;
467 void
468 wDefaultChangeIcon(WScreen *scr, char *instance, char* class, char *file)
470 WDDomain *db = WDWindowAttributes;
471 proplist_t icon_value=NULL, value, attr, key, def_win, def_icon=NULL;
472 proplist_t dict = db->dictionary;
473 char *buffer;
474 int same = 0;
476 if (!dict) {
477 dict = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
478 if (dict) {
479 db->dictionary = dict;
480 value = PLMakeString(db->path);
481 PLSetFilename(dict, value);
482 PLRelease(value);
484 else
485 return;
488 PLSetStringCmpHook(NULL);
490 if (instance && class) {
491 buffer = wmalloc(strlen(instance) + strlen(class) + 2);
492 strcat(strcat(strcpy(buffer, instance), "."), class);
493 key = PLMakeString(buffer);
494 free(buffer);
495 } else if (instance) {
496 key = PLMakeString(instance);
497 } else if (class) {
498 key = PLMakeString(class);
499 } else {
500 key = PLRetain(AnyWindow);
503 if (file) {
504 value = PLMakeString(file);
505 icon_value = PLMakeDictionaryFromEntries(AIcon, value, NULL);
506 PLRelease(value);
508 if ((def_win = PLGetDictionaryEntry(dict, AnyWindow)) != NULL) {
509 def_icon = PLGetDictionaryEntry(def_win, AIcon);
512 if (def_icon && !strcmp(PLGetString(def_icon), file))
513 same = 1;
516 if ((attr = PLGetDictionaryEntry(dict, key)) != NULL) {
517 if (PLIsDictionary(attr)) {
518 if (icon_value!=NULL && !same)
519 PLMergeDictionaries(attr, icon_value);
520 else
521 PLRemoveDictionaryEntry(attr, AIcon);
523 } else if (icon_value!=NULL && !same) {
524 PLInsertDictionaryEntry(dict, key, icon_value);
526 if (!wPreferences.flags.noupdates)
527 PLSave(dict, YES);
529 PLRelease(key);
530 if(icon_value)
531 PLRelease(icon_value);
533 PLSetStringCmpHook(StringCompareHook);
538 /* --------------------------- Local ----------------------- */
540 static int
541 getBool(proplist_t key, proplist_t value)
543 char *val;
545 if (!PLIsString(value)) {
546 wwarning(_("Wrong option format for key \"%s\". Should be %s."),
547 PLGetString(key), "Boolean");
548 return 0;
550 val = PLGetString(value);
552 if ((val[1]=='\0' && (val[0]=='y' || val[0]=='Y' || val[0]=='T'
553 || val[0]=='t' || val[0]=='1'))
554 || (strcasecmp(val, "YES")==0 || strcasecmp(val, "TRUE")==0)) {
556 return 1;
557 } else if ((val[1]=='\0'
558 && (val[0]=='n' || val[0]=='N' || val[0]=='F'
559 || val[0]=='f' || val[0]=='0'))
560 || (strcasecmp(val, "NO")==0 || strcasecmp(val, "FALSE")==0)) {
562 return 0;
563 } else {
564 wwarning(_("can't convert \"%s\" to boolean"), val);
565 /* We return False if we can't convert to BOOLEAN.
566 * This is because all options defaults to False.
567 * -1 is not checked and thus is interpreted as True,
568 * which is not good.*/
569 return 0;
576 * WARNING: Do not free value returned by this!!
578 static char*
579 getString(proplist_t key, proplist_t value)
581 if (!PLIsString(value)) {
582 wwarning(_("Wrong option format for key \"%s\". Should be %s."),
583 PLGetString(key), "String");
584 return NULL;
587 return PLGetString(value);