nsxiva7e30bb
Option handling, merged app.c & events.c into main.c
+143 −163
8 files changed
- D app.h +0 −39
- D events.c +0 −72
- M image.c +9 −2
- M image.h +2 −2
- M main.c +83 −10
- R app.c → options.c +34 −31
- R events.h → options.h +14 −6
- M sxiv.h +1 −1
| @@ -1,39 +0,0 @@ | |||
| 1 | − | /* sxiv: app.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 APP_H | |
| 20 | − | #define APP_H | |
| 21 | − | ||
| 22 | − | #include "image.h" | |
| 23 | − | #include "window.h" | |
| 24 | − | ||
| 25 | − | typedef struct app_s { | |
| 26 | − | char **filenames; | |
| 27 | − | unsigned int filecnt; | |
| 28 | − | unsigned int fileidx; | |
| 29 | − | img_t img; | |
| 30 | − | win_t win; | |
| 31 | − | } app_t; | |
| 32 | − | ||
| 33 | − | void app_init(app_t*); | |
| 34 | − | void app_run(app_t*); | |
| 35 | − | void app_quit(app_t*); | |
| 36 | − | ||
| 37 | − | void app_load_image(app_t*); | |
| 38 | − | ||
| 39 | − | #endif /* APP_H */ | |
| @@ -1,72 +0,0 @@ | |||
| 1 | − | /* sxiv: events.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 | − | ||
| 21 | − | #include <X11/Xlib.h> | |
| 22 | − | #include <X11/keysym.h> | |
| 23 | − | ||
| 24 | − | #include "events.h" | |
| 25 | − | #include "window.h" | |
| 26 | − | ||
| 27 | − | void on_keypress(app_t*, XEvent*); | |
| 28 | − | void on_configurenotify(app_t*, XEvent*); | |
| 29 | − | void on_expose(app_t*, XEvent*); | |
| 30 | − | ||
| 31 | − | static void (*handler[LASTEvent])(app_t*, XEvent*) = { | |
| 32 | − | [Expose] = on_expose, | |
| 33 | − | [ConfigureNotify] = on_configurenotify, | |
| 34 | − | [KeyPress] = on_keypress | |
| 35 | − | }; | |
| 36 | − | ||
| 37 | − | void event_loop(app_t *app) { | |
| 38 | − | XEvent ev; | |
| 39 | − | ||
| 40 | − | while (!XNextEvent(app->win.env.dpy, &ev)) { | |
| 41 | − | if (handler[ev.type]) | |
| 42 | − | handler[ev.type](app, &ev); | |
| 43 | − | } | |
| 44 | − | } | |
| 45 | − | ||
| 46 | − | void on_keypress(app_t *app, XEvent *ev) { | |
| 47 | − | KeySym keysym; | |
| 48 | − | ||
| 49 | − | if (!app || !ev) | |
| 50 | − | return; | |
| 51 | − | ||
| 52 | − | keysym = XLookupKeysym(&ev->xkey, 0); | |
| 53 | − | ||
| 54 | − | switch (keysym) { | |
| 55 | − | case XK_Escape: | |
| 56 | − | app_quit(app); | |
| 57 | − | exit(1); | |
| 58 | − | case XK_q: | |
| 59 | − | app_quit(app); | |
| 60 | − | exit(0); | |
| 61 | − | } | |
| 62 | − | } | |
| 63 | − | ||
| 64 | − | void on_configurenotify(app_t *app, XEvent *ev) { | |
| 65 | − | if (!app || !ev) | |
| 66 | − | return; | |
| 67 | − | ||
| 68 | − | win_configure(&app->win, &ev->xconfigure); | |
| 69 | − | } | |
| 70 | − | ||
| 71 | − | void on_expose(app_t *app, XEvent *ev) { | |
| 72 | − | } | |
| @@ -19,6 +19,8 @@ | |||
| 19 | 19 | #include <stdlib.h> | |
| 20 | 20 | #include <stdio.h> | |
| 21 | 21 | ||
| 22 | + | #include <Imlib2.h> | |
| 23 | + | ||
| 22 | 24 | #include "sxiv.h" | |
| 23 | 25 | #include "image.h" | |
| 24 | 26 | ||
| @@ -32,7 +34,12 @@ void imlib_init(win_t *win) { | |||
| 32 | 34 | imlib_context_set_drawable(win->xwin); | |
| 33 | 35 | } | |
| 34 | 36 | ||
| 35 | − | void img_load(img_t *img, char *filename) { | |
| 37 | + | void imlib_destroy() { | |
| 38 | + | if (imlib_context_get_image()) | |
| 39 | + | imlib_free_image(); | |
| 40 | + | } | |
| 41 | + | ||
| 42 | + | void img_load(img_t *img, const char *filename) { | |
| 36 | 43 | if (!img || !filename) | |
| 37 | 44 | return; | |
| 38 | 45 | ||
| @@ -53,7 +60,7 @@ void img_render(img_t *img, win_t *win) { | |||
| 53 | 60 | unsigned int sx, sy, sw, sh; | |
| 54 | 61 | unsigned int dx, dy, dw, dh; | |
| 55 | 62 | ||
| 56 | − | if (!img || !win) | |
| 63 | + | if (!img || !win || !imlib_context_get_image()) | |
| 57 | 64 | return; | |
| 58 | 65 | ||
| 59 | 66 | /* set zoom level to fit image into window */ | |
| @@ -36,13 +36,13 @@ typedef struct img_s { | |||
| 36 | 36 | int h; | |
| 37 | 37 | int x; | |
| 38 | 38 | int y; | |
| 39 | − | ||
| 40 | 39 | Imlib_Image *im; | |
| 41 | 40 | } img_t; | |
| 42 | 41 | ||
| 43 | 42 | void imlib_init(win_t*); | |
| 43 | + | void imlib_destroy(); | |
| 44 | 44 | ||
| 45 | − | void img_load(img_t*, char*); | |
| 45 | + | void img_load(img_t*, const char*); | |
| 46 | 46 | void img_render(img_t*, win_t*); | |
| 47 | 47 | ||
| 48 | 48 | #endif /* IMAGE_H */ | |
| @@ -18,20 +18,63 @@ | |||
| 18 | 18 | ||
| 19 | 19 | #include <stdlib.h> | |
| 20 | 20 | ||
| 21 | + | #include <X11/Xlib.h> | |
| 22 | + | #include <X11/keysym.h> | |
| 23 | + | ||
| 21 | 24 | #include "sxiv.h" | |
| 22 | − | #include "app.h" | |
| 25 | + | #include "image.h" | |
| 26 | + | #include "options.h" | |
| 27 | + | #include "window.h" | |
| 28 | + | ||
| 29 | + | void on_keypress(XEvent*); | |
| 30 | + | void on_configurenotify(XEvent*); | |
| 31 | + | void on_expose(XEvent*); | |
| 32 | + | ||
| 33 | + | static void (*handler[LASTEvent])(XEvent*) = { | |
| 34 | + | [Expose] = on_expose, | |
| 35 | + | [ConfigureNotify] = on_configurenotify, | |
| 36 | + | [KeyPress] = on_keypress | |
| 37 | + | }; | |
| 38 | + | ||
| 39 | + | img_t img; | |
| 40 | + | win_t win; | |
| 41 | + | unsigned int fileidx; | |
| 42 | + | ||
| 43 | + | void run() { | |
| 44 | + | XEvent ev; | |
| 23 | 45 | ||
| 24 | − | app_t app; | |
| 46 | + | while (!XNextEvent(win.env.dpy, &ev)) { | |
| 47 | + | if (handler[ev.type]) | |
| 48 | + | handler[ev.type](&ev); | |
| 49 | + | } | |
| 50 | + | } | |
| 25 | 51 | ||
| 26 | 52 | int main(int argc, char **argv) { | |
| 53 | + | if (parse_options(argc, argv) < 0) | |
| 54 | + | return 1; | |
| 55 | + | ||
| 56 | + | if (!options->filecnt) { | |
| 57 | + | print_usage(); | |
| 58 | + | exit(1); | |
| 59 | + | } | |
| 60 | + | ||
| 61 | + | fileidx = 0; | |
| 62 | + | ||
| 63 | + | img.zoom = 1.0; | |
| 64 | + | img.scalemode = SCALE_MODE; | |
| 65 | + | ||
| 66 | + | win.w = WIN_WIDTH; | |
| 67 | + | win.h = WIN_HEIGHT; | |
| 27 | 68 | ||
| 28 | − | // TODO: parse cmd line arguments properly | |
| 29 | − | app.filenames = argv + 1; | |
| 30 | − | app.filecnt = argc - 1; | |
| 69 | + | win_open(&win); | |
| 70 | + | imlib_init(&win); | |
| 31 | 71 | ||
| 32 | − | app_init(&app); | |
| 33 | − | app_run(&app); | |
| 34 | − | app_quit(&app); | |
| 72 | + | img_load(&img, options->filenames[fileidx]); | |
| 73 | + | img_render(&img, &win); | |
| 74 | + | ||
| 75 | + | run(); | |
| 76 | + | ||
| 77 | + | cleanup(); | |
| 35 | 78 | ||
| 36 | 79 | return 0; | |
| 37 | 80 | } | |
| @@ -39,6 +82,36 @@ int main(int argc, char **argv) { | |||
| 39 | 82 | void cleanup() { | |
| 40 | 83 | static int in = 0; | |
| 41 | 84 | ||
| 42 | − | if (!in++) | |
| 43 | − | app_quit(&app); | |
| 85 | + | if (!in++) { | |
| 86 | + | imlib_destroy(); | |
| 87 | + | win_close(&win); | |
| 88 | + | } | |
| 89 | + | } | |
| 90 | + | ||
| 91 | + | void on_keypress(XEvent *ev) { | |
| 92 | + | KeySym keysym; | |
| 93 | + | ||
| 94 | + | if (!ev) | |
| 95 | + | return; | |
| 96 | + | ||
| 97 | + | keysym = XLookupKeysym(&ev->xkey, 0); | |
| 98 | + | ||
| 99 | + | switch (keysym) { | |
| 100 | + | case XK_Escape: | |
| 101 | + | cleanup(); | |
| 102 | + | exit(1); | |
| 103 | + | case XK_q: | |
| 104 | + | cleanup(); | |
| 105 | + | exit(0); | |
| 106 | + | } | |
| 107 | + | } | |
| 108 | + | ||
| 109 | + | void on_configurenotify(XEvent *ev) { | |
| 110 | + | if (!ev) | |
| 111 | + | return; | |
| 112 | + | ||
| 113 | + | win_configure(&win, &ev->xconfigure); | |
| 114 | + | } | |
| 115 | + | ||
| 116 | + | void on_expose(XEvent *ev) { | |
| 44 | 117 | } | |
| @@ -1,4 +1,4 @@ | |||
| 1 | − | /* sxiv: app.c | |
| 1 | + | /* sxiv: options.c | |
| 2 | 2 | * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> | |
| 3 | 3 | * | |
| 4 | 4 | * This program is free software; you can redistribute it and/or modify | |
| @@ -16,42 +16,45 @@ | |||
| 16 | 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| 17 | 17 | */ | |
| 18 | 18 | ||
| 19 | − | #include <X11/Xlib.h> | |
| 19 | + | #define _XOPEN_SOURCE | |
| 20 | 20 | ||
| 21 | − | #include "sxiv.h" | |
| 22 | − | #include "app.h" | |
| 23 | − | #include "events.h" | |
| 24 | − | ||
| 25 | − | void app_init(app_t *app) { | |
| 26 | − | if (!app) | |
| 27 | − | return; | |
| 28 | − | ||
| 29 | − | app->fileidx = 0; | |
| 30 | − | ||
| 31 | − | app->img.zoom = 1.0; | |
| 32 | − | app->img.scalemode = SCALE_MODE; | |
| 21 | + | #include <stdlib.h> | |
| 22 | + | #include <stdio.h> | |
| 23 | + | #include <unistd.h> | |
| 33 | 24 | ||
| 34 | − | app->win.w = WIN_WIDTH; | |
| 35 | − | app->win.h = WIN_HEIGHT; | |
| 25 | + | #include "sxiv.h" | |
| 26 | + | #include "options.h" | |
| 36 | 27 | ||
| 37 | − | win_open(&app->win); | |
| 38 | − | ||
| 39 | − | imlib_init(&app->win); | |
| 40 | − | } | |
| 28 | + | options_t _options; | |
| 29 | + | const options_t *options = (const options_t*) &_options; | |
| 41 | 30 | ||
| 42 | − | void app_run(app_t *app) { | |
| 43 | − | app_load_image(app); | |
| 44 | − | event_loop(app); | |
| 31 | + | void print_usage() { | |
| 32 | + | printf("usage: sxiv [-hv] FILES...\n"); | |
| 45 | 33 | } | |
| 46 | 34 | ||
| 47 | − | void app_quit(app_t *app) { | |
| 35 | + | void print_version() { | |
| 36 | + | printf("sxiv - simple x image viewer\n"); | |
| 37 | + | printf("Version %s, written by Bert Muennich\n", VERSION); | |
| 48 | 38 | } | |
| 49 | 39 | ||
| 50 | − | void app_load_image(app_t *app) { | |
| 51 | − | if (!app || app->fileidx >= app->filecnt || !app->filenames) | |
| 52 | − | return; | |
| 53 | − | ||
| 54 | − | img_load(&app->img, app->filenames[app->fileidx]); | |
| 55 | − | ||
| 56 | − | img_render(&app->img, &app->win); | |
| 40 | + | int parse_options(int argc, char **argv) { | |
| 41 | + | int opt; | |
| 42 | + | ||
| 43 | + | _options.filenames = (const char**) argv + 1; | |
| 44 | + | _options.filecnt = argc - 1; | |
| 45 | + | ||
| 46 | + | while ((opt = getopt(argc, argv, "hv")) != -1) { | |
| 47 | + | switch (opt) { | |
| 48 | + | case '?': | |
| 49 | + | return -1; | |
| 50 | + | case 'h': | |
| 51 | + | print_usage(); | |
| 52 | + | exit(0); | |
| 53 | + | case 'v': | |
| 54 | + | print_version(); | |
| 55 | + | exit(0); | |
| 56 | + | } | |
| 57 | + | } | |
| 58 | + | ||
| 59 | + | return 0; | |
| 57 | 60 | } | |
| @@ -1,4 +1,4 @@ | |||
| 1 | − | /* sxiv: events.h | |
| 1 | + | /* sxiv: options.h | |
| 2 | 2 | * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de> | |
| 3 | 3 | * | |
| 4 | 4 | * This program is free software; you can redistribute it and/or modify | |
| @@ -16,11 +16,19 @@ | |||
| 16 | 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| 17 | 17 | */ | |
| 18 | 18 | ||
| 19 | − | #ifndef EVENTS_H | |
| 20 | − | #define EVENTS_H | |
| 19 | + | #ifndef OPTIONS_H | |
| 20 | + | #define OPTIONS_H | |
| 21 | 21 | ||
| 22 | − | #include "app.h" | |
| 22 | + | typedef struct options_s { | |
| 23 | + | const char **filenames; | |
| 24 | + | unsigned int filecnt; | |
| 25 | + | } options_t; | |
| 23 | 26 | ||
| 24 | − | void event_loop(app_t*); | |
| 27 | + | extern const options_t *options; | |
| 25 | 28 | ||
| 26 | − | #endif /* EVENTS_H */ | |
| 29 | + | void print_usage(); | |
| 30 | + | void print_version(); | |
| 31 | + | ||
| 32 | + | int parse_options(int, char**); | |
| 33 | + | ||
| 34 | + | #endif /* OPTIONS_H */ | |
| @@ -21,7 +21,7 @@ | |||
| 21 | 21 | ||
| 22 | 22 | #include "config.h" | |
| 23 | 23 | ||
| 24 | − | #define VERSION "git-20110117" | |
| 24 | + | #define VERSION "git-20110119" | |
| 25 | 25 | ||
| 26 | 26 | #define MIN(a,b) ((a) < (b) ? (a) : (b)) | |
| 27 | 27 | #define MAX(a,b) ((a) > (b) ? (a) : (b)) | |