Krolyxon
Commit a271e16

Branches

Refs →

nsxiva271e16

Reduced usage of preprocessor macros

Author
Bert
Date
2011-07-22 14:49 +0200
Parent
5f780fc
Commit
a271e167443aedca2ed9c28b51fc6de33692dadb

+113 −120

12 files changed

  1. M Makefile +1 −1
  2. M config.h +11 −16
  3. M image.c +11 −11
  4. M image.h +3 −15
  5. M main.c +33 −48
  6. M options.c +1 −1
  7. M options.h +1 −0
  8. M thumbs.c +9 −9
  9. M thumbs.h +3 −9
  10. A types.h +35 −0
  11. M window.c +2 −2
  12. M window.h +3 −8
M Makefile View
@@ -1,6 +1,6 @@
11 all: sxiv
22
3−VERSION = 0.8.2
3+VERSION = git-20110722
44
55 CC = gcc
66 DESTDIR =
M config.h View
@@ -1,19 +1,18 @@
11 /* default window dimensions (overwritten via -g option): */
2−#define WIN_WIDTH 800
3−#define WIN_HEIGHT 600
2+enum { WIN_WIDTH = 800, WIN_HEIGHT = 600 };
43
54 /* default color for window background: *
65 * (see X(7) "COLOR NAMES" section for valid values) */
7−#define BG_COLOR "#999999"
6+static const char * const BG_COLOR = "#999999";
87 /* default color for thumbnail selection: */
9−#define SEL_COLOR "#0066FF"
8+static const char * const SEL_COLOR = "#0066FF";
109
1110 /* how should images be scaled when they are loaded?: *
1211 * (also controllable via -d/-s/-Z/-z options) *
1312 * SCALE_DOWN: 100%, but fit large images into window, *
1413 * SCALE_FIT: fit all images into window, *
1514 * SCALE_ZOOM: use current zoom level, 100% at startup */
16−#define SCALE_MODE SCALE_DOWN
15+static const scalemode_t SCALE_MODE = SCALE_DOWN;
1716
1817 /* levels (percent) to use when zooming via '-' and '+': */
1918 static const float zoom_levels[] = {
@@ -22,20 +21,16 @@ static const float zoom_levels[] = {
2221 };
2322
2423 /* default dimension of thumbnails (width == height): */
25−#define THUMB_SIZE 60
24+enum { THUMB_SIZE = 60 };
2625
2726 /* enable external commands (defined below)? 0=off, 1=on: */
28−#define EXT_COMMANDS 0
27+enum { EXT_COMMANDS = 0 };
2928
3029 /* external commands and corresponding key mappings: */
31−#ifdef MAIN_C
32−#if EXT_COMMANDS
3330 static const command_t commands[] = {
34− /* ctrl-... reload? command, '#' is replaced by filename */
35− { XK_comma, True, "jpegtran -rotate 270 -copy all -outfile # #" },
36− { XK_period, True, "jpegtran -rotate 90 -copy all -outfile # #" },
37− { XK_less, True, "mogrify -rotate -90 #" },
38− { XK_greater, True, "mogrify -rotate +90 #" }
31+ /* ctrl-... reload? command, '#' is replaced by filename */
32+ { ',', 1, "jpegtran -rotate 270 -copy all -outfile # #" },
33+ { '.', 1, "jpegtran -rotate 90 -copy all -outfile # #" },
34+ { '<', 1, "mogrify -rotate -90 #" },
35+ { '>', 1, "mogrify -rotate +90 #" }
3936 };
40−#endif
41−#endif
M image.c View
@@ -18,10 +18,10 @@
1818
1919 #include <unistd.h>
2020
21−#include "config.h"
2221 #include "image.h"
2322 #include "options.h"
2423 #include "util.h"
24+#include "config.h"
2525
2626 int zl_cnt;
2727 float zoom_min;
@@ -275,25 +275,25 @@ int img_move(img_t *img, win_t *win, int dx, int dy) {
275275 return ox != img->x || oy != img->y;
276276 }
277277
278−int img_pan(img_t *img, win_t *win, pandir_t dir, int page) {
278+int img_pan(img_t *img, win_t *win, direction_t dir, int page) {
279279 if (!img || !img->im || !win)
280280 return 0;
281281
282282 switch (dir) {
283− case PAN_LEFT:
283+ case DIR_LEFT:
284284 return img_move(img, win, win->w / (page ? 1 : 5), 0);
285− case PAN_RIGHT:
285+ case DIR_RIGHT:
286286 return img_move(img, win, win->w / (page ? 1 : 5) * -1, 0);
287− case PAN_UP:
287+ case DIR_UP:
288288 return img_move(img, win, 0, win->h / (page ? 1 : 5));
289− case PAN_DOWN:
289+ case DIR_DOWN:
290290 return img_move(img, win, 0, win->h / (page ? 1 : 5) * -1);
291291 }
292292
293293 return 0;
294294 }
295295
296−int img_pan_edge(img_t *img, win_t *win, pandir_t dir) {
296+int img_pan_edge(img_t *img, win_t *win, direction_t dir) {
297297 int ox, oy;
298298
299299 if (!img || !img->im || !win)
@@ -303,16 +303,16 @@ int img_pan_edge(img_t *img, win_t *win, pandir_t dir) {
303303 oy = img->y;
304304
305305 switch (dir) {
306− case PAN_LEFT:
306+ case DIR_LEFT:
307307 img->x = 0;
308308 break;
309− case PAN_RIGHT:
309+ case DIR_RIGHT:
310310 img->x = win->w - img->w * img->zoom;
311311 break;
312− case PAN_UP:
312+ case DIR_UP:
313313 img->y = 0;
314314 break;
315− case PAN_DOWN:
315+ case DIR_DOWN:
316316 img->y = win->h - img->h * img->zoom;
317317 break;
318318 }
M image.h View
@@ -21,21 +21,9 @@
2121
2222 #include <Imlib2.h>
2323
24+#include "types.h"
2425 #include "window.h"
2526
26−typedef enum {
27− SCALE_DOWN = 0,
28− SCALE_FIT,
29− SCALE_ZOOM
30−} scalemode_t;
31−
32−typedef enum {
33− PAN_LEFT = 0,
34− PAN_RIGHT,
35− PAN_UP,
36− PAN_DOWN
37−} pandir_t;
38−
3927 typedef struct {
4028 Imlib_Image *im;
4129
@@ -68,8 +56,8 @@ int img_zoom_in(img_t*, win_t*);
6856 int img_zoom_out(img_t*, win_t*);
6957
7058 int img_move(img_t*, win_t*, int, int);
71−int img_pan(img_t*, win_t*, pandir_t, int);
72−int img_pan_edge(img_t*, win_t*, pandir_t);
59+int img_pan(img_t*, win_t*, direction_t, int);
60+int img_pan_edge(img_t*, win_t*, direction_t);
7361
7462 void img_rotate_left(img_t*, win_t*);
7563 void img_rotate_right(img_t*, win_t*);
M main.c View
@@ -34,26 +34,13 @@
3434 #include "image.h"
3535 #include "options.h"
3636 #include "thumbs.h"
37+#include "types.h"
3738 #include "util.h"
3839 #include "window.h"
39−
40−#define FNAME_CNT 1024
41−#define TITLE_LEN 256
42−
43−typedef enum {
44− MODE_NORMAL = 0,
45− MODE_THUMBS
46−} appmode_t;
47−
48−typedef struct {
49− KeySym ksym;
50− Bool reload;
51− const char *cmdline;
52−} command_t;
53−
54−#define MAIN_C
5540 #include "config.h"
5641
42+enum { TITLE_LEN = 256, FNAME_CNT = 1024 };
43+
5744 void run();
5845
5946 appmode_t mode;
@@ -266,7 +253,6 @@ int main(int argc, char **argv) {
266253 return 0;
267254 }
268255
269−#if EXT_COMMANDS
270256 int run_command(const char *cline, Bool reload) {
271257 int fncnt, fnlen;
272258 char *cn, *cmdline;
@@ -322,16 +308,17 @@ int run_command(const char *cline, Bool reload) {
322308 free(cmdline);
323309 return ret;
324310 }
325−#endif /* EXT_COMMANDS */
326311
327312
328313 /* event handling */
329314
330315 /* timeouts in milliseconds: */
331−#define TO_WIN_RESIZE 75
332−#define TO_IMAGE_DRAG 1
333−#define TO_CURSOR_HIDE 1500
334−#define TO_THUMBS_LOAD 200
316+enum {
317+ TO_WIN_RESIZE = 75,
318+ TO_IMAGE_DRAG = 1,
319+ TO_CURSOR_HIDE = 1500,
320+ TO_THUMBS_LOAD = 200
321+};
335322
336323 int timo_cursor;
337324 int timo_redraw;
@@ -366,11 +353,10 @@ void on_keypress(XKeyEvent *kev) {
366353 changed = 0;
367354 ctrl = CLEANMASK(kev->state) & ControlMask;
368355
369−#if EXT_COMMANDS
370356 /* external commands from commands.h */
371− if (ctrl) {
357+ if (EXT_COMMANDS && ctrl) {
372358 for (x = 0; x < LEN(commands); ++x) {
373− if (commands[x].ksym == ksym) {
359+ if (commands[x].key == key) {
374360 win_set_cursor(&win, CURSOR_WATCH);
375361 if (run_command(commands[x].cmdline, commands[x].reload)) {
376362 if (mode == MODE_NORMAL) {
@@ -396,7 +382,6 @@ void on_keypress(XKeyEvent *kev) {
396382 }
397383 }
398384 }
399−#endif
400385
401386 if (mode == MODE_NORMAL) {
402387 switch (ksym) {
@@ -450,38 +435,38 @@ void on_keypress(XKeyEvent *kev) {
450435 /* panning */
451436 case XK_h:
452437 case XK_Left:
453− changed = img_pan(&img, &win, PAN_LEFT, ctrl);
438+ changed = img_pan(&img, &win, DIR_LEFT, ctrl);
454439 break;
455440 case XK_j:
456441 case XK_Down:
457− changed = img_pan(&img, &win, PAN_DOWN, ctrl);
442+ changed = img_pan(&img, &win, DIR_DOWN, ctrl);
458443 break;
459444 case XK_k:
460445 case XK_Up:
461− changed = img_pan(&img, &win, PAN_UP, ctrl);
446+ changed = img_pan(&img, &win, DIR_UP, ctrl);
462447 break;
463448 case XK_l:
464449 case XK_Right:
465− changed = img_pan(&img, &win, PAN_RIGHT, ctrl);
450+ changed = img_pan(&img, &win, DIR_RIGHT, ctrl);
466451 break;
467452 case XK_Prior:
468− changed = img_pan(&img, &win, PAN_UP, 1);
453+ changed = img_pan(&img, &win, DIR_UP, 1);
469454 break;
470455 case XK_Next:
471− changed = img_pan(&img, &win, PAN_DOWN, 1);
456+ changed = img_pan(&img, &win, DIR_DOWN, 1);
472457 break;
473458
474459 case XK_H:
475− changed = img_pan_edge(&img, &win, PAN_LEFT);
460+ changed = img_pan_edge(&img, &win, DIR_LEFT);
476461 break;
477462 case XK_J:
478− changed = img_pan_edge(&img, &win, PAN_DOWN);
463+ changed = img_pan_edge(&img, &win, DIR_DOWN);
479464 break;
480465 case XK_K:
481− changed = img_pan_edge(&img, &win, PAN_UP);
466+ changed = img_pan_edge(&img, &win, DIR_UP);
482467 break;
483468 case XK_L:
484− changed = img_pan_edge(&img, &win, PAN_RIGHT);
469+ changed = img_pan_edge(&img, &win, DIR_RIGHT);
485470 break;
486471
487472 /* rotation */
@@ -548,19 +533,19 @@ void on_keypress(XKeyEvent *kev) {
548533 /* move selection */
549534 case XK_h:
550535 case XK_Left:
551− changed = tns_move_selection(&tns, &win, TNS_LEFT);
536+ changed = tns_move_selection(&tns, &win, DIR_LEFT);
552537 break;
553538 case XK_j:
554539 case XK_Down:
555− changed = tns_move_selection(&tns, &win, TNS_DOWN);
540+ changed = tns_move_selection(&tns, &win, DIR_DOWN);
556541 break;
557542 case XK_k:
558543 case XK_Up:
559− changed = tns_move_selection(&tns, &win, TNS_UP);
544+ changed = tns_move_selection(&tns, &win, DIR_UP);
560545 break;
561546 case XK_l:
562547 case XK_Right:
563− changed = tns_move_selection(&tns, &win, TNS_RIGHT);
548+ changed = tns_move_selection(&tns, &win, DIR_RIGHT);
564549 break;
565550 case XK_g:
566551 if (tns.sel != 0) {
@@ -642,23 +627,23 @@ void on_buttonpress(XButtonEvent *bev) {
642627 if (mask == ControlMask)
643628 changed = img_zoom_in(&img, &win);
644629 else if (mask == ShiftMask)
645− changed = img_pan(&img, &win, PAN_LEFT, 0);
630+ changed = img_pan(&img, &win, DIR_LEFT, 0);
646631 else
647− changed = img_pan(&img, &win, PAN_UP, 0);
632+ changed = img_pan(&img, &win, DIR_UP, 0);
648633 break;
649634 case Button5:
650635 if (mask == ControlMask)
651636 changed = img_zoom_out(&img, &win);
652637 else if (mask == ShiftMask)
653− changed = img_pan(&img, &win, PAN_RIGHT, 0);
638+ changed = img_pan(&img, &win, DIR_RIGHT, 0);
654639 else
655− changed = img_pan(&img, &win, PAN_DOWN, 0);
640+ changed = img_pan(&img, &win, DIR_DOWN, 0);
656641 break;
657642 case 6:
658− changed = img_pan(&img, &win, PAN_LEFT, 0);
643+ changed = img_pan(&img, &win, DIR_LEFT, 0);
659644 break;
660645 case 7:
661− changed = img_pan(&img, &win, PAN_RIGHT, 0);
646+ changed = img_pan(&img, &win, DIR_RIGHT, 0);
662647 break;
663648 }
664649 } else {
@@ -680,10 +665,10 @@ void on_buttonpress(XButtonEvent *bev) {
680665 }
681666 break;
682667 case Button4:
683− changed = tns_scroll(&tns, TNS_UP);
668+ changed = tns_scroll(&tns, DIR_UP);
684669 break;
685670 case Button5:
686− changed = tns_scroll(&tns, TNS_DOWN);
671+ changed = tns_scroll(&tns, DIR_DOWN);
687672 break;
688673 }
689674 }
M options.c View
@@ -23,9 +23,9 @@
2323 #include <stdio.h>
2424 #include <unistd.h>
2525
26−#include "config.h"
2726 #include "options.h"
2827 #include "util.h"
28+#include "config.h"
2929
3030 options_t _options;
3131 const options_t *options = (const options_t*) &_options;
M options.h View
@@ -20,6 +20,7 @@
2020 #define OPTIONS_H
2121
2222 #include "image.h"
23+#include "types.h"
2324
2425 typedef struct {
2526 char **filenames;
M thumbs.c View
@@ -23,9 +23,9 @@
2323 #include <sys/stat.h>
2424 #include <unistd.h>
2525
26−#include "config.h"
2726 #include "thumbs.h"
2827 #include "util.h"
28+#include "config.h"
2929
3030 #ifdef __NetBSD__
3131 #define st_mtim st_mtimespec
@@ -380,7 +380,7 @@ void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) {
380380 win_draw(win);
381381 }
382382
383−int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
383+int tns_move_selection(tns_t *tns, win_t *win, direction_t dir) {
384384 int old;
385385
386386 if (!tns || !tns->thumbs || !win)
@@ -389,19 +389,19 @@ int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
389389 old = tns->sel;
390390
391391 switch (dir) {
392− case TNS_LEFT:
392+ case DIR_LEFT:
393393 if (tns->sel > 0)
394394 --tns->sel;
395395 break;
396− case TNS_RIGHT:
396+ case DIR_RIGHT:
397397 if (tns->sel < tns->cnt - 1)
398398 ++tns->sel;
399399 break;
400− case TNS_UP:
400+ case DIR_UP:
401401 if (tns->sel >= tns->cols)
402402 tns->sel -= tns->cols;
403403 break;
404− case TNS_DOWN:
404+ case DIR_DOWN:
405405 if (tns->sel + tns->cols < tns->cnt)
406406 tns->sel += tns->cols;
407407 break;
@@ -417,7 +417,7 @@ int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
417417 return tns->sel != old;
418418 }
419419
420−int tns_scroll(tns_t *tns, tnsdir_t dir) {
420+int tns_scroll(tns_t *tns, direction_t dir) {
421421 int old;
422422
423423 if (!tns)
@@ -425,11 +425,11 @@ int tns_scroll(tns_t *tns, tnsdir_t dir) {
425425
426426 old = tns->first;
427427
428− if (dir == TNS_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
428+ if (dir == DIR_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) {
429429 tns->first += tns->cols;
430430 tns_check_view(tns, True);
431431 tns->dirty = 1;
432− } else if (dir == TNS_UP && tns->first >= tns->cols) {
432+ } else if (dir == DIR_UP && tns->first >= tns->cols) {
433433 tns->first -= tns->cols;
434434 tns_check_view(tns, True);
435435 tns->dirty = 1;
M thumbs.h View
@@ -21,15 +21,9 @@
2121
2222 #include <Imlib2.h>
2323
24+#include "types.h"
2425 #include "window.h"
2526
26−typedef enum {
27− TNS_LEFT = 0,
28− TNS_RIGHT,
29− TNS_UP,
30− TNS_DOWN
31−} tnsdir_t;
32−
3327 typedef struct {
3428 Imlib_Image *im;
3529 const char *filename;
@@ -62,8 +56,8 @@ int tns_load(tns_t*, int, const char*, unsigned char);
6256 void tns_render(tns_t*, win_t*);
6357 void tns_highlight(tns_t*, win_t*, int, Bool);
6458
65−int tns_move_selection(tns_t*, win_t*, tnsdir_t);
66−int tns_scroll(tns_t*, tnsdir_t);
59+int tns_move_selection(tns_t*, win_t*, direction_t);
60+int tns_scroll(tns_t*, direction_t);
6761
6862 int tns_translate(tns_t*, int, int);
6963
A types.h View
@@ -0,0 +1,35 @@
1+#ifndef TYPES_H
2+#define TYPES_H
3+
4+typedef enum {
5+ MODE_NORMAL = 0,
6+ MODE_THUMBS
7+} appmode_t;
8+
9+typedef struct {
10+ char key;
11+ int reload;
12+ const char *cmdline;
13+} command_t;
14+
15+typedef enum {
16+ DIR_LEFT = 0,
17+ DIR_RIGHT,
18+ DIR_UP,
19+ DIR_DOWN
20+} direction_t;
21+
22+typedef enum {
23+ SCALE_DOWN = 0,
24+ SCALE_FIT,
25+ SCALE_ZOOM
26+} scalemode_t;
27+
28+typedef enum {
29+ CURSOR_ARROW = 0,
30+ CURSOR_NONE,
31+ CURSOR_HAND,
32+ CURSOR_WATCH
33+} cursor_t;
34+
35+#endif /* TYPES_H */
M window.c View
@@ -21,10 +21,10 @@
2121 #include <X11/Xutil.h>
2222 #include <X11/cursorfont.h>
2323
24−#include "config.h"
2524 #include "options.h"
2625 #include "util.h"
2726 #include "window.h"
27+#include "config.h"
2828
2929 static Cursor carrow;
3030 static Cursor cnone;
@@ -312,7 +312,7 @@ void win_set_title(win_t *win, const char *title) {
312312 PropModeReplace, (unsigned char *) title, strlen(title));
313313 }
314314
315−void win_set_cursor(win_t *win, win_cur_t cursor) {
315+void win_set_cursor(win_t *win, cursor_t cursor) {
316316 if (!win || !win->xwin)
317317 return;
318318
M window.h View
@@ -21,14 +21,9 @@
2121
2222 #include <X11/Xlib.h>
2323
24−#define CLEANMASK(mask) ((mask) & ~LockMask)
24+#include "types.h"
2525
26−typedef enum {
27− CURSOR_ARROW = 0,
28− CURSOR_NONE,
29− CURSOR_HAND,
30− CURSOR_WATCH
31−} win_cur_t;
26+#define CLEANMASK(mask) ((mask) & ~LockMask)
3227
3328 typedef struct {
3429 Display *dpy;
@@ -75,6 +70,6 @@ void win_draw_rect(win_t*, Pixmap, int, int, int, int, Bool, int,
7570 unsigned long);
7671
7772 void win_set_title(win_t*, const char*);
78−void win_set_cursor(win_t*, win_cur_t);
73+void win_set_cursor(win_t*, cursor_t);
7974
8075 #endif /* WINDOW_H */