| 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 | #include <sys/select.h> |
| 22 | |
| 23 | #include <X11/Xlib.h> |
| 24 | #include <X11/Xutil.h> |
| 25 | #include <X11/keysym.h> |
| 26 | |
| 27 | #include "sxiv.h" |
| 28 | #include "image.h" |
| 29 | #include "options.h" |
| 30 | #include "window.h" |
| 31 | |
| 32 | void on_keypress(XEvent*); |
| 33 | void on_buttonpress(XEvent*); |
| 34 | void on_configurenotify(XEvent*); |
| 35 | |
| 36 | void update_title(); |
| 37 | |
| 38 | static void (*handler[LASTEvent])(XEvent*) = { |
| 39 | [KeyPress] = on_keypress, |
| 40 | [ButtonPress] = on_buttonpress, |
| 41 | [ConfigureNotify] = on_configurenotify |
| 42 | }; |
| 43 | |
| 44 | img_t img; |
| 45 | win_t win; |
| 46 | |
| 47 | const char **filenames; |
| 48 | int filecnt, fileidx; |
| 49 | |
| 50 | unsigned char timeout; |
| 51 | |
| 52 | #define TITLE_LEN 256 |
| 53 | char win_title[TITLE_LEN]; |
| 54 | |
| 55 | void run() { |
| 56 | int xfd; |
| 57 | fd_set fds; |
| 58 | struct timeval t; |
| 59 | XEvent ev; |
| 60 | |
| 61 | timeout = 0; |
| 62 | |
| 63 | while (1) { |
| 64 | if (timeout) { |
| 65 | t.tv_sec = 0; |
| 66 | t.tv_usec = 250; |
| 67 | xfd = ConnectionNumber(win.env.dpy); |
| 68 | FD_ZERO(&fds); |
| 69 | FD_SET(xfd, &fds); |
| 70 | |
| 71 | if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) { |
| 72 | img_render(&img, &win); |
| 73 | timeout = 0; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if (!XNextEvent(win.env.dpy, &ev) && handler[ev.type]) |
| 78 | handler[ev.type](&ev); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | int main(int argc, char **argv) { |
| 83 | int i; |
| 84 | |
| 85 | parse_options(argc, argv); |
| 86 | |
| 87 | if (!options->filecnt) { |
| 88 | print_usage(); |
| 89 | exit(1); |
| 90 | } |
| 91 | |
| 92 | if (!(filenames = (const char**) malloc(options->filecnt * sizeof(char*)))) |
| 93 | DIE("could not allocate memory"); |
| 94 | |
| 95 | fileidx = 0; |
| 96 | filecnt = 0; |
| 97 | |
| 98 | for (i = 0; i < options->filecnt; ++i) { |
| 99 | if (!(img_load(&img, options->filenames[i]) < 0)) |
| 100 | filenames[filecnt++] = options->filenames[i]; |
| 101 | } |
| 102 | |
| 103 | if (!filecnt) { |
| 104 | fprintf(stderr, "sxiv: no valid image filename given, aborting\n"); |
| 105 | exit(1); |
| 106 | } |
| 107 | |
| 108 | win_open(&win); |
| 109 | img_init(&img, &win); |
| 110 | |
| 111 | img_load(&img, filenames[fileidx]); |
| 112 | img_render(&img, &win); |
| 113 | update_title(); |
| 114 | |
| 115 | run(); |
| 116 | |
| 117 | cleanup(); |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | void cleanup() { |
| 123 | static int in = 0; |
| 124 | |
| 125 | if (!in++) { |
| 126 | img_free(&img); |
| 127 | win_close(&win); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void on_keypress(XEvent *ev) { |
| 132 | char key; |
| 133 | KeySym keysym; |
| 134 | int changed; |
| 135 | |
| 136 | if (!ev) |
| 137 | return; |
| 138 | |
| 139 | XLookupString(&ev->xkey, &key, 1, &keysym, NULL); |
| 140 | changed = 0; |
| 141 | |
| 142 | switch (keysym) { |
| 143 | case XK_Escape: |
| 144 | cleanup(); |
| 145 | exit(2); |
| 146 | case XK_space: |
| 147 | key = 'n'; |
| 148 | break; |
| 149 | case XK_BackSpace: |
| 150 | key = 'p'; |
| 151 | break; |
| 152 | case XK_Left: |
| 153 | key = 'h'; |
| 154 | break; |
| 155 | case XK_Down: |
| 156 | key = 'j'; |
| 157 | break; |
| 158 | case XK_Up: |
| 159 | key = 'k'; |
| 160 | break; |
| 161 | case XK_Right: |
| 162 | key = 'l'; |
| 163 | break; |
| 164 | } |
| 165 | |
| 166 | switch (key) { |
| 167 | case 'q': |
| 168 | cleanup(); |
| 169 | exit(0); |
| 170 | |
| 171 | /* navigate image list */ |
| 172 | case 'n': |
| 173 | if (fileidx + 1 < filecnt) { |
| 174 | img_load(&img, filenames[++fileidx]); |
| 175 | changed = 1; |
| 176 | } |
| 177 | break; |
| 178 | case 'p': |
| 179 | if (fileidx > 0) { |
| 180 | img_load(&img, filenames[--fileidx]); |
| 181 | changed = 1; |
| 182 | } |
| 183 | break; |
| 184 | case '[': |
| 185 | if (fileidx != 0) { |
| 186 | fileidx = MAX(0, fileidx - 10); |
| 187 | img_load(&img, filenames[fileidx]); |
| 188 | changed = 1; |
| 189 | } |
| 190 | break; |
| 191 | case ']': |
| 192 | if (fileidx != filecnt - 1) { |
| 193 | fileidx = MIN(fileidx + 10, filecnt - 1); |
| 194 | img_load(&img, filenames[fileidx]); |
| 195 | changed = 1; |
| 196 | } |
| 197 | break; |
| 198 | case 'g': |
| 199 | if (fileidx != 0) { |
| 200 | fileidx = 0; |
| 201 | img_load(&img, filenames[fileidx]); |
| 202 | changed = 1; |
| 203 | } |
| 204 | break; |
| 205 | case 'G': |
| 206 | if (fileidx != filecnt - 1) { |
| 207 | fileidx = filecnt - 1; |
| 208 | img_load(&img, filenames[fileidx]); |
| 209 | changed = 1; |
| 210 | } |
| 211 | break; |
| 212 | |
| 213 | /* zooming */ |
| 214 | case '+': |
| 215 | case '=': |
| 216 | changed = img_zoom_in(&img); |
| 217 | break; |
| 218 | case '-': |
| 219 | changed = img_zoom_out(&img); |
| 220 | break; |
| 221 | |
| 222 | /* panning */ |
| 223 | case 'h': |
| 224 | changed = img_pan(&img, &win, PAN_LEFT); |
| 225 | break; |
| 226 | case 'j': |
| 227 | changed = img_pan(&img, &win, PAN_DOWN); |
| 228 | break; |
| 229 | case 'k': |
| 230 | changed = img_pan(&img, &win, PAN_UP); |
| 231 | break; |
| 232 | case 'l': |
| 233 | changed = img_pan(&img, &win, PAN_RIGHT); |
| 234 | break; |
| 235 | |
| 236 | /* rotation */ |
| 237 | case '<': |
| 238 | changed = img_rotate_left(&img, &win); |
| 239 | break; |
| 240 | case '>': |
| 241 | changed = img_rotate_right(&img, &win); |
| 242 | break; |
| 243 | |
| 244 | /* control window */ |
| 245 | case 'f': |
| 246 | win_toggle_fullscreen(&win); |
| 247 | break; |
| 248 | |
| 249 | /* miscellaneous */ |
| 250 | case 'a': |
| 251 | changed = img_toggle_antialias(&img); |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | if (changed) { |
| 256 | img_render(&img, &win); |
| 257 | update_title(); |
| 258 | timeout = 0; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void on_buttonpress(XEvent *ev) { |
| 263 | int changed; |
| 264 | unsigned int mask; |
| 265 | |
| 266 | if (!ev) |
| 267 | return; |
| 268 | |
| 269 | mask = CLEANMASK(ev->xbutton.state); |
| 270 | changed = 0; |
| 271 | |
| 272 | switch (ev->xbutton.button) { |
| 273 | case Button1: |
| 274 | if (fileidx + 1 < filecnt) { |
| 275 | img_load(&img, filenames[++fileidx]); |
| 276 | changed = 1; |
| 277 | } |
| 278 | break; |
| 279 | case Button3: |
| 280 | if (fileidx > 0) { |
| 281 | img_load(&img, filenames[--fileidx]); |
| 282 | changed = 1; |
| 283 | } |
| 284 | break; |
| 285 | case Button4: |
| 286 | if (mask == ControlMask) |
| 287 | changed = img_zoom_in(&img); |
| 288 | else if (mask == ShiftMask) |
| 289 | changed = img_pan(&img, &win, PAN_LEFT); |
| 290 | else |
| 291 | changed = img_pan(&img, &win, PAN_UP); |
| 292 | break; |
| 293 | case Button5: |
| 294 | if (mask == ControlMask) |
| 295 | changed = img_zoom_out(&img); |
| 296 | else if (mask == ShiftMask) |
| 297 | changed = img_pan(&img, &win, PAN_RIGHT); |
| 298 | else |
| 299 | changed = img_pan(&img, &win, PAN_DOWN); |
| 300 | break; |
| 301 | case 6: |
| 302 | changed = img_pan(&img, &win, PAN_LEFT); |
| 303 | break; |
| 304 | case 7: |
| 305 | changed = img_pan(&img, &win, PAN_RIGHT); |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | if (changed) { |
| 310 | img_render(&img, &win); |
| 311 | update_title(); |
| 312 | timeout = 0; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void on_configurenotify(XEvent *ev) { |
| 317 | if (!ev) |
| 318 | return; |
| 319 | |
| 320 | if (win_configure(&win, &ev->xconfigure)) { |
| 321 | img.checkpan = 1; |
| 322 | timeout = 1; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | void update_title() { |
| 327 | int n; |
| 328 | |
| 329 | n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> %s", fileidx + 1, |
| 330 | filecnt, (int) (img.zoom * 100.0), filenames[fileidx]); |
| 331 | |
| 332 | if (n >= TITLE_LEN) { |
| 333 | win_title[TITLE_LEN - 2] = '.'; |
| 334 | win_title[TITLE_LEN - 3] = '.'; |
| 335 | win_title[TITLE_LEN - 4] = '.'; |
| 336 | } |
| 337 | |
| 338 | win_set_title(&win, win_title); |
| 339 | } |