wmgenmenu: A Window Maker menu generator
[wmaker-crm.git] / util / wmgenmenu.c
1 /* Copyright (C) 2010 Carlos R. Mafra */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <locale.h>
7 #include <libintl.h>
8 #define _(text) gettext(text)
9
10 #include <WINGs/WUtil.h>
11
12 #define MAX_NR_APPS 50  /* Maximum number of entries in each apps list */
13 #define MAX_WMS 10      /* Maximum number of other window managers to check */
14 #include "wmgenmenu.h"
15
16 static void find_and_write(char **list);
17 static inline void workspaces(void);
18 static inline void lock_screen(void);
19 static inline void wmaker_config(void);
20 static inline void run_command(void);
21 static void other_window_managers(char **other_wm);
22 static inline void wm_visual(void);
23 static inline void write_first_line(int count);
24
25 char *path;
26
27 int main(int argc, char *argv[])
28 {
29         int fd;
30         char *locale;
31         extern char *path;
32
33         path = getenv("PATH");
34         locale = getenv("LANG");
35         setlocale(LC_ALL, "");
36
37         if (getenv("NLSPATH"))
38                 bindtextdomain("wmgenmenu", getenv("NLSPATH"));
39         else
40                 bindtextdomain("wmgenmenu", LOCALEDIR);
41
42         bind_textdomain_codeset("wmgenmenu", "UTF-8");
43         textdomain("wmgenmenu");
44
45         printf("\(\"Window Maker\",\n");
46
47         printf("     \(\"");
48         printf(_("Applications"));
49         printf("\",\n");
50
51         /* This "first" printf is different from the others! */
52         printf("     \(\"");
53         printf(_("Terminals"));
54         printf("\"");
55         find_and_write(terminals);
56
57         printf("\n     ),\n     \(\"");
58         printf(_("Internet"));
59         printf("\"");
60         find_and_write(internet);
61
62         printf("\n     ),\n     \(\"");
63         printf(_("Email"));
64         printf("\"");
65         find_and_write(email);
66
67         printf("\n     ),\n     \(\"");
68         printf(_("Mathematics"));
69         printf("\"");
70         find_and_write(Mathematiks);
71
72         printf("\n     ),\n     \(\"");
73         printf(_("File Managers"));
74         printf("\"");
75         find_and_write(file_managers);
76
77         printf("\n     ),\n     \(\"");
78         printf(_("Graphics"));
79         printf("\"");
80         find_and_write(Graphics);
81
82         printf("\n     ),\n     \(\"");
83         printf(_("Multimedia"));
84         printf("\"");
85         find_and_write(Multimedia);
86
87         printf("\n     ),\n     \(\"");
88         printf(_("Editors"));
89         printf("\"");
90         find_and_write(Editors);
91
92         printf("\n     ),\n     \(\"");
93         printf(_("Development"));
94         printf("\"");
95         find_and_write(development);
96
97         printf("\n     ),\n     \(\"");
98         printf(_("Window Maker"));
99         printf("\"");
100         find_and_write(WindowMaker);
101
102         printf("\n     ),\n     \(\"");
103         printf(_("Office"));
104         printf("\"");
105         find_and_write(Office);
106
107         printf("\n     ),\n     \(\"");
108         printf(_("Astronomy"));
109         printf("\"");
110         find_and_write(Astronomie);
111
112         printf("\n     ),\n     \(\"");
113         printf(_("Sound"));
114         printf("\"");
115         find_and_write(Sound);
116
117         printf("\n     ),\n     \(\"");
118         printf(_("Comics"));
119         printf("\"");
120         find_and_write(Comics);
121
122         printf("\n     ),\n     \(\"");
123         printf(_("Viewers"));
124         printf("\"");
125         find_and_write(Viewers);
126
127         printf("\n     ),\n     \(\"");
128         printf(_("Utilities"));
129         printf("\"");
130         find_and_write(Utilities);
131
132         printf("\n     ),\n     \(\"");
133         printf(_("System"));
134         printf("\"");
135         find_and_write(System);
136
137         printf("\n     ),\n     \(\"");
138         printf(_("Video"));
139         printf("\"");
140         find_and_write(Video);
141
142         printf("\n     ),\n     \(\"");
143         printf(_("Chat and Talk"));
144         printf("\"");
145         find_and_write(Chat);
146
147         printf("\n     ),\n     \(\"");
148         printf(_("P2P-Network"));
149         printf("\"");
150         find_and_write(P2P);
151
152         printf("\n     ),\n     \(\"");
153         printf(_("Games"));
154         printf("\"");
155         find_and_write(Games);
156
157         printf("\n     ),\n     \(\"");
158         printf(_("OpenSUSE"));
159         printf("\"");
160         find_and_write(OpenSUSE);
161
162         printf("\n     ),\n     \(\"");
163         printf(_("Mandriva"));
164         printf("\"");
165         find_and_write(Mandriva);
166
167         /* This must be after the last entry */
168         printf("\n     )\n");
169         printf("     ),\n");
170
171         run_command();
172         wm_visual();
173         workspaces();
174         wmaker_config();
175
176         printf("(\"");
177         printf(_("Info Panel"));
178         printf("\", INFO_PANEL),\n");
179
180         printf("(\"");
181         printf(_("Restart"));
182         printf("\", RESTART),\n");
183
184         other_window_managers(other_wm);
185         lock_screen();
186
187         printf("(\"");
188         printf(_("Exit..."));
189         printf("\", EXIT)\n");
190
191         /* Final closing parenthesis */
192         printf(")\n");
193
194         exit(EXIT_SUCCESS);
195 }
196
197 static void find_and_write(char **list)
198 {
199         int i, argc;
200         char *location, **argv;
201         extern char *path;
202
203         for (i = 0; i <= MAX_NR_APPS; i++) {
204                 if (list[i]) {
205                         /* Before checking if app exists, split its options */
206                         wtokensplit(list[i], &argv, &argc);
207                         location = wfindfile(path, argv[0]);
208                         if (location) {
209                                 /* check whether it is to be executed in a terminal */
210                                 if (strcmp("!", argv[argc - 1]) < 0)
211                                         printf(",\n       \(\"%s\", EXEC, \"%s\")", argv[0], list[i]);
212                                 else {
213                                         char comm[50], *ptr[1];
214
215                                         strcpy(comm, list[i]);
216                                         /* ugly hack to delete character ! from list[i] */
217                                         ptr[0] = strchr(comm,'!');
218                                         *ptr[0] = ' ';
219                                         printf(",\n       \(\"%s\", EXEC, \"xterm -e %s\")", argv[0], comm);
220                                 }
221                         }
222                 }
223         }
224 }
225
226 static inline void wmaker_config(void)
227 {
228         char *location = wfindfile(path, "WPrefs");
229
230         if (location) {
231                 printf("(\"");
232                 printf(_("Config wmaker..."));
233                 printf("\", EXEC, \"WPrefs\"),\n");
234         }
235 }
236
237 static inline void workspaces(void)
238 {
239         printf("(\"");
240         printf(_("Workspaces"));
241         printf("\", WORKSPACE_MENU),\n");
242
243         printf("  (\"");
244         printf(_("Workspace"));
245         printf("\",\n");
246
247         printf("     (\"");
248         printf(_("Hide others"));
249         printf("\", HIDE_OTHERS),\n");
250
251         printf("     (\"");
252         printf(_("Show all"));
253         printf("\", SHOW_ALL),\n");
254
255         printf("     (\"");
256         printf(_("Arrange Icons"));
257         printf("\", ARRANGE_ICONS),\n");
258
259         printf("     (\"");
260         printf(_("Refresh"));
261         printf("\", REFRESH),\n");
262
263         printf("     (\"");
264         printf(_("Save Session"));
265         printf("\", SAVE_SESSION),\n");
266
267         printf("     (\"");
268         printf(_("Clear Session"));
269         printf("\", CLEAR_SESSION)\n");
270
271         printf("  ),\n");
272 }
273
274 static inline void lock_screen(void)
275 {
276         char *location;
277
278         location = wfindfile(path, "xlock");
279         if (location) {
280                 printf("(\"");
281                 printf(_("Lock Screen"));
282                 printf("\", EXEC, \"xlock -allowroot -usefirst -mode matrix\"),\n");
283         }
284 }
285
286 static void other_window_managers(char **other_wm)
287 {
288         int count = 0;
289         char *location;
290         int i;
291
292         printf("(\"");
293         printf(_("Other Window Managers"));
294         printf("\",\n");
295
296         for (i = 0; i <= MAX_WMS; i++) {
297                 if (other_wm[i]) {
298                         location = wfindfile(path, other_wm[i]);
299                         if (location) {
300                                 write_first_line(count);
301                                 printf(_("Start %s"), other_wm[i]);
302                                 printf("\", RESTART, \"%s\")", other_wm[i]);
303                                 count++;
304                         }
305                 }
306         }
307         printf("\n),\n");
308 }
309
310 static void wm_visual(void)
311 {
312         /* TODO: add more pre-defined dirs to Themes etc */
313         printf("(\"");
314         printf(_("Workspace Appearance"));
315         printf("\",\n");
316
317         printf("(\"");
318         printf(_("Themes"));
319         printf("\", OPEN_MENU, \"-noext $HOME/GNUstep/Library/WindowMaker/Themes WITH setstyle\"),\n");
320
321         printf("(\"");
322         printf(_("Styles"));
323         printf("\", OPEN_MENU, \"-noext $HOME/GNUstep/Library/WindowMaker/Styles WITH setstyle\"),\n");
324
325         printf("(\"");
326         printf(_("Icons"));
327         printf("\", OPEN_MENU, \"-noext $HOME/GNUstep/Library/WindowMaker/IconSets WITH seticons\"),\n");
328
329         printf("(\"");
330         printf(_("Background"));
331         printf("\", OPEN_MENU, \"-noext $HOME/GNUstep/Library/WindowMaker/Backgrounds WITH wmsetbg -u -t\"),\n");
332
333         printf("(\"");
334         printf(_("Save Theme"));
335         printf("\", SHEXEC, \"getstyle -t $HOME/GNUstep/Library/WindowMaker/Themes/\\\"%%a(Theme name)\\\"\"),\n");
336
337         printf("(\"");
338         printf(_("Save Icons"));
339         printf("\", SHEXEC, \"geticonset $HOME/GNUstep/Library/WindowMaker/IconSets/\\\"%%a(IconSet name)\\\"\")\n");
340         printf("),\n");
341
342 }
343
344 static inline void run_command(void)
345 {
346         /*
347          * %A below requires Voinov's "Add dialog history" (which
348          * is included in wmaker-crm), otherwise it should be %a
349          */
350         printf("\(\"");
351         printf(_("Run..."));
352         printf("\", SHEXEC, \"%%A(");
353         printf(_("Run, Type command:"));
354         printf(")\"),\n");
355 }
356
357 static inline void write_first_line(int count)
358 {
359         /* All lines inside a menu must end with a comma, except for
360          * the last one. To cope with this let's check if there is a
361          * previous entry before writing the "start of the line", and
362          * write the trailing comma if there is.
363          */
364         if (count == 0)
365                 printf(" (\"");
366         else {
367                 printf(",\n (\"");
368         }
369 }