16 #define MAXOPTIONS (20)
19 #define ETCDIR "/usr/local/etc"
21 #define DEFAULTPATH (ETCDIR ":.")
23 #define BLANKS " \t\f\r\n"
24 #define SPACES " \t\f"
25 #define ENDLINE "\r\n"
30 char MenuName
[40] = "" ;
32 static char MyPixmapPath
[1024] = "" ;
33 char * PixmapPath
= MyPixmapPath
;
35 static char MyTilePath
[FILENAME_MAX
] = "" ;
36 char * TilePath
= MyTilePath
;
38 static char MyHighlightPath
[FILENAME_MAX
] = "" ;
39 char * HighlightPath
= MyHighlightPath
;
41 bool ClickOnly
= false ;
42 bool AutoScale
= true ;
43 bool HighlightBehind
= false ;
46 int Options_Argc
= 0 ;
47 char * Options_Argv
[MAXOPTIONS
] ;
49 static void Usage (void)
51 printf("Usage: wmmenu [<options>...][-- <dockoptions>...]\n");
52 printf("Normal options:\n");
53 printf("-m MENUNAME set the name of the menu file to load from ~/.wmmenu\n");
54 printf("-g WxH force width and height of tile\n");
55 printf("-l XPMFILE set the pixmap used to highlight icon under cursor\n");
56 printf("-t XPMFILE set the pixmap used as button bar background\n");
57 printf("-O click bar is only triggered by clicks on the tile, not moves\n");
58 printf("-O noautoscale disable automatic pixmap scaling to tile size\n");
59 printf("-O behind draw highlight pixmap behind icon, not above\n");
60 printf("-O hide=N set bar hiding timeout to N ms (default one)\n");
61 printf("-r ROWS set number of menu rows (default one)\n");
62 printf("-v print version information\n");
63 printf("-h print this help message\n");
65 printf(" -t, -l and '-O behind' can also be specified with the defaults file.\n");
68 static void SetGeometry (const char * val
)
70 if (sscanf (val
, "%dx%d", &TileXSize
, &TileYSize
) != 2)
71 error ("bad tile geometry \"%s\"", val
) ;
73 if (64 < TileXSize
|| TileXSize
< 0)
74 error ("incorrect tile width %d", TileXSize
) ;
76 if (64 < TileYSize
|| TileYSize
< 0)
77 error ("incorrect tile height %d", TileYSize
) ;
80 static void SetTilePath (const char * value
)
82 char *ptrToFree
= NULL
;
84 if (value
!= NULL
&& value
[0] == '!')
86 ptrToFree
= File_ReadOutputFromCommand (value
+1) ;
88 if ((value
= ptrToFree
) == NULL
)
92 sprintf (MyTilePath
, "%.*s", (int)(sizeof MyTilePath
)-1, value
) ;
94 if (ptrToFree
!= NULL
)
98 static void SetHighlightPath (const char * value
)
100 sprintf (MyHighlightPath
, "%.*s", (int)(sizeof MyHighlightPath
)-1, value
) ;
103 static void SetHideTimeout (const char * value
)
105 HideTimeout
= atoi (value
) ;
106 if (HideTimeout
<= 0)
108 warn ("hide_timeout must be a strictly positive integer") ;
111 /* no maximum -- for players */
114 static void AddPixmapPath (const char * value
)
119 curLen
= strlen (MyPixmapPath
) ;
120 szLeft
= (sizeof MyPixmapPath
) - curLen
;
122 /* don't forget to count space for ':' and ending EOS */
123 sprintf (MyPixmapPath
+curLen
, ":%.*s", szLeft
-2, value
) ;
126 static void InitDefaults (void)
128 char dftPixmapPath
[FILENAME_MAX
] ;
131 if ((home
= getenv ("HOME")) != NULL
&& home
[0] != EOS
)
133 sprintf (dftPixmapPath
, "%.*s/.wmmenu",
134 (int)(sizeof dftPixmapPath
)-10, home
) ;
138 strcpy (dftPixmapPath
, ".") ;
141 strcpy (MyPixmapPath
, dftPixmapPath
) ;
144 static void ParseDefaults (char * text
)
150 assert (text
!= NULL
) ;
152 p
+= strspn (p
, BLANKS
) ;
154 while (*p
!= EOS
) switch (*p
++)
157 p
+= strcspn (p
, ENDLINE
) ;
158 p
+= strspn (p
, BLANKS
) ;
163 p
+= strcspn (p
, SPACES
) ;
165 p
+= strspn (p
, SPACES
) ;
167 p
+= strcspn (p
, ENDLINE
) ;
169 p
+= strspn (p
, BLANKS
) ;
171 if (streq (name
, "geometry")) SetGeometry (value
) ;
173 if (streq (name
, "xpmpath"))
175 AddPixmapPath (value
) ;
178 if (streq (name
, "tile"))
180 SetTilePath (value
) ;
183 if (streq (name
, "highlight"))
185 SetHighlightPath (value
) ;
188 if (streq (name
, "highlight_behind"))
190 HighlightBehind
= true ;
193 if (streq (name
, "hide_timeout"))
195 SetHideTimeout (value
) ;
197 else error ("unknown setting \"%s\"", name
) ;
203 extern void Options_ParseDefaults (void)
205 char pathList
[1000] ;
206 char path
[FILENAME_MAX
] ;
210 if ((home
= getenv ("HOME")) != NULL
&& *home
!= EOS
)
212 sprintf (pathList
, "%s/.wmmenu:%s", home
, DEFAULTPATH
) ;
216 strcpy (pathList
, DEFAULTPATH
) ;
219 if (File_FindInPath (path
, sizeof path
, pathList
, "defaults") &&
220 (f
= fopen (path
, "r")) != NULL
)
224 text
= File_ReadAll (f
) ;
228 ParseDefaults (text
) ;
233 extern void Options_Parse (int argc
, char ** argv
)
237 assert (argv
!= NULL
) ;
241 Options_Argv
[Options_Argc
++] = MenuName
;
243 while ((opt
= getopt (argc
, argv
, "hg:l:m:r:t:vO:")) != EOF
) switch (opt
)
246 SetGeometry (optarg
) ;
250 sprintf (MenuName
, "%.*s", (int)(sizeof MenuName
)-1, optarg
) ;
254 Menu_SetNbRows (optarg
) ;
258 SetTilePath (optarg
) ;
262 sprintf (MyHighlightPath
, "%.*s",
263 (int)(sizeof MyHighlightPath
)-1, optarg
) ;
267 if (streq (optarg
, "click")) ClickOnly
= true ;
268 else if (streq (optarg
, "noautoscale")) AutoScale
= false ;
269 else if (streq (optarg
, "behind")) HighlightBehind
= true ;
270 else if (streql (optarg
, "hide=", 5)) SetHideTimeout (optarg
+5) ;
271 else printf ("ignoring unknown feature \"%s\"\n", optarg
) ;
275 printf ("wmmenu (c) F.COUTANT v%s\n", VERSION
) ;
276 exit (EXIT_SUCCESS
) ;
281 exit (EXIT_SUCCESS
) ;
286 exit (EXIT_FAILURE
) ;
290 if (MenuName
[0] == EOS
)
291 error ("no menu file name specified") ;
292 if (MyTilePath
[0] == EOS
)
293 warn ("no tile pixmap specified, using internal default") ;
295 while (optind
< argc
&& Options_Argc
< MAXOPTIONS
)
296 Options_Argv
[Options_Argc
++] = argv
[optind
++] ;
299 extern void Options_SetMenuName (const char * name
)
301 sprintf (MenuName
, "%.*s", (int)(sizeof MenuName
)-1, name
) ;