A Simple X Image Viewer
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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