Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Execute custom user commands or scripts on a variety of rofi events #2053

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ Settings config = {
/** Custom command to generate preview icons */
.preview_cmd = NULL,

/** Custom command to call when menu selection changes */
.on_selection_changed = NULL,
/** Custom command to call when menu mode changes */
.on_mode_changed = NULL,
/** Custom command to call when menu entry is accepted */
.on_entry_accepted = NULL,
/** Custom command to call when menu is canceled */
.on_menu_canceled = NULL,
/** Custom command to call when menu finds errors */
.on_menu_error = NULL,
/** Custom command to call when menu screenshot is taken */
.on_screenshot_taken = NULL,
/** Terminal to use. (for ssh and open in terminal) */
.terminal_emulator = "rofi-sensible-terminal",
.ssh_client = "ssh",
Expand Down
12 changes: 12 additions & 0 deletions include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ typedef struct {
/** Custom command to generate preview icons */
char *preview_cmd;

/** Custom command to call when menu selection changes */
char *on_selection_changed;
/** Custom command to call when menu mode changes */
char *on_mode_changed;
/** Custom command to call when menu entry is accepted */
char *on_entry_accepted;
/** Custom command to call when menu is canceled */
char *on_menu_canceled;
/** Custom command to call when menu finds errors */
char *on_menu_error;
/** Custom command to call when menu screenshot is taken */
char *on_screenshot_taken;
/** Terminal to use */
char *terminal_emulator;
/** SSH client to use */
Expand Down
89 changes: 87 additions & 2 deletions source/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,18 @@ static int lev_sort(const void *p1, const void *p2, void *arg) {
return distances[*a] - distances[*b];
}

static void screenshot_taken_user_callback(const char *path) {
if (config.on_screenshot_taken == NULL)
return;

char **args = NULL;
int argv = 0;
helper_parse_setup(config.on_screenshot_taken, &args, &argv, "{path}",
path, (char *)0);
if (args != NULL)
helper_execute(NULL, args, "", config.on_screenshot_taken, NULL);
}

/**
* Stores a screenshot of Rofi at that point in time.
*/
Expand Down Expand Up @@ -271,6 +283,7 @@ void rofi_capture_screenshot(void) {
g_warning("Failed to produce screenshot '%s', got error: '%s'", fpath,
cairo_status_to_string(status));
}
screenshot_taken_user_callback(fpath);
}
cairo_destroy(draw);
}
Expand Down Expand Up @@ -1285,6 +1298,29 @@ inline static void rofi_view_nav_last(RofiViewState *state) {
// state->selected = state->filtered_lines - 1;
listview_set_selected(state->list_view, -1);
}
static void selection_changed_user_callback(unsigned int index, RofiViewState *state) {
if (config.on_selection_changed == NULL)
return;

static unsigned int last_index = UINT32_MAX;
giomatfois62 marked this conversation as resolved.
Show resolved Hide resolved
if (index >= state->filtered_lines)
return;

unsigned int real_index = state->line_map[index];
if (last_index != real_index) {
last_index = real_index;
int fstate = 0;
char *text = mode_get_display_value(state->sw, real_index,
&fstate, NULL, TRUE);
char **args = NULL;
int argv = 0;
helper_parse_setup(config.on_selection_changed, &args, &argv, "{entry}",
text, (char *)0);
if (args != NULL)
helper_execute(NULL, args, "", config.on_selection_changed, NULL);
g_free(text);
}
}
static void selection_changed_callback(G_GNUC_UNUSED listview *lv,
unsigned int index, void *udata) {
RofiViewState *state = (RofiViewState *)udata;
Expand All @@ -1295,7 +1331,6 @@ static void selection_changed_callback(G_GNUC_UNUSED listview *lv,
&fstate, NULL, TRUE);
textbox_text(state->tb_current_entry, text);
g_free(text);

} else {
textbox_text(state->tb_current_entry, "");
}
Expand All @@ -1312,6 +1347,7 @@ static void selection_changed_callback(G_GNUC_UNUSED listview *lv,
icon_set_surface(state->icon_current_entry, NULL);
}
}
selection_changed_user_callback(index, state);
}
static void update_callback(textbox *t, icon *ico, unsigned int index,
void *udata, TextBoxFontType *type, gboolean full) {
Expand Down Expand Up @@ -1882,7 +1918,6 @@ static void rofi_view_trigger_global_action(KeyBindingAction action) {
// Nothing entered and nothing selected.
state->retv = MENU_CUSTOM_INPUT;
}

state->quit = TRUE;
break;
}
Expand Down Expand Up @@ -2086,8 +2121,43 @@ void rofi_view_handle_mouse_motion(RofiViewState *state, gint x, gint y,
}
}

static void rofi_quit_user_callback(RofiViewState *state) {
if (state->retv & MENU_OK) {
if (config.on_entry_accepted == NULL)
return;
int fstate = 0;
unsigned int selected = listview_get_selected(state->list_view);
// TODO: handle custom text
if (selected >= state->filtered_lines)
return;
// Pass selected text to custom command
char *text = mode_get_display_value(state->sw, state->line_map[selected],
&fstate, NULL, TRUE);
char **args = NULL;
int argv = 0;
helper_parse_setup(config.on_entry_accepted, &args, &argv, "{entry}",
text, (char *)0);
if (args != NULL)
helper_execute(NULL, args, "", config.on_entry_accepted, NULL);
g_free(text);
} else if(state->retv & MENU_CANCEL) {
if (config.on_menu_canceled == NULL)
return;
helper_execute_command(NULL, config.on_menu_canceled, FALSE, NULL);
} else if (state->retv & MENU_NEXT ||
state->retv & MENU_PREVIOUS ||
state->retv & MENU_QUICK_SWITCH ||
state->retv & MENU_COMPLETE) {
if (config.on_mode_changed == NULL)
return;
// TODO: pass mode name to custom command
helper_execute_command(NULL, config.on_mode_changed, FALSE, NULL);
}
}
void rofi_view_maybe_update(RofiViewState *state) {
if (rofi_view_get_completed(state)) {
// Exec custom user commands
rofi_quit_user_callback(state);
// This menu is done.
rofi_view_finalize(state);
// If there a state. (for example error) reload it.
Expand Down Expand Up @@ -2571,6 +2641,18 @@ RofiViewState *rofi_view_create(Mode *sw, const char *input,
return state;
}

static void rofi_error_user_callback(const char *msg) {
if (config.on_menu_error == NULL)
return;

char **args = NULL;
int argv = 0;
helper_parse_setup(config.on_menu_error, &args, &argv, "{error}",
msg, (char *)0);
if (args != NULL)
helper_execute(NULL, args, "", config.on_menu_error, NULL);
}

int rofi_view_error_dialog(const char *msg, int markup) {
RofiViewState *state = __rofi_view_state_create();
state->retv = MENU_CANCEL;
Expand Down Expand Up @@ -2608,6 +2690,9 @@ int rofi_view_error_dialog(const char *msg, int markup) {
sn_launchee_context_complete(xcb->sncontext);
}

// Exec custom command
rofi_error_user_callback(msg);

// Set it as current window.
rofi_view_set_active(state);
return TRUE;
Expand Down
38 changes: 37 additions & 1 deletion source/xrmoptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,50 @@ static XrmOption xrmOptions[] = {
NULL,
"Whether to load and show icons",
CONFIG_DEFAULT},

{xrm_String,
"preview-cmd",
{.str = &config.preview_cmd},
NULL,
"Custom command to generate preview icons",
CONFIG_DEFAULT},

{xrm_String,
"on-selection-changed",
{.str = &config.on_selection_changed},
NULL,
"Custom command to call when menu selection changes",
CONFIG_DEFAULT},
{xrm_String,
"on-mode-changed",
{.str = &config.on_mode_changed},
NULL,
"Custom command to call when menu mode changes",
CONFIG_DEFAULT},
{xrm_String,
"on-entry-accepted",
{.str = &config.on_entry_accepted},
NULL,
"Custom command to call when menu entry is accepted",
CONFIG_DEFAULT},
{xrm_String,
"on-menu-canceled",
{.str = &config.on_menu_canceled},
NULL,
"Custom command to call when menu is canceled",
CONFIG_DEFAULT},
{xrm_String,
"on-menu-error",
{.str = &config.on_menu_error},
NULL,
"Custom command to call when menu finds errors",
CONFIG_DEFAULT},
{xrm_String,
"on-screenshot-taken",
{.str = &config.on_screenshot_taken},
NULL,
"Custom command to call when menu screenshot is taken",
CONFIG_DEFAULT},

{xrm_String,
"terminal",
{.str = &config.terminal_emulator},
Expand Down
Loading