Cleanups and indenting.

This commit is contained in:
QC 2014-05-22 09:33:32 +02:00
parent a26cf2637d
commit 21a0666a6e
7 changed files with 309 additions and 285 deletions

View file

@ -70,92 +70,10 @@ Settings config = {
// Mode of window, list (Vertical) or dmenu like (Horizontal)
.hmode = FALSE,
// Padding of the window.
.padding = 5,
.show_title = 1,
.y_offset = 0,
.x_offset = 0,
.fixed_num_lines = FALSE
.padding = 5,
.show_title = 1,
.y_offset = 0,
.x_offset = 0,
.fixed_num_lines = FALSE
};
/**
* Do some input validation, especially the first few could break things.
* It is good to catch them beforehand.
*
* This functions exits the program with 1 when it finds an invalid configuration.
*/
void config_sanity_check( void )
{
if ( config.menu_lines == 0 ) {
fprintf(stderr, "config.menu_lines is invalid. You need at least one visible line.\n");
exit(1);
}
if ( config.menu_columns == 0 ) {
fprintf(stderr, "config.menu_columns is invalid. You need at least one visible column.\n");
exit(1);
}
if ( config.menu_width == 0 ) {
fprintf(stderr, "config.menu_width is invalid. You cannot have a window with no width.\n");
exit(1);
}
if ( !( config.location >= WL_CENTER && config.location <= WL_WEST ) )
{
fprintf(stderr, "config.location is invalid. ( %d >= %d >= %d) does not hold.\n",
WL_WEST, config.location, WL_CENTER);
exit(1);
}
if ( !( config.hmode == TRUE || config.hmode == FALSE ) )
{
fprintf(stderr, "config.hmode is invalid.\n");
exit(1);
}
}
/**
* Print out the current configuration.
*/
void config_print( void )
{
printf("Windows opacity: %3d%%\n", config.window_opacity);
printf("Border width: %3d\n", config.menu_bw);
printf("Padding: %3d\n", config.padding);
printf("Width: %4d%s\n", config.menu_width,
config.menu_width > 100? "px":"%");
printf("offset (x,y): (%2d,%2d)px\n", config.x_offset, config.y_offset);
printf("Location: ");
switch(config.location)
{
case WL_CENTER: printf(" Center\n"); break;
case WL_NORTH_WEST: printf("North West\n"); break;
case WL_NORTH: printf(" North\n"); break;
case WL_NORTH_EAST: printf("North East\n"); break;
case WL_EAST: printf(" East\n"); break;
case WL_EAST_SOUTH: printf("East South\n"); break;
case WL_SOUTH: printf(" South\n"); break;
case WL_SOUTH_WEST: printf("South West\n"); break;
case WL_WEST: printf(" West\n"); break;
default: printf(" Invalid\n"); break;
}
printf("# Lines: %3d\n", config.menu_lines);
printf("# Columns: %3d\n", config.menu_columns);
printf("Fixed number of lines: %5s\n", config.fixed_num_lines?"true":"false");
printf("Horizontal model: %5s\n", config.hmode == TRUE?"true":"false");
printf("Font: %35s\n", config.menu_font);
/* Colors */
printf("FG Color: %7s\n", config.menu_fg);
printf("BG Color: %7s\n", config.menu_bg);
printf("Highlight FG Color: %7s\n", config.menu_hlfg);
printf("Highlight BG Color: %7s\n", config.menu_hlbg);
printf("Border color: %7s\n", config.menu_bc);
/* Terminal */
printf("Terminal emulator: %22s\n", config.terminal_emulator);
/* Keybindings. */
printf("Window switcher key: %7s\n", config.window_key);
printf("Run dialog key: %7s\n", config.run_key);
printf("SSH dialog key: %7s\n", config.ssh_key);
}

View file

@ -33,8 +33,8 @@
#include "rofi.h"
#include "history.h"
#define HISTORY_NAME_LENGTH 256
#define HISTORY_MAX_ENTRIES 25
#define HISTORY_NAME_LENGTH 256
#define HISTORY_MAX_ENTRIES 25
typedef struct __element
{
@ -49,40 +49,41 @@ static int __element_sort_func ( const void *ea, const void *eb )
return b->index - a->index;
}
static void __history_write_element_list( FILE *fd, _element **list, unsigned int length)
static void __history_write_element_list ( FILE *fd, _element **list, unsigned int length )
{
if(list == NULL) {
if ( list == NULL )
{
return;
}
// Sort the list before writing out.
qsort ( list, length, sizeof ( _element* ), __element_sort_func );
// Set the max length of the list.
length = (length > HISTORY_MAX_ENTRIES)? HISTORY_MAX_ENTRIES:length;
length = ( length > HISTORY_MAX_ENTRIES ) ? HISTORY_MAX_ENTRIES : length;
// Write out entries.
for(unsigned int iter = 0; iter < length ;iter++)
for ( unsigned int iter = 0; iter < length; iter++ )
{
fprintf(fd , "%ld %s\n", list[iter]->index, list[iter]->name);
fprintf ( fd, "%ld %s\n", list[iter]->index, list[iter]->name );
}
}
static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
{
char buffer[HISTORY_NAME_LENGTH+16];
char buffer[HISTORY_NAME_LENGTH + 16];
_element **retv = NULL;
if (length == NULL)
if ( length == NULL )
{
return NULL;
}
*length = 0;
if( fd == NULL)
if ( fd == NULL )
{
return NULL;
}
while ( fgets (buffer, HISTORY_NAME_LENGTH+16, fd ) != NULL)
while ( fgets ( buffer, HISTORY_NAME_LENGTH + 16, fd ) != NULL )
{
char * start = NULL;
// Skip empty lines.
@ -90,18 +91,18 @@ static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
{
continue;
}
retv = realloc ( retv, ( *length + 2 ) * sizeof ( _element* ) );
retv[(*length)] = malloc ( sizeof ( _element ) );
retv = realloc ( retv, ( *length + 2 ) * sizeof ( _element* ) );
retv[( *length )] = malloc ( sizeof ( _element ) );
// remove trailing \n
buffer[strlen ( buffer ) - 1] = '\0';
// Parse the number of times.
retv[(*length)]->index = strtol ( buffer, &start, 10 );
strncpy(retv[(*length)]->name, (start+1),HISTORY_NAME_LENGTH);
retv[( *length )]->index = strtol ( buffer, &start, 10 );
strncpy ( retv[( *length )]->name, ( start + 1 ), HISTORY_NAME_LENGTH );
// Force trailing '\0'
retv[(*length)]->name[HISTORY_NAME_LENGTH-1] = '\0';
retv[(*length) + 1] = NULL;
retv[( *length )]->name[HISTORY_NAME_LENGTH - 1] = '\0';
retv[( *length ) + 1] = NULL;
(*length)++;
( *length )++;
}
return retv;
}
@ -109,41 +110,44 @@ static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
void history_set ( const char *filename, const char *entry )
{
int found = 0;
int found = 0;
unsigned int curr = 0;
unsigned int length = 0;
_element **list = NULL;
_element **list = NULL;
// Open file for reading and writing.
FILE *fd = fopen(filename, "a+");
if(fd == NULL)
FILE *fd = fopen ( filename, "a+" );
if ( fd == NULL )
{
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
return ;
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
return;
}
// Get list.
list = __history_get_element_list(fd, &length);
list = __history_get_element_list ( fd, &length );
// Look if the entry exists.
for(unsigned int iter = 0;!found && iter < length; iter++)
for ( unsigned int iter = 0; !found && iter < length; iter++ )
{
if(strcmp(list[iter]->name, entry) == 0)
if ( strcmp ( list[iter]->name, entry ) == 0 )
{
curr = iter;
curr = iter;
found = 1;
}
}
if(found) {
if ( found )
{
// If exists, increment list index number
list[curr]->index++;
}else{
}
else
{
// If not exists, add it.
// Increase list by one
list = realloc(list,(length+2)*sizeof(_element *));
list[length] = malloc(sizeof(_element));
list = realloc ( list, ( length + 2 ) * sizeof ( _element * ) );
list[length] = malloc ( sizeof ( _element ) );
// Copy name
strncpy(list[length]->name, entry, HISTORY_NAME_LENGTH);
list[length]->name[HISTORY_NAME_LENGTH-1] = '\0';
strncpy ( list[length]->name, entry, HISTORY_NAME_LENGTH );
list[length]->name[HISTORY_NAME_LENGTH - 1] = '\0';
// set # hits
list[length]->index = 1;
@ -152,111 +156,130 @@ void history_set ( const char *filename, const char *entry )
}
// Rewind.
fseek(fd, 0L, SEEK_SET);
fseek ( fd, 0L, SEEK_SET );
// Clear file.
if ( ftruncate(fileno(fd), 0) == 0)
if ( ftruncate ( fileno ( fd ), 0 ) == 0 )
{
// Write list.
__history_write_element_list(fd, list, length);
}else {
fprintf(stderr, "Failed to truncate file: %s\n", strerror(errno));
__history_write_element_list ( fd, list, length );
}
else
{
fprintf ( stderr, "Failed to truncate file: %s\n", strerror ( errno ) );
}
// Free the list.
for(unsigned int iter = 0; iter < length; iter++)
for ( unsigned int iter = 0; iter < length; iter++ )
{
if(list[iter] != NULL) { free(list[iter]); }
if ( list[iter] != NULL )
{
free ( list[iter] );
}
}
if ( list != NULL )
{
free ( list );
}
if(list != NULL) free(list);
// Close file.
fclose(fd);
fclose ( fd );
}
void history_remove ( const char *filename, const char *entry )
{
_element ** list = NULL;
int found = 0;
unsigned int curr = 0;
unsigned int length = 0;
_element ** list = NULL;
int found = 0;
unsigned int curr = 0;
unsigned int length = 0;
// Open file for reading and writing.
FILE *fd = fopen(filename, "a+");
if(fd == NULL)
FILE *fd = fopen ( filename, "a+" );
if ( fd == NULL )
{
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
return ;
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
return;
}
// Get list.
list = __history_get_element_list(fd, &length);
list = __history_get_element_list ( fd, &length );
// Find entry.
for(unsigned int iter = 0;!found && iter < length; iter++)
for ( unsigned int iter = 0; !found && iter < length; iter++ )
{
if(strcmp(list[iter]->name, entry) == 0)
if ( strcmp ( list[iter]->name, entry ) == 0 )
{
curr = iter;
curr = iter;
found = 1;
}
}
// If found, remove it and write out new file.
if(found) {
if ( found )
{
// Remove the entry.
free(list[curr]);
free ( list[curr] );
// Swap last to here (if list is size 1, we just swap empty sets).
list[curr] = list[length-1];
list[curr] = list[length - 1];
// Empty last.
list[length-1] = NULL;
list[length - 1] = NULL;
length--;
// Rewind.
fseek(fd, 0L, SEEK_SET);
fseek ( fd, 0L, SEEK_SET );
// Clear list.
if(ftruncate(fileno(fd), 0) == 0) {
if ( ftruncate ( fileno ( fd ), 0 ) == 0 )
{
// Write list.
__history_write_element_list(fd, list, length);
} else {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
__history_write_element_list ( fd, list, length );
}
else
{
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
}
}
// Free the list.
for(unsigned int iter = 0; iter < length; iter++)
for ( unsigned int iter = 0; iter < length; iter++ )
{
if(list[iter] != NULL) { free(list[iter]); }
if ( list[iter] != NULL )
{
free ( list[iter] );
}
}
if ( list != NULL )
{
free ( list );
}
if(list != NULL) free(list);
// Close file.
fclose(fd);
fclose ( fd );
}
char ** history_get_list ( const char *filename, unsigned int *length )
{
_element **list = NULL;
char **retv = NULL;
char **retv = NULL;
// Open file.
FILE *fd = fopen(filename, "r");
if(fd == NULL)
FILE *fd = fopen ( filename, "r" );
if ( fd == NULL )
{
if(errno != ENOENT) {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
if ( errno != ENOENT )
{
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
}
return NULL;
}
// Get list.
list = __history_get_element_list(fd, length);
list = __history_get_element_list ( fd, length );
// Copy list in right format.
if((*length) > 0 )
if ( ( *length ) > 0 )
{
retv = malloc(((*length)+1)*sizeof(char *));
for ( int iter = 0; iter < (*length); iter++)
retv = malloc ( ( ( *length ) + 1 ) * sizeof ( char * ) );
for ( int iter = 0; iter < ( *length ); iter++ )
{
retv[iter] = strdup(list[iter]->name);
free(list[iter]);
retv[iter] = strdup ( list[iter]->name );
free ( list[iter] );
}
retv[(*length)] = NULL;
free(list);
retv[( *length )] = NULL;
free ( list );
}
fclose(fd);
fclose ( fd );
return retv;
}

View file

@ -152,11 +152,7 @@ static inline void tokenize_free ( char **ip )
return;
}
if ( ip[0] )
{
free ( ip[0] );
}
free ( ip[0] );
free ( ip );
}
@ -1495,10 +1491,7 @@ MenuReturn menu ( char **lines, char **input, char *prompt, Time *time, int *shi
release_keyboard ();
}
if ( *input != NULL )
{
free ( *input );
}
free ( *input );
*input = strdup ( text->text );
@ -1673,8 +1666,6 @@ SwitcherMode run_switcher_window ( char **input )
void run_switcher ( int fmode, SwitcherMode mode )
{
// TODO: this whole function is messy. build a nicer solution
// we fork because it's technically possible to have multiple window
// lists up at once on a zaphod multihead X setup.
// this also happens to isolate the Xft font stuff in a child process
@ -1724,10 +1715,7 @@ void run_switcher ( int fmode, SwitcherMode mode )
}
} while ( mode != MODE_EXIT );
if ( input != NULL )
{
free ( input );
}
free ( input );
if ( fmode == FORK )
{
@ -1950,9 +1938,6 @@ static void parse_cmd_options ( int argc, char ** argv )
config_print ();
exit ( EXIT_SUCCESS );
}
// Sanity check
config_sanity_check ();
}
static void cleanup ()
@ -1992,10 +1977,96 @@ static void cleanup ()
xdgWipeHandle ( &xdg_handle );
}
/**
* Do some input validation, especially the first few could break things.
* It is good to catch them beforehand.
*
* This functions exits the program with 1 when it finds an invalid configuration.
*/
void config_sanity_check ( void )
{
if ( config.menu_lines == 0 )
{
fprintf ( stderr, "config.menu_lines is invalid. You need at least one visible line.\n" );
exit ( 1 );
}
if ( config.menu_columns == 0 )
{
fprintf ( stderr, "config.menu_columns is invalid. You need at least one visible column.\n" );
exit ( 1 );
}
if ( config.menu_width == 0 )
{
fprintf ( stderr, "config.menu_width is invalid. You cannot have a window with no width.\n" );
exit ( 1 );
}
if ( !( config.location >= WL_CENTER && config.location <= WL_WEST ) )
{
fprintf ( stderr, "config.location is invalid. ( %d >= %d >= %d) does not hold.\n",
WL_WEST, config.location, WL_CENTER );
exit ( 1 );
}
if ( !( config.hmode == TRUE || config.hmode == FALSE ) )
{
fprintf ( stderr, "config.hmode is invalid.\n" );
exit ( 1 );
}
}
/**
* Print out the current configuration.
*/
void config_print ( void )
{
printf ( "Windows opacity: %3d%%\n", config.window_opacity );
printf ( "Border width: %3d\n", config.menu_bw );
printf ( "Padding: %3d\n", config.padding );
printf ( "Width: %4d%s\n", config.menu_width,
config.menu_width > 100 ? "px" : "%" );
printf ( "offset (x,y): (%2d,%2d)px\n", config.x_offset, config.y_offset );
printf ( "Location: " );
switch ( config.location )
{
case WL_CENTER: printf ( " Center\n" ); break;
case WL_NORTH_WEST: printf ( "North West\n" ); break;
case WL_NORTH: printf ( " North\n" ); break;
case WL_NORTH_EAST: printf ( "North East\n" ); break;
case WL_EAST: printf ( " East\n" ); break;
case WL_EAST_SOUTH: printf ( "East South\n" ); break;
case WL_SOUTH: printf ( " South\n" ); break;
case WL_SOUTH_WEST: printf ( "South West\n" ); break;
case WL_WEST: printf ( " West\n" ); break;
default: printf ( " Invalid\n" ); break;
}
printf ( "# Lines: %3d\n", config.menu_lines );
printf ( "# Columns: %3d\n", config.menu_columns );
printf ( "Fixed number of lines: %5s\n", config.fixed_num_lines ? "true" : "false" );
printf ( "Horizontal model: %5s\n", config.hmode == TRUE ? "true" : "false" );
printf ( "Font: %35s\n", config.menu_font );
/* Colors */
printf ( "FG Color: %7s\n", config.menu_fg );
printf ( "BG Color: %7s\n", config.menu_bg );
printf ( "Highlight FG Color: %7s\n", config.menu_hlfg );
printf ( "Highlight BG Color: %7s\n", config.menu_hlbg );
printf ( "Border color: %7s\n", config.menu_bc );
/* Terminal */
printf ( "Terminal emulator: %22s\n", config.terminal_emulator );
/* Keybindings. */
printf ( "Window switcher key: %7s\n", config.window_key );
printf ( "Run dialog key: %7s\n", config.run_key );
printf ( "SSH dialog key: %7s\n", config.ssh_key );
}
int main ( int argc, char *argv[] )
{
int i, j;
// Initialize xdg, so we can grab the xdgCacheHome
if ( xdgInitHandle ( &xdg_handle ) == NULL )
{
@ -2025,8 +2096,10 @@ int main ( int argc, char *argv[] )
// Parse command line for settings.
parse_cmd_options ( argc, argv );
// Sanity check
config_sanity_check ();
// Set up X interaction.
signal ( SIGCHLD, catch_exit );
screen = DefaultScreenOfDisplay ( display );
screen_id = DefaultScreen ( display );
@ -2039,9 +2112,9 @@ int main ( int argc, char *argv[] )
// determine numlock mask so we can bind on keys with and without it
XModifierKeymap *modmap = XGetModifierMapping ( display );
for ( i = 0; i < 8; i++ )
for ( int i = 0; i < 8; i++ )
{
for ( j = 0; j < ( int ) modmap->max_keypermod; j++ )
for ( int j = 0; j < ( int ) modmap->max_keypermod; j++ )
{
if ( modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode ( display, XK_Num_Lock ) )
{
@ -2056,7 +2129,7 @@ int main ( int argc, char *argv[] )
cache_xattr = winlist_new ();
// X atom values
for ( i = 0; i < NETATOMS; i++ )
for ( int i = 0; i < NETATOMS; i++ )
{
netatoms[i] = XInternAtom ( display, netatom_names[i], False );
}

View file

@ -81,11 +81,12 @@ static pid_t exec_cmd ( const char *cmd, int run_in_term )
* This happens in non-critical time (After launching app)
* It is allowed to be a bit slower.
*/
char *path = NULL;
if(asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) == -1) {
char *path = NULL;
if ( asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) == -1 )
{
return -1;
}
history_set(path, cmd);
history_set ( path, cmd );
free ( path );
@ -99,10 +100,11 @@ static void delete_entry ( const char *cmd )
* It is allowed to be a bit slower.
*/
char *path = NULL;
if(asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) == -1) {
if ( asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) == -1 )
{
return;
}
history_remove(path, cmd);
history_remove ( path, cmd );
free ( path );
}
@ -129,19 +131,20 @@ static char ** get_apps ( void )
}
if(asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) > 0) {
retv = history_get_list(path, &index);
if ( asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) > 0 )
{
retv = history_get_list ( path, &index );
free ( path );
// Keep track of how many where loaded as favorite.
num_favorites=index;
num_favorites = index;
}
path = strdup ( getenv ( "PATH" ) );
for ( const char *dirname = strtok ( path, ":" );
dirname != NULL;
dirname = strtok ( NULL, ":" ) )
dirname != NULL;
dirname = strtok ( NULL, ":" ) )
{
DIR *dir = opendir ( dirname );

View file

@ -53,30 +53,33 @@ static inline int execshssh ( const char *host )
/**
* I am not happy about this code, it causes 7 mallocs and frees
*/
char **args = malloc(sizeof(char*)*7);
int i=0;
char **args = malloc ( sizeof ( char* ) * 7 );
int i = 0;
args[i++] = config.terminal_emulator;
if(config.show_title) {
if ( config.show_title )
{
char *buffer = NULL;
if( asprintf(&buffer, "ssh %s", host) > 0)
if ( asprintf ( &buffer, "ssh %s", host ) > 0 )
{
args[i++] = strdup("-T");
args[i++] = strdup ( "-T" );
args[i++] = buffer;
}
}
args[i++] = strdup("-e");
args[i++] = strdup("ssh");
args[i++] = strdup(host);
args[i++] = strdup ( "-e" );
args[i++] = strdup ( "ssh" );
args[i++] = strdup ( host );
args[i++] = NULL;
int retv = execvp ( config.terminal_emulator, (char * const *)args );
int retv = execvp ( config.terminal_emulator, (char * const *) args );
// Free the args list.
for(i =0; i < 7;i++) {
if(args[i] != NULL) {
free(args[i]);
for ( i = 0; i < 7; i++ )
{
if ( args[i] != NULL )
{
free ( args[i] );
}
}
free(args);
free ( args );
return retv;
}
// execute sub-process
@ -102,10 +105,10 @@ static pid_t exec_ssh ( const char *cmd )
* It is allowed to be a bit slower.
*/
char *path = NULL;
if(asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0)
if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 )
{
history_set(path, cmd);
free(path);
history_set ( path, cmd );
free ( path );
}
return pid;
}
@ -116,10 +119,10 @@ static void delete_ssh ( const char *cmd )
return;
}
char *path = NULL;
if(asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0)
if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 )
{
history_remove(path, cmd);
free(path);
history_remove ( path, cmd );
free ( path );
}
}
static int sort_func ( const void *a, const void *b )
@ -144,17 +147,18 @@ static char ** get_ssh ( void )
return NULL;
}
if(asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0)
if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 )
{
retv = history_get_list(path, &index);
free(path);
num_favorites = index;
retv = history_get_list ( path, &index );
free ( path );
num_favorites = index;
}
FILE *fd = NULL;
FILE *fd = NULL;
const char *hd = getenv ( "HOME" );
if(asprintf ( &path, "%s/%s", hd, ".ssh/config" )>= 0){
if ( asprintf ( &path, "%s/%s", hd, ".ssh/config" ) >= 0 )
{
fd = fopen ( path, "r" );
}

View file

@ -129,7 +129,7 @@ void textbox_text ( textbox *tb, char *text )
textbox_extents ( tb );
}
void textbox_move (textbox *tb, int x, int y)
void textbox_move ( textbox *tb, int x, int y )
{
if ( x != tb->x || y != tb->y )
{
@ -148,13 +148,13 @@ void textbox_moveresize ( textbox *tb, int x, int y, int w, int h )
if ( tb->flags & TB_AUTOWIDTH )
{
if(w > 1)
if ( w > 1 )
{
w = MIN(w, tb->extents.width+2*SIDE_MARGIN);
w = MIN ( w, tb->extents.width + 2 * SIDE_MARGIN );
}
else
{
w = tb->extents.width+2*SIDE_MARGIN;
w = tb->extents.width + 2 * SIDE_MARGIN;
}
}
@ -221,14 +221,14 @@ void textbox_draw ( textbox *tb )
// clear canvas
XftDrawRect ( draw, &tb->color_bg, 0, 0, tb->w, tb->h );
char *text = tb->text ? tb->text : "";
int text_len = strlen ( text );
int length = text_len;
int line_height = tb->font->ascent + tb->font->descent;
int line_width = 0;
char *text = tb->text ? tb->text : "";
int text_len = strlen ( text );
int length = text_len;
int line_height = tb->font->ascent + tb->font->descent;
int line_width = 0;
int cursor_x = 0;
int cursor_width = MAX ( 2, line_height / 10 );
int cursor_x = 0;
int cursor_width = MAX ( 2, line_height / 10 );
if ( tb->flags & TB_EDITABLE )
{
@ -238,13 +238,14 @@ void textbox_draw ( textbox *tb )
// calc cursor position
XftTextExtentsUtf8 ( display, tb->font, ( unsigned char * ) text, cursor_offset, &extents );
// Add a small 4px offset between cursor and last glyph.
cursor_x = extents.width + ((extents.width > 0)?2:0);
cursor_x = extents.width + ( ( extents.width > 0 ) ? 2 : 0 );
}
// calc full input text width
// Calculate the right size, so no characters are cut off.
// TODO: Check performance of this.
do{
do
{
XftTextExtentsUtf8 ( display, tb->font, ( unsigned char * ) text, length, &extents );
line_width = extents.width;
if ( line_width <= ( tb->w - 2 * SIDE_MARGIN ) )
@ -256,8 +257,7 @@ void textbox_draw ( textbox *tb )
{
;
}
}
while ( line_width >0 );
} while ( line_width > 0 );
int x = SIDE_MARGIN, y = tb->font->ascent;
@ -280,7 +280,7 @@ void textbox_draw ( textbox *tb )
XftDrawRect ( draw, &tb->color_fg, cursor_x + SIDE_MARGIN, 2, cursor_width, line_height - 4 );
}
XftDrawRect ( draw, &tb->color_bg, tb->w, 0,0, tb->h );
XftDrawRect ( draw, &tb->color_bg, tb->w, 0, 0, tb->h );
// flip canvas to window
XCopyArea ( display, canvas, tb->window, context, 0, 0, tb->w, tb->h, 0, 0 );

View file

@ -37,9 +37,9 @@
// This maps xresource options to config structure.
typedef enum
{
xrm_String = 0,
xrm_Number = 1,
xrm_Boolean= 2
xrm_String = 0,
xrm_Number = 1,
xrm_Boolean = 2
} XrmOptionType;
typedef struct
@ -58,48 +58,48 @@ typedef struct
* Currently supports string and number.
*/
static XrmOption xrmOptions[] = {
{ xrm_Number, "opacity", { .num = &config.window_opacity }, NULL },
{ xrm_Number, "opacity", { .num = &config.window_opacity }, NULL },
{ xrm_Number, "width", { .num = &config.menu_width }, NULL },
{ xrm_Number, "width", { .num = &config.menu_width }, NULL },
{ xrm_Number, "lines", { .num = &config.menu_lines }, NULL },
{ xrm_Number, "columns", { .num = &config.menu_columns }, NULL },
{ xrm_Number, "lines", { .num = &config.menu_lines }, NULL },
{ xrm_Number, "columns", { .num = &config.menu_columns }, NULL },
{ xrm_String, "font", { .str = &config.menu_font }, NULL },
{ xrm_String, "font", { .str = &config.menu_font }, NULL },
/* Foreground color */
{ xrm_String, "foreground", { .str = &config.menu_fg }, NULL },
{ xrm_String, "fg", { .str = &config.menu_fg }, NULL },
{ xrm_String, "foreground", { .str = &config.menu_fg }, NULL },
{ xrm_String, "fg", { .str = &config.menu_fg }, NULL },
{ xrm_String, "background", { .str = &config.menu_bg }, NULL },
{ xrm_String, "bg", { .str = &config.menu_bg }, NULL },
{ xrm_String, "background", { .str = &config.menu_bg }, NULL },
{ xrm_String, "bg", { .str = &config.menu_bg }, NULL },
{ xrm_String, "highlightfg", { .str = &config.menu_hlfg }, NULL },
{ xrm_String, "hlbg", { .str = &config.menu_hlfg }, NULL },
{ xrm_String, "highlightfg", { .str = &config.menu_hlfg }, NULL },
{ xrm_String, "hlbg", { .str = &config.menu_hlfg }, NULL },
{ xrm_String, "highlightbg", { .str = &config.menu_hlbg }, NULL },
{ xrm_String, "hlbg", { .str = &config.menu_hlbg }, NULL },
{ xrm_String, "highlightbg", { .str = &config.menu_hlbg }, NULL },
{ xrm_String, "hlbg", { .str = &config.menu_hlbg }, NULL },
{ xrm_String, "bordercolor", { .str = &config.menu_bc }, NULL },
{ xrm_String, "bc", { .str = &config.menu_bc }, NULL },
{ xrm_String, "bordercolor", { .str = &config.menu_bc }, NULL },
{ xrm_String, "bc", { .str = &config.menu_bc }, NULL },
{ xrm_Number, "borderwidth", { .num = &config.menu_bw }, NULL },
{ xrm_Number, "bw", { .num = &config.menu_bw }, NULL },
{ xrm_Number, "borderwidth", { .num = &config.menu_bw }, NULL },
{ xrm_Number, "bw", { .num = &config.menu_bw }, NULL },
{ xrm_Number, "location", { .num = &config.location }, NULL },
{ xrm_Number, "loc", { .num = &config.location }, NULL },
{ xrm_Number, "location", { .num = &config.location }, NULL },
{ xrm_Number, "loc", { .num = &config.location }, NULL },
{ xrm_Number, "padding", { .num = &config.padding }, NULL },
{ xrm_Number, "yoffset", { .num = &config.y_offset }, NULL },
{ xrm_Number, "xoffset", { .num = &config.x_offset }, NULL },
{ xrm_Boolean,"fixed-num-lines", { .num = &config.fixed_num_lines }, NULL },
{ xrm_Boolean,"hmode", { .num = &config.hmode }, NULL },
{ xrm_Number, "padding", { .num = &config.padding }, NULL },
{ xrm_Number, "yoffset", { .num = &config.y_offset }, NULL },
{ xrm_Number, "xoffset", { .num = &config.x_offset }, NULL },
{ xrm_Boolean, "fixed-num-lines", { .num = &config.fixed_num_lines }, NULL },
{ xrm_Boolean, "hmode", { .num = &config.hmode }, NULL },
{ xrm_String, "terminal", { .str = &config.terminal_emulator }, NULL },
{ xrm_String, "terminal", { .str = &config.terminal_emulator }, NULL },
/* Key bindings */
{ xrm_String, "key", { .str = &config.window_key }, NULL },
{ xrm_String, "rkey", { .str = &config.run_key }, NULL },
{ xrm_String, "skey", { .str = &config.ssh_key }, NULL },
{ xrm_String, "key", { .str = &config.window_key }, NULL },
{ xrm_String, "rkey", { .str = &config.run_key }, NULL },
{ xrm_String, "skey", { .str = &config.ssh_key }, NULL },
};
@ -151,9 +151,12 @@ void parse_xresource_options ( Display *display )
}
else if ( xrmOptions[i].type == xrm_Boolean )
{
if(xrmValue.size > 0 && strncasecmp(xrmValue.addr, "true", xrmValue.size) == 0) {
if ( xrmValue.size > 0 && strncasecmp ( xrmValue.addr, "true", xrmValue.size ) == 0 )
{
*xrmOptions[i].num = TRUE;
} else {
}
else
{
*xrmOptions[i].num = FALSE;
}
}