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 static const char *prog_name
;
53 /* Global Variables from wmmenugen.h */
55 char *env_lang
, *env_ctry
, *env_enc
, *env_mod
;
57 int main(int argc
, char **argv
)
64 plMenuNodes
= WMCreateArray(8); /* grows on demand */
65 menu
= (WMTreeNode
*)NULL
;
67 validateFilename
= NULL
;
69 /* assemblePLMenuFunc passes this around */
70 previousDepth
= (int *)wmalloc(sizeof(int));
73 /* currently this is used only by the xdg parser, but it might be useful
74 * in the future localizing other menus, so it won't hurt to have it here.
76 parse_locale(NULL
, &env_lang
, &env_ctry
, &env_enc
, &env_mod
);
77 terminal
= find_terminal_emulator();
80 fprintf(stderr
, "Usage: %s -parser:<parser> fspec [fpsec...] "
81 "[-parser:<parser> fspec [fpsec...]...]\n", prog_name
);
82 fputs( "Known parsers: xdg wmconfig\n", stderr
);
86 for (i
= 1; i
< argc
; i
++)
88 if (strncmp(argv
[i
], "-parser:", 8) == 0) {
89 if (strcmp(argv
[i
] + 8, "xdg") == 0) {
91 fputs("Using parser \"xdg\"\n", stderr
);
94 } else if (strcmp(argv
[i
] + 8, "wmconfig") == 0) {
96 fputs("Using parser \"wmconfig\"\n", stderr
);
98 parse
= &parse_wmconfig
;
99 validateFilename
= &wmconfig_validate_file
;
101 fprintf(stderr
, "%s: Unknown parser \"%s\"\n", prog_name
, argv
[i
] + 8);
107 if (stat(argv
[i
], &st
) == -1) {
108 fprintf(stderr
, "%s: unable to stat \"%s\"\n", prog_name
, argv
[i
]);
109 } else if (S_ISREG(st
.st_mode
)) {
110 parse(argv
[i
], addWMMenuEntryCallback
);
111 } else if (S_ISDIR(st
.st_mode
)) {
112 nftw(argv
[i
], dirParseFunc
, 16, FTW_PHYS
);
114 fprintf(stderr
, "%s: \"%s\" is not a file or directory\n", prog_name
, argv
[i
]);
117 fprintf(stderr
, "%s: argument \"%s\" with no valid parser\n", prog_name
, argv
[i
]);
122 fprintf(stderr
, "%s: parsers failed to create a valid menu\n", prog_name
);
126 WMSortTree(menu
, menuSortFunc
);
127 WMTreeWalk(menu
, assemblePLMenuFunc
, previousDepth
, True
);
129 i
= WMGetArrayItemCount(plMenuNodes
);
130 if (i
> 2) { /* more than one submenu unprocessed is almost certainly an error */
131 fprintf(stderr
, "%s: unprocessed levels on the stack. fishy.\n", prog_name
);
133 } else if (i
> 1 ) { /* possibly the top-level attachment is not yet done */
134 WMPropList
*first
, *next
;
135 next
= WMPopFromArray(plMenuNodes
);
136 first
= WMPopFromArray(plMenuNodes
);
137 WMAddToPLArray(first
, next
);
138 WMAddToArray(plMenuNodes
, first
);
141 printf("%s\n", WMGetPropListDescription((WMPropList
*)WMGetFromArray(plMenuNodes
, 0), True
));
146 static int dirParseFunc(const char *filename
, const struct stat
*st
, int tflags
, struct FTW
*ftw
)
152 if (validateFilename
&&
153 !validateFilename(filename
, st
, tflags
, ftw
))
156 parse(filename
, addWMMenuEntryCallback
);
160 /* upon fully deducing one particular menu entry, parsers call back to this
161 * function to have said menu entry added to the wm menu. initializes wm menu
162 * with a root element if needed.
164 static void addWMMenuEntryCallback(WMMenuEntry
*aEntry
)
169 wm
= (WMMenuEntry
*)wmalloc(sizeof(WMMenuEntry
)); /* this entry */
170 at
= (WMTreeNode
*)NULL
; /* will be a child of this entry */
175 root
= (WMMenuEntry
*)wmalloc(sizeof(WMMenuEntry
));
176 root
->Name
= "Applications";
177 root
->CmdLine
= NULL
;
178 root
->SubMenu
= NULL
;
180 menu
= WMCreateTreeNode(root
);
184 at
= findPositionInMenu(aEntry
->SubMenu
);
189 wm
->Flags
= aEntry
->Flags
;
190 wm
->Name
= wstrdup(aEntry
->Name
);
191 wm
->CmdLine
= wstrdup(aEntry
->CmdLine
);
193 WMAddItemToTree(at
, wm
);
197 /* creates the proplist menu out of the abstract menu representation in `menu'.
199 static void assemblePLMenuFunc(WMTreeNode
*aNode
, void *data
)
205 wm
= (WMMenuEntry
*)WMGetDataForTreeNode(aNode
);
206 cDepth
= WMGetTreeNodeDepth(aNode
);
207 pDepth
= *(int *)data
;
209 if (pDepth
> cDepth
) { /* just ascended out of a/several submenu(s) */
210 WMPropList
*last
, *but
; /* merge the tail up to the current position */
212 for (i
= pDepth
- cDepth
; i
> 0; i
--) {
213 last
= WMPopFromArray(plMenuNodes
);
214 but
= WMPopFromArray(plMenuNodes
);
215 WMAddToPLArray(but
, last
);
216 WMAddToArray(plMenuNodes
, but
);
220 if (!wm
->CmdLine
) { /* new submenu */
221 WMAddToArray(plMenuNodes
, WMCreatePLArray(WMCreatePLString(wm
->Name
), NULL
));
222 } else { /* new menu item */
223 pl
= WMPopFromArray(plMenuNodes
);
224 if (wm
->Flags
& F_RESTART_OTHER
) { /* RESTART, somewm */
226 memset(buf
, 0, sizeof(buf
));
227 snprintf(buf
, sizeof(buf
), "%s %s", _("Restart"), wm
->Name
);
228 WMAddToPLArray(pl
, WMCreatePLArray(
229 WMCreatePLString(buf
),
230 WMCreatePLString("RESTART"),
231 WMCreatePLString(wm
->CmdLine
),
234 } else if (wm
->Flags
& F_RESTART_SELF
) {/* RESTART */
235 WMAddToPLArray(pl
, WMCreatePLArray(
236 WMCreatePLString(_("Restart Window Maker")),
237 WMCreatePLString("RESTART"),
240 } else if (wm
->Flags
& F_QUIT
) { /* EXIT */
241 WMAddToPLArray(pl
, WMCreatePLArray(
242 WMCreatePLString(_("Exit Window Maker")),
243 WMCreatePLString("EXIT"),
246 } else { /* plain simple command */
249 memset(buf
, 0, sizeof(buf
));
250 if (wm
->Flags
& F_TERMINAL
) /* XXX: quoting! */
251 snprintf(buf
, sizeof(buf
), "%s -e \"%s\"", terminal
, wm
->CmdLine
);
253 snprintf(buf
, sizeof(buf
), "%s", wm
->CmdLine
);
255 WMAddToPLArray(pl
, WMCreatePLArray(
256 WMCreatePLString(wm
->Name
),
257 WMCreatePLString("SHEXEC"),
258 WMCreatePLString(buf
),
262 WMAddToArray(plMenuNodes
, pl
);
265 *(int *)data
= cDepth
;
269 /* sort the menu tree; callback for WMSortTree()
271 static int menuSortFunc(const void *left
, const void *right
)
274 WMMenuEntry
*rightwm
;
276 leftwm
= (WMMenuEntry
*)WMGetDataForTreeNode(*(WMTreeNode
**)left
);
277 rightwm
= (WMMenuEntry
*)WMGetDataForTreeNode(*(WMTreeNode
**)right
);
280 if (!leftwm
->CmdLine
&& rightwm
->CmdLine
)
282 if (leftwm
->CmdLine
&& !rightwm
->CmdLine
)
285 /* the rest lexicographically */
286 return strcasecmp(leftwm
->Name
, rightwm
->Name
);
290 /* returns the leaf an entry with the submenu spec `submenu' attaches to.
291 * creates `submenu' path (anchored to the root) along the way.
293 static WMTreeNode
*findPositionInMenu(const char *submenu
)
297 WMTreeNode
*node
, *pnode
;
300 /* qualify submenu with "Applications/" (the root node) */
301 memset(buf
, 0, sizeof(buf
));
302 snprintf(buf
, sizeof(buf
), "Applications/%s", submenu
);
304 /* start at the root */
307 q
= strtok(buf
, "/");
310 node
= WMFindInTreeWithDepthLimit(pnode
, nodeFindSubMenuByNameFunc
, q
, 1);
312 wm
= (WMMenuEntry
*)wmalloc(sizeof(WMMenuEntry
));
314 wm
->Name
= wstrdup(q
);
317 node
= WMAddNodeToTree(pnode
, WMCreateTreeNode(wm
));
319 q
= strtok(NULL
, "/");
325 /* find node where Name = cdata and node is a submenu
327 static int nodeFindSubMenuByNameFunc(const void *item
, const void *cdata
)
331 wm
= (WMMenuEntry
*)item
;
333 if (wm
->CmdLine
) /* if it has a cmdline, it can't be a submenu */
336 return strcmp(wm
->Name
, (const char *)cdata
) == 0;