A Simple X Image Viewer
 
 
 
 
 
 

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