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.
 
 
 
 
 
 

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