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.5 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. DRAG_RELATIVE,
  82. DRAG_ABSOLUTE
  83. } dragmode_t;
  84. typedef enum {
  85. CURSOR_ARROW,
  86. CURSOR_DRAG,
  87. CURSOR_WATCH,
  88. CURSOR_LEFT,
  89. CURSOR_RIGHT,
  90. CURSOR_NONE,
  91. CURSOR_COUNT
  92. } cursor_t;
  93. typedef enum {
  94. FF_WARN = 1,
  95. FF_MARK = 2,
  96. FF_TN_INIT = 4
  97. } fileflags_t;
  98. typedef struct {
  99. const char *name; /* as given by user */
  100. const char *path; /* always absolute */
  101. fileflags_t flags;
  102. } fileinfo_t;
  103. /* timeouts in milliseconds: */
  104. enum {
  105. TO_REDRAW_RESIZE = 75,
  106. TO_REDRAW_THUMBS = 200,
  107. TO_CURSOR_HIDE = 1200,
  108. TO_DOUBLE_CLICK = 300
  109. };
  110. typedef void (*timeout_f)(void);
  111. typedef struct arl arl_t;
  112. typedef struct img img_t;
  113. typedef struct opt opt_t;
  114. typedef struct tns tns_t;
  115. typedef struct win win_t;
  116. /* autoreload.c */
  117. struct arl {
  118. int fd;
  119. int wd_dir;
  120. int wd_file;
  121. char *filename;
  122. };
  123. void arl_init(arl_t*);
  124. void arl_cleanup(arl_t*);
  125. void arl_setup(arl_t*, const char* /* result of realpath(3) */);
  126. bool arl_handle(arl_t*);
  127. /* commands.c */
  128. typedef int arg_t;
  129. typedef bool (*cmd_f)(arg_t);
  130. #define G_CMD(c) g_##c,
  131. #define I_CMD(c) i_##c,
  132. #define T_CMD(c) t_##c,
  133. typedef enum {
  134. #include "commands.lst"
  135. CMD_COUNT
  136. } cmd_id_t;
  137. typedef struct {
  138. int mode;
  139. cmd_f func;
  140. } cmd_t;
  141. typedef struct {
  142. unsigned int mask;
  143. KeySym ksym;
  144. cmd_id_t cmd;
  145. arg_t arg;
  146. } keymap_t;
  147. typedef struct {
  148. unsigned int mask;
  149. unsigned int button;
  150. cmd_id_t cmd;
  151. arg_t arg;
  152. } button_t;
  153. extern const cmd_t cmds[CMD_COUNT];
  154. /* image.c */
  155. typedef struct {
  156. Imlib_Image im;
  157. unsigned int delay;
  158. } img_frame_t;
  159. typedef struct {
  160. img_frame_t *frames;
  161. int cap;
  162. int cnt;
  163. int sel;
  164. bool animate;
  165. int framedelay;
  166. int length;
  167. } multi_img_t;
  168. struct img {
  169. Imlib_Image im;
  170. int w;
  171. int h;
  172. win_t *win;
  173. float x;
  174. float y;
  175. scalemode_t scalemode;
  176. float zoom;
  177. bool checkpan;
  178. bool dirty;
  179. bool aa;
  180. bool alpha;
  181. Imlib_Color_Modifier cmod;
  182. int gamma;
  183. struct {
  184. bool on;
  185. int delay;
  186. } ss;
  187. multi_img_t multi;
  188. };
  189. void img_init(img_t*, win_t*);
  190. bool img_load(img_t*, const fileinfo_t*);
  191. CLEANUP void img_close(img_t*, bool);
  192. void img_render(img_t*);
  193. bool img_fit_win(img_t*, scalemode_t);
  194. bool img_zoom(img_t*, float);
  195. bool img_zoom_in(img_t*);
  196. bool img_zoom_out(img_t*);
  197. bool img_pos(img_t*, float, float);
  198. bool img_move(img_t*, float, float);
  199. bool img_pan(img_t*, direction_t, int);
  200. bool img_pan_edge(img_t*, direction_t);
  201. void img_rotate(img_t*, degree_t);
  202. void img_flip(img_t*, flipdir_t);
  203. void img_toggle_antialias(img_t*);
  204. bool img_change_gamma(img_t*, int);
  205. bool img_frame_navigate(img_t*, int);
  206. bool img_frame_animate(img_t*);
  207. /* options.c */
  208. struct opt {
  209. /* file list: */
  210. char **filenames;
  211. bool from_stdin;
  212. bool to_stdout;
  213. bool recursive;
  214. int filecnt;
  215. int startnum;
  216. /* image: */
  217. scalemode_t scalemode;
  218. float zoom;
  219. bool animate;
  220. int gamma;
  221. int slideshow;
  222. int framerate;
  223. /* window: */
  224. bool fullscreen;
  225. bool hide_bar;
  226. long embed;
  227. char *geometry;
  228. char *res_name;
  229. /* misc flags: */
  230. bool quiet;
  231. bool thumb_mode;
  232. bool clean_cache;
  233. bool private_mode;
  234. };
  235. extern const opt_t *options;
  236. void print_usage(void);
  237. void print_version(void);
  238. void parse_options(int, char**);
  239. /* thumbs.c */
  240. typedef struct {
  241. Imlib_Image im;
  242. int w;
  243. int h;
  244. int x;
  245. int y;
  246. } thumb_t;
  247. struct tns {
  248. fileinfo_t *files;
  249. thumb_t *thumbs;
  250. const int *cnt;
  251. int *sel;
  252. int initnext;
  253. int loadnext;
  254. int first, end;
  255. int r_first, r_end;
  256. win_t *win;
  257. int x;
  258. int y;
  259. int cols;
  260. int rows;
  261. int zl;
  262. int bw;
  263. int dim;
  264. bool dirty;
  265. };
  266. void tns_clean_cache(tns_t*);
  267. void tns_init(tns_t*, fileinfo_t*, const int*, int*, win_t*);
  268. CLEANUP void tns_free(tns_t*);
  269. bool tns_load(tns_t*, int, bool, bool);
  270. void tns_unload(tns_t*, int);
  271. void tns_render(tns_t*);
  272. void tns_mark(tns_t*, int, bool);
  273. void tns_highlight(tns_t*, int, bool);
  274. bool tns_move_selection(tns_t*, direction_t, int);
  275. bool tns_scroll(tns_t*, direction_t, bool);
  276. bool tns_zoom(tns_t*, int);
  277. int tns_translate(tns_t*, int, int);
  278. /* util.c */
  279. #include <dirent.h>
  280. typedef struct {
  281. DIR *dir;
  282. char *name;
  283. int d;
  284. bool recursive;
  285. char **stack;
  286. int stcap;
  287. int stlen;
  288. } r_dir_t;
  289. extern const char *progname;
  290. void* emalloc(size_t);
  291. void* erealloc(void*, size_t);
  292. char* estrdup(const char*);
  293. void error(int, int, const char*, ...);
  294. void size_readable(float*, const char**);
  295. int r_opendir(r_dir_t*, const char*, bool);
  296. int r_closedir(r_dir_t*);
  297. char* r_readdir(r_dir_t*, bool);
  298. int r_mkdir(char*);
  299. /* window.c */
  300. #include <X11/Xutil.h>
  301. #include <X11/Xft/Xft.h>
  302. enum {
  303. BAR_L_LEN = 512,
  304. BAR_R_LEN = 64
  305. };
  306. enum {
  307. ATOM_WM_DELETE_WINDOW,
  308. ATOM__NET_WM_NAME,
  309. ATOM__NET_WM_ICON_NAME,
  310. ATOM__NET_WM_ICON,
  311. ATOM__NET_WM_STATE,
  312. ATOM__NET_WM_STATE_FULLSCREEN,
  313. ATOM_COUNT
  314. };
  315. typedef struct {
  316. Display *dpy;
  317. int scr;
  318. int scrw, scrh;
  319. Visual *vis;
  320. Colormap cmap;
  321. int depth;
  322. } win_env_t;
  323. typedef struct {
  324. size_t size;
  325. char *p;
  326. char *buf;
  327. } win_bar_t;
  328. struct win {
  329. Window xwin;
  330. win_env_t env;
  331. XftColor bg;
  332. XftColor fg;
  333. int x;
  334. int y;
  335. unsigned int w;
  336. unsigned int h; /* = win height - bar height */
  337. unsigned int bw;
  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 */