wmaker: Add window snapping feature.
[wmaker-crm.git] / util / wmmenugen.c
blob437b989be7fc2ef9e63ec3481e4b2bb8e202ed03
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(const char *submenu);
42 typedef void fct_parse_menufile(const char *file, cb_add_menu_entry *addWMMenuEntryCallback);
43 typedef Bool fct_validate_filename(const char *filename, const struct stat *st, int tflags, struct FTW *ftw);
46 static WMArray *plMenuNodes;
47 static const char *terminal;
48 static fct_parse_menufile *parse;
49 static fct_validate_filename *validateFilename;
51 extern char *__progname;
53 /* Global Variables from wmmenugen.h */
54 WMTreeNode *menu;
55 char *env_lang, *env_ctry, *env_enc, *env_mod;
57 int main(int argc, char **argv)
59 struct stat st;
60 int i;
61 int *previousDepth;
63 plMenuNodes = WMCreateArray(8); /* grows on demand */
64 menu = (WMTreeNode *)NULL;
65 parse = NULL;
66 validateFilename = NULL;
68 /* assemblePLMenuFunc passes this around */
69 previousDepth = (int *)wmalloc(sizeof(int));
70 *previousDepth = -1;
72 /* currently this is used only by the xdg parser, but it might be useful
73 * in the future localizing other menus, so it won't hurt to have it here.
75 parse_locale(NULL, &env_lang, &env_ctry, &env_enc, &env_mod);
76 terminal = find_terminal_emulator();
78 if (argc < 3) {
79 fprintf(stderr, "Usage: %s -parser:<parser> fspec [fpsec...] "
80 "[-parser:<parser> fspec [fpsec...]...]\n", __progname);
81 fputs( "Known parsers: xdg wmconfig\n", stderr);
82 return 1;
85 for (i = 1; i < argc; i++)
87 if (strncmp(argv[i], "-parser:", 8) == 0) {
88 if (strcmp(argv[i] + 8, "xdg") == 0) {
89 #if DEBUG
90 fputs("Using parser \"xdg\"\n", stderr);
91 #endif
92 parse = &parse_xdg;
93 } else if (strcmp(argv[i] + 8, "wmconfig") == 0) {
94 #if DEBUG
95 fputs("Using parser \"wmconfig\"\n", stderr);
96 #endif
97 parse = &parse_wmconfig;
98 validateFilename = &wmconfig_validate_file;
99 } else {
100 fprintf(stderr, "%s: Unknown parser \"%s\"\n", __progname, argv[i] + 8);
102 continue;
105 if (parse) {
106 if (stat(argv[i], &st) == -1) {
107 fprintf(stderr, "%s: unable to stat \"%s\"\n", __progname, argv[i]);
108 } else if (S_ISREG(st.st_mode)) {
109 parse(argv[i], addWMMenuEntryCallback);
110 } else if (S_ISDIR(st.st_mode)) {
111 nftw(argv[i], dirParseFunc, 16, FTW_PHYS);
112 } else {
113 fprintf(stderr, "%s: \"%s\" is not a file or directory\n", __progname, argv[i]);
115 } else {
116 fprintf(stderr, "%s: argument \"%s\" with no valid parser\n", __progname, argv[i]);
120 if (!menu) {
121 fprintf(stderr, "%s: parsers failed to create a valid menu\n", __progname);
122 return 1;
125 WMSortTree(menu, menuSortFunc);
126 WMTreeWalk(menu, assemblePLMenuFunc, previousDepth, True);
128 i = WMGetArrayItemCount(plMenuNodes);
129 if (i > 2) { /* more than one submenu unprocessed is almost certainly an error */
130 fprintf(stderr, "%s: unprocessed levels on the stack. fishy.\n", __progname);
131 return 1;
132 } else if (i > 1 ) { /* possibly the top-level attachment is not yet done */
133 WMPropList *first, *next;
134 next = WMPopFromArray(plMenuNodes);
135 first = WMPopFromArray(plMenuNodes);
136 WMAddToPLArray(first, next);
137 WMAddToArray(plMenuNodes, first);
140 printf("%s\n", WMGetPropListDescription((WMPropList *)WMGetFromArray(plMenuNodes, 0), True));
142 return 0;
145 static int dirParseFunc(const char *filename, const struct stat *st, int tflags, struct FTW *ftw)
147 (void)st;
148 (void)tflags;
149 (void)ftw;
151 if (validateFilename &&
152 !validateFilename(filename, st, tflags, ftw))
153 return 0;
155 parse(filename, addWMMenuEntryCallback);
156 return 0;
159 /* upon fully deducing one particular menu entry, parsers call back to this
160 * function to have said menu entry added to the wm menu. initializes wm menu
161 * with a root element if needed.
163 static void addWMMenuEntryCallback(WMMenuEntry *aEntry)
165 WMMenuEntry *wm;
166 WMTreeNode *at;
168 wm = (WMMenuEntry *)wmalloc(sizeof(WMMenuEntry)); /* this entry */
169 at = (WMTreeNode *)NULL; /* will be a child of this entry */
171 if (!menu) {
172 WMMenuEntry *root;
174 root = (WMMenuEntry *)wmalloc(sizeof(WMMenuEntry));
175 root->Name = "Applications";
176 root->CmdLine = NULL;
177 root->SubMenu = NULL;
178 root->Flags = 0;
179 menu = WMCreateTreeNode(root);
182 if (aEntry->SubMenu)
183 at = findPositionInMenu(aEntry->SubMenu);
185 if (!at)
186 at = menu;
188 wm->Flags = aEntry->Flags;
189 wm->Name = wstrdup(aEntry->Name);
190 wm->CmdLine = wstrdup(aEntry->CmdLine);
191 wm->SubMenu = NULL;
192 WMAddItemToTree(at, wm);
196 /* creates the proplist menu out of the abstract menu representation in `menu'.
198 static void assemblePLMenuFunc(WMTreeNode *aNode, void *data)
200 WMMenuEntry *wm;
201 WMPropList *pl;
202 int pDepth, cDepth;
204 wm = (WMMenuEntry *)WMGetDataForTreeNode(aNode);
205 cDepth = WMGetTreeNodeDepth(aNode);
206 pDepth = *(int *)data;
208 if (pDepth > cDepth) { /* just ascended out of a/several submenu(s) */
209 WMPropList *last, *but; /* merge the tail up to the current position */
210 int i;
211 for (i = pDepth - cDepth; i > 0; i--) {
212 last = WMPopFromArray(plMenuNodes);
213 but = WMPopFromArray(plMenuNodes);
214 WMAddToPLArray(but, last);
215 WMAddToArray(plMenuNodes, but);
219 if (!wm->CmdLine) { /* new submenu */
220 WMAddToArray(plMenuNodes, WMCreatePLArray(WMCreatePLString(wm->Name), NULL));
221 } else { /* new menu item */
222 pl = WMPopFromArray(plMenuNodes);
223 if (wm->Flags & F_RESTART_OTHER) { /* RESTART, somewm */
224 char buf[1024];
225 memset(buf, 0, sizeof(buf));
226 snprintf(buf, sizeof(buf), "%s %s", _("Restart"), wm->Name);
227 WMAddToPLArray(pl, WMCreatePLArray(
228 WMCreatePLString(buf),
229 WMCreatePLString("RESTART"),
230 WMCreatePLString(wm->CmdLine),
231 NULL)
233 } else if (wm->Flags & F_RESTART_SELF) {/* RESTART */
234 WMAddToPLArray(pl, WMCreatePLArray(
235 WMCreatePLString(_("Restart Window Maker")),
236 WMCreatePLString("RESTART"),
237 NULL)
239 } else if (wm->Flags & F_QUIT) { /* EXIT */
240 WMAddToPLArray(pl, WMCreatePLArray(
241 WMCreatePLString(_("Exit Window Maker")),
242 WMCreatePLString("EXIT"),
243 NULL)
245 } else { /* plain simple command */
246 char buf[1024];
248 memset(buf, 0, sizeof(buf));
249 if (wm->Flags & F_TERMINAL) /* XXX: quoting! */
250 snprintf(buf, sizeof(buf), "%s -e \"%s\"", terminal, wm->CmdLine);
251 else
252 snprintf(buf, sizeof(buf), "%s", wm->CmdLine);
254 WMAddToPLArray(pl, WMCreatePLArray(
255 WMCreatePLString(wm->Name),
256 WMCreatePLString("SHEXEC"),
257 WMCreatePLString(buf),
258 NULL)
261 WMAddToArray(plMenuNodes, pl);
264 *(int *)data = cDepth;
265 return;
268 /* sort the menu tree; callback for WMSortTree()
270 static int menuSortFunc(const void *left, const void *right)
272 WMMenuEntry *leftwm;
273 WMMenuEntry *rightwm;
275 leftwm = (WMMenuEntry *)WMGetDataForTreeNode(*(WMTreeNode **)left);
276 rightwm = (WMMenuEntry *)WMGetDataForTreeNode(*(WMTreeNode **)right);
278 /* submenus first */
279 if (!leftwm->CmdLine && rightwm->CmdLine)
280 return -1;
281 if (leftwm->CmdLine && !rightwm->CmdLine)
282 return 1;
284 /* the rest lexicographically */
285 return strcasecmp(leftwm->Name, rightwm->Name);
289 /* returns the leaf an entry with the submenu spec `submenu' attaches to.
290 * creates `submenu' path (anchored to the root) along the way.
292 static WMTreeNode *findPositionInMenu(const char *submenu)
294 char *q;
295 WMMenuEntry *wm;
296 WMTreeNode *node, *pnode;
297 char buf[1024];
299 /* qualify submenu with "Applications/" (the root node) */
300 memset(buf, 0, sizeof(buf));
301 snprintf(buf, sizeof(buf), "Applications/%s", submenu);
303 /* start at the root */
304 node = menu;
306 q = strtok(buf, "/");
307 while (q) {
308 pnode = node;
309 node = WMFindInTreeWithDepthLimit(pnode, nodeFindSubMenuByNameFunc, q, 1);
310 if (!node) {
311 wm = (WMMenuEntry *)wmalloc(sizeof(WMMenuEntry));
312 wm->Flags = 0;
313 wm->Name = wstrdup(q);
314 wm->CmdLine = NULL;
315 wm->SubMenu = NULL;
316 node = WMAddNodeToTree(pnode, WMCreateTreeNode(wm));
318 q = strtok(NULL, "/");
321 return node;
324 /* find node where Name = cdata and node is a submenu
326 static int nodeFindSubMenuByNameFunc(const void *item, const void *cdata)
328 WMMenuEntry *wm;
330 wm = (WMMenuEntry *)item;
332 if (wm->CmdLine) /* if it has a cmdline, it can't be a submenu */
333 return 0;
335 return strcmp(wm->Name, (const char *)cdata) == 0;