| 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 <string.h> |
| 22 | #include <dirent.h> |
| 23 | #include <sys/select.h> |
| 24 | #include <sys/stat.h> |
| 25 | |
| 26 | #include <X11/Xlib.h> |
| 27 | #include <X11/Xutil.h> |
| 28 | #include <X11/keysym.h> |
| 29 | |
| 30 | #include "image.h" |
| 31 | #include "options.h" |
| 32 | #include "util.h" |
| 33 | #include "window.h" |
| 34 | |
| 35 | void update_title(); |
| 36 | int check_append(const char*); |
| 37 | void read_dir_rec(const char*); |
| 38 | void run(); |
| 39 | |
| 40 | img_t img; |
| 41 | win_t win; |
| 42 | |
| 43 | #define DNAME_CNT 512 |
| 44 | #define FNAME_CNT 4096 |
| 45 | const char **filenames; |
| 46 | int filecnt, fileidx; |
| 47 | size_t filesize; |
| 48 | |
| 49 | #define TITLE_LEN 256 |
| 50 | char win_title[TITLE_LEN]; |
| 51 | |
| 52 | void cleanup() { |
| 53 | static int in = 0; |
| 54 | |
| 55 | if (!in++) { |
| 56 | img_free(&img); |
| 57 | win_close(&win); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | int load_image() { |
| 62 | struct stat fstats; |
| 63 | |
| 64 | if (!stat(filenames[fileidx], &fstats)) |
| 65 | filesize = fstats.st_size; |
| 66 | else |
| 67 | filesize = 0; |
| 68 | |
| 69 | return img_load(&img, filenames[fileidx]); |
| 70 | } |
| 71 | |
| 72 | int main(int argc, char **argv) { |
| 73 | int i; |
| 74 | const char *filename; |
| 75 | struct stat fstats; |
| 76 | |
| 77 | parse_options(argc, argv); |
| 78 | |
| 79 | if (!options->filecnt) { |
| 80 | print_usage(); |
| 81 | exit(1); |
| 82 | } |
| 83 | |
| 84 | if (options->recursive) |
| 85 | filecnt = FNAME_CNT; |
| 86 | else |
| 87 | filecnt = options->filecnt; |
| 88 | |
| 89 | filenames = (const char**) s_malloc(filecnt * sizeof(const char*)); |
| 90 | fileidx = 0; |
| 91 | |
| 92 | for (i = 0; i < options->filecnt; ++i) { |
| 93 | filename = options->filenames[i]; |
| 94 | if (!stat(filename, &fstats) && S_ISDIR(fstats.st_mode)) { |
| 95 | if (options->recursive) |
| 96 | read_dir_rec(filename); |
| 97 | else |
| 98 | warn("ignoring directory: %s", filename); |
| 99 | } else { |
| 100 | check_append(filename); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | filecnt = fileidx; |
| 105 | fileidx = 0; |
| 106 | |
| 107 | if (!filecnt) { |
| 108 | fprintf(stderr, "sxiv: no valid image filename given, aborting\n"); |
| 109 | exit(1); |
| 110 | } |
| 111 | |
| 112 | win_open(&win); |
| 113 | img_init(&img, &win); |
| 114 | |
| 115 | load_image(); |
| 116 | img_render(&img, &win); |
| 117 | update_title(); |
| 118 | |
| 119 | run(); |
| 120 | |
| 121 | cleanup(); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | void update_title() { |
| 127 | int n; |
| 128 | float size; |
| 129 | const char *unit; |
| 130 | |
| 131 | size = filesize; |
| 132 | size_readable(&size, &unit); |
| 133 | |
| 134 | n = snprintf(win_title, TITLE_LEN, "sxiv: [%d/%d] <%d%%> (%.2f%s) %s", |
| 135 | fileidx + 1, filecnt, (int) (img.zoom * 100.0), size, unit, |
| 136 | filenames[fileidx]); |
| 137 | if (n >= TITLE_LEN) { |
| 138 | win_title[TITLE_LEN - 2] = '.'; |
| 139 | win_title[TITLE_LEN - 3] = '.'; |
| 140 | win_title[TITLE_LEN - 4] = '.'; |
| 141 | } |
| 142 | |
| 143 | win_set_title(&win, win_title); |
| 144 | } |
| 145 | |
| 146 | int check_append(const char *filename) { |
| 147 | if (!filename) |
| 148 | return 0; |
| 149 | |
| 150 | if (img_check(filename)) { |
| 151 | if (fileidx == filecnt) { |
| 152 | filecnt *= 2; |
| 153 | filenames = (const char**) s_realloc(filenames, |
| 154 | filecnt * sizeof(const char*)); |
| 155 | } |
| 156 | filenames[fileidx++] = filename; |
| 157 | return 1; |
| 158 | } else { |
| 159 | return 0; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | void read_dir_rec(const char *dirname) { |
| 164 | char *filename; |
| 165 | const char **dirnames; |
| 166 | int dircnt, diridx; |
| 167 | unsigned char first; |
| 168 | size_t len; |
| 169 | DIR *dir; |
| 170 | struct dirent *dentry; |
| 171 | struct stat fstats; |
| 172 | |
| 173 | if (!dirname) |
| 174 | return; |
| 175 | |
| 176 | dircnt = DNAME_CNT; |
| 177 | diridx = first = 1; |
| 178 | dirnames = (const char**) s_malloc(dircnt * sizeof(const char*)); |
| 179 | dirnames[0] = dirname; |
| 180 | |
| 181 | while (diridx > 0) { |
| 182 | dirname = dirnames[--diridx]; |
| 183 | if (!(dir = opendir(dirname))) { |
| 184 | warn("could not open directory: %s", dirname); |
| 185 | } else { |
| 186 | while ((dentry = readdir(dir))) { |
| 187 | if (!strcmp(dentry->d_name, ".") || !strcmp(dentry->d_name, "..")) |
| 188 | continue; |
| 189 | |
| 190 | len = strlen(dirname) + strlen(dentry->d_name) + 2; |
| 191 | filename = (char*) s_malloc(len * sizeof(char)); |
| 192 | snprintf(filename, len, "%s/%s", dirname, dentry->d_name); |
| 193 | |
| 194 | if (!stat(filename, &fstats) && S_ISDIR(fstats.st_mode)) { |
| 195 | if (diridx == dircnt) { |
| 196 | dircnt *= 2; |
| 197 | dirnames = (const char**) s_realloc(dirnames, |
| 198 | dircnt * sizeof(const char*)); |
| 199 | } |
| 200 | dirnames[diridx++] = filename; |
| 201 | } else { |
| 202 | if (!check_append(filename)) |
| 203 | free(filename); |
| 204 | } |
| 205 | } |
| 206 | closedir(dir); |
| 207 | } |
| 208 | |
| 209 | if (!first) |
| 210 | free((void*) dirname); |
| 211 | else |
| 212 | first = 0; |
| 213 | } |
| 214 | |
| 215 | free(dirnames); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /* event handling */ |
| 220 | |
| 221 | unsigned char timeout; |
| 222 | int mox, moy; |
| 223 | |
| 224 | void on_keypress(XKeyEvent *kev) { |
| 225 | int x, y; |
| 226 | unsigned int w, h; |
| 227 | char key; |
| 228 | KeySym ksym; |
| 229 | int changed; |
| 230 | |
| 231 | if (!kev) |
| 232 | return; |
| 233 | |
| 234 | XLookupString(kev, &key, 1, &ksym, NULL); |
| 235 | changed = 0; |
| 236 | |
| 237 | switch (ksym) { |
| 238 | case XK_Escape: |
| 239 | cleanup(); |
| 240 | exit(2); |
| 241 | case XK_q: |
| 242 | cleanup(); |
| 243 | exit(0); |
| 244 | |
| 245 | /* navigate image list */ |
| 246 | case XK_n: |
| 247 | case XK_space: |
| 248 | if (fileidx + 1 < filecnt) { |
| 249 | ++fileidx; |
| 250 | changed = load_image(); |
| 251 | } |
| 252 | break; |
| 253 | case XK_p: |
| 254 | case XK_BackSpace: |
| 255 | if (fileidx > 0) { |
| 256 | --fileidx; |
| 257 | changed = load_image(); |
| 258 | } |
| 259 | break; |
| 260 | case XK_bracketleft: |
| 261 | if (fileidx != 0) { |
| 262 | fileidx = MAX(0, fileidx - 10); |
| 263 | changed = load_image(); |
| 264 | } |
| 265 | break; |
| 266 | case XK_bracketright: |
| 267 | if (fileidx != filecnt - 1) { |
| 268 | fileidx = MIN(fileidx + 10, filecnt - 1); |
| 269 | changed = load_image(); |
| 270 | } |
| 271 | break; |
| 272 | case XK_g: |
| 273 | if (fileidx != 0) { |
| 274 | fileidx = 0; |
| 275 | changed = load_image(); |
| 276 | } |
| 277 | break; |
| 278 | case XK_G: |
| 279 | if (fileidx != filecnt - 1) { |
| 280 | fileidx = filecnt - 1; |
| 281 | changed = load_image(); |
| 282 | } |
| 283 | break; |
| 284 | |
| 285 | /* zooming */ |
| 286 | case XK_plus: |
| 287 | case XK_equal: |
| 288 | changed = img_zoom_in(&img); |
| 289 | break; |
| 290 | case XK_minus: |
| 291 | changed = img_zoom_out(&img); |
| 292 | break; |
| 293 | case XK_0: |
| 294 | changed = img_zoom(&img, 1.0); |
| 295 | break; |
| 296 | case XK_w: |
| 297 | if ((changed = img_fit_win(&img, &win))) |
| 298 | img_center(&img, &win); |
| 299 | break; |
| 300 | |
| 301 | /* panning */ |
| 302 | case XK_h: |
| 303 | case XK_Left: |
| 304 | changed = img_pan(&img, &win, PAN_LEFT); |
| 305 | break; |
| 306 | case XK_j: |
| 307 | case XK_Down: |
| 308 | changed = img_pan(&img, &win, PAN_DOWN); |
| 309 | break; |
| 310 | case XK_k: |
| 311 | case XK_Up: |
| 312 | changed = img_pan(&img, &win, PAN_UP); |
| 313 | break; |
| 314 | case XK_l: |
| 315 | case XK_Right: |
| 316 | changed = img_pan(&img, &win, PAN_RIGHT); |
| 317 | break; |
| 318 | |
| 319 | /* rotation */ |
| 320 | case XK_less: |
| 321 | img_rotate_left(&img, &win); |
| 322 | changed = 1; |
| 323 | break; |
| 324 | case XK_greater: |
| 325 | img_rotate_right(&img, &win); |
| 326 | changed = 1; |
| 327 | break; |
| 328 | |
| 329 | /* control window */ |
| 330 | case XK_f: |
| 331 | win_toggle_fullscreen(&win); |
| 332 | /* render on next configurenotify */ |
| 333 | break; |
| 334 | case XK_W: |
| 335 | x = win.x + img.x; |
| 336 | y = win.y + img.y; |
| 337 | w = img.w * img.zoom; |
| 338 | h = img.h * img.zoom; |
| 339 | if ((changed = win_moveresize(&win, x, y, w, h))) { |
| 340 | img.x = x - win.x; |
| 341 | img.y = y - win.y; |
| 342 | } |
| 343 | break; |
| 344 | |
| 345 | /* miscellaneous */ |
| 346 | case XK_a: |
| 347 | img_toggle_antialias(&img); |
| 348 | changed = 1; |
| 349 | break; |
| 350 | case XK_r: |
| 351 | changed = load_image(); |
| 352 | break; |
| 353 | ; |
| 354 | } |
| 355 | |
| 356 | if (changed) { |
| 357 | img_render(&img, &win); |
| 358 | update_title(); |
| 359 | timeout = 0; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | void on_buttonpress(XButtonEvent *bev) { |
| 364 | int changed; |
| 365 | unsigned int mask; |
| 366 | |
| 367 | if (!bev) |
| 368 | return; |
| 369 | |
| 370 | mask = CLEANMASK(bev->state); |
| 371 | changed = 0; |
| 372 | |
| 373 | switch (bev->button) { |
| 374 | case Button1: |
| 375 | if (fileidx + 1 < filecnt) { |
| 376 | ++fileidx; |
| 377 | changed = load_image(); |
| 378 | } |
| 379 | break; |
| 380 | case Button2: |
| 381 | mox = bev->x; |
| 382 | moy = bev->y; |
| 383 | win_set_cursor(&win, CURSOR_HAND); |
| 384 | break; |
| 385 | case Button3: |
| 386 | if (fileidx > 0) { |
| 387 | --fileidx; |
| 388 | changed = load_image(); |
| 389 | } |
| 390 | break; |
| 391 | case Button4: |
| 392 | if (mask == ControlMask) |
| 393 | changed = img_zoom_in(&img); |
| 394 | else if (mask == ShiftMask) |
| 395 | changed = img_pan(&img, &win, PAN_LEFT); |
| 396 | else |
| 397 | changed = img_pan(&img, &win, PAN_UP); |
| 398 | break; |
| 399 | case Button5: |
| 400 | if (mask == ControlMask) |
| 401 | changed = img_zoom_out(&img); |
| 402 | else if (mask == ShiftMask) |
| 403 | changed = img_pan(&img, &win, PAN_RIGHT); |
| 404 | else |
| 405 | changed = img_pan(&img, &win, PAN_DOWN); |
| 406 | break; |
| 407 | case 6: |
| 408 | changed = img_pan(&img, &win, PAN_LEFT); |
| 409 | break; |
| 410 | case 7: |
| 411 | changed = img_pan(&img, &win, PAN_RIGHT); |
| 412 | break; |
| 413 | } |
| 414 | |
| 415 | if (changed) { |
| 416 | img_render(&img, &win); |
| 417 | update_title(); |
| 418 | timeout = 0; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | void on_motionnotify(XMotionEvent *mev) { |
| 423 | if (!mev) |
| 424 | return; |
| 425 | |
| 426 | if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) { |
| 427 | if (img_move(&img, &win, mev->x - mox, mev->y - moy)) |
| 428 | timeout = 1; |
| 429 | |
| 430 | mox = mev->x; |
| 431 | moy = mev->y; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | void run() { |
| 436 | int xfd; |
| 437 | fd_set fds; |
| 438 | struct timeval t; |
| 439 | XEvent ev; |
| 440 | |
| 441 | timeout = 0; |
| 442 | |
| 443 | while (1) { |
| 444 | if (timeout) { |
| 445 | t.tv_sec = 0; |
| 446 | t.tv_usec = 250; |
| 447 | xfd = ConnectionNumber(win.env.dpy); |
| 448 | FD_ZERO(&fds); |
| 449 | FD_SET(xfd, &fds); |
| 450 | |
| 451 | if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) { |
| 452 | img_render(&img, &win); |
| 453 | timeout = 0; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | if (!XNextEvent(win.env.dpy, &ev)) { |
| 458 | switch (ev.type) { |
| 459 | case KeyPress: |
| 460 | on_keypress(&ev.xkey); |
| 461 | break; |
| 462 | case ButtonPress: |
| 463 | on_buttonpress(&ev.xbutton); |
| 464 | break; |
| 465 | case ButtonRelease: |
| 466 | if (ev.xbutton.button == Button2) |
| 467 | win_set_cursor(&win, CURSOR_ARROW); |
| 468 | break; |
| 469 | case MotionNotify: |
| 470 | on_motionnotify(&ev.xmotion); |
| 471 | break; |
| 472 | case ConfigureNotify: |
| 473 | if (win_configure(&win, &ev.xconfigure)) { |
| 474 | img.checkpan = 1; |
| 475 | timeout = 1; |
| 476 | } |
| 477 | break; |
| 478 | case ClientMessage: |
| 479 | if ((Atom) ev.xclient.data.l[0] == wm_delete_win) |
| 480 | return; |
| 481 | break; |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | } |