Removing duplicate code, small fixes.

This commit is contained in:
QC 2014-02-03 22:49:07 +01:00
parent 2bfc2fff4b
commit b9c192c4fe
7 changed files with 37 additions and 95 deletions

View File

@ -78,9 +78,9 @@ $(BUILD_DIR):
$(QUIET)mkdir -p $@/$(CONFIG_DIR)
# Objects depend on header files and makefile too.
$(BUILD_DIR)/%.o: %.c | Makefile $(HEADERS) $(BUILD_DIR)
$(info Compiling $^ -> $@)
$(QUIET) $(CC) $(CFLAGS) -c -o $@ $^
$(BUILD_DIR)/%.o: %.c Makefile $(HEADERS) | $(BUILD_DIR)
$(info Compiling $< -> $@)
$(QUIET) $(CC) $(CFLAGS) -c -o $@ $<
$(BUILD_DIR)/$(PROGRAM): $(OBJECTS)
$(info Linking $@)

View File

@ -104,4 +104,9 @@ typedef struct _Settings {
} Settings;
extern Settings config;
int token_match ( char **tokens, const char *input,
__attribute__( ( unused ) )int index,
__attribute__( ( unused ) )void *data );
#endif

View File

@ -27,16 +27,12 @@
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <X11/X.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <strings.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "simpleswitcher.h"
@ -51,41 +47,27 @@ static char **get_dmenu ( )
while ( fgets( buffer, 1024, stdin ) != NULL ) {
retv = realloc( retv, ( index+2 )*sizeof( char* ) );
retv[index] = strdup( buffer );
retv[index+1] = NULL;
// Filter out line-end.
if ( retv[index][strlen( buffer )-1] == '\n' )
retv[index][strlen( buffer )-1] = '\0';
retv[index+1] = NULL;
index++;
}
return retv;
}
static int token_match ( char **tokens, const char *input,
__attribute__( ( unused ) )int index,
__attribute__( ( unused ) )void *data )
{
int match = 1;
// Do a tokenized match.
if ( tokens ) for ( int j = 1; match && tokens[j]; j++ ) {
match = ( strcasestr( input, tokens[j] ) != NULL );
}
return match;
}
SwitcherMode dmenu_switcher_dialog ( char **input )
{
int shift=0;
int selected_line = 0;
SwitcherMode retv = MODE_EXIT;
// act as a launcher
char **list = get_dmenu( );
int mretv = menu( list, input, dmenu_prompt,NULL, &shift,
token_match, NULL, &selected_line );
int mretv = menu( list, input, dmenu_prompt,NULL, NULL,
token_match, NULL, &selected_line );
if ( mretv == MENU_NEXT ) {
retv = DMENU_DIALOG;
@ -100,7 +82,6 @@ SwitcherMode dmenu_switcher_dialog ( char **input )
}
if ( list != NULL ) free( list );
return retv;
}

View File

@ -42,9 +42,6 @@
#include "simpleswitcher.h"
#include "profile-dialog.h"
#ifdef TIMING
#include <time.h>
#endif
static inline int execshprofile( const char *profile )
{
@ -66,7 +63,7 @@ static pid_t exec_profile( const char *cmd )
return pid;
}
static char ** add_elements( char **retv, char *element, unsigned int *retv_index )
static inline char ** add_elements( char **retv, char *element, unsigned int *retv_index )
{
retv = realloc( retv, ( ( *retv_index )+2 )*sizeof( char* ) );
retv[( *retv_index )] = element;
@ -80,10 +77,6 @@ static char ** get_profile ( )
unsigned int retv_index = 0;
char *path;
char **retv = NULL;
#ifdef TIMING
struct timespec start, stop;
clock_gettime( CLOCK_REALTIME, &start );
#endif
if ( getenv( "HOME" ) == NULL ) return NULL;
@ -138,32 +131,9 @@ static char ** get_profile ( )
}
free( path );
#ifdef TIMING
clock_gettime( CLOCK_REALTIME, &stop );
if ( stop.tv_sec != start.tv_sec ) {
stop.tv_nsec += ( stop.tv_sec-start.tv_sec )*1e9;
}
long diff = stop.tv_nsec-start.tv_nsec;
printf( "Time elapsed: %ld us\n", diff/1000 );
#endif
return retv;
}
static int token_match ( char **tokens, const char *input,
__attribute__( ( unused ) )int index,
__attribute__( ( unused ) )void *data )
{
int match = 1;
// Do a tokenized match.
if ( tokens ) for ( int j = 1; match && tokens[j]; j++ ) {
match = ( strcasestr( input, tokens[j] ) != NULL );
}
return match;
}
SwitcherMode profile_switcher_dialog ( char **input )
{
@ -172,9 +142,8 @@ SwitcherMode profile_switcher_dialog ( char **input )
char **cmd_list = get_profile( );
if ( cmd_list == NULL ) {
cmd_list = allocate( 2*sizeof( char * ) );
cmd_list[0] = strdup( "No profiles found" );
cmd_list[1] = NULL;
unsigned int retv_index = 0;
cmd_list = add_elements( cmd_list, "No profiles found", &retv_index );
}
int shift=0;

View File

@ -135,8 +135,6 @@ static void delete_entry( const char *cmd )
unsigned int index = 0;
char **retv = NULL;
printf( "Delete entry: %s\n", cmd );
/**
* This happens in non-critical time (After launching app)
* It is allowed to be a bit slower.
@ -280,22 +278,10 @@ static char ** get_apps ( )
return retv;
}
static int token_match ( char **tokens, const char *input,
__attribute__( ( unused ) )int index,
__attribute__( ( unused ) )void *data )
{
int match = 1;
// Do a tokenized match.
if ( tokens ) for ( int j = 1; match && tokens[j]; j++ ) {
match = ( strcasestr( input, tokens[j] ) != NULL );
}
return match;
}
SwitcherMode run_switcher_dialog ( char **input )
{
int shift=0;
int selected_line = 0;
SwitcherMode retv = MODE_EXIT;
// act as a launcher
char **cmd_list = get_apps( );
@ -306,8 +292,6 @@ SwitcherMode run_switcher_dialog ( char **input )
cmd_list[1] = NULL;
}
int shift=0;
int selected_line = 0;
int mretv = menu( cmd_list, input, "$", NULL, &shift,token_match, NULL, &selected_line );
if ( mretv == MENU_NEXT ) {
@ -325,7 +309,7 @@ SwitcherMode run_switcher_dialog ( char **input )
free( cmd_list[i] );
}
free( cmd_list );
if (cmd_list != NULL) free( cmd_list );
return retv;
}

View File

@ -78,7 +78,23 @@
xdgHandle xdg_handle;
const char *cache_dir = NULL;
/**
* Shared 'token_match' function.
* Matches tokenized.
*/
int token_match ( char **tokens, const char *input,
__attribute__( ( unused ) )int index,
__attribute__( ( unused ) )void *data )
{
int match = 1;
// Do a tokenized match.
if ( tokens ) for ( int j = 1; match && tokens[j]; j++ ) {
match = ( strcasestr( input, tokens[j] ) != NULL );
}
return match;
}
void* allocate( unsigned long bytes )
{
@ -1239,6 +1255,7 @@ void run_switcher( int fmode, SwitcherMode mode )
} else if ( mode == SSH_DIALOG ) {
retv = ssh_switcher_dialog( &input );
}
#ifdef __QC_MODE__
else if ( mode == PROFILE_DIALOG ) {
retv = profile_switcher_dialog ( &input );

View File

@ -278,20 +278,6 @@ static char ** get_ssh ( )
return retv;
}
static int token_match ( char **tokens, const char *input,
__attribute__( ( unused ) )int index,
__attribute__( ( unused ) )void *data )
{
int match = 1;
// Do a tokenized match.
if ( tokens ) for ( int j = 1; match && tokens[j]; j++ ) {
match = ( strcasestr( input, tokens[j] ) != NULL );
}
return match;
}
SwitcherMode ssh_switcher_dialog ( char **input )
{
SwitcherMode retv = MODE_EXIT;
@ -324,7 +310,7 @@ SwitcherMode ssh_switcher_dialog ( char **input )
free( cmd_list[i] );
}
free( cmd_list );
if(cmd_list != NULL) free( cmd_list );
return retv;
}