2016-01-06 06:40:41 -05:00
|
|
|
/*
|
2014-03-01 11:27:52 -05:00
|
|
|
* rofi
|
2014-01-20 17:36:20 -05:00
|
|
|
*
|
|
|
|
* MIT/X11 License
|
2015-12-31 18:27:00 -05:00
|
|
|
* Copyright 2013-2016 Qball Cow <qball@gmpclient.org>
|
2014-01-20 17:36:20 -05:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
2016-01-06 06:40:41 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \ingroup RUNMode
|
|
|
|
* @{
|
|
|
|
*/
|
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 <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"
|
2016-01-07 10:01:56 -05:00
|
|
|
#include "settings.h"
|
2014-09-03 07:07:26 -04:00
|
|
|
#include "helper.h"
|
2014-05-13 04:45:59 -04:00
|
|
|
#include "history.h"
|
2015-03-25 03:36:19 -04:00
|
|
|
#include "dialogs/run.h"
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2016-01-07 13:47:37 -05:00
|
|
|
#include "mode-private.h"
|
2016-01-06 06:40:41 -05:00
|
|
|
/**
|
|
|
|
* Name of the history file where previously choosen commands are stored.
|
|
|
|
*/
|
2016-01-14 14:40:19 -05:00
|
|
|
#define RUN_CACHE_FILE "rofi-3.runcache"
|
2014-01-25 17:37:37 -05:00
|
|
|
|
2016-01-06 06:40:41 -05:00
|
|
|
/**
|
|
|
|
* The internal data structure holding the private data of the Run Mode.
|
|
|
|
*/
|
2016-03-24 17:15:10 -04:00
|
|
|
typedef struct
|
2016-01-06 06:40:41 -05:00
|
|
|
{
|
|
|
|
/** 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
|
|
|
|
*/
|
2015-09-29 11:27:47 -04:00
|
|
|
static inline int execsh ( const char *cmd, int run_in_term )
|
2014-01-20 17:36:20 -05:00
|
|
|
{
|
2015-09-29 11:27:47 -04:00
|
|
|
int retv = TRUE;
|
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;
|
2015-09-19 14:59:50 -04:00
|
|
|
g_spawn_async ( NULL, args, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error );
|
2014-09-03 07:07:26 -04:00
|
|
|
if ( error != NULL ) {
|
2015-09-19 14:59:50 -04:00
|
|
|
char *msg = g_strdup_printf ( "Failed to execute: '%s'\nError: '%s'", cmd, error->message );
|
2016-02-08 03:03:11 -05:00
|
|
|
rofi_view_error_dialog ( msg, FALSE );
|
2014-09-03 07:07:26 -04:00
|
|
|
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 );
|
2015-09-29 11:27:47 -04:00
|
|
|
retv = FALSE;
|
2014-08-27 13:44:15 -04:00
|
|
|
}
|
2014-08-25 14:02:48 -04:00
|
|
|
|
|
|
|
// Free the args list.
|
|
|
|
g_strfreev ( args );
|
2015-09-29 11:27:47 -04:00
|
|
|
return retv;
|
2014-01-20 17:36:20 -05:00
|
|
|
}
|
2014-03-08 15:35:27 -05:00
|
|
|
|
2016-01-06 06:40:41 -05:00
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
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
|
|
|
{
|
2016-01-14 14:40:19 -05:00
|
|
|
GError *error = NULL;
|
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
|
|
|
}
|
2016-01-14 14:40:19 -05:00
|
|
|
gsize lf_cmd_size = 0;
|
|
|
|
gchar *lf_cmd = g_locale_from_utf8 ( cmd, -1, NULL, &lf_cmd_size, &error );
|
|
|
|
if ( error != NULL ) {
|
|
|
|
fprintf ( stderr, "Failed to convert command to locale encoding: %s\n", error->message );
|
|
|
|
g_error_free ( error );
|
|
|
|
return;
|
|
|
|
}
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2016-01-14 14:40:19 -05:00
|
|
|
if ( execsh ( lf_cmd, run_in_term ) ) {
|
2015-09-29 11:27:47 -04:00
|
|
|
/**
|
|
|
|
* This happens in non-critical time (After launching app)
|
|
|
|
* It is allowed to be a bit slower.
|
|
|
|
*/
|
2016-01-06 06:40:41 -05:00
|
|
|
char *path = g_build_filename ( cache_dir, RUN_CACHE_FILE, NULL );
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2015-09-29 11:27:47 -04:00
|
|
|
history_set ( path, cmd );
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2015-09-29 11:27:47 -04:00
|
|
|
g_free ( path );
|
|
|
|
}
|
2016-01-14 14:40:19 -05:00
|
|
|
g_free ( lf_cmd );
|
2014-01-20 17:36:20 -05:00
|
|
|
}
|
2016-01-06 06:40:41 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param cmd The command to remove from history
|
|
|
|
*
|
|
|
|
* Remove command from history.
|
|
|
|
*/
|
2014-03-22 16:04:19 -04:00
|
|
|
static void delete_entry ( const char *cmd )
|
2014-02-01 08:03:23 -05:00
|
|
|
{
|
2016-01-06 06:40:41 -05:00
|
|
|
char *path = g_build_filename ( cache_dir, RUN_CACHE_FILE, NULL );
|
2014-08-09 05:40:42 -04:00
|
|
|
|
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
|
|
|
}
|
2016-01-06 06:40:41 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2015-07-05 03:47:55 -04:00
|
|
|
static int sort_func ( const void *a, const void *b, void *data __attribute__( ( unused ) ) )
|
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;
|
|
|
|
}
|
2016-01-06 06:40:41 -05:00
|
|
|
return g_ascii_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 ) {
|
2016-04-10 08:30:13 -04:00
|
|
|
char *buffer = NULL;
|
|
|
|
size_t buffer_length = 0;
|
|
|
|
|
|
|
|
while ( getline ( &buffer, &buffer_length, inp ) > 0 ) {
|
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-03-27 15:28:53 -04:00
|
|
|
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( char* ) );
|
|
|
|
retv[( *length )] = g_strdup ( buffer );
|
2015-01-05 15:53:50 -05:00
|
|
|
|
|
|
|
( *length )++;
|
|
|
|
}
|
2016-04-10 08:30:13 -04:00
|
|
|
if ( buffer != NULL ) {
|
|
|
|
free ( buffer );
|
|
|
|
}
|
2015-07-31 04:21:32 -04:00
|
|
|
if ( fclose ( inp ) != 0 ) {
|
2015-09-19 06:21:30 -04:00
|
|
|
fprintf ( stderr, "Failed to close stdout off executor script: '%s'\n",
|
|
|
|
strerror ( errno ) );
|
2015-07-31 04:21:32 -04:00
|
|
|
}
|
2015-01-05 15:53:50 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-27 15:28:53 -04:00
|
|
|
retv[( *length ) ] = NULL;
|
2015-01-05 15:53:50 -05:00
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal spider used to get list of executables.
|
|
|
|
*/
|
2015-03-27 15:28:53 -04:00
|
|
|
static char ** get_apps ( unsigned int *length )
|
2014-01-20 17:36:20 -05:00
|
|
|
{
|
2016-01-14 14:40:19 -05:00
|
|
|
GError *error = NULL;
|
2015-03-27 15:28:53 -04:00
|
|
|
char **retv = NULL;
|
2015-02-19 07:22:10 -05:00
|
|
|
unsigned int num_favorites = 0;
|
|
|
|
char *path;
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2015-05-14 13:45:57 -04:00
|
|
|
if ( g_getenv ( "PATH" ) == NULL ) {
|
2014-03-22 16:04:19 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-01-14 14:40:19 -05:00
|
|
|
TICK_N ( "start" );
|
2016-01-06 06:40:41 -05:00
|
|
|
path = g_build_filename ( cache_dir, RUN_CACHE_FILE, NULL );
|
2014-08-09 05:40:42 -04:00
|
|
|
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
|
|
|
|
2016-01-14 15:06:03 -05:00
|
|
|
path = g_strdup ( g_getenv ( "PATH" ) );
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2016-01-14 14:40:19 -05:00
|
|
|
gsize l = 0;
|
|
|
|
gchar *homedir = g_locale_to_utf8 ( g_get_home_dir (), -1, NULL, &l, &error );
|
|
|
|
if ( error != NULL ) {
|
|
|
|
fprintf ( stderr, "Failed to convert homedir to UTF-8: %s\n", error->message );
|
2016-01-14 15:06:03 -05:00
|
|
|
g_clear_error ( &error );
|
2016-01-14 14:40:19 -05:00
|
|
|
g_free ( homedir );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-19 08:29:04 -04:00
|
|
|
const char *const sep = ":";
|
|
|
|
for ( const char *dirname = strtok ( path, sep ); dirname != NULL; dirname = strtok ( NULL, sep ) ) {
|
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;
|
2016-01-14 14:40:19 -05:00
|
|
|
gsize dirn_len = 0;
|
2016-01-14 15:06:03 -05:00
|
|
|
gchar *dirn = g_locale_to_utf8 ( dirname, -1, NULL, &dirn_len, &error );
|
2016-01-14 14:40:19 -05:00
|
|
|
if ( error != NULL ) {
|
|
|
|
fprintf ( stderr, "Failed to convert directory name to UTF-8: %s\n", error->message );
|
2016-01-14 15:06:03 -05:00
|
|
|
g_clear_error ( &error );
|
2016-01-14 14:40:19 -05:00
|
|
|
continue;
|
|
|
|
}
|
2016-01-14 15:06:03 -05:00
|
|
|
gboolean is_homedir = g_str_has_prefix ( dirn, homedir );
|
2016-01-14 14:40:19 -05:00
|
|
|
g_free ( dirn );
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2014-06-04 15:29:23 -04:00
|
|
|
while ( ( dent = readdir ( dir ) ) != NULL ) {
|
2015-09-19 14:59:50 -04:00
|
|
|
if ( dent->d_type != DT_REG && dent->d_type != DT_LNK && 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;
|
|
|
|
}
|
2016-01-14 14:40:19 -05:00
|
|
|
if ( is_homedir ) {
|
|
|
|
gchar *fpath = g_build_filename ( dirname, dent->d_name, NULL );
|
|
|
|
gboolean b = g_file_test ( fpath, G_FILE_TEST_IS_EXECUTABLE );
|
|
|
|
g_free ( fpath );
|
|
|
|
if ( !b ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2016-01-14 14:40:19 -05:00
|
|
|
gsize name_len;
|
|
|
|
gchar *name = g_filename_to_utf8 ( dent->d_name, -1, NULL, &name_len, &error );
|
|
|
|
if ( error != NULL ) {
|
|
|
|
fprintf ( stderr, "Failed to convert filename to UTF-8: %s\n", error->message );
|
2016-01-14 15:06:03 -05:00
|
|
|
g_clear_error ( &error );
|
2016-01-14 14:40:19 -05:00
|
|
|
g_free ( name );
|
|
|
|
continue;
|
|
|
|
}
|
2014-01-20 17:36:20 -05:00
|
|
|
// This is a nice little penalty, but doable? time will tell.
|
|
|
|
// given num_favorites is max 25.
|
2016-01-14 14:40:19 -05:00
|
|
|
int found = 0;
|
2014-06-04 15:29:23 -04:00
|
|
|
for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) {
|
2016-01-14 14:40:19 -05:00
|
|
|
if ( g_strcmp0 ( 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* ) );
|
2016-01-14 14:40:19 -05:00
|
|
|
retv[( *length )] = name;
|
2014-08-09 05:40:42 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2016-01-14 14:40:19 -05:00
|
|
|
g_free ( homedir );
|
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 ) {
|
2015-09-19 14:59:50 -04:00
|
|
|
g_qsort_with_data ( &retv[num_favorites], ( *length ) - num_favorites, sizeof ( char* ), sort_func, NULL );
|
2014-03-11 15:16:44 -04:00
|
|
|
}
|
2014-08-09 05:40:42 -04:00
|
|
|
g_free ( path );
|
2014-11-06 11:44:41 -05:00
|
|
|
|
|
|
|
unsigned int removed = 0;
|
|
|
|
for ( unsigned int index = num_favorites; index < ( ( *length ) - 1 ); index++ ) {
|
2015-05-14 13:45:57 -04:00
|
|
|
if ( g_strcmp0 ( retv[index], retv[index + 1] ) == 0 ) {
|
2014-11-06 11:44:41 -05:00
|
|
|
g_free ( retv[index] );
|
|
|
|
retv[index] = NULL;
|
|
|
|
removed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ( *length ) > num_favorites ) {
|
2015-09-19 06:21:30 -04:00
|
|
|
g_qsort_with_data ( &retv[num_favorites], ( *length ) - num_favorites, sizeof ( char* ),
|
|
|
|
sort_func,
|
|
|
|
NULL );
|
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
|
|
|
|
2016-01-14 14:40:19 -05:00
|
|
|
TICK_N ( "stop" );
|
2014-01-20 17:36:20 -05:00
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
2016-01-08 03:16:59 -05:00
|
|
|
static int run_mode_init ( Mode *sw )
|
2014-01-20 17:36:20 -05:00
|
|
|
{
|
2015-03-27 15:28:53 -04:00
|
|
|
if ( sw->private_data == NULL ) {
|
|
|
|
RunModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
|
|
|
|
sw->private_data = (void *) pd;
|
2015-11-21 17:59:59 -05:00
|
|
|
pd->cmd_list = get_apps ( &( pd->cmd_list_length ) );
|
2014-01-20 17:36:20 -05:00
|
|
|
}
|
2016-01-08 03:16:59 -05:00
|
|
|
|
|
|
|
return TRUE;
|
2015-03-27 15:28:53 -04:00
|
|
|
}
|
2016-03-20 05:20:40 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2015-11-25 03:26:38 -05:00
|
|
|
static unsigned int run_mode_get_num_entries ( const Mode *sw )
|
2015-03-27 15:28:53 -04:00
|
|
|
{
|
2015-11-23 15:52:55 -05:00
|
|
|
const RunModePrivateData *rmpd = (const RunModePrivateData *) sw->private_data;
|
2015-11-21 17:59:59 -05:00
|
|
|
return rmpd->cmd_list_length;
|
2015-03-27 15:28:53 -04:00
|
|
|
}
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2015-11-25 03:26:38 -05:00
|
|
|
static ModeMode run_mode_result ( Mode *sw, int mretv, char **input, unsigned int selected_line )
|
2015-03-27 15:28:53 -04:00
|
|
|
{
|
|
|
|
RunModePrivateData *rmpd = (RunModePrivateData *) sw->private_data;
|
2015-11-25 03:26:38 -05:00
|
|
|
ModeMode retv = MODE_EXIT;
|
2015-03-27 15:28:53 -04:00
|
|
|
|
2016-03-17 08:14:30 -04:00
|
|
|
gboolean run_in_term = ( ( mretv & MENU_CUSTOM_ACTION ) == MENU_CUSTOM_ACTION );
|
2015-03-27 15:28:53 -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
|
|
|
}
|
2015-03-27 15:28:53 -04:00
|
|
|
else if ( mretv & MENU_PREVIOUS ) {
|
2014-11-11 15:50:16 -05:00
|
|
|
retv = PREVIOUS_DIALOG;
|
|
|
|
}
|
2015-03-27 15:28:53 -04:00
|
|
|
else if ( mretv & MENU_QUICK_SWITCH ) {
|
2015-05-03 07:04:03 -04:00
|
|
|
retv = ( mretv & MENU_LOWER_MASK );
|
2014-07-21 17:19:45 -04:00
|
|
|
}
|
2015-03-27 15:28:53 -04:00
|
|
|
else if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line] != NULL ) {
|
2016-03-17 08:14:30 -04:00
|
|
|
exec_cmd ( rmpd->cmd_list[selected_line], run_in_term );
|
2014-03-22 16:04:19 -04:00
|
|
|
}
|
2015-03-27 15:28:53 -04:00
|
|
|
else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) {
|
2016-03-17 08:14:30 -04:00
|
|
|
exec_cmd ( *input, run_in_term );
|
2014-03-22 16:04:19 -04:00
|
|
|
}
|
2015-03-27 15:28:53 -04:00
|
|
|
else if ( ( mretv & MENU_ENTRY_DELETE ) && rmpd->cmd_list[selected_line] ) {
|
|
|
|
delete_entry ( rmpd->cmd_list[selected_line] );
|
2015-05-02 05:53:27 -04:00
|
|
|
|
|
|
|
// Clear the list.
|
2016-03-21 15:27:16 -04:00
|
|
|
retv = RELOAD_DIALOG;
|
2016-03-20 05:20:40 -04:00
|
|
|
run_mode_destroy ( sw );
|
|
|
|
run_mode_init ( sw );
|
2014-01-20 17:36:20 -05:00
|
|
|
}
|
2015-03-27 15:28:53 -04:00
|
|
|
return retv;
|
|
|
|
}
|
2014-01-20 17:36:20 -05:00
|
|
|
|
2016-01-07 15:27:20 -05:00
|
|
|
static char *_get_display_value ( const Mode *sw, unsigned int selected_line, G_GNUC_UNUSED int *state, int get_entry )
|
2015-11-21 17:59:59 -05:00
|
|
|
{
|
2015-11-21 18:33:26 -05:00
|
|
|
const RunModePrivateData *rmpd = (const RunModePrivateData *) sw->private_data;
|
2015-11-21 17:59:59 -05:00
|
|
|
return get_entry ? g_strdup ( rmpd->cmd_list[selected_line] ) : NULL;
|
|
|
|
}
|
2016-05-22 11:47:34 -04:00
|
|
|
static int run_token_match ( const Mode *sw, GRegex **tokens, unsigned int index )
|
2015-03-31 16:45:02 -04:00
|
|
|
{
|
2015-11-21 18:33:26 -05:00
|
|
|
const RunModePrivateData *rmpd = (const RunModePrivateData *) sw->private_data;
|
2016-05-26 02:39:33 -04:00
|
|
|
return token_match ( tokens, rmpd->cmd_list[index] );
|
2015-11-21 17:59:59 -05:00
|
|
|
}
|
2016-01-07 15:27:20 -05:00
|
|
|
|
|
|
|
#include "mode-private.h"
|
2015-11-25 03:26:38 -05:00
|
|
|
Mode run_mode =
|
2015-03-27 15:28:53 -04:00
|
|
|
{
|
2016-01-07 15:27:20 -05:00
|
|
|
.name = "run",
|
2016-01-12 16:17:53 -05:00
|
|
|
.cfg_name_key = "display-run",
|
2016-01-07 15:27:20 -05:00
|
|
|
._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,
|
|
|
|
._get_display_value = _get_display_value,
|
|
|
|
._get_completion = NULL,
|
2016-05-26 02:39:33 -04:00
|
|
|
._preprocess_input = NULL,
|
2016-01-07 15:27:20 -05:00
|
|
|
.private_data = NULL,
|
|
|
|
.free = NULL
|
2015-03-27 15:28:53 -04:00
|
|
|
};
|
2016-01-06 06:40:41 -05:00
|
|
|
/*@}*/
|