From 4b7da73a20ae07cf0492b6169647124f279b29db Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Sat, 27 Mar 2021 16:28:27 +0100 Subject: [PATCH] Add [no]focus restore option. --- config/config.c | 4 +++- doc/rofi.1 | 6 ++++++ doc/rofi.1.markdown | 4 ++++ include/settings.h | 2 ++ source/dialogs/window.c | 9 +++++++-- source/xcb.c | 4 ++++ source/xrmoptions.c | 2 ++ 7 files changed, 28 insertions(+), 3 deletions(-) diff --git a/config/config.c b/config/config.c index 942063bc..e09f732d 100644 --- a/config/config.c +++ b/config/config.c @@ -173,5 +173,7 @@ Settings config = { .benchmark_ui = FALSE, /** normalize match */ - .normalize_match = FALSE + .normalize_match = FALSE, + /** restore focus */ + .restore_focus = TRUE }; diff --git a/doc/rofi.1 b/doc/rofi.1 index 57e0dd26..97219285 100644 --- a/doc/rofi.1 +++ b/doc/rofi.1 @@ -442,6 +442,12 @@ Use Pango markup to format output wherever possible. .PP Make \fBrofi\fP react like a normal application window. Useful for scripts like Clerk that are basically an application. +.PP +\fB\fC\-[no\-]restore\-focus\fR + +.PP +Make rofi restore focus to window that held it when launched. + .SS Matching .PP \fB\fC\-matching\fR \fImethod\fP diff --git a/doc/rofi.1.markdown b/doc/rofi.1.markdown index b664aa92..b8c857e8 100644 --- a/doc/rofi.1.markdown +++ b/doc/rofi.1.markdown @@ -254,6 +254,10 @@ Use Pango markup to format output wherever possible. Make **rofi** react like a normal application window. Useful for scripts like Clerk that are basically an application. +`-[no-]restore-focus` + +Make rofi restore focus to window that held it when launched. + ### Matching `-matching` *method* diff --git a/include/settings.h b/include/settings.h index 491036b5..f0d2c13f 100644 --- a/include/settings.h +++ b/include/settings.h @@ -205,6 +205,8 @@ typedef struct gboolean benchmark_ui; gboolean normalize_match; + /** Restore focus */ + gboolean restore_focus; } Settings; /** Global Settings structure. */ extern Settings config; diff --git a/source/dialogs/window.c b/source/dialogs/window.c index 73adf045..4ca84bbc 100644 --- a/source/dialogs/window.c +++ b/source/dialogs/window.c @@ -751,11 +751,15 @@ static void helper_eval_add_str ( GString *str, const char *input, int l, int ma else { if ( nc > l ) { int bl = g_utf8_offset_to_pointer ( input_nn, l ) - input_nn; - g_string_append_len ( str, input_nn, bl ); + char *tmp = g_regex_escape_string(input_nn, bl); + g_string_append ( str, tmp ); + g_free(tmp); } else { spaces = l - nc; - g_string_append ( str, input_nn ); + char *tmp = g_regex_escape_string(input_nn, -1); + g_string_append ( str, tmp ); + g_free(tmp); } } while ( spaces-- ) { @@ -819,6 +823,7 @@ static char *_get_display_value ( const Mode *sw, unsigned int selected_line, in if ( c->active ) { *state |= ACTIVE; } + *state |= MARKUP; return get_entry ? _generate_display_string ( rmpd, c ) : NULL; } diff --git a/source/xcb.c b/source/xcb.c index a2ebefb4..8dfbf539 100644 --- a/source/xcb.c +++ b/source/xcb.c @@ -1187,6 +1187,10 @@ static gboolean main_loop_x11_event_handler ( xcb_generic_event_t *ev, G_GNUC_UN void rofi_xcb_set_input_focus ( xcb_window_t w ) { + if ( config.restore_focus == FALSE ) { + xcb->focus_revert = 0; + return; + } xcb_generic_error_t *error; xcb_get_input_focus_reply_t *freply; xcb_get_input_focus_cookie_t fcookie = xcb_get_input_focus ( xcb->connection ); diff --git a/source/xrmoptions.c b/source/xrmoptions.c index f4edddcb..ab60183f 100644 --- a/source/xrmoptions.c +++ b/source/xrmoptions.c @@ -233,6 +233,8 @@ static XrmOption xrmOptions[] = { "DRUN: If enabled, reload the cache with desktop file content.", CONFIG_DEFAULT }, { xrm_Boolean, "normalize-match", { .snum = &config.normalize_match }, NULL, "Normalize string when matching (implies -no-show-match).", CONFIG_DEFAULT }, + { xrm_Boolean, "restore-focus", { .snum = &config.restore_focus }, NULL, + "Restore focus on close to window that had it on rofi start.", CONFIG_DEFAULT }, }; /** Dynamic array of extra options */