rofi/source/dialogs/drun.c

489 lines
15 KiB
C
Raw Normal View History

2015-11-03 07:31:38 +00:00
/**
* rofi
*
* MIT/X11 License
2015-12-31 23:27:00 +00:00
* Copyright 2013-2016 Qball Cow <qball@gmpclient.org>
2015-11-03 07:31:38 +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.
*
*/
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
2015-11-03 07:31:38 +00:00
#include <dirent.h>
#include <strings.h>
#include <string.h>
#include <errno.h>
#include "rofi.h"
2016-01-07 15:01:56 +00:00
#include "settings.h"
2015-11-03 07:31:38 +00:00
#include "helper.h"
#include "textbox.h"
2016-02-16 09:40:21 +00:00
#include "history.h"
2015-11-03 07:31:38 +00:00
#include "dialogs/drun.h"
#define DRUN_CACHE_FILE "rofi2.druncache"
2015-11-03 07:31:38 +00:00
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 );
2016-02-08 08:03:11 +00:00
rofi_view_error_dialog ( msg, FALSE );
2015-11-03 07:31:38 +00:00
g_free ( msg );
// print error.
g_error_free ( error );
retv = FALSE;
}
// Free the args list.
g_strfreev ( args );
return retv;
}
// execute sub-process
static void exec_cmd ( const char *cmd, int run_in_term )
{
if ( !cmd || !cmd[0] ) {
return;
}
execsh ( cmd, run_in_term );
}
2015-11-07 10:20:25 +00:00
/**
* Store extra information about the entry.
* Currently the executable and if it should run in terminal.
*/
typedef struct
{
/* ID */
char *id;
/* Root */
char *root;
2016-02-16 09:40:21 +00:00
/* Path to desktop file */
char *path;
/* Executable */
char *exec;
/* Name of the Entry */
char *name;
/* Generic Name */
char *generic_name;
/* Application needs to be launched in terminal. */
unsigned int terminal;
} DRunModeEntry;
typedef struct
{
DRunModeEntry *entry_list;
unsigned int cmd_list_length;
2016-02-16 09:40:21 +00:00
unsigned int history_length;
} DRunModePrivateData;
static void exec_cmd_entry ( DRunModeEntry *e )
{
// strip % arguments
gchar *str = g_strdup ( e->exec );
for ( ssize_t i = 0; str != NULL && str[i] != '\0'; i++ ) {
if ( str[i] == '%' ) {
while ( str[i] != ' ' && str[i] != '\0' ) {
str[i++] = ' ';
}
// We might have hit '\0' in prev. loop, break out of for then.
if ( str[i] == '\0' ) {
break;
}
}
}
gchar *fp = rofi_expand_path ( g_strstrip ( str ) );
2016-02-16 09:40:21 +00:00
if ( execsh ( fp, e->terminal ) ) {
char *path = g_build_filename ( cache_dir, DRUN_CACHE_FILE, NULL );
char *key = g_strdup_printf ( "%s:::%s", e->root, e->path );
history_set ( path, key );
g_free ( key );
2016-02-16 09:40:21 +00:00
g_free ( path );
}
g_free ( str );
g_free ( fp );
}
2016-02-16 09:40:21 +00:00
/**
* This function absorbs/freeś path, so this is no longer available afterwards.
*/
static void read_desktop_file ( DRunModePrivateData *pd, const char *root, char *path, const char *filename )
2016-02-16 09:40:21 +00:00
{
// Create ID.
char *id = g_strdup ( &( path[strlen ( root ) + 1] ) );
ssize_t id_len = strlen ( id );
for ( int index = 0; index < id_len; index++ ) {
if ( id[index] == '/' ) {
id[index] = '-';
}
}
for ( unsigned int index = 0; index < pd->cmd_list_length; index++ ) {
if ( g_strcmp0 ( id, pd->entry_list[index].id ) == 0 ) {
2016-02-16 09:40:21 +00:00
g_free ( path );
g_free ( id );
2016-02-16 09:40:21 +00:00
return;
}
}
GKeyFile *kf = g_key_file_new ();
GError *error = NULL;
g_key_file_load_from_file ( kf, path, 0, &error );
// If error, skip to next entry
if ( error != NULL ) {
g_error_free ( error );
g_key_file_free ( kf );
g_free ( path );
g_free ( id );
2016-02-16 09:40:21 +00:00
return;
}
// Skip hidden entries.
if ( g_key_file_has_key ( kf, "Desktop Entry", "Hidden", NULL ) ) {
if ( g_key_file_get_boolean ( kf, "Desktop Entry", "Hidden", NULL ) ) {
g_key_file_free ( kf );
g_free ( path );
g_free ( id );
2016-02-16 09:40:21 +00:00
return;
}
}
// Skip entries that have NoDisplay set.
if ( g_key_file_has_key ( kf, "Desktop Entry", "NoDisplay", NULL ) ) {
if ( g_key_file_get_boolean ( kf, "Desktop Entry", "NoDisplay", NULL ) ) {
g_key_file_free ( kf );
g_free ( path );
g_free ( id );
2016-02-16 09:40:21 +00:00
return;
}
}
// Name key is required.
if ( !g_key_file_has_key ( kf, "Desktop Entry", "Name", NULL ) ) {
fprintf(stderr, "Invalid DesktopFile: '%s', no 'Name' key present.\n", path);
g_key_file_free ( kf );
g_free ( path );
g_free ( id );
return;
}
2016-02-16 09:40:21 +00:00
if ( g_key_file_has_key ( kf, "Desktop Entry", "Exec", NULL ) ) {
size_t nl = ( ( pd->cmd_list_length ) + 1 );
pd->entry_list = g_realloc ( pd->entry_list, nl * sizeof ( *( pd->entry_list ) ) );
pd->entry_list[pd->cmd_list_length].id = id;
pd->entry_list[pd->cmd_list_length].root = g_strdup ( root );
2016-02-16 09:40:21 +00:00
pd->entry_list[pd->cmd_list_length].path = path;
pd->entry_list[pd->cmd_list_length].terminal = FALSE;
{
2016-02-16 09:40:21 +00:00
gchar *n = g_key_file_get_locale_string ( kf, "Desktop Entry", "Name", NULL, NULL );
pd->entry_list[pd->cmd_list_length].name = n;
gchar *gn = g_key_file_get_locale_string ( kf, "Desktop Entry", "GenericName", NULL, NULL );
2016-02-16 09:40:21 +00:00
pd->entry_list[pd->cmd_list_length].generic_name = gn;
}
pd->entry_list[pd->cmd_list_length].exec = g_key_file_get_string ( kf, "Desktop Entry", "Exec", NULL );
if ( g_key_file_has_key ( kf, "Desktop Entry", "Terminal", NULL ) ) {
pd->entry_list[pd->cmd_list_length].terminal = g_key_file_get_boolean ( kf, "Desktop Entry", "Terminal", NULL );
}
( pd->cmd_list_length )++;
}
else {
g_free ( path );
g_free ( id );
}
2016-02-16 09:40:21 +00:00
g_key_file_free ( kf );
}
2015-11-03 07:31:38 +00:00
/**
* Internal spider used to get list of executables.
*/
static void walk_dir ( DRunModePrivateData *pd, const char *root, const char *dirname )
2015-11-03 07:31:38 +00:00
{
DIR *dir;
2015-11-03 07:31:38 +00:00
dir = opendir ( dirname );
if ( dir == NULL ) {
return;
}
2015-11-03 07:31:38 +00:00
struct dirent *file;
gchar *filename = NULL;
struct stat st;
while ( ( file = readdir ( dir ) ) != NULL ) {
if ( file->d_name[0] == '.' ) {
continue;
}
switch ( file->d_type )
{
case DT_LNK:
case DT_REG:
case DT_DIR:
case DT_UNKNOWN:
filename = g_build_filename ( dirname, file->d_name, NULL );
break;
default:
continue;
}
// On a link, or if FS does not support providing this information
// Fallback to stat method.
if ( file->d_type == DT_LNK || file->d_type == DT_UNKNOWN ) {
file->d_type = DT_UNKNOWN;
if ( stat ( filename, &st ) == 0 ) {
if ( S_ISDIR ( st.st_mode ) ) {
file->d_type = DT_DIR;
}
else if ( S_ISREG ( st.st_mode ) ) {
file->d_type = DT_REG;
}
}
2015-11-03 07:31:38 +00:00
}
switch ( file->d_type )
{
case DT_REG:
read_desktop_file ( pd, root, filename, file->d_name );
filename = NULL;
break;
case DT_DIR:
walk_dir ( pd, root, filename );
break;
default:
break;
}
g_free ( filename );
2015-11-03 07:31:38 +00:00
}
closedir ( dir );
2015-11-03 07:31:38 +00:00
}
2016-02-16 09:40:21 +00:00
/**
2016-06-01 05:34:41 +00:00
* @param entry The command entry to remove from history
2016-02-16 09:40:21 +00:00
*
* Remove command from history.
*/
static void delete_entry_history ( const DRunModeEntry *entry )
{
char *path = g_build_filename ( cache_dir, DRUN_CACHE_FILE, NULL );
history_remove ( path, entry->path );
2015-11-03 07:31:38 +00:00
2016-02-16 09:40:21 +00:00
g_free ( path );
}
2016-02-16 09:40:21 +00:00
static void get_apps_history ( DRunModePrivateData *pd )
{
unsigned int length = 0;
gchar *path = g_build_filename ( cache_dir, DRUN_CACHE_FILE, NULL );
gchar **retv = history_get_list ( path, &length );
g_free ( path );
for ( unsigned int index = 0; index < length; index++ ) {
char **st = g_strsplit ( retv[index], ":::", 2 );
if ( st && st[0] && st[1] ) {
gchar *name = g_path_get_basename ( st[1] );
printf ( ": %s\n", st[0] );
read_desktop_file ( pd, st[0], g_strdup ( st[1] ), name );
g_free ( name );
}
g_strfreev ( st );
2016-02-16 09:40:21 +00:00
}
g_free ( retv );
pd->history_length = pd->cmd_list_length;
}
static void get_apps ( DRunModePrivateData *pd )
2015-11-03 07:31:38 +00:00
{
2016-02-16 09:40:21 +00:00
get_apps_history ( pd );
gchar *dir;
// First read the user directory.
dir = g_build_filename ( g_get_user_data_dir (), "applications", NULL );
walk_dir ( pd, dir, dir );
g_free ( dir );
// Then read thee system data dirs.
const gchar * const * sys = g_get_system_data_dirs ();
for (; *sys != NULL; ++sys ) {
dir = g_build_filename ( *sys, "applications", NULL );
walk_dir ( pd, dir, dir );
g_free ( dir );
}
}
2015-11-03 07:31:38 +00:00
static int drun_mode_init ( Mode *sw )
2015-11-03 07:31:38 +00:00
{
2016-01-07 20:27:20 +00:00
if ( mode_get_private_data ( sw ) == NULL ) {
2015-11-03 07:31:38 +00:00
DRunModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
2016-01-07 20:27:20 +00:00
mode_set_private_data ( sw, (void *) pd );
get_apps ( pd );
2015-11-03 07:31:38 +00:00
}
return TRUE;
2015-11-03 07:31:38 +00:00
}
2016-02-16 09:40:21 +00:00
static void drun_entry_clear ( DRunModeEntry *e )
{
g_free ( e->root );
2016-02-16 09:40:21 +00:00
g_free ( e->path );
g_free ( e->exec );
g_free ( e->name );
g_free ( e->generic_name );
}
2015-11-03 07:31:38 +00:00
static ModeMode drun_mode_result ( Mode *sw, int mretv, char **input, unsigned int selected_line )
2015-11-03 07:31:38 +00:00
{
2016-01-07 20:27:20 +00:00
DRunModePrivateData *rmpd = (DRunModePrivateData *) mode_get_private_data ( sw );
ModeMode retv = MODE_EXIT;
2015-11-03 07:31:38 +00:00
gboolean run_in_term = ( ( mretv & MENU_CUSTOM_ACTION ) == MENU_CUSTOM_ACTION );
2015-11-03 07:31:38 +00:00
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 ) ) {
exec_cmd_entry ( &( rmpd->entry_list[selected_line] ) );
2015-11-03 07:31:38 +00:00
}
else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) {
exec_cmd ( *input, run_in_term );
2015-11-03 07:31:38 +00:00
}
2016-02-16 09:40:21 +00:00
else if ( ( mretv & MENU_ENTRY_DELETE ) && selected_line < rmpd->cmd_list_length ) {
if ( selected_line < rmpd->history_length ) {
delete_entry_history ( &( rmpd->entry_list[selected_line] ) );
drun_entry_clear ( &( rmpd->entry_list[selected_line] ) );
memmove ( &( rmpd->entry_list[selected_line] ), &rmpd->entry_list[selected_line + 1],
sizeof ( DRunModeEntry ) * ( rmpd->cmd_list_length - selected_line - 1 ) );
rmpd->cmd_list_length--;
}
retv = RELOAD_DIALOG;
}
2015-11-03 07:31:38 +00:00
return retv;
}
static void drun_mode_destroy ( Mode *sw )
2015-11-03 07:31:38 +00:00
{
2016-01-07 20:27:20 +00:00
DRunModePrivateData *rmpd = (DRunModePrivateData *) mode_get_private_data ( sw );
2015-11-03 07:31:38 +00:00
if ( rmpd != NULL ) {
for ( size_t i = 0; i < rmpd->cmd_list_length; i++ ) {
2016-02-16 09:40:21 +00:00
drun_entry_clear ( &( rmpd->entry_list[i] ) );
}
g_free ( rmpd->entry_list );
2015-11-03 07:31:38 +00:00
g_free ( rmpd );
2016-01-07 20:27:20 +00:00
mode_set_private_data ( sw, NULL );
2015-11-03 07:31:38 +00:00
}
}
2016-01-07 20:27:20 +00:00
static char *_get_display_value ( const Mode *sw, unsigned int selected_line, int *state, int get_entry )
2015-11-03 07:31:38 +00:00
{
2016-01-07 20:27:20 +00:00
DRunModePrivateData *pd = (DRunModePrivateData *) mode_get_private_data ( sw );
*state |= MARKUP;
if ( !get_entry ) {
return NULL;
}
if ( pd->entry_list == NULL ) {
// Should never get here.
return g_strdup ( "Failed" );
}
/* Free temp storage. */
DRunModeEntry *dr = &( pd->entry_list[selected_line] );
if ( dr->generic_name == NULL ) {
return g_markup_escape_text ( dr->name, -1 );
}
else {
return g_markup_printf_escaped ( "%s <span weight='light' size='small'><i>(%s)</i></span>", dr->name,
dr->generic_name );
}
}
static char *drun_get_completion ( const Mode *sw, unsigned int index )
{
2016-01-07 20:27:20 +00:00
DRunModePrivateData *pd = (DRunModePrivateData *) mode_get_private_data ( sw );
/* Free temp storage. */
DRunModeEntry *dr = &( pd->entry_list[index] );
if ( dr->generic_name == NULL ) {
return g_strdup ( dr->name );
}
else {
return g_strdup_printf ( "%s", dr->name );
}
}
static int drun_token_match ( const Mode *data, GRegex **tokens, unsigned int index )
{
2016-01-07 20:27:20 +00:00
DRunModePrivateData *rmpd = (DRunModePrivateData *) mode_get_private_data ( data );
2015-12-14 19:28:25 +00:00
int match = 1;
if ( tokens ) {
for ( int j = 0; match && tokens != NULL && tokens[j] != NULL; j++ ) {
int test = 0;
2016-05-22 15:47:34 +00:00
GRegex *ftokens[2] = { tokens[j], NULL };
if ( !test && rmpd->entry_list[index].name &&
2016-05-22 15:47:34 +00:00
token_match ( ftokens, rmpd->entry_list[index].name ) ) {
2015-12-14 19:28:25 +00:00
test = 1;
}
if ( !test && rmpd->entry_list[index].generic_name &&
token_match ( ftokens, rmpd->entry_list[index].generic_name ) ) {
2015-12-14 19:28:25 +00:00
test = 1;
}
if ( !test && token_match ( ftokens, rmpd->entry_list[index].exec ) ) {
test = 1;
}
if ( test == 0 ) {
match = 0;
}
}
}
return match;
}
static unsigned int drun_mode_get_num_entries ( const Mode *sw )
{
2016-01-07 20:27:20 +00:00
const DRunModePrivateData *pd = (const DRunModePrivateData *) mode_get_private_data ( sw );
return pd->cmd_list_length;
}
2016-01-07 20:27:20 +00:00
#include "mode-private.h"
Mode drun_mode =
2015-11-03 07:31:38 +00:00
{
2016-01-07 20:27:20 +00:00
.name = "drun",
.cfg_name_key = "display-drun",
2016-01-07 20:27:20 +00:00
._init = drun_mode_init,
._get_num_entries = drun_mode_get_num_entries,
._result = drun_mode_result,
._destroy = drun_mode_destroy,
._token_match = drun_token_match,
._get_completion = drun_get_completion,
._get_display_value = _get_display_value,
._preprocess_input = NULL,
2016-01-07 20:27:20 +00:00
.private_data = NULL,
.free = NULL
2015-11-03 07:31:38 +00:00
};