A Simple X Image Viewer
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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