Prevent border drifting.
[wmaker-crm.git] / util / wmmenugen.c
blob82ad8ce02519229cbd254ff5c1b8e85004ceccc2
1 /*
2 * wmmenugen - Window Maker PropList menu generator
4 * Copyright (c) 2010. Tamas Tevesz <ice@extreme.hu>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <sys/types.h>
22 #include <sys/stat.h>
24 #include <ctype.h>
25 #include <ftw.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <unistd.h>
32 #include "wmmenugen.h"
34 static void addWMMenuEntryCallback(WMMenuEntry *aEntry);
35 static void assemblePLMenuFunc(WMTreeNode *aNode, void *data);
36 static int dirParseFunc(const char *filename, const struct stat *st, int tflags, struct FTW *ftw);
37 static int menuSortFunc(const void *left, const void *right);
38 static int nodeFindSubMenuByNameFunc(const void *item, const void *cdata);
39 static WMTreeNode *findPositionInMenu(char *submenu);
40 static void (*parse)(const char *file, void (*addWMMenuEntryCallback)(WMMenuEntry *aEntry));
41 static Bool (*validateFilename)(const char *filename, const struct stat *st, int tflags, struct FTW *ftw);
43 static WMArray *plMenuNodes;
44 char *terminal;
46 extern char *__progname;
48 int main(int argc, char **argv)
50 struct stat st;
51 int i;
52 int *previousDepth;
54 plMenuNodes = WMCreateArray(8); /* grows on demand */
55 menu = (WMTreeNode *)NULL;
56 parse = NULL;
57 validateFilename = NULL;
59 /* assemblePLMenuFunc passes this around */
60 previousDepth = (int *)wmalloc(sizeof(int));
61 *previousDepth = -1;
63 /* currently this is used only by the xdg parser, but it might be useful
64 * in the future localizing other menus, so it won't hurt to have it here.
66 parse_locale(NULL, &env_lang, &env_ctry, &env_enc, &env_mod);
67 terminal = find_terminal_emulator();
69 if (argc < 3) {
70 fprintf(stderr, "Usage: %s -parser:<parser> fspec [fpsec...] "
71 "[-parser:<parser> fspec [fpsec...]...]\n", __progname);
72 fputs( "Known parsers: xdg wmconfig\n", stderr);
73 return 1;
76 for (i = 1; i < argc; i++)
78 if (strncmp(argv[i], "-parser:", 8) == 0) {
79 if (strcmp(argv[i] + 8, "xdg") == 0) {
80 #if DEBUG
81 fputs("Using parser \"xdg\"\n", stderr);
82 #endif
83 parse = &parse_xdg;
84 } else if (strcmp(argv[i] + 8, "wmconfig") == 0) {
85 #if DEBUG
86 fputs("Using parser \"wmconfig\"\n", stderr);
87 #endif
88 parse = &parse_wmconfig;
89 validateFilename = &wmconfig_validate_file;
90 } else {
91 fprintf(stderr, "%s: Unknown parser \"%s\"\n", __progname, argv[i] + 8);
93 continue;
96 if (parse) {
97 if (stat(argv[i], &st) == -1) {
98 fprintf(stderr, "%s: unable to stat \"%s\"\n", __progname, argv[i]);
99 } else if (S_ISREG(st.st_mode)) {
100 parse(argv[i], addWMMenuEntryCallback);
101 } else if (S_ISDIR(st.st_mode)) {
102 nftw(argv[i], dirParseFunc, 16, FTW_PHYS);
103 } else {
104 fprintf(stderr, "%s: \"%s\" is not a file or directory\n", __progname, argv[i]);
106 } else {
107 fprintf(stderr, "%s: argument \"%s\" with no valid parser\n", __progname, argv[i]);
111 if (!menu) {
112 fprintf(stderr, "%s: parsers failed to create a valid menu\n", __progname);
113 return 1;
116 WMSortTree(menu, menuSortFunc);
117 WMTreeWalk(menu, assemblePLMenuFunc, previousDepth, True);
119 i = WMGetArrayItemCount(plMenuNodes);
120 if (i > 2) { /* more than one submenu unprocessed is almost certainly an error */
121 fprintf(stderr, "%s: unprocessed levels on the stack. fishy.\n", __progname);
122 return 1;
123 } else if (i > 1 ) { /* possibly the top-level attachment is not yet done */
124 WMPropList *first, *next;
125 next = WMPopFromArray(plMenuNodes);
126 first = WMPopFromArray(plMenuNodes);
127 WMAddToPLArray(first, next);
128 WMAddToArray(plMenuNodes, first);
131 printf("%s\n", WMGetPropListDescription((WMPropList *)WMGetFromArray(plMenuNodes, 0), True));
133 return 0;
136 static int dirParseFunc(const char *filename, const struct stat *st, int tflags, struct FTW *ftw)
138 (void)st;
139 (void)tflags;
140 (void)ftw;
142 if (validateFilename &&
143 !validateFilename(filename, st, tflags, ftw))
144 return 0;
146 parse(filename, addWMMenuEntryCallback);
147 return 0;
150 /* upon fully deducing one particular menu entry, parsers call back to this
151 * function to have said menu entry added to the wm menu. initializes wm menu
152 * with a root element if needed.
154 static void addWMMenuEntryCallback(WMMenuEntry *aEntry)
156 WMMenuEntry *wm;
157 WMTreeNode *at;
159 wm = (WMMenuEntry *)wmalloc(sizeof(WMMenuEntry)); /* this entry */
160 at = (WMTreeNode *)NULL; /* will be a child of this entry */
162 if (!menu) {
163 WMMenuEntry *root;
165 root = (WMMenuEntry *)wmalloc(sizeof(WMMenuEntry));
166 root->Name = "Applications";
167 root->CmdLine = NULL;
168 root->SubMenu = NULL;
169 root->Flags = 0;
170 menu = WMCreateTreeNode(root);
173 if (aEntry->SubMenu)
174 at = findPositionInMenu(wstrdup(aEntry->SubMenu));
176 if (!at)
177 at = menu;
179 wm->Flags = aEntry->Flags;
180 wm->Name = wstrdup(aEntry->Name);
181 wm->CmdLine = wstrdup(aEntry->CmdLine);
182 wm->SubMenu = NULL;
183 WMAddItemToTree(at, wm);
187 /* creates the proplist menu out of the abstract menu representation in `menu'.
189 static void assemblePLMenuFunc(WMTreeNode *aNode, void *data)
191 WMMenuEntry *wm;
192 WMPropList *pl;
193 int pDepth, cDepth;
195 wm = (WMMenuEntry *)WMGetDataForTreeNode(aNode);
196 cDepth = WMGetTreeNodeDepth(aNode);
197 pDepth = *(int *)data;
199 if (pDepth > cDepth) { /* just ascended out of a/several submenu(s) */
200 WMPropList *last, *but; /* merge the tail up to the current position */
201 int i;
202 for (i = pDepth - cDepth; i > 0; i--) {
203 last = WMPopFromArray(plMenuNodes);
204 but = WMPopFromArray(plMenuNodes);
205 WMAddToPLArray(but, last);
206 WMAddToArray(plMenuNodes, but);
210 if (!wm->CmdLine) { /* new submenu */
211 WMAddToArray(plMenuNodes, WMCreatePLArray(WMCreatePLString(wm->Name), NULL));
212 } else { /* new menu item */
213 pl = WMPopFromArray(plMenuNodes);
214 if (wm->Flags & F_RESTART_OTHER) { /* RESTART, somewm */
215 char buf[1024];
216 memset(buf, 0, sizeof(buf));
217 snprintf(buf, sizeof(buf), "%s %s", _("Restart"), wm->Name);
218 WMAddToPLArray(pl, WMCreatePLArray(
219 WMCreatePLString(buf),
220 WMCreatePLString("RESTART"),
221 WMCreatePLString(wm->CmdLine),
222 NULL)
224 } else if (wm->Flags & F_RESTART_SELF) {/* RESTART */
225 WMAddToPLArray(pl, WMCreatePLArray(
226 WMCreatePLString(_("Restart Window Maker")),
227 WMCreatePLString("RESTART"),
228 NULL)
230 } else if (wm->Flags & F_QUIT) { /* EXIT */
231 WMAddToPLArray(pl, WMCreatePLArray(
232 WMCreatePLString(_("Exit Window Maker")),
233 WMCreatePLString("EXIT"),
234 NULL)
236 } else { /* plain simple command */
237 char buf[1024];
238 memset(buf, 0, sizeof(buf));
239 if (wm->Flags & F_TERMINAL) /* XXX: quoting! */
240 snprintf(buf, sizeof(buf), "%s -e \"%s\"", terminal, wm->CmdLine);
241 else
242 snprintf(buf, sizeof(buf), "%s", wm->CmdLine);
243 WMAddToPLArray(pl, WMCreatePLArray(
244 WMCreatePLString(wm->Name),
245 WMCreatePLString("SHEXEC"),
246 WMCreatePLString(buf),
247 NULL)
250 WMAddToArray(plMenuNodes, pl);
253 *(int *)data = cDepth;
254 return;
257 /* sort the menu tree; callback for WMSortTree()
259 static int menuSortFunc(const void *left, const void *right)
261 WMMenuEntry *leftwm;
262 WMMenuEntry *rightwm;
264 leftwm = (WMMenuEntry *)WMGetDataForTreeNode(*(WMTreeNode **)left);
265 rightwm = (WMMenuEntry *)WMGetDataForTreeNode(*(WMTreeNode **)right);
267 /* submenus first */
268 if (!leftwm->CmdLine && rightwm->CmdLine)
269 return -1;
270 if (leftwm->CmdLine && !rightwm->CmdLine)
271 return 1;
273 /* the rest lexicographically */
274 return strcasecmp(leftwm->Name, rightwm->Name);
278 /* returns the leaf an entry with the submenu spec `submenu' attaches to.
279 * creates `submenu' path (anchored to the root) along the way.
281 static WMTreeNode *findPositionInMenu(char *submenu)
283 char *q;
284 WMMenuEntry *wm;
285 WMTreeNode *node, *pnode;
286 char buf[1024];
288 /* qualify submenu with "Applications/" (the root node) */
289 memset(buf, 0, sizeof(buf));
290 snprintf(buf, sizeof(buf), "Applications/%s", submenu);
292 /* start at the root */
293 node = menu;
295 q = strtok(buf, "/");
296 while (q) {
297 pnode = node;
298 node = WMFindInTreeWithDepthLimit(pnode, nodeFindSubMenuByNameFunc, q, 1);
299 if (!node) {
300 wm = (WMMenuEntry *)wmalloc(sizeof(WMMenuEntry));
301 wm->Flags = 0;
302 wm->Name = wstrdup(q);
303 wm->CmdLine = NULL;
304 wm->SubMenu = NULL;
305 node = WMAddNodeToTree(pnode, WMCreateTreeNode(wm));
307 q = strtok(NULL, "/");
310 return node;
313 /* find node where Name = cdata and node is a submenu
315 static int nodeFindSubMenuByNameFunc(const void *item, const void *cdata)
317 WMMenuEntry *wm;
319 wm = (WMMenuEntry *)item;
321 if (wm->CmdLine) /* if it has a cmdline, it can't be a submenu */
322 return 0;
324 return strcmp(wm->Name, (const char *)cdata) == 0;