From 8d0bf427e0dd88399268b17ba1e814d52fa21fa3 Mon Sep 17 00:00:00 2001 From: Qball Cow Date: Tue, 14 Jan 2014 08:36:07 +0100 Subject: [PATCH] Sort the executable list. * Add option to check timing. --- Makefile | 8 ++++++-- simpleswitcher.c | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 522ca6e8..7bb21a59 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,20 @@ -CFLAGS?=-Wall -Wextra -Os -g3 +CFLAGS?=-Wall -Wextra -Os PREFIX?=$(DESTDIR)/usr BINDIR?=$(PREFIX)/bin MANDIR?=$(PREFIX)/share/man/man1 # Check deps. +ifeq (${DEBUG},1) +CFLAGS+=-DTIMING=1 +LDADD+=-lrt +endif PKG_CONFIG?=$(shell which pkg-config) ifeq (${PKG_CONFIG},${EMPTY}) $(error Failed to find pkg-config. Please install pkg-config) endif -LDADD?=$(shell ${PKG_CONFIG} --cflags --libs x11 xinerama xft) +LDADD+=$(shell ${PKG_CONFIG} --cflags --libs x11 xinerama xft) ifeq (${LDADD},${EMPTY}) $(error Failed to find the required dependencies: x11, xinerama, xft) diff --git a/simpleswitcher.c b/simpleswitcher.c index 28b77d2b..6bb25574 100644 --- a/simpleswitcher.c +++ b/simpleswitcher.c @@ -71,6 +71,10 @@ #define OPACITY "_NET_WM_WINDOW_OPACITY" #define I3_SOCKET_PATH_PROP "I3_SOCKET_PATH" +#ifdef TIMING +#include +#endif + static void* allocate( unsigned long bytes ) { void *ptr = malloc( bytes ); @@ -994,14 +998,23 @@ int menu( char **lines, char **input, char *prompt, int selected, Time *time ) #define FORK 1 #define NOFORK 2 - +int sort_func ( const void *a, const void *b ) +{ + const char *astr = *( const char * const * )a; + const char *bstr = *( const char * const * )b; + return strcasecmp( astr,bstr ); +} static char ** get_apps ( ) { +#ifdef TIMING + struct timespec start, stop; + clock_gettime(CLOCK_REALTIME, &start); +#endif if ( getenv( "PATH" ) == NULL ) return NULL; char *path = strdup( getenv( "PATH" ) ); char **retv = NULL; - int index = 0; + unsigned int index = 0; for ( const char *dirname = strtok( path, ":" ); dirname != NULL; dirname = strtok( NULL, ":" ) ) { DIR *dir = opendir( dirname ); @@ -1010,7 +1023,11 @@ static char ** get_apps ( ) struct dirent *dent; while ( ( dent=readdir( dir ) )!=NULL ) { - if ( dent->d_name[0] == '.' ) continue; + if ( dent->d_type != DT_REG && + dent->d_type != DT_LNK && + dent->d_type != DT_UNKNOWN ) { + continue; + } retv = realloc( retv, ( index+2 )*sizeof( char* ) ); retv[index] = strdup( dent->d_name ); @@ -1021,8 +1038,17 @@ static char ** get_apps ( ) closedir( dir ); } } - + // TODO: check this is still fast enough. (takes 1ms on laptop.) + qsort( retv,index, sizeof( char* ), sort_func ); 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; }