From a271e167443aedca2ed9c28b51fc6de33692dadb Mon Sep 17 00:00:00 2001 From: Bert Date: Fri, 22 Jul 2011 14:49:06 +0200 Subject: Reduced usage of preprocessor macros --- Makefile | 2 +- config.h | 27 +++++++++------------ image.c | 22 ++++++++--------- image.h | 18 +++----------- main.c | 81 ++++++++++++++++++++++++++------------------------------------- options.c | 2 +- options.h | 1 + thumbs.c | 18 +++++++------- thumbs.h | 12 +++------- types.h | 35 +++++++++++++++++++++++++++ window.c | 4 ++-- window.h | 11 +++------ 12 files changed, 113 insertions(+), 120 deletions(-) create mode 100644 types.h diff --git a/Makefile b/Makefile index e283a0b..52622ca 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ all: sxiv -VERSION = 0.8.2 +VERSION = git-20110722 CC = gcc DESTDIR = diff --git a/config.h b/config.h index 3bfd61f..d852091 100644 --- a/config.h +++ b/config.h @@ -1,19 +1,18 @@ /* default window dimensions (overwritten via -g option): */ -#define WIN_WIDTH 800 -#define WIN_HEIGHT 600 +enum { WIN_WIDTH = 800, WIN_HEIGHT = 600 }; /* default color for window background: * * (see X(7) "COLOR NAMES" section for valid values) */ -#define BG_COLOR "#999999" +static const char * const BG_COLOR = "#999999"; /* default color for thumbnail selection: */ -#define SEL_COLOR "#0066FF" +static const char * const SEL_COLOR = "#0066FF"; /* how should images be scaled when they are loaded?: * * (also controllable via -d/-s/-Z/-z options) * * SCALE_DOWN: 100%, but fit large images into window, * * SCALE_FIT: fit all images into window, * * SCALE_ZOOM: use current zoom level, 100% at startup */ -#define SCALE_MODE SCALE_DOWN +static const scalemode_t SCALE_MODE = SCALE_DOWN; /* levels (percent) to use when zooming via '-' and '+': */ static const float zoom_levels[] = { @@ -22,20 +21,16 @@ static const float zoom_levels[] = { }; /* default dimension of thumbnails (width == height): */ -#define THUMB_SIZE 60 +enum { THUMB_SIZE = 60 }; /* enable external commands (defined below)? 0=off, 1=on: */ -#define EXT_COMMANDS 0 +enum { EXT_COMMANDS = 0 }; /* external commands and corresponding key mappings: */ -#ifdef MAIN_C -#if EXT_COMMANDS static const command_t commands[] = { - /* ctrl-... reload? command, '#' is replaced by filename */ - { XK_comma, True, "jpegtran -rotate 270 -copy all -outfile # #" }, - { XK_period, True, "jpegtran -rotate 90 -copy all -outfile # #" }, - { XK_less, True, "mogrify -rotate -90 #" }, - { XK_greater, True, "mogrify -rotate +90 #" } + /* ctrl-... reload? command, '#' is replaced by filename */ + { ',', 1, "jpegtran -rotate 270 -copy all -outfile # #" }, + { '.', 1, "jpegtran -rotate 90 -copy all -outfile # #" }, + { '<', 1, "mogrify -rotate -90 #" }, + { '>', 1, "mogrify -rotate +90 #" } }; -#endif -#endif diff --git a/image.c b/image.c index 8233df5..8a82f32 100644 --- a/image.c +++ b/image.c @@ -18,10 +18,10 @@ #include -#include "config.h" #include "image.h" #include "options.h" #include "util.h" +#include "config.h" int zl_cnt; float zoom_min; @@ -275,25 +275,25 @@ int img_move(img_t *img, win_t *win, int dx, int dy) { return ox != img->x || oy != img->y; } -int img_pan(img_t *img, win_t *win, pandir_t dir, int page) { +int img_pan(img_t *img, win_t *win, direction_t dir, int page) { if (!img || !img->im || !win) return 0; switch (dir) { - case PAN_LEFT: + case DIR_LEFT: return img_move(img, win, win->w / (page ? 1 : 5), 0); - case PAN_RIGHT: + case DIR_RIGHT: return img_move(img, win, win->w / (page ? 1 : 5) * -1, 0); - case PAN_UP: + case DIR_UP: return img_move(img, win, 0, win->h / (page ? 1 : 5)); - case PAN_DOWN: + case DIR_DOWN: return img_move(img, win, 0, win->h / (page ? 1 : 5) * -1); } return 0; } -int img_pan_edge(img_t *img, win_t *win, pandir_t dir) { +int img_pan_edge(img_t *img, win_t *win, direction_t dir) { int ox, oy; if (!img || !img->im || !win) @@ -303,16 +303,16 @@ int img_pan_edge(img_t *img, win_t *win, pandir_t dir) { oy = img->y; switch (dir) { - case PAN_LEFT: + case DIR_LEFT: img->x = 0; break; - case PAN_RIGHT: + case DIR_RIGHT: img->x = win->w - img->w * img->zoom; break; - case PAN_UP: + case DIR_UP: img->y = 0; break; - case PAN_DOWN: + case DIR_DOWN: img->y = win->h - img->h * img->zoom; break; } diff --git a/image.h b/image.h index 711f15c..2d14b7a 100644 --- a/image.h +++ b/image.h @@ -21,21 +21,9 @@ #include +#include "types.h" #include "window.h" -typedef enum { - SCALE_DOWN = 0, - SCALE_FIT, - SCALE_ZOOM -} scalemode_t; - -typedef enum { - PAN_LEFT = 0, - PAN_RIGHT, - PAN_UP, - PAN_DOWN -} pandir_t; - typedef struct { Imlib_Image *im; @@ -68,8 +56,8 @@ int img_zoom_in(img_t*, win_t*); int img_zoom_out(img_t*, win_t*); int img_move(img_t*, win_t*, int, int); -int img_pan(img_t*, win_t*, pandir_t, int); -int img_pan_edge(img_t*, win_t*, pandir_t); +int img_pan(img_t*, win_t*, direction_t, int); +int img_pan_edge(img_t*, win_t*, direction_t); void img_rotate_left(img_t*, win_t*); void img_rotate_right(img_t*, win_t*); diff --git a/main.c b/main.c index e653476..2a80aff 100644 --- a/main.c +++ b/main.c @@ -34,26 +34,13 @@ #include "image.h" #include "options.h" #include "thumbs.h" +#include "types.h" #include "util.h" #include "window.h" - -#define FNAME_CNT 1024 -#define TITLE_LEN 256 - -typedef enum { - MODE_NORMAL = 0, - MODE_THUMBS -} appmode_t; - -typedef struct { - KeySym ksym; - Bool reload; - const char *cmdline; -} command_t; - -#define MAIN_C #include "config.h" +enum { TITLE_LEN = 256, FNAME_CNT = 1024 }; + void run(); appmode_t mode; @@ -266,7 +253,6 @@ int main(int argc, char **argv) { return 0; } -#if EXT_COMMANDS int run_command(const char *cline, Bool reload) { int fncnt, fnlen; char *cn, *cmdline; @@ -322,16 +308,17 @@ int run_command(const char *cline, Bool reload) { free(cmdline); return ret; } -#endif /* EXT_COMMANDS */ /* event handling */ /* timeouts in milliseconds: */ -#define TO_WIN_RESIZE 75 -#define TO_IMAGE_DRAG 1 -#define TO_CURSOR_HIDE 1500 -#define TO_THUMBS_LOAD 200 +enum { + TO_WIN_RESIZE = 75, + TO_IMAGE_DRAG = 1, + TO_CURSOR_HIDE = 1500, + TO_THUMBS_LOAD = 200 +}; int timo_cursor; int timo_redraw; @@ -366,11 +353,10 @@ void on_keypress(XKeyEvent *kev) { changed = 0; ctrl = CLEANMASK(kev->state) & ControlMask; -#if EXT_COMMANDS /* external commands from commands.h */ - if (ctrl) { + if (EXT_COMMANDS && ctrl) { for (x = 0; x < LEN(commands); ++x) { - if (commands[x].ksym == ksym) { + if (commands[x].key == key) { win_set_cursor(&win, CURSOR_WATCH); if (run_command(commands[x].cmdline, commands[x].reload)) { if (mode == MODE_NORMAL) { @@ -396,7 +382,6 @@ void on_keypress(XKeyEvent *kev) { } } } -#endif if (mode == MODE_NORMAL) { switch (ksym) { @@ -450,38 +435,38 @@ void on_keypress(XKeyEvent *kev) { /* panning */ case XK_h: case XK_Left: - changed = img_pan(&img, &win, PAN_LEFT, ctrl); + changed = img_pan(&img, &win, DIR_LEFT, ctrl); break; case XK_j: case XK_Down: - changed = img_pan(&img, &win, PAN_DOWN, ctrl); + changed = img_pan(&img, &win, DIR_DOWN, ctrl); break; case XK_k: case XK_Up: - changed = img_pan(&img, &win, PAN_UP, ctrl); + changed = img_pan(&img, &win, DIR_UP, ctrl); break; case XK_l: case XK_Right: - changed = img_pan(&img, &win, PAN_RIGHT, ctrl); + changed = img_pan(&img, &win, DIR_RIGHT, ctrl); break; case XK_Prior: - changed = img_pan(&img, &win, PAN_UP, 1); + changed = img_pan(&img, &win, DIR_UP, 1); break; case XK_Next: - changed = img_pan(&img, &win, PAN_DOWN, 1); + changed = img_pan(&img, &win, DIR_DOWN, 1); break; case XK_H: - changed = img_pan_edge(&img, &win, PAN_LEFT); + changed = img_pan_edge(&img, &win, DIR_LEFT); break; case XK_J: - changed = img_pan_edge(&img, &win, PAN_DOWN); + changed = img_pan_edge(&img, &win, DIR_DOWN); break; case XK_K: - changed = img_pan_edge(&img, &win, PAN_UP); + changed = img_pan_edge(&img, &win, DIR_UP); break; case XK_L: - changed = img_pan_edge(&img, &win, PAN_RIGHT); + changed = img_pan_edge(&img, &win, DIR_RIGHT); break; /* rotation */ @@ -548,19 +533,19 @@ void on_keypress(XKeyEvent *kev) { /* move selection */ case XK_h: case XK_Left: - changed = tns_move_selection(&tns, &win, TNS_LEFT); + changed = tns_move_selection(&tns, &win, DIR_LEFT); break; case XK_j: case XK_Down: - changed = tns_move_selection(&tns, &win, TNS_DOWN); + changed = tns_move_selection(&tns, &win, DIR_DOWN); break; case XK_k: case XK_Up: - changed = tns_move_selection(&tns, &win, TNS_UP); + changed = tns_move_selection(&tns, &win, DIR_UP); break; case XK_l: case XK_Right: - changed = tns_move_selection(&tns, &win, TNS_RIGHT); + changed = tns_move_selection(&tns, &win, DIR_RIGHT); break; case XK_g: if (tns.sel != 0) { @@ -642,23 +627,23 @@ void on_buttonpress(XButtonEvent *bev) { if (mask == ControlMask) changed = img_zoom_in(&img, &win); else if (mask == ShiftMask) - changed = img_pan(&img, &win, PAN_LEFT, 0); + changed = img_pan(&img, &win, DIR_LEFT, 0); else - changed = img_pan(&img, &win, PAN_UP, 0); + changed = img_pan(&img, &win, DIR_UP, 0); break; case Button5: if (mask == ControlMask) changed = img_zoom_out(&img, &win); else if (mask == ShiftMask) - changed = img_pan(&img, &win, PAN_RIGHT, 0); + changed = img_pan(&img, &win, DIR_RIGHT, 0); else - changed = img_pan(&img, &win, PAN_DOWN, 0); + changed = img_pan(&img, &win, DIR_DOWN, 0); break; case 6: - changed = img_pan(&img, &win, PAN_LEFT, 0); + changed = img_pan(&img, &win, DIR_LEFT, 0); break; case 7: - changed = img_pan(&img, &win, PAN_RIGHT, 0); + changed = img_pan(&img, &win, DIR_RIGHT, 0); break; } } else { @@ -680,10 +665,10 @@ void on_buttonpress(XButtonEvent *bev) { } break; case Button4: - changed = tns_scroll(&tns, TNS_UP); + changed = tns_scroll(&tns, DIR_UP); break; case Button5: - changed = tns_scroll(&tns, TNS_DOWN); + changed = tns_scroll(&tns, DIR_DOWN); break; } } diff --git a/options.c b/options.c index 7bc92b8..03c6194 100644 --- a/options.c +++ b/options.c @@ -23,9 +23,9 @@ #include #include -#include "config.h" #include "options.h" #include "util.h" +#include "config.h" options_t _options; const options_t *options = (const options_t*) &_options; diff --git a/options.h b/options.h index 4438fd0..a4b4a01 100644 --- a/options.h +++ b/options.h @@ -20,6 +20,7 @@ #define OPTIONS_H #include "image.h" +#include "types.h" typedef struct { char **filenames; diff --git a/thumbs.c b/thumbs.c index 75663cb..dd1dba6 100644 --- a/thumbs.c +++ b/thumbs.c @@ -23,9 +23,9 @@ #include #include -#include "config.h" #include "thumbs.h" #include "util.h" +#include "config.h" #ifdef __NetBSD__ #define st_mtim st_mtimespec @@ -380,7 +380,7 @@ void tns_highlight(tns_t *tns, win_t *win, int n, Bool hl) { win_draw(win); } -int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) { +int tns_move_selection(tns_t *tns, win_t *win, direction_t dir) { int old; if (!tns || !tns->thumbs || !win) @@ -389,19 +389,19 @@ int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) { old = tns->sel; switch (dir) { - case TNS_LEFT: + case DIR_LEFT: if (tns->sel > 0) --tns->sel; break; - case TNS_RIGHT: + case DIR_RIGHT: if (tns->sel < tns->cnt - 1) ++tns->sel; break; - case TNS_UP: + case DIR_UP: if (tns->sel >= tns->cols) tns->sel -= tns->cols; break; - case TNS_DOWN: + case DIR_DOWN: if (tns->sel + tns->cols < tns->cnt) tns->sel += tns->cols; break; @@ -417,7 +417,7 @@ int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) { return tns->sel != old; } -int tns_scroll(tns_t *tns, tnsdir_t dir) { +int tns_scroll(tns_t *tns, direction_t dir) { int old; if (!tns) @@ -425,11 +425,11 @@ int tns_scroll(tns_t *tns, tnsdir_t dir) { old = tns->first; - if (dir == TNS_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) { + if (dir == DIR_DOWN && tns->first + tns->cols * tns->rows < tns->cnt) { tns->first += tns->cols; tns_check_view(tns, True); tns->dirty = 1; - } else if (dir == TNS_UP && tns->first >= tns->cols) { + } else if (dir == DIR_UP && tns->first >= tns->cols) { tns->first -= tns->cols; tns_check_view(tns, True); tns->dirty = 1; diff --git a/thumbs.h b/thumbs.h index a81287b..347c146 100644 --- a/thumbs.h +++ b/thumbs.h @@ -21,15 +21,9 @@ #include +#include "types.h" #include "window.h" -typedef enum { - TNS_LEFT = 0, - TNS_RIGHT, - TNS_UP, - TNS_DOWN -} tnsdir_t; - typedef struct { Imlib_Image *im; const char *filename; @@ -62,8 +56,8 @@ int tns_load(tns_t*, int, const char*, unsigned char); void tns_render(tns_t*, win_t*); void tns_highlight(tns_t*, win_t*, int, Bool); -int tns_move_selection(tns_t*, win_t*, tnsdir_t); -int tns_scroll(tns_t*, tnsdir_t); +int tns_move_selection(tns_t*, win_t*, direction_t); +int tns_scroll(tns_t*, direction_t); int tns_translate(tns_t*, int, int); diff --git a/types.h b/types.h new file mode 100644 index 0000000..0726286 --- /dev/null +++ b/types.h @@ -0,0 +1,35 @@ +#ifndef TYPES_H +#define TYPES_H + +typedef enum { + MODE_NORMAL = 0, + MODE_THUMBS +} appmode_t; + +typedef struct { + char key; + int reload; + const char *cmdline; +} command_t; + +typedef enum { + DIR_LEFT = 0, + DIR_RIGHT, + DIR_UP, + DIR_DOWN +} direction_t; + +typedef enum { + SCALE_DOWN = 0, + SCALE_FIT, + SCALE_ZOOM +} scalemode_t; + +typedef enum { + CURSOR_ARROW = 0, + CURSOR_NONE, + CURSOR_HAND, + CURSOR_WATCH +} cursor_t; + +#endif /* TYPES_H */ diff --git a/window.c b/window.c index 68ef24f..7508641 100644 --- a/window.c +++ b/window.c @@ -21,10 +21,10 @@ #include #include -#include "config.h" #include "options.h" #include "util.h" #include "window.h" +#include "config.h" static Cursor carrow; static Cursor cnone; @@ -312,7 +312,7 @@ void win_set_title(win_t *win, const char *title) { PropModeReplace, (unsigned char *) title, strlen(title)); } -void win_set_cursor(win_t *win, win_cur_t cursor) { +void win_set_cursor(win_t *win, cursor_t cursor) { if (!win || !win->xwin) return; diff --git a/window.h b/window.h index 74e3102..0942137 100644 --- a/window.h +++ b/window.h @@ -21,14 +21,9 @@ #include -#define CLEANMASK(mask) ((mask) & ~LockMask) +#include "types.h" -typedef enum { - CURSOR_ARROW = 0, - CURSOR_NONE, - CURSOR_HAND, - CURSOR_WATCH -} win_cur_t; +#define CLEANMASK(mask) ((mask) & ~LockMask) typedef struct { Display *dpy; @@ -75,6 +70,6 @@ void win_draw_rect(win_t*, Pixmap, int, int, int, int, Bool, int, unsigned long); void win_set_title(win_t*, const char*); -void win_set_cursor(win_t*, win_cur_t); +void win_set_cursor(win_t*, cursor_t); #endif /* WINDOW_H */ -- cgit v1.2.3-54-g00ecf