2015-03-30 18:12:22 +00:00
|
|
|
/**
|
|
|
|
* rofi
|
|
|
|
*
|
|
|
|
* MIT/X11 License
|
2015-12-31 23:27:00 +00:00
|
|
|
* Copyright 2013-2016 Qball Cow <qball@gmpclient.org>
|
2015-03-30 18:12:22 +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 <rofi.h>
|
2016-01-07 15:01:56 +00:00
|
|
|
#include "settings.h"
|
2015-03-30 18:12:22 +00:00
|
|
|
|
2015-11-03 18:57:07 +00:00
|
|
|
#include <dialogs/dialogs.h>
|
2015-03-31 20:45:02 +00:00
|
|
|
|
2015-03-30 18:12:22 +00:00
|
|
|
/**
|
2015-11-25 08:26:38 +00:00
|
|
|
* Combi Mode
|
2015-03-30 18:12:22 +00:00
|
|
|
*/
|
2016-03-24 21:15:10 +00:00
|
|
|
typedef struct
|
2015-03-30 18:12:22 +00:00
|
|
|
{
|
|
|
|
// List of (combined) entries.
|
|
|
|
unsigned int cmd_list_length;
|
|
|
|
// List to validate where each switcher starts.
|
|
|
|
unsigned int *starts;
|
|
|
|
unsigned int *lengths;
|
|
|
|
// List of switchers to combine.
|
|
|
|
unsigned int num_switchers;
|
2015-11-25 08:26:38 +00:00
|
|
|
Mode **switchers;
|
2016-05-26 06:39:33 +00:00
|
|
|
Mode *current;
|
2015-03-30 18:12:22 +00:00
|
|
|
} CombiModePrivateData;
|
|
|
|
|
2015-11-25 08:26:38 +00:00
|
|
|
static void combi_mode_parse_switchers ( Mode *sw )
|
2015-03-30 18:12:22 +00:00
|
|
|
{
|
2016-01-07 20:27:20 +00:00
|
|
|
CombiModePrivateData *pd = mode_get_private_data ( sw );
|
2015-03-31 20:45:02 +00:00
|
|
|
char *savept = NULL;
|
|
|
|
// Make a copy, as strtok will modify it.
|
|
|
|
char *switcher_str = g_strdup ( config.combi_modi );
|
2016-03-20 09:16:55 +00:00
|
|
|
const char * const sep = ",";
|
2015-03-31 20:45:02 +00:00
|
|
|
// Split token on ','. This modifies switcher_str.
|
2016-03-19 12:29:04 +00:00
|
|
|
for ( char *token = strtok_r ( switcher_str, sep, &savept ); token != NULL;
|
|
|
|
token = strtok_r ( NULL, sep, &savept ) ) {
|
2015-03-31 20:45:02 +00:00
|
|
|
// Resize and add entry.
|
2015-11-25 08:26:38 +00:00
|
|
|
pd->switchers = (Mode * *) g_realloc ( pd->switchers,
|
|
|
|
sizeof ( Mode* ) * ( pd->num_switchers + 1 ) );
|
2015-03-31 20:45:02 +00:00
|
|
|
|
|
|
|
// Window switcher.
|
2015-09-14 06:57:10 +00:00
|
|
|
#ifdef WINDOW_MODE
|
2015-03-31 20:45:02 +00:00
|
|
|
if ( strcasecmp ( token, "window" ) == 0 ) {
|
|
|
|
pd->switchers[pd->num_switchers++] = &window_mode;
|
|
|
|
}
|
2015-09-14 06:57:10 +00:00
|
|
|
else if ( strcasecmp ( token, "windowcd" ) == 0 ) {
|
|
|
|
pd->switchers[pd->num_switchers++] = &window_mode_cd;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif // WINDOW_MODE
|
2015-03-31 20:45:02 +00:00
|
|
|
// SSh dialog
|
2015-09-14 06:57:10 +00:00
|
|
|
if ( strcasecmp ( token, "ssh" ) == 0 ) {
|
2015-03-31 20:45:02 +00:00
|
|
|
pd->switchers[pd->num_switchers++] = &ssh_mode;
|
|
|
|
}
|
|
|
|
// Run dialog
|
|
|
|
else if ( strcasecmp ( token, "run" ) == 0 ) {
|
|
|
|
pd->switchers[pd->num_switchers++] = &run_mode;
|
|
|
|
}
|
2016-08-29 19:38:29 +00:00
|
|
|
#ifdef ENABLE_DRUN
|
2015-11-03 18:57:07 +00:00
|
|
|
else if ( strcasecmp ( token, "drun" ) == 0 ) {
|
|
|
|
pd->switchers[pd->num_switchers++] = &drun_mode;
|
|
|
|
}
|
2016-08-29 19:38:29 +00:00
|
|
|
#endif // ENABLE_DRUN
|
2015-03-31 20:45:02 +00:00
|
|
|
else {
|
|
|
|
// If not build in, use custom switchers.
|
2015-11-25 08:26:38 +00:00
|
|
|
Mode *sw = script_switcher_parse_setup ( token );
|
2015-03-31 20:45:02 +00:00
|
|
|
if ( sw != NULL ) {
|
|
|
|
pd->switchers[pd->num_switchers++] = sw;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
// Report error, don't continue.
|
|
|
|
fprintf ( stderr, "Invalid script switcher: %s\n", token );
|
|
|
|
token = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Keybinding.
|
|
|
|
}
|
|
|
|
// Free string that was modified by strtok_r
|
|
|
|
g_free ( switcher_str );
|
2015-03-30 18:12:22 +00:00
|
|
|
}
|
2015-03-31 20:45:02 +00:00
|
|
|
|
2016-01-08 08:16:59 +00:00
|
|
|
static int combi_mode_init ( Mode *sw )
|
2015-03-30 18:12:22 +00:00
|
|
|
{
|
2016-01-07 20:27:20 +00:00
|
|
|
if ( mode_get_private_data ( sw ) == NULL ) {
|
2015-03-30 18:12:22 +00:00
|
|
|
CombiModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
|
2016-01-07 20:27:20 +00:00
|
|
|
mode_set_private_data ( sw, (void *) pd );
|
2015-03-31 20:45:02 +00:00
|
|
|
combi_mode_parse_switchers ( sw );
|
2015-03-30 18:12:22 +00:00
|
|
|
pd->starts = g_malloc0 ( sizeof ( int ) * pd->num_switchers );
|
|
|
|
pd->lengths = g_malloc0 ( sizeof ( int ) * pd->num_switchers );
|
2015-03-31 20:45:02 +00:00
|
|
|
for ( unsigned int i = 0; i < pd->num_switchers; i++ ) {
|
2016-01-08 08:16:59 +00:00
|
|
|
if ( !mode_init ( pd->switchers[i] ) ) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-03-31 20:45:02 +00:00
|
|
|
}
|
2015-11-21 22:59:59 +00:00
|
|
|
if ( pd->cmd_list_length == 0 ) {
|
|
|
|
pd->cmd_list_length = 0;
|
|
|
|
for ( unsigned int i = 0; i < pd->num_switchers; i++ ) {
|
2016-01-07 18:47:37 +00:00
|
|
|
unsigned int length = mode_get_num_entries ( pd->switchers[i] );
|
2015-11-21 22:59:59 +00:00
|
|
|
pd->starts[i] = pd->cmd_list_length;
|
|
|
|
pd->lengths[i] = length;
|
|
|
|
pd->cmd_list_length += length;
|
|
|
|
}
|
|
|
|
}
|
2015-03-30 18:12:22 +00:00
|
|
|
}
|
2016-01-08 08:16:59 +00:00
|
|
|
return TRUE;
|
2015-03-30 18:12:22 +00:00
|
|
|
}
|
2015-11-25 08:26:38 +00:00
|
|
|
static unsigned int combi_mode_get_num_entries ( const Mode *sw )
|
2015-03-30 18:12:22 +00:00
|
|
|
{
|
2016-01-07 20:27:20 +00:00
|
|
|
const CombiModePrivateData *pd = (const CombiModePrivateData *) mode_get_private_data ( sw );
|
2015-11-21 22:59:59 +00:00
|
|
|
return pd->cmd_list_length;
|
2015-03-30 18:12:22 +00:00
|
|
|
}
|
2015-11-25 08:26:38 +00:00
|
|
|
static void combi_mode_destroy ( Mode *sw )
|
2015-03-30 18:12:22 +00:00
|
|
|
{
|
2016-01-07 20:27:20 +00:00
|
|
|
CombiModePrivateData *pd = (CombiModePrivateData *) mode_get_private_data ( sw );
|
2015-03-30 18:12:22 +00:00
|
|
|
if ( pd != NULL ) {
|
|
|
|
g_free ( pd->starts );
|
|
|
|
g_free ( pd->lengths );
|
2015-03-31 20:45:02 +00:00
|
|
|
// Cleanup switchers.
|
|
|
|
for ( unsigned int i = 0; i < pd->num_switchers; i++ ) {
|
2016-01-07 18:47:37 +00:00
|
|
|
mode_destroy ( pd->switchers[i] );
|
2015-03-31 20:45:02 +00:00
|
|
|
}
|
2015-03-30 18:13:47 +00:00
|
|
|
g_free ( pd->switchers );
|
2015-06-30 19:18:45 +00:00
|
|
|
g_free ( pd );
|
2016-01-07 20:27:20 +00:00
|
|
|
mode_set_private_data ( sw, NULL );
|
2015-03-30 18:12:22 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-25 08:26:38 +00:00
|
|
|
static ModeMode combi_mode_result ( Mode *sw, int mretv, char **input, unsigned int selected_line )
|
2015-03-30 18:12:22 +00:00
|
|
|
{
|
2016-01-07 20:27:20 +00:00
|
|
|
CombiModePrivateData *pd = mode_get_private_data ( sw );
|
2015-10-18 20:12:06 +00:00
|
|
|
if ( *input[0] == '!' ) {
|
|
|
|
int switcher = -1;
|
|
|
|
for ( unsigned i = 0; switcher == -1 && i < pd->num_switchers; i++ ) {
|
2016-01-07 20:27:20 +00:00
|
|
|
if ( ( *input )[1] == mode_get_name ( pd->switchers[i] )[0] ) {
|
2015-10-18 20:12:06 +00:00
|
|
|
switcher = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( switcher >= 0 ) {
|
|
|
|
char *n = strchr ( *input, ' ' );
|
|
|
|
// skip whitespace
|
2015-10-19 07:23:06 +00:00
|
|
|
if ( n != NULL ) {
|
2015-10-18 20:12:06 +00:00
|
|
|
n++;
|
2016-01-07 20:27:20 +00:00
|
|
|
return mode_result ( pd->switchers[switcher], mretv, &n,
|
|
|
|
selected_line - pd->starts[switcher] );
|
2015-10-18 20:12:06 +00:00
|
|
|
}
|
|
|
|
return MODE_EXIT;
|
|
|
|
}
|
|
|
|
}
|
2016-10-08 16:57:59 +00:00
|
|
|
if ( mretv & MENU_QUICK_SWITCH ) {
|
|
|
|
return mretv & MENU_LOWER_MASK;
|
|
|
|
}
|
2015-03-30 18:12:22 +00:00
|
|
|
|
|
|
|
for ( unsigned i = 0; i < pd->num_switchers; i++ ) {
|
|
|
|
if ( selected_line >= pd->starts[i] &&
|
|
|
|
selected_line < ( pd->starts[i] + pd->lengths[i] ) ) {
|
2016-01-07 20:27:20 +00:00
|
|
|
return mode_result ( pd->switchers[i], mretv, input, selected_line - pd->starts[i] );
|
2015-03-30 18:12:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return MODE_EXIT;
|
|
|
|
}
|
2016-05-22 15:47:34 +00:00
|
|
|
static int combi_mode_match ( const Mode *sw, GRegex **tokens, unsigned int index )
|
2015-03-30 18:12:22 +00:00
|
|
|
{
|
2016-01-07 20:27:20 +00:00
|
|
|
CombiModePrivateData *pd = mode_get_private_data ( sw );
|
2016-05-26 06:39:33 +00:00
|
|
|
for ( unsigned i = 0; i < pd->num_switchers; i++ ) {
|
|
|
|
if ( pd->current != NULL && pd->switchers[i] != pd->current ) {
|
|
|
|
continue;
|
2015-03-30 18:12:22 +00:00
|
|
|
}
|
2016-05-26 06:39:33 +00:00
|
|
|
if ( index >= pd->starts[i] && index < ( pd->starts[i] + pd->lengths[i] ) ) {
|
|
|
|
return mode_token_match ( pd->switchers[i], tokens, index - pd->starts[i] );
|
2015-11-24 13:14:55 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-30 18:12:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-11-25 08:26:38 +00:00
|
|
|
static char * combi_mgrv ( const Mode *sw, unsigned int selected_line, int *state, int get_entry )
|
2015-03-31 20:45:02 +00:00
|
|
|
{
|
2016-01-07 20:27:20 +00:00
|
|
|
CombiModePrivateData *pd = mode_get_private_data ( sw );
|
2015-11-21 22:59:59 +00:00
|
|
|
if ( !get_entry ) {
|
|
|
|
for ( unsigned i = 0; i < pd->num_switchers; i++ ) {
|
|
|
|
if ( selected_line >= pd->starts[i] && selected_line < ( pd->starts[i] + pd->lengths[i] ) ) {
|
2016-01-07 20:27:20 +00:00
|
|
|
mode_get_display_value ( pd->switchers[i], selected_line - pd->starts[i], state, FALSE );
|
2015-11-21 22:59:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-03-31 20:45:02 +00:00
|
|
|
for ( unsigned i = 0; i < pd->num_switchers; i++ ) {
|
2015-09-19 18:59:50 +00:00
|
|
|
if ( selected_line >= pd->starts[i] && selected_line < ( pd->starts[i] + pd->lengths[i] ) ) {
|
2016-01-07 20:27:20 +00:00
|
|
|
char * str = mode_get_display_value ( pd->switchers[i], selected_line - pd->starts[i], state, TRUE );
|
2016-01-12 21:17:53 +00:00
|
|
|
char * retv = g_strdup_printf ( "%s %s", mode_get_display_name ( pd->switchers[i] ), str );
|
2015-11-21 22:59:59 +00:00
|
|
|
g_free ( str );
|
|
|
|
return retv;
|
2015-03-31 20:45:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-11-25 08:26:38 +00:00
|
|
|
static char * combi_get_completion ( const Mode *sw, unsigned int index )
|
2015-11-24 12:59:35 +00:00
|
|
|
{
|
2016-01-07 20:27:20 +00:00
|
|
|
CombiModePrivateData *pd = mode_get_private_data ( sw );
|
2015-11-24 12:59:35 +00:00
|
|
|
for ( unsigned i = 0; i < pd->num_switchers; i++ ) {
|
|
|
|
if ( index >= pd->starts[i] && index < ( pd->starts[i] + pd->lengths[i] ) ) {
|
2016-04-19 20:10:34 +00:00
|
|
|
char *comp = mode_get_completion ( pd->switchers[i], index - pd->starts[i] );
|
2016-04-10 18:57:12 +00:00
|
|
|
char *mcomp = g_strdup_printf ( "!%c %s", mode_get_name ( pd->switchers[i] )[0], comp );
|
2016-04-19 20:10:34 +00:00
|
|
|
g_free ( comp );
|
2016-04-10 18:57:12 +00:00
|
|
|
return mcomp;
|
2015-11-24 12:59:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Should never get here.
|
|
|
|
g_error ( "Failure, could not resolve sub-switcher." );
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-03-30 18:12:22 +00:00
|
|
|
|
2016-05-26 06:39:33 +00:00
|
|
|
static char * combi_preprocess_input ( Mode *sw, const char *input )
|
|
|
|
{
|
|
|
|
CombiModePrivateData *pd = mode_get_private_data ( sw );
|
|
|
|
pd->current = NULL;
|
|
|
|
if ( input != NULL && input[0] == '!' && strlen ( input ) > 1 ) {
|
|
|
|
for ( unsigned i = 0; i < pd->num_switchers; i++ ) {
|
|
|
|
if ( input[1] == mode_get_name ( pd->switchers[i] )[0] ) {
|
|
|
|
pd->current = pd->switchers[i];
|
|
|
|
if ( input[2] == '\0' ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return g_strdup ( &input[2] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return g_strdup ( input );
|
|
|
|
}
|
|
|
|
|
2016-01-07 20:27:20 +00:00
|
|
|
#include "mode-private.h"
|
2015-11-25 08:26:38 +00:00
|
|
|
Mode combi_mode =
|
2015-03-30 18:12:22 +00:00
|
|
|
{
|
2016-01-07 20:27:20 +00:00
|
|
|
.name = "combi",
|
2016-01-12 21:17:53 +00:00
|
|
|
.cfg_name_key = "display-combi",
|
2016-01-07 20:27:20 +00:00
|
|
|
._init = combi_mode_init,
|
|
|
|
._get_num_entries = combi_mode_get_num_entries,
|
|
|
|
._result = combi_mode_result,
|
|
|
|
._destroy = combi_mode_destroy,
|
|
|
|
._token_match = combi_mode_match,
|
|
|
|
._get_completion = combi_get_completion,
|
|
|
|
._get_display_value = combi_mgrv,
|
2016-05-26 06:39:33 +00:00
|
|
|
._preprocess_input = combi_preprocess_input,
|
2016-01-07 20:27:20 +00:00
|
|
|
.private_data = NULL,
|
|
|
|
.free = NULL
|
2015-03-30 18:12:22 +00:00
|
|
|
};
|