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

Split of run-dialog part in separate file.

This commit is contained in:
QC 2014-01-20 23:36:20 +01:00
parent f3a77c9b00
commit 5e72018497
5 changed files with 325 additions and 245 deletions

8
include/run-dialog.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef __RUN_DIALOG_H__
#define __RUN_DIALOG_H__
SwitcherMode run_switcher_dialog ( char **input );
#endif

38
include/simpleswitcher.h Normal file
View file

@ -0,0 +1,38 @@
#ifndef __SIMPLESWITCHER_H__
#define __SIMPLESWITCHER_H__
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define NEAR(a,o,b) ((b) > (a)-(o) && (b) < (a)+(o))
#define OVERLAP(a,b,c,d) (((a)==(c) && (b)==(d)) || MIN((a)+(b), (c)+(d)) - MAX((a), (c)) > 0)
#define INTERSECT(x,y,w,h,x1,y1,w1,h1) (OVERLAP((x),(w),(x1),(w1)) && OVERLAP((y),(h),(y1),(h1)))
typedef enum {
WINDOW_SWITCHER,
RUN_DIALOG,
MODE_EXIT
} SwitcherMode;
// window lists
typedef struct {
Window *array;
void **data;
int len;
} winlist;
int menu( char **lines, char **input, char *prompt,
int selected, Time *time, int *shift, winlist *ids );
/**
* Allocator wrappers
*/
void* allocate( unsigned long bytes );
void* allocate_clear( unsigned long bytes );
void* reallocate( void *ptr, unsigned long bytes );
void catch_exit( __attribute__( ( unused ) ) int sig );
#endif

262
source/run-dialog.c Normal file
View file

@ -0,0 +1,262 @@
/**
* simpleswitcher
*
* MIT/X11 License
* Copyright 2013-2014 Qball Cow <qball@gmpclient.org>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#define _GNU_SOURCE
#include <X11/X.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xmd.h>
#include <X11/Xutil.h>
#include <X11/Xproto.h>
#include <X11/keysym.h>
#include <X11/XKBlib.h>
#include <X11/Xft/Xft.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <dirent.h>
#include <strings.h>
#include <string.h>
#include "simpleswitcher.h"
#include "run-dialog.h"
extern char *config_terminal_emulator;
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 );
}
// execute sub-process
static pid_t exec_cmd( const char *cmd, int run_in_term )
{
if ( !cmd || !cmd[0] ) return -1;
signal( SIGCHLD, catch_exit );
pid_t pid = fork();
if ( !pid ) {
setsid();
execsh( cmd, run_in_term );
exit( EXIT_FAILURE );
}
int curr = -1;
unsigned int index = 0;
char **retv = NULL;
const char *hd = getenv( "HOME" );
if ( hd == NULL ) return pid;
/**
* This happens in non-critical time (After launching app)
* It is allowed to be a bit slower.
*/
char *path = allocate( strlen( hd ) + strlen( "/.simpleswitcher.cache" )+2 );
sprintf( path, "%s/%s", hd, ".simpleswitcher.cache" );
FILE *fd = fopen ( path, "r" );
char buffer[1024];
if ( fd != NULL ) {
while ( fgets( buffer,1024,fd ) != NULL ) {
retv = realloc( retv, ( index+2 )*sizeof( char* ) );
buffer[strlen( buffer )-1] = '\0';
retv[index] = strdup( buffer );
retv[index+1] = NULL;
if ( strcasecmp( retv[index], cmd ) == 0 ) {
curr = index;
}
index++;
}
fclose( fd );
}
/**
* Write out the last 25 results again.
*/
fd = fopen ( path, "w" );
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 ) {
fputs( retv[i], fd );
fputc( '\n', fd );
}
}
fclose( fd );
}
for ( int i=0; retv[i] != NULL; i++ ) {
free( retv[i] );
}
free( retv );
free( path );
return pid;
}
int sort_func ( const void *a, const void *b )
{
const char *astr = *( const char * const * )a;
const char *bstr = *( const char * const * )b;
return strcasecmp( astr,bstr );
}
static char ** get_apps ( )
{
int num_favorites = 0;
unsigned int index = 0;
char *path;
char **retv = NULL;
#ifdef TIMING
struct timespec start, stop;
clock_gettime( CLOCK_REALTIME, &start );
#endif
if ( getenv( "PATH" ) == NULL ) return NULL;
const char *hd = getenv( "HOME" );
if ( hd == NULL ) return NULL;
path = allocate( strlen( hd ) + strlen( "/.simpleswitcher.cache" )+2 );
sprintf( path, "%s/%s", hd, ".simpleswitcher.cache" );
FILE *fd = fopen ( path, "r" );
char buffer[1024];
if ( fd != NULL ) {
while ( fgets( buffer,1024,fd ) != NULL ) {
retv = realloc( retv, ( index+2 )*sizeof( char* ) );
buffer[strlen( buffer )-1] = '\0';
retv[index] = strdup( buffer );
retv[index+1] = NULL;
index++;
num_favorites++;
}
fclose( fd );
}
free( path );
path = strdup( getenv( "PATH" ) );
for ( const char *dirname = strtok( path, ":" ); dirname != NULL; dirname = strtok( NULL, ":" ) ) {
DIR *dir = opendir( dirname );
if ( dir != NULL ) {
struct dirent *dent;
while ( ( dent=readdir( dir ) )!=NULL ) {
if ( dent->d_type != DT_REG &&
dent->d_type != DT_LNK &&
dent->d_type != DT_UNKNOWN ) {
continue;
}
int found = 0;
// This is a nice little penalty, but doable? time will tell.
// given num_favorites is max 25.
for ( int j = 0; found == 0 && j < num_favorites; j++ ) {
if ( strcasecmp( dent->d_name, retv[j] ) == 0 ) found = 1;
}
if ( found == 1 ) continue;
retv = realloc( retv, ( index+2 )*sizeof( char* ) );
retv[index] = strdup( dent->d_name );
retv[index+1] = NULL;
index++;
}
closedir( dir );
}
}
// TODO: check this is still fast enough. (takes 1ms on laptop.)
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 ) {
stop.tv_nsec += ( stop.tv_sec-start.tv_sec )*1e9;
}
long diff = stop.tv_nsec-start.tv_nsec;
printf( "Time elapsed: %ld us\n", diff/1000 );
#endif
return retv;
}
SwitcherMode run_switcher_dialog ( char **input )
{
SwitcherMode retv = MODE_EXIT;
// act as a launcher
char **cmd_list = get_apps( );
if ( cmd_list == NULL ) {
cmd_list = allocate( 2*sizeof( char * ) );
cmd_list[0] = strdup( "No applications found" );
cmd_list[1] = NULL;
}
int shift=0;
int n = menu( cmd_list, input, "$ ", 0, NULL, &shift,NULL );
if ( n == -2 ) {
retv = WINDOW_SWITCHER;
} else if ( n >=0 && cmd_list[n] != NULL ) {
exec_cmd( cmd_list[n], shift );
} else if ( n == -3 && *input != NULL && *input[0] != '\0' ) {
exec_cmd( *input, shift );
}
for ( int i=0; cmd_list[i] != NULL; i++ ) {
free( cmd_list[i] );
}
free( cmd_list );
return retv;
}

View file

@ -27,15 +27,6 @@
*/ */
#define _GNU_SOURCE #define _GNU_SOURCE
#include <X11/X.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xmd.h>
#include <X11/Xutil.h>
#include <X11/Xproto.h>
#include <X11/keysym.h>
#include <X11/XKBlib.h>
#include <X11/Xft/Xft.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -47,9 +38,17 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <fcntl.h> #include <fcntl.h>
#include <err.h> #include <err.h>
#include <X11/X.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xmd.h>
#include <X11/Xutil.h>
#include <X11/Xproto.h>
#include <X11/keysym.h>
#include <X11/XKBlib.h>
#include <X11/Xft/Xft.h>
#include <X11/extensions/Xinerama.h> #include <X11/extensions/Xinerama.h>
#include <sys/types.h>
#include <dirent.h>
#ifdef I3 #ifdef I3
#include <errno.h> #include <errno.h>
#include <linux/un.h> #include <linux/un.h>
@ -58,12 +57,8 @@
#include <i3/ipc.h> #include <i3/ipc.h>
#endif #endif
#include "simpleswitcher.h"
#define MAX(a, b) ((a) > (b) ? (a) : (b)) #include "run-dialog.h"
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define NEAR(a,o,b) ((b) > (a)-(o) && (b) < (a)+(o))
#define OVERLAP(a,b,c,d) (((a)==(c) && (b)==(d)) || MIN((a)+(b), (c)+(d)) - MAX((a), (c)) > 0)
#define INTERSECT(x,y,w,h,x1,y1,w1,h1) (OVERLAP((x),(w),(x1),(w1)) && OVERLAP((y),(h),(y1),(h1)))
#define INNER_MARGIN 5 #define INNER_MARGIN 5
@ -84,7 +79,7 @@ char *config_terminal_emulator;
static void* allocate( unsigned long bytes ) void* allocate( unsigned long bytes )
{ {
void *ptr = malloc( bytes ); void *ptr = malloc( bytes );
@ -95,13 +90,13 @@ static void* allocate( unsigned long bytes )
return ptr; return ptr;
} }
static void* allocate_clear( unsigned long bytes ) void* allocate_clear( unsigned long bytes )
{ {
void *ptr = allocate( bytes ); void *ptr = allocate( bytes );
memset( ptr, 0, bytes ); memset( ptr, 0, bytes );
return ptr; return ptr;
} }
static void* reallocate( void *ptr, unsigned long bytes ) void* reallocate( void *ptr, unsigned long bytes )
{ {
ptr = realloc( ptr, bytes ); ptr = realloc( ptr, bytes );
@ -113,6 +108,7 @@ static void* reallocate( void *ptr, unsigned long bytes )
return ptr; return ptr;
} }
static char **tokenize( const char *input ) static char **tokenize( const char *input )
{ {
if ( input == NULL ) return NULL; if ( input == NULL ) return NULL;
@ -217,92 +213,7 @@ void catch_exit( __attribute__( ( unused ) ) int sig )
while ( 0 < waitpid( -1, NULL, WNOHANG ) ); while ( 0 < waitpid( -1, NULL, WNOHANG ) );
} }
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 );
}
// execute sub-process
static pid_t exec_cmd( const char *cmd, int run_in_term )
{
if ( !cmd || !cmd[0] ) return -1;
signal( SIGCHLD, catch_exit );
pid_t pid = fork();
if ( !pid ) {
setsid();
execsh( cmd, run_in_term );
exit( EXIT_FAILURE );
}
int curr = -1;
unsigned int index = 0;
char **retv = NULL;
const char *hd = getenv( "HOME" );
if ( hd == NULL ) return pid;
/**
* This happens in non-critical time (After launching app)
* It is allowed to be a bit slower.
*/
char *path = allocate( strlen( hd ) + strlen( "/.simpleswitcher.cache" )+2 );
sprintf( path, "%s/%s", hd, ".simpleswitcher.cache" );
FILE *fd = fopen ( path, "r" );
char buffer[1024];
if ( fd != NULL ) {
while ( fgets( buffer,1024,fd ) != NULL ) {
retv = realloc( retv, ( index+2 )*sizeof( char* ) );
buffer[strlen( buffer )-1] = '\0';
retv[index] = strdup( buffer );
retv[index+1] = NULL;
if ( strcasecmp( retv[index], cmd ) == 0 ) {
curr = index;
}
index++;
}
fclose( fd );
}
/**
* Write out the last 25 results again.
*/
fd = fopen ( path, "w" );
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 ) {
fputs( retv[i], fd );
fputc( '\n', fd );
}
}
fclose( fd );
}
for ( int i=0; retv[i] != NULL; i++ ) {
free( retv[i] );
}
free( retv );
free( path );
return pid;
}
// cli arg handling // 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 )
{ {
@ -399,12 +310,6 @@ typedef struct {
int l, r, t, b; int l, r, t, b;
} workarea; } workarea;
// window lists
typedef struct {
Window *array;
void **data;
int len;
} winlist;
winlist *cache_client; winlist *cache_client;
winlist *cache_xattr; winlist *cache_xattr;
@ -1143,140 +1048,9 @@ int menu( char **lines, char **input, char *prompt, int selected, Time *time, in
#define FORK 1 #define FORK 1
#define NOFORK 2 #define NOFORK 2
int sort_func ( const void *a, const void *b )
{
const char *astr = *( const char * const * )a;
const char *bstr = *( const char * const * )b;
return strcasecmp( astr,bstr );
}
static char ** get_apps ( )
{
int num_favorites = 0;
unsigned int index = 0;
char *path;
char **retv = NULL;
#ifdef TIMING
struct timespec start, stop;
clock_gettime( CLOCK_REALTIME, &start );
#endif
if ( getenv( "PATH" ) == NULL ) return NULL;
const char *hd = getenv( "HOME" );
if ( hd == NULL ) return NULL;
path = allocate( strlen( hd ) + strlen( "/.simpleswitcher.cache" )+2 );
sprintf( path, "%s/%s", hd, ".simpleswitcher.cache" );
FILE *fd = fopen ( path, "r" );
char buffer[1024];
if ( fd != NULL ) {
while ( fgets( buffer,1024,fd ) != NULL ) {
retv = realloc( retv, ( index+2 )*sizeof( char* ) );
buffer[strlen( buffer )-1] = '\0';
retv[index] = strdup( buffer );
retv[index+1] = NULL;
index++;
num_favorites++;
}
fclose( fd );
}
free( path );
path = strdup( getenv( "PATH" ) );
for ( const char *dirname = strtok( path, ":" ); dirname != NULL; dirname = strtok( NULL, ":" ) ) {
DIR *dir = opendir( dirname );
if ( dir != NULL ) {
struct dirent *dent;
while ( ( dent=readdir( dir ) )!=NULL ) {
if ( dent->d_type != DT_REG &&
dent->d_type != DT_LNK &&
dent->d_type != DT_UNKNOWN ) {
continue;
}
int found = 0;
// This is a nice little penalty, but doable? time will tell.
// given num_favorites is max 25.
for ( int j = 0; found == 0 && j < num_favorites; j++ ) {
if ( strcasecmp( dent->d_name, retv[j] ) == 0 ) found = 1;
}
if ( found == 1 ) continue;
retv = realloc( retv, ( index+2 )*sizeof( char* ) );
retv[index] = strdup( dent->d_name );
retv[index+1] = NULL;
index++;
}
closedir( dir );
}
}
// TODO: check this is still fast enough. (takes 1ms on laptop.)
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 ) {
stop.tv_nsec += ( stop.tv_sec-start.tv_sec )*1e9;
}
long diff = stop.tv_nsec-start.tv_nsec;
printf( "Time elapsed: %ld us\n", diff/1000 );
#endif
return retv;
}
typedef enum {
WINDOW_SWITCHER,
RUN_DIALOG,
MODE_EXIT
} SwitcherMode;
SwitcherMode run_switcher_dialog ( char **input )
{
Time time;
SwitcherMode retv = MODE_EXIT;
// act as a launcher
char **cmd_list = get_apps( );
if ( cmd_list == NULL ) {
cmd_list = allocate( 2*sizeof( char * ) );
cmd_list[0] = strdup( "No applications found" );
cmd_list[1] = NULL;
}
int shift=0;
int n = menu( cmd_list, input, "$ ", 0, &time, &shift,NULL );
if ( n == -2 ) {
retv = WINDOW_SWITCHER;
} else if ( n >=0 && cmd_list[n] != NULL ) {
exec_cmd( cmd_list[n], shift );
} else if ( n == -3 && *input != NULL && *input[0] != '\0' ) {
exec_cmd( *input, shift );
}
for ( int i=0; cmd_list[i] != NULL; i++ ) {
free( cmd_list[i] );
}
free( cmd_list );
return retv;
}
SwitcherMode run_switcher_window ( char **input ) SwitcherMode run_switcher_window ( char **input )
{ {

View file

@ -37,11 +37,9 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <X11/Xft/Xft.h> #include <X11/Xft/Xft.h>
#include <ctype.h> #include <ctype.h>
#include "simpleswitcher.h"
#include "textbox.h" #include "textbox.h"
#define SIDE_MARGIN 3 #define SIDE_MARGIN 3
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
extern Display *display; extern Display *display;