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.
 
 
 
 
 
 

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