diff options
Diffstat (limited to 'source/rofi.c')
-rw-r--r-- | source/rofi.c | 1830 |
1 files changed, 1101 insertions, 729 deletions
diff --git a/source/rofi.c b/source/rofi.c index edfd614c..a14fd1d7 100644 --- a/source/rofi.c +++ b/source/rofi.c @@ -70,16 +70,16 @@ #include "xrmoptions.h" -#define LINE_MARGIN 4 +#define LINE_MARGIN 4 -#define OPAQUE 0xffffffff -#define OPACITY "_NET_WM_WINDOW_OPACITY" -#define I3_SOCKET_PATH_PROP "I3_SOCKET_PATH" -#define FORK 1 -#define NOFORK 2 +#define OPAQUE 0xffffffff +#define OPACITY "_NET_WM_WINDOW_OPACITY" +#define I3_SOCKET_PATH_PROP "I3_SOCKET_PATH" +#define FORK 1 +#define NOFORK 2 -xdgHandle xdg_handle; +xdgHandle xdg_handle; const char *cache_dir = NULL; /** @@ -87,336 +87,389 @@ const char *cache_dir = NULL; * Matches tokenized. */ int token_match ( char **tokens, const char *input, - __attribute__( ( unused ) )int index, - __attribute__( ( unused ) )void *data ) + __attribute__( ( unused ) ) int index, + __attribute__( ( unused ) ) void *data ) { int match = 1; // Do a tokenized match. - if ( tokens ) for ( int j = 1; match && tokens[j]; j++ ) { - match = ( strcasestr( input, tokens[j] ) != NULL ); + if ( tokens ) + { + for ( int j = 1; match && tokens[j]; j++ ) + { + match = ( strcasestr ( input, tokens[j] ) != NULL ); } + } return match; } -void* allocate( unsigned long bytes ) +void* allocate ( unsigned long bytes ) { - if ( bytes == 0 ) return NULL; + if ( bytes == 0 ) + { + return NULL; + } - void *ptr = malloc( bytes ); + void *ptr = malloc ( bytes ); - if ( !ptr ) { - fprintf( stderr, "malloc failed!\n" ); - exit( EXIT_FAILURE ); + if ( !ptr ) + { + fprintf ( stderr, "malloc failed!\n" ); + exit ( EXIT_FAILURE ); } return ptr; } -void* allocate_clear( unsigned long bytes ) +void* allocate_clear ( unsigned long bytes ) { - if ( bytes == 0 ) return NULL; + if ( bytes == 0 ) + { + return NULL; + } - void *ptr = allocate( bytes ); - memset( ptr, 0, bytes ); + void *ptr = allocate ( bytes ); + memset ( ptr, 0, bytes ); return ptr; } -void* reallocate( void *ptr, unsigned long bytes ) +void* reallocate ( void *ptr, unsigned long bytes ) { - if ( bytes == 0 ) return NULL; + if ( bytes == 0 ) + { + return NULL; + } - ptr = realloc( ptr, bytes ); + ptr = realloc ( ptr, bytes ); - if ( !ptr ) { - fprintf( stderr, "realloc failed!\n" ); - exit( EXIT_FAILURE ); + if ( !ptr ) + { + fprintf ( stderr, "realloc failed!\n" ); + exit ( EXIT_FAILURE ); } return ptr; } -static char **tokenize( const char *input ) +static char **tokenize ( const char *input ) { - if ( input == NULL ) return NULL; + if ( input == NULL ) + { + return NULL; + } char *saveptr = NULL, *token; - char **retv = NULL; + char **retv = NULL; // First entry is always full (modified) stringtext. - int num_tokens = 1; + int num_tokens = 1; //First entry is string that is modified. - retv = allocate ( 2*sizeof( char* ) ); - retv[0] = strdup( input ); + retv = allocate ( 2 * sizeof ( char* ) ); + retv[0] = strdup ( input ); retv[1] = NULL; // Iterate over tokens. // strtok should still be valid for utf8. for ( - token = strtok_r( retv[0], " ", &saveptr ); + token = strtok_r ( retv[0], " ", &saveptr ); token != NULL; - token = strtok_r( NULL, " ", &saveptr ) ) { - retv = reallocate( retv, sizeof( char* )*( num_tokens+2 ) ); - retv[num_tokens+1] = NULL; - retv[num_tokens] = token; + token = strtok_r ( NULL, " ", &saveptr ) ) + { + retv = reallocate ( retv, sizeof ( char* ) * ( num_tokens + 2 ) ); + retv[num_tokens + 1] = NULL; + retv[num_tokens] = token; num_tokens++; } return retv; } -static inline void tokenize_free( char **ip ) +static inline void tokenize_free ( char **ip ) { - if ( ip == NULL ) return; + if ( ip == NULL ) + { + return; + } if ( ip[0] ) - free( ip[0] ); + { + free ( ip[0] ); + } - free( ip ); + free ( ip ); } #ifdef HAVE_I3_IPC_H // Path to HAVE_I3_IPC_H socket. char *i3_socket_path = NULL; // Focus window on HAVE_I3_IPC_H window manager. -static void focus_window_i3( const char *socket_path, int id ) +static void focus_window_i3 ( const char *socket_path, int id ) { - i3_ipc_header_t head; - char command[128]; - int s, t, len; + i3_ipc_header_t head; + char command[128]; + int s, t, len; struct sockaddr_un remote; - if ( strlen( socket_path ) > UNIX_PATH_MAX ) { - fprintf( stderr, "Socket path is to long. %zd > %d\n", strlen( socket_path ), UNIX_PATH_MAX ); + if ( strlen ( socket_path ) > UNIX_PATH_MAX ) + { + fprintf ( stderr, "Socket path is to long. %zd > %d\n", strlen ( socket_path ), UNIX_PATH_MAX ); return; } - if ( ( s = socket( AF_UNIX, SOCK_STREAM, 0 ) ) == -1 ) { - fprintf( stderr, "Failed to open connection to I3: %s\n", strerror( errno ) ); + if ( ( s = socket ( AF_UNIX, SOCK_STREAM, 0 ) ) == -1 ) + { + fprintf ( stderr, "Failed to open connection to I3: %s\n", strerror ( errno ) ); return; } remote.sun_family = AF_UNIX; - strcpy( remote.sun_path, socket_path ); - len = strlen( remote.sun_path ) + sizeof( remote.sun_family ); + strcpy ( remote.sun_path, socket_path ); + len = strlen ( remote.sun_path ) + sizeof ( remote.sun_family ); - if ( connect( s, ( struct sockaddr * )&remote, len ) == -1 ) { - fprintf( stderr, "Failed to connect to I3 (%s): %s\n", socket_path,strerror( errno ) ); - close( s ); - return ; + if ( connect ( s, ( struct sockaddr * ) &remote, len ) == -1 ) + { + fprintf ( stderr, "Failed to connect to I3 (%s): %s\n", socket_path, strerror ( errno ) ); + close ( s ); + return; } // Formulate command - snprintf( command, 128, "[id=\"%d\"] focus", id ); + snprintf ( command, 128, "[id=\"%d\"] focus", id ); // Prepare header. - memcpy( head.magic, I3_IPC_MAGIC, 6 ); - head.size = strlen( command ); + memcpy ( head.magic, I3_IPC_MAGIC, 6 ); + head.size = strlen ( command ); head.type = I3_IPC_MESSAGE_TYPE_COMMAND; // Send header. - send( s, &head, sizeof( head ),0 ); + send ( s, &head, sizeof ( head ), 0 ); // Send message - send( s, command, strlen( command ),0 ); + send ( s, command, strlen ( command ), 0 ); // Receive header. - t = recv( s, &head, sizeof( head ),0 ); + t = recv ( s, &head, sizeof ( head ), 0 ); - if ( t == sizeof( head ) ) { - recv( s, command, head.size, 0 ); + if ( t == sizeof ( head ) ) + { + recv ( s, command, head.size, 0 ); } - close( s ); + close ( s ); } #endif -void catch_exit( __attribute__( ( unused ) ) int sig ) +void catch_exit ( __attribute__( ( unused ) ) int sig ) { - while ( 0 < waitpid( -1, NULL, WNOHANG ) ); + while ( 0 < waitpid ( -1, NULL, WNOHANG ) ) + { + ; + } } // cli arg handling -static int find_arg( const int argc, char * const argv[], const char * const key ) +static int find_arg ( const int argc, char * const argv[], const char * const key ) { int i; - for ( i = 0; i < argc && strcasecmp( argv[i], key ); i++ ); + for ( i = 0; i < argc && strcasecmp ( argv[i], key ); i++ ) + { + ; + } - return i < argc ? i: -1; + return i < argc ? i : -1; } -static void find_arg_str( int argc, char *argv[], char *key, char** val ) +static void find_arg_str ( int argc, char *argv[], char *key, char** val ) { - int i = find_arg( argc, argv, key ); + int i = find_arg ( argc, argv, key ); - if ( val != NULL && i > 0 && i < argc-1 ) { - *val = argv[i+1]; + if ( val != NULL && i > 0 && i < argc - 1 ) + { + *val = argv[i + 1]; } } static void find_arg_int ( int argc, char *argv[], char *key, unsigned int *val ) { - int i = find_arg( argc, argv, key ); + int i = find_arg ( argc, argv, key ); - if ( val != NULL && i > 0 && i < ( argc-1 ) ) { - *val = strtol( argv[i+1], NULL, 10 ); + if ( val != NULL && i > 0 && i < ( argc - 1 ) ) + { + *val = strtol ( argv[i + 1], NULL, 10 ); } } unsigned int NumlockMask = 0; -Display *display = NULL; -Screen *screen; -Window root; -int screen_id; - -static int ( *xerror )( Display *, XErrorEvent * ); - -#define ATOM_ENUM(x) x -#define ATOM_CHAR(x) #x - -#define EWMH_ATOMS(X) \ -X(_NET_SUPPORTING_WM_CHECK),\ -X(_NET_CLIENT_LIST),\ -X(_NET_CLIENT_LIST_STACKING),\ -X(_NET_NUMBER_OF_DESKTOPS),\ -X(_NET_CURRENT_DESKTOP),\ -X(_NET_DESKTOP_GEOMETRY),\ -X(_NET_DESKTOP_VIEWPORT),\ -X(_NET_WORKAREA),\ -X(_NET_ACTIVE_WINDOW),\ -X(_NET_CLOSE_WINDOW),\ -X(_NET_MOVERESIZE_WINDOW),\ -X(_NET_WM_NAME),\ -X(_NET_WM_STATE),\ -X(_NET_WM_STATE_SKIP_TASKBAR),\ -X(_NET_WM_STATE_SKIP_PAGER),\ -X(_NET_WM_STATE_FULLSCREEN),\ -X(_NET_WM_STATE_ABOVE),\ -X(_NET_WM_STATE_BELOW),\ -X(_NET_WM_STATE_DEMANDS_ATTENTION),\ -X(_NET_WM_DESKTOP),\ -X(_NET_SUPPORTED) - -enum { EWMH_ATOMS( ATOM_ENUM ), NETATOMS }; -const char *netatom_names[] = { EWMH_ATOMS( ATOM_CHAR ) }; -Atom netatoms[NETATOMS]; +Display *display = NULL; +Screen *screen; +Window root; +int screen_id; + +static int ( *xerror )( Display *, XErrorEvent * ); + +#define ATOM_ENUM( x ) x +#define ATOM_CHAR( x ) # x + +#define EWMH_ATOMS( X ) \ + X ( _NET_SUPPORTING_WM_CHECK ), \ + X ( _NET_CLIENT_LIST ), \ + X ( _NET_CLIENT_LIST_STACKING ), \ + X ( _NET_NUMBER_OF_DESKTOPS ), \ + X ( _NET_CURRENT_DESKTOP ), \ + X ( _NET_DESKTOP_GEOMETRY ), \ + X ( _NET_DESKTOP_VIEWPORT ), \ + X ( _NET_WORKAREA ), \ + X ( _NET_ACTIVE_WINDOW ), \ + X ( _NET_CLOSE_WINDOW ), \ + X ( _NET_MOVERESIZE_WINDOW ), \ + X ( _NET_WM_NAME ), \ + X ( _NET_WM_STATE ), \ + X ( _NET_WM_STATE_SKIP_TASKBAR ), \ + X ( _NET_WM_STATE_SKIP_PAGER ), \ + X ( _NET_WM_STATE_FULLSCREEN ), \ + X ( _NET_WM_STATE_ABOVE ), \ + X ( _NET_WM_STATE_BELOW ), \ + X ( _NET_WM_STATE_DEMANDS_ATTENTION ), \ + X ( _NET_WM_DESKTOP ), \ + X ( _NET_SUPPORTED ) + +enum { EWMH_ATOMS ( ATOM_ENUM ), NETATOMS }; +const char *netatom_names[] = { EWMH_ATOMS ( ATOM_CHAR ) }; +Atom netatoms[NETATOMS]; // X error handler -int oops( __attribute__( ( unused ) ) Display *d, XErrorEvent *ee ) +int oops ( __attribute__( ( unused ) ) Display *d, XErrorEvent *ee ) { if ( ee->error_code == BadWindow || ( ee->request_code == X_GrabButton && ee->error_code == BadAccess ) || ( ee->request_code == X_GrabKey && ee->error_code == BadAccess ) - ) return 0; + ) + { + return 0; + } - fprintf( stderr, "error: request code=%d, error code=%d\n", ee->request_code, ee->error_code ); - return xerror( display, ee ); + fprintf ( stderr, "error: request code=%d, error code=%d\n", ee->request_code, ee->error_code ); + return xerror ( display, ee ); } // usable space on a monitor -typedef struct { +typedef struct +{ int x, y, w, h; int l, r, t, b; } workarea; // window lists -typedef struct { +typedef struct +{ Window *array; - void **data; - int len; + void **data; + int len; } winlist; winlist *cache_client; winlist *cache_xattr; -#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)--) +#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 )-- ) -#define WINLIST 32 +#define WINLIST 32 -winlist* winlist_new() +winlist* winlist_new () { - winlist *l = allocate( sizeof( winlist ) ); - l->len = 0; - l->array = allocate( sizeof( Window ) * ( WINLIST+1 ) ); - l->data = allocate( sizeof( void* ) * ( WINLIST+1 ) ); + winlist *l = allocate ( sizeof ( winlist ) ); + l->len = 0; + l->array = allocate ( sizeof ( Window ) * ( WINLIST + 1 ) ); + l->data = allocate ( sizeof ( void* ) * ( WINLIST + 1 ) ); return l; } -int winlist_append( winlist *l , Window w, void *d ) +int winlist_append ( winlist *l, Window w, void *d ) { - if ( l->len > 0 && !( l->len % WINLIST ) ) { - l->array = reallocate( l->array, sizeof( Window ) * ( l->len+WINLIST+1 ) ); - l->data = reallocate( l->data, sizeof( void* ) * ( l->len+WINLIST+1 ) ); + if ( l->len > 0 && !( l->len % WINLIST ) ) + { + l->array = reallocate ( l->array, sizeof ( Window ) * ( l->len + WINLIST + 1 ) ); + l->data = reallocate ( l->data, sizeof ( void* ) * ( l->len + WINLIST + 1 ) ); } // Make clang-check happy. // TODO: make clang-check clear this should never be 0. - if(l->data == NULL || l->array == NULL) return 0; + if ( l->data == NULL || l->array == NULL ) + { + return 0; + } - l->data[l->len] = d; + l->data[l->len] = d; l->array[l->len++] = w; - return l->len-1; + return l->len - 1; } -void winlist_empty( winlist *l ) +void winlist_empty ( winlist *l ) { - while ( l->len > 0 ) free( l->data[--( l->len )] ); + while ( l->len > 0 ) + { + free ( l->data[--( l->len )] ); + } } -void winlist_free( winlist *l ) +void winlist_free ( winlist *l ) { - winlist_empty( l ); - free( l->array ); - free( l->data ); - free( l ); + winlist_empty ( l ); + free ( l->array ); + free ( l->data ); + free ( l ); } -int winlist_find( winlist *l, Window w ) +int winlist_find ( winlist *l, Window w ) { // iterate backwards. theory is: windows most often accessed will be // nearer the end. testing with kcachegrind seems to support this... - int i; + int i; Window o = 0; - winlist_descend( l, i, o ) if ( w == o ) return i; + winlist_descend ( l, i, o ) if ( w == o ) + { + return i; + } return -1; } -#define CLIENTTITLE 100 -#define CLIENTCLASS 50 -#define CLIENTNAME 50 -#define CLIENTSTATE 10 -#define CLIENTROLE 50 +#define CLIENTTITLE 100 +#define CLIENTCLASS 50 +#define CLIENTNAME 50 +#define CLIENTSTATE 10 +#define CLIENTROLE 50 // a managable window -typedef struct { - Window window, trans; +typedef struct +{ + Window window, trans; XWindowAttributes xattr; - char title[CLIENTTITLE]; - char class[CLIENTCLASS]; - char name[CLIENTNAME]; - char role[CLIENTROLE]; - int states; - Atom state[CLIENTSTATE]; - workarea monitor; + char title[CLIENTTITLE]; + char class[CLIENTCLASS]; + char name[CLIENTNAME]; + char role[CLIENTROLE]; + int states; + Atom state[CLIENTSTATE]; + workarea monitor; } client; // allocate a pixel value for an X named color -static unsigned int color_get( const char *const name ) +static unsigned int color_get ( const char *const name ) { - XColor color; - Colormap map = DefaultColormap( display, screen_id ); - return XAllocNamedColor( display, map, name, &color, &color ) ? color.pixel: None; + XColor color; + Colormap map = DefaultColormap ( display, screen_id ); + return XAllocNamedColor ( display, map, name, &color, &color ) ? color.pixel : None; } // find mouse pointer location -int pointer_get( Window root, int *x, int *y ) +int pointer_get ( Window root, int *x, int *y ) { *x = 0; *y = 0; - Window rr, cr; - int rxr, ryr, wxr, wyr; + Window rr, cr; + int rxr, ryr, wxr, wyr; unsigned int mr; - if ( XQueryPointer( display, root, &rr, &cr, &rxr, &ryr, &wxr, &wyr, &mr ) ) { + if ( XQueryPointer ( display, root, &rr, &cr, &rxr, &ryr, &wxr, &wyr, &mr ) ) + { *x = rxr; *y = ryr; return 1; @@ -425,40 +478,45 @@ int pointer_get( Window root, int *x, int *y ) return 0; } -int take_keyboard( Window w ) +int take_keyboard ( Window w ) { int i; - for ( i = 0; i < 1000; i++ ) { - if ( XGrabKeyboard( display, w, True, GrabModeAsync, GrabModeAsync, CurrentTime ) == GrabSuccess ) + for ( i = 0; i < 1000; i++ ) + { + if ( XGrabKeyboard ( display, w, True, GrabModeAsync, GrabModeAsync, CurrentTime ) == GrabSuccess ) + { return 1; + } struct timespec rsl = { 0, 100000L }; - nanosleep(&rsl, NULL); + nanosleep ( &rsl, NULL ); } return 0; } -void release_keyboard() +void release_keyboard () { - XUngrabKeyboard( display, CurrentTime ); + XUngrabKeyboard ( display, CurrentTime ); } // XGetWindowAttributes with caching -XWindowAttributes* window_get_attributes( Window w ) +XWindowAttributes* window_get_attributes ( Window w ) { - int idx = winlist_find( cache_xattr, w ); + int idx = winlist_find ( cache_xattr, w ); - if ( idx < 0 ) { - XWindowAttributes *cattr = allocate( sizeof( XWindowAttributes ) ); + if ( idx < 0 ) + { + XWindowAttributes *cattr = allocate ( sizeof ( XWindowAttributes ) ); - if ( XGetWindowAttributes( display, w, cattr ) ) { - winlist_append( cache_xattr, w, cattr ); + if ( XGetWindowAttributes ( display, w, cattr ) ) + { + winlist_append ( cache_xattr, w, cattr ); return cattr; } - free( cattr ); + free ( cattr ); return NULL; } @@ -466,31 +524,47 @@ XWindowAttributes* window_get_attributes( Window w ) } // retrieve a property of any type from a window -int window_get_prop( Window w, Atom prop, Atom *type, int *items, void *buffer, unsigned int bytes ) +int window_get_prop ( Window w, Atom prop, Atom *type, int *items, void *buffer, unsigned int bytes ) { Atom _type; - if ( !type ) type = &_type; + if ( !type ) + { + type = &_type; + } int _items; - if ( !items ) items = &_items; + if ( !items ) + { + items = &_items; + } - int format; + int format; unsigned long nitems, nbytes; unsigned char *ret = NULL; - memset( buffer, 0, bytes ); - - if ( XGetWindowProperty( display, w, prop, 0, bytes/4, False, AnyPropertyType, type, - &format, &nitems, &nbytes, &ret ) == Success && ret && *type != None && format ) { - if ( format == 8 ) memmove( buffer, ret, MIN( bytes, nitems ) ); + memset ( buffer, 0, bytes ); + + if ( XGetWindowProperty ( display, w, prop, 0, bytes / 4, False, AnyPropertyType, type, + &format, &nitems, &nbytes, &ret ) == Success && ret && *type != None && format ) + { + if ( format == 8 ) + { + memmove ( buffer, ret, MIN ( bytes, nitems ) ); + } - if ( format == 16 ) memmove( buffer, ret, MIN( bytes, nitems * sizeof( short ) ) ); + if ( format == 16 ) + { + memmove ( buffer, ret, MIN ( bytes, nitems * sizeof ( short ) ) ); + } - if ( format == 32 ) memmove( buffer, ret, MIN( bytes, nitems * sizeof( long ) ) ); + if ( format == 32 ) + { + memmove ( buffer, ret, MIN ( bytes, nitems * sizeof ( long ) ) ); + } - *items = ( int )nitems; - XFree( ret ); + *items = ( int ) nitems; + XFree ( ret ); return 1; } @@ -499,85 +573,98 @@ int window_get_prop( Window w, Atom prop, Atom *type, int *items, void *buffer, // retrieve a text property from a window // technically we could use window_get_prop(), but this is better for character set support -char* window_get_text_prop( Window w, Atom atom ) +char* window_get_text_prop ( Window w, Atom atom ) { XTextProperty prop; - char *res = NULL; - char **list = NULL; - int count; - - if ( XGetTextProperty( display, w, &prop, atom ) && prop.value && prop.nitems ) { - if ( prop.encoding == XA_STRING ) { - res = allocate( strlen( ( char* )prop.value )+1 ); + char *res = NULL; + char **list = NULL; + int count; + + if ( XGetTextProperty ( display, w, &prop, atom ) && prop.value && prop.nitems ) + { + if ( prop.encoding == XA_STRING ) + { + res = allocate ( strlen ( ( char * ) prop.value ) + 1 ); // make clang-check happy. - if(res) { - strcpy( res, ( char* )prop.value ); + if ( res ) + { + strcpy ( res, ( char * ) prop.value ); } - } else if ( Xutf8TextPropertyToTextList( display, &prop, &list, &count ) >= Success && count > 0 && *list ) { - res = allocate( strlen( *list )+1 ); + } + else if ( Xutf8TextPropertyToTextList ( display, &prop, &list, &count ) >= Success && count > 0 && *list ) + { + res = allocate ( strlen ( *list ) + 1 ); // make clang-check happy. - if(res) { - strcpy( res, *list ); + if ( res ) + { + strcpy ( res, *list ); } - XFreeStringList( list ); + XFreeStringList ( list ); } } - if ( prop.value ) XFree( prop.value ); + if ( prop.value ) + { + XFree ( prop.value ); + } return res; } -int window_get_atom_prop( Window w, Atom atom, Atom *list, int count ) +int window_get_atom_prop ( Window w, Atom atom, Atom *list, int count ) { Atom type; - int items; - return window_get_prop( w, atom, &type, &items, list, count*sizeof( Atom ) ) && type == XA_ATOM ? items:0; + int items; + return window_get_prop ( w, atom, &type, &items, list, count * sizeof ( Atom ) ) && type == XA_ATOM ? items : 0; } -void window_set_atom_prop( Window w, Atom prop, Atom *atoms, int count ) +void window_set_atom_prop ( Window w, Atom prop, Atom *atoms, int count ) { - XChangeProperty( display, w, prop, XA_ATOM, 32, PropModeReplace, ( unsigned char* )atoms, count ); + XChangeProperty ( display, w, prop, XA_ATOM, 32, PropModeReplace, ( unsigned char * ) atoms, count ); } -int window_get_cardinal_prop(Window w, Atom atom, unsigned long *list, int count) +int window_get_cardinal_prop ( Window w, Atom atom, unsigned long *list, int count ) { Atom type; int items; - return window_get_prop(w, atom, &type, &items, list, count*sizeof(unsigned long)) && type == XA_CARDINAL ? items:0; + return window_get_prop ( w, atom, &type, &items, list, count * sizeof ( unsigned long ) ) && type == XA_CARDINAL ? items : 0; } // a ClientMessage -int window_send_message( Window target, Window subject, Atom atom, unsigned long protocol, unsigned long mask, Time time ) +int window_send_message ( Window target, Window subject, Atom atom, unsigned long protocol, unsigned long mask, Time time ) { XEvent e; - memset( &e, 0, sizeof( XEvent ) ); - e.xclient.type = ClientMessage; + memset ( &e, 0, sizeof ( XEvent ) ); + e.xclient.type = ClientMessage; e.xclient.message_type = atom; - e.xclient.window = subject; + e.xclient.window = subject; e.xclient.data.l[0] = protocol; - e.xclient.data.l[1] = time; + e.xclient.data.l[1] = time; e.xclient.send_event = True; - e.xclient.format = 32; - int r = XSendEvent( display, target, False, mask, &e ) ?1:0; - XFlush( display ); + e.xclient.format = 32; + int r = XSendEvent ( display, target, False, mask, &e ) ? 1 : 0; + XFlush ( display ); return r; } // find the dimensions of the monitor displaying point x,y -void monitor_dimensions( Screen *screen, int x, int y, workarea *mon ) +void monitor_dimensions ( Screen *screen, int x, int y, workarea *mon ) { - memset( mon, 0, sizeof( workarea ) ); - mon->w = WidthOfScreen( screen ); - mon->h = HeightOfScreen( screen ); + memset ( mon, 0, sizeof ( workarea ) ); + mon->w = WidthOfScreen ( screen ); + mon->h = HeightOfScreen ( screen ); // locate the current monitor - if ( XineramaIsActive( display ) ) { - int monitors; - XineramaScreenInfo *info = XineramaQueryScreens( display, &monitors ); - - if ( info ) { - for ( int i = 0; i < monitors; i++ ) { - if ( INTERSECT( x, y, 1, 1, info[i].x_org, info[i].y_org, info[i].width, info[i].height ) ) { + if ( XineramaIsActive ( display ) ) + { + int monitors; + XineramaScreenInfo *info = XineramaQueryScreens ( display, &monitors ); + + if ( info ) + { + for ( int i = 0; i < monitors; i++ ) + { + if ( INTERSECT ( x, y, 1, 1, info[i].x_org, info[i].y_org, info[i].width, info[i].height ) ) + { mon->x = info[i].x_org; mon->y = info[i].y_org; mon->w = info[i].width; @@ -587,349 +674,442 @@ void monitor_dimensions( Screen *screen, int x, int y, workarea *mon ) } } - XFree( info ); + XFree ( info ); } } // determine which monitor holds the active window, or failing that the mouse pointer -void monitor_active( workarea *mon ) +void monitor_active ( workarea *mon ) { - Window root = RootWindow( display, XScreenNumberOfScreen( screen ) ); + Window root = RootWindow ( display, XScreenNumberOfScreen ( screen ) ); unsigned long id; - Atom type; - int count; - - if ( window_get_prop( root, netatoms[_NET_ACTIVE_WINDOW], &type, &count, &id, 1 ) - && type == XA_WINDOW && count > 0 ) { - XWindowAttributes *attr = window_get_attributes( id ); - monitor_dimensions( screen, attr->x, attr->y, mon ); + Atom type; + int count; + + if ( window_get_prop ( root, netatoms[_NET_ACTIVE_WINDOW], &type, &count, &id, 1 ) + && type == XA_WINDOW && count > 0 ) + { + XWindowAttributes *attr = window_get_attributes ( id ); + monitor_dimensions ( screen, attr->x, attr->y, mon ); return; } int x, y; - if ( pointer_get( root, &x, &y ) ) { - monitor_dimensions( screen, x, y, mon ); + if ( pointer_get ( root, &x, &y ) ) + { + monitor_dimensions ( screen, x, y, mon ); return; } - monitor_dimensions( screen, 0, 0, mon ); + monitor_dimensions ( screen, 0, 0, mon ); } // _NET_WM_STATE_* -int client_has_state( client *c, Atom state ) +int client_has_state ( client *c, Atom state ) { int i; for ( i = 0; i < c->states; i++ ) - if ( c->state[i] == state ) return 1; + { + if ( c->state[i] == state ) + { + return 1; + } + } return 0; } // collect info on any window // doesn't have to be a window we'll end up managing -client* window_client( Window win ) +client* window_client ( Window win ) { - if ( win == None ) return NULL; + if ( win == None ) + { + return NULL; + } - int idx = winlist_find( cache_client, win ); + int idx = winlist_find ( cache_client, win ); - if ( idx >= 0 ) { + if ( idx >= 0 ) + { return cache_client->data[idx]; } // if this fails, we're up that creek - XWindowAttributes *attr = window_get_attributes( win ); + XWindowAttributes *attr = window_get_attributes ( win ); - if ( !attr ) return NULL; + if ( !attr ) + { + return NULL; + } - client *c = allocate_clear( sizeof( client ) ); + client *c = allocate_clear ( sizeof ( client ) ); c->window = win; // copy xattr so we don't have to care when stuff is freed - memmove( &c->xattr, attr, sizeof( XWindowAttributes ) ); - XGetTransientForHint( display, win, &c->trans ); + memmove ( &c->xattr, attr, sizeof ( XWindowAttributes ) ); + XGetTransientForHint ( display, win, &c->trans ); - c->states = window_get_atom_prop( win, netatoms[_NET_WM_STATE], c->state, CLIENTSTATE ); + c->states = window_get_atom_prop ( win, netatoms[_NET_WM_STATE], c->state, CLIENTSTATE ); char *name; - if ( ( name = window_get_text_prop( c->window, netatoms[_NET_WM_NAME] ) ) && name ) { - snprintf( c->title, CLIENTTITLE, "%s", name ); - free( name ); - } else if ( XFetchName( display, c->window, &name ) ) { - snprintf( c->title, CLIENTTITLE, "%s", name ); - XFree( name ); + if ( ( name = window_get_text_prop ( c->window, netatoms[_NET_WM_NAME] ) ) && name ) + { + snprintf ( c->title, CLIENTTITLE, "%s", name ); + free ( name ); + } + else if ( XFetchName ( display, c->window, &name ) ) + { + snprintf ( c->title, CLIENTTITLE, "%s", name ); + XFree ( name ); } - name = window_get_text_prop ( c->window, XInternAtom( display, "WM_WINDOW_ROLE", False ) ); + name = window_get_text_prop ( c->window, XInternAtom ( display, "WM_WINDOW_ROLE", False ) ); - if ( name != NULL ) { - snprintf( c->role, CLIENTROLE, "%s", name ); - XFree( name ); + if ( name != NULL ) + { + snprintf ( c->role, CLIENTROLE, "%s", name ); + XFree ( name ); } XClassHint chint; - if ( XGetClassHint( display, c->window, &chint ) ) { - snprintf( c->class, CLIENTCLASS, "%s", chint.res_class ); - snprintf( c->name, CLIENTNAME, "%s", chint.res_name ); - XFree( chint.res_class ); - XFree( chint.res_name ); + if ( XGetClassHint ( display, c->window, &chint ) ) + { + snprintf ( c->class, CLIENTCLASS, "%s", chint.res_class ); + snprintf ( c->name, CLIENTNAME, "%s", chint.res_name ); + XFree ( chint.res_class ); + XFree ( chint.res_name ); } - monitor_dimensions( c->xattr.screen, c->xattr.x, c->xattr.y, &c->monitor ); - winlist_append( cache_client, c->window, c ); + monitor_dimensions ( c->xattr.screen, c->xattr.x, c->xattr.y, &c->monitor ); + winlist_append ( cache_client, c->window, c ); return c; } unsigned int windows_modmask; -KeySym windows_keysym; +KeySym windows_keysym; unsigned int rundialog_modmask; -KeySym rundialog_keysym; +KeySym rundialog_keysym; unsigned int sshdialog_modmask; -KeySym sshdialog_keysym; +KeySym sshdialog_keysym; -Window main_window = None; -GC gc = NULL; +Window main_window = None; +GC gc = NULL; #include "textbox.h" -void menu_draw( textbox *text, textbox **boxes, int max_lines,int num_lines, int *last_offset, int selected, char **filtered ) +void menu_draw ( textbox *text, textbox **boxes, int max_lines, int num_lines, int *last_offset, int selected, char **filtered ) { - int i,offset=0; - textbox_draw( text ); + int i, offset = 0; + textbox_draw ( text ); // selected row is always visible. // If selected is visible do not scroll. - if ( ( selected - ( *last_offset ) ) < ( max_lines ) &&( selected-( *last_offset ) ) >= 0 ) offset = *last_offset; - else { + if ( ( selected - ( *last_offset ) ) < ( max_lines ) && ( selected - ( *last_offset ) ) >= 0 ) + { + offset = *last_offset; + } + else + { // If selected is above visible, scroll up. - if ( ( selected-( *last_offset ) ) >= ( max_lines ) ) { - offset = selected-max_lines+1; + if ( ( selected - ( *last_offset ) ) >= ( max_lines ) ) + { + offset = selected - max_lines + 1; // Scroll down otherwise - } else if ( ( selected-( *last_offset ) ) < 0 ) { + } + else if ( ( selected - ( *last_offset ) ) < 0 ) + { offse |