Krolyxon
Commit 12efa0e

Branches

Refs →

nsxiv12efa0e

Add ability to bind arbitrary functions.

Before all the predated commands where kept in an array and their
indexes were used in bindings. This meant that users couldn't add their
own functions from the config file. Now key/mouse bindings have been
changed to to store the function ptr (wrapped in a cmd_t struct to also
store the mode) directly instead.

General cleanup done in this commit:
Defined `MODE_ALL` instead of using magic number.

For example, suppose one had bindings like:
{ 0,                   XK_q,             g_quit,                     None },
{ ShitMask,            XK_q,             {quit_err},                 None }
{ ControlMask,         XK_q,             {quit_err, .mode=MODE_IMAGE}, None }

The existing binding `q` has been left unchanged and is defined the same
way. However, the new hypothetical binding `Shift-q` can be used to call
the custom function quit_err in any mode (default). `Ctrl-q` on the
other hand will be called only on image mode.

Closes #50
Author
Arthur Williams
Date
2021-09-18 12:27 -0700
Committer
N-R-K · 2021-10-13 06:05 +0600
Parent
5c6947c
Commit
12efa0e3b429675047cb2900d49e1f38afeb650b

+96 −70

6 files changed

  1. M Makefile +1 −1
  2. M commands.c +0 −12
  3. A commands.h +83 −0
  4. D commands.lst +0 −36
  5. M main.c +8 −7
  6. M nsxiv.h +4 −14
M Makefile View
@@ -64,7 +64,7 @@ nsxiv: $(OBJS)
6464 @echo "CC $@"
6565 $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
6666
67−$(OBJS): Makefile nsxiv.h commands.lst config.h
67+$(OBJS): Makefile nsxiv.h config.h
6868 options.o: version.h
6969 window.o: icon/data.h
7070
M commands.c View
@@ -436,15 +436,3 @@ bool ct_reload_all(arg_t _)
436436 tns.dirty = true;
437437 return true;
438438 }
439−
440−
441−#undef G_CMD
442−#define G_CMD(c) { -1, cg_##c },
443−#undef I_CMD
444−#define I_CMD(c) { MODE_IMAGE, ci_##c },
445−#undef T_CMD
446−#define T_CMD(c) { MODE_THUMB, ct_##c },
447−
448−const cmd_t cmds[CMD_COUNT] = {
449−#include "commands.lst"
450−};
A commands.h View
@@ -0,0 +1,83 @@
1+#ifndef COMMANDS_H
2+#define COMMANDS_H
3+#include <stdbool.h>
4+
5+/* global */
6+bool cg_change_gamma();
7+bool cg_first();
8+bool cg_mark_range();
9+bool cg_n_or_last();
10+bool cg_navigate_marked();
11+bool cg_prefix_external();
12+bool cg_quit();
13+bool cg_reload_image();
14+bool cg_remove_image();
15+bool cg_reverse_marks();
16+bool cg_scroll_screen();
17+bool cg_switch_mode();
18+bool cg_toggle_bar();
19+bool cg_toggle_fullscreen();
20+bool cg_toggle_image_mark();
21+bool cg_unmark_all();
22+bool cg_zoom();
23+/* image mode */
24+bool ci_alternate();
25+bool ci_cursor_navigate();
26+bool ci_drag();
27+bool ci_fit_to_win();
28+bool ci_flip();
29+bool ci_navigate();
30+bool ci_navigate_frame();
31+bool ci_rotate();
32+bool ci_scroll();
33+bool ci_scroll_to_edge();
34+bool ci_set_zoom();
35+bool ci_slideshow();
36+bool ci_toggle_alpha();
37+bool ci_toggle_animation();
38+bool ci_toggle_antialias();
39+/* thumbnails mode */
40+bool ct_move_sel();
41+bool ct_reload_all();
42+
43+/* global */
44+#define g_change_gamma { cg_change_gamma, MODE_ALL }
45+#define g_first { cg_first, MODE_ALL }
46+#define g_mark_range { cg_mark_range, MODE_ALL }
47+#define g_n_or_last { cg_n_or_last, MODE_ALL }
48+#define g_navigate_marked { cg_navigate_marked, MODE_ALL }
49+#define g_prefix_external { cg_prefix_external, MODE_ALL }
50+#define g_quit { cg_quit, MODE_ALL }
51+#define g_reload_image { cg_reload_image, MODE_ALL }
52+#define g_remove_image { cg_remove_image, MODE_ALL }
53+#define g_reverse_marks { cg_reverse_marks, MODE_ALL }
54+#define g_scroll_screen { cg_scroll_screen, MODE_ALL }
55+#define g_switch_mode { cg_switch_mode, MODE_ALL }
56+#define g_toggle_bar { cg_toggle_bar, MODE_ALL }
57+#define g_toggle_fullscreen { cg_toggle_fullscreen, MODE_ALL }
58+#define g_toggle_image_mark { cg_toggle_image_mark, MODE_ALL }
59+#define g_unmark_all { cg_unmark_all, MODE_ALL }
60+#define g_zoom { cg_zoom, MODE_ALL }
61+
62+/* image mode */
63+#define i_alternate { ci_alternate, MODE_IMAGE }
64+#define i_cursor_navigate { ci_cursor_navigate, MODE_IMAGE }
65+#define i_drag { ci_drag, MODE_IMAGE }
66+#define i_fit_to_win { ci_fit_to_win, MODE_IMAGE }
67+#define i_flip { ci_flip, MODE_IMAGE }
68+#define i_navigate { ci_navigate, MODE_IMAGE }
69+#define i_navigate_frame { ci_navigate_frame, MODE_IMAGE }
70+#define i_rotate { ci_rotate, MODE_IMAGE }
71+#define i_scroll { ci_scroll, MODE_IMAGE }
72+#define i_scroll_to_edge { ci_scroll_to_edge, MODE_IMAGE }
73+#define i_set_zoom { ci_set_zoom, MODE_IMAGE }
74+#define i_slideshow { ci_slideshow, MODE_IMAGE }
75+#define i_toggle_alpha { ci_toggle_alpha, MODE_IMAGE }
76+#define i_toggle_animation { ci_toggle_animation, MODE_IMAGE }
77+#define i_toggle_antialias { ci_toggle_antialias, MODE_IMAGE }
78+
79+/* thumbnails mode */
80+#define t_move_sel { ct_move_sel, MODE_THUMB }
81+#define t_reload_all { ct_reload_all, MODE_THUMB }
82+
83+#endif
D commands.lst View
@@ -1,36 +0,0 @@
1−G_CMD(quit)
2−G_CMD(switch_mode)
3−G_CMD(toggle_fullscreen)
4−G_CMD(toggle_bar)
5−G_CMD(prefix_external)
6−G_CMD(reload_image)
7−G_CMD(remove_image)
8−G_CMD(first)
9−G_CMD(n_or_last)
10−G_CMD(scroll_screen)
11−G_CMD(zoom)
12−G_CMD(toggle_image_mark)
13−G_CMD(reverse_marks)
14−G_CMD(mark_range)
15−G_CMD(unmark_all)
16−G_CMD(navigate_marked)
17−G_CMD(change_gamma)
18−
19−I_CMD(navigate)
20−I_CMD(cursor_navigate)
21−I_CMD(alternate)
22−I_CMD(navigate_frame)
23−I_CMD(toggle_animation)
24−I_CMD(scroll)
25−I_CMD(scroll_to_edge)
26−I_CMD(drag)
27−I_CMD(set_zoom)
28−I_CMD(fit_to_win)
29−I_CMD(rotate)
30−I_CMD(flip)
31−I_CMD(toggle_antialias)
32−I_CMD(toggle_alpha)
33−I_CMD(slideshow)
34−
35−T_CMD(move_sel)
36−T_CMD(reload_all)
M main.c View
@@ -17,6 +17,7 @@
1717 */
1818
1919 #include "nsxiv.h"
20+#include "commands.h"
2021 #define _MAPPINGS_CONFIG
2122 #include "config.h"
2223
@@ -613,10 +614,10 @@ void on_keypress(XKeyEvent *kev)
613614 } else for (i = 0; i < ARRLEN(keys); i++) {
614615 if (keys[i].ksym == ksym &&
615616 MODMASK(keys[i].mask | sh) == MODMASK(kev->state) &&
616− keys[i].cmd >= 0 && keys[i].cmd < CMD_COUNT &&
617− (cmds[keys[i].cmd].mode < 0 || cmds[keys[i].cmd].mode == mode))
617+ keys[i].cmd.func &&
618+ (keys[i].cmd.mode == MODE_ALL || keys[i].cmd.mode == mode))
618619 {
619− if (cmds[keys[i].cmd].func(keys[i].arg))
620+ if (keys[i].cmd.func(keys[i].arg))
620621 dirty = true;
621622 }
622623 }
@@ -638,10 +639,10 @@ void on_buttonpress(XButtonEvent *bev)
638639 for (i = 0; i < ARRLEN(buttons); i++) {
639640 if (buttons[i].button == bev->button &&
640641 MODMASK(buttons[i].mask) == MODMASK(bev->state) &&
641− buttons[i].cmd >= 0 && buttons[i].cmd < CMD_COUNT &&
642− (cmds[buttons[i].cmd].mode < 0 || cmds[buttons[i].cmd].mode == mode))
642+ buttons[i].cmd.func &&
643+ (buttons[i].cmd.mode == MODE_ALL || buttons[i].cmd.mode == mode))
643644 {
644− if (cmds[buttons[i].cmd].func(buttons[i].arg))
645+ if (buttons[i].cmd.func(buttons[i].arg))
645646 dirty = true;
646647 }
647648 }
@@ -778,7 +779,7 @@ void run(void)
778779 break;
779780 case ClientMessage:
780781 if ((Atom) ev.xclient.data.l[0] == atoms[ATOM_WM_DELETE_WINDOW])
781− cmds[g_quit].func(0);
782+ cg_quit();
782783 break;
783784 case DestroyNotify:
784785 exit(EXIT_FAILURE);
M nsxiv.h View
@@ -60,6 +60,7 @@ typedef enum {
6060 } byteorder_t;
6161
6262 typedef enum {
63+ MODE_ALL,
6364 MODE_IMAGE,
6465 MODE_THUMB
6566 } appmode_t;
@@ -162,36 +163,25 @@ bool arl_handle(arl_t*);
162163 typedef int arg_t;
163164 typedef bool (*cmd_f)(arg_t);
164165
165−#define G_CMD(c) g_##c,
166−#define I_CMD(c) i_##c,
167−#define T_CMD(c) t_##c,
168−
169−typedef enum {
170−#include "commands.lst"
171− CMD_COUNT
172−} cmd_id_t;
173−
174166 typedef struct {
175− int mode;
176167 cmd_f func;
168+ appmode_t mode;
177169 } cmd_t;
178170
179171 typedef struct {
180172 unsigned int mask;
181173 KeySym ksym;
182− cmd_id_t cmd;
174+ cmd_t cmd;
183175 arg_t arg;
184176 } keymap_t;
185177
186178 typedef struct {
187179 unsigned int mask;
188180 unsigned int button;
189− cmd_id_t cmd;
181+ cmd_t cmd;
190182 arg_t arg;
191183 } button_t;
192184
193−extern const cmd_t cmds[CMD_COUNT];
194−
195185
196186 /* image.c */
197187