rofi/source/run-dialog.c

434 lines
11 KiB
C
Raw Normal View History

/**
2014-03-01 16:27:52 +00:00
* rofi
*
* 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
2014-01-20 23:20:09 +00:00
#include <stdlib.h>
#include <stdio.h>
#include <X11/X.h>
#include <unistd.h>
2014-03-08 20:35:27 +00:00
#include <limits.h>
#include <signal.h>
#include <sys/types.h>
#include <dirent.h>
#include <strings.h>
#include <string.h>
2014-01-21 13:56:25 +00:00
#include <errno.h>
2014-03-12 07:41:38 +00:00
#include "rofi.h"
#include "run-dialog.h"
2014-01-20 23:20:09 +00:00
#ifdef TIMING
#include <time.h>
#endif
2014-03-22 20:04:19 +00:00
#define RUN_CACHE_FILE "rofi-2.runcache"
2014-01-25 22:37:37 +00:00
2014-03-22 20:04:19 +00:00
static inline int execsh ( const char *cmd, int run_in_term )
{
// use sh for args parsing
if ( run_in_term )
2014-03-22 20:04:19 +00:00
{
return execlp ( config.terminal_emulator, config.terminal_emulator, "-e", "sh", "-c", cmd, NULL );
}
2014-03-22 20:04:19 +00:00
return execlp ( "/bin/sh", "sh", "-c", cmd, NULL );
}
2014-03-08 20:35:27 +00:00
2014-04-23 06:47:46 +00:00
#define RUN_DIALOG_NAME_LENGTH 256
2014-03-22 20:04:19 +00:00
typedef struct _element
{
2014-03-08 20:35:27 +00:00
long int index;
2014-04-23 06:47:46 +00:00
char name[RUN_DIALOG_NAME_LENGTH];
2014-03-08 20:35:27 +00:00
}element;
2014-03-22 20:04:19 +00:00
static int element_sort_func ( const void *ea, const void *eb )
2014-03-08 20:35:27 +00:00
{
2014-03-22 20:04:19 +00:00
element *a = *(element * *) ea;
element *b = *(element * *) eb;
2014-03-08 20:35:27 +00:00
return b->index - a->index;
}
// execute sub-process
2014-03-22 20:04:19 +00:00
static pid_t exec_cmd ( const char *cmd, int run_in_term )
{
2014-03-22 20:04:19 +00:00
if ( !cmd || !cmd[0] )
{
return -1;
}
2014-03-22 20:04:19 +00:00
signal ( SIGCHLD, catch_exit );
pid_t pid = fork ();
2014-03-22 20:04:19 +00:00
if ( !pid )
{
setsid ();
execsh ( cmd, run_in_term );
exit ( EXIT_FAILURE );
}
2014-03-22 20:04:19 +00:00
int curr = -1;
unsigned int index = 0;
element **retv = NULL;
/**
* This happens in non-critical time (After launching app)
* It is allowed to be a bit slower.
*/
2014-04-23 06:47:46 +00:00
char *path = NULL;
if(asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) == -1) {
return -1;
}
2014-03-22 20:04:19 +00:00
FILE *fd = fopen ( path, "r" );
2014-03-22 20:04:19 +00:00
if ( fd != NULL )
{
2014-02-27 19:26:35 +00:00
char buffer[1024];
2014-03-22 20:04:19 +00:00
while ( fgets ( buffer, 1024, fd ) != NULL )
{
if ( strlen ( buffer ) == 0 )
{
continue;
}
2014-04-23 06:47:46 +00:00
retv = reallocate ( retv, ( index + 2 ) * sizeof ( element* ) );
retv[index] = allocate ( sizeof ( element ) );
// remove trailing \n
2014-04-24 21:39:26 +00:00
buffer[strlen ( buffer ) - 1] = '\0';
2014-03-08 20:35:27 +00:00
char * start = NULL;
2014-03-22 20:04:19 +00:00
retv[index]->index = strtol ( buffer, &start, 10 );
2014-04-23 06:47:46 +00:00
snprintf ( retv[index]->name, RUN_DIALOG_NAME_LENGTH, "%s", start + 1 );
2014-03-22 20:04:19 +00:00
retv[index + 1] = NULL;
2014-03-22 20:04:19 +00:00
if ( strcasecmp ( retv[index]->name, cmd ) == 0 )
{
curr = index;
}
index++;
}
2014-03-22 20:04:19 +00:00
fclose ( fd );
}
2014-03-22 20:04:19 +00:00
if ( curr < 0 )
{
retv = reallocate ( retv, ( index + 2 ) * sizeof ( element* ) );
retv[index] = allocate ( sizeof ( element ) );
retv[index]->index = 1;
2014-04-23 06:47:46 +00:00
snprintf ( retv[index]->name, RUN_DIALOG_NAME_LENGTH, "%s", cmd );
2014-03-22 20:04:19 +00:00
index++;
}
else
{
retv[curr]->index++;
2014-03-08 20:35:27 +00:00
}
// Sort the list.
2014-03-22 20:04:19 +00:00
qsort ( retv, index, sizeof ( element* ), element_sort_func );
2014-03-08 20:35:27 +00:00
/**
* Write out the last 25 results again.
*/
fd = fopen ( path, "w" );
2014-03-22 20:04:19 +00:00
if ( fd )
{
for ( int i = 0; i < ( int ) index && i < 20; i++ )
{
if ( retv[i]->name && retv[i]->name[0] != '\0' )
{
fprintf ( fd, "%ld %s\n",
retv[i]->index,
retv[i]->name
);
2014-03-12 16:38:24 +00:00
}
}
2014-03-22 20:04:19 +00:00
fclose ( fd );
}
2014-03-22 20:04:19 +00:00
for ( int i = 0; retv != NULL && retv[i] != NULL; i++ )
{
free ( retv[i] );
}
2014-03-22 20:04:19 +00:00
free ( retv );
2014-03-22 20:04:19 +00:00
free ( path );
return pid;
}
// execute sub-process
2014-03-22 20:04:19 +00:00
static void delete_entry ( const char *cmd )
{
2014-03-22 20:04:19 +00:00
int curr = -1;
unsigned int index = 0;
element **retv = NULL;
2014-03-12 16:38:24 +00:00
/**
* This happens in non-critical time (After launching app)
* It is allowed to be a bit slower.
*/
2014-04-23 06:47:46 +00:00
char *path = NULL;
if(asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) == -1) {
return;
}
FILE *fd = fopen ( path, "r" );
2014-03-22 20:04:19 +00:00
if ( fd != NULL )
{
2014-02-27 19:26:35 +00:00
char buffer[1024];
2014-03-22 20:04:19 +00:00
while ( fgets ( buffer, 1024, fd ) != NULL )
{
2014-04-23 06:47:46 +00:00
char * start = NULL;
2014-03-22 20:04:19 +00:00
if ( strlen ( buffer ) == 0 )
{
continue;
}
2014-04-23 06:47:46 +00:00
retv = reallocate ( retv, ( index + 2 ) * sizeof ( element* ) );
retv[index] = allocate ( sizeof ( element ) );
// remove trailing \n
2014-04-24 21:39:26 +00:00
buffer[strlen ( buffer ) - 1] = '\0';
2014-03-22 20:04:19 +00:00
retv[index]->index = strtol ( buffer, &start, 10 );
2014-04-23 06:47:46 +00:00
snprintf ( retv[index]->name, RUN_DIALOG_NAME_LENGTH, "%s", start + 1 );
2014-03-22 20:04:19 +00:00
retv[index + 1] = NULL;
2014-03-22 20:04:19 +00:00
if ( strcasecmp ( retv[index]->name, cmd ) == 0 )
{
curr = index;
}
index++;
}
2014-03-22 20:04:19 +00:00
fclose ( fd );
}
/**
* Write out the last 25 results again.
*/
fd = fopen ( path, "w" );
2014-03-22 20:04:19 +00:00
if ( fd )
{
for ( int i = 0; i < ( int ) index && i < 20; i++ )
{
if ( i != curr )
{
if ( retv[i]->name && retv[i]->name[0] != '\0' )
{
fprintf ( fd, "%ld %s\n",
retv[i]->index,
retv[i]->name
);
2014-03-12 16:38:24 +00:00
}
}
}
2014-03-22 20:04:19 +00:00
fclose ( fd );
}
2014-03-22 20:04:19 +00:00
for ( int i = 0; retv != NULL && retv[i] != NULL; i++ )
{
free ( retv[i] );
}
2014-03-22 20:04:19 +00:00
free ( retv );
2014-03-22 20:04:19 +00:00
free ( path );
}
2014-01-21 09:01:55 +00:00
static int sort_func ( const void *a, const void *b )
{
2014-03-22 20:04:19 +00:00
const char *astr = *( const char * const * ) a;
const char *bstr = *( const char * const * ) b;
return strcasecmp ( astr, bstr );
}
static char ** get_apps ( )
{
2014-03-22 20:04:19 +00:00
unsigned int num_favorites = 0;
unsigned int index = 0;
char *path;
char **retv = NULL;
#ifdef TIMING
struct timespec start, stop;
2014-03-22 20:04:19 +00:00
clock_gettime ( CLOCK_REALTIME, &start );
#endif
2014-03-22 20:04:19 +00:00
if ( getenv ( "PATH" ) == NULL )
{
return NULL;
}
2014-04-23 06:47:46 +00:00
if(asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) > 0) {
FILE *fd = fopen ( path, "r" );
2014-04-23 06:47:46 +00:00
if ( fd != NULL )
2014-03-22 20:04:19 +00:00
{
2014-04-23 06:47:46 +00:00
char buffer[1024];
while ( fgets ( buffer, 1024, fd ) != NULL )
2014-03-22 20:04:19 +00:00
{
2014-04-23 06:47:46 +00:00
if ( strlen ( buffer ) == 0 )
{
continue;
}
// remove trailing \n
2014-04-23 06:47:46 +00:00
buffer[strlen ( buffer ) - 1] = '\0';
char *start = NULL;
// Don't use result.
strtol ( buffer, &start, 10 );
if ( start == NULL )
{
continue;
}
retv = reallocate ( retv, ( index + 2 ) * sizeof ( char* ) );
retv[index] = strdup ( start + 1 );
retv[index + 1] = NULL;
index++;
num_favorites++;
2014-03-22 20:04:19 +00:00
}
2014-04-23 06:47:46 +00:00
fclose ( fd );
}
2014-04-23 06:47:46 +00:00
free ( path );
}
2014-03-22 20:04:19 +00:00
path = strdup ( getenv ( "PATH" ) );
2014-04-23 06:47:46 +00:00
for ( const char *dirname = strtok ( path, ":" );
dirname != NULL;
dirname = strtok ( NULL, ":" ) )
2014-03-22 20:04:19 +00:00
{
DIR *dir = opendir ( dirname );
2014-03-22 20:04:19 +00:00
if ( dir != NULL )
{
struct dirent *dent;
2014-03-22 20:04:19 +00:00
while ( ( dent = readdir ( dir ) ) != NULL )
{
if ( dent->d_type != DT_REG &&
dent->d_type != DT_LNK &&
2014-03-22 20:04:19 +00:00
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.
2014-03-22 20:04:19 +00:00
for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ )
{
if ( strcasecmp ( dent->d_name, retv[j] ) == 0 )
{
found = 1;
}
}
2014-03-22 20:04:19 +00:00
if ( found == 1 )
{
continue;
}
2014-03-22 20:04:19 +00:00
retv = reallocate ( retv, ( index + 2 ) * sizeof ( char* ) );
retv[index] = strdup ( dent->d_name );
retv[index + 1] = NULL;
index++;
}
2014-03-22 20:04:19 +00:00
closedir ( dir );
}
}
// TODO: check this is still fast enough. (takes 1ms on laptop.)
2014-03-22 20:04:19 +00:00
if ( index > num_favorites )
{
qsort ( &retv[num_favorites], index - num_favorites, sizeof ( char* ), sort_func );
2014-03-11 19:16:44 +00:00
}
2014-03-22 20:04:19 +00:00
free ( path );
#ifdef TIMING
2014-03-22 20:04:19 +00:00
clock_gettime ( CLOCK_REALTIME, &stop );
2014-03-22 20:04:19 +00:00
if ( stop.tv_sec != start.tv_sec )
{
stop.tv_nsec += ( stop.tv_sec - start.tv_sec ) * 1e9;
}
2014-03-22 20:04:19 +00:00
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 )
{
2014-03-22 20:04:19 +00:00
int shift = 0;
int selected_line = 0;
SwitcherMode retv = MODE_EXIT;
// act as a launcher
2014-03-22 20:04:19 +00:00
char **cmd_list = get_apps ( );
2014-03-22 20:04:19 +00:00
if ( cmd_list == NULL )
{
cmd_list = allocate ( 2 * sizeof ( char * ) );
cmd_list[0] = strdup ( "No applications found" );
cmd_list[1] = NULL;
}
2014-05-10 20:58:21 +00:00
int mretv = menu ( cmd_list, input, "run", NULL, &shift, token_match, NULL, &selected_line );
2014-03-22 20:04:19 +00:00
if ( mretv == MENU_NEXT )
{
2014-01-22 08:24:31 +00:00
retv = NEXT_DIALOG;
2014-03-22 20:04:19 +00:00
}
else if ( mretv == MENU_OK && cmd_list[selected_line] != NULL )
{
exec_cmd ( cmd_list[selected_line], shift );
}
else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' )
{
exec_cmd ( *input, shift );
}
else if ( mretv == MENU_ENTRY_DELETE && cmd_list[selected_line] )
{
delete_entry ( cmd_list[selected_line] );
2014-02-01 13:39:49 +00:00
retv = RUN_DIALOG;
}
2014-03-22 20:04:19 +00:00
for ( int i = 0; cmd_list != NULL && cmd_list[i] != NULL; i++ )
{
free ( cmd_list[i] );
}
2014-03-22 20:04:19 +00:00
if ( cmd_list != NULL )
{
free ( cmd_list );
}
return retv;
}