Skip to content

Commit

Permalink
[awtk]: update
Browse files Browse the repository at this point in the history
  • Loading branch information
jiale-gdyd committed Feb 28, 2023
1 parent 0506bcd commit 5171a74
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 28 deletions.
17 changes: 17 additions & 0 deletions project/entry/gui_demo/awtk/res/assets/devices.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"/dev/fb0" : {
"type" : "fb"
},
"/dev/dri/card0" : {
"type" : "drm"
},
"/dev/input/event0" : {
"type" : "ts"
},
"/dev/input/event1" : {
"type" : "input"
},
"/dev/input/mouse0" : {
"type" : "mouse"
}
}
1 change: 1 addition & 0 deletions project/gui/awtk/driver/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
obj-y += lcd_linux/
obj-y += input_thread/

obj-y += devices.o
obj-y += main_loop_linux.o
103 changes: 103 additions & 0 deletions project/gui/awtk/driver/devices.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include "devices.h"
#include "tkc/mem.h"
#include "tkc/path.h"
#include "tkc/utils.h"
#include "conf_io/conf_json.h"

#define DEVICES_CONFIG_FILEPATH "assets/devices.json"

static bool_t s_ext_set = FALSE;
static uint32_t s_devices_nr = 0;
static device_info_t *s_devices = NULL;

ret_t devices_load(void)
{
uint32_t i = 0;
tk_object_t *conf = NULL;
char abs_path[MAX_PATH + 1] = {0}, abs_path_with_schema[MAX_PATH + 1] = {0};

return_value_if_fail(RET_OK == devices_unload(), RET_FAIL);

path_prepend_app_root(abs_path, DEVICES_CONFIG_FILEPATH);
tk_snprintf(abs_path_with_schema, MAX_PATH, "%s%s", STR_SCHEMA_FILE, abs_path);

log_debug("%s : path = %s\r\n", __FUNCTION__, abs_path_with_schema);

conf = conf_json_load(abs_path_with_schema, FALSE);
return_value_if_fail(conf != NULL, RET_OOM);

s_devices_nr = tk_object_get_prop_uint32(conf, "#size", 0);
goto_error_if_fail(s_devices_nr > 0);

s_devices = TKMEM_ZALLOCN(device_info_t, s_devices_nr);
goto_error_if_fail(s_devices != NULL);

for (i = 0; i < s_devices_nr; i++) {
char key[TK_NAME_LEN + 1] = {0};

tk_snprintf(key, sizeof(key), "[%d].#name", i);
tk_strncpy(s_devices[i].path, tk_object_get_prop_str(conf, key), sizeof(s_devices[i].path) - 1);

tk_snprintf(key, sizeof(key), "[%d].type", i);
tk_strncpy(s_devices[i].type, tk_object_get_prop_str(conf, key), sizeof(s_devices[i].type) - 1);

log_debug("devices[%d]: path = %s, type = %s\r\n", i, s_devices[i].path, s_devices[i].type);
}

TK_OBJECT_UNREF(conf);
return RET_OK;

error:
TK_OBJECT_UNREF(conf);
return RET_FAIL;
}

ret_t devices_unload(void)
{
if (s_devices != NULL) {
if (!s_ext_set) {
TKMEM_FREE(s_devices);
}

s_devices = NULL;
}

s_devices_nr = 0;
s_ext_set = FALSE;

return RET_OK;
}

ret_t devices_set(device_info_t *devices, uint32_t nr)
{
uint32_t i = 0;

return_value_if_fail(devices != NULL && nr > 0, RET_BAD_PARAMS);
return_value_if_fail(RET_OK == devices_unload(), RET_FAIL);

s_ext_set = TRUE;
s_devices_nr = nr;
s_devices = devices;

for (i = 0; i < s_devices_nr; i++) {
log_debug("devices[%d]: path = %s, type = %s\r\n", i, s_devices[i].path, s_devices[i].type);
}

return RET_OK;
}

ret_t devices_foreach(device_visit_t visit, void *ctx)
{
uint32_t i = 0;

return_value_if_fail(visit != NULL, RET_BAD_PARAMS);
return_value_if_fail(s_devices != NULL, RET_BAD_PARAMS);

for (i = 0; i < s_devices_nr; i++) {
if (RET_OK != visit(ctx, &s_devices[i])) {
break;
}
}

return RET_OK;
}
23 changes: 23 additions & 0 deletions project/gui/awtk/driver/devices.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef TK_DEVICES_H
#define TK_DEVICES_H

#include "tkc/types_def.h"

BEGIN_C_DECLS

typedef struct _device_info_t {
char type[TK_NAME_LEN + 1];
char path[MAX_PATH + 1];
} device_info_t;

typedef ret_t (*device_visit_t)(void *ctx, const device_info_t *info);

ret_t devices_load(void);
ret_t devices_unload(void);

ret_t devices_set(device_info_t *devices, uint32_t nr);
ret_t devices_foreach(device_visit_t visit, void *ctx);

END_C_DECLS

#endif
81 changes: 59 additions & 22 deletions project/gui/awtk/driver/main_loop_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "lcd_linux/lcd_linux_fb.h"
#include "lcd_linux/lcd_linux_drm.h"

#include "devices.h"
#include "main_loop_linux.h"

#ifndef FB_DEVICE_FILENAME
Expand All @@ -35,9 +36,14 @@
#define MICE_DEVICE_FILENAME "/dev/input/mouse0"
#endif

static tk_thread_t *s_kb_thread = NULL;
static tk_thread_t *s_ts_thread = NULL;
static tk_thread_t *s_mice_thread = NULL;
static slist_t s_device_threads_list;
static device_info_t s_devices_default[] = {
{"fb", FB_DEVICE_FILENAME},
{"drm", DRM_DEVICE_FILENAME},
{"ts", TS_DEVICE_FILENAME},
{"input", KB_DEVICE_FILENAME},
{"mouse", MICE_DEVICE_FILENAME}
};

static ret_t main_loop_linux_destroy(main_loop_t *l)
{
Expand Down Expand Up @@ -109,29 +115,65 @@ ret_t input_dispatch_to_main_loop(void *ctx, const event_queue_req_t *evt, const

static void on_app_exit(void)
{
if (s_kb_thread != NULL) {
tk_thread_destroy(s_kb_thread);
slist_deinit(&s_device_threads_list);
input_thread_global_deinit();
devices_unload();
}

static ret_t lcd_create_on_devices_visit(void *ctx, const device_info_t *info)
{
lcd_t **p_lcd = (lcd_t **)ctx;

#if defined(CONFIG_DRM_DISP_DRIVER)
if (tk_str_eq(info->type, "drm")) {
*p_lcd = lcd_linux_drm_create(info->path);
}
#else
if (tk_str_eq(info->type, "fb")) {
*p_lcd = lcd_linux_fb_create(info->path);
}
#endif

if (s_mice_thread != NULL) {
tk_thread_destroy(s_mice_thread);
if (*p_lcd != NULL) {
return RET_STOP;
}

if (s_ts_thread != NULL) {
tk_thread_destroy(s_ts_thread);
return RET_OK;
}

static ret_t device_thread_run_on_devices_visit(void *ctx, const device_info_t *info)
{
ret_t ret = RET_OK;
tk_thread_t *thread = NULL;
main_loop_simple_t *loop = (main_loop_simple_t *)ctx;

if (tk_str_eq(info->type, "input")) {
thread = input_thread_run(info->path, input_dispatch_to_main_loop, loop, loop->w, loop->h);
} else if (tk_str_eq(info->type, "mouse")) {
thread = mouse_thread_run(info->path, input_dispatch_to_main_loop, loop, loop->w, loop->h);
} else if (tk_str_eq(info->type, "ts")) {
#if defined(CONFIG_TSLIB)
thread = tslib_thread_run(info->path, input_dispatch_to_main_loop, loop, loop->w, loop->h);
#endif
}

input_thread_global_deinit();
if (thread != NULL) {
ret = slist_append(&s_device_threads_list, thread);
}

return ret;
}

main_loop_t *main_loop_init(int w, int h)
{
lcd_t *lcd = NULL;
main_loop_simple_t *loop = NULL;
#if defined(CONFIG_DRM_DISP_DRIVER)
lcd_t *lcd = lcd_linux_drm_create(DRM_DEVICE_FILENAME);
#else
lcd_t *lcd = lcd_linux_fb_create(FB_DEVICE_FILENAME);
#endif

if (RET_OK != devices_load()) {
log_warn("Devices load fail! Used default\n");
devices_set(s_devices_default, ARRAY_SIZE(s_devices_default));
}
devices_foreach(lcd_create_on_devices_visit, &lcd);

return_value_if_fail(lcd != NULL, NULL);

Expand All @@ -141,13 +183,8 @@ main_loop_t *main_loop_init(int w, int h)
loop->base.destroy = main_loop_linux_destroy;

input_thread_global_init();

#if defined(CONFIG_TSLIB)
s_ts_thread = tslib_thread_run(TS_DEVICE_FILENAME, input_dispatch_to_main_loop, loop, lcd->w, lcd->h);
#endif

s_kb_thread = input_thread_run(KB_DEVICE_FILENAME, input_dispatch_to_main_loop, loop, lcd->w, lcd->h);
s_mice_thread = mouse_thread_run(MICE_DEVICE_FILENAME, input_dispatch_to_main_loop, loop, lcd->w, lcd->h);
slist_init(&s_device_threads_list, (tk_destroy_t)tk_thread_destroy, NULL);
devices_foreach(device_thread_run_on_devices_visit, loop);

atexit(on_app_exit);

Expand Down
15 changes: 14 additions & 1 deletion project/gui/lvgl/src/libs/fsdrv/lv_fs_posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,20 @@ static lv_fs_res_t fs_write(lv_fs_drv_t * drv, void * file_p, const void * buf,
static lv_fs_res_t fs_seek(lv_fs_drv_t * drv, void * file_p, uint32_t pos, lv_fs_whence_t whence)
{
LV_UNUSED(drv);
off_t offset = lseek((lv_uintptr_t)file_p, pos, whence);
int w;
switch(whence) {
case LV_FS_SEEK_SET:
w = SEEK_SET;
break;
case LV_FS_SEEK_CUR:
w = SEEK_CUR;
break;
case LV_FS_SEEK_END:
w = SEEK_END;
break;
}

off_t offset = lseek((lv_uintptr_t)file_p, pos, w);
return offset < 0 ? LV_FS_RES_FS_ERR : LV_FS_RES_OK;
}

Expand Down
13 changes: 8 additions & 5 deletions project/gui/lvgl/src/misc/lv_fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,11 @@ static lv_fs_res_t lv_fs_read_cached(lv_fs_file_t * file_p, char * buf, uint32_t
/*First part of data is in cache buffer, but we need to read rest of data from FS*/
lv_memcpy(buf, buffer + buffer_offset, buffer_remaining_length);

file_p->drv->seek_cb(file_p->drv, file_p->file_d, file_p->cache->file_position + buffer_remaining_length,
LV_FS_SEEK_SET);

uint32_t bytes_read_to_buffer = 0;
if(btr > buffer_size) {
if(btr - buffer_remaining_length > buffer_size) {
/*If remaining data chuck is bigger than buffer size, then do not use cache, instead read it directly from FS*/
res = file_p->drv->read_cb(file_p->drv, file_p->file_d, (void *)(buf + buffer_remaining_length),
btr - buffer_remaining_length, &bytes_read_to_buffer);
Expand Down Expand Up @@ -259,7 +262,7 @@ lv_fs_res_t lv_fs_seek(lv_fs_file_t * file_p, uint32_t pos, lv_fs_whence_t whenc
file_p->cache->file_position = pos;

/*FS seek if new position is outside cache buffer*/
if(file_p->cache->file_position < file_p->cache->start || file_p->cache->file_position > file_p->cache->end) {
if(file_p->cache->file_position < file_p->cache->start || file_p->cache->file_position >= file_p->cache->end) {
res = file_p->drv->seek_cb(file_p->drv, file_p->file_d, file_p->cache->file_position, LV_FS_SEEK_SET);
}

Expand All @@ -269,14 +272,14 @@ lv_fs_res_t lv_fs_seek(lv_fs_file_t * file_p, uint32_t pos, lv_fs_whence_t whenc
file_p->cache->file_position += pos;

/*FS seek if new position is outside cache buffer*/
if(file_p->cache->file_position < file_p->cache->start || file_p->cache->file_position > file_p->cache->end) {
if(file_p->cache->file_position < file_p->cache->start || file_p->cache->file_position >= file_p->cache->end) {
res = file_p->drv->seek_cb(file_p->drv, file_p->file_d, file_p->cache->file_position, LV_FS_SEEK_SET);
}

break;
}
case LV_FS_SEEK_END: {
/*Because we don't know the file size, we do a little trick: do a FS seek, then get new file position from FS*/
/*Because we don't know the file size, we do a little trick: do a FS seek, then get the new file position from FS*/
res = file_p->drv->seek_cb(file_p->drv, file_p->file_d, pos, whence);
if(res == LV_FS_RES_OK) {
uint32_t tmp_position;
Expand Down Expand Up @@ -515,4 +518,4 @@ static const char * lv_fs_get_real_path(const char * path)
if(*path == ':') path++;

return path;
}
}

0 comments on commit 5171a74

Please sign in to comment.