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

Extra checks, first part cleanup

This commit is contained in:
Qball Cow 2014-05-19 21:02:05 +02:00
parent 6cd01dba0e
commit 0a95ba47fa
4 changed files with 78 additions and 26 deletions

View file

@ -24,6 +24,8 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "rofi.h"
Settings config = {
@ -33,14 +35,14 @@ Settings config = {
.window_opacity = 100,
// Border width around the window.
.menu_bw = 1,
// The width of the switcher. (0-100 in % > 100 in pixels)
// The width of the switcher. (0100 in % > 100 in pixels)
.menu_width = 50,
// Maximum number of options to show.
.menu_lines = 15,
// Number of columns
.menu_columns = 1,
// Font
.menu_font = "mono-12",
.menu_font = "mono12",
// Foreground color
.menu_fg = "#222222",
// Background color
@ -74,3 +76,33 @@ Settings config = {
.x_offset = 0,
.fixed_num_lines = 0
};
/**
* 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);
}
}

View file

@ -107,4 +107,6 @@ extern Settings config;
int token_match ( char **tokens, const char *input,
__attribute__( ( unused ) ) int index,
__attribute__( ( unused ) ) void *data );
void config_sanity_check ( void );
#endif

View file

@ -320,8 +320,8 @@ typedef struct
int len;
} winlist;
winlist *cache_client;
winlist *cache_xattr;
winlist *cache_client = NULL;
winlist *cache_xattr = NULL;
#define winlist_ascend( l, i, w ) for ( ( i ) = 0; ( i ) < ( l )->len && ( ( ( w ) = ( l )->array[i] ) || 1 ); ( i )++ )
#define winlist_descend( l, i, w ) for ( ( i ) = ( l )->len - 1; ( i ) >= 0 && ( ( ( w ) = ( l )->array[i] ) || 1 ); ( i )-- )
@ -1893,6 +1893,38 @@ static void parse_cmd_options( int argc, char ** argv )
}
}
static void cleanup()
{
printf("cleanup\n");
// Cleanup
if ( display != NULL )
{
if ( main_window != None )
{
XFreeGC ( display, gc );
XDestroyWindow ( display, main_window );
XCloseDisplay ( display );
}
}
if(cache_xattr != NULL)
{
winlist_free ( cache_xattr );
}
if(cache_client != NULL)
{
winlist_free ( cache_client );
}
#ifdef HAVE_I3_IPC_H
if ( i3_socket_path != NULL )
{
free ( i3_socket_path );
}
#endif
xdgWipeHandle ( &xdg_handle );
}
int main ( int argc, char *argv[] )
{
int i, j;
@ -1913,6 +1945,7 @@ int main ( int argc, char *argv[] )
return EXIT_SUCCESS;
}
// Get DISPLAY
char *display_str = getenv ( "DISPLAY" );
find_arg_str ( argc, argv, "-display", &display_str );
@ -1930,6 +1963,9 @@ int main ( int argc, char *argv[] )
return EXIT_FAILURE;
}
// Register cleanup function.
atexit(cleanup);
cache_dir = xdgCacheHome ( &xdg_handle );
@ -1976,6 +2012,9 @@ int main ( int argc, char *argv[] )
// Parse command line for settings.
parse_cmd_options ( argc, argv );
// Sanity check
config_sanity_check ();
// flags to run immediately and exit
if ( find_arg ( argc, argv, "-now" ) >= 0 )
{
@ -2033,27 +2072,5 @@ int main ( int argc, char *argv[] )
}
}
// Cleanup
if ( display != NULL )
{
if ( main_window != None )
{
XFreeGC ( display, gc );
XDestroyWindow ( display, main_window );
XCloseDisplay ( display );
}
}
winlist_free ( cache_xattr );
winlist_free ( cache_client );
#ifdef HAVE_I3_IPC_H
if ( i3_socket_path != NULL )
{
free ( i3_socket_path );
}
#endif
xdgWipeHandle ( &xdg_handle );
return EXIT_SUCCESS;
}

View file

@ -105,6 +105,7 @@ void parse_xresource_options ( Display *display )
{
if ( xrmOptions[i].type == xrm_String )
{
//TODO this leaks memory.
*xrmOptions[i].str = ( char * ) malloc ( xrmValue.size * sizeof ( char ) );
strncpy ( *xrmOptions[i].str, xrmValue.addr, xrmValue.size );
}