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>
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 */
55 char *env_lang
, *env_ctry
, *env_enc
, *env_mod
;
57 int main(int argc
, char **argv
)
63 plMenuNodes
= WMCreateArray(8); /* grows on demand */
64 menu
= (WMTreeNode
*)NULL
;
66 validateFilename
= NULL
;
68 /* assemblePLMenuFunc passes this around */
69 previousDepth
= (int *)wmalloc(sizeof(int));
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();
79 fprintf(stderr
, "Usage: %s -parser:<parser> fspec [fpsec...] "
80 "[-parser:<parser> fspec [fpsec...]...]\n", __progname
);
81 fputs( "Known parsers: xdg wmconfig\n", stderr
);
85 for (i
= 1; i
< argc
; i
++)
87 if (strncmp(argv
[i
], "-parser:", 8) == 0) {
88 if (strcmp(argv
[i
] + 8, "xdg") == 0) {
90 fputs("Using parser \"xdg\"\n", stderr
);
93 } else if (strcmp(argv
[i
] + 8, "wmconfig") == 0) {
95 fputs("Using parser \"wmconfig\"\n", stderr
);
97 parse
= &parse_wmconfig
;
98 validateFilename
= &wmconfig_validate_file
;
100 fprintf(stderr
, "%s: Unknown parser \"%s\"\n", __progname
, argv
[i
] + 8);
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
);
113 fprintf(stderr
, "%s: \"%s\" is not a file or directory\n", __progname
, argv
[i
]);
116 fprintf(stderr
, "%s: argument \"%s\" with no valid parser\n", __progname
, argv
[i
]);
121 fprintf(stderr
, "%s: parsers failed to create a valid menu\n", __progname
);
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
);
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
));
145 static int dirParseFunc(const char *filename
, const struct stat
*st
, int tflags
, struct FTW
*ftw
)
151 if (validateFilename
&&
152 !validateFilename(filename
, st
, tflags
, ftw
))
155 parse(filename
, addWMMenuEntryCallback
);
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
)
168 wm
= (WMMenuEntry
*)wmalloc(sizeof(WMMenuEntry
)); /* this entry */
169 at
= (WMTreeNode
*)NULL
; /* will be a child of this entry */
174 root
= (WMMenuEntry
*)wmalloc(sizeof(WMMenuEntry
));
175 root
->Name
= "Applications";
176 root
->CmdLine
= NULL
;
177 root
->SubMenu
= NULL
;
179 menu
= WMCreateTreeNode(root
);
183 at
= findPositionInMenu(aEntry
->SubMenu
);
188 wm
->Flags
= aEntry
->Flags
;
189 wm
->Name
= wstrdup(aEntry
->Name
);
190 wm
->CmdLine
= wstrdup(aEntry
->CmdLine
);
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
)
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 */
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 */
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
),
233 } else if (wm
->Flags
& F_RESTART_SELF
) {/* RESTART */
234 WMAddToPLArray(pl
, WMCreatePLArray(
235 WMCreatePLString(_("Restart Window Maker")),
236 WMCreatePLString("RESTART"),
239 } else if (wm
->Flags
& F_QUIT
) { /* EXIT */
240 WMAddToPLArray(pl
, WMCreatePLArray(
241 WMCreatePLString(_("Exit Window Maker")),
242 WMCreatePLString("EXIT"),
245 } else { /* plain simple command */
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
);
252 snprintf(buf
, sizeof(buf
), "%s", wm
->CmdLine
);
254 WMAddToPLArray(pl
, WMCreatePLArray(
255 WMCreatePLString(wm
->Name
),
256 WMCreatePLString("SHEXEC"),
257 WMCreatePLString(buf
),
261 WMAddToArray(plMenuNodes
, pl
);
264 *(int *)data
= cDepth
;
268 /* sort the menu tree; callback for WMSortTree()
270 static int menuSortFunc(const void *left
, const void *right
)
273 WMMenuEntry
*rightwm
;
275 leftwm
= (WMMenuEntry
*)WMGetDataForTreeNode(*(WMTreeNode
**)left
);
276 rightwm
= (WMMenuEntry
*)WMGetDataForTreeNode(*(WMTreeNode
**)right
);
279 if (!leftwm
->CmdLine
&& rightwm
->CmdLine
)
281 if (leftwm
->CmdLine
&& !rightwm
->CmdLine
)
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
)
296 WMTreeNode
*node
, *pnode
;
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 */
306 q
= strtok(buf
, "/");
309 node
= WMFindInTreeWithDepthLimit(pnode
, nodeFindSubMenuByNameFunc
, q
, 1);
311 wm
= (WMMenuEntry
*)wmalloc(sizeof(WMMenuEntry
));
313 wm
->Name
= wstrdup(q
);
316 node
= WMAddNodeToTree(pnode
, WMCreateTreeNode(wm
));
318 q
= strtok(NULL
, "/");
324 /* find node where Name = cdata and node is a submenu
326 static int nodeFindSubMenuByNameFunc(const void *item
, const void *cdata
)
330 wm
= (WMMenuEntry
*)item
;
332 if (wm
->CmdLine
) /* if it has a cmdline, it can't be a submenu */
335 return strcmp(wm
->Name
, (const char *)cdata
) == 0;