A Simple X Image Viewer
 
 
 
 
 
 

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