mirror of
https://github.com/lbonn/rofi
synced 2024-12-19 00:33:21 +00:00
397 lines
12 KiB
C
397 lines
12 KiB
C
/*
|
|
* rofi
|
|
*
|
|
* MIT/X11 License
|
|
* Copyright 2013-2016 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.
|
|
*
|
|
*/
|
|
|
|
/**
|
|
* \ingroup RUNMode
|
|
* @{
|
|
*/
|
|
#include <config.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <X11/X.h>
|
|
|
|
#include <unistd.h>
|
|
#include <limits.h>
|
|
#include <signal.h>
|
|
#include <sys/types.h>
|
|
#include <dirent.h>
|
|
#include <strings.h>
|
|
#include <string.h>
|
|
#include <errno.h>
|
|
|
|
#include "rofi.h"
|
|
#include "helper.h"
|
|
#include "history.h"
|
|
#include "dialogs/run.h"
|
|
|
|
/**
|
|
* Name of the history file where previously choosen commands are stored.
|
|
*/
|
|
#define RUN_CACHE_FILE "rofi-2.runcache"
|
|
|
|
/**
|
|
* The internal data structure holding the private data of the Run Mode.
|
|
*/
|
|
typedef struct _RunModePrivateData
|
|
{
|
|
/** list of available commands. */
|
|
char **cmd_list;
|
|
/** Length of the #cmd_list. */
|
|
unsigned int cmd_list_length;
|
|
} RunModePrivateData;
|
|
|
|
/**
|
|
* @param cmd The cmd to execute
|
|
* @param run_in_term Indicate if command should be run in a terminal
|
|
*
|
|
* Execute command.
|
|
*
|
|
* @returns FALSE On failure, TRUE on success
|
|
*/
|
|
static inline int execsh ( const char *cmd, int run_in_term )
|
|
{
|
|
int retv = TRUE;
|
|
char **args = NULL;
|
|
int argc = 0;
|
|
if ( run_in_term ) {
|
|
helper_parse_setup ( config.run_shell_command, &args, &argc, "{cmd}", cmd, NULL );
|
|
}
|
|
else {
|
|
helper_parse_setup ( config.run_command, &args, &argc, "{cmd}", cmd, NULL );
|
|
}
|
|
GError *error = NULL;
|
|
g_spawn_async ( NULL, args, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error );
|
|
if ( error != NULL ) {
|
|
char *msg = g_strdup_printf ( "Failed to execute: '%s'\nError: '%s'", cmd, error->message );
|
|
error_dialog ( msg, FALSE );
|
|
g_free ( msg );
|
|
// print error.
|
|
g_error_free ( error );
|
|
retv = FALSE;
|
|
}
|
|
|
|
// Free the args list.
|
|
g_strfreev ( args );
|
|
return retv;
|
|
}
|
|
|
|
/**
|
|
* @param cmd The cmd to execute
|
|
* @param run_in_term Indicate if command should be run in a terminal
|
|
*
|
|
* Execute command and add to history.
|
|
*/
|
|
static void exec_cmd ( const char *cmd, int run_in_term )
|
|
{
|
|
if ( !cmd || !cmd[0] ) {
|
|
return;
|
|
}
|
|
|
|
if ( execsh ( cmd, run_in_term ) ) {
|
|
/**
|
|
* This happens in non-critical time (After launching app)
|
|
* It is allowed to be a bit slower.
|
|
*/
|
|
char *path = g_build_filename ( cache_dir, RUN_CACHE_FILE, NULL );
|
|
|
|
history_set ( path, cmd );
|
|
|
|
g_free ( path );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param cmd The command to remove from history
|
|
*
|
|
* Remove command from history.
|
|
*/
|
|
static void delete_entry ( const char *cmd )
|
|
{
|
|
char *path = g_build_filename ( cache_dir, RUN_CACHE_FILE, NULL );
|
|
|
|
history_remove ( path, cmd );
|
|
|
|
g_free ( path );
|
|
}
|
|
|
|
/**
|
|
* @param a The First key to compare
|
|
* @param b The second key to compare
|
|
*
|
|
* Function used for sorting.
|
|
*
|
|
* @returns returns less then, equal to and greater than zero is a is less than, is a match or greater than b.
|
|
*/
|
|
static int sort_func ( const void *a, const void *b, void *data __attribute__( ( unused ) ) )
|
|
{
|
|
const char *astr = *( const char * const * ) a;
|
|
const char *bstr = *( const char * const * ) b;
|
|
|
|
if ( astr == NULL && bstr == NULL ) {
|
|
return 0;
|
|
}
|
|
else if ( astr == NULL ) {
|
|
return 1;
|
|
}
|
|
else if ( bstr == NULL ) {
|
|
return -1;
|
|
}
|
|
return g_ascii_strcasecmp ( astr, bstr );
|
|
}
|
|
|
|
/**
|
|
* External spider to get list of executables.
|
|
*/
|
|
static char ** get_apps_external ( char **retv, unsigned int *length, unsigned int num_favorites )
|
|
{
|
|
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 ) {
|
|
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.
|
|
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
|
|
retv[( *length )] = g_strdup ( buffer );
|
|
|
|
( *length )++;
|
|
}
|
|
if ( fclose ( inp ) != 0 ) {
|
|
fprintf ( stderr, "Failed to close stdout off executor script: '%s'\n",
|
|
strerror ( errno ) );
|
|
}
|
|
}
|
|
}
|
|
retv[( *length ) ] = NULL;
|
|
return retv;
|
|
}
|
|
|
|
/**
|
|
* Internal spider used to get list of executables.
|
|
*/
|
|
static char ** get_apps ( unsigned int *length )
|
|
{
|
|
char **retv = NULL;
|
|
unsigned int num_favorites = 0;
|
|
char *path;
|
|
|
|
if ( g_getenv ( "PATH" ) == NULL ) {
|
|
return NULL;
|
|
}
|
|
|
|
path = g_build_filename ( cache_dir, RUN_CACHE_FILE, NULL );
|
|
retv = history_get_list ( path, length );
|
|
g_free ( path );
|
|
// Keep track of how many where loaded as favorite.
|
|
num_favorites = ( *length );
|
|
|
|
path = g_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;
|
|
}
|
|
// Skip dot files.
|
|
if ( dent->d_name[0] == '.' ) {
|
|
continue;
|
|
}
|
|
|
|
int found = 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 ( dent->d_name, retv[j] ) == 0 ) {
|
|
found = 1;
|
|
}
|
|
}
|
|
|
|
if ( found == 1 ) {
|
|
continue;
|
|
}
|
|
|
|
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
|
|
retv[( *length )] = g_strdup ( dent->d_name );
|
|
retv[( *length ) + 1] = NULL;
|
|
( *length )++;
|
|
}
|
|
|
|
closedir ( dir );
|
|
}
|
|
}
|
|
|
|
// Get external apps.
|
|
if ( config.run_list_command != NULL && config.run_list_command[0] != '\0' ) {
|
|
retv = get_apps_external ( retv, length, num_favorites );
|
|
}
|
|
// No sorting needed.
|
|
if ( ( *length ) == 0 ) {
|
|
return retv;
|
|
}
|
|
// TODO: check this is still fast enough. (takes 1ms on laptop.)
|
|
if ( ( *length ) > num_favorites ) {
|
|
g_qsort_with_data ( &retv[num_favorites], ( *length ) - num_favorites, sizeof ( char* ), sort_func, NULL );
|
|
}
|
|
g_free ( path );
|
|
|
|
unsigned int removed = 0;
|
|
for ( unsigned int index = num_favorites; index < ( ( *length ) - 1 ); index++ ) {
|
|
if ( g_strcmp0 ( retv[index], retv[index + 1] ) == 0 ) {
|
|
g_free ( retv[index] );
|
|
retv[index] = NULL;
|
|
removed++;
|
|
}
|
|
}
|
|
|
|
if ( ( *length ) > num_favorites ) {
|
|
g_qsort_with_data ( &retv[num_favorites], ( *length ) - num_favorites, sizeof ( char* ),
|
|
sort_func,
|
|
NULL );
|
|
}
|
|
// Reduce array length;
|
|
( *length ) -= removed;
|
|
|
|
return retv;
|
|
}
|
|
|
|
static void run_mode_init ( Mode *sw )
|
|
{
|
|
if ( sw->private_data == NULL ) {
|
|
RunModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
|
|
sw->private_data = (void *) pd;
|
|
pd->cmd_list = get_apps ( &( pd->cmd_list_length ) );
|
|
}
|
|
}
|
|
|
|
static unsigned int run_mode_get_num_entries ( const Mode *sw )
|
|
{
|
|
const RunModePrivateData *rmpd = (const RunModePrivateData *) sw->private_data;
|
|
return rmpd->cmd_list_length;
|
|
}
|
|
|
|
static ModeMode run_mode_result ( Mode *sw, int mretv, char **input, unsigned int selected_line )
|
|
{
|
|
RunModePrivateData *rmpd = (RunModePrivateData *) sw->private_data;
|
|
ModeMode retv = MODE_EXIT;
|
|
|
|
int shift = ( ( mretv & MENU_SHIFT ) == MENU_SHIFT );
|
|
|
|
if ( mretv & MENU_NEXT ) {
|
|
retv = NEXT_DIALOG;
|
|
}
|
|
else if ( mretv & MENU_PREVIOUS ) {
|
|
retv = PREVIOUS_DIALOG;
|
|
}
|
|
else if ( mretv & MENU_QUICK_SWITCH ) {
|
|
retv = ( mretv & MENU_LOWER_MASK );
|
|
}
|
|
else if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line] != NULL ) {
|
|
exec_cmd ( rmpd->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 ) && rmpd->cmd_list[selected_line] ) {
|
|
delete_entry ( rmpd->cmd_list[selected_line] );
|
|
|
|
g_free ( rmpd->cmd_list );
|
|
// Clear the list.
|
|
rmpd->cmd_list = NULL;
|
|
rmpd->cmd_list_length = 0;
|
|
retv = RELOAD_DIALOG;
|
|
}
|
|
return retv;
|
|
}
|
|
|
|
static void run_mode_destroy ( Mode *sw )
|
|
{
|
|
RunModePrivateData *rmpd = (RunModePrivateData *) sw->private_data;
|
|
if ( rmpd != NULL ) {
|
|
g_strfreev ( rmpd->cmd_list );
|
|
g_free ( rmpd );
|
|
sw->private_data = NULL;
|
|
}
|
|
}
|
|
|
|
static char *mgrv ( const Mode *sw, unsigned int selected_line, G_GNUC_UNUSED int *state, int get_entry )
|
|
{
|
|
const RunModePrivateData *rmpd = (const RunModePrivateData *) sw->private_data;
|
|
return get_entry ? g_strdup ( rmpd->cmd_list[selected_line] ) : NULL;
|
|
}
|
|
static int run_token_match ( const Mode *sw, char **tokens, int not_ascii, int case_sensitive, unsigned int index )
|
|
{
|
|
const RunModePrivateData *rmpd = (const RunModePrivateData *) sw->private_data;
|
|
return token_match ( tokens, rmpd->cmd_list[index], not_ascii, case_sensitive );
|
|
}
|
|
|
|
static int run_is_not_ascii ( const Mode *sw, unsigned int index )
|
|
{
|
|
const RunModePrivateData *rmpd = (const RunModePrivateData *) sw->private_data;
|
|
return !g_str_is_ascii ( rmpd->cmd_list[index] );
|
|
}
|
|
Mode run_mode =
|
|
{
|
|
.name = "run",
|
|
.keycfg = NULL,
|
|
.keystr = NULL,
|
|
.modmask = AnyModifier,
|
|
.init = run_mode_init,
|
|
.get_num_entries = run_mode_get_num_entries,
|
|
.result = run_mode_result,
|
|
.destroy = run_mode_destroy,
|
|
.token_match = run_token_match,
|
|
.mgrv = mgrv,
|
|
.get_completion = NULL,
|
|
.is_not_ascii = run_is_not_ascii,
|
|
.private_data = NULL,
|
|
.free = NULL
|
|
};
|
|
/*@}*/
|