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>
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
typedef struct _CombiModePrivateData
|
|
|
|
{
|
|
|
|
// 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;
|
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
|
|
|
{
|
2015-03-31 20:45:02 +00:00
|
|
|
CombiModePrivateData *pd = sw->private_data;
|
|
|
|
char *savept = NULL;
|
|
|
|
// Make a copy, as strtok will modify it.
|
|
|
|
char *switcher_str = g_strdup ( config.combi_modi );
|
|
|
|
// Split token on ','. This modifies switcher_str.
|
2015-09-19 18:59:50 +00:00
|
|
|
for ( char *token = strtok_r ( switcher_str, ",", &savept ); token != NULL;
|
2015-03-31 20:45:02 +00:00
|
|
|
token = strtok_r ( NULL, ",", &savept ) ) {
|
|
|
|
// 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;
|
|
|
|
}
|
2015-11-03 18:57:07 +00:00
|
|
|
else if ( strcasecmp ( token, "drun" ) == 0 ) {
|
|
|
|
pd->switchers[pd->num_switchers++] = &drun_mode;
|
|
|
|
}
|
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
|
|
|
|
2015-11-25 08:26:38 +00:00
|
|
|
static void combi_mode_init ( Mode *sw )
|
2015-03-30 18:12:22 +00:00
|
|
|
{
|
|
|
|
if ( sw->private_data == NULL ) {
|
|
|
|
CombiModePrivateData *pd = g_malloc0 ( sizeof ( *pd ) );
|
|
|
|
sw->private_data = (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++ ) {
|
|
|
|
pd->switchers[i]->init ( pd->switchers[i] );
|
|
|
|
}
|
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++ ) {
|
|
|
|
unsigned int length = pd->switchers[i]->get_num_entries ( pd->switchers[i] );;
|
|
|
|
pd->starts[i] = pd->cmd_list_length;
|
|
|
|
pd->lengths[i] = length;
|
|
|
|
pd->cmd_list_length += length;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
2015-11-23 21:14:26 +00:00
|
|
|
const CombiModePrivateData *pd = (const CombiModePrivateData *) sw->private_data;
|
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
|
|
|
{
|
|
|
|
CombiModePrivateData *pd = (CombiModePrivateData *) sw->private_data;
|
|
|
|
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++ ) {
|
|
|
|
pd->switchers[i]->destroy ( pd->switchers[i] );
|
|
|
|
}
|
2015-03-30 18:13:47 +00:00
|
|
|
g_free ( pd->switchers );
|
2015-06-30 19:18:45 +00:00
|
|
|
g_free ( pd );
|
2015-03-30 18:12:22 +00:00
|
|
|
sw->private_data = NULL;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
CombiModePrivateData *pd = sw->private_data;
|
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++ ) {
|
|
|
|
if ( ( *input )[1] == pd->switchers[i]->name[0] ) {
|
|
|
|
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++;
|
2015-11-23 21:14:26 +00:00
|
|
|
return pd->switchers[switcher]->result ( pd->switchers[switcher], mretv, &n,
|
|
|
|
selected_line - pd->starts[switcher] );
|
2015-10-18 20:12:06 +00:00
|
|
|
}
|
|
|
|
return MODE_EXIT;
|
|
|
|
}
|
|
|
|
}
|
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] ) ) {
|
2015-11-23 21:14:26 +00:00
|
|
|
return pd->switchers[i]->result ( pd->switchers[i], mretv, input, selected_line - pd->starts[i] );
|
2015-03-30 18:12:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return MODE_EXIT;
|
|
|
|
}
|
2015-11-25 08:26:38 +00:00
|
|
|
static int combi_mode_match ( const Mode *sw, char **tokens, int not_ascii,
|
2015-11-23 21:14:26 +00:00
|
|
|
int case_sensitive, unsigned int index )
|
2015-03-30 18:12:22 +00:00
|
|
|
{
|
|
|
|
CombiModePrivateData *pd = sw->private_data;
|
2015-11-24 13:14:55 +00:00
|
|
|
if ( config.regex || config.glob ) {
|
|
|
|
// Bang support only works in text mode.
|
|
|
|
for ( unsigned i = 0; i < pd->num_switchers; i++ ) {
|
|
|
|
if ( index >= pd->starts[i] && index < ( pd->starts[i] + pd->lengths[i] ) ) {
|
2015-11-23 21:14:26 +00:00
|
|
|
return pd->switchers[i]->token_match ( pd->switchers[i], tokens, not_ascii, case_sensitive,
|
|
|
|
index - pd->starts[i] );
|
2015-09-28 16:19:10 +00:00
|
|
|
}
|
2015-03-30 18:12:22 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-24 13:14:55 +00:00
|
|
|
else{
|
|
|
|
for ( unsigned i = 0; i < pd->num_switchers; i++ ) {
|
|
|
|
if ( index >= pd->starts[i] && index < ( pd->starts[i] + pd->lengths[i] ) ) {
|
|
|
|
if ( tokens && tokens[0][0] == '!' ) {
|
|
|
|
if ( tokens[0][1] == pd->switchers[i]->name[0] ) {
|
|
|
|
return pd->switchers[i]->token_match ( pd->switchers[i], &tokens[1], not_ascii, case_sensitive,
|
|
|
|
index - pd->starts[i] );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return pd->switchers[i]->token_match ( pd->switchers[i], tokens, not_ascii, case_sensitive,
|
|
|
|
index - pd->starts[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-30 18:12:22 +00:00
|
|
|
abort ();
|
|
|
|
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
|
|
|
{
|
2015-11-21 22:59:59 +00:00
|
|
|
CombiModePrivateData *pd = sw->private_data;
|
|
|
|
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] ) ) {
|
2015-11-24 13:22:35 +00:00
|
|
|
pd->switchers[i]->mgrv ( 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] ) ) {
|
2015-11-24 13:22:35 +00:00
|
|
|
char * str = pd->switchers[i]->mgrv ( pd->switchers[i], selected_line - pd->starts[i], state, TRUE );
|
2015-11-21 22:59:59 +00:00
|
|
|
char * retv = g_strdup_printf ( "(%s) %s", pd->switchers[i]->name, str );
|
|
|
|
g_free ( str );
|
|
|
|
return retv;
|
2015-03-31 20:45:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-11-25 08:26:38 +00:00
|
|
|
static int combi_is_not_ascii ( const Mode *sw, unsigned int index )
|
2015-11-21 22:59:59 +00:00
|
|
|
{
|
|
|
|
CombiModePrivateData *pd = sw->private_data;
|
|
|
|
for ( unsigned i = 0; i < pd->num_switchers; i++ ) {
|
|
|
|
if ( index >= pd->starts[i] && index < ( pd->starts[i] + pd->lengths[i] ) ) {
|
|
|
|
return pd->switchers[i]->is_not_ascii ( pd->switchers[i], index - pd->starts[i] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
CombiModePrivateData *pd = sw->private_data;
|
|
|
|
for ( unsigned i = 0; i < pd->num_switchers; i++ ) {
|
|
|
|
if ( index >= pd->starts[i] && index < ( pd->starts[i] + pd->lengths[i] ) ) {
|
|
|
|
char * str = NULL;
|
|
|
|
if ( pd->switchers[i]->get_completion != NULL ) {
|
|
|
|
str = pd->switchers[i]->get_completion ( pd->switchers[i], index - pd->starts[i] );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int state;
|
2015-11-24 13:22:35 +00:00
|
|
|
str = pd->switchers[i]->mgrv ( pd->switchers[i], index - pd->starts[i], &state, TRUE );
|
2015-11-24 12:59:35 +00:00
|
|
|
}
|
2015-11-24 13:14:55 +00:00
|
|
|
return str;
|
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
|
|
|
|
2015-11-25 08:26:38 +00:00
|
|
|
Mode combi_mode =
|
2015-03-30 18:12:22 +00:00
|
|
|
{
|
2015-11-21 22:59:59 +00:00
|
|
|
.name = "combi",
|
|
|
|
.keycfg = NULL,
|
|
|
|
.keystr = NULL,
|
|
|
|
.modmask = AnyModifier,
|
|
|
|
.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,
|
2015-11-24 12:59:35 +00:00
|
|
|
.get_completion = combi_get_completion,
|
2015-11-21 22:59:59 +00:00
|
|
|
.mgrv = combi_mgrv,
|
|
|
|
.is_not_ascii = combi_is_not_ascii,
|
|
|
|
.private_data = NULL,
|
|
|
|
.free = NULL
|
2015-03-30 18:12:22 +00:00
|
|
|
};
|