rofi  1.5.1
keyb.c
Go to the documentation of this file.
1 /*
2  * rofi
3  *
4  * MIT/X11 License
5  * Copyright © 2013-2017 Qball Cow <qball@gmpclient.org>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  */
27 
28 #include <config.h>
29 #include <string.h>
30 #include "rofi.h"
31 #include "nkutils-bindings.h"
32 #include "xrmoptions.h"
33 
34 typedef struct
35 {
36  guint id;
37  guint scope;
38  char *name;
39  char *binding;
40  char *comment;
42 
47 {
48  { .id = PASTE_PRIMARY, .name = "kb-primary-paste", .binding = "Control+V,Shift+Insert", .comment = "Paste primary selection" },
49  { .id = PASTE_SECONDARY, .name = "kb-secondary-paste", .binding = "Control+v,Insert", .comment = "Paste clipboard" },
50  { .id = CLEAR_LINE, .name = "kb-clear-line", .binding = "Control+w", .comment = "Clear input line" },
51  { .id = MOVE_FRONT, .name = "kb-move-front", .binding = "Control+a", .comment = "Beginning of line" },
52  { .id = MOVE_END, .name = "kb-move-end", .binding = "Control+e", .comment = "End of line" },
53  { .id = MOVE_WORD_BACK, .name = "kb-move-word-back", .binding = "Alt+b", .comment = "Move back one word" },
54  { .id = MOVE_WORD_FORWARD, .name = "kb-move-word-forward", .binding = "Alt+f", .comment = "Move forward one word" },
55  { .id = MOVE_CHAR_BACK, .name = "kb-move-char-back", .binding = "Left,Control+b", .comment = "Move back one char" },
56  { .id = MOVE_CHAR_FORWARD, .name = "kb-move-char-forward", .binding = "Right,Control+f", .comment = "Move forward one char" },
57  { .id = REMOVE_WORD_BACK, .name = "kb-remove-word-back", .binding = "Control+Alt+h,Control+BackSpace", .comment = "Delete previous word" },
58  { .id = REMOVE_WORD_FORWARD, .name = "kb-remove-word-forward", .binding = "Control+Alt+d", .comment = "Delete next word" },
59  { .id = REMOVE_CHAR_FORWARD, .name = "kb-remove-char-forward", .binding = "Delete,Control+d", .comment = "Delete next char" },
60  { .id = REMOVE_CHAR_BACK, .name = "kb-remove-char-back", .binding = "BackSpace,Control+h", .comment = "Delete previous char" },
61  { .id = REMOVE_TO_EOL, .name = "kb-remove-to-eol", .binding = "Control+k", .comment = "Delete till the end of line" },
62  { .id = REMOVE_TO_SOL, .name = "kb-remove-to-sol", .binding = "Control+u", .comment = "Delete till the start of line" },
63  { .id = ACCEPT_ENTRY, .name = "kb-accept-entry", .binding = "Control+j,Control+m,Return,KP_Enter", .comment = "Accept entry" },
64  { .id = ACCEPT_CUSTOM, .name = "kb-accept-custom", .binding = "Control+Return", .comment = "Use entered text as command (in ssh/run modi)" },
65  { .id = ACCEPT_ALT, .name = "kb-accept-alt", .binding = "Shift+Return", .comment = "Use alternate accept command." },
66  { .id = DELETE_ENTRY, .name = "kb-delete-entry", .binding = "Shift+Delete", .comment = "Delete entry from history" },
67  { .id = MODE_NEXT, .name = "kb-mode-next", .binding = "Shift+Right,Control+Tab", .comment = "Switch to the next mode." },
68  { .id = MODE_PREVIOUS, .name = "kb-mode-previous", .binding = "Shift+Left,Control+ISO_Left_Tab", .comment = "Switch to the previous mode." },
69  { .id = ROW_LEFT, .name = "kb-row-left", .binding = "Control+Page_Up", .comment = "Go to the previous column" },
70  { .id = ROW_RIGHT, .name = "kb-row-right", .binding = "Control+Page_Down", .comment = "Go to the next column" },
71  { .id = ROW_UP, .name = "kb-row-up", .binding = "Up,Control+p,ISO_Left_Tab", .comment = "Select previous entry" },
72  { .id = ROW_DOWN, .name = "kb-row-down", .binding = "Down,Control+n", .comment = "Select next entry" },
73  { .id = ROW_TAB, .name = "kb-row-tab", .binding = "Tab", .comment = "Go to next row, if one left, accept it, if no left next mode." },
74  { .id = PAGE_PREV, .name = "kb-page-prev", .binding = "Page_Up", .comment = "Go to the previous page" },
75  { .id = PAGE_NEXT, .name = "kb-page-next", .binding = "Page_Down", .comment = "Go to the next page" },
76  { .id = ROW_FIRST, .name = "kb-row-first", .binding = "Home,KP_Home", .comment = "Go to the first entry" },
77  { .id = ROW_LAST, .name = "kb-row-last", .binding = "End,KP_End", .comment = "Go to the last entry" },
78  { .id = ROW_SELECT, .name = "kb-row-select", .binding = "Control+space", .comment = "Set selected item as input text" },
79  { .id = SCREENSHOT, .name = "kb-screenshot", .binding = "Alt+S", .comment = "Take a screenshot of the rofi window" },
80  { .id = TOGGLE_CASE_SENSITIVITY, .name = "kb-toggle-case-sensitivity", .binding = "grave,dead_grave", .comment = "Toggle case sensitivity" },
81  { .id = TOGGLE_SORT, .name = "kb-toggle-sort", .binding = "Alt+grave", .comment = "Toggle sort" },
82  { .id = CANCEL, .name = "kb-cancel", .binding = "Escape,Control+g,Control+bracketleft", .comment = "Quit rofi" },
83  { .id = CUSTOM_1, .name = "kb-custom-1", .binding = "Alt+1", .comment = "Custom keybinding 1" },
84  { .id = CUSTOM_2, .name = "kb-custom-2", .binding = "Alt+2", .comment = "Custom keybinding 2" },
85  { .id = CUSTOM_3, .name = "kb-custom-3", .binding = "Alt+3", .comment = "Custom keybinding 3" },
86  { .id = CUSTOM_4, .name = "kb-custom-4", .binding = "Alt+4", .comment = "Custom keybinding 4" },
87  { .id = CUSTOM_5, .name = "kb-custom-5", .binding = "Alt+5", .comment = "Custom Keybinding 5" },
88  { .id = CUSTOM_6, .name = "kb-custom-6", .binding = "Alt+6", .comment = "Custom keybinding 6" },
89  { .id = CUSTOM_7, .name = "kb-custom-7", .binding = "Alt+7", .comment = "Custom Keybinding 7" },
90  { .id = CUSTOM_8, .name = "kb-custom-8", .binding = "Alt+8", .comment = "Custom keybinding 8" },
91  { .id = CUSTOM_9, .name = "kb-custom-9", .binding = "Alt+9", .comment = "Custom keybinding 9" },
92  { .id = CUSTOM_10, .name = "kb-custom-10", .binding = "Alt+0", .comment = "Custom keybinding 10" },
93  { .id = CUSTOM_11, .name = "kb-custom-11", .binding = "Alt+exclam", .comment = "Custom keybinding 11" },
94  { .id = CUSTOM_12, .name = "kb-custom-12", .binding = "Alt+at", .comment = "Custom keybinding 12" },
95  { .id = CUSTOM_13, .name = "kb-custom-13", .binding = "Alt+numbersign", .comment = "Csutom keybinding 13" },
96  { .id = CUSTOM_14, .name = "kb-custom-14", .binding = "Alt+dollar", .comment = "Custom keybinding 14" },
97  { .id = CUSTOM_15, .name = "kb-custom-15", .binding = "Alt+percent", .comment = "Custom keybinding 15" },
98  { .id = CUSTOM_16, .name = "kb-custom-16", .binding = "Alt+dead_circumflex", .comment = "Custom keybinding 16" },
99  { .id = CUSTOM_17, .name = "kb-custom-17", .binding = "Alt+ampersand", .comment = "Custom keybinding 17" },
100  { .id = CUSTOM_18, .name = "kb-custom-18", .binding = "Alt+asterisk", .comment = "Custom keybinding 18" },
101  { .id = CUSTOM_19, .name = "kb-custom-19", .binding = "Alt+parenleft", .comment = "Custom Keybinding 19" },
102  { .id = SELECT_ELEMENT_1, .name = "kb-select-1", .binding = "Super+1", .comment = "Select row 1" },
103  { .id = SELECT_ELEMENT_2, .name = "kb-select-2", .binding = "Super+2", .comment = "Select row 2" },
104  { .id = SELECT_ELEMENT_3, .name = "kb-select-3", .binding = "Super+3", .comment = "Select row 3" },
105  { .id = SELECT_ELEMENT_4, .name = "kb-select-4", .binding = "Super+4", .comment = "Select row 4" },
106  { .id = SELECT_ELEMENT_5, .name = "kb-select-5", .binding = "Super+5", .comment = "Select row 5" },
107  { .id = SELECT_ELEMENT_6, .name = "kb-select-6", .binding = "Super+6", .comment = "Select row 6" },
108  { .id = SELECT_ELEMENT_7, .name = "kb-select-7", .binding = "Super+7", .comment = "Select row 7" },
109  { .id = SELECT_ELEMENT_8, .name = "kb-select-8", .binding = "Super+8", .comment = "Select row 8" },
110  { .id = SELECT_ELEMENT_9, .name = "kb-select-9", .binding = "Super+9", .comment = "Select row 9" },
111  { .id = SELECT_ELEMENT_10, .name = "kb-select-10", .binding = "Super+0", .comment = "Select row 10" },
112 
113  /* Mouse-aware bindings */
114 
115  { .id = SCROLL_LEFT, .scope = SCOPE_MOUSE_LISTVIEW, .name = "ml-row-left", .binding = "ScrollLeft", .comment = "Go to the previous column" },
116  { .id = SCROLL_RIGHT, .scope = SCOPE_MOUSE_LISTVIEW, .name = "ml-row-right", .binding = "ScrollRight", .comment = "Go to the next column" },
117  { .id = SCROLL_UP, .scope = SCOPE_MOUSE_LISTVIEW, .name = "ml-row-up", .binding = "ScrollUp", .comment = "Select previous entry" },
118  { .id = SCROLL_DOWN, .scope = SCOPE_MOUSE_LISTVIEW, .name = "ml-row-down", .binding = "ScrollDown", .comment = "Select next entry" },
119 
120  { .id = SELECT_HOVERED_ENTRY, .scope = SCOPE_MOUSE_LISTVIEW_ELEMENT, .name = "me-select-entry", .binding = "MousePrimary", .comment = "Select hovered row" },
121  { .id = ACCEPT_HOVERED_ENTRY, .scope = SCOPE_MOUSE_LISTVIEW_ELEMENT, .name = "me-accept-entry", .binding = "MouseDPrimary", .comment = "Accept hovered row" },
122  { .id = ACCEPT_HOVERED_CUSTOM, .scope = SCOPE_MOUSE_LISTVIEW_ELEMENT, .name = "me-accept-custom", .binding = "Control+MouseDPrimary", .comment = "Accept hovered row with custom action" },
123 };
124 
125 static const gchar *mouse_default_bindings[] = {
126  [MOUSE_CLICK_DOWN] = "MousePrimary",
127  [MOUSE_CLICK_UP] = "!MousePrimary",
128  [MOUSE_DCLICK_DOWN] = "MouseDPrimary",
129  [MOUSE_DCLICK_UP] = "!MouseDPrimary",
130 };
131 
132 void setup_abe ( void )
133 {
134  for ( gsize i = 0; i < G_N_ELEMENTS ( rofi_bindings ); ++i ) {
136  b->binding = g_strdup ( b->binding );
137  config_parser_add_option ( xrm_String, b->name, (void * *) &( b->binding ), b->comment );
138  }
139 }
140 
141 static gboolean binding_trigger_action ( guint64 scope, G_GNUC_UNUSED gpointer target, gpointer user_data )
142 {
143  return rofi_view_trigger_action ( rofi_view_get_active (), scope, GPOINTER_TO_UINT ( user_data ) );
144 }
145 
146 gboolean parse_keys_abe ( NkBindings *bindings )
147 {
148  GError *error = NULL;
149  GString *error_msg = g_string_new ( "" );
150  for ( gsize i = 0; i < G_N_ELEMENTS ( rofi_bindings ); ++i ) {
152  char *keystr = g_strdup ( b->binding );
153  char *sp = NULL;
154 
155  // Iter over bindings.
156  const char *const sep = ",";
157  for ( char *entry = strtok_r ( keystr, sep, &sp ); entry != NULL; entry = strtok_r ( NULL, sep, &sp ) ) {
158  if ( !nk_bindings_add_binding ( bindings, b->scope, entry, binding_trigger_action, GUINT_TO_POINTER ( b->id ), NULL, &error ) ) {
159  char *str = g_markup_printf_escaped ( "Failed to set binding <i>%s</i> for: <i>%s (%s)</i>:\n\t<span size=\"smaller\" style=\"italic\">%s</span>\n",
160  b->binding, b->comment, b->name, error->message );
161  g_string_append ( error_msg, str );
162  g_free ( str );
163  g_clear_error ( &error );
164  }
165  }
166 
167  g_free ( keystr );
168  }
169  if ( error_msg->len > 0 ) {
170  //rofi_view_error_dialog ( error_msg->str, TRUE );
171  rofi_add_error_message ( error_msg );
172 // g_string_free ( error_msg, TRUE );
173  return FALSE;
174  }
175 
176  for ( gsize i = SCOPE_MIN_FIXED; i <= SCOPE_MAX_FIXED; ++i ) {
177  for ( gsize j = 1; j < G_N_ELEMENTS ( mouse_default_bindings ); ++j ) {
178  nk_bindings_add_binding ( bindings, i, mouse_default_bindings[j], binding_trigger_action, GSIZE_TO_POINTER ( j ), NULL, NULL );
179  }
180  }
181 
182  g_string_free ( error_msg, TRUE );
183  return TRUE;
184 }
static const gchar * mouse_default_bindings[]
Definition: keyb.c:125
NkBindings * bindings
Definition: rofi.c:106
guint scope
Definition: keyb.c:37
Definition: keyb.h:103
Definition: keyb.h:96
Definition: keyb.h:105
Definition: keyb.h:99
Definition: keyb.h:95
Definition: keyb.h:98
char * name
Definition: keyb.c:38
Definition: keyb.h:114
Definition: keyb.h:106
RofiViewState * rofi_view_get_active(void)
Definition: view.c:446
Definition: keyb.h:66
#define SCOPE_MIN_FIXED
Definition: keyb.h:45
#define SCOPE_MAX_FIXED
Definition: keyb.h:49
Definition: keyb.h:108
Definition: keyb.h:110
Definition: keyb.h:112
gboolean rofi_view_trigger_action(RofiViewState *state, BindingsScope scope, guint action)
Definition: view.c:1337
void rofi_add_error_message(GString *str)
Definition: rofi.c:86
Definition: keyb.h:91
static gboolean binding_trigger_action(guint64 scope, G_GNUC_UNUSED gpointer target, gpointer user_data)
Definition: keyb.c:141
char * binding
Definition: keyb.c:39
gboolean parse_keys_abe(NkBindings *bindings)
Definition: keyb.c:146
void setup_abe(void)
Definition: keyb.c:132
void config_parser_add_option(XrmOptionType type, const char *key, void **value, const char *comment)
Definition: xrmoptions.c:220
char * comment
Definition: keyb.c:40
Definition: keyb.h:107
Definition: keyb.h:97
Definition: keyb.h:111
Definition: keyb.h:109
Definition: keyb.h:113
ActionBindingEntry rofi_bindings[]
Definition: keyb.c:46