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.
 
 
 
 
 
 

452 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. const char *base;
  98. fileflags_t flags;
  99. } fileinfo_t;
  100. /* timeouts in milliseconds: */
  101. enum {
  102. TO_REDRAW_RESIZE = 75,
  103. TO_REDRAW_THUMBS = 200,
  104. TO_CURSOR_HIDE = 1200,
  105. TO_DOUBLE_CLICK = 300
  106. };
  107. typedef void (*timeout_f)(void);
  108. typedef struct arl arl_t;
  109. typedef struct img img_t;
  110. typedef struct opt opt_t;
  111. typedef struct tns tns_t;
  112. typedef struct win win_t;
  113. /* autoreload.c */
  114. struct arl {
  115. int fd;
  116. int wd_dir;
  117. int wd_file;
  118. char *filename;
  119. };
  120. void arl_init(arl_t*);
  121. void arl_cleanup(arl_t*);
  122. void arl_setup(arl_t*, const char* /* result of realpath(3) */);
  123. bool arl_handle(arl_t*);
  124. /* commands.c */
  125. typedef int arg_t;
  126. typedef bool (*cmd_f)(arg_t);
  127. #define G_CMD(c) g_##c,
  128. #define I_CMD(c) i_##c,
  129. #define T_CMD(c) t_##c,
  130. typedef enum {
  131. #include "commands.lst"
  132. CMD_COUNT
  133. } cmd_id_t;
  134. typedef struct {
  135. int mode;
  136. cmd_f func;
  137. } cmd_t;
  138. typedef struct {
  139. unsigned int mask;
  140. KeySym ksym;
  141. cmd_id_t cmd;
  142. arg_t arg;
  143. } keymap_t;
  144. typedef struct {
  145. unsigned int mask;
  146. unsigned int button;
  147. cmd_id_t cmd;
  148. arg_t arg;
  149. } button_t;
  150. extern const cmd_t cmds[CMD_COUNT];
  151. /* image.c */
  152. typedef struct {
  153. Imlib_Image im;
  154. unsigned int delay;
  155. } img_frame_t;
  156. typedef struct {
  157. img_frame_t *frames;
  158. int cap;
  159. int cnt;
  160. int sel;
  161. bool animate;
  162. int framedelay;
  163. int length;
  164. } multi_img_t;
  165. struct img {
  166. Imlib_Image im;
  167. int w;
  168. int h;
  169. win_t *win;
  170. float x;
  171. float y;
  172. scalemode_t scalemode;
  173. float zoom;
  174. bool checkpan;
  175. bool dirty;
  176. bool aa;
  177. bool alpha;
  178. Imlib_Color_Modifier cmod;
  179. int gamma;
  180. struct {
  181. bool on;
  182. int delay;
  183. } ss;
  184. multi_img_t multi;
  185. };
  186. void img_init(img_t*, win_t*);
  187. bool img_load(img_t*, const fileinfo_t*);
  188. CLEANUP void img_close(img_t*, bool);
  189. void img_render(img_t*);
  190. bool img_fit_win(img_t*, scalemode_t);
  191. bool img_zoom(img_t*, float);
  192. bool img_zoom_in(img_t*);
  193. bool img_zoom_out(img_t*);
  194. bool img_pos(img_t*, float, float);
  195. bool img_move(img_t*, float, float);
  196. bool img_pan(img_t*, direction_t, int);
  197. bool img_pan_edge(img_t*, direction_t);
  198. void img_rotate(img_t*, degree_t);
  199. void img_flip(img_t*, flipdir_t);
  200. void img_toggle_antialias(img_t*);
  201. bool img_change_gamma(img_t*, int);
  202. bool img_frame_navigate(img_t*, int);
  203. bool img_frame_animate(img_t*);
  204. /* options.c */
  205. struct opt {
  206. /* file list: */
  207. char **filenames;
  208. bool from_stdin;
  209. bool to_stdout;
  210. bool recursive;
  211. int filecnt;
  212. int startnum;
  213. /* image: */
  214. scalemode_t scalemode;
  215. float zoom;
  216. bool animate;
  217. int gamma;
  218. int slideshow;
  219. int framerate;
  220. /* window: */
  221. bool fullscreen;
  222. bool hide_bar;
  223. long embed;
  224. char *geometry;
  225. char *res_name;
  226. /* misc flags: */
  227. bool quiet;
  228. bool thumb_mode;
  229. bool clean_cache;
  230. bool private_mode;
  231. };
  232. extern const opt_t *options;
  233. void print_usage(void);
  234. void print_version(void);
  235. void parse_options(int, char**);
  236. /* thumbs.c */
  237. typedef struct {
  238. Imlib_Image im;
  239. int w;
  240. int h;
  241. int x;
  242. int y;
  243. } thumb_t;
  244. struct tns {
  245. fileinfo_t *files;
  246. thumb_t *thumbs;
  247. const int *cnt;
  248. int *sel;
  249. int initnext;
  250. int loadnext;
  251. int first, end;
  252. int r_first, r_end;
  253. win_t *win;
  254. int x;
  255. int y;
  256. int cols;
  257. int rows;
  258. int zl;
  259. int bw;
  260. int dim;
  261. bool dirty;
  262. };
  263. void tns_clean_cache(tns_t*);
  264. void tns_init(tns_t*, fileinfo_t*, const int*, int*, win_t*);
  265. CLEANUP void tns_free(tns_t*);
  266. bool tns_load(tns_t*, int, bool, bool);
  267. void tns_unload(tns_t*, int);
  268. void tns_render(tns_t*);
  269. void tns_mark(tns_t*, int, bool);
  270. void tns_highlight(tns_t*, int, bool);
  271. bool tns_move_selection(tns_t*, direction_t, int);
  272. bool tns_scroll(tns_t*, direction_t, bool);
  273. bool tns_zoom(tns_t*, int);
  274. int tns_translate(tns_t*, int, int);
  275. /* util.c */
  276. #include <dirent.h>
  277. typedef struct {
  278. DIR *dir;
  279. char *name;
  280. int d;
  281. bool recursive;
  282. char **stack;
  283. int stcap;
  284. int stlen;
  285. } r_dir_t;
  286. extern const char *progname;
  287. void* emalloc(size_t);
  288. void* erealloc(void*, size_t);
  289. char* estrdup(const char*);
  290. void error(int, int, const char*, ...);
  291. void size_readable(float*, const char**);
  292. int r_opendir(r_dir_t*, const char*, bool);
  293. int r_closedir(r_dir_t*);
  294. char* r_readdir(r_dir_t*);
  295. int r_mkdir(char*);
  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. int win_textwidth(const win_env_t*, const char*, unsigned int, bool);
  362. void win_set_title(win_t*, const char*);
  363. void win_set_cursor(win_t*, cursor_t);
  364. void win_cursor_pos(win_t*, int*, int*);
  365. #endif /* SXIV_H */