<commit_before>#include <fudge.h>
#include <abi.h>

struct position
{

    int x;
    int y;

};

struct size
{

    unsigned int w;
    unsigned int h;

};

struct image
{

    struct size size;
    void *data;

};

struct mouse
{

    struct position position;
    struct image image;
    unsigned int drag;
    unsigned int resize;

};

struct display
{

    struct position position;
    void *framebuffer;
    struct size size;
    unsigned int bpp;

};

struct configuration
{

    unsigned int padding;
    unsigned int lineheight;
    unsigned int steplength;

};

struct linesegment
{

    unsigned int relp0;
    int p0;
    unsigned int relp1;
    int p1;
    unsigned int color;

};

struct window
{

    char *title;
    unsigned int focus;
    struct position position;
    struct size size;

};

struct repaint
{

    unsigned int state;
    struct position position0;
    struct position position1;

};

static unsigned int optwidth = 1920;
static unsigned int optheight = 1080;
static unsigned int optbpp = 4;
/*
static unsigned int keymod = KEYMOD_NONE;
*/
static struct display display;
static struct mouse mouse;
static struct configuration configuration;
static struct repaint repaint;
static struct window window1;
static struct window window2;
static unsigned char fontdata[0x8000];
static unsigned char mousedata24[] = {
    0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
unsigned char mousedata16[] = {
    0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00,
    0x00, 0x02, 0x01, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x02, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
static unsigned int mousecmap[] = {
    0xFF000000,
    0xFFB05070,
    0xFFF898B8
};

#define RELX0 1
#define RELX1 2

#if 0
#define BORDERRECT_COLOR_NORMAL 0

static unsigned int borderrectcmap[] = {
    0xFFFF0000,
};
static struct linesegment borderrect0[1] = {
    {RELX0, 1, RELX1, -1, BORDERRECT_COLOR_NORMAL}
};

static struct linesegment borderrect1[2] = {
    {RELX0, 0, RELX0, 1, BORDERRECT_COLOR_NORMAL},
    {RELX1, -1, RELX1, 0, BORDERRECT_COLOR_NORMAL}
};
#endif

#define WINDOW_COLOR_SHADOW 0
#define WINDOW_COLOR_MAIN_LIGHT 1
#define WINDOW_COLOR_MAIN_NORMAL 2
#define WINDOW_COLOR_AREA_NORMAL 3

static unsigned int windowcmapnormal[] = {
    0xFF101010,
    0xFFA0A0A0,
    0xFF808080,
    0xFF242424
};
static unsigned int windowcmapfocus[] = {
    0xFF101010,
    0xFFA8C898,
    0xFF88A878,
    0xFF242424
};
static struct linesegment windowborder0[1] = {
    {RELX0, 1, RELX1, -1, WINDOW_COLOR_SHADOW}
};

static struct linesegment windowborder1[1] = {
    {RELX0, 0, RELX1, 0, WINDOW_COLOR_SHADOW}
};

static struct linesegment windowborder2[3] = {
    {RELX0, 0, RELX0, 3, WINDOW_COLOR_SHADOW},
    {RELX0, 3, RELX1, -3, WINDOW_COLOR_MAIN_LIGHT},
    {RELX1, -3, RELX1, 0, WINDOW_COLOR_SHADOW}
};

static struct linesegment windowborder3[5] = {
    {RELX0, 0, RELX0, 2, WINDOW_COLOR_SHADOW},
    {RELX0, 2, RELX0, 2, WINDOW_COLOR_MAIN_LIGHT},
    {RELX0, 4, RELX1, -4, WINDOW_COLOR_MAIN_NORMAL},
    {RELX1, -4, RELX1, -2, WINDOW_COLOR_MAIN_LIGHT},
    {RELX1, -2, RELX1, 0, WINDOW_COLOR_SHADOW}
};

static struct linesegment windowbordertitle[5] = {
    {RELX0, 0, RELX0, 2, WINDOW_COLOR_SHADOW},
    {RELX0, 2, RELX0, 3, WINDOW_COLOR_MAIN_LIGHT},
    {RELX0, 3, RELX1, -3, WINDOW_COLOR_MAIN_NORMAL},
    {RELX1, -3, RELX1, -2, WINDOW_COLOR_MAIN_LIGHT},
    {RELX1, -2, RELX1, 0, WINDOW_COLOR_SHADOW}
};

static struct linesegment windowborderspacing[7] = {
    {RELX0, 0, RELX0, 2, WINDOW_COLOR_SHADOW},
    {RELX0, 2, RELX0, 3, WINDOW_COLOR_MAIN_LIGHT},
    {RELX0, 3, RELX0, 4, WINDOW_COLOR_MAIN_NORMAL},
    {RELX0, 4, RELX1, -4, WINDOW_COLOR_SHADOW},
    {RELX1, -4, RELX1, -3, WINDOW_COLOR_MAIN_NORMAL},
    {RELX1, -3, RELX1, -2, WINDOW_COLOR_MAIN_LIGHT},
    {RELX1, -2, RELX1, 0, WINDOW_COLOR_SHADOW}
};

static struct linesegment windowborderarea[9] = {
    {RELX0, 0, RELX0, 2, WINDOW_COLOR_SHADOW},
    {RELX0, 2, RELX0, 3, WINDOW_COLOR_MAIN_LIGHT},
    {RELX0, 3, RELX0, 4, WINDOW_COLOR_MAIN_NORMAL},
    {RELX0, 4, RELX0, 5, WINDOW_COLOR_SHADOW},
    {RELX0, 5, RELX1, -5, WINDOW_COLOR_AREA_NORMAL},
    {RELX1, -5, RELX1, -4, WINDOW_COLOR_SHADOW},
    {RELX1, -4, RELX1, -3, WINDOW_COLOR_MAIN_NORMAL},
    {RELX1, -3, RELX1, -2, WINDOW_COLOR_MAIN_LIGHT},
    {RELX1, -2, RELX1, 0, WINDOW_COLOR_SHADOW}
};

static void setupvideo(void)
{

    struct ctrl_videosettings settings;
    unsigned char black[768];

    ctrl_setvideosettings(&settings, optwidth, optheight, optbpp);
    buffer_clear(black, 768);

    if (!file_walk(FILE_L0, FILE_G3, "ctrl"))
        return;

    if (!file_walk(FILE_L1, FILE_G3, "colormap"))
        return;

    file_seekwriteall(FILE_L1, black, 768, 0);
    file_seekwriteall(FILE_L0, &settings, sizeof (struct ctrl_videosettings), 0);

}

static void setmouse(unsigned int x, unsigned int y, unsigned int factor)
{

    mouse.position.x = x;
    mouse.position.y = y;

    switch (factor)
    {

    case 0:
    case 1:
        mouse.image.size.w = 12;
        mouse.image.size.h = 16;
        mouse.image.data = mousedata16;

        break;

    case 2:
    default:
        mouse.image.size.w = 18;
        mouse.image.size.h = 24;
        mouse.image.data = mousedata24;

        break;

    }

}

static void loadfont(unsigned int factor)
{

    switch (factor)
    {

    case 0:
        file_walk2(FILE_L0, "/data/ter-112n.pcf");

        break;

    case 1:
        file_walk2(FILE_L0, "/data/ter-114n.pcf");

        break;

    case 2:
        file_walk2(FILE_L0, "/data/ter-116n.pcf");

        break;

    default:
        file_walk2(FILE_L0, "/data/ter-118n.pcf");

        break;

    }

    file_seekread(FILE_L0, fontdata, 0x8000, 0);

}

static void posset(struct position *p0, struct position *p1, unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1)
{

    p0->x = x0;
    p0->y = y0;
    p1->x = x1;
    p1->y = y1;

}

static void posshrink(struct position *p0, struct position *p1, int x0, int y0, int x1, int y1)
{

    if (x0 < p0->x)
        p0->x = x0;

    if (y0 < p0->y)
        p0->y = y0;

    if (x1 > p1->x)
        p1->x = x1;

    if (y1 > p1->y)
        p1->y = y1;

}

static void markforpaint(int x0, int y0, int x1, int y1)
{

    if (repaint.state)
        posshrink(&repaint.position0, &repaint.position1, x0, y0, x1, y1);
    else
        posset(&repaint.position0, &repaint.position1, x0, y0, x1, y1);

    repaint.state = 1;

}

static void blit_line(unsigned int x0, unsigned int x1, unsigned int w, unsigned int color, unsigned int y)
{

    unsigned int *buffer = display.framebuffer;
    unsigned int x;

    for (x = x0; x < x1; x++)
        buffer[y * w + x] = color;

}

static void blit_cmap32line(struct position *p, struct image *image, unsigned int *cmap, unsigned int tw, unsigned int y)
{

    unsigned int *buffer = display.framebuffer;
    unsigned char *data = image->data;
    unsigned int x;

    for (x = 0; x < image->size.w; x++)
    {

        unsigned int soffset = (y * image->size.w + x);
        unsigned int toffset = (p->y * tw + p->x) + (y * tw + x);

        if (data[soffset] != 0xFF)
            buffer[toffset] = cmap[data[soffset]];

    }

}

static void paintlinesegment(int x0, int x1, unsigned int *cmap, struct linesegment *p, unsigned int y)
{

    int p0;
    int p1;

    switch (p->relp0)
    {

    case RELX0:
        p0 = x0 + p->p0;

        break;

    case RELX1:
        p0 = x1 + p->p0;

        break;

    default:
        p0 = x0;

        break;

    }

    switch (p->relp1)
    {

    case RELX0:
        p1 = x0 + p->p1;

        break;

    case RELX1:
        p1 = x1 + p->p1;

        break;

    default:
        p1 = x1;

        break;

    }

    blit_line(p0, p1, display.size.w, cmap[p->color], y);

}

static void paintlinesegments(int x0, int x1, unsigned int *cmap, struct linesegment *ls, unsigned int n, unsigned int y)
{

    unsigned int i;

    for (i = 0; i < n; i++)
        paintlinesegment(x0, x1, cmap, &ls[i], y);

}

static void paintmouse(struct mouse *m, unsigned int y)<commit_msg>more<commit_after>#include <fudge.h>
#include <abi.h>

struct position
{

    int x;
    int y;

};

struct size
{

    unsigned int w;
    unsigned int h;

};

struct image
{

    struct size size;
    void *data;

};

struct mouse
{

    struct position position;
    struct image image;
    unsigned int drag;
    unsigned int resize;

};

struct display
{

    struct position position;
    void *framebuffer;
    struct size size;
    unsigned int bpp;

};

struct configuration
{

    unsigned int padding;
    unsigned int lineheight;
    unsigned int steplength;

};

struct linesegment
{

    unsigned int type;
    int p0;
    int p1;
    unsigned int color;

};

struct window
{

    char *title;
    unsigned int focus;
    struct position position;
    struct size size;

};

struct repaint
{

    unsigned int state;
    struct position position0;
    struct position position1;

};

static unsigned int optwidth = 1920;
static unsigned int optheight = 1080;
static unsigned int optbpp = 4;
/*
static unsigned int keymod = KEYMOD_NONE;
*/
static struct display display;
static struct mouse mouse;
static struct configuration configuration;
static struct repaint repaint;
static struct window window1;
static struct window window2;
static unsigned char fontdata[0x8000];
static unsigned char mousedata24[] = {
    0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
unsigned char mousedata16[] = {
    0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00, 0xFF,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x00, 0x00,
    0x00, 0x02, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00,
    0x00, 0x02, 0x01, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x02, 0x01, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x02, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
static unsigned int mousecmap[] = {
    0xFF000000,
    0xFFB05070,
    0xFFF898B8
};

#define RELX0X0 1
#define RELX0X1 2
#define RELX1X1 3

#if 0
#define BORDERRECT_COLOR_NORMAL 0

static unsigned int borderrectcmap[] = {
    0xFFFF0000,
};
static struct linesegment borderrect0[1] = {
    {RELX0X1, 1, -1, BORDERRECT_COLOR_NORMAL}
};

static struct linesegment borderrect1[2] = {
    {RELX0X0, 0, 1, BORDERRECT_COLOR_NORMAL},
    {RELX1X1, -1, 0, BORDERRECT_COLOR_NORMAL}
};
#endif

#define WINDOW_COLOR_SHADOW 0
#define WINDOW_COLOR_MAIN_LIGHT 1
#define WINDOW_COLOR_MAIN_NORMAL 2
#define WINDOW_COLOR_AREA_NORMAL 3

static unsigned int windowcmapnormal[] = {
    0xFF101010,
    0xFFA0A0A0,
    0xFF808080,
    0xFF242424
};
static unsigned int windowcmapfocus[] = {
    0xFF101010,
    0xFFA8C898,
    0xFF88A878,
    0xFF242424
};
static struct linesegment windowborder0[1] = {
    {RELX0X1, 1, -1, WINDOW_COLOR_SHADOW}
};

static struct linesegment windowborder1[1] = {
    {RELX0X1, 0, 0, WINDOW_COLOR_SHADOW}
};

static struct linesegment windowborder2[3] = {
    {RELX0X0, 0, 3, WINDOW_COLOR_SHADOW},
    {RELX0X1, 3, -3, WINDOW_COLOR_MAIN_LIGHT},
    {RELX1X1, -3, 0, WINDOW_COLOR_SHADOW}
};

static struct linesegment windowborder3[5] = {
    {RELX0X0, 0, 2, WINDOW_COLOR_SHADOW},
    {RELX0X0, 2, 2, WINDOW_COLOR_MAIN_LIGHT},
    {RELX0X1, 4, -4, WINDOW_COLOR_MAIN_NORMAL},
    {RELX1X1, -4, -2, WINDOW_COLOR_MAIN_LIGHT},
    {RELX1X1, -2, 0, WINDOW_COLOR_SHADOW}
};

static struct linesegment windowbordertitle[5] = {
    {RELX0X0, 0, 2, WINDOW_COLOR_SHADOW},
    {RELX0X0, 2, 3, WINDOW_COLOR_MAIN_LIGHT},
    {RELX0X1, 3, -3, WINDOW_COLOR_MAIN_NORMAL},
    {RELX1X1, -3, -2, WINDOW_COLOR_MAIN_LIGHT},
    {RELX1X1, -2, 0, WINDOW_COLOR_SHADOW}
};

static struct linesegment windowborderspacing[7] = {
    {RELX0X0, 0, 2, WINDOW_COLOR_SHADOW},
    {RELX0X0, 2, 3, WINDOW_COLOR_MAIN_LIGHT},
    {RELX0X0, 3, 4, WINDOW_COLOR_MAIN_NORMAL},
    {RELX0X1, 4, -4, WINDOW_COLOR_SHADOW},
    {RELX1X1, -4, -3, WINDOW_COLOR_MAIN_NORMAL},
    {RELX1X1, -3, -2, WINDOW_COLOR_MAIN_LIGHT},
    {RELX1X1, -2, 0, WINDOW_COLOR_SHADOW}
};

static struct linesegment windowborderarea[9] = {
    {RELX0X0, 0, 2, WINDOW_COLOR_SHADOW},
    {RELX0X0, 2, 3, WINDOW_COLOR_MAIN_LIGHT},
    {RELX0X0, 3, 4, WINDOW_COLOR_MAIN_NORMAL},
    {RELX0X0, 4, 5, WINDOW_COLOR_SHADOW},
    {RELX0X1, 5, -5, WINDOW_COLOR_AREA_NORMAL},
    {RELX1X1, -5, -4, WINDOW_COLOR_SHADOW},
    {RELX1X1, -4, -3, WINDOW_COLOR_MAIN_NORMAL},
    {RELX1X1, -3, -2, WINDOW_COLOR_MAIN_LIGHT},
    {RELX1X1, -2, 0, WINDOW_COLOR_SHADOW}
};

static void setupvideo(void)
{

    struct ctrl_videosettings settings;
    unsigned char black[768];

    ctrl_setvideosettings(&settings, optwidth, optheight, optbpp);
    buffer_clear(black, 768);

    if (!file_walk(FILE_L0, FILE_G3, "ctrl"))
        return;

    if (!file_walk(FILE_L1, FILE_G3, "colormap"))
        return;

    file_seekwriteall(FILE_L1, black, 768, 0);
    file_seekwriteall(FILE_L0, &settings, sizeof (struct ctrl_videosettings), 0);

}

static void setmouse(unsigned int x, unsigned int y, unsigned int factor)
{

    mouse.position.x = x;
    mouse.position.y = y;

    switch (factor)
    {

    case 0:
    case 1:
        mouse.image.size.w = 12;
        mouse.image.size.h = 16;
        mouse.image.data = mousedata16;

        break;

    case 2:
    default:
        mouse.image.size.w = 18;
        mouse.image.size.h = 24;
        mouse.image.data = mousedata24;

        break;

    }

}

static void loadfont(unsigned int factor)
{

    switch (factor)
    {

    case 0:
        file_walk2(FILE_L0, "/data/ter-112n.pcf");

        break;

    case 1:
        file_walk2(FILE_L0, "/data/ter-114n.pcf");

        break;

    case 2:
        file_walk2(FILE_L0, "/data/ter-116n.pcf");

        break;

    default:
        file_walk2(FILE_L0, "/data/ter-118n.pcf");

        break;

    }

    file_seekread(FILE_L0, fontdata, 0x8000, 0);

}

static void posset(struct position *p0, struct position *p1, unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1)
{

    p0->x = x0;
    p0->y = y0;
    p1->x = x1;
    p1->y = y1;

}

static void posshrink(struct position *p0, struct position *p1, int x0, int y0, int x1, int y1)
{

    if (x0 < p0->x)
        p0->x = x0;

    if (y0 < p0->y)
        p0->y = y0;

    if (x1 > p1->x)
        p1->x = x1;

    if (y1 > p1->y)
        p1->y = y1;

}

static void markforpaint(int x0, int y0, int x1, int y1)
{

    if (repaint.state)
        posshrink(&repaint.position0, &repaint.position1, x0, y0, x1, y1);
    else
        posset(&repaint.position0, &repaint.position1, x0, y0, x1, y1);

    repaint.state = 1;

}

static void blit_line(unsigned int x0, unsigned int x1, unsigned int w, unsigned int color, unsigned int y)
{

    unsigned int *buffer = display.framebuffer;
    unsigned int x;

    for (x = x0; x < x1; x++)
        buffer[y * w + x] = color;

}

static void blit_cmap32line(struct position *p, struct image *image, unsigned int *cmap, unsigned int tw, unsigned int y)
{

    unsigned int *buffer = display.framebuffer;
    unsigned char *data = image->data;
    unsigned int x;

    for (x = 0; x < image->size.w; x++)
    {

        unsigned int soffset = (y * image->size.w + x);
        unsigned int toffset = (p->y * tw + p->x) + (y * tw + x);

        if (data[soffset] != 0xFF)
            buffer[toffset] = cmap[data[soffset]];

    }

}

static void paintlinesegment(int x0, int x1, unsigned int *cmap, struct linesegment *p, unsigned int y)
{

    int p0;
    int p1;

    switch (p->type)
    {

    case RELX0X0:
        p0 = x0 + p->p0;
        p1 = x0 + p->p1;

        break;

    case RELX0X1:
        p0 = x0 + p->p0;
        p1 = x1 + p->p1;

        break;

    case RELX1X1:
        p0 = x1 + p->p0;
        p1 = x1 + p->p1;

        break;

    default:
        p0 = x0;
        p1 = x1;

        break;

    }

    blit_line(p0, p1, display.size.w, cmap[p->color], y);

}

static void paintlinesegments(int x0, int x1, unsigned int *cmap, struct linesegment *ls, unsigned int n, unsigned int y)
{

    unsigned int i;

    for (i = 0; i < n; i++)
        paintlinesegment(x0, x1, cmap, &ls[i], y);

}

static void paintmouse(struct mouse *m, unsigned int y)
{'fraction_non_alphanumeric': 0.13241343846417553, 'fraction_numerical': 0.15019712032910523, 'mean_word_length': 3.1502756535657124, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2072710', 'n_tokens_mistral': 13130, 'n_tokens_neox': 10757, 'n_words': 2806}