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.
 
 
 
 
 
 

451 lines
7.6 KiB

  1. /* Copyright 2011 Bert Muennich
  2. *
  3. * This file is part of sxiv.
  4. *
  5. * sxiv is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published
  7. * by the Free Software Foundation; either version 2 of the License,
  8. * or (at your option) any later version.
  9. *
  10. * sxiv is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef SXIV_H
  19. #define SXIV_H
  20. #include <stdarg.h>
  21. #include <stdbool.h>
  22. #include <stdio.h>
  23. #include <sys/time.h>
  24. #include <sys/types.h>
  25. #include <Imlib2.h>
  26. #include <X11/Xlib.h>
  27. /*
  28. * Annotation for functions called in cleanup().
  29. * These functions are not allowed to call error(!0, ...) or exit().
  30. */
  31. #define CLEANUP
  32. #ifndef MIN
  33. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  34. #endif
  35. #ifndef MAX
  36. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  37. #endif
  38. #define ARRLEN(a) (sizeof(a) / sizeof((a)[0]))
  39. #define STREQ(s1,s2) (strcmp((s1), (s2)) == 0)
  40. #define TV_DIFF(t1,t2) (((t1)->tv_sec - (t2)->tv_sec ) * 1000 + \
  41. ((t1)->tv_usec - (t2)->tv_usec) / 1000)
  42. #define TV_SET_MSEC(tv,t) { \
  43. (tv)->tv_sec = (t) / 1000; \
  44. (tv)->tv_usec = (t) % 1000 * 1000; \
  45. }
  46. #define TV_ADD_MSEC(tv,t) { \
  47. (tv)->tv_sec += (t) / 1000; \
  48. (tv)->tv_usec += (t) % 1000 * 1000; \
  49. }
  50. typedef enum {
  51. BO_BIG_ENDIAN,
  52. BO_LITTLE_ENDIAN
  53. } byteorder_t;
  54. typedef enum {
  55. MODE_IMAGE,
  56. MODE_THUMB
  57. } appmode_t;
  58. typedef enum {
  59. DIR_LEFT = 1,
  60. DIR_RIGHT = 2,
  61. DIR_UP = 4,
  62. DIR_DOWN = 8
  63. } direction_t;
  64. typedef enum {
  65. DEGREE_90 = 1,
  66. DEGREE_180 = 2,
  67. DEGREE_270 = 3
  68. } degree_t;
  69. typedef enum {
  70. FLIP_HORIZONTAL = 1,
  71. FLIP_VERTICAL = 2
  72. } flipdir_t;
  73. typedef enum {
  74. SCALE_DOWN,
  75. SCALE_FIT,
  76. SCALE_WIDTH,
  77. SCALE_HEIGHT,
  78. SCALE_ZOOM
  79. } scalemode_t;
  80. typedef enum {
  81. CURSOR_ARROW,
  82. CURSOR_DRAG,
  83. CURSOR_WATCH,
  84. CURSOR_LEFT,
  85. CURSOR_RIGHT,
  86. CURSOR_NONE,
  87. CURSOR_COUNT
  88. } cursor_t;
  89. typedef enum {
  90. FF_WARN = 1,
  91. FF_MARK = 2,
  92. FF_TN_INIT = 4
  93. } fileflags_t;
  94. typedef struct {
  95. const char *name; /* as given by user */
  96. const char *path; /* always absolute */
  97. fileflags_t flags;
  98. } fileinfo_t;
  99. /* timeouts in milliseconds: */
  100. enum {
  101. TO_REDRAW_RESIZE = 75,
  102. TO_REDRAW_THUMBS = 200,
  103. TO_CURSOR_HIDE = 1200,
  104. TO_DOUBLE_CLICK = 300
  105. };
  106. typedef void (*timeout_f)(void);
  107. typedef struct arl arl_t;
  108. typedef struct img img_t;
  109. typedef struct opt opt_t;
  110. typedef struct tns tns_t;
  111. typedef struct win win_t;
  112. /* autoreload.c */
  113. struct arl {
  114. int fd;
  115. int wd_dir;
  116. int wd_file;
  117. char *filename;
  118. };
  119. void arl_init(arl_t*);
  120. void arl_cleanup(arl_t*);
  121. void arl_setup(arl_t*, const char* /* result of realpath(3) */);
  122. bool arl_handle(arl_t*);
  123. /* commands.c */
  124. typedef int arg_t;
  125. typedef bool (*cmd_f)(arg_t);
  126. #define G_CMD(c) g_##c,
  127. #define I_CMD(c) i_##c,
  128. #define T_CMD(c) t_##c,
  129. typedef enum {
  130. #include "commands.lst"
  131. CMD_COUNT
  132. } cmd_id_t;
  133. typedef struct {
  134. int mode;
  135. cmd_f func;
  136. } cmd_t;
  137. typedef struct {
  138. unsigned int mask;
  139. KeySym ksym;
  140. cmd_id_t cmd;
  141. arg_t arg;
  142. } keymap_t;
  143. typedef struct {
  144. unsigned int mask;
  145. unsigned int button;
  146. cmd_id_t cmd;
  147. arg_t arg;
  148. } button_t;
  149. extern const cmd_t cmds[CMD_COUNT];
  150. /* image.c */
  151. typedef struct {
  152. Imlib_Image im;
  153. unsigned int delay;
  154. } img_frame_t;
  155. typedef struct {
  156. img_frame_t *frames;
  157. int cap;
  158. int cnt;
  159. int sel;
  160. bool animate;
  161. int framedelay;
  162. int length;
  163. } multi_img_t;
  164. struct img {
  165. Imlib_Image im;
  166. int w;
  167. int h;
  168. win_t *win;
  169. float x;
  170. float y;
  171. scalemode_t scalemode;
  172. float zoom;
  173. bool checkpan;
  174. bool dirty;
  175. bool aa;
  176. bool alpha;
  177. Imlib_Color_Modifier cmod;
  178. int gamma;
  179. struct {
  180. bool on;
  181. int delay;
  182. } ss;
  183. multi_img_t multi;
  184. };
  185. void img_init(img_t*, win_t*);
  186. bool img_load(img_t*, const fileinfo_t*);
  187. CLEANUP void img_close(img_t*, bool);
  188. void img_render(img_t*);
  189. bool img_fit_win(img_t*, scalemode_t);
  190. bool img_zoom(img_t*, float);
  191. bool img_zoom_in(img_t*);
  192. bool img_zoom_out(img_t*);
  193. bool img_pos(img_t*, float, float);
  194. bool img_move(img_t*, float, float);
  195. bool img_pan(img_t*, direction_t, int);
  196. bool img_pan_edge(img_t*, direction_t);
  197. void img_rotate(img_t*, degree_t);
  198. void img_flip(img_t*, flipdir_t);
  199. void img_toggle_antialias(img_t*);
  200. bool img_change_gamma(img_t*, int);
  201. bool img_frame_navigate(img_t*, int);
  202. bool img_frame_animate(img_t*);
  203. /* options.c */
  204. struct opt {
  205. /* file list: */
  206. char **filenames;
  207. bool from_stdin;
  208. bool to_stdout;
  209. bool recursive;
  210. int filecnt;
  211. int startnum;
  212. /* image: */
  213. scalemode_t scalemode;
  214. float zoom;
  215. bool animate;
  216. int gamma;
  217. int slideshow;
  218. int framerate;
  219. /* window: */
  220. bool fullscreen;
  221. bool hide_bar;
  222. long embed;
  223. char *geometry;
  224. char *res_name;
  225. /* misc flags: */
  226. bool quiet;
  227. bool thumb_mode;
  228. bool clean_cache;
  229. bool private_mode;
  230. };
  231. extern const opt_t *options;
  232. void print_usage(void);
  233. void print_version(void);
  234. void parse_options(int, char**);
  235. /* thumbs.c */
  236. typedef struct {
  237. Imlib_Image im;
  238. int w;
  239. int h;
  240. int x;
  241. int y;
  242. } thumb_t;
  243. struct tns {
  244. fileinfo_t *files;
  245. thumb_t *thumbs;
  246. const int *cnt;
  247. int *sel;
  248. int initnext;
  249. int loadnext;
  250. int first, end;
  251. int r_first, r_end;
  252. win_t *win;
  253. int x;
  254. int y;
  255. int cols;
  256. int rows;
  257. int zl;
  258. int bw;
  259. int dim;
  260. bool dirty;
  261. };
  262. void tns_clean_cache(tns_t*);
  263. void tns_init(tns_t*, fileinfo_t*, const int*, int*, win_t*);
  264. CLEANUP void tns_free(tns_t*);
  265. bool tns_load(tns_t*, int, bool, bool);
  266. void tns_unload(tns_t*, int);
  267. void tns_render(tns_t*);
  268. void tns_mark(tns_t*, int, bool);
  269. void tns_highlight(tns_t*, int, bool);
  270. bool tns_move_selection(tns_t*, direction_t, int);
  271. bool tns_scroll(tns_t*, direction_t, bool);
  272. bool tns_zoom(tns_t*, int);
  273. int tns_translate(tns_t*, int, int);
  274. /* util.c */
  275. #include <dirent.h>
  276. typedef struct {
  277. DIR *dir;
  278. char *name;
  279. int d;
  280. bool recursive;
  281. char **stack;
  282. int stcap;
  283. int stlen;
  284. } r_dir_t;
  285. extern const char *progname;
  286. void* emalloc(size_t);
  287. void* erealloc(void*, size_t);
  288. char* estrdup(const char*);
  289. void error(int, int, const char*, ...);
  290. void size_readable(float*, const char**);
  291. int r_opendir(r_dir_t*, const char*, bool);
  292. int r_closedir(r_dir_t*);
  293. char* r_readdir(r_dir_t*);
  294. int r_mkdir(char*);
  295. void* utf8codepoint(const void * __restrict__ str, long * __restrict__ out_codepoint);
  296. /* window.c */
  297. #include <X11/Xutil.h>
  298. #include <X11/Xft/Xft.h>
  299. enum {
  300. BAR_L_LEN = 512,
  301. BAR_R_LEN = 64
  302. };
  303. enum {
  304. ATOM_WM_DELETE_WINDOW,
  305. ATOM__NET_WM_NAME,
  306. ATOM__NET_WM_ICON_NAME,
  307. ATOM__NET_WM_ICON,
  308. ATOM__NET_WM_STATE,
  309. ATOM__NET_WM_STATE_FULLSCREEN,
  310. ATOM__NET_SUPPORTED,
  311. ATOM_COUNT
  312. };
  313. typedef struct {
  314. Display *dpy;
  315. int scr;
  316. int scrw, scrh;
  317. Visual *vis;
  318. Colormap cmap;
  319. int depth;
  320. } win_env_t;
  321. typedef struct {
  322. size_t size;
  323. char *p;
  324. char *buf;
  325. } win_bar_t;
  326. struct win {
  327. Window xwin;
  328. win_env_t env;
  329. XftColor bgcol;
  330. XftColor fscol;
  331. XftColor selcol;
  332. int x;
  333. int y;
  334. unsigned int w;
  335. unsigned int h; /* = win height - bar height */
  336. unsigned int bw;
  337. bool fullscreen;
  338. struct {
  339. int w;
  340. int h;
  341. Pixmap pm;
  342. } buf;
  343. struct {
  344. unsigned int h;
  345. win_bar_t l;
  346. win_bar_t r;
  347. XftColor bgcol;
  348. XftColor fgcol;
  349. } bar;
  350. };
  351. extern Atom atoms[ATOM_COUNT];
  352. void win_init(win_t*);
  353. void win_open(win_t*);
  354. CLEANUP void win_close(win_t*);
  355. bool win_configure(win_t*, XConfigureEvent*);
  356. void win_toggle_fullscreen(win_t*);
  357. void win_toggle_bar(win_t*);
  358. void win_clear(win_t*);
  359. void win_draw(win_t*);
  360. void win_draw_rect(win_t*, int, int, int, int, bool, int, unsigned long);
  361. void win_set_title(win_t*, const char*);
  362. void win_set_cursor(win_t*, cursor_t);
  363. void win_cursor_pos(win_t*, int*, int*);
  364. #endif /* SXIV_H */