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