A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

593 lines
12 KiB

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