1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/* croma-window-manager.c
* Copyright (C) 2002 Seth Nickell
* Copyright (C) 2002 Red Hat, Inc.
*
* Written by: Seth Nickell <snickell@stanford.edu>,
* Havoc Pennington <hp@redhat.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include <config.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#include "croma-window-manager.h"
#define CROMA_SCHEMA "org.cafe.Croma.general"
#define CROMA_THEME_KEY "theme"
#define CROMA_FONT_KEY "titlebar-font"
#define CROMA_FOCUS_KEY "focus-mode"
#define CROMA_USE_SYSTEM_FONT_KEY "titlebar-uses-system-font"
#define CROMA_AUTORAISE_KEY "auto-raise"
#define CROMA_AUTORAISE_DELAY_KEY "auto-raise-delay"
#define CROMA_MOUSE_MODIFIER_KEY "mouse-button-modifier"
#define CROMA_DOUBLE_CLICK_TITLEBAR_KEY "action-double-click-titlebar"
#define CROMA_COMPOSITING_MANAGER_KEY "compositing-manager"
#define CROMA_COMPOSITING_FAST_ALT_TAB_KEY "compositing-fast-alt-tab"
/* keep following enums in sync with croma */
enum
{
ACTION_TITLEBAR_TOGGLE_SHADE,
ACTION_TITLEBAR_TOGGLE_MAXIMIZE,
ACTION_TITLEBAR_TOGGLE_MAXIMIZE_HORIZONTALLY,
ACTION_TITLEBAR_TOGGLE_MAXIMIZE_VERTICALLY,
ACTION_TITLEBAR_MINIMIZE,
ACTION_TITLEBAR_NONE,
ACTION_TITLEBAR_LOWER,
ACTION_TITLEBAR_MENU
};
enum
{
FOCUS_MODE_CLICK,
FOCUS_MODE_SLOPPY,
FOCUS_MODE_MOUSE
};
static CafeWindowManagerClass *parent_class;
struct _CromaWindowManagerPrivate {
GSettings *settings;
char *font;
char *theme;
char *mouse_modifier;
};
static void
value_changed (GSettings *settings,
gchar *key,
void *data)
{
CromaWindowManager *meta_wm;
meta_wm = CROMA_WINDOW_MANAGER (data);
cafe_window_manager_settings_changed (CAFE_WINDOW_MANAGER (meta_wm));
}
/* this function is called when the shared lib is loaded */
GObject *
window_manager_new (int expected_interface_version)
{
GObject *wm;
if (expected_interface_version != CAFE_WINDOW_MANAGER_INTERFACE_VERSION) {
g_warning ("Croma window manager module wasn't compiled with the current version of cafe-control-center");
return NULL;
}
wm = g_object_new (croma_window_manager_get_type (), NULL);
return wm;
}
static GList *
add_themes_from_dir (GList *current_list, const char *path)
{
DIR *theme_dir;
struct dirent *entry;
char *theme_file_path;<--- The scope of the variable 'theme_file_path' can be reduced. [+]The scope of the variable 'theme_file_path' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
GList *node;<--- Variable 'node' can be declared as pointer to const
gboolean found = FALSE;
if (!(g_file_test (path, G_FILE_TEST_EXISTS) && g_file_test (path, G_FILE_TEST_IS_DIR))) {
return current_list;
}
theme_dir = opendir (path);
/* If this is NULL, then we couldn't open ~/.themes. The test above
* only checks existence, not wether we can really read it.*/
if (theme_dir == NULL)
return current_list;
for (entry = readdir (theme_dir); entry != NULL; entry = readdir (theme_dir)) {
theme_file_path = g_build_filename (path, entry->d_name, "metacity-1/metacity-theme-2.xml", NULL);
if (g_file_test (theme_file_path, G_FILE_TEST_EXISTS)) {
for (node = current_list; (node != NULL) && (!found); node = node->next) {
found = (strcmp (node->data, entry->d_name) == 0);
}
if (!found) {
current_list = g_list_prepend (current_list, g_strdup (entry->d_name));
}
}
else {
g_free (theme_file_path);
theme_file_path = g_build_filename (path, entry->d_name, "metacity-1/metacity-theme-1.xml", NULL);
if (g_file_test (theme_file_path, G_FILE_TEST_EXISTS)) {
for (node = current_list; (node != NULL) && (!found); node = node->next) {
found = (strcmp (node->data, entry->d_name) == 0);
}
if (!found) {
current_list = g_list_prepend (current_list, g_strdup (entry->d_name));
}
}
}
found = FALSE;
/*g_free (entry);*/
g_free (theme_file_path);
}
closedir (theme_dir);
return current_list;
}
static GList *
croma_get_theme_list (CafeWindowManager *wm)
{
GList *themes = NULL;
char *home_dir_themes;
home_dir_themes = g_build_filename (g_get_home_dir (), ".themes", NULL);
themes = add_themes_from_dir (themes, CROMA_THEME_DIR);
themes = add_themes_from_dir (themes, "/usr/share/themes");
themes = add_themes_from_dir (themes, home_dir_themes);
g_free (home_dir_themes);
return themes;
}
static char *
croma_get_user_theme_folder (CafeWindowManager *wm)
{
return g_build_filename (g_get_home_dir (), ".themes", NULL);
}
static void
croma_change_settings (CafeWindowManager *wm,
const CafeWMSettings *settings)
{
CromaWindowManager *meta_wm;
meta_wm = CROMA_WINDOW_MANAGER (wm);
if (settings->flags & CAFE_WM_SETTING_COMPOSITING_MANAGER)
g_settings_set_boolean (meta_wm->p->settings,
CROMA_COMPOSITING_MANAGER_KEY,
settings->compositing_manager);
if (settings->flags & CAFE_WM_SETTING_COMPOSITING_ALTTAB)
g_settings_set_boolean (meta_wm->p->settings,
CROMA_COMPOSITING_FAST_ALT_TAB_KEY,
settings->compositing_fast_alt_tab);
if (settings->flags & CAFE_WM_SETTING_MOUSE_FOCUS)
g_settings_set_enum (meta_wm->p->settings,
CROMA_FOCUS_KEY,
settings->focus_follows_mouse ?
FOCUS_MODE_SLOPPY : FOCUS_MODE_CLICK);
if (settings->flags & CAFE_WM_SETTING_AUTORAISE)
g_settings_set_boolean (meta_wm->p->settings,
CROMA_AUTORAISE_KEY,
settings->autoraise);
if (settings->flags & CAFE_WM_SETTING_AUTORAISE_DELAY)
g_settings_set_int (meta_wm->p->settings,
CROMA_AUTORAISE_DELAY_KEY,
settings->autoraise_delay);
if (settings->flags & CAFE_WM_SETTING_FONT) {
g_settings_set_string (meta_wm->p->settings,
CROMA_FONT_KEY,
settings->font);
}
if (settings->flags & CAFE_WM_SETTING_MOUSE_MOVE_MODIFIER) {
char *value;
value = g_strdup_printf ("<%s>", settings->mouse_move_modifier);
g_settings_set_string (meta_wm->p->settings,
CROMA_MOUSE_MODIFIER_KEY,
value);
g_free (value);
}
if (settings->flags & CAFE_WM_SETTING_THEME) {
g_settings_set_string (meta_wm->p->settings,
CROMA_THEME_KEY,
settings->theme);
}
if (settings->flags & CAFE_WM_SETTING_DOUBLE_CLICK_ACTION) {
g_settings_set_enum (meta_wm->p->settings,
CROMA_DOUBLE_CLICK_TITLEBAR_KEY,
settings->double_click_action);
}
}
static void
croma_get_settings (CafeWindowManager *wm,
CafeWMSettings *settings)
{
int to_get;
CromaWindowManager *meta_wm;
meta_wm = CROMA_WINDOW_MANAGER (wm);
to_get = settings->flags;
settings->flags = 0;
if (to_get & CAFE_WM_SETTING_COMPOSITING_MANAGER) {
settings->compositing_manager = g_settings_get_boolean (meta_wm->p->settings,
CROMA_COMPOSITING_MANAGER_KEY);
settings->flags |= CAFE_WM_SETTING_COMPOSITING_MANAGER;
}
if (to_get & CAFE_WM_SETTING_COMPOSITING_ALTTAB) {
settings->compositing_fast_alt_tab = g_settings_get_boolean (meta_wm->p->settings,
CROMA_COMPOSITING_FAST_ALT_TAB_KEY);
settings->flags |= CAFE_WM_SETTING_COMPOSITING_ALTTAB;
}
if (to_get & CAFE_WM_SETTING_MOUSE_FOCUS) {
gint croma_focus_value;
croma_focus_value = g_settings_get_enum (meta_wm->p->settings,
CROMA_FOCUS_KEY);
settings->focus_follows_mouse = FALSE;
if (croma_focus_value == FOCUS_MODE_SLOPPY || croma_focus_value == FOCUS_MODE_MOUSE)
settings->focus_follows_mouse = TRUE;
settings->flags |= CAFE_WM_SETTING_MOUSE_FOCUS;
}
if (to_get & CAFE_WM_SETTING_AUTORAISE) {
settings->autoraise = g_settings_get_boolean (meta_wm->p->settings,
CROMA_AUTORAISE_KEY);
settings->flags |= CAFE_WM_SETTING_AUTORAISE;
}
if (to_get & CAFE_WM_SETTING_AUTORAISE_DELAY) {
settings->autoraise_delay =
g_settings_get_int (meta_wm->p->settings,
CROMA_AUTORAISE_DELAY_KEY);
settings->flags |= CAFE_WM_SETTING_AUTORAISE_DELAY;
}
if (to_get & CAFE_WM_SETTING_FONT) {
char *str;
str = g_settings_get_string (meta_wm->p->settings,
CROMA_FONT_KEY);
if (str == NULL)
str = g_strdup ("Sans Bold 12");
if (meta_wm->p->font &&
strcmp (meta_wm->p->font, str) == 0) {
g_free (str);
} else {
g_free (meta_wm->p->font);
meta_wm->p->font = str;
}
settings->font = meta_wm->p->font;
settings->flags |= CAFE_WM_SETTING_FONT;
}
if (to_get & CAFE_WM_SETTING_MOUSE_MOVE_MODIFIER) {
char *str;
const char *new;
str = g_settings_get_string (meta_wm->p->settings,
CROMA_MOUSE_MODIFIER_KEY);
if (str == NULL)
str = g_strdup ("<Super>");
if (strcmp (str, "<Super>") == 0)
new = "Super";
else if (strcmp (str, "<Alt>") == 0)
new = "Alt";
else if (strcmp (str, "<Meta>") == 0)
new = "Meta";
else if (strcmp (str, "<Hyper>") == 0)
new = "Hyper";
else if (strcmp (str, "<Control>") == 0)
new = "Control";
else
new = NULL;
if (new && meta_wm->p->mouse_modifier &&
strcmp (new, meta_wm->p->mouse_modifier) == 0) {
/* unchanged */;
} else {
g_free (meta_wm->p->mouse_modifier);
meta_wm->p->mouse_modifier = g_strdup (new);
}
g_free (str);
settings->mouse_move_modifier = meta_wm->p->mouse_modifier;
settings->flags |= CAFE_WM_SETTING_MOUSE_MOVE_MODIFIER;
}
if (to_get & CAFE_WM_SETTING_THEME) {
char *str;
str = g_settings_get_string (meta_wm->p->settings,
CROMA_THEME_KEY);
if (str == NULL)
str = g_strdup ("Spidey");
g_free (meta_wm->p->theme);
meta_wm->p->theme = str;
settings->theme = meta_wm->p->theme;
settings->flags |= CAFE_WM_SETTING_THEME;
}
if (to_get & CAFE_WM_SETTING_DOUBLE_CLICK_ACTION) {
settings->double_click_action =
g_settings_get_enum (meta_wm->p->settings,
CROMA_DOUBLE_CLICK_TITLEBAR_KEY);
settings->flags |= CAFE_WM_SETTING_DOUBLE_CLICK_ACTION;
}
}
static int
croma_get_settings_mask (CafeWindowManager *wm)
{
return CAFE_WM_SETTING_MASK;
}
static void
croma_get_double_click_actions (CafeWindowManager *wm,
const CafeWMDoubleClickAction **actions_p,
int *n_actions_p)
{
static CafeWMDoubleClickAction actions[] = {
{ ACTION_TITLEBAR_TOGGLE_SHADE, N_("Roll up") },
{ ACTION_TITLEBAR_TOGGLE_MAXIMIZE, N_("Maximize") },
{ ACTION_TITLEBAR_TOGGLE_MAXIMIZE_HORIZONTALLY, N_("Maximize Horizontally") },
{ ACTION_TITLEBAR_TOGGLE_MAXIMIZE_VERTICALLY, N_("Maximize Vertically") },
{ ACTION_TITLEBAR_MINIMIZE, N_("Minimize") },
{ ACTION_TITLEBAR_NONE, N_("None") }
};
static gboolean initialized = FALSE;
if (!initialized) {
int i;
initialized = TRUE;
for (i = 0; i < G_N_ELEMENTS (actions); i++) {
actions[i].human_readable_name = _(actions[i].human_readable_name);
}
}
*actions_p = actions;
*n_actions_p = (int) G_N_ELEMENTS (actions);
}
static void
croma_window_manager_init (CromaWindowManager *croma_window_manager,
CromaWindowManagerClass *class)
{
croma_window_manager->p = g_new0 (CromaWindowManagerPrivate, 1);
croma_window_manager->p->settings = g_settings_new (CROMA_SCHEMA);
croma_window_manager->p->font = NULL;
croma_window_manager->p->theme = NULL;
croma_window_manager->p->mouse_modifier = NULL;
g_signal_connect (croma_window_manager->p->settings,
"changed",
G_CALLBACK (value_changed), croma_window_manager);
}
static void
croma_window_manager_finalize (GObject *object)
{
CromaWindowManager *croma_window_manager;
g_return_if_fail (object != NULL);
g_return_if_fail (IS_CROMA_WINDOW_MANAGER (object));
croma_window_manager = CROMA_WINDOW_MANAGER (object);
g_object_unref (croma_window_manager->p->settings);
g_free (croma_window_manager->p);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
croma_window_manager_class_init (CromaWindowManagerClass *class)
{
GObjectClass *object_class;
CafeWindowManagerClass *wm_class;
object_class = G_OBJECT_CLASS (class);
wm_class = CAFE_WINDOW_MANAGER_CLASS (class);
object_class->finalize = croma_window_manager_finalize;
wm_class->change_settings = croma_change_settings;
wm_class->get_settings = croma_get_settings;
wm_class->get_settings_mask = croma_get_settings_mask;
wm_class->get_user_theme_folder = croma_get_user_theme_folder;
wm_class->get_theme_list = croma_get_theme_list;
wm_class->get_double_click_actions = croma_get_double_click_actions;
parent_class = g_type_class_peek_parent (class);
}
GType
croma_window_manager_get_type (void)
{
static GType croma_window_manager_type = 0;
if (!croma_window_manager_type) {
static GTypeInfo croma_window_manager_info = {
sizeof (CromaWindowManagerClass),
NULL, /* GBaseInitFunc */
NULL, /* GBaseFinalizeFunc */
(GClassInitFunc) croma_window_manager_class_init,
NULL, /* GClassFinalizeFunc */
NULL, /* user-supplied data */
sizeof (CromaWindowManager),
0, /* n_preallocs */
(GInstanceInitFunc) croma_window_manager_init,
NULL
};
croma_window_manager_type =
g_type_register_static (cafe_window_manager_get_type (),
"CromaWindowManager",
&croma_window_manager_info, 0);
}
return croma_window_manager_type;
}
|