20 #define BLANKS " \t\f\r\n"
21 #define SPACES " \t\f"
22 #define ENDLINE "\r\n"
24 static int NbEntries
= 0 ;
26 static const char * Pixmaps
[MAXENTRIES
] ;
27 static const char * Commands
[MAXENTRIES
] ;
28 static char * MenuFileText
= NULL
;
29 static char MenuPath
[FILENAME_MAX
] = "" ;
30 static time_t MenuAge
= 0 ;
32 static void ParseMenu (void)
36 assert (MenuFileText
!= NULL
) ;
38 p
+= strspn (p
, BLANKS
) ;
43 while (NbEntries
< MAXENTRIES
&& *p
!= EOS
) switch (*p
++)
46 p
+= strcspn (p
, ENDLINE
) ;
47 p
+= strspn (p
, BLANKS
) ;
51 Pixmaps
[NbEntries
] = p
;
52 p
+= strcspn (p
, "\"") ;
54 p
+= strspn (p
, SPACES
) ;
58 if (Pixmaps
[NbEntries
] == NULL
)
59 warn ("entry #%d has no pixmap", NbEntries
) ;
60 else Commands
[NbEntries
++] = p
-1 ;
62 p
+= strcspn (p
, ENDLINE
) ;
64 p
+= strspn (p
, BLANKS
) ;
66 Commands
[NbEntries
] = NULL
;
67 Pixmaps
[NbEntries
] = NULL
;
72 warn ("too many entries") ;
75 extern void Menu_LoadFromFile (const char * name
)
77 char path
[FILENAME_MAX
] ;
82 assert (name
!= NULL
) ;
84 if (strchr (name
, '/') == NULL
&& (home
= getenv ("HOME")) != NULL
&&
86 sprintf (path
, "%s/.wmmenu/%s", home
, name
) ;
87 else sprintf (path
, "%s", name
) ;
89 if ((f
= fopen (path
, "r")) == NULL
)
90 error ("can't open %s", path
) ;
93 if (fstat (fileno (f
), & finfo
) == 0)
95 MenuAge
= finfo
.st_mtime
;
98 sprintf (MenuPath
, "%.*s", (int)(sizeof MenuPath
)-1, path
) ;
100 if (MenuFileText
!= NULL
) free (MenuFileText
) ;
101 MenuFileText
= File_ReadAll (f
) ;
109 extern int Menu_GetNbRows(void)
115 extern void Menu_SetNbRows (const char *s
)
120 if (h
> 0) Rows
= h
;
123 extern int Menu_GetNbColumns (void)
125 assert (NbEntries
> 1 && Rows
> 0) ;
127 Remove 1 entry used for header, then apply the formula:
128 UNITS = int (floor ((USED - 1) / PERUNIT)) + 1
129 (USED is NbEntries - 1)
131 return ((NbEntries
- 2) / Rows
) + 1 ;
134 extern int Menu_GetNbEntries (void)
136 assert (NbEntries
> 1) ;
140 extern const char * Menu_GetEntryPixmap (int i
)
142 assert (0 <= i
&& i
< NbEntries
-1) ;
143 return Pixmaps
[i
+1] ;
146 extern const char * Menu_GetEntryCommand (int i
)
148 assert (0 <= i
&& i
< NbEntries
-1) ;
149 return Commands
[i
+1] ;
152 extern const char * Menu_GetPixmap (void)
154 assert (0 < NbEntries
) ;
158 extern const char * Menu_GetTitle (void)
160 assert (0 < NbEntries
) ;
164 extern int Menu_HasChanged (void)
168 if (stat (MenuPath
, & finfo
) == 0 && finfo
.st_mtime
> MenuAge
)
170 return 1 ; /* should reload */
174 return 0 ; /* don't try to reload */
178 extern void Menu_Reload (void)
183 if ((f
= fopen (MenuPath
, "r")) != NULL
)
185 if (fstat (fileno (f
), & finfo
) == 0)
187 MenuAge
= finfo
.st_mtime
;
190 if (MenuFileText
!= NULL
) free (MenuFileText
) ;
191 MenuFileText
= File_ReadAll (f
) ;