2014-01-20 17:36:20 -05:00
|
|
|
/**
|
2014-03-01 11:27:52 -05:00
|
|
|
* rofi
|
2014-01-20 17:36:20 -05:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
2014-08-22 03:26:46 -04:00
|
|
|
#include <config.h>
|
2014-01-20 18:20:09 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2014-01-20 17:36:20 -05:00
|
|
|
#include <X11/X.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2014-03-08 15:35:27 -05:00
|
|
|
#include <limits.h>
|
2014-01-20 17:36:20 -05:00
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <string.h>
|
2014-01-21 08:56:25 -05:00
|
|
|
#include <errno.h>
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2014-03-12 03:41:38 -04:00
|
|
|
#include "rofi.h"
|
2014-09-03 07:07:26 -04:00
|
|
|
#include "helper.h"
|
2014-05-13 04:45:59 -04:00
|
|
|
#include "history.h"
|
2014-01-20 17:36:20 -05:00
|
|
|
#include "run-dialog.h"
|
2014-01-20 18:20:09 -05:00
|
|
|
#ifdef TIMING
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2014-03-22 16:04:19 -04:00
|
|
|
#define RUN_CACHE_FILE "rofi-2.runcache"
|
2014-01-25 17:37:37 -05:00
|
|
|
|
2014-08-26 14:25:00 -04:00
|
|
|
static inline void execsh ( const char *cmd, int run_in_term )
|
2014-01-20 17:36:20 -05:00
|
|
|
{
|
2014-09-03 07:07:26 -04:00
|
|
|
char **args = NULL;
|
|
|
|
int argc = 0;
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( run_in_term ) {
|
2014-09-03 07:07:26 -04:00
|
|
|
helper_parse_setup ( config.run_shell_command, &args, &argc, "{cmd}", cmd, NULL );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
helper_parse_setup ( config.run_command, &args, &argc, "{cmd}", cmd, NULL );
|
2014-03-22 16:04:19 -04:00
|
|
|
}
|
2014-08-27 13:44:15 -04:00
|
|
|
GError *error = NULL;
|
2014-08-25 14:02:48 -04:00
|
|
|
g_spawn_async ( NULL, args, NULL,
|
|
|
|
G_SPAWN_SEARCH_PATH,
|
2014-08-27 13:44:15 -04:00
|
|
|
NULL, NULL, NULL, &error );
|
2014-09-03 07:07:26 -04:00
|
|
|
if ( error != NULL ) {
|
|
|
|
char *msg = g_strdup_printf ( "Failed to execute: '%s'\nError: '%s'", cmd,
|
|
|
|
error->message );
|
|
|
|
error_dialog ( msg );
|
|
|
|
g_free ( msg );
|
2014-08-27 13:44:15 -04:00
|
|
|
// print error.
|
2014-09-03 07:07:26 -04:00
|
|
|
g_error_free ( error );
|
2014-08-27 13:44:15 -04:00
|
|
|
}
|
2014-08-25 14:02:48 -04:00
|
|
|
|
|
|
|
// Free the args list.
|
|
|
|
g_strfreev ( args );
|
2014-01-20 17:36:20 -05:00
|
|
|
}
|
2014-03-08 15:35:27 -05:00
|
|
|
|
2014-01-20 17:36:20 -05:00
|
|
|
// execute sub-process
|
2014-08-25 14:02:48 -04:00
|
|
|
static void exec_cmd ( const char *cmd, int run_in_term )
|
2014-01-20 17:36:20 -05:00
|
|
|
{
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( !cmd || !cmd[0] ) {
|
2014-08-25 14:02:48 -04:00
|
|
|
return;
|
2014-03-22 16:04:19 -04:00
|
|
|
}
|
2014-01-20 17:36:20 -05:00
|
|
|
|
|
|
|
|
2014-08-25 14:02:48 -04:00
|
|
|
execsh ( cmd, run_in_term );
|
2014-01-20 17:36:20 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This happens in non-critical time (After launching app)
|
|
|
|
* It is allowed to be a bit slower.
|
|
|
|
*/
|
2014-08-09 05:40:42 -04:00
|
|
|
char *path = g_strdup_printf ( "%s/%s", cache_dir, RUN_CACHE_FILE );
|
|
|
|
|
2014-05-22 03:33:32 -04:00
|
|
|
history_set ( path, cmd );
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2014-08-09 05:40:42 -04:00
|
|
|
g_free ( path );
|
2014-01-20 17:36:20 -05:00
|
|
|
}
|
2014-02-01 08:03:23 -05:00
|
|
|
// execute sub-process
|
2014-03-22 16:04:19 -04:00
|
|
|
static void delete_entry ( const char *cmd )
|
2014-02-01 08:03:23 -05:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* This happens in non-critical time (After launching app)
|
|
|
|
* It is allowed to be a bit slower.
|
|
|
|
*/
|
2014-08-09 05:40:42 -04:00
|
|
|
char *path = g_strdup_printf ( "%s/%s", cache_dir, RUN_CACHE_FILE );
|
|
|
|
|
2014-05-22 03:33:32 -04:00
|
|
|
history_remove ( path, cmd );
|
2014-02-01 08:03:23 -05:00
|
|
|
|
2014-08-09 05:40:42 -04:00
|
|
|
g_free ( path );
|
2014-02-01 08:03:23 -05:00
|
|
|
}
|
2014-01-21 04:01:55 -05:00
|
|
|
static int sort_func ( const void *a, const void *b )
|
2014-01-20 17:36:20 -05:00
|
|
|
{
|
2014-03-22 16:04:19 -04:00
|
|
|
const char *astr = *( const char * const * ) a;
|
|
|
|
const char *bstr = *( const char * const * ) b;
|
2014-11-06 11:44:41 -05:00
|
|
|
|
|
|
|
if ( astr == NULL && bstr == NULL ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if ( astr == NULL ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if ( bstr == NULL ) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-03-22 16:04:19 -04:00
|
|
|
return strcasecmp ( astr, bstr );
|
2014-01-20 17:36:20 -05:00
|
|
|
}
|
2015-01-05 15:53:50 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* External spider to get list of executables.
|
|
|
|
*/
|
2015-01-07 09:53:41 -05:00
|
|
|
static char ** get_apps_external ( char **retv, unsigned int *length, unsigned int num_favorites )
|
2015-01-05 15:53:50 -05:00
|
|
|
{
|
|
|
|
int fd = execute_generator ( config.run_list_command );
|
|
|
|
if ( fd >= 0 ) {
|
|
|
|
FILE *inp = fdopen ( fd, "r" );
|
|
|
|
if ( inp ) {
|
|
|
|
char buffer[1024];
|
|
|
|
while ( fgets ( buffer, 1024, inp ) != NULL ) {
|
2015-01-07 09:53:41 -05:00
|
|
|
int found = 0;
|
|
|
|
// Filter out line-end.
|
|
|
|
if ( buffer[strlen ( buffer ) - 1] == '\n' ) {
|
|
|
|
buffer[strlen ( buffer ) - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a nice little penalty, but doable? time will tell.
|
|
|
|
// given num_favorites is max 25.
|
|
|
|
for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) {
|
|
|
|
if ( strcasecmp ( buffer, retv[j] ) == 0 ) {
|
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( found == 1 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No duplicate, add it.
|
2015-01-05 15:53:50 -05:00
|
|
|
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
|
|
|
|
retv[( *length )] = g_strdup ( buffer );
|
|
|
|
retv[( *length ) + 1] = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
( *length )++;
|
|
|
|
}
|
|
|
|
fclose ( inp );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal spider used to get list of executables.
|
|
|
|
*/
|
2015-01-05 16:16:13 -05:00
|
|
|
static char ** get_apps ( char **retv, unsigned int *length )
|
2014-01-20 17:36:20 -05:00
|
|
|
{
|
2014-03-22 16:04:19 -04:00
|
|
|
unsigned int num_favorites = 0;
|
|
|
|
char *path;
|
2014-01-20 17:36:20 -05:00
|
|
|
#ifdef TIMING
|
|
|
|
struct timespec start, stop;
|
2014-03-22 16:04:19 -04:00
|
|
|
clock_gettime ( CLOCK_REALTIME, &start );
|
2014-01-20 17:36:20 -05:00
|
|
|
#endif
|
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( getenv ( "PATH" ) == NULL ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-01-20 17:36:20 -05:00
|
|
|
|
|
|
|
|
2014-08-09 05:40:42 -04:00
|
|
|
path = g_strdup_printf ( "%s/%s", cache_dir, RUN_CACHE_FILE );
|
|
|
|
retv = history_get_list ( path, length );
|
|
|
|
g_free ( path );
|
|
|
|
// Keep track of how many where loaded as favorite.
|
|
|
|
num_favorites = ( *length );
|
2014-01-20 17:36:20 -05:00
|
|
|
|
|
|
|
|
2014-08-09 05:40:42 -04:00
|
|
|
path = g_strdup ( getenv ( "PATH" ) );
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2014-04-23 02:47:46 -04:00
|
|
|
for ( const char *dirname = strtok ( path, ":" );
|
2014-05-22 03:33:32 -04:00
|
|
|
dirname != NULL;
|
2014-06-04 15:29:23 -04:00
|
|
|
dirname = strtok ( NULL, ":" ) ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
DIR *dir = opendir ( dirname );
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( dir != NULL ) {
|
2014-01-20 17:36:20 -05:00
|
|
|
struct dirent *dent;
|
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
while ( ( dent = readdir ( dir ) ) != NULL ) {
|
2014-01-20 17:36:20 -05:00
|
|
|
if ( dent->d_type != DT_REG &&
|
|
|
|
dent->d_type != DT_LNK &&
|
2014-06-04 15:29:23 -04:00
|
|
|
dent->d_type != DT_UNKNOWN ) {
|
2014-01-20 17:36:20 -05:00
|
|
|
continue;
|
|
|
|
}
|
2014-06-07 05:00:21 -04:00
|
|
|
// Skip dot files.
|
2014-07-05 13:47:55 -04:00
|
|
|
if ( dent->d_name[0] == '.' ) {
|
2014-06-07 05:00:21 -04:00
|
|
|
continue;
|
|
|
|
}
|
2014-01-20 17:36:20 -05:00
|
|
|
|
|
|
|
int found = 0;
|
|
|
|
|
|
|
|
// This is a nice little penalty, but doable? time will tell.
|
|
|
|
// given num_favorites is max 25.
|
2014-06-04 15:29:23 -04:00
|
|
|
for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) {
|
|
|
|
if ( strcasecmp ( dent->d_name, retv[j] ) == 0 ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
found = 1;
|
|
|
|
}
|
2014-01-20 17:36:20 -05:00
|
|
|
}
|
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( found == 1 ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
continue;
|
|
|
|
}
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2014-08-09 05:40:42 -04:00
|
|
|
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
|
|
|
|
retv[( *length )] = g_strdup ( dent->d_name );
|
|
|
|
retv[( *length ) + 1] = NULL;
|
|
|
|
( *length )++;
|
2014-01-20 17:36:20 -05:00
|
|
|
}
|
|
|
|
|
2014-03-22 16:04:19 -04:00
|
|
|
closedir ( dir );
|
2014-01-20 17:36:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 09:53:41 -05:00
|
|
|
// Get external apps.
|
2015-01-05 16:16:13 -05:00
|
|
|
if ( config.run_list_command != NULL && config.run_list_command[0] != '\0' ) {
|
2015-01-07 09:53:41 -05:00
|
|
|
retv = get_apps_external ( retv, length, num_favorites );
|
2015-01-05 16:16:13 -05:00
|
|
|
}
|
2015-01-06 05:45:01 -05:00
|
|
|
// No sorting needed.
|
2015-01-07 09:53:41 -05:00
|
|
|
if ( ( *length ) == 0 ) {
|
2015-01-06 05:45:01 -05:00
|
|
|
return retv;
|
|
|
|
}
|
2014-01-20 17:36:20 -05:00
|
|
|
// TODO: check this is still fast enough. (takes 1ms on laptop.)
|
2014-07-20 06:29:27 -04:00
|
|
|
if ( ( *length ) > num_favorites ) {
|
|
|
|
qsort ( &retv[num_favorites], ( *length ) - num_favorites, sizeof ( char* ), sort_func );
|
2014-03-11 15:16:44 -04:00
|
|
|
}
|
2014-08-09 05:40:42 -04:00
|
|
|
g_free ( path );
|
2014-01-20 17:36:20 -05:00
|
|
|
#ifdef TIMING
|
2014-11-06 11:44:41 -05:00
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
|
unsigned int removed = 0;
|
|
|
|
for ( unsigned int index = num_favorites; index < ( ( *length ) - 1 ); index++ ) {
|
|
|
|
if ( strcmp ( retv[index], retv[index + 1] ) == 0 ) {
|
|
|
|
g_free ( retv[index] );
|
|
|
|
retv[index] = NULL;
|
|
|
|
removed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2014-11-06 11:44:41 -05:00
|
|
|
if ( ( *length ) > num_favorites ) {
|
|
|
|
qsort ( &retv[num_favorites], ( *length ) - num_favorites, sizeof ( char* ), sort_func );
|
2014-01-20 17:36:20 -05:00
|
|
|
}
|
2014-11-06 11:44:41 -05:00
|
|
|
// Reduce array length;
|
|
|
|
( *length ) -= removed;
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2014-11-06 11:44:41 -05:00
|
|
|
#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 );
|
|
|
|
}
|
2014-01-20 17:36:20 -05:00
|
|
|
#endif
|
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
2014-08-22 03:43:26 -04:00
|
|
|
SwitcherMode run_switcher_dialog ( char **input, G_GNUC_UNUSED void *data )
|
2014-01-20 17:36:20 -05:00
|
|
|
{
|
2014-03-22 16:04:19 -04:00
|
|
|
int shift = 0;
|
|
|
|
int selected_line = 0;
|
|
|
|
SwitcherMode retv = MODE_EXIT;
|
2014-01-20 17:36:20 -05:00
|
|
|
// act as a launcher
|
2014-07-20 06:29:27 -04:00
|
|
|
unsigned int cmd_list_length = 0;
|
2015-01-05 15:53:50 -05:00
|
|
|
char **cmd_list = NULL;
|
2015-01-05 16:16:13 -05:00
|
|
|
cmd_list = get_apps ( cmd_list, &cmd_list_length );
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( cmd_list == NULL ) {
|
2014-08-09 05:40:42 -04:00
|
|
|
cmd_list = g_malloc_n ( 2, sizeof ( char * ) );
|
|
|
|
cmd_list[0] = g_strdup ( "No applications found" );
|
2014-01-20 17:36:20 -05:00
|
|
|
cmd_list[1] = NULL;
|
|
|
|
}
|
|
|
|
|
2014-08-03 15:09:20 -04:00
|
|
|
int mretv = menu ( cmd_list, cmd_list_length, input, "run:",
|
2015-01-12 08:13:46 -05:00
|
|
|
NULL, &shift, token_match, NULL, &selected_line,
|
|
|
|
config.levenshtein_sort );
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
if ( mretv == MENU_NEXT ) {
|
2014-01-22 03:24:31 -05:00
|
|
|
retv = NEXT_DIALOG;
|
2014-03-22 16:04:19 -04:00
|
|
|
}
|
2014-11-11 15:50:16 -05:00
|
|
|
else if ( mretv == MENU_PREVIOUS ) {
|
|
|
|
retv = PREVIOUS_DIALOG;
|
|
|
|
}
|
2014-07-21 17:19:45 -04:00
|
|
|
else if ( mretv == MENU_QUICK_SWITCH ) {
|
|
|
|
retv = selected_line;
|
|
|
|
}
|
2014-06-04 15:29:23 -04:00
|
|
|
else if ( mretv == MENU_OK && cmd_list[selected_line] != NULL ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
exec_cmd ( cmd_list[selected_line], shift );
|
|
|
|
}
|
2014-06-04 15:29:23 -04:00
|
|
|
else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
exec_cmd ( *input, shift );
|
|
|
|
}
|
2014-06-04 15:29:23 -04:00
|
|
|
else if ( mretv == MENU_ENTRY_DELETE && cmd_list[selected_line] ) {
|
2014-02-01 08:03:23 -05:00
|
|
|
delete_entry ( cmd_list[selected_line] );
|
2014-07-21 15:39:24 -04:00
|
|
|
retv = RELOAD_DIALOG;
|
2014-01-20 17:36:20 -05:00
|
|
|
}
|
|
|
|
|
2014-08-09 05:40:42 -04:00
|
|
|
g_strfreev ( cmd_list );
|
2014-01-20 17:36:20 -05:00
|
|
|
|
|
|
|
return retv;
|
|
|
|
}
|