-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesktop.c
More file actions
357 lines (290 loc) · 7.7 KB
/
desktop.c
File metadata and controls
357 lines (290 loc) · 7.7 KB
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
/*
* This file is part of uxlaunch
*
* (C) Copyright 2009 Intel Corporation
* Authors:
* Auke Kok <auke@linux.intel.com>
* Arjan van de Ven <arjan@linux.intel.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; version 2
* of the License.
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <dirent.h>
#include <time.h>
#include <errno.h>
#include <glib.h>
#include "uxlaunch.h"
#if defined(__i386__)
# define __NR_ioprio_set 289
#elif defined(__x86_64__)
# define __NR_ioprio_set 251
#else
# error "Unsupported arch"
#endif
#define IOPRIO_WHO_PROCESS 1
#define IOPRIO_CLASS_IDLE 3
#define IOPRIO_CLASS_SHIFT 13
#define IOPRIO_IDLE_LOWEST (7 | (IOPRIO_CLASS_IDLE << IOPRIO_CLASS_SHIFT))
int session_pid;
char session_filter[16] = "MOBLIN";
static int delay = 0;
/*
* 50ms steps in between async job startups
* lower priority jobs are started up further apart
*/
#define DELAY_UNIT 50000
struct desktop_entry_struct {
char *exec;
int prio;
};
static GList *desktop_entries;
static void desktop_entry_add(const char *exec, int prio)
{
GList *item;
struct desktop_entry_struct *entry;
/* make sure we don't insert items twice */
item = g_list_first(desktop_entries);
while (item) {
entry = item->data;
if (!strcmp(entry->exec, exec)) {
lprintf("Duplicate entry %s", exec);
return;
}
item = g_list_next(item);
}
entry = malloc(sizeof(struct desktop_entry_struct));
if (!entry) {
lprintf("Error allocating memory for desktop entry");
return;
}
entry->prio = prio; /* panels start at highest prio */
entry->exec = g_strdup(exec);
lprintf("Adding %s with prio %d", entry->exec, entry->prio);
desktop_entries = g_list_prepend(desktop_entries, entry);
}
gint sort_entries(gconstpointer a, gconstpointer b)
{
const struct desktop_entry_struct *A = a, *B = b;
if (A->prio > B->prio)
return 1;
if (A->prio < B->prio)
return -1;
return strcmp(A->exec, B->exec);
}
/*
* Process a .desktop file
* Objective: fine the "Exec=" line which has the command to run
* Constraints:
* if there is a OnlyShowIn line, then it must contain the string "MOBLIN"
* (this is to allow moblin-only settings apps to show up, but not gnome settings apps)
* if there is a NotShowIn line, it must not contain "MOBLIN" or "GNOME"
* (this allows KDE etc systems to hide stuff for GNOME as they do today and not show it
* on moblin)
*/
static void do_desktop_file(const char *filename)
{
FILE *file;
char line[4096];
char exec[4096];
int show = 1;
int prio = 1; /* medium/normal prio */
file = fopen(filename, "r");
if (!file)
return;
memset(exec, 0, 4096);
while (!feof(file)) {
char *c;
if (fgets(line, 4096, file) == NULL)
break;
c = strchr(line, '\n');
if (c) *c = 0;
if (strstr(line, "Exec="))
strncpy(exec, line+5, 4095);
if (strstr(line, "OnlyShowIn"))
if (strstr(line, session_filter) == NULL)
show = 0;
if (strstr(line, "NotShowIn")) {
if (strstr(line, session_filter))
show = 0;
/* for moblin, hide stuff hidden to gnome */
if (!strcmp(session_filter, "MOBLIN"))
if (strstr(line, "GNOME"))
show = 0;
}
if (strstr(line, "X-Moblin-Priority")) {
if (strstr(line, "Highest"))
prio = -1;
if (strstr(line, "High"))
prio = 0;
/* default: prio = 1 */
if (strstr(line, "Low"))
prio = 2;
}
}
fclose(file);
if (show && strlen(exec)>0)
desktop_entry_add(exec, prio);
}
void autostart_panels(void)
{
if (!strstr(session_filter, "MOBLIN"))
return;
if (!strstr(session, "mutter"))
return;
desktop_entry_add(MUTTER_LIBEXECDIR "/moblin-panel-myzone", -1);
desktop_entry_add(MUTTER_LIBEXECDIR "/moblin-panel-status", 0);
desktop_entry_add(MUTTER_LIBEXECDIR "/moblin-panel-people", 0);
desktop_entry_add(MUTTER_LIBEXECDIR "/moblin-panel-internet", 0);
desktop_entry_add(MUTTER_LIBEXECDIR "/moblin-panel-media", 1);
desktop_entry_add(MUTTER_LIBEXECDIR "/moblin-panel-pasteboard", 0);
desktop_entry_add(MUTTER_LIBEXECDIR "/moblin-panel-applications", 1);
}
void get_session_type(void)
{
/* adjust filter based on what our session cmd is */
if (strstr(session, "xfce"))
snprintf(session_filter, 16, "XFCE");
if (strstr(session, "gnome"))
snprintf(session_filter, 16, "GNOME");
if (strstr(session, "kde"))
snprintf(session_filter, 16, "KDE");
/* default == MOBLIN */
}
/*
* We need to process all the .desktop files in /etc/xdg/autostart.
* Simply walk the directory
*/
void autostart_desktop_files(void)
{
DIR *dir;
struct dirent *entry;
lprintf("Entering autostart_desktop_files");
dir = opendir("/etc/xdg/autostart");
if (!dir) {
lprintf("Autostart directory not found");
return;
}
while (1) {
char filename[PATH_MAX];
entry = readdir(dir);
if (!entry)
break;
if (entry->d_name[0] == '.')
continue;
if (entry->d_type != DT_REG)
continue;
if (strchr(entry->d_name, '~'))
continue; /* editor backup file */
snprintf(filename, 4096, "/etc/xdg/autostart/%s", entry->d_name);
do_desktop_file(filename);
}
closedir(dir);
}
void start_xinitrd_scripts(void)
{
DIR* dir;
dir = opendir("/etc/X11/xinit/xinitrc.d");
while (dir) {
int ret;
struct dirent *entry;
char cmd[PATH_MAX];
entry = readdir(dir);
if (!entry)
break;
if (entry->d_type != DT_REG)
continue;
/* queue every 0.4s after xdg/autostart stuff */
delay = delay + 8 * DELAY_UNIT;
if (fork())
continue;
usleep(delay);
snprintf(cmd, PATH_MAX, "/etc/X11/xinit/xinitrc.d/%s", entry->d_name);
lprintf("Starting \"%s\" at %d", cmd, delay);
ret = system(cmd);
if (ret)
lprintf("Warning: \"%s\" returned %d", cmd, ret);
exit(ret);
}
}
void do_autostart(void)
{
GList *item;
struct desktop_entry_struct *entry;
lprintf("Entering do_autostart");
/* sort by priority */
desktop_entries = g_list_sort(desktop_entries, sort_entries);
item = g_list_first(desktop_entries);
while (item) {
char *ptrs[256];
int count = 0;
int ret = 0;
entry = item->data;
delay = delay + ((1 << (entry->prio + 1)) * DELAY_UNIT);
lprintf("Queueing %s with prio %d at %d", entry->exec, entry->prio, delay);
if (fork()) {
item = g_list_next(item);
continue;
}
if (entry->prio >= 1) {
syscall(__NR_ioprio_set, IOPRIO_WHO_PROCESS, 0, IOPRIO_IDLE_LOWEST);
ret = nice(5);
}
memset(ptrs, 0, sizeof(ptrs));
ptrs[0] = strtok(entry->exec, " \t");
while (ptrs[count] && count < 255)
ptrs[++count] = strtok(NULL, " \t");
usleep(delay);
lprintf("Starting %s with prio %d at %d", entry->exec, entry->prio, delay);
execvp(ptrs[0], ptrs);
exit(ret);
}
start_xinitrd_scripts();
}
void start_desktop_session(void)
{
int ret;
int count = 0;
char *ptrs[256];
ret = fork();
if (ret) {
session_pid = ret;
return; /* parent continues */
}
lprintf("Entering start_desktop_session");
ret = system("/usr/bin/xdg-user-dirs-update");
if (ret)
lprintf("/usr/bin/xdg-user-dirs-update failed");
memset(ptrs, 0, sizeof(ptrs));
ptrs[0] = strtok(session, " \t");
while (ptrs[count] && count < 255)
ptrs[++count] = strtok(NULL, " \t");
lprintf("Exec session '%s'", session);
ret = execv(ptrs[0], ptrs);
if (ret != EXIT_SUCCESS)
lprintf("Failed to start %s", session);
}
void wait_for_session_exit(void)
{
lprintf("wait_for_session_exit");
for (;;) {
errno = 0;
if (waitpid (session_pid, NULL, 0) < 0) {
if (errno == EINTR) {
continue;
} else if (errno == ECHILD)
break; /* child already reaped */
else
lprintf("waidpid error '%s'", strerror (errno));
}
break;
}
lprintf("session exited");
}