Change to the linux kernel coding style
[wmaker-crm.git] / src / wdefaults.c
1 /* wdefaults.c - window specific defaults
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997-2003 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.
11 *
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.
16 *
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.
21 */
22
23 #include "wconfig.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <ctype.h>
30
31 #include <X11/Xlib.h>
32 #include <X11/Xutil.h>
33 #include <X11/keysym.h>
34
35 #include <wraster.h>
36
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"
44
45 /* Global stuff */
46
47 extern WPreferences wPreferences;
48
49 extern WMPropList *wAttributeDomainName;
50
51 extern WDDomain *WDWindowAttributes;
52
53 /* Local stuff */
54
55 /* type converters */
56 static int getBool(WMPropList *, WMPropList *);
57
58 static char *getString(WMPropList *, WMPropList *);
59
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
86
87 static WMPropList *AStartWorkspace;
88
89 static WMPropList *AIcon;
90
91 static WMPropList *AnyWindow;
92 static WMPropList *No;
93
94 static void init_wdefaults(WScreen * scr)
95 {
96 AIcon = WMCreatePLString("Icon");
97
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
124
125 AStartWorkspace = WMCreatePLString("StartWorkspace");
126
127 AnyWindow = WMCreatePLString("*");
128 No = WMCreatePLString("No");
129 /*
130 if (!scr->wattribs) {
131 scr->wattribs = PLGetDomain(wAttributeDomainName);
132 } */
133 }
134
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)
138 {
139 WMPropList *value;
140
141 if (dict_win) {
142 value = WMGetFromPLDictionary(dict_win, option);
143 if (value)
144 return value;
145 }
146
147 if (dict_name) {
148 value = WMGetFromPLDictionary(dict_name, option);
149 if (value)
150 return value;
151 }
152
153 if (dict_class) {
154 value = WMGetFromPLDictionary(dict_class, option);
155 if (value)
156 return value;
157 }
158
159 if (!useGlobalDefault)
160 return NULL;
161
162 if (dict_any) {
163 value = WMGetFromPLDictionary(dict_any, option);
164 if (value)
165 return value;
166 }
167
168 return default_value;
169 }
170
171 /*
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.
179 *
180 *----------------------------------------------------------------------
181 */
182 void
183 wDefaultFillAttributes(WScreen * scr, char *instance, char *class,
184 WWindowAttributes * attr, WWindowAttributes * mask, Bool useGlobalDefault)
185 {
186 WMPropList *value, *key1, *key2, *key3, *dw, *dc, *dn, *da;
187
188 if (class && instance) {
189 char *buffer;
190
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;
197 }
198
199 if (instance)
200 key2 = WMCreatePLString(instance);
201 else
202 key2 = NULL;
203
204 if (class)
205 key3 = WMCreatePLString(class);
206 else
207 key3 = NULL;
208
209 if (!ANoTitlebar) {
210 init_wdefaults(scr);
211 }
212
213 WMPLSetCaseSensitive(True);
214
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;
228 }
229 if (key1)
230 WMReleasePropList(key1);
231 if (key2)
232 WMReleasePropList(key2);
233 if (key3)
234 WMReleasePropList(key3);
235
236 #define APPLY_VAL(value, flag, attrib) \
237 if (value) {attr->flag = getBool(attrib, value); \
238 if (mask) mask->flag = 1;}
239
240 /* get the data */
241 value = get_value(dw, dc, dn, da, ANoTitlebar, No, useGlobalDefault);
242 APPLY_VAL(value, no_titlebar, ANoTitlebar);
243
244 value = get_value(dw, dc, dn, da, ANoResizebar, No, useGlobalDefault);
245 APPLY_VAL(value, no_resizebar, ANoResizebar);
246
247 value = get_value(dw, dc, dn, da, ANoMiniaturizeButton, No, useGlobalDefault);
248 APPLY_VAL(value, no_miniaturize_button, ANoMiniaturizeButton);
249
250 value = get_value(dw, dc, dn, da, ANoCloseButton, No, useGlobalDefault);
251 APPLY_VAL(value, no_close_button, ANoCloseButton);
252
253 value = get_value(dw, dc, dn, da, ANoBorder, No, useGlobalDefault);
254 APPLY_VAL(value, no_border, ANoBorder);
255
256 value = get_value(dw, dc, dn, da, ANoHideOthers, No, useGlobalDefault);
257 APPLY_VAL(value, no_hide_others, ANoHideOthers);
258
259 value = get_value(dw, dc, dn, da, ANoMouseBindings, No, useGlobalDefault);
260 APPLY_VAL(value, no_bind_mouse, ANoMouseBindings);
261
262 value = get_value(dw, dc, dn, da, ANoKeyBindings, No, useGlobalDefault);
263 APPLY_VAL(value, no_bind_keys, ANoKeyBindings);
264
265 value = get_value(dw, dc, dn, da, ANoAppIcon, No, useGlobalDefault);
266 APPLY_VAL(value, no_appicon, ANoAppIcon);
267
268 value = get_value(dw, dc, dn, da, ASharedAppIcon, No, useGlobalDefault);
269 APPLY_VAL(value, shared_appicon, ASharedAppIcon);
270
271 value = get_value(dw, dc, dn, da, AKeepOnTop, No, useGlobalDefault);
272 APPLY_VAL(value, floating, AKeepOnTop);
273
274 value = get_value(dw, dc, dn, da, AKeepOnBottom, No, useGlobalDefault);
275 APPLY_VAL(value, sunken, AKeepOnBottom);
276
277 value = get_value(dw, dc, dn, da, AOmnipresent, No, useGlobalDefault);
278 APPLY_VAL(value, omnipresent, AOmnipresent);
279
280 value = get_value(dw, dc, dn, da, ASkipWindowList, No, useGlobalDefault);
281 APPLY_VAL(value, skip_window_list, ASkipWindowList);
282
283 value = get_value(dw, dc, dn, da, AKeepInsideScreen, No, useGlobalDefault);
284 APPLY_VAL(value, dont_move_off, AKeepInsideScreen);
285
286 value = get_value(dw, dc, dn, da, AUnfocusable, No, useGlobalDefault);
287 APPLY_VAL(value, no_focusable, AUnfocusable);
288
289 value = get_value(dw, dc, dn, da, AAlwaysUserIcon, No, useGlobalDefault);
290 APPLY_VAL(value, always_user_icon, AAlwaysUserIcon);
291
292 value = get_value(dw, dc, dn, da, AStartMiniaturized, No, useGlobalDefault);
293 APPLY_VAL(value, start_miniaturized, AStartMiniaturized);
294
295 value = get_value(dw, dc, dn, da, AStartHidden, No, useGlobalDefault);
296 APPLY_VAL(value, start_hidden, AStartHidden);
297
298 value = get_value(dw, dc, dn, da, AStartMaximized, No, useGlobalDefault);
299 APPLY_VAL(value, start_maximized, AStartMaximized);
300
301 value = get_value(dw, dc, dn, da, ADontSaveSession, No, useGlobalDefault);
302 APPLY_VAL(value, dont_save_session, ADontSaveSession);
303
304 value = get_value(dw, dc, dn, da, AEmulateAppIcon, No, useGlobalDefault);
305 APPLY_VAL(value, emulate_appicon, AEmulateAppIcon);
306
307 value = get_value(dw, dc, dn, da, AFullMaximize, No, useGlobalDefault);
308 APPLY_VAL(value, full_maximize, AFullMaximize);
309
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
314
315 /* clean up */
316 WMPLSetCaseSensitive(False);
317 }
318
319 WMPropList *get_generic_value(WScreen * scr, char *instance, char *class, WMPropList * option, Bool noDefault)
320 {
321 WMPropList *value, *key, *dict;
322
323 value = NULL;
324
325 WMPLSetCaseSensitive(True);
326
327 if (class && instance) {
328 char *buffer;
329
330 buffer = wmalloc(strlen(class) + strlen(instance) + 2);
331 sprintf(buffer, "%s.%s", instance, class);
332 key = WMCreatePLString(buffer);
333 wfree(buffer);
334
335 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, key);
336 WMReleasePropList(key);
337
338 if (dict) {
339 value = WMGetFromPLDictionary(dict, option);
340 }
341 }
342
343 if (!value && instance) {
344 key = WMCreatePLString(instance);
345
346 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, key);
347 WMReleasePropList(key);
348 if (dict) {
349 value = WMGetFromPLDictionary(dict, option);
350 }
351 }
352
353 if (!value && class) {
354 key = WMCreatePLString(class);
355
356 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, key);
357 WMReleasePropList(key);
358
359 if (dict) {
360 value = WMGetFromPLDictionary(dict, option);
361 }
362 }
363
364 if (!value && !noDefault) {
365 dict = WMGetFromPLDictionary(WDWindowAttributes->dictionary, AnyWindow);
366
367 if (dict) {
368 value = WMGetFromPLDictionary(dict, option);
369 }
370 }
371
372 WMPLSetCaseSensitive(False);
373
374 return value;
375 }
376
377 char *wDefaultGetIconFile(WScreen * scr, char *instance, char *class, Bool noDefault)
378 {
379 WMPropList *value;
380 char *tmp;
381
382 if (!ANoTitlebar) {
383 init_wdefaults(scr);
384 }
385
386 if (!WDWindowAttributes->dictionary)
387 return NULL;
388
389 value = get_generic_value(scr, instance, class, AIcon, noDefault);
390
391 if (!value)
392 return NULL;
393
394 tmp = getString(AIcon, value);
395
396 return tmp;
397 }
398
399 RImage *wDefaultGetImage(WScreen * scr, char *winstance, char *wclass)
400 {
401 char *file_name;
402 char *path;
403 RImage *image;
404
405 file_name = wDefaultGetIconFile(scr, winstance, wclass, False);
406 if (!file_name)
407 return NULL;
408
409 path = FindImage(wPreferences.icon_path, file_name);
410
411 if (!path) {
412 wwarning(_("could not find icon file \"%s\""), file_name);
413 return NULL;
414 }
415
416 image = RLoadImage(scr->rcontext, path, 0);
417 if (!image) {
418 wwarning(_("error loading image file \"%s\""), path, RMessageForError(RErrorCode));
419 }
420 wfree(path);
421
422 image = wIconValidateIconSize(scr, image);
423
424 return image;
425 }
426
427 int wDefaultGetStartWorkspace(WScreen * scr, char *instance, char *class)
428 {
429 WMPropList *value;
430 int w, i;
431 char *tmp;
432
433 if (!ANoTitlebar) {
434 init_wdefaults(scr);
435 }
436
437 if (!WDWindowAttributes->dictionary)
438 return -1;
439
440 value = get_generic_value(scr, instance, class, AStartWorkspace, False);
441
442 if (!value)
443 return -1;
444
445 tmp = getString(AStartWorkspace, value);
446
447 if (!tmp || strlen(tmp) == 0)
448 return -1;
449
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;
456 }
457 }
458 } else {
459 w--;
460 }
461
462 return w;
463 }
464
465 void wDefaultChangeIcon(WScreen * scr, char *instance, char *class, char *file)
466 {
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;
471
472 if (!dict) {
473 dict = WMCreatePLDictionary(NULL, NULL);
474 if (dict) {
475 db->dictionary = dict;
476 } else {
477 return;
478 }
479 }
480
481 WMPLSetCaseSensitive(True);
482
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);
495 }
496
497 if (file) {
498 value = WMCreatePLString(file);
499 icon_value = WMCreatePLDictionary(AIcon, value, NULL);
500 WMReleasePropList(value);
501
502 if ((def_win = WMGetFromPLDictionary(dict, AnyWindow)) != NULL) {
503 def_icon = WMGetFromPLDictionary(def_win, AIcon);
504 }
505
506 if (def_icon && !strcmp(WMGetFromPLString(def_icon), file))
507 same = 1;
508 }
509
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);
516 }
517 } else if (icon_value != NULL && !same) {
518 WMPutInPLDictionary(dict, key, icon_value);
519 }
520 if (!wPreferences.flags.noupdates) {
521 UpdateDomainFile(db);
522 }
523
524 WMReleasePropList(key);
525 if (icon_value)
526 WMReleasePropList(icon_value);
527
528 WMPLSetCaseSensitive(False);
529 }
530
531 /* --------------------------- Local ----------------------- */
532
533 static int getBool(WMPropList * key, WMPropList * value)
534 {
535 char *val;
536
537 if (!WMIsPLString(value)) {
538 wwarning(_("Wrong option format for key \"%s\". Should be %s."),
539 WMGetFromPLString(key), "Boolean");
540 return 0;
541 }
542 val = WMGetFromPLString(value);
543
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)) {
546
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)) {
551
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;
560 }
561 }
562
563 /* WARNING: Do not free the value returned by this function!! */
564 static char *getString(WMPropList * key, WMPropList * value)
565 {
566 if (!WMIsPLString(value)) {
567 wwarning(_("Wrong option format for key \"%s\". Should be %s."), WMGetFromPLString(key), "String");
568 return NULL;
569 }
570
571 return WMGetFromPLString(value);
572 }