| 1 | /* sxiv: main.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 <X11/Xlib.h> |
| 23 | #include <X11/keysym.h> |
| 24 | |
| 25 | #include "sxiv.h" |
| 26 | #include "image.h" |
| 27 | #include "options.h" |
| 28 | #include "window.h" |
| 29 | |
| 30 | void on_keypress(XEvent*); |
| 31 | void on_configurenotify(XEvent*); |
| 32 | void on_expose(XEvent*); |
| 33 | |
| 34 | void update_title(); |
| 35 | |
| 36 | static void (*handler[LASTEvent])(XEvent*) = { |
| 37 | [Expose] = on_expose, |
| 38 | [ConfigureNotify] = on_configurenotify, |
| 39 | [KeyPress] = on_keypress |
| 40 | }; |
| 41 | |
| 42 | img_t img; |
| 43 | win_t win; |
| 44 | |
| 45 | const char **filenames; |
| 46 | unsigned int filecnt; |
| 47 | unsigned int fileidx; |
| 48 | |
| 49 | #define TITLE_LEN 256 |
| 50 | char win_title[TITLE_LEN]; |
| 51 | |
| 52 | void run() { |
| 53 | XEvent ev; |
| 54 | |
| 55 | while (!XNextEvent(win.env.dpy, &ev)) { |
| 56 | if (handler[ev.type]) |
| 57 | handler[ev.type](&ev); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | int main(int argc, char **argv) { |
| 62 | int i; |
| 63 | |
| 64 | parse_options(argc, argv); |
| 65 | |
| 66 | if (!options->filecnt) { |
| 67 | print_usage(); |
| 68 | exit(1); |
| 69 | } |
| 70 | |
| 71 | if (!(filenames = (const char**) malloc(options->filecnt * sizeof(char*)))) |
| 72 | DIE("could not allocate memory"); |
| 73 | |
| 74 | fileidx = 0; |
| 75 | filecnt = 0; |
| 76 | |
| 77 | for (i = 0; i < options->filecnt; ++i) { |
| 78 | if (!(img_load(&img, options->filenames[i]) < 0)) |
| 79 | filenames[filecnt++] = options->filenames[i]; |
| 80 | } |
| 81 | |
| 82 | if (!filecnt) { |
| 83 | fprintf(stderr, "sxiv: no valid image filename given, aborting\n"); |
| 84 | exit(1); |
| 85 | } |
| 86 | |
| 87 | img.zoom = 1.0; |
| 88 | img.scalemode = SCALE_MODE; |
| 89 | |
| 90 | win.w = WIN_WIDTH; |
| 91 | win.h = WIN_HEIGHT; |
| 92 | |
| 93 | win_open(&win); |
| 94 | imlib_init(&win); |
| 95 | |
| 96 | img_load(&img, filenames[fileidx]); |
| 97 | img_display(&img, &win); |
| 98 | update_title(); |
| 99 | |
| 100 | run(); |
| 101 | |
| 102 | cleanup(); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | void cleanup() { |
| 108 | static int in = 0; |
| 109 | |
| 110 | if (!in++) { |
| 111 | imlib_destroy(); |
| 112 | win_close(&win); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void on_keypress(XEvent *ev) { |
| 117 | KeySym keysym; |
| 118 | |
| 119 | if (!ev) |
| 120 | return; |
| 121 | |
| 122 | keysym = XLookupKeysym(&ev->xkey, 0); |
| 123 | |
| 124 | switch (keysym) { |
| 125 | case XK_Escape: |
| 126 | cleanup(); |
| 127 | exit(1); |
| 128 | case XK_q: |
| 129 | cleanup(); |
| 130 | exit(0); |
| 131 | case XK_n: |
| 132 | case XK_space: |
| 133 | if (fileidx + 1 < filecnt) { |
| 134 | img_load(&img, filenames[++fileidx]); |
| 135 | img_display(&img, &win); |
| 136 | update_title(); |
| 137 | } |
| 138 | break; |
| 139 | case XK_p: |
| 140 | case XK_BackSpace: |
| 141 | if (fileidx > 0) { |
| 142 | img_load(&img, filenames[--fileidx]); |
| 143 | img_display(&img, &win); |
| 144 | update_title(); |
| 145 | } |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | void on_configurenotify(XEvent *ev) { |
| 151 | if (!ev) |
| 152 | return; |
| 153 | |
| 154 | win_configure(&win, &ev->xconfigure); |
| 155 | } |
| 156 | |
| 157 | void on_expose(XEvent *ev) { |
| 158 | if (!ev) |
| 159 | return; |
| 160 | |
| 161 | img_render(&img, &win, ev->xexpose.x, ev->xexpose.y, |
| 162 | ev->xexpose.width, ev->xexpose.height); |
| 163 | } |
| 164 | |
| 165 | void update_title() { |
| 166 | int n; |
| 167 | |
| 168 | n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1, |
| 169 | filecnt, (int) (img.zoom * 100.0), filenames[fileidx]); |
| 170 | |
| 171 | if (n >= TITLE_LEN) { |
| 172 | win_title[TITLE_LEN - 2] = '.'; |
| 173 | win_title[TITLE_LEN - 3] = '.'; |
| 174 | win_title[TITLE_LEN - 4] = '.'; |
| 175 | } |
| 176 | |
| 177 | win_set_title(&win, win_title); |
| 178 | } |