/*
 * Copyright (c) 2019 Clementine Computing LLC.
 *
 * This file is part of PopuFare.
 * 
 * PopuFare is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * PopuFare is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with PopuFare.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

#define XRES	(640)
#define YRES	(480)

#define PIXELS	(XRES * YRES)

int open_framebuffer_nondestructive();
int open_framebuffer();
void close_framebuffer();

void set_color(int r, int g, int b);
void set_bgcolor(int r, int g, int b);

void set_color_t(int r, int g, int b, int a);
void set_bgcolor_t(int r, int g, int b, int a);

void cls();

void set_pos(int x, int y);
void line_to(int xdest, int ydest);

void box(int x, int y);
void box_fill(int x, int y);

void set_font(unsigned char font);
void print_string(char *str, int bkgr);
void font_cell_size(int *w, int *h);

void set_backlight(int fd, int on);

void beep(int fd, int hz, int milis);

int translate_ts_x(unsigned rawx);
int translate_ts_y(unsigned rawy);

int present_framebuffer();

void reset_clip_rect();
void set_clip_rect(unsigned x, unsigned y, unsigned xx, unsigned yy);
void get_clip_rect(unsigned *x, unsigned *y, unsigned *xx, unsigned *yy);

#define BLENDMODE_DARK	(0)
#define BLENDMODE_LIGHT	(1)

//This function selects which of the two blend modes will be used for subsequent blend operations
//when a pixel is plotted that has its blend flag set.  This is quick because it just selects a differnt
//pre-computed-at-compile-time table.
//
void select_blend_mode(int mode);

//This function is VERY slow as it precomputes the results of every
//possible alpha blending operation and stores it in a table that is
//normally generated at compile-time with the two tables being filled
//with reasonable defaults:
//
//	BLENDMODE_DARK	is the dark table and uses an alpha of ALPHA_DRK (usually 75% opaque)
//	BLENDMODE_LIGHT is the light table and uses an alpha of ALPHA_LGT (usually 50% opaque)
//
//	(so yeah, don't call this function you bastard!)
//
void set_alpha_level(unsigned int alpha, unsigned int mode);

typedef unsigned char pixel;

#define		FBCOLOR_RED		((pixel)0x60)
#define		FBCOLOR_DKRED		((pixel)0x20)
#define		FBCOLOR_GREEN		((pixel)0x1C)
#define		FBCOLOR_DKGREEN		((pixel)0x10)
#define		FBCOLOR_BLUE		((pixel)0x03)
#define		FBCOLOR_DKBLUE		((pixel)0x02)

#define		FBCOLOR_YELLOW		(FBCOLOR_RED | FBCOLOR_GREEN)
#define		FBCOLOR_CYAN		(FBCOLOR_GREEN | FBCOLOR_BLUE)
#define		FBCOLOR_MAGENTA		(FBCOLOR_BLUE | FBCOLOR_RED)

#define		FBCOLOR_DKYELLOW	(FBCOLOR_DKRED | FBCOLOR_DKGREEN)
#define		FBCOLOR_DKCYAN		(FBCOLOR_DKGREEN | FBCOLOR_DKBLUE)
#define		FBCOLOR_DKMAGENTA	(FBCOLOR_DKBLUE | FBCOLOR_DKRED)

#define		FBCOLOR_WHITE		((pixel)0x7F)
#define		FBCOLOR_GRAY		((pixel)0x52)
#define		FBCOLOR_DKGRAY		((pixel)0x29)
#define		FBCOLOR_BLACK		((pixel)0x00)

#define		FBCOLOR_LTRED		(FBCOLOR_RED | FBCOLOR_GRAY)
#define		FBCOLOR_LTGREEN		(FBCOLOR_GREEN | FBCOLOR_GRAY)
#define		FBCOLOR_LTBLUE		(FBCOLOR_BLUE | FBCOLOR_GRAY)
#define		FBCOLOR_LTYELLOW	(FBCOLOR_YELLOW | FBCOLOR_GRAY)
#define		FBCOLOR_LTMAGENTA	(FBCOLOR_MAGENTA | FBCOLOR_GRAY)
#define		FBCOLOR_LTCYAN		(FBCOLOR_CYAN | FBCOLOR_GRAY)

//This value can be OR'd with any of the above colors as a modifier
#define		FBCOLOR_TRANSLUCENT	((pixel)0x80)

typedef struct named_color_struct
{
    char *name;		//this is the name of the color (for the purposes of config files)
                        //a NULL value is an end-of-array sentinel.
    
    pixel color;	//this is the raw color associated with that name

} named_color;


//This function looks up any of our named colors, or converts hex colors in the following formats:
//    #XX  		8 bit raw color in hex (bits: TRRGGGBB)
//    #RRGGBB		24 bit true color in hex
//    #RRGGBBAA		32 bit true color w/ alpha in hex
pixel string_to_color(char *str);

void set_rawcolor(pixel color);
void set_bgrawcolor(pixel color);

typedef struct sprite_struct
{
    unsigned width;
    unsigned height;
    
    pixel *data;

} sprite;

void draw_sprite(sprite *s);

int begin_touchscreen_calibration();

int draw_touchscreen_calibration();

int advance_touchscreen_calibration(int rawx, int rawy, int down);

