A Simple X Image Viewer
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

main.c 11 KiB

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