A Simple X Image Viewer
 
 
 
 
 
 

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