Krolyxon
Commit 1c68a34

Branches

Refs →

nsxiv1c68a34

Renamed key/mouse commands to indicate supported modes

Author
Bert
Date
2011-08-19 13:06 +0200
Parent
535c18a
Commit
1c68a34463f9eb94da9a8289b659ce02677b865f

+197 −189

3 files changed

  1. M config.h +92 −88
  2. M events.c +81 −78
  3. M events.h +24 −23
M config.h View
@@ -1,25 +1,29 @@
11 #ifdef _WINDOW_CONFIG
22
3−/* default window dimensions (overwritten via -g option): */
4−enum { WIN_WIDTH = 800, WIN_HEIGHT = 600 };
3+/* default window dimensions (overwritten via -g option): */
4+enum {
5+ WIN_WIDTH = 800,
6+ WIN_HEIGHT = 600
7+};
58
6−/* default color for window background: *
7− * (see X(7) "COLOR NAMES" section for valid values) */
9+/* default color for window background: */
810 static const char * const BG_COLOR = "#777777";
9−/* default color for thumbnail selection: */
11+/* default color for thumbnail selection: */
1012 static const char * const SEL_COLOR = "#DDDDDD";
13+/* (see X(7) section "COLOR NAMES" for valid values) */
1114
1215 #endif
1316 #ifdef _IMAGE_CONFIG
1417
15−/* how should images be scaled when they are loaded?: *
16− * (also controllable via -d/-s/-Z/-z options) *
17− * SCALE_DOWN: 100%, but fit large images into window, *
18− * SCALE_FIT: fit all images into window, *
19− * SCALE_ZOOM: use current zoom level, 100% at startup */
18+/* how should images be scaled when they are loaded?
19+ * (also controllable via -d/-s/-Z/-z options)
20+ * SCALE_DOWN: 100%, but fit large images into window,
21+ * SCALE_FIT: fit all images into window,
22+ * SCALE_ZOOM: use current zoom level, 100% at startup
23+ */
2024 static const scalemode_t SCALE_MODE = SCALE_DOWN;
2125
22−/* levels (percent) to use when zooming via '-' and '+': */
26+/* levels (percent) to use when zooming via '-' and '+': */
2327 static const float zoom_levels[] = {
2428 12.5, 25.0, 50.0, 75.0,
2529 100.0, 150.0, 200.0, 400.0, 800.0
@@ -28,92 +32,92 @@ static const float zoom_levels[] = {
2832 #endif
2933 #ifdef _THUMBS_CONFIG
3034
31−/* default dimension of thumbnails (width == height): */
35+/* default dimension of thumbnails (width == height): */
3236 enum { THUMB_SIZE = 60 };
3337
3438 #endif
3539 #ifdef _MAPPINGS_CONFIG
3640
37−/* keyboard mappings for image and thumbnail mode: */
41+/* keyboard mappings for image and thumbnail mode: */
3842 static const keymap_t keys[] = {
39− /* ctrl key function argument */
40− { False, XK_q, quit, (arg_t) None },
41− { False, XK_r, reload, (arg_t) None },
42− { False, XK_f, toggle_fullscreen, (arg_t) None },
43− { False, XK_a, toggle_antialias, (arg_t) None },
44− { False, XK_A, toggle_alpha, (arg_t) None },
45− { False, XK_Return, switch_mode, (arg_t) None },
46−
47− { False, XK_g, first, (arg_t) None },
48− { False, XK_G, last, (arg_t) None },
49− { False, XK_n, navigate, (arg_t) +1 },
50− { False, XK_space, navigate, (arg_t) +1 },
51− { False, XK_p, navigate, (arg_t) -1 },
52− { False, XK_BackSpace, navigate, (arg_t) -1 },
53− { False, XK_bracketright, navigate, (arg_t) +10 },
54− { False, XK_bracketleft, navigate, (arg_t) -10 },
55−
56− { False, XK_D, remove_image, (arg_t) None },
57−
58− { False, XK_h, move, (arg_t) DIR_LEFT },
59− { False, XK_Left, move, (arg_t) DIR_LEFT },
60− { False, XK_j, move, (arg_t) DIR_DOWN },
61− { False, XK_Down, move, (arg_t) DIR_DOWN },
62− { False, XK_k, move, (arg_t) DIR_UP },
63− { False, XK_Up, move, (arg_t) DIR_UP },
64− { False, XK_l, move, (arg_t) DIR_RIGHT },
65− { False, XK_Right, move, (arg_t) DIR_RIGHT },
66−
67− { True, XK_h, pan_screen, (arg_t) DIR_LEFT },
68− { True, XK_Left, pan_screen, (arg_t) DIR_LEFT },
69− { True, XK_j, pan_screen, (arg_t) DIR_DOWN },
70− { True, XK_Down, pan_screen, (arg_t) DIR_DOWN },
71− { True, XK_k, pan_screen, (arg_t) DIR_UP },
72− { True, XK_Up, pan_screen, (arg_t) DIR_UP },
73− { True, XK_l, pan_screen, (arg_t) DIR_RIGHT },
74− { True, XK_Right, pan_screen, (arg_t) DIR_RIGHT },
75−
76− { False, XK_H, pan_edge, (arg_t) DIR_LEFT },
77− { False, XK_J, pan_edge, (arg_t) DIR_DOWN },
78− { False, XK_K, pan_edge, (arg_t) DIR_UP },
79− { False, XK_L, pan_edge, (arg_t) DIR_RIGHT },
80−
81− { False, XK_plus, zoom, (arg_t) +1 },
82− { False, XK_equal, zoom, (arg_t) +1 },
83− { False, XK_KP_Add, zoom, (arg_t) +1 },
84− { False, XK_minus, zoom, (arg_t) -1 },
85− { False, XK_KP_Subtract, zoom, (arg_t) -1 },
86− { False, XK_0, zoom, (arg_t) None },
87− { False, XK_KP_0, zoom, (arg_t) None },
88− { False, XK_w, fit_to_win, (arg_t) None },
89− { False, XK_W, fit_to_img, (arg_t) None },
90−
91− { False, XK_less, rotate, (arg_t) DIR_LEFT },
92− { False, XK_greater, rotate, (arg_t) DIR_RIGHT },
93−
94− /* open the current image with given program: */
95− { True, XK_g, open_with, (arg_t) "gimp" },
96−
97− /* run shell command line on the current file,
98− * '#' is replaced by filename: */
99− { True, XK_less, run_command, (arg_t) "mogrify -rotate -90 #" },
100− { True, XK_greater, run_command, (arg_t) "mogrify -rotate +90 #" },
101− { True, XK_comma, run_command, (arg_t) "jpegtran -rotate 270 -copy all -outfile # #" },
102− { True, XK_period, run_command, (arg_t) "jpegtran -rotate 90 -copy all -outfile # #" },
43+ /* ctrl key function argument */
44+ { False, XK_q, it_quit, (arg_t) None },
45+ { False, XK_Return, it_switch_mode, (arg_t) None },
46+ { False, XK_f, it_toggle_fullscreen, (arg_t) None },
47+
48+ { False, XK_r, it_reload_image, (arg_t) None },
49+ { False, XK_D, it_remove_image, (arg_t) None },
50+
51+ { False, XK_n, i_navigate, (arg_t) +1 },
52+ { False, XK_space, i_navigate, (arg_t) +1 },
53+ { False, XK_p, i_navigate, (arg_t) -1 },
54+ { False, XK_BackSpace, i_navigate, (arg_t) -1 },
55+ { False, XK_bracketright, i_navigate, (arg_t) +10 },
56+ { False, XK_bracketleft, i_navigate, (arg_t) -10 },
57+ { False, XK_g, it_first, (arg_t) None },
58+ { False, XK_G, it_last, (arg_t) None },
59+
60+ { False, XK_h, it_move, (arg_t) DIR_LEFT },
61+ { False, XK_Left, it_move, (arg_t) DIR_LEFT },
62+ { False, XK_j, it_move, (arg_t) DIR_DOWN },
63+ { False, XK_Down, it_move, (arg_t) DIR_DOWN },
64+ { False, XK_k, it_move, (arg_t) DIR_UP },
65+ { False, XK_Up, it_move, (arg_t) DIR_UP },
66+ { False, XK_l, it_move, (arg_t) DIR_RIGHT },
67+ { False, XK_Right, it_move, (arg_t) DIR_RIGHT },
68+
69+ { True, XK_h, i_pan_screen, (arg_t) DIR_LEFT },
70+ { True, XK_Left, i_pan_screen, (arg_t) DIR_LEFT },
71+ { True, XK_j, i_pan_screen, (arg_t) DIR_DOWN },
72+ { True, XK_Down, i_pan_screen, (arg_t) DIR_DOWN },
73+ { True, XK_k, i_pan_screen, (arg_t) DIR_UP },
74+ { True, XK_Up, i_pan_screen, (arg_t) DIR_UP },
75+ { True, XK_l, i_pan_screen, (arg_t) DIR_RIGHT },
76+ { True, XK_Right, i_pan_screen, (arg_t) DIR_RIGHT },
77+
78+ { False, XK_H, i_pan_edge, (arg_t) DIR_LEFT },
79+ { False, XK_J, i_pan_edge, (arg_t) DIR_DOWN },
80+ { False, XK_K, i_pan_edge, (arg_t) DIR_UP },
81+ { False, XK_L, i_pan_edge, (arg_t) DIR_RIGHT },
82+
83+ { False, XK_plus, i_zoom, (arg_t) +1 },
84+ { False, XK_equal, i_zoom, (arg_t) +1 },
85+ { False, XK_KP_Add, i_zoom, (arg_t) +1 },
86+ { False, XK_minus, i_zoom, (arg_t) -1 },
87+ { False, XK_KP_Subtract, i_zoom, (arg_t) -1 },
88+ { False, XK_0, i_zoom, (arg_t) None },
89+ { False, XK_KP_0, i_zoom, (arg_t) None },
90+ { False, XK_w, i_fit_to_win, (arg_t) None },
91+ { False, XK_W, i_fit_to_img, (arg_t) None },
92+
93+ { False, XK_less, i_rotate, (arg_t) DIR_LEFT },
94+ { False, XK_greater, i_rotate, (arg_t) DIR_RIGHT },
95+
96+ { False, XK_a, i_toggle_antialias, (arg_t) None },
97+ { False, XK_A, i_toggle_alpha, (arg_t) None },
98+
99+ /* open current image with given program: */
100+ { True, XK_g, it_open_with, (arg_t) "gimp" },
101+
102+ /* run shell command line on current file ('#' is replaced by file path: */
103+ { True, XK_less, it_shell_cmd, (arg_t) "mogrify -rotate -90 #" },
104+ { True, XK_greater, it_shell_cmd, (arg_t) "mogrify -rotate +90 #" },
105+ { True, XK_comma, it_shell_cmd, (arg_t) "jpegtran -rotate 270 -copy all -outfile # #" },
106+ { True, XK_period, it_shell_cmd, (arg_t) "jpegtran -rotate 90 -copy all -outfile # #" },
103107 };
104108
105−/* mouse button mappings for image mode: */
109+/* mouse button mappings for image mode: */
106110 static const button_t buttons[] = {
107− /* ctrl shift button function argument */
108− { False, False, Button1, navigate, (arg_t) +1 },
109− { False, False, Button3, navigate, (arg_t) -1 },
110− { False, False, Button2, drag, (arg_t) None },
111− { False, False, Button4, move, (arg_t) DIR_UP },
112− { False, False, Button5, move, (arg_t) DIR_DOWN },
113− { False, True, Button4, move, (arg_t) DIR_LEFT },
114− { False, True, Button5, move, (arg_t) DIR_RIGHT },
115− { True, False, Button4, zoom, (arg_t) +1 },
116− { True, False, Button5, zoom, (arg_t) -1 },
111+ /* ctrl shift button function argument */
112+ { False, False, Button1, i_navigate, (arg_t) +1 },
113+ { False, False, Button3, i_navigate, (arg_t) -1 },
114+ { False, False, Button2, i_drag, (arg_t) None },
115+ { False, False, Button4, it_move, (arg_t) DIR_UP },
116+ { False, False, Button5, it_move, (arg_t) DIR_DOWN },
117+ { False, True, Button4, it_move, (arg_t) DIR_LEFT },
118+ { False, True, Button5, it_move, (arg_t) DIR_RIGHT },
119+ { True, False, Button4, i_zoom, (arg_t) +1 },
120+ { True, False, Button5, i_zoom, (arg_t) -1 },
117121 };
118122
119123 #endif
M events.c View
@@ -93,7 +93,7 @@ void on_keypress(XKeyEvent *kev) {
9393
9494 for (i = 0; i < LEN(keys); i++) {
9595 if (keys[i].ksym == ksym && keymask(&keys[i], kev->state)) {
96− if (keys[i].handler && keys[i].handler(keys[i].arg))
96+ if (keys[i].cmd && keys[i].cmd(keys[i].arg))
9797 redraw();
9898 return;
9999 }
@@ -114,7 +114,7 @@ void on_buttonpress(XButtonEvent *bev) {
114114 if (buttons[i].button == bev->button &&
115115 buttonmask(&buttons[i], bev->state))
116116 {
117− if (buttons[i].handler && buttons[i].handler(buttons[i].arg))
117+ if (buttons[i].cmd && buttons[i].cmd(buttons[i].arg))
118118 redraw();
119119 return;
120120 }
@@ -241,23 +241,32 @@ void run() {
241241 }
242242
243243
244−/* handler functions for key and button mappings: */
244+/* command functions for key and button mappings: */
245245
246−int quit(arg_t a) {
246+int it_quit(arg_t a) {
247247 cleanup();
248248 exit(0);
249249 }
250250
251−int reload(arg_t a) {
251+int it_switch_mode(arg_t a) {
252252 if (mode == MODE_NORMAL) {
253− load_image(fileidx);
254− return 1;
253+ if (!tns.thumbs)
254+ tns_init(&tns, filecnt);
255+ img_close(&img, 0);
256+ win_set_cursor(&win, CURSOR_ARROW);
257+ timo_cursor = 0;
258+ tns.sel = fileidx;
259+ tns.dirty = 1;
260+ mode = MODE_THUMBS;
255261 } else {
256− return 0;
262+ timo_cursor = TO_CURSOR_HIDE;
263+ load_image(tns.sel);
264+ mode = MODE_NORMAL;
257265 }
266+ return 1;
258267 }
259268
260−int toggle_fullscreen(arg_t a) {
269+int it_toggle_fullscreen(arg_t a) {
261270 win_toggle_fullscreen(&win);
262271 if (mode == MODE_NORMAL)
263272 img.checkpan = 1;
@@ -267,43 +276,35 @@ int toggle_fullscreen(arg_t a) {
267276 return 0;
268277 }
269278
270−int toggle_antialias(arg_t a) {
279+int it_reload_image(arg_t a) {
271280 if (mode == MODE_NORMAL) {
272− img_toggle_antialias(&img);
273− return 1;
274− } else {
275− return 0;
281+ load_image(fileidx);
282+ } else if (!tns_load(&tns, tns.sel, &files[tns.sel], 0)) {
283+ remove_file(tns.sel, 0);
284+ tns.dirty = 1;
285+ if (tns.sel >= tns.cnt)
286+ tns.sel = tns.cnt - 1;
276287 }
288+ return 1;
277289 }
278290
279−int toggle_alpha(arg_t a) {
291+int it_remove_image(arg_t a) {
280292 if (mode == MODE_NORMAL) {
281− img.alpha ^= 1;
293+ remove_file(fileidx, 1);
294+ load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
282295 return 1;
283− } else {
284− return 0;
285− }
286−}
287−
288−int switch_mode(arg_t a) {
289− if (mode == MODE_NORMAL) {
290− if (!tns.thumbs)
291− tns_init(&tns, filecnt);
292− img_close(&img, 0);
293− win_set_cursor(&win, CURSOR_ARROW);
294− timo_cursor = 0;
295− tns.sel = fileidx;
296+ } else if (tns.sel < tns.cnt) {
297+ remove_file(tns.sel, 1);
296298 tns.dirty = 1;
297− mode = MODE_THUMBS;
299+ if (tns.sel >= tns.cnt)
300+ tns.sel = tns.cnt - 1;
301+ return 1;
298302 } else {
299− timo_cursor = TO_CURSOR_HIDE;
300− load_image(tns.sel);
301− mode = MODE_NORMAL;
303+ return 0;
302304 }
303− return 1;
304305 }
305306
306−int navigate(arg_t a) {
307+int i_navigate(arg_t a) {
307308 int n = (int) a;
308309
309310 if (mode == MODE_NORMAL) {
@@ -321,7 +322,7 @@ int navigate(arg_t a) {
321322 return 0;
322323 }
323324
324−int first(arg_t a) {
325+int it_first(arg_t a) {
325326 if (mode == MODE_NORMAL && fileidx != 0) {
326327 load_image(0);
327328 return 1;
@@ -334,7 +335,7 @@ int first(arg_t a) {
334335 }
335336 }
336337
337−int last(arg_t a) {
338+int it_last(arg_t a) {
338339 if (mode == MODE_NORMAL && fileidx != filecnt - 1) {
339340 load_image(filecnt - 1);
340341 return 1;
@@ -347,23 +348,7 @@ int last(arg_t a) {
347348 }
348349 }
349350
350−int remove_image(arg_t a) {
351− if (mode == MODE_NORMAL) {
352− remove_file(fileidx, 1);
353− load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
354− return 1;
355− } else if (tns.sel < tns.cnt) {
356− remove_file(tns.sel, 1);
357− tns.dirty = 1;
358− if (tns.sel >= tns.cnt)
359− tns.sel = tns.cnt - 1;
360− return 1;
361− } else {
362− return 0;
363− }
364−}
365−
366−int move(arg_t a) {
351+int it_move(arg_t a) {
367352 direction_t dir = (direction_t) a;
368353
369354 if (mode == MODE_NORMAL)
@@ -372,7 +357,7 @@ int move(arg_t a) {
372357 return tns_move_selection(&tns, &win, dir);
373358 }
374359
375−int pan_screen(arg_t a) {
360+int i_pan_screen(arg_t a) {
376361 direction_t dir = (direction_t) a;
377362
378363 if (mode == MODE_NORMAL)
@@ -381,7 +366,7 @@ int pan_screen(arg_t a) {
381366 return 0;
382367 }
383368
384−int pan_edge(arg_t a) {
369+int i_pan_edge(arg_t a) {
385370 direction_t dir = (direction_t) a;
386371
387372 if (mode == MODE_NORMAL)
@@ -390,12 +375,12 @@ int pan_edge(arg_t a) {
390375 return 0;
391376 }
392377
393−/* Xlib helper function for drag() */
378+/* Xlib helper function for i_drag() */
394379 Bool is_motionnotify(Display *d, XEvent *e, XPointer a) {
395380 return e != NULL && e->type == MotionNotify;
396381 }
397382
398−int drag(arg_t a) {
383+int i_drag(arg_t a) {
399384 int dx = 0, dy = 0, i, ox, oy, x, y;
400385 unsigned int ui;
401386 Bool dragging = True, next = False;
@@ -445,22 +430,7 @@ int drag(arg_t a) {
445430 return 0;
446431 }
447432
448−int rotate(arg_t a) {
449− direction_t dir = (direction_t) a;
450−
451− if (mode == MODE_NORMAL) {
452− if (dir == DIR_LEFT) {
453− img_rotate_left(&img, &win);
454− return 1;
455− } else if (dir == DIR_RIGHT) {
456− img_rotate_right(&img, &win);
457− return 1;
458− }
459− }
460− return 0;
461−}
462−
463−int zoom(arg_t a) {
433+int i_zoom(arg_t a) {
464434 int scale = (int) a;
465435
466436 if (mode != MODE_NORMAL)
@@ -473,7 +443,7 @@ int zoom(arg_t a) {
473443 return img_zoom(&img, &win, 1.0);
474444 }
475445
476−int fit_to_win(arg_t a) {
446+int i_fit_to_win(arg_t a) {
477447 int ret;
478448
479449 if (mode == MODE_NORMAL) {
@@ -485,7 +455,7 @@ int fit_to_win(arg_t a) {
485455 }
486456 }
487457
488−int fit_to_img(arg_t a) {
458+int i_fit_to_img(arg_t a) {
489459 int ret, x, y;
490460 unsigned int w, h;
491461
@@ -504,7 +474,40 @@ int fit_to_img(arg_t a) {
504474 }
505475 }
506476
507−int open_with(arg_t a) {
477+int i_rotate(arg_t a) {
478+ direction_t dir = (direction_t) a;
479+
480+ if (mode == MODE_NORMAL) {
481+ if (dir == DIR_LEFT) {
482+ img_rotate_left(&img, &win);
483+ return 1;
484+ } else if (dir == DIR_RIGHT) {
485+ img_rotate_right(&img, &win);
486+ return 1;
487+ }
488+ }
489+ return 0;
490+}
491+
492+int i_toggle_antialias(arg_t a) {
493+ if (mode == MODE_NORMAL) {
494+ img_toggle_antialias(&img);
495+ return 1;
496+ } else {
497+ return 0;
498+ }
499+}
500+
501+int i_toggle_alpha(arg_t a) {
502+ if (mode == MODE_NORMAL) {
503+ img.alpha ^= 1;
504+ return 1;
505+ } else {
506+ return 0;
507+ }
508+}
509+
510+int it_open_with(arg_t a) {
508511 const char *prog = (const char*) a;
509512 pid_t pid;
510513
@@ -523,7 +526,7 @@ int open_with(arg_t a) {
523526 return 0;
524527 }
525528
526−int run_command(arg_t a) {
529+int it_shell_cmd(arg_t a) {
527530 const char *cline = (const char*) a;
528531 char *cn, *cmdline;
529532 const char *co, *fpath;
M events.h View
@@ -22,11 +22,12 @@
2222 #include <X11/Xlib.h>
2323
2424 typedef void* arg_t;
25+typedef int (*command_f)(arg_t);
2526
2627 typedef struct {
2728 Bool ctrl;
2829 KeySym ksym;
29− int (*handler)(arg_t);
30+ command_f cmd;
3031 arg_t arg;
3132 } keymap_t;
3233
@@ -34,32 +35,32 @@ typedef struct {
3435 Bool ctrl;
3536 Bool shift;
3637 unsigned int button;
37− int (*handler)(arg_t);
38+ command_f cmd;
3839 arg_t arg;
3940 } button_t;
4041
4142 void run();
4243
43−/* handler functions for key and button mappings: */
44−int quit(arg_t);
45−int reload(arg_t);
46−int toggle_fullscreen(arg_t);
47−int toggle_antialias(arg_t);
48−int toggle_alpha(arg_t);
49−int switch_mode(arg_t);
50−int navigate(arg_t);
51−int first(arg_t);
52−int last(arg_t);
53−int remove_image(arg_t);
54−int move(arg_t);
55−int pan_screen(arg_t);
56−int pan_edge(arg_t);
57−int drag(arg_t);
58−int rotate(arg_t);
59−int zoom(arg_t);
60−int fit_to_win(arg_t);
61−int fit_to_img(arg_t);
62−int open_with(arg_t);
63−int run_command(arg_t);
44+/* command functions for key and button mappings: */
45+int it_quit(arg_t);
46+int it_switch_mode(arg_t);
47+int it_toggle_fullscreen(arg_t);
48+int it_reload_image(arg_t);
49+int it_remove_image(arg_t);
50+int i_navigate(arg_t);
51+int it_first(arg_t);
52+int it_last(arg_t);
53+int it_move(arg_t);
54+int i_pan_screen(arg_t);
55+int i_pan_edge(arg_t);
56+int i_drag(arg_t);
57+int i_zoom(arg_t);
58+int i_fit_to_win(arg_t);
59+int i_fit_to_img(arg_t);
60+int i_rotate(arg_t);
61+int i_toggle_antialias(arg_t);
62+int i_toggle_alpha(arg_t);
63+int it_open_with(arg_t);
64+int it_shell_cmd(arg_t);
6465
6566 #endif /* EVENTS_H */