2017-04-15 10:32:05 +00:00
|
|
|
/*
|
2014-07-21 19:39:24 +00:00
|
|
|
* rofi
|
|
|
|
*
|
|
|
|
* MIT/X11 License
|
2020-01-01 11:23:12 +00:00
|
|
|
* Copyright © 2013-2020 Qball Cow <qball@gmpclient.org>
|
2014-07-21 19:39:24 +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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-06-12 09:45:42 +00:00
|
|
|
/** The log domain of this dialog. */
|
2017-04-27 20:59:14 +00:00
|
|
|
#define G_LOG_DOMAIN "Dialogs.Script"
|
2017-04-15 09:58:49 +00:00
|
|
|
|
2014-08-22 07:26:46 +00:00
|
|
|
#include <config.h>
|
2014-07-21 19:39:24 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <assert.h>
|
2015-07-31 08:21:32 +00:00
|
|
|
#include <errno.h>
|
2014-07-21 19:39:24 +00:00
|
|
|
#include "rofi.h"
|
2015-03-25 07:36:19 +00:00
|
|
|
#include "dialogs/script.h"
|
2014-09-03 11:07:26 +00:00
|
|
|
#include "helper.h"
|
2017-05-22 07:24:05 +00:00
|
|
|
|
2017-09-27 18:00:33 +00:00
|
|
|
#include "widgets/textbox.h"
|
|
|
|
|
2017-05-23 15:19:04 +00:00
|
|
|
#include "mode-private.h"
|
2017-09-27 18:00:33 +00:00
|
|
|
|
2018-09-09 15:53:56 +00:00
|
|
|
#include "rofi-icon-fetcher.h"
|
|
|
|
|
2019-04-02 11:51:53 +00:00
|
|
|
#include "dialogs/dmenuscriptshared.h"
|
2018-09-09 15:53:56 +00:00
|
|
|
|
2017-09-27 18:00:33 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
/** ID of the current script. */
|
2017-10-05 15:45:50 +00:00
|
|
|
unsigned int id;
|
2017-09-27 18:00:33 +00:00
|
|
|
/** List of visible items. */
|
2020-02-02 12:56:37 +00:00
|
|
|
DmenuScriptEntry *cmd_list;
|
2017-09-27 18:00:33 +00:00
|
|
|
/** length list of visible items. */
|
2017-10-05 15:45:50 +00:00
|
|
|
unsigned int cmd_list_length;
|
2017-09-27 18:00:33 +00:00
|
|
|
|
|
|
|
/** Urgent list */
|
|
|
|
struct rofi_range_pair * urgent_list;
|
2017-10-05 15:45:50 +00:00
|
|
|
unsigned int num_urgent_list;
|
2017-09-27 18:00:33 +00:00
|
|
|
/** Active list */
|
|
|
|
struct rofi_range_pair * active_list;
|
2017-10-05 15:45:50 +00:00
|
|
|
unsigned int num_active_list;
|
2017-09-27 18:00:33 +00:00
|
|
|
/** Configuration settings. */
|
2017-10-05 15:45:50 +00:00
|
|
|
char *message;
|
|
|
|
char *prompt;
|
|
|
|
gboolean do_markup;
|
2020-04-01 11:58:01 +00:00
|
|
|
char delim;
|
2020-04-22 21:41:25 +00:00
|
|
|
/** no custom */
|
|
|
|
gboolean no_custom;
|
2017-09-27 18:00:33 +00:00
|
|
|
} ScriptModePrivateData;
|
|
|
|
|
2019-04-02 11:51:53 +00:00
|
|
|
/**
|
|
|
|
* Shared function between DMENU and Script mode.
|
|
|
|
*/
|
|
|
|
void dmenuscript_parse_entry_extras ( G_GNUC_UNUSED Mode *sw, DmenuScriptEntry *entry, char *buffer, size_t length )
|
2018-09-09 15:53:56 +00:00
|
|
|
{
|
2020-02-02 12:56:37 +00:00
|
|
|
size_t length_key = 0; //strlen ( line );
|
2020-04-06 21:26:07 +00:00
|
|
|
while ( length_key < length && buffer[length_key] != '\x1f' ) {
|
2018-09-09 15:53:56 +00:00
|
|
|
length_key++;
|
|
|
|
}
|
2020-04-06 21:26:07 +00:00
|
|
|
// Should be not last character in buffer.
|
2020-04-07 11:14:53 +00:00
|
|
|
if ( (length_key+1) < (length) ) {
|
2018-09-09 15:53:56 +00:00
|
|
|
buffer[length_key] = '\0';
|
|
|
|
char *value = buffer + length_key + 1;
|
2020-02-02 12:56:37 +00:00
|
|
|
if ( strcasecmp ( buffer, "icon" ) == 0 ) {
|
|
|
|
entry->icon_name = g_strdup ( value );
|
2018-09-09 15:53:56 +00:00
|
|
|
}
|
2020-02-02 13:21:29 +00:00
|
|
|
else if ( strcasecmp ( buffer, "meta" ) == 0 ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
entry->meta = g_strdup ( value );
|
2020-02-01 12:49:11 +00:00
|
|
|
}
|
2020-02-02 13:21:29 +00:00
|
|
|
else if ( strcasecmp ( buffer, "nonselectable" ) == 0 ) {
|
|
|
|
entry->nonselectable = strcasecmp ( value, "true" ) == 0;
|
|
|
|
}
|
2018-09-09 15:53:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 11:51:53 +00:00
|
|
|
/**
|
|
|
|
* End of shared functions.
|
|
|
|
*/
|
|
|
|
|
2017-09-27 18:00:33 +00:00
|
|
|
static void parse_header_entry ( Mode *sw, char *line, ssize_t length )
|
|
|
|
{
|
2017-10-05 15:45:50 +00:00
|
|
|
ScriptModePrivateData *pd = (ScriptModePrivateData *) sw->private_data;
|
|
|
|
ssize_t length_key = 0;//strlen ( line );
|
2020-04-07 11:14:53 +00:00
|
|
|
while ( length_key < length && line[length_key] != '\x1f' ) {
|
2017-09-27 18:00:33 +00:00
|
|
|
length_key++;
|
|
|
|
}
|
|
|
|
|
2020-04-07 11:20:16 +00:00
|
|
|
if ( (length_key+1) < length ) {
|
2017-09-27 18:00:33 +00:00
|
|
|
line[length_key] = '\0';
|
2017-10-05 15:45:50 +00:00
|
|
|
char *value = line + length_key + 1;
|
2017-09-27 18:00:33 +00:00
|
|
|
if ( strcasecmp ( line, "message" ) == 0 ) {
|
|
|
|
g_free ( pd->message );
|
2018-08-08 13:55:13 +00:00
|
|
|
pd->message = strlen ( value ) ? g_strdup ( value ) : NULL;
|
2017-10-05 15:45:50 +00:00
|
|
|
}
|
|
|
|
else if ( strcasecmp ( line, "prompt" ) == 0 ) {
|
2017-09-27 18:00:33 +00:00
|
|
|
g_free ( pd->prompt );
|
2017-10-05 15:45:50 +00:00
|
|
|
pd->prompt = g_strdup ( value );
|
2017-09-27 18:00:33 +00:00
|
|
|
sw->display_name = pd->prompt;
|
2017-10-05 15:45:50 +00:00
|
|
|
}
|
|
|
|
else if ( strcasecmp ( line, "markup-rows" ) == 0 ) {
|
2017-09-27 18:00:33 +00:00
|
|
|
pd->do_markup = ( strcasecmp ( value, "true" ) == 0 );
|
2017-10-05 15:45:50 +00:00
|
|
|
}
|
|
|
|
else if ( strcasecmp ( line, "urgent" ) == 0 ) {
|
2017-09-27 18:00:33 +00:00
|
|
|
parse_ranges ( value, &( pd->urgent_list ), &( pd->num_urgent_list ) );
|
2017-10-05 15:45:50 +00:00
|
|
|
}
|
|
|
|
else if ( strcasecmp ( line, "active" ) == 0 ) {
|
2017-09-27 18:00:33 +00:00
|
|
|
parse_ranges ( value, &( pd->active_list ), &( pd->num_active_list ) );
|
|
|
|
}
|
2020-04-01 11:58:01 +00:00
|
|
|
else if ( strcasecmp ( line, "delim" ) == 0 ) {
|
|
|
|
pd->delim = helper_parse_char ( value );
|
2020-04-22 21:41:25 +00:00
|
|
|
} else if ( strcasecmp ( line, "no-custom" ) == 0 ) {
|
|
|
|
pd->no_custom = ( strcasecmp ( value, "true") == 0 );
|
2020-04-01 11:58:01 +00:00
|
|
|
}
|
2017-09-27 18:00:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 10:55:00 +00:00
|
|
|
static DmenuScriptEntry *execute_executor ( Mode *sw, char *arg, unsigned int *length, int value )
|
2014-07-21 19:39:24 +00:00
|
|
|
{
|
2020-04-01 11:58:01 +00:00
|
|
|
ScriptModePrivateData *pd = (ScriptModePrivateData *) sw->private_data;
|
2020-02-02 12:56:37 +00:00
|
|
|
int fd = -1;
|
|
|
|
GError *error = NULL;
|
|
|
|
DmenuScriptEntry *retv = NULL;
|
|
|
|
char **argv = NULL;
|
|
|
|
int argc = 0;
|
2014-07-21 19:39:24 +00:00
|
|
|
*length = 0;
|
2020-04-05 10:55:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Environment
|
|
|
|
char ** env = g_get_environ ();
|
|
|
|
|
|
|
|
char *str_value = g_strdup_printf("%d", value);
|
|
|
|
env = g_environ_setenv ( env, "ROFI_RETV", str_value, TRUE);
|
|
|
|
g_free ( str_value );
|
|
|
|
|
|
|
|
|
|
|
|
if ( g_shell_parse_argv ( sw->ed, &argc, &argv, &error ) ) {
|
2017-09-05 11:52:44 +00:00
|
|
|
argv = g_realloc ( argv, ( argc + 2 ) * sizeof ( char* ) );
|
|
|
|
argv[argc] = g_strdup ( arg );
|
|
|
|
argv[argc + 1] = NULL;
|
2020-04-05 10:55:00 +00:00
|
|
|
g_spawn_async_with_pipes ( NULL, argv, env, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL, &fd, NULL, &error );
|
2017-08-14 19:33:12 +00:00
|
|
|
}
|
2020-04-05 10:55:00 +00:00
|
|
|
g_strfreev ( env );
|
2017-08-14 15:28:25 +00:00
|
|
|
if ( error != NULL ) {
|
2020-04-05 10:55:00 +00:00
|
|
|
char *msg = g_strdup_printf ( "Failed to execute: '%s'\nError: '%s'", (char*)sw->ed, error->message );
|
2017-08-14 15:28:25 +00:00
|
|
|
rofi_view_error_dialog ( msg, FALSE );
|
|
|
|
g_free ( msg );
|
|
|
|
// print error.
|
|
|
|
g_error_free ( error );
|
|
|
|
fd = -1;
|
|
|
|
}
|
2014-08-25 18:02:48 +00:00
|
|
|
if ( fd >= 0 ) {
|
|
|
|
FILE *inp = fdopen ( fd, "r" );
|
|
|
|
if ( inp ) {
|
2017-10-05 15:45:50 +00:00
|
|
|
char *buffer = NULL;
|
|
|
|
size_t buffer_length = 0;
|
|
|
|
ssize_t read_length = 0;
|
2018-08-08 13:55:13 +00:00
|
|
|
size_t actual_size = 0;
|
2020-04-01 11:58:01 +00:00
|
|
|
while ( ( read_length = getdelim ( &buffer, &buffer_length, pd->delim, inp ) ) > 0 ) {
|
2017-05-23 15:19:04 +00:00
|
|
|
// Filter out line-end.
|
2020-04-01 11:58:01 +00:00
|
|
|
if ( buffer[read_length - 1] == pd->delim ) {
|
2017-10-05 15:45:50 +00:00
|
|
|
buffer[read_length - 1] = '\0';
|
2017-09-27 18:00:33 +00:00
|
|
|
}
|
|
|
|
if ( buffer[0] == '\0' ) {
|
2017-10-05 15:45:50 +00:00
|
|
|
parse_header_entry ( sw, &buffer[1], read_length - 1 );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ( actual_size < ( ( *length ) + 2 ) ) {
|
2017-09-27 18:00:33 +00:00
|
|
|
actual_size += 256;
|
2019-04-02 11:51:53 +00:00
|
|
|
retv = g_realloc ( retv, ( actual_size ) * sizeof ( DmenuScriptEntry ) );
|
2017-09-27 18:00:33 +00:00
|
|
|
}
|
2020-02-02 12:56:37 +00:00
|
|
|
size_t buf_length = strlen ( buffer ) + 1;
|
|
|
|
retv[( *length )].entry = g_memdup ( buffer, buf_length );
|
|
|
|
retv[( *length )].icon_name = NULL;
|
|
|
|
retv[( *length )].meta = NULL;
|
|
|
|
retv[( *length )].icon_fetch_uid = 0;
|
2020-02-13 15:52:11 +00:00
|
|
|
retv[( *length )].nonselectable = FALSE;
|
2020-02-02 12:56:37 +00:00
|
|
|
if ( buf_length > 0 && ( read_length > (ssize_t) buf_length ) ) {
|
|
|
|
dmenuscript_parse_entry_extras ( sw, &( retv[( *length )] ), buffer + buf_length, read_length - buf_length );
|
2018-09-09 15:53:56 +00:00
|
|
|
}
|
|
|
|
retv[( *length ) + 1].entry = NULL;
|
2017-09-27 18:00:33 +00:00
|
|
|
( *length )++;
|
2017-05-23 15:19:04 +00:00
|
|
|
}
|
2014-08-25 18:02:48 +00:00
|
|
|
}
|
2016-04-10 12:30:13 +00:00
|
|
|
if ( buffer ) {
|
|
|
|
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
|
|
|
}
|
2014-07-21 19:39:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-05 09:48:03 +00:00
|
|
|
g_strfreev ( argv );
|
2014-07-21 19:39:24 +00:00
|
|
|
return retv;
|
|
|
|
}
|
|
|
|
|
2015-11-25 08:26:38 +00:00
|
|
|
static void script_switcher_free ( Mode *sw )
|
2014-07-21 19:39:24 +00:00
|
|
|
{
|
2015-03-27 19:28:53 +00:00
|
|
|
if ( sw == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-04-21 07:37:03 +00:00
|
|
|
g_free ( sw->name );
|
2015-03-27 19:28:53 +00:00
|
|
|
g_free ( sw->ed );
|
|
|
|
g_free ( sw );
|
|
|
|
}
|
2014-07-21 19:39:24 +00:00
|
|
|
|
2016-01-08 08:16:59 +00:00
|
|
|
static int script_mode_init ( Mode *sw )
|
2015-03-27 19:28:53 +00:00
|
|
|
{
|
|
|
|
if ( sw->private_data == NULL ) {
|
|
|
|
ScriptModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
|
2020-04-01 11:58:01 +00:00
|
|
|
pd->delim = '\n';
|
2015-03-27 19:28:53 +00:00
|
|
|
sw->private_data = (void *) pd;
|
2020-04-05 10:55:00 +00:00
|
|
|
pd->cmd_list = execute_executor ( sw, NULL, &( pd->cmd_list_length ), 0 );
|
2015-03-27 19:28:53 +00:00
|
|
|
}
|
2016-01-08 08:16:59 +00:00
|
|
|
return TRUE;
|
2015-03-27 19:28:53 +00:00
|
|
|
}
|
2015-11-25 08:26:38 +00:00
|
|
|
static unsigned int script_mode_get_num_entries ( const Mode *sw )
|
2015-03-27 19:28:53 +00:00
|
|
|
{
|
2015-11-23 20:52:55 +00:00
|
|
|
const ScriptModePrivateData *rmpd = (const ScriptModePrivateData *) sw->private_data;
|
2015-11-21 22:59:59 +00:00
|
|
|
return rmpd->cmd_list_length;
|
2014-07-21 19:39:24 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 16:56:31 +00:00
|
|
|
static void script_mode_reset_highlight ( Mode *sw )
|
|
|
|
{
|
2018-08-08 13:55:13 +00:00
|
|
|
ScriptModePrivateData *rmpd = (ScriptModePrivateData *) sw->private_data;
|
2018-02-08 16:56:31 +00:00
|
|
|
|
|
|
|
rmpd->num_urgent_list = 0;
|
|
|
|
g_free ( rmpd->urgent_list );
|
2018-08-08 13:55:13 +00:00
|
|
|
rmpd->urgent_list = NULL;
|
2018-02-08 16:56:31 +00:00
|
|
|
rmpd->num_active_list = 0;
|
|
|
|
g_free ( rmpd->active_list );
|
|
|
|
rmpd->active_list = NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-25 08:26:38 +00:00
|
|
|
static ModeMode script_mode_result ( Mode *sw, int mretv, char **input, unsigned int selected_line )
|
2014-07-21 19:39:24 +00:00
|
|
|
{
|
2015-03-27 19:28:53 +00:00
|
|
|
ScriptModePrivateData *rmpd = (ScriptModePrivateData *) sw->private_data;
|
2015-11-25 08:26:38 +00:00
|
|
|
ModeMode retv = MODE_EXIT;
|
2020-02-02 12:56:37 +00:00
|
|
|
DmenuScriptEntry *new_list = NULL;
|
2015-03-27 19:28:53 +00:00
|
|
|
unsigned int new_length = 0;
|
|
|
|
|
|
|
|
if ( ( mretv & MENU_NEXT ) ) {
|
2020-02-13 20:29:02 +00:00
|
|
|
retv = NEXT_DIALOG;
|
2014-07-21 19:39:24 +00:00
|
|
|
}
|
2015-03-27 19:28:53 +00:00
|
|
|
else if ( ( mretv & MENU_PREVIOUS ) ) {
|
|
|
|
retv = PREVIOUS_DIALOG;
|
|
|
|
}
|
|
|
|
else if ( ( mretv & MENU_QUICK_SWITCH ) ) {
|
2020-04-05 10:55:00 +00:00
|
|
|
//retv = 1+( mretv & MENU_LOWER_MASK );
|
|
|
|
script_mode_reset_highlight ( sw );
|
2020-04-05 11:19:22 +00:00
|
|
|
if ( selected_line != UINT32_MAX ) {
|
|
|
|
new_list = execute_executor ( sw, rmpd->cmd_list[selected_line].entry, &new_length,10+( mretv & MENU_LOWER_MASK ) );
|
|
|
|
} else {
|
2020-04-22 21:41:25 +00:00
|
|
|
if ( rmpd->no_custom == FALSE ) {
|
|
|
|
new_list = execute_executor ( sw, *input, &new_length,10+( mretv & MENU_LOWER_MASK ) );
|
|
|
|
} else {
|
|
|
|
return RELOAD_DIALOG;
|
|
|
|
}
|
2020-04-05 11:19:22 +00:00
|
|
|
}
|
2015-03-27 19:28:53 +00:00
|
|
|
}
|
2018-09-09 15:53:56 +00:00
|
|
|
else if ( ( mretv & MENU_OK ) && rmpd->cmd_list[selected_line].entry != NULL ) {
|
2020-02-02 13:21:29 +00:00
|
|
|
if ( rmpd->cmd_list[selected_line].nonselectable ) {
|
2020-02-13 20:32:19 +00:00
|
|
|
return RELOAD_DIALOG;
|
2020-02-02 13:21:29 +00:00
|
|
|
}
|
2018-02-08 16:56:31 +00:00
|
|
|
script_mode_reset_highlight ( sw );
|
2020-04-05 10:55:00 +00:00
|
|
|
new_list = execute_executor ( sw, rmpd->cmd_list[selected_line].entry, &new_length, 1 );
|
2015-03-27 19:28:53 +00:00
|
|
|
}
|
|
|
|
else if ( ( mretv & MENU_CUSTOM_INPUT ) && *input != NULL && *input[0] != '\0' ) {
|
2020-04-22 21:41:25 +00:00
|
|
|
if ( rmpd->no_custom == FALSE ) {
|
|
|
|
script_mode_reset_highlight ( sw );
|
|
|
|
new_list = execute_executor ( sw, *input, &new_length, 2 );
|
|
|
|
} else {
|
|
|
|
return RELOAD_DIALOG;
|
|
|
|
}
|
2015-03-27 19:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If a new list was generated, use that an loop around.
|
|
|
|
if ( new_list != NULL ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
for ( unsigned int i = 0; i < rmpd->cmd_list_length; i++ ) {
|
2018-09-09 15:53:56 +00:00
|
|
|
g_free ( rmpd->cmd_list[i].entry );
|
|
|
|
g_free ( rmpd->cmd_list[i].icon_name );
|
2020-02-01 12:49:11 +00:00
|
|
|
g_free ( rmpd->cmd_list[i].meta );
|
2018-09-09 15:53:56 +00:00
|
|
|
}
|
|
|
|
g_free ( rmpd->cmd_list );
|
2015-03-27 19:28:53 +00:00
|
|
|
|
|
|
|
rmpd->cmd_list = new_list;
|
|
|
|
rmpd->cmd_list_length = new_length;
|
2017-01-08 20:36:06 +00:00
|
|
|
retv = RESET_DIALOG;
|
2015-03-27 19:28:53 +00:00
|
|
|
}
|
|
|
|
return retv;
|
2014-07-21 19:39:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-25 08:26:38 +00:00
|
|
|
static void script_mode_destroy ( Mode *sw )
|
2015-03-27 19:28:53 +00:00
|
|
|
{
|
|
|
|
ScriptModePrivateData *rmpd = (ScriptModePrivateData *) sw->private_data;
|
|
|
|
if ( rmpd != NULL ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
for ( unsigned int i = 0; i < rmpd->cmd_list_length; i++ ) {
|
2018-09-09 15:53:56 +00:00
|
|
|
g_free ( rmpd->cmd_list[i].entry );
|
|
|
|
g_free ( rmpd->cmd_list[i].icon_name );
|
2020-02-01 12:49:11 +00:00
|
|
|
g_free ( rmpd->cmd_list[i].meta );
|
2018-09-09 15:53:56 +00:00
|
|
|
}
|
|
|
|
g_free ( rmpd->cmd_list );
|
2017-09-27 18:00:33 +00:00
|
|
|
g_free ( rmpd->message );
|
|
|
|
g_free ( rmpd->prompt );
|
|
|
|
g_free ( rmpd->urgent_list );
|
|
|
|
g_free ( rmpd->active_list );
|
2015-03-27 19:28:53 +00:00
|
|
|
g_free ( rmpd );
|
|
|
|
sw->private_data = NULL;
|
|
|
|
}
|
|
|
|
}
|
2019-07-09 07:01:35 +00:00
|
|
|
static inline unsigned int get_index ( unsigned int length, int index )
|
2019-07-06 15:04:17 +00:00
|
|
|
{
|
2020-02-02 12:56:37 +00:00
|
|
|
if ( index >= 0 ) {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
if ( ( (unsigned int) -index ) <= length ) {
|
|
|
|
return length + index;
|
|
|
|
}
|
2019-07-06 15:04:17 +00:00
|
|
|
// Out of range.
|
|
|
|
return UINT_MAX;
|
|
|
|
}
|
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
|
|
|
{
|
2017-09-27 18:00:33 +00:00
|
|
|
ScriptModePrivateData *pd = sw->private_data;
|
|
|
|
for ( unsigned int i = 0; i < pd->num_active_list; i++ ) {
|
2019-07-06 15:04:17 +00:00
|
|
|
unsigned int start = get_index ( pd->cmd_list_length, pd->active_list[i].start );
|
|
|
|
unsigned int stop = get_index ( pd->cmd_list_length, pd->active_list[i].stop );
|
2019-07-02 19:27:46 +00:00
|
|
|
if ( selected_line >= start && selected_line <= stop ) {
|
2017-09-27 18:00:33 +00:00
|
|
|
*state |= ACTIVE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for ( unsigned int i = 0; i < pd->num_urgent_list; i++ ) {
|
2019-07-06 15:04:17 +00:00
|
|
|
unsigned int start = get_index ( pd->cmd_list_length, pd->urgent_list[i].start );
|
|
|
|
unsigned int stop = get_index ( pd->cmd_list_length, pd->urgent_list[i].stop );
|
2019-07-02 19:27:46 +00:00
|
|
|
if ( selected_line >= start && selected_line <= stop ) {
|
2017-09-27 18:00:33 +00:00
|
|
|
*state |= URGENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( pd->do_markup ) {
|
|
|
|
*state |= MARKUP;
|
|
|
|
}
|
2018-09-09 15:53:56 +00:00
|
|
|
return get_entry ? g_strdup ( pd->cmd_list[selected_line].entry ) : NULL;
|
2015-11-21 22:59:59 +00:00
|
|
|
}
|
|
|
|
|
2017-09-29 06:32:09 +00:00
|
|
|
static int script_token_match ( const Mode *sw, rofi_int_matcher **tokens, unsigned int index )
|
2015-11-21 22:59:59 +00:00
|
|
|
{
|
|
|
|
ScriptModePrivateData *rmpd = sw->private_data;
|
2020-02-02 12:56:37 +00:00
|
|
|
int match = 1;
|
2020-02-01 12:49:11 +00:00
|
|
|
if ( tokens ) {
|
2020-02-02 12:56:37 +00:00
|
|
|
for ( int j = 0; match && tokens != NULL && tokens[j] != NULL; j++ ) {
|
|
|
|
rofi_int_matcher *ftokens[2] = { tokens[j], NULL };
|
|
|
|
int test = 0;
|
|
|
|
test = helper_token_match ( ftokens, rmpd->cmd_list[index].entry );
|
|
|
|
if ( test == tokens[j]->invert && rmpd->cmd_list[index].meta ) {
|
|
|
|
test = helper_token_match ( ftokens, rmpd->cmd_list[index].meta );
|
|
|
|
}
|
2020-02-01 12:49:11 +00:00
|
|
|
|
2020-02-02 12:56:37 +00:00
|
|
|
if ( test == 0 ) {
|
|
|
|
match = 0;
|
|
|
|
}
|
|
|
|
}
|
2020-02-01 12:49:11 +00:00
|
|
|
}
|
|
|
|
return match;
|
2015-03-31 20:45:02 +00:00
|
|
|
}
|
2017-09-27 18:00:33 +00:00
|
|
|
static char *script_get_message ( const Mode *sw )
|
|
|
|
{
|
|
|
|
ScriptModePrivateData *pd = sw->private_data;
|
|
|
|
return g_strdup ( pd->message );
|
|
|
|
}
|
2018-09-09 15:53:56 +00:00
|
|
|
static cairo_surface_t *script_get_icon ( const Mode *sw, unsigned int selected_line, int height )
|
|
|
|
{
|
|
|
|
ScriptModePrivateData *pd = (ScriptModePrivateData *) mode_get_private_data ( sw );
|
|
|
|
g_return_val_if_fail ( pd->cmd_list != NULL, NULL );
|
2020-02-02 12:56:37 +00:00
|
|
|
DmenuScriptEntry *dr = &( pd->cmd_list[selected_line] );
|
2018-09-09 15:53:56 +00:00
|
|
|
if ( dr->icon_name == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ( dr->icon_fetch_uid > 0 ) {
|
|
|
|
return rofi_icon_fetcher_get ( dr->icon_fetch_uid );
|
|
|
|
}
|
|
|
|
dr->icon_fetch_uid = rofi_icon_fetcher_query ( dr->icon_name, height );
|
2018-10-05 09:01:48 +00:00
|
|
|
return rofi_icon_fetcher_get ( dr->icon_fetch_uid );
|
2018-09-09 15:53:56 +00:00
|
|
|
}
|
2014-07-21 19:39:24 +00:00
|
|
|
|
2016-01-07 20:27:20 +00:00
|
|
|
#include "mode-private.h"
|
2015-11-25 08:26:38 +00:00
|
|
|
Mode *script_switcher_parse_setup ( const char *str )
|
2014-07-21 19:39:24 +00:00
|
|
|
{
|
2016-03-20 09:16:55 +00:00
|
|
|
Mode *sw = g_malloc0 ( sizeof ( *sw ) );
|
|
|
|
char *endp = NULL;
|
|
|
|
char *parse = g_strdup ( str );
|
|
|
|
unsigned int index = 0;
|
|
|
|
const char *const sep = ":";
|
2016-03-19 12:29:04 +00:00
|
|
|
for ( char *token = strtok_r ( parse, sep, &endp ); token != NULL; token = strtok_r ( NULL, sep, &endp ) ) {
|
2014-07-21 19:39:24 +00:00
|
|
|
if ( index == 0 ) {
|
2017-04-21 07:37:03 +00:00
|
|
|
sw->name = g_strdup ( token );
|
2014-07-21 19:39:24 +00:00
|
|
|
}
|
|
|
|
else if ( index == 1 ) {
|
2015-11-27 12:01:25 +00:00
|
|
|
sw->ed = (void *) rofi_expand_path ( token );
|
2014-07-21 19:39:24 +00:00
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
2014-08-09 09:40:42 +00:00
|
|
|
g_free ( parse );
|
2014-07-21 19:39:24 +00:00
|
|
|
if ( index == 2 ) {
|
2016-01-07 20:27:20 +00:00
|
|
|
sw->free = script_switcher_free;
|
|
|
|
sw->_init = script_mode_init;
|
|
|
|
sw->_get_num_entries = script_mode_get_num_entries;
|
|
|
|
sw->_result = script_mode_result;
|
|
|
|
sw->_destroy = script_mode_destroy;
|
|
|
|
sw->_token_match = script_token_match;
|
2017-09-27 18:00:33 +00:00
|
|
|
sw->_get_message = script_get_message;
|
2018-09-09 15:53:56 +00:00
|
|
|
sw->_get_icon = script_get_icon;
|
2017-05-23 15:19:04 +00:00
|
|
|
sw->_get_completion = NULL,
|
|
|
|
sw->_preprocess_input = NULL,
|
2016-01-07 20:27:20 +00:00
|
|
|
sw->_get_display_value = _get_display_value;
|
2015-03-27 19:28:53 +00:00
|
|
|
|
2014-07-21 19:39:24 +00:00
|
|
|
return sw;
|
|
|
|
}
|
2017-04-21 07:37:03 +00:00
|
|
|
fprintf ( stderr, "The script command '%s' has %u options, but needs 2: <name>:<script>.", str, index );
|
2015-03-27 19:28:53 +00:00
|
|
|
script_switcher_free ( sw );
|
2014-07-21 19:39:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-04-20 07:24:09 +00:00
|
|
|
|
|
|
|
gboolean script_switcher_is_valid ( const char *token )
|
|
|
|
{
|
2017-04-27 20:59:14 +00:00
|
|
|
return strchr ( token, ':' ) != NULL;
|
2017-04-20 07:24:09 +00:00
|
|
|
}
|