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.
22 (_XOPEN_SOURCE && _XOPEN_SOURCE < 500) || \
24 #define _XOPEN_SOURCE 500 /* nftw */
27 #include <sys/types.h>
38 #include "wmmenugen.h"
40 static void addWMMenuEntryCallback(WMMenuEntry
*aEntry
);
41 static void assemblePLMenuFunc(WMTreeNode
*aNode
, void *data
);
42 static int dirParseFunc(const char *filename
, const struct stat
*st
, int tflags
, struct FTW
*ftw
);
43 static int menuSortFunc(const void *left
, const void *right
);
44 static int nodeFindSubMenuByNameFunc(const void *item
, const void *cdata
);
45 static WMTreeNode
*findPositionInMenu(char *submenu
);
46 static void (*parse
)(const char *file
, void (*addWMMenuEntryCallback
)(WMMenuEntry
*aEntry
));
47 static Bool (*validateFilename
)(const char *filename
, const struct stat
*st
, int tflags
, struct FTW
*ftw
);
49 static WMArray
*plMenuNodes
;
52 extern char *__progname
;
54 int main(int argc
, char **argv
)
60 plMenuNodes
= WMCreateArray(8); /* grows on demand */
61 menu
= (WMTreeNode
*)NULL
;
63 validateFilename
= NULL
;
65 /* assemblePLMenuFunc passes this around */
66 previousDepth
= (int *)wmalloc(sizeof(int));
69 /* currently this is used only by the xdg parser, but it might be useful
70 * in the future localizing other menus, so it won't hurt to have it here.
72 parse_locale(NULL
, &env_lang
, &env_ctry
, &env_enc
, &env_mod
);
73 terminal
= find_terminal_emulator();
76 fprintf(stderr
, "Usage: %s -parser:<parser> fspec [fpsec...] "
77 "[-parser:<parser> fspec [fpsec...]...]\n", __progname
);
78 fputs( "Known parsers: xdg wmconfig\n", stderr
);
82 for (i
= 1; i
< argc
; i
++)
84 if (strncmp(argv
[i
], "-parser:", 8) == 0) {
85 if (strcmp(argv
[i
] + 8, "xdg") == 0) {
87 fputs("Using parser \"xdg\"\n", stderr
);
90 } else if (strcmp(argv
[i
] + 8, "wmconfig") == 0) {
92 fputs("Using parser \"wmconfig\"\n", stderr
);
94 parse
= &parse_wmconfig
;
95 validateFilename
= &wmconfig_validate_file
;
97 fprintf(stderr
, "%s: Unknown parser \"%s\"\n", __progname
, argv
[i
] + 8);
103 if (stat(argv
[i
], &st
) == -1) {
104 fprintf(stderr
, "%s: unable to stat \"%s\"\n", __progname
, argv
[i
]);
105 } else if (S_ISREG(st
.st_mode
)) {
106 parse(argv
[i
], addWMMenuEntryCallback
);
107 } else if (S_ISDIR(st
.st_mode
)) {
108 nftw(argv
[i
], dirParseFunc
, 16, FTW_PHYS
);
110 fprintf(stderr
, "%s: \"%s\" is not a file or directory\n", __progname
, argv
[i
]);
113 fprintf(stderr
, "%s: argument \"%s\" with no valid parser\n", __progname
, argv
[i
]);
118 fprintf(stderr
, "%s: parsers failed to create a valid menu\n", __progname
);
122 WMSortTree(menu
, menuSortFunc
);
123 WMTreeWalk(menu
, assemblePLMenuFunc
, previousDepth
, True
);
125 i
= WMGetArrayItemCount(plMenuNodes
);
126 if (i
> 2) { /* more than one submenu unprocessed is almost certainly an error */
127 fprintf(stderr
, "%s: unprocessed levels on the stack. fishy.\n", __progname
);
129 } else if (i
> 1 ) { /* possibly the top-level attachment is not yet done */
130 WMPropList
*first
, *next
;
131 next
= WMPopFromArray(plMenuNodes
);
132 first
= WMPopFromArray(plMenuNodes
);
133 WMAddToPLArray(first
, next
);
134 WMAddToArray(plMenuNodes
, first
);
137 printf("%s\n", WMGetPropListDescription((WMPropList
*)WMGetFromArray(plMenuNodes
, 0), True
));
142 static int dirParseFunc(const char *filename
, const struct stat
*st
, int tflags
, struct FTW
*ftw
)
148 if (validateFilename
&&
149 !validateFilename(filename
, st
, tflags
, ftw
))
152 parse(filename
, addWMMenuEntryCallback
);
156 /* upon fully deducing one particular menu entry, parsers call back to this
157 * function to have said menu entry added to the wm menu. initializes wm menu
158 * with a root element if needed.
160 static void addWMMenuEntryCallback(WMMenuEntry
*aEntry
)
165 wm
= (WMMenuEntry
*)wmalloc(sizeof(WMMenuEntry
)); /* this entry */
166 at
= (WMTreeNode
*)NULL
; /* will be a child of this entry */
171 root
= (WMMenuEntry
*)wmalloc(sizeof(WMMenuEntry
));
172 root
->Name
= "Applications";
173 root
->CmdLine
= NULL
;
174 root
->SubMenu
= NULL
;
176 menu
= WMCreateTreeNode(root
);
180 at
= findPositionInMenu(wstrdup(aEntry
->SubMenu
));
185 wm
->Flags
= aEntry
->Flags
;
186 wm
->Name
= wstrdup(aEntry
->Name
);
187 wm
->CmdLine
= wstrdup(aEntry
->CmdLine
);
189 WMAddItemToTree(at
, wm
);
193 /* creates the proplist menu out of the abstract menu representation in `menu'.
195 static void assemblePLMenuFunc(WMTreeNode
*aNode
, void *data
)
201 wm
= (WMMenuEntry
*)WMGetDataForTreeNode(aNode
);
202 cDepth
= WMGetTreeNodeDepth(aNode
);
203 pDepth
= *(int *)data
;
205 if (pDepth
> cDepth
) { /* just ascended out of a/several submenu(s) */
206 WMPropList
*last
, *but
; /* merge the tail up to the current position */
208 for (i
= pDepth
- cDepth
; i
> 0; i
--) {
209 last
= WMPopFromArray(plMenuNodes
);
210 but
= WMPopFromArray(plMenuNodes
);
211 WMAddToPLArray(but
, last
);
212 WMAddToArray(plMenuNodes
, but
);
216 if (!wm
->CmdLine
) { /* new submenu */
217 WMAddToArray(plMenuNodes
, WMCreatePLArray(WMCreatePLString(wm
->Name
), NULL
));
218 } else { /* new menu item */
219 pl
= WMPopFromArray(plMenuNodes
);
220 if (wm
->Flags
& F_RESTART_OTHER
) { /* RESTART, somewm */
222 memset(buf
, 0, sizeof(buf
));
223 snprintf(buf
, sizeof(buf
), "%s %s", _("Restart"), wm
->Name
);
224 WMAddToPLArray(pl
, WMCreatePLArray(
225 WMCreatePLString(buf
),
226 WMCreatePLString("RESTART"),
227 WMCreatePLString(wm
->CmdLine
),
230 } else if (wm
->Flags
& F_RESTART_SELF
) {/* RESTART */
231 WMAddToPLArray(pl
, WMCreatePLArray(
232 WMCreatePLString(_("Restart Window Maker")),
233 WMCreatePLString("RESTART"),
236 } else if (wm
->Flags
& F_QUIT
) { /* EXIT */
237 WMAddToPLArray(pl
, WMCreatePLArray(
238 WMCreatePLString(_("Exit Window Maker")),
239 WMCreatePLString("EXIT"),
242 } else { /* plain simple command */
244 memset(buf
, 0, sizeof(buf
));
245 if (wm
->Flags
& F_TERMINAL
) /* XXX: quoting! */
246 snprintf(buf
, sizeof(buf
), "%s -e \"%s\"", terminal
, wm
->CmdLine
);
248 snprintf(buf
, sizeof(buf
), "%s", wm
->CmdLine
);
249 WMAddToPLArray(pl
, WMCreatePLArray(
250 WMCreatePLString(wm
->Name
),
251 WMCreatePLString("SHEXEC"),
252 WMCreatePLString(buf
),
256 WMAddToArray(plMenuNodes
, pl
);
259 *(int *)data
= cDepth
;
263 /* sort the menu tree; callback for WMSortTree()
265 static int menuSortFunc(const void *left
, const void *right
)
268 WMMenuEntry
*rightwm
;
270 leftwm
= (WMMenuEntry
*)WMGetDataForTreeNode(*(WMTreeNode
**)left
);
271 rightwm
= (WMMenuEntry
*)WMGetDataForTreeNode(*(WMTreeNode
**)right
);
274 if (!leftwm
->CmdLine
&& rightwm
->CmdLine
)
276 if (leftwm
->CmdLine
&& !rightwm
->CmdLine
)
279 /* the rest lexicographically */
280 return strcasecmp(leftwm
->Name
, rightwm
->Name
);
284 /* returns the leaf an entry with the submenu spec `submenu' attaches to.
285 * creates `submenu' path (anchored to the root) along the way.
287 static WMTreeNode
*findPositionInMenu(char *submenu
)
291 WMTreeNode
*node
, *pnode
;
294 /* qualify submenu with "Applications/" (the root node) */
295 memset(buf
, 0, sizeof(buf
));
296 snprintf(buf
, sizeof(buf
), "Applications/%s", submenu
);
298 /* start at the root */
301 q
= strtok(buf
, "/");
304 node
= WMFindInTreeWithDepthLimit(pnode
, nodeFindSubMenuByNameFunc
, q
, 1);
306 wm
= (WMMenuEntry
*)wmalloc(sizeof(WMMenuEntry
));
308 wm
->Name
= wstrdup(q
);
311 node
= WMAddNodeToTree(pnode
, WMCreateTreeNode(wm
));
313 q
= strtok(NULL
, "/");
319 /* find node where Name = cdata and node is a submenu
321 static int nodeFindSubMenuByNameFunc(const void *item
, const void *cdata
)
325 wm
= (WMMenuEntry
*)item
;
327 if (wm
->CmdLine
) /* if it has a cmdline, it can't be a submenu */
330 return strcmp(wm
->Name
, (const char *)cdata
) == 0;