1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2024-11-25 13:55:34 -05:00
This commit is contained in:
QC 2014-01-30 10:02:01 +01:00
parent bbe4a3d330
commit 760a634731
3 changed files with 73 additions and 61 deletions

View file

@ -63,10 +63,11 @@ static char **get_dmenu ( )
int index = 0; int index = 0;
char buffer[1024]; char buffer[1024];
while(fgets(buffer, 1024, stdin) != NULL) {
retv = realloc(retv, (index+2)*sizeof(char*)); while ( fgets( buffer, 1024, stdin ) != NULL ) {
retv[index] = strdup(buffer); retv = realloc( retv, ( index+2 )*sizeof( char* ) );
retv[index][strlen(buffer)-1] = '\0'; retv[index] = strdup( buffer );
retv[index][strlen( buffer )-1] = '\0';
retv[index+1] = NULL; retv[index+1] = NULL;
index++; index++;
} }
@ -116,13 +117,14 @@ SwitcherMode dmenu_switcher_dialog ( char **input )
if ( n == -2 ) { if ( n == -2 ) {
retv = DMENU_DIALOG; retv = DMENU_DIALOG;
} else if ( n >=0 && list[n] != NULL ) { } else if ( n >=0 && list[n] != NULL ) {
fputs(list[n],stdout); fputs( list[n],stdout );
} }
for ( unsigned int i=0; list[i] != NULL; i++ ) { for ( unsigned int i=0; list[i] != NULL; i++ ) {
free( list[i] ); free( list[i] );
} }
free(list);
free( list );
return retv; return retv;
} }

View file

@ -62,7 +62,7 @@ static inline int execsh( const char *cmd ,int run_in_term )
return execlp( "/bin/sh", "sh", "-c", cmd, NULL ); return execlp( "/bin/sh", "sh", "-c", cmd, NULL );
} }
static void exec_json( const char *cmd, int run_in_term) static void exec_json( const char *cmd, int run_in_term )
{ {
if ( !cmd || !cmd[0] ) return; if ( !cmd || !cmd[0] ) return;
@ -85,7 +85,7 @@ typedef struct pmenu {
char **commands; char **commands;
} pmenu; } pmenu;
static struct json_object * read_json_file_descr(FILE *fd) static struct json_object * read_json_file_descr( FILE *fd )
{ {
struct json_object *jo = NULL; struct json_object *jo = NULL;
enum json_tokener_error err; enum json_tokener_error err;
@ -93,21 +93,23 @@ static struct json_object * read_json_file_descr(FILE *fd)
char buffer[64]; char buffer[64];
ssize_t len = 0; ssize_t len = 0;
while( (len = fread(buffer, 1, 64, fd)) > 0) { while ( ( len = fread( buffer, 1, 64, fd ) ) > 0 ) {
jo = json_tokener_parse_ex(tok, buffer, len); jo = json_tokener_parse_ex( tok, buffer, len );
if((err = json_tokener_get_error(tok)) != json_tokener_continue) if ( ( err = json_tokener_get_error( tok ) ) != json_tokener_continue ) {
{ if ( err != json_tokener_success ) {
if(err != json_tokener_success) { fprintf( stderr, "Error parsing json file: %s\n", json_tokener_error_desc( err ) );
fprintf(stderr, "Error parsing json file: %s\n", json_tokener_error_desc(err));
jo = NULL; jo = NULL;
} }
break; break;
} }
} }
// Get reference before we destroy the tokener. // Get reference before we destroy the tokener.
if(jo) json_object_get(jo); if ( jo ) json_object_get( jo );
json_tokener_free(tok);
json_tokener_free( tok );
return jo; return jo;
} }
@ -120,59 +122,64 @@ static pmenu *get_json ( )
#endif #endif
struct json_object *jo = NULL; struct json_object *jo = NULL;
if(json_input_file != NULL) {
if (json_input_file[0] == '-' && json_input_file[1] == '\0') { if ( json_input_file != NULL ) {
jo = read_json_file_descr(stdin); if ( json_input_file[0] == '-' && json_input_file[1] == '\0' ) {
} jo = read_json_file_descr( stdin );
else if (json_input_file[0] == '{' && strlen(json_input_file) > 3) { } else if ( json_input_file[0] == '{' && strlen( json_input_file ) > 3 ) {
jo = json_tokener_parse(json_input_file); jo = json_tokener_parse( json_input_file );
} } else {
else FILE *fd = fopen( json_input_file, "r" );
{
FILE *fd = fopen(json_input_file, "r"); if ( fd == NULL ) {
if(fd == NULL) { fprintf( stderr, "Failed to open file: %s: %s\n",
fprintf(stderr, "Failed to open file: %s: %s\n", json_input_file, strerror( errno ) );
json_input_file, strerror(errno));
return NULL; return NULL;
} }
jo = read_json_file_descr(fd);
fclose(fd); jo = read_json_file_descr( fd );
fclose( fd );
} }
} }
if(jo && json_object_get_type(jo) != json_type_object) { if ( jo && json_object_get_type( jo ) != json_type_object ) {
fprintf(stderr, "Json file has invalid root type.\n"); fprintf( stderr, "Json file has invalid root type.\n" );
json_object_put(jo); json_object_put( jo );
jo = NULL; jo = NULL;
} }
// Create error and exit. // Create error and exit.
if(jo == NULL ) { if ( jo == NULL ) {
return NULL; return NULL;
} }
pmenu *retv = allocate_clear(sizeof(*retv));
pmenu *retv = allocate_clear( sizeof( *retv ) );
struct json_object *jo2; struct json_object *jo2;
if(json_object_object_get_ex(jo, "prompt", &jo2)) {
retv->prompt = strdup(json_object_get_string(jo2)); if ( json_object_object_get_ex( jo, "prompt", &jo2 ) ) {
} retv->prompt = strdup( json_object_get_string( jo2 ) );
if(json_object_object_get_ex(jo, "run-in-terminal", &jo2)) {
retv->execute_in_term = json_object_get_int(jo2);
} }
if(json_object_object_get_ex(jo, "commands", &jo2)) { if ( json_object_object_get_ex( jo, "run-in-terminal", &jo2 ) ) {
json_object_object_foreach(jo2, key, child) { retv->execute_in_term = json_object_get_int( jo2 );
}
if ( json_object_object_get_ex( jo, "commands", &jo2 ) ) {
json_object_object_foreach( jo2, key, child ) {
retv->entries= realloc( retv->entries, ( retv->num_entries+2 )*sizeof( char* ) ); retv->entries= realloc( retv->entries, ( retv->num_entries+2 )*sizeof( char* ) );
retv->entries[retv->num_entries] = strdup( key); retv->entries[retv->num_entries] = strdup( key );
retv->entries[retv->num_entries+1] = NULL; retv->entries[retv->num_entries+1] = NULL;
retv->commands= realloc( retv->commands, ( retv->num_entries+2 )*sizeof( char* ) ); retv->commands= realloc( retv->commands, ( retv->num_entries+2 )*sizeof( char* ) );
retv->commands[retv->num_entries] = strdup( json_object_get_string(child)); retv->commands[retv->num_entries] = strdup( json_object_get_string( child ) );
retv->commands[retv->num_entries+1] = NULL; retv->commands[retv->num_entries+1] = NULL;
retv->num_entries++; retv->num_entries++;
} }
} }
json_object_put(jo);
json_object_put( jo );
@ -213,7 +220,7 @@ SwitcherMode json_switcher_dialog ( char **input )
return retv; return retv;
} }
if(list->num_entries > 0) { if ( list->num_entries > 0 ) {
int shift=0; int shift=0;
int n = menu( list->entries, input, list->prompt, NULL, &shift,token_match, NULL ); int n = menu( list->entries, input, list->prompt, NULL, &shift,token_match, NULL );
@ -221,20 +228,23 @@ SwitcherMode json_switcher_dialog ( char **input )
if ( n == -2 ) { if ( n == -2 ) {
retv = JSON_DIALOG; retv = JSON_DIALOG;
} else if ( n >=0 && list->commands[n] != NULL ) { } else if ( n >=0 && list->commands[n] != NULL ) {
exec_json( list->commands[n], list->execute_in_term); exec_json( list->commands[n], list->execute_in_term );
} }
} else { } else {
fprintf(stderr, "No commands found in json file\n"); fprintf( stderr, "No commands found in json file\n" );
} }
for ( unsigned int i=0; i < list->num_entries; i++ ) { for ( unsigned int i=0; i < list->num_entries; i++ ) {
free( list->entries[i] ); free( list->entries[i] );
free( list->commands[i] ); free( list->commands[i] );
} }
if(list->entries)free( list->entries ); if ( list->entries )free( list->entries );
if(list->commands)free( list->commands );
if(list->prompt)free( list->prompt ); if ( list->commands )free( list->commands );
if ( list->prompt )free( list->prompt );
return retv; return retv;
} }

View file

@ -1202,8 +1202,7 @@ void run_switcher( int fmode, SwitcherMode mode )
#endif // __QC_MODE__ #endif // __QC_MODE__
else if ( mode == JSON_DIALOG ) { else if ( mode == JSON_DIALOG ) {
retv = json_switcher_dialog ( &input ); retv = json_switcher_dialog ( &input );
} } else if ( mode == DMENU_DIALOG ) {
else if ( mode == DMENU_DIALOG ) {
retv = dmenu_switcher_dialog ( &input ); retv = dmenu_switcher_dialog ( &input );
} }
@ -1387,7 +1386,7 @@ int main( int argc, char *argv[] )
find_arg_str( argc, argv, "-term", &( config.terminal_emulator ) ); find_arg_str( argc, argv, "-term", &( config.terminal_emulator ) );
if(find_arg ( argc, argv, "-zeltak") >= 0) { if ( find_arg ( argc, argv, "-zeltak" ) >= 0 ) {
config.zeltak_mode = 1; config.zeltak_mode = 1;
} }
@ -1398,7 +1397,8 @@ int main( int argc, char *argv[] )
find_arg_int( argc, argv, "-lines",&( config.menu_lines ) ); find_arg_int( argc, argv, "-lines",&( config.menu_lines ) );
find_arg_int( argc, argv, "-loc", &( config.location ) ); find_arg_int( argc, argv, "-loc", &( config.location ) );
if( find_arg( argc, argv, "-hmode" ) >= 0) {
if ( find_arg( argc, argv, "-hmode" ) >= 0 ) {
config.wmode = HORIZONTAL; config.wmode = HORIZONTAL;
} }
@ -1432,11 +1432,11 @@ int main( int argc, char *argv[] )
run_switcher( NOFORK, MARK_DIALOG ); run_switcher( NOFORK, MARK_DIALOG );
#endif #endif
} else if ( find_arg( argc, argv, "-json" ) >= 0 ) { } else if ( find_arg( argc, argv, "-json" ) >= 0 ) {
find_arg_str( argc, argv, "-json", &json_input_file); find_arg_str( argc, argv, "-json", &json_input_file );
run_switcher( NOFORK, JSON_DIALOG ); run_switcher( NOFORK, JSON_DIALOG );
} else if ( find_arg( argc, argv, "-dmenu" ) >= 0 ) { } else if ( find_arg( argc, argv, "-dmenu" ) >= 0 ) {
find_arg_str( argc, argv, "-dmenu", &dmenu_prompt); find_arg_str( argc, argv, "-dmenu", &dmenu_prompt );
run_switcher( NOFORK, DMENU_DIALOG); run_switcher( NOFORK, DMENU_DIALOG );
} else { } else {
// Daemon mode, Listen to key presses.. // Daemon mode, Listen to key presses..