3 * aka "2wm" - too weak menu, two week menu, akin to twm
5 * Craig wants a super minimal menu ASAP before launch, so lets see what I can put together in 2 weeks with not much
6 * free time ;) I'd like to do a fuller ('tiny', but with plugin support and a decent expansion and customizing design..)
11 /* mmwrapper -- we probably want the menu to exeunt-with-alarums when running applications, to minimize the memory and
12 * performance footprint; it could be running in-X or atop a desktop-env, or purely on SDL or framebuffer with nothing..
13 * so wrapper will actually do the running, and be tiny.
14 * Likewise, if the menu proper dies, the wrapper can fire it up again.
15 * A side effect is, people could use the wrappers abilities, and slap another cruddy menu on top of it .. yay for pure
16 * curses based or lynx-based menus ;)
19 /* mmwrapper's lifecycle:
20 * 1) launch 'frontend' (mmmenu, could be others)
21 * 2) when it exits, pick up the output...
22 * a) either a command (quit, run some app, pick new frontend)
23 * b) or a crash, in which case, back to (1)
24 * 3) execute command, if any
34 #include <sys/types.h>
38 #include "pnd_logger.h"
40 #include "mmwrapcmd.h"
42 unsigned char g_daemon_mode = 0;
43 char *g_frontend = NULL;
47 pndn_rem, // will set default log level to here, so 'debug' is omitted
51 } pndnotify_loglevels_e;
53 int main ( int argc, char *argv[] ) {
54 int logall = -1; // -1 means normal logging rules; >=0 means log all!
57 // pull conf, determine frontend; alternate is to check command line.
59 // boilerplate stuff from pndnotifyd and pndevmapperd
61 /* iterate across args
63 for ( i = 1; i < argc; i++ ) {
65 if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
66 //printf ( "Going daemon mode. Silent running.\n" );
68 } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'l' ) {
70 if ( isdigit ( argv [ i ][ 2 ] ) ) {
71 unsigned char x = atoi ( argv [ i ] + 2 );
81 } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'f' && argv [ i ][ 2 ] != '\0' )
83 g_frontend = argv [ i ] + 2;
85 //printf ( "Unknown: %s\n", argv [ i ] );
86 printf ( "%s [-l[##]] [-d] [-fFRONTENDPATH]\n", argv [ 0 ] );
87 printf ( "-d\tDaemon mode; detach from terminal, chdir to /tmp, suppress output. Optional.\n" );
88 printf ( "-l#\tLog-it; -l is 0-and-up (or all), and -l2 means 2-and-up (not all); l[0-3] for now. Log goes to /tmp/mmwrapper.log\n" );
89 printf ( "-f\tFull path of frontend to run\n" );
97 pnd_log_set_pretext ( "mmwrapper" );
98 pnd_log_set_flush ( 1 );
100 if ( logall == -1 ) {
101 // standard logging; non-daemon versus daemon
103 if ( g_daemon_mode ) {
106 pnd_log_set_filter ( pndn_rem );
107 //pnd_log_set_filter ( pndn_debug );
114 f = fopen ( "/tmp/mmwrapper.log", "w" );
117 pnd_log_set_filter ( logall );
118 pnd_log_to_stream ( f );
119 pnd_log ( pndn_rem, "logall mode - logging to /tmp/mmwrapper.log\n" );
122 if ( logall == pndn_debug ) {
123 pnd_log_set_buried_logging ( 1 ); // log the shit out of it
124 pnd_log ( pndn_rem, "logall mode 0 - turned on buried logging\n" );
129 pnd_log ( pndn_rem, "%s built %s %s", argv [ 0 ], __DATE__, __TIME__ );
131 pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );
133 pnd_log ( pndn_rem, "Frontend is %s", g_frontend );
135 if ( g_daemon_mode ) {
137 // set a CWD somewhere else
142 // detach from terminal
143 if ( ( i = fork() ) < 0 ) {
144 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
148 exit ( 0 ); // exit parent
153 umask ( 022 ); // emitted files can be rwxr-xr-x
158 if ( ! g_frontend ) {
159 pnd_log ( pndn_error, "ERROR: No frontend specified!\n" );
167 // wait for something to come back when it exits
169 char cmdbuf [ 1024 ];
176 bzero ( cmdbuf, 1024 );
180 pnd_log ( pndn_debug, "Invoking frontend: %s\n", g_frontend );
182 FILE *fe = popen ( g_frontend, "r" );
183 while ( fgets ( cmdbuf, 1000, fe ) ) {
184 if ( strstr ( cmdbuf, MM_WATCHIT ) ) {
187 pnd_log ( pndn_debug, "Junk: %s", cmdbuf );
191 if ( ( c = strchr ( cmdbuf, '\n' ) ) ) {
192 *c = '\0'; // truncate trailing newline
195 if ( ( c = strchr ( cmdbuf, ' ' ) ) ) {
200 pnd_log ( pndn_debug, "Command from frontend: '%s' args: '%s'\n", cmdbuf, args ? args : "none" );
203 if ( strcasecmp ( cmdbuf, MM_QUIT ) == 0 ) {
205 pnd_log ( pndn_rem, "Frontend requests shutdown.\n" );
207 } else if ( strcasecmp ( cmdbuf, MM_RUN ) == 0 ) {
208 // shell out and run it
209 int retval = system ( args );
212 pnd_log ( pndn_error, "ERROR: Couldn't invoke application specified by frontend: '%s'\n", args );
214 pnd_log ( pndn_rem, "Invoked application returned %d\n", WEXITSTATUS(retval) );
218 pnd_log ( pndn_rem, "Unexpected exit of frontend; restarting it!\n" );
219 // next time around the loop