/**
 * @file lv_custom.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include <sys/time.h>
#include <stdlib.h>
#include "lv_custom.h"
#ifndef PC_SIMULATOR
#include "comm/video_buf.h"
#endif

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *      STRUCTURES
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *  STATIC VARIABLES
 **********************/
static lv_coord_t lv_hor_res_max = 0;
static lv_coord_t lv_ver_res_max = 0;
static lv_coord_t lv_hor_pos = 0;
static lv_coord_t lv_ver_pos = 0;
static bool lv_yuv_device = 0;
#ifndef PC_SIMULATOR
static VMF_VDISP_HANDLE_T *lv_disp_handle = NULL;
#endif

/**********************
 * GLOBAL VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/
void lv_scr_setup(void *handle, uint32_t w, uint32_t h, uint32_t x, uint32_t y, bool yuv)
{
#ifndef PC_SIMULATOR
	lv_disp_handle = handle;
	lv_hor_res_max = VMF_32_ALIGN(w);
	lv_ver_res_max = VMF_16_ALIGN(h);
#else //PC_SIMULATOR
	(void) handle;
	lv_hor_res_max = (w + 31) & ~31;
	lv_ver_res_max = (h + 15) & ~15;
#endif //PC_SIMULATOR
	lv_hor_pos = x;
	lv_ver_pos = y;
	lv_yuv_device = yuv;
}

lv_coord_t lv_scr_get_hor_res(void)
{
	return lv_hor_res_max;
}

lv_coord_t lv_scr_get_ver_res(void)
{
	return lv_ver_res_max;
}

lv_coord_t lv_scr_get_hor_pos(void)
{
	return lv_hor_pos;
}

lv_coord_t lv_scr_get_ver_pos(void)
{
	return lv_ver_pos;
}

bool lv_scr_is_yuv_device(void)
{
	return lv_yuv_device;
}

#ifndef PC_SIMULATOR
VMF_VDISP_HANDLE_T *lv_scr_get_disp_handle(void)
{
	return lv_disp_handle;
}
#endif

uint32_t custom_tick_get(void)
{
	static uint64_t start_ms = 0;
	if(start_ms == 0) {
		struct timeval tv_start;
		gettimeofday(&tv_start, NULL);
		start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
	}

	struct timeval tv_now;
	gettimeofday(&tv_now, NULL);
	uint64_t now_ms;
	now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

	uint32_t time_ms = now_ms - start_ms;
	return time_ms;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
