Initial revision
[wmaker-crm.git] / src / wdefaults.c
blob6460fb4affe39080ca2e5a2de6670bf97bef3052
1 /* wdefaults.c - window specific defaults
2 *
3 * WindowMaker 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 AOmnipresent;
75 static proplist_t ASkipWindowList;
76 static proplist_t AKeepInsideScreen;
77 static proplist_t AUnfocusable;
78 static proplist_t AAlwaysUserIcon;
79 static proplist_t AStartMiniaturized;
80 static proplist_t AStartHidden; /* app */
81 static proplist_t ADontSaveSession; /* app */
82 static proplist_t AEmulateAppIcon;
84 static proplist_t AStartWorkspace;
86 static proplist_t AIcon;
89 static proplist_t AnyWindow;
90 static proplist_t No;
93 static void
94 init_wdefaults(WScreen *scr)
96 AIcon = PLMakeString("Icon");
98 ANoTitlebar = PLMakeString("NoTitlebar");
99 ANoResizebar = PLMakeString("NoResizebar");
100 ANoMiniaturizeButton = PLMakeString("NoMiniaturizeButton");
101 ANoCloseButton = PLMakeString("NoCloseButton");
102 ANoHideOthers = PLMakeString("NoHideOthers");
103 ANoMouseBindings = PLMakeString("NoMouseBindings");
104 ANoKeyBindings = PLMakeString("NoKeyBindings");
105 ANoAppIcon = PLMakeString("NoAppIcon");
106 AKeepOnTop = PLMakeString("KeepOnTop");
107 AOmnipresent = PLMakeString("Omnipresent");
108 ASkipWindowList = PLMakeString("SkipWindowList");
109 AKeepInsideScreen = PLMakeString("KeepInsideScreen");
110 AUnfocusable = PLMakeString("Unfocusable");
111 AAlwaysUserIcon = PLMakeString("AlwaysUserIcon");
112 AStartMiniaturized = PLMakeString("StartMiniaturized");
113 AStartHidden = PLMakeString("StartHidden");
114 ADontSaveSession = PLMakeString("DontSaveSession");
115 AEmulateAppIcon = PLMakeString("EmulateAppIcon");
117 AStartWorkspace = PLMakeString("StartWorkspace");
119 AnyWindow = PLMakeString("*");
120 No = PLMakeString("No");
122 if (!scr->wattribs) {
123 scr->wattribs = PLGetDomain(wAttributeDomainName);
129 static proplist_t
130 get_value(proplist_t dict_win, proplist_t dict_class, proplist_t dict_name,
131 proplist_t dict_any, proplist_t option, proplist_t default_value,
132 Bool useGlobalDefault)
134 proplist_t value;
137 if (dict_win) {
138 value = PLGetDictionaryEntry(dict_win, option);
139 if (value)
140 return value;
143 if (dict_name) {
144 value = PLGetDictionaryEntry(dict_name, option);
145 if (value)
146 return value;
149 if (dict_class) {
150 value = PLGetDictionaryEntry(dict_class, option);
151 if (value)
152 return value;
155 if (!useGlobalDefault)
156 return NULL;
158 if (dict_any) {
159 value = PLGetDictionaryEntry(dict_any, option);
160 if (value)
161 return value;
164 return default_value;
168 void
169 wDefaultFillAttributes(WScreen *scr, char *instance, char *class,
170 WWindowAttributes *attr, Bool useGlobalDefault)
172 proplist_t value;
173 proplist_t key1, key2, key3;
174 proplist_t dw, dc, dn, da;
175 char buffer[256];
178 if (class && instance)
179 key1 = PLMakeString(strcat(strcat(strcpy(buffer,instance),"."),class));
180 else
181 key1 = NULL;
183 if (instance)
184 key2 = PLMakeString(instance);
185 else
186 key2 = NULL;
188 if (class)
189 key3 = PLMakeString(class);
190 else
191 key3 = NULL;
193 if (!ANoTitlebar) {
194 init_wdefaults(scr);
197 PLSetStringCmpHook(NULL);
199 if (WDWindowAttributes->dictionary) {
200 dw = key1 ? PLGetDictionaryEntry(WDWindowAttributes->dictionary, key1) : NULL;
201 dn = key2 ? PLGetDictionaryEntry(WDWindowAttributes->dictionary, key2) : NULL;
202 dc = key3 ? PLGetDictionaryEntry(WDWindowAttributes->dictionary, key3) : NULL;
203 if (useGlobalDefault)
204 da = PLGetDictionaryEntry(WDWindowAttributes->dictionary, AnyWindow);
205 else
206 da = NULL;
207 } else {
208 dw = NULL;
209 dn = NULL;
210 dc = NULL;
211 da = NULL;
213 if (key1)
214 PLRelease(key1);
215 if (key2)
216 PLRelease(key2);
217 if (key3)
218 PLRelease(key3);
220 /* get the data */
221 value = get_value(dw, dc, dn, da, ANoTitlebar, No, useGlobalDefault);
222 if (value)
223 attr->no_titlebar = getBool(ANoTitlebar, value);
225 value = get_value(dw, dc, dn, da, ANoResizebar, No, useGlobalDefault);
226 if (value)
227 attr->no_resizebar = getBool(ANoResizebar, value);
229 value = get_value(dw, dc, dn, da, ANoMiniaturizeButton, No, useGlobalDefault);
230 if (value)
231 attr->no_miniaturize_button = getBool(ANoMiniaturizeButton, value);
233 value = get_value(dw, dc, dn, da, ANoCloseButton, No, useGlobalDefault);
234 if (value)
235 attr->no_close_button = getBool(ANoCloseButton, value);
237 value = get_value(dw, dc, dn, da, ANoHideOthers, No, useGlobalDefault);
238 if (value)
239 attr->no_hide_others = getBool(ANoHideOthers, value);
241 value = get_value(dw, dc, dn, da, ANoMouseBindings, No, useGlobalDefault);
242 if (value)
243 attr->no_bind_mouse = getBool(ANoMouseBindings, value);
245 value = get_value(dw, dc, dn, da, ANoKeyBindings, No, useGlobalDefault);
246 if (value)
247 attr->no_bind_keys = getBool(ANoKeyBindings, value);
249 value = get_value(dw, dc, dn, da, ANoAppIcon, No, useGlobalDefault);
250 if (value)
251 attr->no_appicon = getBool(ANoAppIcon, value);
253 value = get_value(dw, dc, dn, da, AKeepOnTop, No, useGlobalDefault);
254 if (value)
255 attr->floating = getBool(AKeepOnTop, value);
257 value = get_value(dw, dc, dn, da, AOmnipresent, No, useGlobalDefault);
258 if (value)
259 attr->omnipresent = getBool(AOmnipresent, value);
261 value = get_value(dw, dc, dn, da, ASkipWindowList, No, useGlobalDefault);
262 if (value)
263 attr->skip_window_list = getBool(ASkipWindowList, value);
265 value = get_value(dw, dc, dn, da, AKeepInsideScreen, No, useGlobalDefault);
266 if (value)
267 attr->dont_move_off = getBool(AKeepInsideScreen, value);
269 value = get_value(dw, dc, dn, da, AUnfocusable, No, useGlobalDefault);
270 if (value)
271 attr->no_focusable = getBool(AUnfocusable, value);
273 value = get_value(dw, dc, dn, da, AAlwaysUserIcon, No, useGlobalDefault);
274 if (value)
275 attr->always_user_icon = getBool(AAlwaysUserIcon, value);
277 value = get_value(dw, dc, dn, da, AStartMiniaturized, No, useGlobalDefault);
278 if (value)
279 attr->start_miniaturized = getBool(AStartMiniaturized, value);
281 value = get_value(dw, dc, dn, da, AStartHidden, No, useGlobalDefault);
282 if (value)
283 attr->start_hidden = getBool(AStartHidden, value);
285 value = get_value(dw, dc, dn, da, ADontSaveSession, No, useGlobalDefault);
286 if (value)
287 attr->dont_save_session = getBool(ADontSaveSession, value);
289 value = get_value(dw, dc, dn, da, AEmulateAppIcon, No, useGlobalDefault);
290 if (value)
291 attr->emulate_appicon = getBool(AEmulateAppIcon, value);
293 /* clean up */
294 PLSetStringCmpHook(StringCompareHook);
299 proplist_t
300 get_generic_value(WScreen *scr, char *instance, char *class, proplist_t option,
301 Bool noDefault)
303 proplist_t value, key, dict;
304 char buffer[256];
306 value = NULL;
308 PLSetStringCmpHook(NULL);
310 if (class && instance) {
311 key = PLMakeString(strcat(strcat(strcpy(buffer,instance),"."),class));
313 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, key);
314 PLRelease(key);
316 if (dict) {
317 value = PLGetDictionaryEntry(dict, option);
321 if (!value && instance) {
322 key = PLMakeString(instance);
324 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, key);
325 PLRelease(key);
327 if (dict) {
328 value = PLGetDictionaryEntry(dict, option);
332 if (!value && class) {
333 key = PLMakeString(class);
335 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, key);
336 PLRelease(key);
338 if (dict) {
339 value = PLGetDictionaryEntry(dict, option);
343 if (!value && !noDefault) {
344 dict = PLGetDictionaryEntry(WDWindowAttributes->dictionary, AnyWindow);
346 if (dict) {
347 value = PLGetDictionaryEntry(dict, option);
351 PLSetStringCmpHook(StringCompareHook);
353 return value;
357 char*
358 wDefaultGetIconFile(WScreen *scr, char *instance, char *class,
359 Bool noDefault)
361 proplist_t value;
362 char *tmp;
364 if (!ANoTitlebar) {
365 init_wdefaults(scr);
368 if (!WDWindowAttributes->dictionary)
369 return NULL;
371 value = get_generic_value(scr, instance, class, AIcon, noDefault);
373 if (!value)
374 return NULL;
376 tmp = getString(AIcon, value);
378 return tmp;
382 RImage*
383 wDefaultGetImage(WScreen *scr, char *winstance, char *wclass)
385 char *file_name;
386 char *path;
387 RImage *image;
389 file_name = wDefaultGetIconFile(scr, winstance, wclass, False);
390 if (!file_name)
391 return NULL;
393 path = FindImage(wPreferences.icon_path, file_name);
395 if (!path) {
396 wwarning(_("could not find icon file \"%s\""), file_name);
397 return NULL;
400 image = RLoadImage(scr->rcontext, path, 0);
401 if (!image) {
402 wwarning(_("error loading image file \"%s\""), path, RErrorString);
404 free(path);
406 image = wIconValidateIconSize(scr, image);
408 return image;
413 wDefaultGetStartWorkspace(WScreen *scr, char *instance, char *class)
415 proplist_t value;
416 int w, i;
417 char *tmp;
419 if (!ANoTitlebar) {
420 init_wdefaults(scr);
423 if (!WDWindowAttributes->dictionary)
424 return -1;
426 value = get_generic_value(scr, instance, class, AStartWorkspace,
427 False);
429 if (!value)
430 return -1;
432 tmp = getString(AStartWorkspace, value);
434 if (!tmp || strlen(tmp)==0)
435 return -1;
437 if (sscanf(tmp, "%i", &w)!=1) {
438 w = -1;
439 for (i=0; i < scr->workspace_count; i++) {
440 if (strcmp(scr->workspaces[i]->name, tmp)==0) {
441 w = i;
442 break;
445 } else {
446 w--;
449 return w;
453 void
454 wDefaultChangeIcon(WScreen *scr, char *instance, char* class, char *file)
456 WDDomain *db = WDWindowAttributes;
457 proplist_t icon_value=NULL, value, attr, key, def_win, def_icon=NULL;
458 proplist_t dict = db->dictionary;
459 char *buffer;
460 int same = 0;
462 if (!dict) {
463 dict = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
464 if (dict) {
465 db->dictionary = dict;
466 value = PLMakeString(db->path);
467 PLSetFilename(dict, value);
468 PLRelease(value);
470 else
471 return;
474 PLSetStringCmpHook(NULL);
476 if (instance && class) {
477 buffer = wmalloc(strlen(instance) + strlen(class) + 2);
478 strcat(strcat(strcpy(buffer, instance), "."), class);
479 key = PLMakeString(buffer);
480 if (PLGetDictionaryEntry(dict, key)==NULL) {
481 PLRelease(key);
482 key = PLMakeString(instance);
483 if (PLGetDictionaryEntry(dict, key)==NULL) {
484 PLRelease(key);
485 key = PLMakeString(class);
486 if (PLGetDictionaryEntry(dict, key)==NULL) {
487 PLRelease(key);
488 key = PLMakeString(buffer);
492 free(buffer);
493 } else if (instance) {
494 key = PLMakeString(instance);
495 } else if (class) {
496 key = PLMakeString(class);
497 } else {
498 key = PLRetain(AnyWindow);
501 if (file) {
502 value = PLMakeString(file);
503 icon_value = PLMakeDictionaryFromEntries(AIcon, value, NULL);
504 PLRelease(value);
506 if ((def_win = PLGetDictionaryEntry(dict, AnyWindow)) != NULL) {
507 def_icon = PLGetDictionaryEntry(def_win, AIcon);
510 if (def_icon && !strcmp(PLGetString(def_icon), file))
511 same = 1;
514 if((attr = PLGetDictionaryEntry(dict, key)) != NULL) {
515 if (PLIsDictionary(attr)) {
516 if (icon_value!=NULL && !same)
517 PLMergeDictionaries(attr, icon_value);
518 else
519 PLRemoveDictionaryEntry(attr, AIcon);
522 else if (icon_value!=NULL && !same) {
523 PLInsertDictionaryEntry(dict, key, icon_value);
525 PLSave(dict, YES);
527 PLRelease(key);
528 if(icon_value)
529 PLRelease(icon_value);
531 PLSetStringCmpHook(StringCompareHook);
536 /* --------------------------- Local ----------------------- */
538 static int
539 getBool(proplist_t key, proplist_t value)
541 char *val;
543 if (!PLIsString(value)) {
544 wwarning(_("Wrong option format for key \"%s\". Should be %s."),
545 PLGetString(key), "Boolean");
546 return 0;
548 val = PLGetString(value);
550 if ((val[1]=='\0' && (val[0]=='y' || val[0]=='Y' || val[0]=='T'
551 || val[0]=='t' || val[0]=='1'))
552 || (strcasecmp(val, "YES")==0 || strcasecmp(val, "TRUE")==0)) {
554 return 1;
555 } else if ((val[1]=='\0'
556 && (val[0]=='n' || val[0]=='N' || val[0]=='F'
557 || val[0]=='f' || val[0]=='0'))
558 || (strcasecmp(val, "NO")==0 || strcasecmp(val, "FALSE")==0)) {
560 return 0;
561 } else {
562 wwarning(_("can't convert \"%s\" to boolean"), val);
563 /* We return False if we can't convert to BOOLEAN.
564 * This is because all options defaults to False.
565 * -1 is not checked and thus is interpreted as True,
566 * which is not good.*/
567 return 0;
574 * WARNING: Do not free value returned by this!!
576 static char*
577 getString(proplist_t key, proplist_t value)
579 if (!PLIsString(value)) {
580 wwarning(_("Wrong option format for key \"%s\". Should be %s."),
581 PLGetString(key), "String");
582 return NULL;
585 return PLGetString(value);