#ifndef _H_X11
#define _H_X11

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/sync.h>
#define XK_MISCELLANY
#include <X11/keysymdef.h>
#include <X11/Xft/Xft.h>
#include <fontconfig/fontconfig.h>

#include <popt.h>

#include "proto.h"

#define FS_NORMAL 0
#define FS_BOLD   1
#define FS_ITALIC 2
#define FS_MAX    3

struct font {
        int number;
        XftFont **fonts;
};

struct fontset {
        struct font fonts[FS_MAX];
};

extern const struct poptOption x11_options[];


struct conn {
        Display * dpy;
        struct fontset *fs;  // per-screen fonts
        FcPattern *pattern;
};

struct win {
        Window w;
        GC gc;
        int width, height;
        int current_screen;
        XftDraw *xftdraw;
        XftFont *fn;
        XSetWindowAttributes att;
        XSyncCounter syncCounter;
        XSyncValue syncValue;
};


extern struct win win;
extern struct conn *conn;

void x11_init(int argc, char *argv[]);
void x11_close(void);
void get_charcell_size(int *width, int *height);
void x11_switchscreen(int nscreen);
void x11_detach(void);
void x11_get_info(struct x11_info *x);
void x11_attach(struct x11_info *x);
struct font* get_font(struct conn *conn, int screen, int type);
void font_lookup_char(struct conn *conn, wchar_t ch, int screen, int type, XftFont **font);

// #define SCREEN (DefaultScreen(conn->dpy))
// #define CMAP (DefaultColormap(conn->dpy,SCREEN))
// #define BLACK (BlackPixel(conn->dpy, SCREEN))
// #define WHITE (WhitePixel(conn->dpy, SCREEN))

#define IS_ATTACHED()  (conn != NULL)

#endif

