From 8f4af658ae484c3090dbd5b5b9b498943b3eb959 Mon Sep 17 00:00:00 2001 From: Bert Date: Thu, 3 Feb 2011 10:15:01 +0100 Subject: Refactored, new files util.[ch], C89 --- Makefile | 4 ++-- image.c | 9 ++++----- main.c | 60 +++++++++++++++++++++---------------------------------- options.c | 2 +- sxiv.h | 49 --------------------------------------------- util.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ util.h | 34 ++++++++++++++++++++++++++++++++ window.c | 12 +++++------ 8 files changed, 137 insertions(+), 101 deletions(-) delete mode 100644 sxiv.h create mode 100644 util.c create mode 100644 util.h diff --git a/Makefile b/Makefile index 5ef4755..016224b 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ all: sxiv -VERSION=git-20110202 +VERSION=git-20110203 CC?=gcc PREFIX?=/usr/local -CFLAGS+= -std=c99 -Wall -pedantic -DVERSION=\"$(VERSION)\" +CFLAGS+= -Wall -pedantic -DVERSION=\"$(VERSION)\" LDFLAGS+= LIBS+= -lX11 -lImlib2 diff --git a/image.c b/image.c index d1a1e35..13df76a 100644 --- a/image.c +++ b/image.c @@ -16,13 +16,12 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include -#include - #include -#include "sxiv.h" +#include "config.h" #include "image.h" +#include "options.h" +#include "util.h" int zl_cnt; float zoom_min; @@ -59,7 +58,7 @@ int _imlib_load_image(const char *filename) { return 0; if (!(im = imlib_load_image(filename))) { - WARN("could not open image: %s", filename); + warn("could not open image: %s", filename); return 0; } diff --git a/main.c b/main.c index bde737d..9362989 100644 --- a/main.c +++ b/main.c @@ -27,13 +27,11 @@ #include #include -#include "sxiv.h" #include "image.h" +#include "options.h" +#include "util.h" #include "window.h" -void* s_malloc(size_t); -void* s_realloc(void*, size_t); - void on_keypress(XEvent*); void on_buttonpress(XEvent*); void on_buttonrelease(XEvent*); @@ -44,13 +42,7 @@ void update_title(); void check_append(const char*); void read_dir_rec(const char*); -static void (*handler[LASTEvent])(XEvent*) = { - [KeyPress] = on_keypress, - [ButtonPress] = on_buttonpress, - [ButtonRelease] = on_buttonrelease, - [MotionNotify] = on_motionnotify, - [ConfigureNotify] = on_configurenotify -}; +static void (*handler[LASTEvent])(XEvent*); img_t img; win_t win; @@ -68,12 +60,27 @@ int moy; #define TITLE_LEN 256 char win_title[TITLE_LEN]; +void cleanup() { + static int in = 0; + + if (!in++) { + img_free(&img); + win_close(&win); + } +} + void run() { int xfd; fd_set fds; struct timeval t; XEvent ev; + handler[KeyPress] = on_keypress; + handler[ButtonPress] = on_buttonpress; + handler[ButtonRelease] = on_buttonrelease; + handler[MotionNotify] = on_motionnotify; + handler[ConfigureNotify] = on_configurenotify; + timeout = 0; while (1) { @@ -118,12 +125,12 @@ int main(int argc, char **argv) { for (i = 0; i < options->filecnt; ++i) { filename = options->filenames[i]; if (stat(filename, &fstats)) { - WARN("could not stat file: %s", filename); + warn("could not stat file: %s", filename); } else if (S_ISDIR(fstats.st_mode)) { if (options->recursive) read_dir_rec(filename); else - WARN("ignoring directory: %s", filename); + warn("ignoring directory: %s", filename); } else { check_append(filename); } @@ -151,15 +158,6 @@ int main(int argc, char **argv) { return 0; } -void cleanup() { - static int in = 0; - - if (!in++) { - img_free(&img); - win_close(&win); - } -} - void on_keypress(XEvent *ev) { char key; KeySym ksym; @@ -417,7 +415,7 @@ void read_dir_rec(const char *dirname) { while (diridx > 0) { dirname = dirnames[--diridx]; if (!(dir = opendir(dirname))) - DIE("could not open directory: %s", dirname); + die("could not open directory: %s", dirname); while ((dentry = readdir(dir))) { if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, "..")) continue; @@ -425,7 +423,7 @@ void read_dir_rec(const char *dirname) { filename = (char*) s_malloc(len * sizeof(char)); snprintf(filename, len, "%s/%s", dirname, dentry->d_name); if (stat(filename, &fstats)) { - WARN("could not stat file: %s", filename); + warn("could not stat file: %s", filename); free(filename); } else if (S_ISDIR(fstats.st_mode)) { if (diridx == dircnt) { @@ -447,17 +445,3 @@ void read_dir_rec(const char *dirname) { free(dirnames); } - -void* s_malloc(size_t size) { - void *ptr; - - if (!(ptr = malloc(size))) - DIE("could not allocate memory"); - return ptr; -} - -void* s_realloc(void *ptr, size_t size) { - if (!(ptr = realloc(ptr, size))) - DIE("could not allocate memory"); - return ptr; -} diff --git a/options.c b/options.c index 74daf5b..3e519f2 100644 --- a/options.c +++ b/options.c @@ -22,7 +22,7 @@ #include #include -#include "sxiv.h" +#include "config.h" #include "options.h" options_t _options; diff --git a/sxiv.h b/sxiv.h deleted file mode 100644 index b859a38..0000000 --- a/sxiv.h +++ /dev/null @@ -1,49 +0,0 @@ -/* sxiv: sxiv.h - * Copyright (c) 2011 Bert Muennich - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - -#ifndef SXIV_H -#define SXIV_H - -#include "config.h" -#include "options.h" - -#define ABS(a) ((a) < 0 ? (-(a)) : (a)) -#define MIN(a,b) ((a) < (b) ? (a) : (b)) -#define MAX(a,b) ((a) > (b) ? (a) : (b)) - -#define WARN(...) \ - do { \ - if (!options->quiet) { \ - fprintf(stderr, "sxiv: %s:%d: warning: ", __FILE__, __LINE__); \ - fprintf(stderr, __VA_ARGS__); \ - fprintf(stderr, "\n"); \ - } \ - } while (0) - -#define DIE(...) \ - do { \ - fprintf(stderr, "sxiv: %s:%d: error: ", __FILE__, __LINE__); \ - fprintf(stderr, __VA_ARGS__); \ - fprintf(stderr, "\n"); \ - cleanup(); \ - exit(1); \ - } while (0) - -void cleanup(); - -#endif /* SXIV_H */ diff --git a/util.c b/util.c new file mode 100644 index 0000000..472faf2 --- /dev/null +++ b/util.c @@ -0,0 +1,68 @@ +/* sxiv: util.c + * Copyright (c) 2011 Bert Muennich + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include +#include + +#include "options.h" +#include "util.h" + +void cleanup(); + +void* s_malloc(size_t size) { + void *ptr; + + if (!(ptr = malloc(size))) + die("could not allocate memory"); + return ptr; +} + +void* s_realloc(void *ptr, size_t size) { + if (!(ptr = realloc(ptr, size))) + die("could not allocate memory"); + return ptr; +} + +void warn(const char* fmt, ...) { + va_list args; + + if (!fmt || options->quiet) + return; + + va_start(args, fmt); + fprintf(stderr, "sxiv: warning: "); + fprintf(stderr, fmt, args); + fprintf(stderr, "\n"); + va_end(args); +} + +void die(const char* fmt, ...) { + va_list args; + + if (!fmt) + return; + + va_start(args, fmt); + fprintf(stderr, "sxiv: error: "); + fprintf(stderr, fmt, args); + fprintf(stderr, "\n"); + va_end(args); + + cleanup(); + exit(1); +} diff --git a/util.h b/util.h new file mode 100644 index 0000000..4f05dea --- /dev/null +++ b/util.h @@ -0,0 +1,34 @@ +/* sxiv: util.h + * Copyright (c) 2011 Bert Muennich + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef UTIL_H +#define UTIL_H + +#include + +#define ABS(a) ((a) < 0 ? (-(a)) : (a)) +#define MIN(a,b) ((a) < (b) ? (a) : (b)) +#define MAX(a,b) ((a) > (b) ? (a) : (b)) + +void* s_malloc(size_t); +void* s_realloc(void*, size_t); + +void warn(const char*, ...); +void die(const char*, ...); + +#endif /* UTIL_H */ diff --git a/window.c b/window.c index a7a1121..aa2f699 100644 --- a/window.c +++ b/window.c @@ -16,14 +16,14 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include -#include #include #include #include -#include "sxiv.h" +#include "config.h" +#include "options.h" +#include "util.h" #include "window.h" static Cursor arrow; @@ -43,7 +43,7 @@ void win_open(win_t *win) { e = &win->env; if (!(e->dpy = XOpenDisplay(NULL))) - DIE("could not open display"); + die("could not open display"); e->scr = DefaultScreen(e->dpy); e->scrw = DisplayWidth(e->dpy, e->scr); @@ -54,7 +54,7 @@ void win_open(win_t *win) { if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR, &bgcol, &bgcol)) - DIE("could not allocate color: %s", BG_COLOR); + die("could not allocate color: %s", BG_COLOR); win->bgcol = bgcol.pixel; win->pm = 0; @@ -88,7 +88,7 @@ void win_open(win_t *win) { win->x, win->y, win->w, win->h, 0, e->depth, InputOutput, e->vis, 0, None); if (win->xwin == None) - DIE("could not create window"); + die("could not create window"); XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask | Button2MotionMask); -- cgit v1.2.3-54-g00ecf