1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2024-11-18 13:54:36 -05:00

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

@ -77,85 +77,3 @@ Settings config = {
.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

@ -51,7 +51,8 @@ static int __element_sort_func ( const void *ea, const void *eb )
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.
@ -133,10 +134,13 @@ void history_set ( const char *filename, const char *entry )
}
}
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 * ) );
@ -158,15 +162,23 @@ void history_set ( const char *filename, const char *entry )
{
// Write list.
__history_write_element_list ( fd, list, length );
}else {
}
else
{
fprintf ( stderr, "Failed to truncate file: %s\n", strerror ( errno ) );
}
// Free the list.
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 );
}
@ -198,7 +210,8 @@ void history_remove ( const char *filename, const char *entry )
}
// If found, remove it and write out new file.
if(found) {
if ( found )
{
// Remove the entry.
free ( list[curr] );
// Swap last to here (if list is size 1, we just swap empty sets).
@ -210,10 +223,13 @@ void history_remove ( const char *filename, const char *entry )
// Rewind.
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 {
}
else
{
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
}
}
@ -221,9 +237,15 @@ void history_remove ( const char *filename, const char *entry )
// Free the list.
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 );
}
@ -236,7 +258,8 @@ char ** history_get_list ( const char *filename, unsigned int *length )
FILE *fd = fopen ( filename, "r" );
if ( fd == NULL )
{
if(errno != ENOENT) {
if ( errno != ENOENT )
{
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
}
return NULL;

View file

@ -152,11 +152,7 @@ static inline void tokenize_free ( char **ip )
return;
}
if ( 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 );
}
*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 );
}
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

@ -82,7 +82,8 @@ static pid_t exec_cmd ( const char *cmd, int run_in_term )
* 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 -1;
}
history_set ( path, cmd );
@ -99,7 +100,8 @@ 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 );
@ -129,7 +131,8 @@ static char ** get_apps ( void )
}
if(asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) > 0) {
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.

View file

@ -56,7 +56,8 @@ static inline int execshssh ( const char *host )
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 )
{
@ -71,8 +72,10 @@ static inline int execshssh ( const char *host )
int retv = execvp ( config.terminal_emulator, (char * const *) args );
// Free the args list.
for(i =0; i < 7;i++) {
if(args[i] != NULL) {
for ( i = 0; i < 7; i++ )
{
if ( args[i] != NULL )
{
free ( args[i] );
}
}
@ -154,7 +157,8 @@ static char ** get_ssh ( void )
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

@ -244,7 +244,8 @@ 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.
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;

View file

@ -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;
}
}