5 #include <glib/gi18n-lib.h>
15 #if GTK_MAJOR_VERSION == 2
24 typedef struct gdk_keyboard_button_s
28 } gdk_keyboard_button_t;
30 static const gdk_keyboard_button_t gdk_keyboard_buttons[] = {
31 {
"BackSpace", GDK_KEY_BackSpace},
32 {
"CapsLock", GDK_KEY_Caps_Lock},
33 {
"Down", GDK_KEY_Down},
34 {
"Esc", GDK_KEY_Escape},
47 {
"Left", GDK_KEY_Left},
48 {
"PageDown", GDK_KEY_Page_Down},
49 {
"PageUp", GDK_KEY_Page_Up},
50 {
"Return", GDK_KEY_Return},
51 {
"Right", GDK_KEY_Right},
52 {
"Space", GDK_KEY_space},
53 {
"Super", GDK_KEY_Super_L},
55 {
"ShiftTab", GDK_KEY_ISO_Left_Tab},
59 typedef struct gdk_mouse_button_s
65 static const gdk_mouse_button_t gdk_mouse_buttons[] = {
77 typedef struct event_type_s
83 static const event_type_t event_types[] = {
91 typedef struct mouse_event_s
97 static const mouse_event_t mouse_events[] = {
106 if (number_of_arguments < ((unmap ==
true) ? 1 : 2)) {
115 int shortcut_mask = 0;
116 int shortcut_key = 0;
117 int shortcut_mouse_button = 0;
119 char* shortcut_argument_data = NULL;
120 int shortcut_argument_n = 0;
121 char* shortcut_buffer_command = NULL;
124 bool mouse_event =
false;
126 size_t current_command = 0;
128 size_t tmp_length = strlen(tmp);
131 bool is_mode =
false;
132 if (tmp_length >= 3 && tmp[0] ==
'[' && tmp[tmp_length - 1] ==
']') {
133 char* tmp_inner = g_strndup(tmp + 1, tmp_length - 2);
136 if (!g_strcmp0(tmp_inner, mode->name)) {
137 shortcut_mode = mode->index;
143 if (is_mode ==
false) {
152 unsigned int limit = (unmap ==
true) ? 1 : 2;
153 if (number_of_arguments < limit) {
154 girara_warning(
"Invalid number of arguments passed: %zu instead of at least %u", number_of_arguments, limit);
156 _(
"Invalid number of arguments passed: %zu instead of at least %u"), number_of_arguments, limit);
160 if (is_mode ==
true) {
162 tmp_length = strlen(tmp);
166 if (tmp_length >= 3 && tmp[0] ==
'<' && tmp[tmp_length - 1] ==
'>') {
167 tmp = g_strndup(tmp + 1, tmp_length - 2);
168 tmp_length = strlen(tmp);
171 if (strchr(tmp,
'-') != NULL && tmp_length > 2) {
174 shortcut_mask = GDK_SHIFT_MASK;
177 shortcut_mask = GDK_MOD1_MASK;
180 shortcut_mask = GDK_CONTROL_MASK;
190 if (tmp_length == 3) {
191 shortcut_key = tmp[2];
195 for (
unsigned int i = 0; i <
LENGTH(gdk_keyboard_buttons); i++) {
196 if (g_strcmp0(tmp + 2, gdk_keyboard_buttons[i].identifier) == 0) {
197 shortcut_key = gdk_keyboard_buttons[i].keyval;
203 for (
unsigned int i = 0; i <
LENGTH(gdk_mouse_buttons); i++) {
204 if (!g_strcmp0(tmp + 2, gdk_mouse_buttons[i].identifier)) {
205 shortcut_mouse_button = gdk_mouse_buttons[i].button;
212 for (
unsigned int i = 0; i <
LENGTH(event_types); i++) {
213 if (!g_strcmp0(tmp + 2, event_types[i].identifier)) {
214 event_type = event_types[i].event;
221 if (found ==
false) {
231 for (
unsigned int i = 0; i <
LENGTH(gdk_keyboard_buttons); i++) {
232 if (g_strcmp0(tmp, gdk_keyboard_buttons[i].identifier) == 0) {
233 shortcut_key = gdk_keyboard_buttons[i].keyval;
239 for (
unsigned int i = 0; i <
LENGTH(gdk_mouse_buttons); i++) {
240 if (!g_strcmp0(tmp, gdk_mouse_buttons[i].identifier)) {
241 shortcut_mouse_button = gdk_mouse_buttons[i].button;
248 for (
unsigned int i = 0; i <
LENGTH(event_types); i++) {
249 if (!g_strcmp0(tmp, event_types[i].identifier)) {
250 event_type = event_types[i].event;
257 if (found ==
false) {
267 }
else if (tmp_length == 1) {
268 shortcut_key = tmp[0];
271 shortcut_buffer_command = g_strdup(tmp);
275 bool mouse_mode =
false;
276 if (unmap ==
false) {
277 if (++current_command < number_of_arguments) {
279 tmp_length = strlen(tmp);
281 if (tmp_length >= 3 && tmp[0] ==
'[' && tmp[tmp_length - 1] ==
']') {
283 if (mouse_event ==
false) {
288 char* tmp_inner = g_strndup(tmp + 1, tmp_length - 2);
291 for (
unsigned int i = 0; i <
LENGTH(mouse_events); i++) {
292 if (!g_strcmp0(tmp_inner, mouse_events[i].identifier)) {
293 event_type = mouse_events[i].event;
299 if (found ==
false) {
300 girara_warning(
"Invalid mouse event mode has been passed: %s", tmp_inner);
313 if (unmap ==
false) {
314 limit = (mouse_mode ==
true) ? 3 : 2;
315 if (number_of_arguments < limit) {
316 girara_warning(
"Invalid number of arguments passed: %zu instead of at least %u", number_of_arguments, limit);
318 _(
"Invalid number of arguments passed: %zu instead of at least %u"), number_of_arguments, limit);
322 if (mouse_mode ==
true) {
328 if (unmap ==
false) {
329 bool found_mapping =
false;
330 GIRARA_LIST_FOREACH(session->config.shortcut_mappings, girara_shortcut_mapping_t*, iter, mapping)
331 if (!g_strcmp0(tmp, mapping->identifier)) {
332 shortcut_function = mapping->function;
333 found_mapping =
true;
338 if (found_mapping ==
false) {
341 if (shortcut_buffer_command) {
342 g_free(shortcut_buffer_command);
349 if (unmap ==
false) {
350 if (++current_command < number_of_arguments) {
353 GIRARA_LIST_FOREACH(session->config.argument_mappings, girara_argument_mapping_t*, iter, mapping)
354 if (!g_strcmp0(tmp, mapping->identifier)) {
355 shortcut_argument_n = mapping->value;
361 if (shortcut_argument_n == 0) {
362 shortcut_argument_data = tmp;
365 }
else if (++current_command < number_of_arguments) {
367 shortcut_argument_data = tmp;
372 if (mouse_event ==
false) {
375 shortcut_buffer_command, shortcut_mode);
378 shortcut_function, shortcut_mode, shortcut_argument_n, shortcut_argument_data);
386 shortcut_function, shortcut_mode, event_type, shortcut_argument_n, shortcut_argument_data);
390 if (shortcut_buffer_command) {
391 g_free(shortcut_buffer_command);
426 if (number_of_arguments == 0) {
431 if (number_of_arguments > 2) {
444 if (setting == NULL) {
450 if (number_of_arguments == 1) {
499 if (g_strcmp0(value,
"false") == 0 || g_strcmp0(value,
"0") == 0) {
502 }
else if (g_strcmp0(value,
"true") == 0 || g_strcmp0(value,
"1") == 0) {
512 float f = strtof(value, NULL);
538 g_return_val_if_fail(session != NULL,
false);
539 g_return_val_if_fail(command != NULL,
false);
540 g_return_val_if_fail(
function != NULL,
false);
544 if (g_strcmp0(commands_it->command, command) == 0) {
545 g_free(commands_it->abbr);
546 g_free(commands_it->description);
548 commands_it->abbr = abbreviation ? g_strdup(abbreviation) : NULL;
549 commands_it->function =
function;
550 commands_it->completion = completion;
551 commands_it->description = description ? g_strdup(description) : NULL;
559 girara_command_t* new_command = g_slice_new(girara_command_t);
561 new_command->command = g_strdup(command);
562 new_command->abbr = abbreviation ? g_strdup(abbreviation) : NULL;
563 new_command->function =
function;
564 new_command->completion = completion;
565 new_command->description = description ? g_strdup(description) : NULL;
574 g_return_val_if_fail(session != NULL,
false);
575 g_return_val_if_fail(
function != NULL,
false);
577 girara_argument_t argument = {argument_n, argument_data};
580 GIRARA_LIST_FOREACH(session->bindings.special_commands, girara_special_command_t*, iter, scommand_it)
581 if (scommand_it->identifier == identifier) {
582 scommand_it->function =
function;
583 scommand_it->always = always;
584 scommand_it->argument = argument;
591 girara_special_command_t* special_command = g_slice_new(girara_special_command_t);
593 special_command->identifier = identifier;
594 special_command->function =
function;
595 special_command->always = always;
596 special_command->argument = argument;
606 if (special_command == NULL) {
609 g_slice_free(girara_special_command_t, special_command);
615 if (command == NULL) {
619 g_free(command->command);
620 g_free(command->abbr);
621 g_free(command->description);
622 g_slice_free(girara_command_t, command);
630 if (cmd == NULL || strlen(cmd) == 0) {
637 GString* command = g_string_new(cmd);
641 g_string_append_c(command,
' ');
642 char* tmp = g_shell_quote(value);
643 g_string_append(command, tmp);
647 GError* error = NULL;
648 gboolean ret = g_spawn_command_line_async(command->str, &error);
655 g_string_free(command, TRUE);