#include <signal.h>
#include "SDL.h"
#include "SDL_syswm.h"
#include <sys/select.h>
#include "draw.h"
#include "chars.h"

#include "rote/rote.h"
#include "/usr/src/rote-0.2.6/roteprivate.h"

extern int xy_do_events;
FILE *err;

static unsigned char getout = 0;
void sigchld(int signo) { getout = 1; fprintf(err, "sigchld\n"); }

#define YLOSS 8
#define CHEIGHT (xy_char_maxy+1) // 42
#define CWIDTH  (xy_char_maxx+1) // 24
#define COLS (xy_size/CWIDTH)
#define ROWS ((xy_size-YLOSS)/CHEIGHT)

int main() {
    RoteTerm *rt;
    int rotefd;
    int xfd;

    err = fopen("term.err", "w");
    rt = rote_vt_create(ROWS, COLS);

    // rote_vt_forkpty(rt, "less term.c");
    rote_vt_forkpty(rt, "bash --login");

    rotefd = rt->pd->pty;    
    printf("rote_fd = %d\n", rotefd);

    signal(SIGCHLD, sigchld);

    xy_init(); xy_factor = 8; xy_shift=8;
    SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

    SDL_EnableUNICODE(1);
    {
        SDL_SysWMinfo i;
        SDL_VERSION(&i.version);
        int result = SDL_GetWMInfo(&i);
        printf("SDL_GetWMInfo() -> %d\n", result);
        xfd = ConnectionNumber(i.info.x11.display);
    }

    xy_do_events = 0;

    fprintf(err, "getout=%d\n", getout);
    int cursor = 0;
    while(!getout) {
        int r, c, ch;
        cursor = !cursor;
        rote_vt_update(rt);
        for(r=0; r<ROWS; r++) {
            for(c=0; c<COLS; c++) {
                int x = c * CWIDTH;
                int y = xy_size - 1 - CHEIGHT * r - xy_char_maxy;
                int ch = rt->cells[r][c].ch;
                int i = xy_char_end[ch-1];
                int end = xy_char_end[ch];
                if(i != end) {
                    if(!xy_hasroom(end-i+xy_shift/xy_factor)) xy_nextline();
                    xy_moveto(x + xy_char[i].x, y + xy_char[i].y);
                    xy_moveto(x + xy_char[i].x, y + xy_char[i].y);
                    xy_moveto(x + xy_char[i].x, y + xy_char[i].y);
                    xy_moveto(x + xy_char[i].x, y + xy_char[i].y);
                    xy_draw_pixel(x + xy_char[i].x, y + xy_char[i].y);
                    xy_draw_pixel(x + xy_char[i].x, y + xy_char[i].y);
                    for(;i<end; i++) {
                        xy_draw_pixel(x + xy_char[i].x, y + xy_char[i].y);
                        xy_draw_pixel(x + xy_char[i].x, y + xy_char[i].y);
                    }
                        xy_draw_pixel(x + xy_char[i-1].x, y + xy_char[i-1].y);
                        xy_draw_pixel(x + xy_char[i-1].x, y + xy_char[i-1].y);
                }
            }
        }
        xy_nextline();
        int x0 = rt->ccol * CWIDTH;
        int y0 = xy_size - 1 - CHEIGHT * (rt->crow  + 1);
        xy_moveto(x0,y0);
        for(int x=x0; x<x0+CWIDTH; x+=2) {
            xy_draw_line(x, y0, x, y0+CHEIGHT-1);
            xy_draw_line(x+1, y0+CHEIGHT, x+1, y0+1);
        }
        xy_nextline();
        xy_flipscreen();

        struct timeval timeout = {10, 0};
        fd_set rfd;
        int topfd = rotefd > xfd ? rotefd : xfd;
        printf("topfd=%d rotefd=%d xfd=%d\n", topfd, rotefd, xfd);
        printf("fds = %08x\n", *(int*)&rfd);
        FD_ZERO(&rfd); FD_SET(xfd, &rfd); FD_SET(rotefd, &rfd);
        select(topfd+1, &rfd, NULL, NULL, &timeout);
        
        SDL_Event event;
        while(SDL_PollEvent(&event)) {
            switch(event.type) {
                case SDL_KEYDOWN: {
                    int code = event.key.keysym.unicode;
                    int mod = event.key.keysym.mod;
                    printf("code=%04x mod=%02x\n", code, mod);
                    if(code == 0) continue;
                    if(mod & (KMOD_ALT | KMOD_META)) 
                        rote_vt_keypress(rt, 033);
                    rote_vt_keypress(rt, code);
                }
            }
        }
    }
    fprintf(err, "getout=%d\n", getout);
    return 0;
}
