Browse Source

Reduced usage of preprocessor macros

master
Bert 13 years ago
parent
commit
a271e16744
12 changed files with 113 additions and 120 deletions
  1. +1
    -1
      Makefile
  2. +11
    -16
      config.h
  3. +11
    -11
      image.c
  4. +3
    -15
      image.h
  5. +33
    -48
      main.c
  6. +1
    -1
      options.c
  7. +1
    -0
      options.h
  8. +9
    -9
      thumbs.c
  9. +3
    -9
      thumbs.h
  10. +35
    -0
      types.h
  11. +2
    -2
      window.c
  12. +3
    -8
      window.h

+ 1
- 1
Makefile View File

@@ -1,6 +1,6 @@
all: sxiv all: sxiv


VERSION = 0.8.2
VERSION = git-20110722


CC = gcc CC = gcc
DESTDIR = DESTDIR =


+ 11
- 16
config.h View File

@@ -1,19 +1,18 @@
/* default window dimensions (overwritten via -g option): */ /* 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: * /* default color for window background: *
* (see X(7) "COLOR NAMES" section for valid values) */ * (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: */ /* 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?: * /* how should images be scaled when they are loaded?: *
* (also controllable via -d/-s/-Z/-z options) * * (also controllable via -d/-s/-Z/-z options) *
* SCALE_DOWN: 100%, but fit large images into window, * * SCALE_DOWN: 100%, but fit large images into window, *
* SCALE_FIT: fit all images into window, * * SCALE_FIT: fit all images into window, *
* SCALE_ZOOM: use current zoom level, 100% at startup */ * 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 '+': */ /* levels (percent) to use when zooming via '-' and '+': */
static const float zoom_levels[] = { static const float zoom_levels[] = {
@@ -22,20 +21,16 @@ static const float zoom_levels[] = {
}; };


/* default dimension of thumbnails (width == height): */ /* default dimension of thumbnails (width == height): */
#define THUMB_SIZE 60
enum { THUMB_SIZE = 60 };


/* enable external commands (defined below)? 0=off, 1=on: */ /* enable external commands (defined below)? 0=off, 1=on: */
#define EXT_COMMANDS 0
enum { EXT_COMMANDS = 0 };


/* external commands and corresponding key mappings: */ /* external commands and corresponding key mappings: */
#ifdef MAIN_C
#if EXT_COMMANDS
static const command_t 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

+ 11
- 11
image.c View File

@@ -18,10 +18,10 @@


#include <unistd.h> #include <unistd.h>


#include "config.h"
#include "image.h" #include "image.h"
#include "options.h" #include "options.h"
#include "util.h" #include "util.h"
#include "config.h"


int zl_cnt; int zl_cnt;
float zoom_min; 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; 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) if (!img || !img->im || !win)
return 0; return 0;


switch (dir) { switch (dir) {
case PAN_LEFT:
case DIR_LEFT:
return img_move(img, win, win->w / (page ? 1 : 5), 0); 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); 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)); 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 img_move(img, win, 0, win->h / (page ? 1 : 5) * -1);
} }


return 0; 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; int ox, oy;


if (!img || !img->im || !win) 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; oy = img->y;


switch (dir) { switch (dir) {
case PAN_LEFT:
case DIR_LEFT:
img->x = 0; img->x = 0;
break; break;
case PAN_RIGHT:
case DIR_RIGHT:
img->x = win->w - img->w * img->zoom; img->x = win->w - img->w * img->zoom;
break; break;
case PAN_UP:
case DIR_UP:
img->y = 0; img->y = 0;
break; break;
case PAN_DOWN:
case DIR_DOWN:
img->y = win->h - img->h * img->zoom; img->y = win->h - img->h * img->zoom;
break; break;
} }


+ 3
- 15
image.h View File

@@ -21,21 +21,9 @@


#include <Imlib2.h> #include <Imlib2.h>


#include "types.h"
#include "window.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 { typedef struct {
Imlib_Image *im; 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_zoom_out(img_t*, win_t*);


int img_move(img_t*, win_t*, int, int); 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_left(img_t*, win_t*);
void img_rotate_right(img_t*, win_t*); void img_rotate_right(img_t*, win_t*);


+ 33
- 48
main.c View File

@@ -34,26 +34,13 @@
#include "image.h" #include "image.h"
#include "options.h" #include "options.h"
#include "thumbs.h" #include "thumbs.h"
#include "types.h"
#include "util.h" #include "util.h"
#include "window.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" #include "config.h"


enum { TITLE_LEN = 256, FNAME_CNT = 1024 };

void run(); void run();


appmode_t mode; appmode_t mode;
@@ -266,7 +253,6 @@ int main(int argc, char **argv) {
return 0; return 0;
} }


#if EXT_COMMANDS
int run_command(const char *cline, Bool reload) { int run_command(const char *cline, Bool reload) {
int fncnt, fnlen; int fncnt, fnlen;
char *cn, *cmdline; char *cn, *cmdline;
@@ -322,16 +308,17 @@ int run_command(const char *cline, Bool reload) {
free(cmdline); free(cmdline);
return ret; return ret;
} }
#endif /* EXT_COMMANDS */




/* event handling */ /* event handling */


/* timeouts in milliseconds: */ /* 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_cursor;
int timo_redraw; int timo_redraw;
@@ -366,11 +353,10 @@ void on_keypress(XKeyEvent *kev) {
changed = 0; changed = 0;
ctrl = CLEANMASK(kev->state) & ControlMask; ctrl = CLEANMASK(kev->state) & ControlMask;


#if EXT_COMMANDS
/* external commands from commands.h */ /* external commands from commands.h */
if (ctrl) {
if (EXT_COMMANDS && ctrl) {
for (x = 0; x < LEN(commands); ++x) { for (x = 0; x < LEN(commands); ++x) {
if (commands[x].ksym == ksym) {
if (commands[x].key == key) {
win_set_cursor(&win, CURSOR_WATCH); win_set_cursor(&win, CURSOR_WATCH);
if (run_command(commands[x].cmdline, commands[x].reload)) { if (run_command(commands[x].cmdline, commands[x].reload)) {
if (mode == MODE_NORMAL) { if (mode == MODE_NORMAL) {
@@ -396,7 +382,6 @@ void on_keypress(XKeyEvent *kev) {
} }
} }
} }
#endif


if (mode == MODE_NORMAL) { if (mode == MODE_NORMAL) {
switch (ksym) { switch (ksym) {
@@ -450,38 +435,38 @@ void on_keypress(XKeyEvent *kev) {
/* panning */ /* panning */
case XK_h: case XK_h:
case XK_Left: case XK_Left:
changed = img_pan(&img, &win, PAN_LEFT, ctrl);
changed = img_pan(&img, &win, DIR_LEFT, ctrl);
break; break;
case XK_j: case XK_j:
case XK_Down: case XK_Down:
changed = img_pan(&img, &win, PAN_DOWN, ctrl);
changed = img_pan(&img, &win, DIR_DOWN, ctrl);
break; break;
case XK_k: case XK_k:
case XK_Up: case XK_Up:
changed = img_pan(&img, &win, PAN_UP, ctrl);
changed = img_pan(&img, &win, DIR_UP, ctrl);
break; break;
case XK_l: case XK_l:
case XK_Right: case XK_Right:
changed = img_pan(&img, &win, PAN_RIGHT, ctrl);
changed = img_pan(&img, &win, DIR_RIGHT, ctrl);
break; break;
case XK_Prior: case XK_Prior:
changed = img_pan(&img, &win, PAN_UP, 1);
changed = img_pan(&img, &win, DIR_UP, 1);
break; break;
case XK_Next: case XK_Next:
changed = img_pan(&img, &win, PAN_DOWN, 1);
changed = img_pan(&img, &win, DIR_DOWN, 1);
break; break;


case XK_H: case XK_H:
changed = img_pan_edge(&img, &win, PAN_LEFT);
changed = img_pan_edge(&img, &win, DIR_LEFT);
break; break;
case XK_J: case XK_J:
changed = img_pan_edge(&img, &win, PAN_DOWN);
changed = img_pan_edge(&img, &win, DIR_DOWN);
break; break;
case XK_K: case XK_K:
changed = img_pan_edge(&img, &win, PAN_UP);
changed = img_pan_edge(&img, &win, DIR_UP);
break; break;
case XK_L: case XK_L:
changed = img_pan_edge(&img, &win, PAN_RIGHT);
changed = img_pan_edge(&img, &win, DIR_RIGHT);
break; break;


/* rotation */ /* rotation */
@@ -548,19 +533,19 @@ void on_keypress(XKeyEvent *kev) {
/* move selection */ /* move selection */
case XK_h: case XK_h:
case XK_Left: case XK_Left:
changed = tns_move_selection(&tns, &win, TNS_LEFT);
changed = tns_move_selection(&tns, &win, DIR_LEFT);
break; break;
case XK_j: case XK_j:
case XK_Down: case XK_Down:
changed = tns_move_selection(&tns, &win, TNS_DOWN);
changed = tns_move_selection(&tns, &win, DIR_DOWN);
break; break;
case XK_k: case XK_k:
case XK_Up: case XK_Up:
changed = tns_move_selection(&tns, &win, TNS_UP);
changed = tns_move_selection(&tns, &win, DIR_UP);
break; break;
case XK_l: case XK_l:
case XK_Right: case XK_Right:
changed = tns_move_selection(&tns, &win, TNS_RIGHT);
changed = tns_move_selection(&tns, &win, DIR_RIGHT);
break; break;
case XK_g: case XK_g:
if (tns.sel != 0) { if (tns.sel != 0) {
@@ -642,23 +627,23 @@ void on_buttonpress(XButtonEvent *bev) {
if (mask == ControlMask) if (mask == ControlMask)
changed = img_zoom_in(&img, &win); changed = img_zoom_in(&img, &win);
else if (mask == ShiftMask) else if (mask == ShiftMask)
changed = img_pan(&img, &win, PAN_LEFT, 0);
changed = img_pan(&img, &win, DIR_LEFT, 0);
else else
changed = img_pan(&img, &win, PAN_UP, 0);
changed = img_pan(&img, &win, DIR_UP, 0);
break; break;
case Button5: case Button5:
if (mask == ControlMask) if (mask == ControlMask)
changed = img_zoom_out(&img, &win); changed = img_zoom_out(&img, &win);
else if (mask == ShiftMask) else if (mask == ShiftMask)
changed = img_pan(&img, &win, PAN_RIGHT, 0);
changed = img_pan(&img, &win, DIR_RIGHT, 0);
else else
changed = img_pan(&img, &win, PAN_DOWN, 0);
changed = img_pan(&img, &win, DIR_DOWN, 0);
break; break;
case 6: case 6:
changed = img_pan(&img, &win, PAN_LEFT, 0);
changed = img_pan(&img, &win, DIR_LEFT, 0);
break; break;
case 7: case 7:
changed = img_pan(&img, &win, PAN_RIGHT, 0);
changed = img_pan(&img, &win, DIR_RIGHT, 0);
break; break;
} }
} else { } else {
@@ -680,10 +665,10 @@ void on_buttonpress(XButtonEvent *bev) {
} }
break; break;
case Button4: case Button4:
changed = tns_scroll(&tns, TNS_UP);
changed = tns_scroll(&tns, DIR_UP);
break; break;
case Button5: case Button5:
changed = tns_scroll(&tns, TNS_DOWN);
changed = tns_scroll(&tns, DIR_DOWN);
break; break;
} }
} }


+ 1
- 1
options.c View File

@@ -23,9 +23,9 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>


#include "config.h"
#include "options.h" #include "options.h"
#include "util.h" #include "util.h"
#include "config.h"


options_t _options; options_t _options;
const options_t *options = (const options_t*) &_options; const options_t *options = (const options_t*) &_options;


+ 1
- 0
options.h View File

@@ -20,6 +20,7 @@
#define OPTIONS_H #define OPTIONS_H


#include "image.h" #include "image.h"
#include "types.h"


typedef struct { typedef struct {
char **filenames; char **filenames;


+ 9
- 9
thumbs.c View File

@@ -23,9 +23,9 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>


#include "config.h"
#include "thumbs.h" #include "thumbs.h"
#include "util.h" #include "util.h"
#include "config.h"


#ifdef __NetBSD__ #ifdef __NetBSD__
#define st_mtim st_mtimespec #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); 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; int old;


if (!tns || !tns->thumbs || !win) 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; old = tns->sel;


switch (dir) { switch (dir) {
case TNS_LEFT:
case DIR_LEFT:
if (tns->sel > 0) if (tns->sel > 0)
--tns->sel; --tns->sel;
break; break;
case TNS_RIGHT:
case DIR_RIGHT:
if (tns->sel < tns->cnt - 1) if (tns->sel < tns->cnt - 1)
++tns->sel; ++tns->sel;
break; break;
case TNS_UP:
case DIR_UP:
if (tns->sel >= tns->cols) if (tns->sel >= tns->cols)
tns->sel -= tns->cols; tns->sel -= tns->cols;
break; break;
case TNS_DOWN:
case DIR_DOWN:
if (tns->sel + tns->cols < tns->cnt) if (tns->sel + tns->cols < tns->cnt)
tns->sel += tns->cols; tns->sel += tns->cols;
break; break;
@@ -417,7 +417,7 @@ int tns_move_selection(tns_t *tns, win_t *win, tnsdir_t dir) {
return tns->sel != old; return tns->sel != old;
} }


int tns_scroll(tns_t *tns, tnsdir_t dir) {
int tns_scroll(tns_t *tns, direction_t dir) {
int old; int old;


if (!tns) if (!tns)
@@ -425,11 +425,11 @@ int tns_scroll(tns_t *tns, tnsdir_t dir) {


old = tns->first; 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->first += tns->cols;
tns_check_view(tns, True); tns_check_view(tns, True);
tns->dirty = 1; 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->first -= tns->cols;
tns_check_view(tns, True); tns_check_view(tns, True);
tns->dirty = 1; tns->dirty = 1;


+ 3
- 9
thumbs.h View File

@@ -21,15 +21,9 @@


#include <Imlib2.h> #include <Imlib2.h>


#include "types.h"
#include "window.h" #include "window.h"


typedef enum {
TNS_LEFT = 0,
TNS_RIGHT,
TNS_UP,
TNS_DOWN
} tnsdir_t;

typedef struct { typedef struct {
Imlib_Image *im; Imlib_Image *im;
const char *filename; 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_render(tns_t*, win_t*);
void tns_highlight(tns_t*, win_t*, int, Bool); 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); int tns_translate(tns_t*, int, int);




+ 35
- 0
types.h View File

@@ -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 */

+ 2
- 2
window.c View File

@@ -21,10 +21,10 @@
#include <X11/Xutil.h> #include <X11/Xutil.h>
#include <X11/cursorfont.h> #include <X11/cursorfont.h>


#include "config.h"
#include "options.h" #include "options.h"
#include "util.h" #include "util.h"
#include "window.h" #include "window.h"
#include "config.h"


static Cursor carrow; static Cursor carrow;
static Cursor cnone; static Cursor cnone;
@@ -312,7 +312,7 @@ void win_set_title(win_t *win, const char *title) {
PropModeReplace, (unsigned char *) title, strlen(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) if (!win || !win->xwin)
return; return;




+ 3
- 8
window.h View File

@@ -21,14 +21,9 @@


#include <X11/Xlib.h> #include <X11/Xlib.h>


#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 { typedef struct {
Display *dpy; Display *dpy;
@@ -75,6 +70,6 @@ void win_draw_rect(win_t*, Pixmap, int, int, int, int, Bool, int,
unsigned long); unsigned long);


void win_set_title(win_t*, const char*); 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 */ #endif /* WINDOW_H */

Loading…
Cancel
Save