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(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
;
46 extern char *__progname
;
48 int main(int argc
, char **argv
)
54 plMenuNodes
= WMCreateArray(8); /* grows on demand */
55 menu
= (WMTreeNode
*)NULL
;
57 validateFilename
= NULL
;
59 /* assemblePLMenuFunc passes this around */
60 previousDepth
= (int *)wmalloc(sizeof(int));
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();
70 fprintf(stderr
, "Usage: %s -parser:<parser> fspec [fpsec...] "
71 "[-parser:<parser> fspec [fpsec...]...]\n", __progname
);
72 fputs( "Known parsers: xdg wmconfig\n", stderr
);
76 for (i
= 1; i
< argc
; i
++)
78 if (strncmp(argv
[i
], "-parser:", 8) == 0) {
79 if (strcmp(argv
[i
] + 8, "xdg") == 0) {
81 fputs("Using parser \"xdg\"\n", stderr
);
84 } else if (strcmp(argv
[i
] + 8, "wmconfig") == 0) {
86 fputs("Using parser \"wmconfig\"\n", stderr
);
88 parse
= &parse_wmconfig
;
89 validateFilename
= &wmconfig_validate_file
;
91 fprintf(stderr
, "%s: Unknown parser \"%s\"\n", __progname
, argv
[i
] + 8);
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
);
104 fprintf(stderr
, "%s: \"%s\" is not a file or directory\n", __progname
, argv
[i
]);
107 fprintf(stderr
, "%s: argument \"%s\" with no valid parser\n", __progname
, argv
[i
]);
112 fprintf(stderr
, "%s: parsers failed to create a valid menu\n", __progname
);
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
);
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
));
136 static int dirParseFunc(const char *filename
, const struct stat
*st
, int tflags
, struct FTW
*ftw
)
142 if (validateFilename
&&
143 !validateFilename(filename
, st
, tflags
, ftw
))
146 parse(filename
, addWMMenuEntryCallback
);
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
)
159 wm
= (WMMenuEntry
*)wmalloc(sizeof(WMMenuEntry
)); /* this entry */
160 at
= (WMTreeNode
*)NULL
; /* will be a child of this entry */
165 root
= (WMMenuEntry
*)wmalloc(sizeof(WMMenuEntry
));
166 root
->Name
= "Applications";
167 root
->CmdLine
= NULL
;
168 root
->SubMenu
= NULL
;
170 menu
= WMCreateTreeNode(root
);
174 at
= findPositionInMenu(wstrdup(aEntry
->SubMenu
));
179 wm
->Flags
= aEntry
->Flags
;
180 wm
->Name
= wstrdup(aEntry
->Name
);
181 wm
->CmdLine
= wstrdup(aEntry
->CmdLine
);
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
)
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 */
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 */
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
),
224 } else if (wm
->Flags
& F_RESTART_SELF
) {/* RESTART */
225 WMAddToPLArray(pl
, WMCreatePLArray(
226 WMCreatePLString(_("Restart Window Maker")),
227 WMCreatePLString("RESTART"),
230 } else if (wm
->Flags
& F_QUIT
) { /* EXIT */
231 WMAddToPLArray(pl
, WMCreatePLArray(
232 WMCreatePLString(_("Exit Window Maker")),
233 WMCreatePLString("EXIT"),
236 } else { /* plain simple command */
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
);
242 snprintf(buf
, sizeof(buf
), "%s", wm
->CmdLine
);
243 WMAddToPLArray(pl
, WMCreatePLArray(
244 WMCreatePLString(wm
->Name
),
245 WMCreatePLString("SHEXEC"),
246 WMCreatePLString(buf
),
250 WMAddToArray(plMenuNodes
, pl
);
253 *(int *)data
= cDepth
;
257 /* sort the menu tree; callback for WMSortTree()
259 static int menuSortFunc(const void *left
, const void *right
)
262 WMMenuEntry
*rightwm
;
264 leftwm
= (WMMenuEntry
*)WMGetDataForTreeNode(*(WMTreeNode
**)left
);
265 rightwm
= (WMMenuEntry
*)WMGetDataForTreeNode(*(WMTreeNode
**)right
);
268 if (!leftwm
->CmdLine
&& rightwm
->CmdLine
)
270 if (leftwm
->CmdLine
&& !rightwm
->CmdLine
)
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
)
285 WMTreeNode
*node
, *pnode
;
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 */
295 q
= strtok(buf
, "/");
298 node
= WMFindInTreeWithDepthLimit(pnode
, nodeFindSubMenuByNameFunc
, q
, 1);
300 wm
= (WMMenuEntry
*)wmalloc(sizeof(WMMenuEntry
));
302 wm
->Name
= wstrdup(q
);
305 node
= WMAddNodeToTree(pnode
, WMCreateTreeNode(wm
));
307 q
= strtok(NULL
, "/");
313 /* find node where Name = cdata and node is a submenu
315 static int nodeFindSubMenuByNameFunc(const void *item
, const void *cdata
)
319 wm
= (WMMenuEntry
*)item
;
321 if (wm
->CmdLine
) /* if it has a cmdline, it can't be a submenu */
324 return strcmp(wm
->Name
, (const char *)cdata
) == 0;