Indent o mania.

This commit is contained in:
QC 2014-03-22 21:04:19 +01:00
parent 4d37cf35dc
commit c87312be36
10 changed files with 1906 additions and 1328 deletions

View File

@ -14,7 +14,8 @@ extern const char *cache_dir;
extern char *i3_socket_path;
#endif
typedef enum {
typedef enum
{
WINDOW_SWITCHER,
RUN_DIALOG,
SSH_DIALOG,
@ -24,13 +25,13 @@ typedef enum {
NEXT_DIALOG
} SwitcherMode;
typedef enum {
typedef enum
{
MENU_OK = 0,
MENU_CANCEL = -1,
MENU_NEXT = -2,
MENU_CUSTOM_INPUT = -3,
MENU_ENTRY_DELETE = -4
} MenuReturn;
@ -51,7 +52,8 @@ void* reallocate( void *ptr, unsigned long bytes );
void catch_exit ( __attribute__( ( unused ) ) int sig );
typedef enum _WindowLocation {
typedef enum _WindowLocation
{
WL_CENTER = 0,
WL_NORTH_WEST = 1,
WL_NORTH = 2,
@ -63,7 +65,8 @@ typedef enum _WindowLocation {
WL_WEST = 8
} WindowLocation;
typedef enum {
typedef enum
{
VERTICAL = 0,
HORIZONTAL = 1
} WindowMode;
@ -71,7 +74,8 @@ typedef enum {
* Settings
*/
typedef struct _Settings {
typedef struct _Settings
{
// Window settings
unsigned int window_opacity;
// Menu settings

View File

@ -1,7 +1,8 @@
#ifndef __TEXTBOX_H__
#define __TEXTBOX_H__
typedef struct {
typedef struct
{
unsigned long flags;
Window window, parent;
short x, y, w, h;
@ -15,7 +16,8 @@ typedef struct {
} textbox;
typedef enum {
typedef enum
{
TB_AUTOHEIGHT = 1 << 0,
TB_AUTOWIDTH = 1 << 1,
TB_LEFT = 1 << 16,

View File

@ -44,14 +44,17 @@ static char **get_dmenu ( )
char **retv = NULL;
int index = 0;
while ( fgets( buffer, 1024, stdin ) != NULL ) {
while ( fgets ( buffer, 1024, stdin ) != NULL )
{
retv = reallocate ( 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';
}
index++;
}
@ -69,19 +72,28 @@ SwitcherMode dmenu_switcher_dialog ( char **input )
int mretv = menu ( list, input, dmenu_prompt, NULL, NULL,
token_match, NULL, &selected_line );
if ( mretv == MENU_NEXT ) {
if ( mretv == MENU_NEXT )
{
retv = DMENU_DIALOG;
} else if ( mretv == MENU_OK && list[selected_line] != NULL ) {
}
else if ( mretv == MENU_OK && list[selected_line] != NULL )
{
fputs ( list[selected_line], stdout );
} else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' ) {
}
else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' )
{
fputs ( *input, stdout );
}
for ( unsigned int i=0; list != NULL && list[i] != NULL; i++ ) {
for ( unsigned int i = 0; list != NULL && list[i] != NULL; i++ )
{
free ( list[i] );
}
if ( list != NULL ) free( list );
if ( list != NULL )
{
free ( list );
}
return retv;
}

File diff suppressed because it is too large Load Diff

View File

@ -51,12 +51,15 @@ static inline int execsh( const char *cmd ,int run_in_term )
{
// use sh for args parsing
if ( run_in_term )
{
return execlp ( config.terminal_emulator, config.terminal_emulator, "-e", "sh", "-c", cmd, NULL );
}
return execlp ( "/bin/sh", "sh", "-c", cmd, NULL );
}
typedef struct _element{
typedef struct _element
{
long int index;
char name[1024];
}element;
@ -69,12 +72,16 @@ static int element_sort_func(const void *ea,const void *eb)
// execute sub-process
static pid_t exec_cmd ( const char *cmd, int run_in_term )
{
if ( !cmd || !cmd[0] ) return -1;
if ( !cmd || !cmd[0] )
{
return -1;
}
signal ( SIGCHLD, catch_exit );
pid_t pid = fork ();
if ( !pid ) {
if ( !pid )
{
setsid ();
execsh ( cmd, run_in_term );
exit ( EXIT_FAILURE );
@ -93,10 +100,15 @@ static pid_t exec_cmd( const char *cmd, int run_in_term )
snprintf ( path, path_length, "%s/%s", cache_dir, RUN_CACHE_FILE );
FILE *fd = fopen ( path, "r" );
if ( fd != NULL ) {
if ( fd != NULL )
{
char buffer[1024];
while ( fgets( buffer,1024,fd ) != NULL ) {
if(strlen(buffer) == 0) continue;
while ( fgets ( buffer, 1024, fd ) != NULL )
{
if ( strlen ( buffer ) == 0 )
{
continue;
}
retv = reallocate ( retv, ( index + 2 ) * sizeof ( element* ) );
retv[index] = allocate ( sizeof ( element ) );
buffer[strlen ( buffer ) - 1] = '\0';
@ -105,7 +117,8 @@ static pid_t exec_cmd( const char *cmd, int run_in_term )
snprintf ( retv[index]->name, 1024, "%s", start + 1 );
retv[index + 1] = NULL;
if ( strcasecmp( retv[index]->name, cmd ) == 0 ) {
if ( strcasecmp ( retv[index]->name, cmd ) == 0 )
{
curr = index;
}
index++;
@ -114,13 +127,16 @@ static pid_t exec_cmd( const char *cmd, int run_in_term )
fclose ( fd );
}
if(curr < 0) {
if ( curr < 0 )
{
retv = reallocate ( retv, ( index + 2 ) * sizeof ( element* ) );
retv[index] = allocate ( sizeof ( element ) );
retv[index]->index = 1;
snprintf ( retv[index]->name, 1024, "%s", cmd );
index++;
}else {
}
else
{
retv[curr]->index++;
}
// Sort the list.
@ -131,10 +147,12 @@ static pid_t exec_cmd( const char *cmd, int run_in_term )
*/
fd = fopen ( path, "w" );
if ( fd ) {
for ( int i = 0; i < ( int )index && i < 20; i++ ) {
if(retv[i]->name && retv[i]->name[0] != '\0') {
if ( fd )
{
for ( int i = 0; i < ( int ) index && i < 20; i++ )
{
if ( retv[i]->name && retv[i]->name[0] != '\0' )
{
fprintf ( fd, "%ld %s\n",
retv[i]->index,
retv[i]->name
@ -145,7 +163,8 @@ static pid_t exec_cmd( const char *cmd, int run_in_term )
fclose ( fd );
}
for ( int i=0; retv != NULL && retv[i] != NULL; i++ ) {
for ( int i = 0; retv != NULL && retv[i] != NULL; i++ )
{
free ( retv[i] );
}
@ -171,10 +190,15 @@ static void delete_entry( const char *cmd )
sprintf ( path, "%s/%s", cache_dir, RUN_CACHE_FILE );
FILE *fd = fopen ( path, "r" );
if ( fd != NULL ) {
if ( fd != NULL )
{
char buffer[1024];
while ( fgets( buffer,1024,fd ) != NULL ) {
if(strlen(buffer) == 0) continue;
while ( fgets ( buffer, 1024, fd ) != NULL )
{
if ( strlen ( buffer ) == 0 )
{
continue;
}
retv = reallocate ( retv, ( index + 2 ) * sizeof ( element* ) );
retv[index] = allocate ( sizeof ( element ) );
buffer[strlen ( buffer ) - 1] = '\0';
@ -183,7 +207,8 @@ static void delete_entry( const char *cmd )
snprintf ( retv[index]->name, 1024, "%s", start + 1 );
retv[index + 1] = NULL;
if ( strcasecmp( retv[index]->name, cmd ) == 0 ) {
if ( strcasecmp ( retv[index]->name, cmd ) == 0 )
{
curr = index;
}
index++;
@ -197,10 +222,14 @@ static void delete_entry( const char *cmd )
*/
fd = fopen ( path, "w" );
if ( fd ) {
for ( int i = 0; i < ( int )index && i < 20; i++ ) {
if ( i != curr ) {
if(retv[i]->name && retv[i]->name[0] != '\0') {
if ( fd )
{
for ( int i = 0; i < ( int ) index && i < 20; i++ )
{
if ( i != curr )
{
if ( retv[i]->name && retv[i]->name[0] != '\0' )
{
fprintf ( fd, "%ld %s\n",
retv[i]->index,
retv[i]->name
@ -212,14 +241,14 @@ static void delete_entry( const char *cmd )
fclose ( fd );
}
for ( int i=0; retv != NULL && retv[i] != NULL; i++ ) {
for ( int i = 0; retv != NULL && retv[i] != NULL; i++ )
{
free ( retv[i] );
}
free ( retv );
free ( path );
}
static int sort_func ( const void *a, const void *b )
{
@ -238,22 +267,33 @@ static char ** get_apps ( )
clock_gettime ( CLOCK_REALTIME, &start );
#endif
if ( getenv( "PATH" ) == NULL ) return NULL;
if ( getenv ( "PATH" ) == NULL )
{
return NULL;
}
path = allocate ( strlen ( cache_dir ) + strlen ( RUN_CACHE_FILE ) + 3 );
sprintf ( path, "%s/%s", cache_dir, RUN_CACHE_FILE );
FILE *fd = fopen ( path, "r" );
if ( fd != NULL ) {
if ( fd != NULL )
{
char buffer[1024];
while ( fgets( buffer,1024,fd ) != NULL ) {
if(strlen(buffer) == 0) continue;
while ( fgets ( buffer, 1024, fd ) != NULL )
{
if ( strlen ( buffer ) == 0 )
{
continue;
}
buffer[strlen ( buffer ) - 1] = '\0';
char *start = NULL;
// Don't use result.
strtol ( buffer, &start, 10 );
if(start == NULL) continue;
if ( start == NULL )
{
continue;
}
retv = reallocate ( retv, ( index + 2 ) * sizeof ( char* ) );
retv[index] = strdup ( start + 1 );
retv[index + 1] = NULL;
@ -269,16 +309,20 @@ static char ** get_apps ( )
path = strdup ( getenv ( "PATH" ) );
for ( const char *dirname = strtok( path, ":" ); dirname != NULL; dirname = strtok( NULL, ":" ) ) {
for ( const char *dirname = strtok ( path, ":" ); dirname != NULL; dirname = strtok ( NULL, ":" ) )
{
DIR *dir = opendir ( dirname );
if ( dir != NULL ) {
if ( dir != NULL )
{
struct dirent *dent;
while ( ( dent=readdir( dir ) )!=NULL ) {
while ( ( dent = readdir ( dir ) ) != NULL )
{
if ( dent->d_type != DT_REG &&
dent->d_type != DT_LNK &&
dent->d_type != DT_UNKNOWN ) {
dent->d_type != DT_UNKNOWN )
{
continue;
}
@ -286,11 +330,18 @@ static char ** get_apps ( )
// This is a nice little penalty, but doable? time will tell.
// given num_favorites is max 25.
for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) {
if ( strcasecmp( dent->d_name, retv[j] ) == 0 ) found = 1;
for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ )
{
if ( strcasecmp ( dent->d_name, retv[j] ) == 0 )
{
found = 1;
}
}
if ( found == 1 ) continue;
if ( found == 1 )
{
continue;
}
retv = reallocate ( retv, ( index + 2 ) * sizeof ( char* ) );
retv[index] = strdup ( dent->d_name );
@ -303,14 +354,16 @@ static char ** get_apps ( )
}
// TODO: check this is still fast enough. (takes 1ms on laptop.)
if(index > num_favorites) {
if ( index > num_favorites )
{
qsort ( &retv[num_favorites], index - num_favorites, sizeof ( char* ), sort_func );
}
free ( path );
#ifdef TIMING
clock_gettime ( CLOCK_REALTIME, &stop );
if ( stop.tv_sec != start.tv_sec ) {
if ( stop.tv_sec != start.tv_sec )
{
stop.tv_nsec += ( stop.tv_sec - start.tv_sec ) * 1e9;
}
@ -328,7 +381,8 @@ SwitcherMode run_switcher_dialog ( char **input )
// act as a launcher
char **cmd_list = get_apps ( );
if ( cmd_list == NULL ) {
if ( cmd_list == NULL )
{
cmd_list = allocate ( 2 * sizeof ( char * ) );
cmd_list[0] = strdup ( "No applications found" );
cmd_list[1] = NULL;
@ -336,22 +390,33 @@ SwitcherMode run_switcher_dialog ( char **input )
int mretv = menu ( cmd_list, input, "$", NULL, &shift, token_match, NULL, &selected_line );
if ( mretv == MENU_NEXT ) {
if ( mretv == MENU_NEXT )
{
retv = NEXT_DIALOG;
} else if ( mretv == MENU_OK && cmd_list[selected_line] != NULL ) {
}
else if ( mretv == MENU_OK && cmd_list[selected_line] != NULL )
{
exec_cmd ( cmd_list[selected_line], shift );
} else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' ) {
}
else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' )
{
exec_cmd ( *input, shift );
} else if ( mretv == MENU_ENTRY_DELETE && cmd_list[selected_line] ) {
}
else if ( mretv == MENU_ENTRY_DELETE && cmd_list[selected_line] )
{
delete_entry ( cmd_list[selected_line] );
retv = RUN_DIALOG;
}
for ( int i=0; cmd_list != NULL && cmd_list[i] != NULL; i++ ) {
for ( int i = 0; cmd_list != NULL && cmd_list[i] != NULL; i++ )
{
free ( cmd_list[i] );
}
if ( cmd_list != NULL ) free( cmd_list );
if ( cmd_list != NULL )
{
free ( cmd_list );
}
return retv;
}

View File

@ -54,12 +54,16 @@ static inline int execshssh( const char *host )
// execute sub-process
static pid_t exec_ssh ( const char *cmd )
{
if ( !cmd || !cmd[0] ) return -1;
if ( !cmd || !cmd[0] )
{
return -1;
}
signal ( SIGCHLD, catch_exit );
pid_t pid = fork ();
if ( !pid ) {
if ( !pid )
{
setsid ();
execshssh ( cmd );
exit ( EXIT_FAILURE );
@ -77,15 +81,18 @@ static pid_t exec_ssh( const char *cmd )
sprintf ( path, "%s/%s", cache_dir, SSH_CACHE_FILE );
FILE *fd = fopen ( path, "r" );
if ( fd != NULL ) {
if ( fd != NULL )
{
char buffer[1024];
while ( fgets( buffer,1024,fd ) != NULL ) {
while ( fgets ( buffer, 1024, fd ) != NULL )
{
retv = reallocate ( retv, ( index + 2 ) * sizeof ( char* ) );
buffer[strlen ( buffer ) - 1] = '\0';
retv[index] = strdup ( buffer );
retv[index + 1] = NULL;
if ( strcasecmp( retv[index], cmd ) == 0 ) {
if ( strcasecmp ( retv[index], cmd ) == 0 )
{
curr = index;
}
@ -100,13 +107,16 @@ static pid_t exec_ssh( const char *cmd )
*/
fd = fopen ( path, "w" );
if ( fd ) {
if ( fd )
{
// Last one goes on top!
fputs ( cmd, fd );
fputc ( '\n', fd );
for ( int i = 0; i < ( int )index && i < 20; i++ ) {
if ( i != curr ) {
for ( int i = 0; i < ( int ) index && i < 20; i++ )
{
if ( i != curr )
{
fputs ( retv[i], fd );
fputc ( '\n', fd );
}
@ -115,7 +125,8 @@ static pid_t exec_ssh( const char *cmd )
fclose ( fd );
}
for ( int i=0; retv != NULL && retv[i] != NULL; i++ ) {
for ( int i = 0; retv != NULL && retv[i] != NULL; i++ )
{
free ( retv[i] );
}
@ -127,7 +138,10 @@ static pid_t exec_ssh( const char *cmd )
}
static void delete_ssh ( const char *cmd )
{
if ( !cmd || !cmd[0] ) return ;
if ( !cmd || !cmd[0] )
{
return;
}
int curr = -1;
unsigned int index = 0;
@ -141,15 +155,18 @@ static void delete_ssh( const char *cmd )
sprintf ( path, "%s/%s", cache_dir, SSH_CACHE_FILE );
FILE *fd = fopen ( path, "r" );
if ( fd != NULL ) {
if ( fd != NULL )
{
char buffer[1024];
while ( fgets( buffer,1024,fd ) != NULL ) {
while ( fgets ( buffer, 1024, fd ) != NULL )
{
retv = reallocate ( retv, ( index + 2 ) * sizeof ( char* ) );
buffer[strlen ( buffer ) - 1] = '\0';
retv[index] = strdup ( buffer );
retv[index + 1] = NULL;
if ( strcasecmp( retv[index], cmd ) == 0 ) {
if ( strcasecmp ( retv[index], cmd ) == 0 )
{
curr = index;
}
@ -164,10 +181,12 @@ static void delete_ssh( const char *cmd )
*/
fd = fopen ( path, "w" );
if ( fd ) {
for ( int i = 0; i < ( int )index && i < 20; i++ ) {
if ( i != curr ) {
if ( fd )
{
for ( int i = 0; i < ( int ) index && i < 20; i++ )
{
if ( i != curr )
{
fputs ( retv[i], fd );
fputc ( '\n', fd );
}
@ -176,14 +195,14 @@ static void delete_ssh( const char *cmd )
fclose ( fd );
}
for ( int i=0; retv != NULL && retv[i] != NULL; i++ ) {
for ( int i = 0; retv != NULL && retv[i] != NULL; i++ )
{
free ( retv[i] );
}
free ( retv );
free ( path );
}
static int sort_func ( const void *a, const void *b )
{
@ -202,15 +221,20 @@ static char ** get_ssh ( )
clock_gettime ( CLOCK_REALTIME, &start );
#endif
if ( getenv( "HOME" ) == NULL ) return NULL;
if ( getenv ( "HOME" ) == NULL )
{
return NULL;
}
path = allocate ( strlen ( cache_dir ) + strlen ( "/"SSH_CACHE_FILE ) + 2 );
sprintf ( path, "%s/%s", cache_dir, SSH_CACHE_FILE );
FILE *fd = fopen ( path, "r" );
char buffer[1024];
if ( fd != NULL ) {
while ( fgets( buffer,1024,fd ) != NULL ) {
if ( fd != NULL )
{
while ( fgets ( buffer, 1024, fd ) != NULL )
{
retv = reallocate ( retv, ( index + 2 ) * sizeof ( char* ) );
buffer[strlen ( buffer ) - 1] = '\0';
retv[index] = strdup ( buffer );
@ -228,29 +252,48 @@ static char ** get_ssh ( )
sprintf ( path, "%s/%s", hd, ".ssh/config" );
fd = fopen ( path, "r" );
if ( fd != NULL ) {
while ( fgets( buffer,1024,fd ) != NULL ) {
if ( strncasecmp( buffer, "Host", 4 ) == 0 ) {
if ( fd != NULL )
{
while ( fgets ( buffer, 1024, fd ) != NULL )
{
if ( strncasecmp ( buffer, "Host", 4 ) == 0 )
{
int start = 0, stop = 0;
buffer[strlen ( buffer ) - 1] = '\0';
for ( start=4; isspace( buffer[start] ); start++ );
for ( start = 4; isspace ( buffer[start] ); start++ )
{
;
}
for ( stop = start; isalnum ( buffer[stop] ) ||
buffer[stop] == '_' ||
buffer[stop] == '.' ; stop++ );
buffer[stop] == '.'; stop++ )
{
;
}
int found = 0;
if ( start == stop ) continue;
if ( start == stop )
{
continue;
}
// This is a nice little penalty, but doable? time will tell.
// given num_favorites is max 25.
for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) {
if ( strncasecmp( &buffer[start], retv[j],stop-start ) == 0 ) found = 1;
for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ )
{
if ( strncasecmp ( &buffer[start], retv[j], stop - start ) == 0 )
{
found = 1;
}
}
if ( found == 1 ) continue;
if ( found == 1 )
{
continue;
}
retv = reallocate ( retv, ( index + 2 ) * sizeof ( char* ) );
retv[index] = strndup ( &buffer[start], stop - start );
@ -263,14 +306,16 @@ static char ** get_ssh ( )
}
// TODO: check this is still fast enough. (takes 1ms on laptop.)
if(index > num_favorites) {
if ( index > num_favorites )
{
qsort ( &retv[num_favorites], index - num_favorites, sizeof ( char* ), sort_func );
}
free ( path );
#ifdef TIMING
clock_gettime ( CLOCK_REALTIME, &stop );
if ( stop.tv_sec != start.tv_sec ) {
if ( stop.tv_sec != start.tv_sec )
{
stop.tv_nsec += ( stop.tv_sec - start.tv_sec ) * 1e9;
}
@ -286,7 +331,8 @@ SwitcherMode ssh_switcher_dialog ( char **input )
// act as a launcher
char **cmd_list = get_ssh ( );
if ( cmd_list == NULL ) {
if ( cmd_list == NULL )
{
cmd_list = allocate ( 2 * sizeof ( char * ) );
cmd_list[0] = strdup ( "No ssh hosts found" );
cmd_list[1] = NULL;
@ -296,23 +342,34 @@ SwitcherMode ssh_switcher_dialog ( char **input )
int selected_line = 0;
int mretv = menu ( cmd_list, input, "ssh", NULL, &shift, token_match, NULL, &selected_line );
if ( mretv == MENU_NEXT ) {
if ( mretv == MENU_NEXT )
{
retv = NEXT_DIALOG;
} else if ( mretv == MENU_OK && cmd_list[selected_line] != NULL ) {
}
else if ( mretv == MENU_OK && cmd_list[selected_line] != NULL )
{
exec_ssh ( cmd_list[selected_line] );
} else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' ) {
}
else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' )
{
exec_ssh ( *input );
} else if ( mretv == MENU_ENTRY_DELETE && cmd_list[selected_line] ) {
}
else if ( mretv == MENU_ENTRY_DELETE && cmd_list[selected_line] )
{
delete_ssh ( cmd_list[selected_line] );
// Stay
retv = SSH_DIALOG;
}
for ( int i=0; cmd_list[i] != NULL; i++ ) {
for ( int i = 0; cmd_list[i] != NULL; i++ )
{
free ( cmd_list[i] );
}
if ( cmd_list != NULL ) free( cmd_list );
if ( cmd_list != NULL )
{
free ( cmd_list );
}
return retv;
}

View File

@ -79,7 +79,8 @@ textbox* textbox_create( Window parent,
textbox_moveresize ( tb, tb->x, tb->y, tb->w, tb->h );
// edit mode controls
if ( tb->flags & TB_EDITABLE ) {
if ( tb->flags & TB_EDITABLE )
{
tb->xim = XOpenIM ( display, NULL, NULL, NULL );
tb->xic = XCreateIC ( tb->xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, tb->window, XNFocusWindow, tb->window, NULL );
}
@ -90,7 +91,8 @@ textbox* textbox_create( Window parent,
// set an Xft font by name
void textbox_font ( textbox *tb, char *font, char *fg, char *bg )
{
if ( tb->font ) {
if ( tb->font )
{
XftColorFree ( display,
DefaultVisual ( display, DefaultScreen ( display ) ),
DefaultColormap ( display, DefaultScreen ( display ) ),
@ -121,7 +123,10 @@ void textbox_extents( textbox *tb )
// set the default text to display
void textbox_text ( textbox *tb, char *text )
{
if ( tb->text ) free( tb->text );
if ( tb->text )
{
free ( tb->text );
}
tb->text = strdup ( text );
tb->cursor = MAX ( 0, MIN ( ( int ) strlen ( text ), tb->cursor ) );
@ -132,12 +137,17 @@ void textbox_text( textbox *tb, char *text )
void textbox_moveresize ( textbox *tb, int x, int y, int w, int h )
{
if ( tb->flags & TB_AUTOHEIGHT )
{
h = tb->font->ascent + tb->font->descent;
}
if ( tb->flags & TB_AUTOWIDTH )
{
w = tb->extents.width;
}
if ( x != tb->x || y != tb->y || w != tb->w || h != tb->h ) {
if ( x != tb->x || y != tb->y || w != tb->w || h != tb->h )
{
tb->x = x;
tb->y = y;
tb->w = MAX ( 1, w );
@ -155,16 +165,24 @@ void textbox_show( textbox *tb )
// will also unmap the window if still displayed
void textbox_free ( textbox *tb )
{
if ( tb->flags & TB_EDITABLE ) {
if ( tb->flags & TB_EDITABLE )
{
XDestroyIC ( tb->xic );
XCloseIM ( tb->xim );
}
if ( tb->text ) free( tb->text );
if ( tb->text )
{
free ( tb->text );
}
if ( tb->prompt ) free( tb->prompt );
if ( tb->prompt )
{
free ( tb->prompt );
}
if ( tb->font ) {
if ( tb->font )
{
XftColorFree ( display,
DefaultVisual ( display, DefaultScreen ( display ) ),
DefaultColormap ( display, DefaultScreen ( display ) ),
@ -204,7 +222,8 @@ void textbox_draw( textbox *tb )
int cursor_x = 0;
int cursor_width = MAX ( 2, line_height / 10 );
if ( tb->flags & TB_EDITABLE ) {
if ( tb->flags & TB_EDITABLE )
{
int cursor_offset = 0;
int prompt_len = strlen ( prompt ) + 1;
length = text_len + prompt_len;
@ -214,7 +233,13 @@ void textbox_draw( textbox *tb )
sprintf ( line, "%s %s", prompt, text );
// replace spaces so XftTextExtents8 includes their width
for ( int i = 0; i < length; i++ ) if ( isspace( line[i] ) ) line[i] = '_';
for ( int i = 0; i < length; i++ )
{
if ( isspace ( line[i] ) )
{
line[i] = '_';
}
}
// calc cursor position
XftTextExtentsUtf8 ( display, tb->font, ( unsigned char * ) line, cursor_offset, &extents );
@ -227,27 +252,42 @@ void textbox_draw( textbox *tb )
// calc full input text width
// Calculate the right size, so no characters are cut off.
// TODO: Check performance of this.
while ( 1 ) {
while ( 1 )
{
XftTextExtentsUtf8 ( display, tb->font, ( unsigned char * ) line, length, &extents );
line_width = extents.width;
if ( line_width < ( tb->w-2*SIDE_MARGIN ) ) break;
if ( line_width < ( tb->w - 2 * SIDE_MARGIN ) )
{
break;
}
for(length-=1; length > 0 && (line[length]&0xc0) == 0x80; length -=1);
for ( length -= 1; length > 0 && ( line[length] & 0xc0 ) == 0x80; length -= 1 )
{
;
}
}
int x = SIDE_MARGIN, y = tb->font->ascent;
if ( tb->flags & TB_RIGHT ) x = tb->w - line_width;
if ( tb->flags & TB_RIGHT )
{
x = tb->w - line_width;
}
if ( tb->flags & TB_CENTER ) x = ( tb->w - line_width ) / 2;
if ( tb->flags & TB_CENTER )
{
x = ( tb->w - line_width ) / 2;
}
// draw the text, including any prompt in edit mode
XftDrawStringUtf8 ( draw, &tb->color_fg, tb->font, x, y, ( unsigned char * ) line, length );
// draw the cursor
if ( tb->flags & TB_EDITABLE )
{
XftDrawRect ( draw, &tb->color_fg, cursor_x + SIDE_MARGIN, 2, cursor_width, line_height - 4 );
}
XftDrawRect ( draw, &tb->color_bg, tb->w - SIDE_MARGIN, 0, SIDE_MARGIN, tb->h );
// flip canvas to window
@ -259,11 +299,15 @@ void textbox_draw( textbox *tb )
}
static size_t nextrune(textbox *tb, int inc) {
static size_t nextrune ( textbox *tb, int inc )
{
ssize_t n;
/* return location of next utf8 rune in the given direction (+1 or -1) */
for(n = tb->cursor + inc; n + inc >= 0 && (tb->text[n] & 0xc0) == 0x80; n += inc);
for ( n = tb->cursor + inc; n + inc >= 0 && ( tb->text[n] & 0xc0 ) == 0x80; n += inc )
{
;
}
return n;
}
// cursor handling for edit mode
@ -327,7 +371,8 @@ void textbox_cursor_del( textbox *tb )
// back up and delete one character
void textbox_cursor_bkspc ( textbox *tb )
{
if ( tb->cursor > 0 ) {
if ( tb->cursor > 0 )
{
textbox_cursor_dec ( tb );
textbox_cursor_del ( tb );
}
@ -344,15 +389,21 @@ int textbox_keypress( textbox *tb, XEvent *ev )
char pad[32];
int len;
if ( !( tb->flags & TB_EDITABLE ) ) return 0;
if ( !( tb->flags & TB_EDITABLE ) )
{
return 0;
}
len = Xutf8LookupString ( tb->xic, &ev->xkey, pad, sizeof ( pad ), &key, &stat );
pad[len] = 0;
if ( key == XK_Left ) {
if ( key == XK_Left )
{
textbox_cursor_dec ( tb );
return 1;
} else if ( key == XK_Right ) {
}
else if ( key == XK_Right )
{
textbox_cursor_inc ( tb );
return 1;
} /*else if ( key == XK_Home ) {
@ -362,15 +413,23 @@ int textbox_keypress( textbox *tb, XEvent *ev )
} else if ( key == XK_End ) {
textbox_cursor_end( tb );
return 1;
} */else if ( key == XK_Delete ) {
} */
else if ( key == XK_Delete )
{
textbox_cursor_del ( tb );
return 1;
} else if ( key == XK_BackSpace ) {
}
else if ( key == XK_BackSpace )
{
textbox_cursor_bkspc ( tb );
return 1;
} else if ( key == XK_Return ) {
}
else if ( key == XK_Return )
{
return -1;
} else if ( !iscntrl( *pad ) ) {
}
else if ( !iscntrl ( *pad ) )
{
textbox_insert ( tb, tb->cursor, pad );
textbox_cursor_inc ( tb );
return 1;

View File

@ -34,15 +34,18 @@
// Big thanks to Sean Pringle for this code.
// This maps xresource options to config structure.
typedef enum {
typedef enum
{
xrm_String = 0,
xrm_Number = 1
} XrmOptionType;
typedef struct {
typedef struct
{
int type;
char * name;
union {
union
{
unsigned int * num;
char ** str;
};
@ -69,7 +72,8 @@ void parse_xresource_options( Display *display )
XrmInitialize ();
char * xRMS = XResourceManagerString ( display );
if ( xRMS != NULL ) {
if ( xRMS != NULL )
{
XrmDatabase xDB = XrmGetStringDatabase ( xRMS );
char * xrmType;
@ -78,7 +82,8 @@ void parse_xresource_options( Display *display )
const char * namePrefix = "rofi";
const char * classPrefix = "Simpleswitcher";
for ( unsigned int i = 0; i < sizeof ( xrmOptions ) / sizeof ( *xrmOptions ); ++i ) {
for ( unsigned int i = 0; i < sizeof ( xrmOptions ) / sizeof ( *xrmOptions ); ++i )
{
char *name = ( char * ) allocate ( ( strlen ( namePrefix ) + 1 + strlen ( xrmOptions[i].name ) ) *
sizeof ( char ) + 1 );
char *class = ( char * ) allocate ( ( strlen ( classPrefix ) + 1 + strlen ( xrmOptions[i].name ) ) *
@ -86,12 +91,15 @@ void parse_xresource_options( Display *display )
sprintf ( name, "%s.%s", namePrefix, xrmOptions[i].name );
sprintf ( class, "%s.%s", classPrefix, xrmOptions[i].name );
if ( XrmGetResource ( xDB, name, class, &xrmType, &xrmValue ) ) {
if ( xrmOptions[i].type == xrm_String ) {
if ( XrmGetResource ( xDB, name, class, &xrmType, &xrmValue ) )
{
if ( xrmOptions[i].type == xrm_String )
{
*xrmOptions[i].str = ( char * ) allocate ( xrmValue.size * sizeof ( char ) );
strncpy ( *xrmOptions[i].str, xrmValue.addr, xrmValue.size );
} else if ( xrmOptions[i].type == xrm_Number ) {
}
else if ( xrmOptions[i].type == xrm_Number )
{
*xrmOptions[i].num = strtol ( xrmValue.addr, NULL, 10 );
}
}
@ -100,5 +108,4 @@ void parse_xresource_options( Display *display )
free ( class );
}
}
}