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 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/* cafe-wm-manager.c
* Copyright (C) 2002 Seth Nickell
* Copyright (C) 1998, 2002 Red Hat, Inc.
*
* Written by: Seth Nickell <snickell@stanford.edu>,
* Havoc Pennington <hp@redhat.com>
* Owen Taylor <otaylor@redhat.com>,
* Bradford Hovinen <hovinen@helixcode.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 "cafe-wm-manager.h"
#include <glib.h>
#include <glib/gi18n.h>
#include <cdk/cdk.h>
#include <cdk/cdkx.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
typedef struct {
CafeDesktopItem *ditem;
char *name; /* human readable, localized */
char *identify_name; /* name we expect to be set on the screen */
char *exec;
char *config_exec;
char *config_tryexec;
char *module;
guint session_managed : 1;
guint is_user : 1;
guint is_present : 1;
guint is_config_present : 1;
CafeWindowManager *cafe_wm;<--- struct member 'AvailableWindowManager::cafe_wm' is never used.
} AvailableWindowManager;
static gboolean done_scan = FALSE;
static GList *available_wms;
static void
wm_free (AvailableWindowManager *wm)
{
g_free (wm->name);
g_free (wm->exec);
g_free (wm->config_exec);
g_free (wm->config_tryexec);
g_free (wm->module);
g_free (wm->identify_name);
g_free (wm);
}
static GList *
list_desktop_files_in_dir (gchar *directory)
{
DIR *dir;
struct dirent *child;
GList *result = NULL;
gchar *suffix;<--- The scope of the variable 'suffix' can be reduced. [+]The scope of the variable 'suffix' 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.
dir = opendir (directory);
if (dir == NULL)
return NULL;
while ((child = readdir (dir)) != NULL) {
/* Ignore files without .desktop suffix, and ignore
* .desktop files with no prefix
*/
suffix = child->d_name + strlen (child->d_name) - 8;
/* strlen(".desktop") == 8 */
if (suffix <= child->d_name ||
strcmp (suffix, ".desktop") != 0)
continue;
result = g_list_prepend (result,
g_build_filename (directory, child->d_name, NULL));
}
closedir (dir);
return result;
}
static gint
wm_compare (gconstpointer a, gconstpointer b)
{
const AvailableWindowManager *wm_a = (const AvailableWindowManager *)a;
const AvailableWindowManager *wm_b = (const AvailableWindowManager *)b;
/* mmm, sloooow */
return g_utf8_collate (cafe_desktop_item_get_string (wm_a->ditem, CAFE_DESKTOP_ITEM_NAME),
cafe_desktop_item_get_string (wm_b->ditem, CAFE_DESKTOP_ITEM_NAME));
}
static AvailableWindowManager*
wm_load (const char *desktop_file,
gboolean is_user)
{
gchar *path;
AvailableWindowManager *wm;
wm = g_new0 (AvailableWindowManager, 1);
wm->ditem = cafe_desktop_item_new_from_file (desktop_file, 0, NULL);
if (wm->ditem == NULL) {
g_free (wm);
return NULL;
}
cafe_desktop_item_set_entry_type (wm->ditem, CAFE_DESKTOP_ITEM_TYPE_APPLICATION);
wm->exec = g_strdup (cafe_desktop_item_get_string (wm->ditem,
CAFE_DESKTOP_ITEM_EXEC));
wm->name = g_strdup (cafe_desktop_item_get_string (wm->ditem,
CAFE_DESKTOP_ITEM_NAME));
wm->config_exec = g_strdup (cafe_desktop_item_get_string (wm->ditem,
"ConfigExec"));
wm->config_tryexec = g_strdup (cafe_desktop_item_get_string (wm->ditem,
"ConfigTryExec"));
wm->session_managed = cafe_desktop_item_get_boolean (wm->ditem,
"SessionManaged");
wm->module = g_strdup (cafe_desktop_item_get_string (wm->ditem,
"X-CAFE-WMSettingsModule"));
wm->identify_name = g_strdup (cafe_desktop_item_get_string (wm->ditem,
"X-CAFE-WMName"));
wm->is_user = is_user;
if (cafe_desktop_item_get_string (wm->ditem, CAFE_DESKTOP_ITEM_EXEC)) {
const char *tryexec;
tryexec = cafe_desktop_item_get_string (wm->ditem, CAFE_DESKTOP_ITEM_TRY_EXEC);
if (tryexec) {
path = g_find_program_in_path (tryexec);
wm->is_present = (path != NULL);
if (path)
g_free (path);
} else
wm->is_present = TRUE;
} else
wm->is_present = FALSE;
if (wm->config_exec) {
if (wm->config_tryexec) {
path = g_find_program_in_path (wm->config_tryexec);
wm->is_config_present = (path != NULL);
if (path)
g_free (path);
} else {
path = g_find_program_in_path (wm->config_exec);
wm->is_config_present = (path != NULL);
if (path)
g_free (path);
}
} else
wm->is_config_present = FALSE;
if (wm->name && wm->exec &&
(wm->is_user || wm->is_present))
return wm;
else {
wm_free (wm);
return NULL;
}
}
static void
scan_wm_directory (gchar *directory, gboolean is_user)
{
GList *tmp_list;
GList *files;
files = list_desktop_files_in_dir (directory);
tmp_list = files;
while (tmp_list) {
AvailableWindowManager *wm;
wm = wm_load (tmp_list->data, is_user);
if (wm != NULL)
available_wms = g_list_prepend (available_wms, wm);
tmp_list = tmp_list->next;
}
g_list_foreach (files, (GFunc) g_free, NULL);
g_list_free (files);
}
void cafe_wm_manager_init(void)
{
char* tempdir;
if (done_scan)
{
return;
}
done_scan = TRUE;
tempdir = g_build_filename(CAFE_WM_PROPERTY_PATH, NULL);
scan_wm_directory(tempdir, FALSE);
g_free(tempdir);
tempdir = g_build_filename(g_get_user_config_dir(), "cafe", "wm-properties", NULL);
scan_wm_directory(tempdir, TRUE);
g_free(tempdir);
available_wms = g_list_sort(available_wms, wm_compare);
}
static AvailableWindowManager*
get_current_wm (CdkScreen *screen)
{
AvailableWindowManager *current_wm;
const char *name;
GList *tmp_list;
g_return_val_if_fail (CDK_IS_SCREEN (screen), NULL);
name = cdk_x11_screen_get_window_manager_name (screen);
current_wm = NULL;
tmp_list = available_wms;
while (tmp_list != NULL) {
AvailableWindowManager *wm = tmp_list->data;
if (wm->identify_name &&
strcmp (wm->identify_name, name) == 0) {
current_wm = wm;
break;
}
tmp_list = tmp_list->next;
}
if (current_wm == NULL) {
/* Try with localized name, sort of crackrock
* back compat hack
*/
tmp_list = available_wms;
while (tmp_list != NULL) {
AvailableWindowManager *wm = tmp_list->data;
if (strcmp (wm->name, name) == 0) {
current_wm = wm;
break;
}
tmp_list = tmp_list->next;
}
}
return current_wm;
}
CafeWindowManager*
cafe_wm_manager_get_current (CdkScreen *screen)
{
AvailableWindowManager *wm;
wm = get_current_wm (screen);
if (wm != NULL && wm->module != NULL)
/* may still return NULL here */
return (CafeWindowManager*) cafe_window_manager_new (wm->ditem);
else
return NULL;
}
gboolean
cafe_wm_manager_spawn_config_tool_for_current (CdkScreen *screen,
GError **error)
{
AvailableWindowManager *wm;
wm = get_current_wm (screen);
if (wm != NULL && wm->config_exec != NULL) {
return g_spawn_command_line_async (wm->config_exec,
error);
} else {
const char *name;
name = cdk_x11_screen_get_window_manager_name (screen);
g_set_error (error,
G_SPAWN_ERROR,
G_SPAWN_ERROR_FAILED,
_("Window manager \"%s\" has not registered a configuration tool\n"),
name);
return FALSE;
}
}
|