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 --- main.c | 60 ++++++++++++++++++++++-------------------------------------- 1 file changed, 22 insertions(+), 38 deletions(-) (limited to 'main.c') 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; -} -- cgit v1.2.3-54-g00ecf