nsxiv8f4af65
Refactored, new files util.[ch], C89
+137 −101
8 files changed
- M Makefile +2 −2
- M image.c +4 −5
- M main.c +22 −38
- M options.c +1 −1
- D sxiv.h +0 −49
- A util.c +68 −0
- A util.h +34 −0
- M window.c +6 −6
| @@ -1,10 +1,10 @@ | |||
| 1 | 1 | all: sxiv | |
| 2 | 2 | ||
| 3 | − | VERSION=git-20110202 | |
| 3 | + | VERSION=git-20110203 | |
| 4 | 4 | ||
| 5 | 5 | CC?=gcc | |
| 6 | 6 | PREFIX?=/usr/local | |
| 7 | − | CFLAGS+= -std=c99 -Wall -pedantic -DVERSION=\"$(VERSION)\" | |
| 7 | + | CFLAGS+= -Wall -pedantic -DVERSION=\"$(VERSION)\" | |
| 8 | 8 | LDFLAGS+= | |
| 9 | 9 | LIBS+= -lX11 -lImlib2 | |
| 10 | 10 | ||
| @@ -16,13 +16,12 @@ | |||
| 16 | 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| 17 | 17 | */ | |
| 18 | 18 | ||
| 19 | − | #include <stdlib.h> | |
| 20 | − | #include <stdio.h> | |
| 21 | − | ||
| 22 | 19 | #include <Imlib2.h> | |
| 23 | 20 | ||
| 24 | − | #include "sxiv.h" | |
| 21 | + | #include "config.h" | |
| 25 | 22 | #include "image.h" | |
| 23 | + | #include "options.h" | |
| 24 | + | #include "util.h" | |
| 26 | 25 | ||
| 27 | 26 | int zl_cnt; | |
| 28 | 27 | float zoom_min; | |
| @@ -59,7 +58,7 @@ int _imlib_load_image(const char *filename) { | |||
| 59 | 58 | return 0; | |
| 60 | 59 | ||
| 61 | 60 | if (!(im = imlib_load_image(filename))) { | |
| 62 | − | WARN("could not open image: %s", filename); | |
| 61 | + | warn("could not open image: %s", filename); | |
| 63 | 62 | return 0; | |
| 64 | 63 | } | |
| 65 | 64 | ||
| @@ -27,13 +27,11 @@ | |||
| 27 | 27 | #include <X11/Xutil.h> | |
| 28 | 28 | #include <X11/keysym.h> | |
| 29 | 29 | ||
| 30 | − | #include "sxiv.h" | |
| 31 | 30 | #include "image.h" | |
| 31 | + | #include "options.h" | |
| 32 | + | #include "util.h" | |
| 32 | 33 | #include "window.h" | |
| 33 | 34 | ||
| 34 | − | void* s_malloc(size_t); | |
| 35 | − | void* s_realloc(void*, size_t); | |
| 36 | − | ||
| 37 | 35 | void on_keypress(XEvent*); | |
| 38 | 36 | void on_buttonpress(XEvent*); | |
| 39 | 37 | void on_buttonrelease(XEvent*); | |
| @@ -44,13 +42,7 @@ void update_title(); | |||
| 44 | 42 | void check_append(const char*); | |
| 45 | 43 | void read_dir_rec(const char*); | |
| 46 | 44 | ||
| 47 | − | static void (*handler[LASTEvent])(XEvent*) = { | |
| 48 | − | [KeyPress] = on_keypress, | |
| 49 | − | [ButtonPress] = on_buttonpress, | |
| 50 | − | [ButtonRelease] = on_buttonrelease, | |
| 51 | − | [MotionNotify] = on_motionnotify, | |
| 52 | − | [ConfigureNotify] = on_configurenotify | |
| 53 | − | }; | |
| 45 | + | static void (*handler[LASTEvent])(XEvent*); | |
| 54 | 46 | ||
| 55 | 47 | img_t img; | |
| 56 | 48 | win_t win; | |
| @@ -68,12 +60,27 @@ int moy; | |||
| 68 | 60 | #define TITLE_LEN 256 | |
| 69 | 61 | char win_title[TITLE_LEN]; | |
| 70 | 62 | ||
| 63 | + | void cleanup() { | |
| 64 | + | static int in = 0; | |
| 65 | + | ||
| 66 | + | if (!in++) { | |
| 67 | + | img_free(&img); | |
| 68 | + | win_close(&win); | |
| 69 | + | } | |
| 70 | + | } | |
| 71 | + | ||
| 71 | 72 | void run() { | |
| 72 | 73 | int xfd; | |
| 73 | 74 | fd_set fds; | |
| 74 | 75 | struct timeval t; | |
| 75 | 76 | XEvent ev; | |
| 76 | 77 | ||
| 78 | + | handler[KeyPress] = on_keypress; | |
| 79 | + | handler[ButtonPress] = on_buttonpress; | |
| 80 | + | handler[ButtonRelease] = on_buttonrelease; | |
| 81 | + | handler[MotionNotify] = on_motionnotify; | |
| 82 | + | handler[ConfigureNotify] = on_configurenotify; | |
| 83 | + | ||
| 77 | 84 | timeout = 0; | |
| 78 | 85 | ||
| 79 | 86 | while (1) { | |
| @@ -118,12 +125,12 @@ int main(int argc, char **argv) { | |||
| 118 | 125 | for (i = 0; i < options->filecnt; ++i) { | |
| 119 | 126 | filename = options->filenames[i]; | |
| 120 | 127 | if (stat(filename, &fstats)) { | |
| 121 | − | WARN("could not stat file: %s", filename); | |
| 128 | + | warn("could not stat file: %s", filename); | |
| 122 | 129 | } else if (S_ISDIR(fstats.st_mode)) { | |
| 123 | 130 | if (options->recursive) | |
| 124 | 131 | read_dir_rec(filename); | |
| 125 | 132 | else | |
| 126 | − | WARN("ignoring directory: %s", filename); | |
| 133 | + | warn("ignoring directory: %s", filename); | |
| 127 | 134 | } else { | |
| 128 | 135 | check_append(filename); | |
| 129 | 136 | } | |
| @@ -151,15 +158,6 @@ int main(int argc, char **argv) { | |||
| 151 | 158 | return 0; | |
| 152 | 159 | } | |
| 153 | 160 | ||
| 154 | − | void cleanup() { | |
| 155 | − | static int in = 0; | |
| 156 | − | ||
| 157 | − | if (!in++) { | |
| 158 | − | img_free(&img); | |
| 159 | − | win_close(&win); | |
| 160 | − | } | |
| 161 | − | } | |
| 162 | − | ||
| 163 | 161 | void on_keypress(XEvent *ev) { | |
| 164 | 162 | char key; | |
| 165 | 163 | KeySym ksym; | |
| @@ -417,7 +415,7 @@ void read_dir_rec(const char *dirname) { | |||
| 417 | 415 | while (diridx > 0) { | |
| 418 | 416 | dirname = dirnames[--diridx]; | |
| 419 | 417 | if (!(dir = opendir(dirname))) | |
| 420 | − | DIE("could not open directory: %s", dirname); | |
| 418 | + | die("could not open directory: %s", dirname); | |
| 421 | 419 | while ((dentry = readdir(dir))) { | |
| 422 | 420 | if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, "..")) | |
| 423 | 421 | continue; | |
| @@ -425,7 +423,7 @@ void read_dir_rec(const char *dirname) { | |||
| 425 | 423 | filename = (char*) s_malloc(len * sizeof(char)); | |
| 426 | 424 | snprintf(filename, len, "%s/%s", dirname, dentry->d_name); | |
| 427 | 425 | if (stat(filename, &fstats)) { | |
| 428 | − | WARN("could not stat file: %s", filename); | |
| 426 | + | warn("could not stat file: %s", filename); | |
| 429 | 427 | free(filename); | |
| 430 | 428 | } else if (S_ISDIR(fstats.st_mode)) { | |
| 431 | 429 | if (diridx == dircnt) { | |
| @@ -447,17 +445,3 @@ void read_dir_rec(const char *dirname) { | |||
| 447 | 445 | ||
| 448 | 446 | free(dirnames); | |
| 449 | 447 | } | |
| 450 | − | ||
| 451 | − | void* s_malloc(size_t size) { | |
| 452 | − | void *ptr; | |
| 453 | − | ||
| 454 | − | if (!(ptr = malloc(size))) | |
| 455 | − | DIE("could not allocate memory"); | |
| 456 | − | return ptr; | |
| 457 | − | } | |
| 458 | − | ||
| 459 | − | void* s_realloc(void *ptr, size_t size) { | |
| 460 | − | if (!(ptr = realloc(ptr, size))) | |
| 461 | − | DIE("could not allocate memory"); | |
| 462 | − | return ptr; | |
| 463 | − | } | |
| @@ -22,7 +22,7 @@ | |||
| 22 | 22 | #include <stdio.h> | |
| 23 | 23 | #include <unistd.h> | |
| 24 | 24 | ||
| 25 | − | #include "sxiv.h" | |
| 25 | + | #include "config.h" | |
| 26 | 26 | #include "options.h" | |
| 27 | 27 | ||
| 28 | 28 | options_t _options; | |
| @@ -1,49 +0,0 @@ | |||
| 1 | − | /* sxiv: sxiv.h | |
| 2 | − | * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> | |
| 3 | − | * | |
| 4 | − | * This program is free software; you can redistribute it and/or modify | |
| 5 | − | * it under the terms of the GNU General Public License as published by | |
| 6 | − | * the Free Software Foundation; either version 2 of the License, or | |
| 7 | − | * (at your option) any later version. | |
| 8 | − | * | |
| 9 | − | * This program is distributed in the hope that it will be useful, | |
| 10 | − | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | − | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | − | * GNU General Public License for more details. | |
| 13 | − | * | |
| 14 | − | * You should have received a copy of the GNU General Public License | |
| 15 | − | * along with this program; if not, write to the Free Software | |
| 16 | − | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| 17 | − | */ | |
| 18 | − | ||
| 19 | − | #ifndef SXIV_H | |
| 20 | − | #define SXIV_H | |
| 21 | − | ||
| 22 | − | #include "config.h" | |
| 23 | − | #include "options.h" | |
| 24 | − | ||
| 25 | − | #define ABS(a) ((a) < 0 ? (-(a)) : (a)) | |
| 26 | − | #define MIN(a,b) ((a) < (b) ? (a) : (b)) | |
| 27 | − | #define MAX(a,b) ((a) > (b) ? (a) : (b)) | |
| 28 | − | ||
| 29 | − | #define WARN(...) \ | |
| 30 | − | do { \ | |
| 31 | − | if (!options->quiet) { \ | |
| 32 | − | fprintf(stderr, "sxiv: %s:%d: warning: ", __FILE__, __LINE__); \ | |
| 33 | − | fprintf(stderr, __VA_ARGS__); \ | |
| 34 | − | fprintf(stderr, "\n"); \ | |
| 35 | − | } \ | |
| 36 | − | } while (0) | |
| 37 | − | ||
| 38 | − | #define DIE(...) \ | |
| 39 | − | do { \ | |
| 40 | − | fprintf(stderr, "sxiv: %s:%d: error: ", __FILE__, __LINE__); \ | |
| 41 | − | fprintf(stderr, __VA_ARGS__); \ | |
| 42 | − | fprintf(stderr, "\n"); \ | |
| 43 | − | cleanup(); \ | |
| 44 | − | exit(1); \ | |
| 45 | − | } while (0) | |
| 46 | − | ||
| 47 | − | void cleanup(); | |
| 48 | − | ||
| 49 | − | #endif /* SXIV_H */ | |
| @@ -0,0 +1,68 @@ | |||
| 1 | + | /* sxiv: util.c | |
| 2 | + | * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> | |
| 3 | + | * | |
| 4 | + | * This program is free software; you can redistribute it and/or modify | |
| 5 | + | * it under the terms of the GNU General Public License as published by | |
| 6 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 7 | + | * (at your option) any later version. | |
| 8 | + | * | |
| 9 | + | * This program is distributed in the hope that it will be useful, | |
| 10 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | + | * GNU General Public License for more details. | |
| 13 | + | * | |
| 14 | + | * You should have received a copy of the GNU General Public License | |
| 15 | + | * along with this program; if not, write to the Free Software | |
| 16 | + | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| 17 | + | */ | |
| 18 | + | ||
| 19 | + | #include <stdlib.h> | |
| 20 | + | #include <stdio.h> | |
| 21 | + | ||
| 22 | + | #include "options.h" | |
| 23 | + | #include "util.h" | |
| 24 | + | ||
| 25 | + | void cleanup(); | |
| 26 | + | ||
| 27 | + | void* s_malloc(size_t size) { | |
| 28 | + | void *ptr; | |
| 29 | + | ||
| 30 | + | if (!(ptr = malloc(size))) | |
| 31 | + | die("could not allocate memory"); | |
| 32 | + | return ptr; | |
| 33 | + | } | |
| 34 | + | ||
| 35 | + | void* s_realloc(void *ptr, size_t size) { | |
| 36 | + | if (!(ptr = realloc(ptr, size))) | |
| 37 | + | die("could not allocate memory"); | |
| 38 | + | return ptr; | |
| 39 | + | } | |
| 40 | + | ||
| 41 | + | void warn(const char* fmt, ...) { | |
| 42 | + | va_list args; | |
| 43 | + | ||
| 44 | + | if (!fmt || options->quiet) | |
| 45 | + | return; | |
| 46 | + | ||
| 47 | + | va_start(args, fmt); | |
| 48 | + | fprintf(stderr, "sxiv: warning: "); | |
| 49 | + | fprintf(stderr, fmt, args); | |
| 50 | + | fprintf(stderr, "\n"); | |
| 51 | + | va_end(args); | |
| 52 | + | } | |
| 53 | + | ||
| 54 | + | void die(const char* fmt, ...) { | |
| 55 | + | va_list args; | |
| 56 | + | ||
| 57 | + | if (!fmt) | |
| 58 | + | return; | |
| 59 | + | ||
| 60 | + | va_start(args, fmt); | |
| 61 | + | fprintf(stderr, "sxiv: error: "); | |
| 62 | + | fprintf(stderr, fmt, args); | |
| 63 | + | fprintf(stderr, "\n"); | |
| 64 | + | va_end(args); | |
| 65 | + | ||
| 66 | + | cleanup(); | |
| 67 | + | exit(1); | |
| 68 | + | } | |
| @@ -0,0 +1,34 @@ | |||
| 1 | + | /* sxiv: util.h | |
| 2 | + | * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> | |
| 3 | + | * | |
| 4 | + | * This program is free software; you can redistribute it and/or modify | |
| 5 | + | * it under the terms of the GNU General Public License as published by | |
| 6 | + | * the Free Software Foundation; either version 2 of the License, or | |
| 7 | + | * (at your option) any later version. | |
| 8 | + | * | |
| 9 | + | * This program is distributed in the hope that it will be useful, | |
| 10 | + | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | + | * GNU General Public License for more details. | |
| 13 | + | * | |
| 14 | + | * You should have received a copy of the GNU General Public License | |
| 15 | + | * along with this program; if not, write to the Free Software | |
| 16 | + | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| 17 | + | */ | |
| 18 | + | ||
| 19 | + | #ifndef UTIL_H | |
| 20 | + | #define UTIL_H | |
| 21 | + | ||
| 22 | + | #include <stdarg.h> | |
| 23 | + | ||
| 24 | + | #define ABS(a) ((a) < 0 ? (-(a)) : (a)) | |
| 25 | + | #define MIN(a,b) ((a) < (b) ? (a) : (b)) | |
| 26 | + | #define MAX(a,b) ((a) > (b) ? (a) : (b)) | |
| 27 | + | ||
| 28 | + | void* s_malloc(size_t); | |
| 29 | + | void* s_realloc(void*, size_t); | |
| 30 | + | ||
| 31 | + | void warn(const char*, ...); | |
| 32 | + | void die(const char*, ...); | |
| 33 | + | ||
| 34 | + | #endif /* UTIL_H */ | |
| @@ -16,14 +16,14 @@ | |||
| 16 | 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| 17 | 17 | */ | |
| 18 | 18 | ||
| 19 | − | #include <stdlib.h> | |
| 20 | − | #include <stdio.h> | |
| 21 | 19 | #include <string.h> | |
| 22 | 20 | ||
| 23 | 21 | #include <X11/Xutil.h> | |
| 24 | 22 | #include <X11/cursorfont.h> | |
| 25 | 23 | ||
| 26 | − | #include "sxiv.h" | |
| 24 | + | #include "config.h" | |
| 25 | + | #include "options.h" | |
| 26 | + | #include "util.h" | |
| 27 | 27 | #include "window.h" | |
| 28 | 28 | ||
| 29 | 29 | static Cursor arrow; | |
| @@ -43,7 +43,7 @@ void win_open(win_t *win) { | |||
| 43 | 43 | ||
| 44 | 44 | e = &win->env; | |
| 45 | 45 | if (!(e->dpy = XOpenDisplay(NULL))) | |
| 46 | − | DIE("could not open display"); | |
| 46 | + | die("could not open display"); | |
| 47 | 47 | ||
| 48 | 48 | e->scr = DefaultScreen(e->dpy); | |
| 49 | 49 | e->scrw = DisplayWidth(e->dpy, e->scr); | |
| @@ -54,7 +54,7 @@ void win_open(win_t *win) { | |||
| 54 | 54 | ||
| 55 | 55 | if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR, | |
| 56 | 56 | &bgcol, &bgcol)) | |
| 57 | − | DIE("could not allocate color: %s", BG_COLOR); | |
| 57 | + | die("could not allocate color: %s", BG_COLOR); | |
| 58 | 58 | ||
| 59 | 59 | win->bgcol = bgcol.pixel; | |
| 60 | 60 | win->pm = 0; | |
| @@ -88,7 +88,7 @@ void win_open(win_t *win) { | |||
| 88 | 88 | win->x, win->y, win->w, win->h, 0, | |
| 89 | 89 | e->depth, InputOutput, e->vis, 0, None); | |
| 90 | 90 | if (win->xwin == None) | |
| 91 | − | DIE("could not create window"); | |
| 91 | + | die("could not create window"); | |
| 92 | 92 | ||
| 93 | 93 | XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask | | |
| 94 | 94 | ButtonPressMask | ButtonReleaseMask | Button2MotionMask); | |