2016-01-06 11:40:41 +00:00
|
|
|
/*
|
2014-03-01 16:27:52 +00:00
|
|
|
* rofi
|
2014-01-20 22:36:20 +00:00
|
|
|
*
|
|
|
|
* MIT/X11 License
|
2020-01-01 11:23:12 +00:00
|
|
|
* Copyright © 2013-2020 Qball Cow <qball@gmpclient.org>
|
2014-01-20 22:36:20 +00: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 11:40:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \ingroup RUNMode
|
|
|
|
* @{
|
|
|
|
*/
|
2017-04-15 09:58:49 +00:00
|
|
|
|
|
|
|
/** The log domain of this dialog. */
|
2017-04-27 20:59:14 +00:00
|
|
|
#define G_LOG_DOMAIN "Dialogs.Run"
|
2017-04-15 09:58:49 +00:00
|
|
|
|
2014-08-22 07:26:46 +00:00
|
|
|
#include <config.h>
|
2014-01-20 23:20:09 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2014-01-20 22:36:20 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
2014-03-08 20:35:27 +00:00
|
|
|
#include <limits.h>
|
2014-01-20 22:36:20 +00:00
|
|
|
#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-01-20 22:36:20 +00:00
|
|
|
|
2014-03-12 07:41:38 +00:00
|
|
|
#include "rofi.h"
|
2016-01-07 15:01:56 +00:00
|
|
|
#include "settings.h"
|
2014-09-03 11:07:26 +00:00
|
|
|
#include "helper.h"
|
2014-05-13 08:45:59 +00:00
|
|
|
#include "history.h"
|
2015-03-25 07:36:19 +00:00
|
|
|
#include "dialogs/run.h"
|
2021-06-01 11:14:45 +00:00
|
|
|
#include "dialogs/filebrowser.h"
|
2014-01-20 22:36:20 +00:00
|
|
|
|
2016-01-07 18:47:37 +00:00
|
|
|
#include "mode-private.h"
|
2017-02-14 07:52:17 +00:00
|
|
|
|
|
|
|
#include "timings.h"
|
2018-06-09 17:13:57 +00:00
|
|
|
#include "rofi-icon-fetcher.h"
|
2016-01-06 11:40:41 +00:00
|
|
|
/**
|
2017-11-23 17:41:52 +00:00
|
|
|
* Name of the history file where previously chosen commands are stored.
|
2016-01-06 11:40:41 +00:00
|
|
|
*/
|
2016-01-14 19:40:19 +00:00
|
|
|
#define RUN_CACHE_FILE "rofi-3.runcache"
|
2016-10-25 20:45:11 +00:00
|
|
|
|
2021-06-08 17:28:13 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *entry;
|
|
|
|
uint32_t icon_fetch_uid;
|
|
|
|
/* Surface holding the icon. */
|
|
|
|
cairo_surface_t *icon;
|
|
|
|
} RunEntry;
|
|
|
|
|
2016-01-06 11:40:41 +00:00
|
|
|
/**
|
|
|
|
* The internal data structure holding the private data of the Run Mode.
|
|
|
|
*/
|
2016-03-24 21:15:10 +00:00
|
|
|
typedef struct
|
2016-01-06 11:40:41 +00:00
|
|
|
{
|
|
|
|
/** list of available commands. */
|
2021-06-08 17:28:13 +00:00
|
|
|
RunEntry *cmd_list;
|
2016-01-06 11:40:41 +00:00
|
|
|
/** Length of the #cmd_list. */
|
|
|
|
unsigned int cmd_list_length;
|
2021-06-01 11:14:45 +00:00
|
|
|
|
|
|
|
/** Current mode. */
|
|
|
|
gboolean file_complete;
|
|
|
|
uint32_t selected_line;
|
|
|
|
char *old_input;
|
|
|
|
|
|
|
|
Mode *completer;
|
|
|
|
char *old_completer_input;
|
2021-06-08 18:21:28 +00:00
|
|
|
/** fallback icon */
|
|
|
|
uint32_t fallback_icon_fetch_uid;
|
|
|
|
cairo_surface_t *fallback_icon;
|
2016-01-06 11:40:41 +00:00
|
|
|
} RunModePrivateData;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
|
|
|
*/
|
2020-09-13 19:56:25 +00:00
|
|
|
static gboolean exec_cmd ( const char *cmd, int run_in_term )
|
2014-01-20 22:36:20 +00:00
|
|
|
{
|
2016-01-14 19:40:19 +00:00
|
|
|
GError *error = NULL;
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( !cmd || !cmd[0] ) {
|
2020-09-13 19:56:25 +00:00
|
|
|
return FALSE;
|
2014-03-22 20:04:19 +00:00
|
|
|
}
|
2016-01-14 19:40:19 +00:00
|
|
|
gsize lf_cmd_size = 0;
|
|
|
|
gchar *lf_cmd = g_locale_from_utf8 ( cmd, -1, NULL, &lf_cmd_size, &error );
|
|
|
|
if ( error != NULL ) {
|
2017-04-15 09:58:49 +00:00
|
|
|
g_warning ( "Failed to convert command to locale encoding: %s", error->message );
|
2016-01-14 19:40:19 +00:00
|
|
|
g_error_free ( error );
|
2020-09-13 19:56:25 +00:00
|
|
|
return FALSE;
|
2016-01-14 19:40:19 +00:00
|
|
|
}
|
2014-01-20 22:36:20 +00:00
|
|
|
|
2017-06-03 18:35:50 +00:00
|
|
|
char *path = g_build_filename ( cache_dir, RUN_CACHE_FILE, NULL );
|
2017-06-02 12:57:19 +00:00
|
|
|
RofiHelperExecuteContext context = { .name = NULL };
|
|
|
|
// FIXME: assume startup notification support for terminals
|
|
|
|
if ( helper_execute_command ( NULL, lf_cmd, run_in_term, run_in_term ? &context : NULL ) ) {
|
2015-09-29 15:27:47 +00:00
|
|
|
/**
|
|
|
|
* This happens in non-critical time (After launching app)
|
|
|
|
* It is allowed to be a bit slower.
|
|
|
|
*/
|
2014-01-20 22:36:20 +00:00
|
|
|
|
2015-09-29 15:27:47 +00:00
|
|
|
history_set ( path, cmd );
|
2020-09-13 19:56:25 +00:00
|
|
|
g_free ( path );
|
|
|
|
g_free ( lf_cmd );
|
|
|
|
return TRUE;
|
2015-09-29 15:27:47 +00:00
|
|
|
}
|
2016-09-17 19:28:22 +00:00
|
|
|
else {
|
|
|
|
history_remove ( path, cmd );
|
2020-09-13 19:56:25 +00:00
|
|
|
g_free ( path );
|
|
|
|
g_free ( lf_cmd );
|
|
|
|
return FALSE;
|
2016-09-17 19:28:22 +00:00
|
|
|
}
|
2014-01-20 22:36:20 +00:00
|
|
|
}
|
2016-01-06 11:40:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param cmd The command to remove from history
|
|
|
|
*
|
|
|
|
* Remove command from history.
|
|
|
|
*/
|
2021-06-08 17:28:13 +00:00
|
|
|
static void delete_entry ( const RunEntry *cmd )
|
2014-02-01 13:03:23 +00:00
|
|
|
{
|
2016-01-06 11:40:41 +00:00
|
|
|
char *path = g_build_filename ( cache_dir, RUN_CACHE_FILE, NULL );
|
2014-08-09 09:40:42 +00:00
|
|
|
|
2021-06-08 17:28:13 +00:00
|
|
|
history_remove ( path, cmd->entry );
|
2014-02-01 13:03:23 +00:00
|
|
|
|
2014-08-09 09:40:42 +00:00
|
|
|
g_free ( path );
|
2014-02-01 13:03:23 +00:00
|
|
|
}
|
2016-01-06 11:40:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param a The First key to compare
|
|
|
|
* @param b The second key to compare
|
2016-07-29 06:32:34 +00:00
|
|
|
* @param data Unused.
|
2016-01-06 11:40:41 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-07-29 06:32:34 +00:00
|
|
|
static int sort_func ( const void *a, const void *b, G_GNUC_UNUSED void *data )
|
2014-01-20 22:36:20 +00:00
|
|
|
{
|
2021-06-08 17:28:13 +00:00
|
|
|
const RunEntry *astr = ( const RunEntry * ) a;
|
|
|
|
const RunEntry *bstr = ( const RunEntry * ) b;
|
2014-11-06 16:44:41 +00:00
|
|
|
|
2021-06-08 17:28:13 +00:00
|
|
|
if ( astr->entry == NULL && bstr->entry == NULL ) {
|
2014-11-06 16:44:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2021-06-08 17:28:13 +00:00
|
|
|
else if ( astr->entry == NULL ) {
|
2014-11-06 16:44:41 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2021-06-08 17:28:13 +00:00
|
|
|
else if ( bstr->entry == NULL ) {
|
2014-11-06 16:44:41 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2021-06-08 17:28:13 +00:00
|
|
|
return g_strcmp0 ( astr->entry , bstr->entry );
|
2014-01-20 22:36:20 +00:00
|
|
|
}
|
2015-01-05 20:53:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* External spider to get list of executables.
|
|
|
|
*/
|
2021-06-08 17:28:13 +00:00
|
|
|
static RunEntry * get_apps_external ( RunEntry *retv, unsigned int *length, unsigned int num_favorites )
|
2015-01-05 20:53:50 +00:00
|
|
|
{
|
|
|
|
int fd = execute_generator ( config.run_list_command );
|
|
|
|
if ( fd >= 0 ) {
|
|
|
|
FILE *inp = fdopen ( fd, "r" );
|
|
|
|
if ( inp ) {
|
2016-04-10 12:30:13 +00:00
|
|
|
char *buffer = NULL;
|
|
|
|
size_t buffer_length = 0;
|
|
|
|
|
|
|
|
while ( getline ( &buffer, &buffer_length, inp ) > 0 ) {
|
2015-01-07 14:53:41 +00: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++ ) {
|
2021-06-08 17:28:13 +00:00
|
|
|
if ( strcasecmp ( buffer, retv[j].entry ) == 0 ) {
|
2015-01-07 14:53:41 +00:00
|
|
|
found = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( found == 1 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No duplicate, add it.
|
2021-06-08 17:28:13 +00:00
|
|
|
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( RunEntry ) );
|
|
|
|
retv[( *length )].entry = g_strdup ( buffer );
|
|
|
|
retv[( *length )].icon = NULL;
|
|
|
|
retv[( *length )].icon_fetch_uid = 0;
|
2015-01-05 20:53:50 +00:00
|
|
|
|
|
|
|
( *length )++;
|
|
|
|
}
|
2016-04-10 12:30:13 +00:00
|
|
|
if ( buffer != NULL ) {
|
|
|
|
free ( buffer );
|
|
|
|
}
|
2015-07-31 08:21:32 +00:00
|
|
|
if ( fclose ( inp ) != 0 ) {
|
2017-04-15 09:58:49 +00:00
|
|
|
g_warning ( "Failed to close stdout off executor script: '%s'",
|
2017-04-27 20:59:14 +00:00
|
|
|
g_strerror ( errno ) );
|
2015-07-31 08:21:32 +00:00
|
|
|
}
|
2015-01-05 20:53:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-08 17:28:13 +00:00
|
|
|
retv[( *length ) ].entry = NULL;
|
|
|
|
retv[( *length ) ].icon = NULL;
|
|
|
|
retv[( *length ) ].icon_fetch_uid = 0;
|
2015-01-05 20:53:50 +00:00
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal spider used to get list of executables.
|
|
|
|
*/
|
2021-06-08 17:28:13 +00:00
|
|
|
static RunEntry * get_apps ( unsigned int *length )
|
2014-01-20 22:36:20 +00:00
|
|
|
{
|
2016-01-14 19:40:19 +00:00
|
|
|
GError *error = NULL;
|
2021-06-08 17:28:13 +00:00
|
|
|
RunEntry *retv = NULL;
|
2015-02-19 12:22:10 +00:00
|
|
|
unsigned int num_favorites = 0;
|
|
|
|
char *path;
|
2014-01-20 22:36:20 +00:00
|
|
|
|
2015-05-14 17:45:57 +00:00
|
|
|
if ( g_getenv ( "PATH" ) == NULL ) {
|
2014-03-22 20:04:19 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-01-14 19:40:19 +00:00
|
|
|
TICK_N ( "start" );
|
2016-01-06 11:40:41 +00:00
|
|
|
path = g_build_filename ( cache_dir, RUN_CACHE_FILE, NULL );
|
2021-06-08 17:28:13 +00:00
|
|
|
char **hretv = history_get_list ( path, length );
|
|
|
|
retv = (RunEntry*)g_malloc0((*length+1)*sizeof(RunEntry));
|
|
|
|
for(unsigned int i =0; i < *length; i++ ){
|
|
|
|
retv[i].entry = hretv[i];
|
|
|
|
}
|
|
|
|
g_free(hretv);
|
2014-08-09 09:40:42 +00:00
|
|
|
g_free ( path );
|
|
|
|
// Keep track of how many where loaded as favorite.
|
|
|
|
num_favorites = ( *length );
|
2014-01-20 22:36:20 +00:00
|
|
|
|
2016-01-14 20:06:03 +00:00
|
|
|
path = g_strdup ( g_getenv ( "PATH" ) );
|
2014-01-20 22:36:20 +00:00
|
|
|
|
2016-01-14 19:40:19 +00:00
|
|
|
gsize l = 0;
|
|
|
|
gchar *homedir = g_locale_to_utf8 ( g_get_home_dir (), -1, NULL, &l, &error );
|
|
|
|
if ( error != NULL ) {
|
2017-04-15 09:37:50 +00:00
|
|
|
g_debug ( "Failed to convert homedir to UTF-8: %s", error->message );
|
2016-01-14 20:06:03 +00:00
|
|
|
g_clear_error ( &error );
|
2016-01-14 19:40:19 +00:00
|
|
|
g_free ( homedir );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-11-01 07:04:39 +00:00
|
|
|
const char *const sep = ":";
|
|
|
|
char *strtok_savepointer = NULL;
|
|
|
|
for ( const char *dirname = strtok_r ( path, sep, &strtok_savepointer ); dirname != NULL; dirname = strtok_r ( NULL, sep, &strtok_savepointer ) ) {
|
2017-04-19 07:26:35 +00:00
|
|
|
char *fpath = rofi_expand_path ( dirname );
|
2017-04-27 20:59:14 +00:00
|
|
|
DIR *dir = opendir ( fpath );
|
2017-04-19 08:24:09 +00:00
|
|
|
g_debug ( "Checking path %s for executable.", fpath );
|
2017-04-19 07:26:35 +00:00
|
|
|
g_free ( fpath );
|
2014-01-20 22:36:20 +00:00
|
|
|
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( dir != NULL ) {
|
2014-01-20 22:36:20 +00:00
|
|
|
struct dirent *dent;
|
2016-01-14 19:40:19 +00:00
|
|
|
gsize dirn_len = 0;
|
2016-01-14 20:06:03 +00:00
|
|
|
gchar *dirn = g_locale_to_utf8 ( dirname, -1, NULL, &dirn_len, &error );
|
2016-01-14 19:40:19 +00:00
|
|
|
if ( error != NULL ) {
|
2017-04-15 09:37:50 +00:00
|
|
|
g_debug ( "Failed to convert directory name to UTF-8: %s", error->message );
|
2016-01-14 20:06:03 +00:00
|
|
|
g_clear_error ( &error );
|
2016-10-17 18:54:41 +00:00
|
|
|
closedir ( dir );
|
2016-01-14 19:40:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-01-14 20:06:03 +00:00
|
|
|
gboolean is_homedir = g_str_has_prefix ( dirn, homedir );
|
2016-01-14 19:40:19 +00:00
|
|
|
g_free ( dirn );
|
2014-01-20 22:36:20 +00:00
|
|
|
|
2014-06-04 19:29:23 +00:00
|
|
|
while ( ( dent = readdir ( dir ) ) != NULL ) {
|
2015-09-19 18:59:50 +00:00
|
|
|
if ( dent->d_type != DT_REG && dent->d_type != DT_LNK && dent->d_type != DT_UNKNOWN ) {
|
2014-01-20 22:36:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-06-07 09:00:21 +00:00
|
|
|
// Skip dot files.
|
2014-07-05 17:47:55 +00:00
|
|
|
if ( dent->d_name[0] == '.' ) {
|
2014-06-07 09:00:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-01-14 19:40:19 +00: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 22:36:20 +00:00
|
|
|
|
2016-01-14 19:40:19 +00:00
|
|
|
gsize name_len;
|
|
|
|
gchar *name = g_filename_to_utf8 ( dent->d_name, -1, NULL, &name_len, &error );
|
|
|
|
if ( error != NULL ) {
|
2017-04-15 09:37:50 +00:00
|
|
|
g_debug ( "Failed to convert filename to UTF-8: %s", error->message );
|
2016-01-14 20:06:03 +00:00
|
|
|
g_clear_error ( &error );
|
2016-01-14 19:40:19 +00:00
|
|
|
g_free ( name );
|
|
|
|
continue;
|
|
|
|
}
|
2014-01-20 22:36:20 +00:00
|
|
|
// This is a nice little penalty, but doable? time will tell.
|
|
|
|
// given num_favorites is max 25.
|
2016-01-14 19:40:19 +00:00
|
|
|
int found = 0;
|
2014-06-04 19:29:23 +00:00
|
|
|
for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) {
|
2021-06-08 17:28:13 +00:00
|
|
|
if ( g_strcmp0 ( name, retv[j].entry ) == 0 ) {
|
2014-03-22 20:04:19 +00:00
|
|
|
found = 1;
|
|
|
|
}
|
2014-01-20 22:36:20 +00:00
|
|
|
}
|
|
|
|
|
2014-06-04 19:29:23 +00:00
|
|
|
if ( found == 1 ) {
|
2016-08-02 16:37:14 +00:00
|
|
|
g_free ( name );
|
2014-03-22 20:04:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-01-20 22:36:20 +00:00
|
|
|
|
2021-06-08 17:28:13 +00:00
|
|
|
retv = g_realloc ( retv, ( ( *length ) + 2 ) * sizeof ( RunEntry ) );
|
|
|
|
retv[( *length )].entry = name;
|
|
|
|
retv[( *length )].icon = NULL;
|
|
|
|
retv[( *length )].icon_fetch_uid = 0;
|
|
|
|
retv[( *length ) + 1].entry = NULL;
|
|
|
|
retv[( *length ) + 1].icon = NULL;
|
|
|
|
retv[( *length ) + 1].icon_fetch_uid = 0;
|
2014-08-09 09:40:42 +00:00
|
|
|
( *length )++;
|
2014-01-20 22:36:20 +00:00
|
|
|
}
|
|
|
|
|
2014-03-22 20:04:19 +00:00
|
|
|
closedir ( dir );
|
2014-01-20 22:36:20 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-14 19:40:19 +00:00
|
|
|
g_free ( homedir );
|
2014-01-20 22:36:20 +00:00
|
|
|
|
2015-01-07 14:53:41 +00:00
|
|
|
// Get external apps.
|
2015-01-05 21:16:13 +00:00
|
|
|
if ( config.run_list_command != NULL && config.run_list_command[0] != '\0' ) {
|
2015-01-07 14:53:41 +00:00
|
|
|
retv = get_apps_external ( retv, length, num_favorites );
|
2015-01-05 21:16:13 +00:00
|
|
|
}
|
2015-01-06 10:45:01 +00:00
|
|
|
// No sorting needed.
|
2015-01-07 14:53:41 +00:00
|
|
|
if ( ( *length ) == 0 ) {
|
2015-01-06 10:45:01 +00:00
|
|
|
return retv;
|
|
|
|
}
|
2014-01-20 22:36:20 +00:00
|
|
|
// TODO: check this is still fast enough. (takes 1ms on laptop.)
|
2014-07-20 10:29:27 +00:00
|
|
|
if ( ( *length ) > num_favorites ) {
|
2021-06-08 17:28:13 +00:00
|
|
|
g_qsort_with_data ( &(retv[num_favorites]), ( *length ) - num_favorites, sizeof ( RunEntry ), sort_func, NULL );
|
2014-03-11 19:16:44 +00:00
|
|
|
}
|
2014-08-09 09:40:42 +00:00
|
|
|
g_free ( path );
|
2014-11-06 16:44:41 +00:00
|
|
|
|
|
|
|
unsigned int removed = 0;
|
|
|
|
for ( unsigned int index = num_favorites; index < ( ( *length ) - 1 ); index++ ) {
|
2021-06-08 17:28:13 +00:00
|
|
|
if ( g_strcmp0 ( retv[index].entry , retv[index + 1].entry ) == 0 ) {
|
|
|
|
g_free ( retv[index].entry );
|
|
|
|
retv[index].entry = NULL;
|
2014-11-06 16:44:41 +00:00
|
|
|
removed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ( *length ) > num_favorites ) {
|
2021-06-08 17:28:13 +00:00
|
|
|
g_qsort_with_data ( &(retv[num_favorites]), ( *length ) - num_favorites, sizeof ( RunEntry ),
|
2015-09-19 10:21:30 +00:00
|
|
|
sort_func,
|
|
|
|
NULL );
|
2014-01-20 22:36:20 +00:00
|
|
|
}
|
2014-11-06 16:44:41 +00:00
|
|
|
// Reduce array length;
|
|
|
|
( *length ) -= removed;
|
2014-01-20 22:36:20 +00:00
|
|
|
|
2016-01-14 19:40:19 +00:00
|
|
|
TICK_N ( "stop" );
|
2014-01-20 22:36:20 +00:00
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
2016-01-08 08:16:59 +00:00
|
|
|
static int run_mode_init ( Mode *sw )
|
2014-01-20 22:36:20 +00:00
|
|
|
{
|
2015-03-27 19:28:53 +00:00
|
|
|
if ( sw->private_data == NULL ) {
|
|
|
|
RunModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
|
|
|
|
sw->private_data = (void *) pd;
|
2015-11-21 22:59:59 +00:00
|
|
|
pd->cmd_list = get_apps ( &( pd->cmd_list_length ) );
|
2021-06-01 11:14:45 +00:00
|
|
|
pd->completer = create_new_file_browser ();
|
|
|
|
mode_init ( pd->completer );
|
2014-01-20 22:36:20 +00:00
|
|
|
}
|
2016-01-08 08:16:59 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2015-03-27 19:28:53 +00:00
|
|
|
}
|
2016-03-20 09:20:40 +00:00
|
|
|
static void run_mode_destroy ( Mode *sw )
|
|
|
|
{
|
|
|
|
RunModePrivateData *rmpd = (RunModePrivateData *) sw->private_data;
|
|
|
|
if ( rmpd != NULL ) {
|
2021-06-08 17:28:13 +00:00
|
|
|
for ( unsigned int i = 0; i < rmpd->cmd_list_length; i++ ){
|
|
|
|
g_free ( rmpd->cmd_list[i].entry );
|
|
|
|
if ( rmpd->cmd_list[i].icon != NULL ) {
|
|
|
|
cairo_surface_destroy ( rmpd->cmd_list[i].icon );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free ( rmpd->cmd_list );
|
2021-06-01 11:14:45 +00:00
|
|
|
g_free ( rmpd->old_input );
|
|
|
|
g_free ( rmpd->old_completer_input );
|
|
|
|
mode_destroy ( rmpd->completer );
|
2016-03-20 09:20:40 +00:00
|
|
|
g_free ( rmpd );
|
|
|
|
sw->private_data = NULL;
|
|
|
|
}
|
|
|
|
}
|
2014-01-20 22:36:20 +00:00
|
|
|
|
2015-11-25 08:26:38 +00:00
|
|
|
static unsigned int run_mode_get_num_entries ( const Mode *sw )
|
2015-03-27 19:28:53 +00:00
|
|
|
{
|
2015-11-23 20:52:55 +00:00
|
|
|
const RunModePrivateData *rmpd = (const RunModePrivateData *) sw->private_data;
|
2021-06-01 11:14:45 +00:00
|
|
|
if ( rmpd->file_complete ){
|
|
|
|
return rmpd->completer->_get_num_entries( rmpd->completer );
|
|
|
|
}
|
2015-11-21 22:59:59 +00:00
|
|
|
return rmpd->cmd_list_length;
|
2015-03-27 19:28:53 +00:00
|
|
|
}
|
2014-01-20 22:36:20 +00:00
|
|
|
|
2015-11-25 08:26:38 +00:00
|
|
|
static ModeMode run_mode_result ( Mode *sw, int mretv, char **input, unsigned int selected_line )
|
2015-03-27 19:28:53 +00:00
|
|
|
{
|
|
|
|
RunModePrivateData *rmpd = (RunModePrivateData *) sw->private_data;
|
2015-11-25 08:26:38 +00:00
|
|
|
ModeMode retv = MODE_EXIT;
|
2015-03-27 19:28:53 +00:00
|
|
|
|
2016-03-17 12:14:30 +00:00
|
|
|
gboolean run_in_term = ( ( mretv & MENU_CUSTOM_ACTION ) == MENU_CUSTOM_ACTION );
|
2021-06-01 11:14:45 +00:00
|
|
|
if ( rmpd->file_complete == TRUE ) {
|
|
|
|
|
|
|
|
retv = RELOAD_DIALOG;
|
|
|
|
|
|
|
|
if ( ( mretv& (MENU_COMPLETE)) ) {
|
|
|
|
g_free ( rmpd->old_completer_input );
|
|
|
|
rmpd->old_completer_input = *input;
|
|
|
|
*input = NULL;
|
|
|
|
if ( rmpd->selected_line < rmpd->cmd_list_length ) {
|
|
|
|
(*input) = g_strdup ( rmpd->old_input );
|
|
|
|
}
|
|
|
|
rmpd->file_complete = FALSE;
|
|
|
|
} else if ( (mretv&MENU_CANCEL) ) {
|
|
|
|
retv = MODE_EXIT;
|
|
|
|
} else {
|
|
|
|
char *path = NULL;
|
|
|
|
retv = file_browser_mode_completer ( rmpd->completer, mretv, input, selected_line, &path );
|
|
|
|
if ( retv == MODE_EXIT ) {
|
|
|
|
if ( path == NULL )
|
|
|
|
{
|
2021-06-08 17:28:13 +00:00
|
|
|
exec_cmd ( rmpd->cmd_list[rmpd->selected_line].entry, run_in_term );
|
2021-06-01 11:14:45 +00:00
|
|
|
} else {
|
2021-06-08 17:28:13 +00:00
|
|
|
char *arg= g_strdup_printf ( "%s '%s'", rmpd->cmd_list[rmpd->selected_line].entry, path);
|
2021-06-01 11:14:45 +00:00
|
|
|
exec_cmd ( arg, run_in_term );
|
|
|
|
g_free(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
g_free (path);
|
|
|
|
}
|
|
|
|
return retv;
|
|
|
|
}
|
2015-03-27 19:28:53 +00:00
|
|
|
|
2021-06-08 17:28:13 +00:00
|
|
|
if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line].entry != NULL ) {
|
|
|
|
if ( !exec_cmd ( rmpd->cmd_list[selected_line].entry, run_in_term ) ) {
|
2020-11-03 22:57:02 +00:00
|
|
|
retv = RELOAD_DIALOG;
|
2020-09-13 19:56:25 +00:00
|
|
|
}
|
2014-03-22 20:04:19 +00:00
|
|
|
}
|
2015-03-27 19:28:53 +00:00
|
|
|
else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) {
|
2020-09-13 19:56:25 +00:00
|
|
|
if ( !exec_cmd ( *input, run_in_term ) ) {
|
2020-11-03 22:57:02 +00:00
|
|
|
retv = RELOAD_DIALOG;
|
2020-09-13 19:56:25 +00:00
|
|
|
}
|
2014-03-22 20:04:19 +00:00
|
|
|
}
|
2021-06-08 17:28:13 +00:00
|
|
|
else if ( ( mretv & MENU_ENTRY_DELETE ) && rmpd->cmd_list[selected_line].entry ) {
|
|
|
|
delete_entry ( &(rmpd->cmd_list[selected_line]) );
|
2015-05-02 09:53:27 +00:00
|
|
|
|
|
|
|
// Clear the list.
|
2016-03-21 19:27:16 +00:00
|
|
|
retv = RELOAD_DIALOG;
|
2016-03-20 09:20:40 +00:00
|
|
|
run_mode_destroy ( sw );
|
|
|
|
run_mode_init ( sw );
|
2021-06-01 08:44:23 +00:00
|
|
|
}
|
|
|
|
else if ( mretv & MENU_CUSTOM_COMMAND ) {
|
2021-04-10 21:04:05 +00:00
|
|
|
retv = ( mretv & MENU_LOWER_MASK );
|
2021-06-01 11:14:45 +00:00
|
|
|
} else if ( ( mretv& MENU_COMPLETE) ) {
|
|
|
|
retv = RELOAD_DIALOG;
|
|
|
|
if ( selected_line < rmpd->cmd_list_length ) {
|
|
|
|
rmpd->selected_line = selected_line;
|
|
|
|
|
|
|
|
g_free ( rmpd->old_input );
|
|
|
|
rmpd->old_input = g_strdup ( *input );
|
|
|
|
|
|
|
|
if ( *input ) g_free (*input);
|
|
|
|
*input = g_strdup ( rmpd->old_completer_input );
|
|
|
|
|
|
|
|
rmpd->file_complete = TRUE;
|
|
|
|
}
|
2014-01-20 22:36:20 +00:00
|
|
|
}
|
2015-03-27 19:28:53 +00:00
|
|
|
return retv;
|
|
|
|
}
|
2014-01-20 22:36:20 +00:00
|
|
|
|
2017-03-10 22:39:29 +00:00
|
|
|
static char *_get_display_value ( const Mode *sw, unsigned int selected_line, G_GNUC_UNUSED int *state, G_GNUC_UNUSED GList **list, int get_entry )
|
2015-11-21 22:59:59 +00:00
|
|
|
{
|
2015-11-21 23:33:26 +00:00
|
|
|
const RunModePrivateData *rmpd = (const RunModePrivateData *) sw->private_data;
|
2021-06-01 11:14:45 +00:00
|
|
|
if ( rmpd->file_complete ){
|
|
|
|
return rmpd->completer->_get_display_value (rmpd->completer, selected_line, state, list, get_entry );
|
|
|
|
}
|
2021-06-08 17:28:13 +00:00
|
|
|
return get_entry ? g_strdup ( rmpd->cmd_list[selected_line].entry ) : NULL;
|
2015-11-21 22:59:59 +00:00
|
|
|
}
|
2018-06-09 17:13:57 +00:00
|
|
|
|
2017-09-29 06:32:09 +00:00
|
|
|
static int run_token_match ( const Mode *sw, rofi_int_matcher **tokens, unsigned int index )
|
2015-03-31 20:45:02 +00:00
|
|
|
{
|
2015-11-21 23:33:26 +00:00
|
|
|
const RunModePrivateData *rmpd = (const RunModePrivateData *) sw->private_data;
|
2021-06-01 11:14:45 +00:00
|
|
|
if ( rmpd->file_complete ){
|
|
|
|
return rmpd->completer->_token_match (rmpd->completer, tokens, index );
|
|
|
|
}
|
2021-06-08 17:28:13 +00:00
|
|
|
return helper_token_match ( tokens, rmpd->cmd_list[index].entry );
|
2015-11-21 22:59:59 +00:00
|
|
|
}
|
2021-06-01 11:14:45 +00:00
|
|
|
static char *run_get_message ( const Mode *sw )
|
|
|
|
{
|
|
|
|
RunModePrivateData *pd = sw->private_data;
|
|
|
|
if ( pd->file_complete ) {
|
|
|
|
if ( pd->selected_line < pd->cmd_list_length ) {
|
|
|
|
char *msg = mode_get_message ( pd->completer);
|
|
|
|
if (msg ){
|
2021-06-08 17:28:13 +00:00
|
|
|
char *retv = g_strdup_printf("File complete for: %s\n%s", pd->cmd_list[pd->selected_line].entry, msg);
|
2021-06-01 11:14:45 +00:00
|
|
|
g_free (msg);
|
|
|
|
return retv;
|
|
|
|
}
|
2021-06-08 17:28:13 +00:00
|
|
|
return g_strdup_printf("File complete for: %s", pd->cmd_list[pd->selected_line].entry);
|
2021-06-01 11:14:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-06-08 18:21:28 +00:00
|
|
|
static cairo_surface_t *fallback_icon ( RunModePrivateData *pd, int height )
|
|
|
|
{
|
|
|
|
if ( config.application_fallback_icon ) {
|
|
|
|
// FALLBACK
|
|
|
|
if ( pd->fallback_icon_fetch_uid > 0 ) {
|
|
|
|
return rofi_icon_fetcher_get ( pd->fallback_icon_fetch_uid );
|
|
|
|
}
|
|
|
|
pd->fallback_icon_fetch_uid = rofi_icon_fetcher_query ( config.application_fallback_icon, height );
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
2021-06-08 17:28:13 +00:00
|
|
|
static cairo_surface_t *_get_icon ( const Mode *sw, unsigned int selected_line, int height )
|
|
|
|
{
|
|
|
|
RunModePrivateData *pd = (RunModePrivateData *) mode_get_private_data ( sw );
|
|
|
|
if ( pd->file_complete ) {
|
|
|
|
return pd->completer->_get_icon ( pd->completer, selected_line, height );
|
|
|
|
}
|
|
|
|
g_return_val_if_fail ( pd->cmd_list != NULL, NULL );
|
|
|
|
RunEntry *dr = &( pd->cmd_list[selected_line] );
|
2021-06-08 18:21:28 +00:00
|
|
|
|
2021-06-08 17:28:13 +00:00
|
|
|
if ( dr->icon_fetch_uid > 0 ) {
|
2021-06-08 18:21:28 +00:00
|
|
|
cairo_surface_t *icon = rofi_icon_fetcher_get ( dr->icon_fetch_uid );
|
|
|
|
if ( icon ) {
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
return fallback_icon ( pd, height );
|
2021-06-08 17:28:13 +00:00
|
|
|
}
|
2021-06-08 18:21:28 +00:00
|
|
|
/** lookup icon */
|
2021-06-08 17:28:13 +00:00
|
|
|
char ** str = g_strsplit(dr->entry, " ", 2);
|
|
|
|
if ( str ) {
|
|
|
|
dr->icon_fetch_uid = rofi_icon_fetcher_query ( str[0], height );
|
|
|
|
g_strfreev ( str );
|
2021-06-08 18:21:28 +00:00
|
|
|
cairo_surface_t *icon = rofi_icon_fetcher_get ( dr->icon_fetch_uid );
|
|
|
|
if ( icon ) {
|
|
|
|
return icon;
|
|
|
|
}
|
2021-06-08 17:28:13 +00:00
|
|
|
}
|
2021-06-08 18:21:28 +00:00
|
|
|
return fallback_icon ( pd, height );
|
2021-06-08 17:28:13 +00:00
|
|
|
}
|
2016-01-07 20:27:20 +00:00
|
|
|
|
|
|
|
#include "mode-private.h"
|
2015-11-25 08:26:38 +00:00
|
|
|
Mode run_mode =
|
2015-03-27 19:28:53 +00:00
|
|
|
{
|
2016-01-07 20:27:20 +00:00
|
|
|
.name = "run",
|
2016-01-12 21:17:53 +00:00
|
|
|
.cfg_name_key = "display-run",
|
2016-01-07 20:27:20 +00: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,
|
2021-06-01 11:14:45 +00:00
|
|
|
._get_message = run_get_message,
|
2016-01-07 20:27:20 +00:00
|
|
|
._get_display_value = _get_display_value,
|
2021-06-08 17:28:13 +00:00
|
|
|
._get_icon = _get_icon,
|
2016-01-07 20:27:20 +00:00
|
|
|
._get_completion = NULL,
|
2016-05-26 06:39:33 +00:00
|
|
|
._preprocess_input = NULL,
|
2016-01-07 20:27:20 +00:00
|
|
|
.private_data = NULL,
|
|
|
|
.free = NULL
|
2015-03-27 19:28:53 +00:00
|
|
|
};
|
2020-10-12 19:39:36 +00:00
|
|
|
/** @}*/
|