diff --git a/filesystem/dfs/src/dfs_posix.c b/filesystem/dfs/src/dfs_posix.c index 9a903cf790..96ae60e7fb 100644 --- a/filesystem/dfs/src/dfs_posix.c +++ b/filesystem/dfs/src/dfs_posix.c @@ -63,16 +63,21 @@ int open(const char *file, int flags, int mode) | +------------------------------------------------------------------------------ */ -int close(int d) +int close(int fd) { int result; - struct dfs_fd* fd; + struct dfs_fd* d; - fd = fd_get(d); + d = fd_get(fd); + if (d == RT_NULL) + { + rt_set_errno(-RT_ERROR); + return -1; + } - result = dfile_raw_close(fd); - fd_put(fd); - fd_put(fd); + result = dfile_raw_close(d); + fd_put(d); + fd_put(d); if (result < 0) { @@ -100,6 +105,11 @@ int read(int fd, char *buf, int len) /* get the fd */ d = fd_get(fd); + if (d == RT_NULL) + { + rt_set_errno(-RT_ERROR); + return -1; + } result = dfile_raw_read(d, buf, len); if (result < 0) @@ -133,6 +143,11 @@ int write(int fd, char *buf, int len) /* get the fd */ d = fd_get(fd); + if (d == RT_NULL) + { + rt_set_errno(-RT_ERROR); + return -1; + } result = dfile_raw_write(d, buf, len); if (result < 0) @@ -165,6 +180,11 @@ int lseek(int fd, int offset, int dir) struct dfs_fd* d; d = fd_get(fd); + if (d == RT_NULL) + { + rt_set_errno(-RT_ERROR); + return -1; + } switch (dir) { @@ -391,6 +411,11 @@ struct dfs_dirent* readdir(DIR *d) struct dfs_fd* fd; fd = fd_get(d->fd); + if (fd == RT_NULL) + { + rt_set_errno(-RT_ERROR); + return RT_NULL; + } if (!d->num || (d->cur += ((struct dfs_dirent*)(d->buf + d->cur))->d_reclen) >= d->num) { @@ -428,6 +453,12 @@ rt_off_t telldir(DIR *d) rt_off_t result; fd = fd_get(d->fd); + if (fd == RT_NULL) + { + rt_set_errno(-RT_ERROR); + return 0; + } + result = fd->pos - d->num + d->cur; fd_put(fd); @@ -450,6 +481,12 @@ void seekdir(DIR *d, rt_off_t offset) struct dfs_fd* fd; fd = fd_get(d->fd); + if (fd == RT_NULL) + { + rt_set_errno(-RT_ERROR); + return ; + } + if (dfile_raw_lseek(fd, offset) >= 0) d->num = d->cur = 0; fd_put(fd); } @@ -470,6 +507,12 @@ void rewinddir(DIR *d) struct dfs_fd* fd; fd = fd_get(d->fd); + if (fd == RT_NULL) + { + rt_set_errno(-RT_ERROR); + return ; + } + if (dfile_raw_lseek(fd, 0) >= 0) d->num = d->cur = 0; fd_put(fd); } @@ -491,6 +534,12 @@ int closedir(DIR* d) struct dfs_fd* fd; fd = fd_get(d->fd); + if (fd == RT_NULL) + { + rt_set_errno(-RT_ERROR); + return -1; + } + result = dfile_raw_close(fd); fd_put(fd); diff --git a/include/rtthread.h b/include/rtthread.h index b8d4847aaf..00cfd42758 100644 --- a/include/rtthread.h +++ b/include/rtthread.h @@ -172,6 +172,10 @@ void* rt_malloc(rt_size_t nbytes); void rt_free (void *ptr); void* rt_realloc(void *ptr, rt_size_t nbytes); void *rt_calloc(rt_size_t count, rt_size_t size); + +void rt_memory_info(rt_uint32_t *total, + rt_uint32_t *used, + rt_uint32_t *max_used); #ifdef RT_USING_HOOK void rt_malloc_sethook(void (*hook)(void *ptr, rt_uint32_t size)); diff --git a/rtgui/common/font.c b/rtgui/common/font.c index 65a64705cc..4979348c22 100644 --- a/rtgui/common/font.c +++ b/rtgui/common/font.c @@ -29,26 +29,41 @@ void rtgui_font_system_init() rtgui_list_init(&(_rtgui_font_list)); /* set default font to NULL */ - rtgui_default_font = RT_NULL; + rtgui_default_font = RT_NULL; #ifdef RTGUI_USING_FONT16 rtgui_font_system_add_font(&rtgui_font_asc16); #ifdef RTGUI_USING_FONTHZ rtgui_font_system_add_font(&rtgui_font_hz16); #endif -#endif +#endif + +#ifdef RTGUI_USING_FONT12 rtgui_font_system_add_font(&rtgui_font_asc12); #ifdef RTGUI_USING_FONTHZ rtgui_font_system_add_font(&rtgui_font_hz12); #endif +#endif +#ifdef RTGUI_USING_FONT12 rtgui_font_set_defaut(&rtgui_font_asc12); +#elif defined(RTGUI_USING_FONT16) + rtgui_font_set_defaut(&rtgui_font_asc16); +#endif } void rtgui_font_system_add_font(struct rtgui_font* font) { rtgui_list_init(&(font->list)); rtgui_list_append(&_rtgui_font_list, &(font->list)); + + /* init font */ + if (font->engine->font_init != RT_NULL) + font->engine->font_init(font); + + /* first refer, load it */ + if (font->engine->font_load != RT_NULL) + font->engine->font_load(font); } void rtgui_font_system_remove_font(struct rtgui_font* font) diff --git a/rtgui/common/font_hz_file.c b/rtgui/common/font_hz_file.c index ac4372e50b..6c2967317d 100644 --- a/rtgui/common/font_hz_file.c +++ b/rtgui/common/font_hz_file.c @@ -7,30 +7,7 @@ #include #include -#define HZ_DATA_SIZE (2 * 16) #define HZ_CACHE_MAX 64 -SPLAY_HEAD(cache_tree, hz_cache); -struct hz_cache -{ - SPLAY_ENTRY(hz_cache) hz_node; - - rt_uint16_t hz_id; -}; - -struct rtgui_hz_file_font -{ - struct cache_tree cache_root; - rt_uint16_t cache_size; - - /* font size */ - rt_uint16_t font_size; - - /* file descriptor */ - int fd; - - /* font file name */ - const char* font_fn; -}; static int _font_cache_compare(struct hz_cache* node1, struct hz_cache* node2); @@ -70,24 +47,25 @@ static rt_uint8_t* _font_cache_get(struct rtgui_hz_file_font* font, rt_uint16_t } /* can not find it, load to cache */ - cache = (struct hz_cache*) rtgui_malloc(sizeof(struct hz_cache) + HZ_DATA_SIZE); + cache = (struct hz_cache*) rtgui_malloc(sizeof(struct hz_cache) + font->font_data_size); if (cache == RT_NULL) return RT_NULL; /* no memory yet */ cache->hz_id = hz_id; - seek = 94 * ((hz_id & 0xff - 0xA0) - 1) + ((hz_id >> 8) - 0xA0); - seek = seek * HZ_DATA_SIZE; + seek = 94 * (((hz_id & 0xff) - 0xA0) - 1) + ((hz_id >> 8) - 0xA0) - 1; + seek *= font->font_data_size; /* read hz font data */ if ((lseek(font->fd, seek, SEEK_SET) < 0) || - read(font->fd, (char*)(cache + 1), HZ_DATA_SIZE) != - HZ_DATA_SIZE) + read(font->fd, (char*)(cache + 1), font->font_data_size) != + font->font_data_size) { rtgui_free(cache); return RT_NULL; } /* insert to cache */ - SPLAY_INSERT(cache_tree, &(font->cache_root), cache); + SPLAY_INSERT(cache_tree, &(font->cache_root), cache); + font->cache_size ++; if (font->cache_size > HZ_CACHE_MAX) { @@ -97,7 +75,8 @@ static rt_uint8_t* _font_cache_get(struct rtgui_hz_file_font* font, rt_uint16_t while (SPLAY_LEFT(left, hz_node) != RT_NULL) left = SPLAY_LEFT(left, hz_node); /* remove the left node */ - SPLAY_REMOVE(cache_tree, &(font->cache_root), left); + SPLAY_REMOVE(cache_tree, &(font->cache_root), left); + font->cache_size --; } return (rt_uint8_t*)(cache + 1); @@ -105,7 +84,7 @@ static rt_uint8_t* _font_cache_get(struct rtgui_hz_file_font* font, rt_uint16_t static void rtgui_hz_file_font_load(struct rtgui_font* font) { - struct rtgui_hz_file_font* hz_file_font = (struct rtgui_hz_file_font*)font; + struct rtgui_hz_file_font* hz_file_font = (struct rtgui_hz_file_font*)font->data; RT_ASSERT(hz_file_font != RT_NULL); hz_file_font->fd = open(hz_file_font->font_fn, O_RDONLY, 0); @@ -115,7 +94,7 @@ static void rtgui_hz_file_font_draw_text(struct rtgui_font* font, struct rtgui_d { rt_base_t h; rt_uint8_t* str; - struct rtgui_hz_file_font* hz_file_font = (struct rtgui_hz_file_font*)font; + struct rtgui_hz_file_font* hz_file_font = (struct rtgui_hz_file_font*)font->data; RT_ASSERT(hz_file_font != RT_NULL); /* drawing height */ @@ -130,7 +109,7 @@ static void rtgui_hz_file_font_draw_text(struct rtgui_font* font, struct rtgui_d register rt_base_t i, j, k; /* get font pixel data */ - font_ptr = _font_cache_get(hz_file_font, *(rt_uint16_t*)str); + font_ptr = _font_cache_get(hz_file_font, *str | (*(str+1) << 8)); /* draw word */ for (i=0; i < h; i ++) @@ -155,7 +134,7 @@ static void rtgui_hz_file_font_draw_text(struct rtgui_font* font, struct rtgui_d static void rtgui_hz_file_font_get_metrics(struct rtgui_font* font, const rt_uint8_t* text, rtgui_rect_t* rect) { - struct rtgui_hz_file_font* hz_file_font = (struct rtgui_hz_file_font*)font; + struct rtgui_hz_file_font* hz_file_font = (struct rtgui_hz_file_font*)font->data; RT_ASSERT(hz_file_font != RT_NULL); /* set metrics rect */ diff --git a/rtgui/common/hz12font.c b/rtgui/common/hz12font.c index 0c2e697505..e51ed16e47 100644 --- a/rtgui/common/hz12font.c +++ b/rtgui/common/hz12font.c @@ -1,5 +1,6 @@ #include - + +#ifdef RTGUI_USING_FONT12 #ifndef RTGUI_USING_HZ_FILE const unsigned char hz12_font[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -12311,3 +12312,4 @@ struct rtgui_font rtgui_font_hz12 = (void*)&hz12, /* font private data */ }; #endif +#endif diff --git a/rtgui/common/hz16font.c b/rtgui/common/hz16font.c index 01adb78534..8f316f13fd 100644 --- a/rtgui/common/hz16font.c +++ b/rtgui/common/hz16font.c @@ -1,5 +1,6 @@ -#include +#include +#ifdef RTGUI_USING_FONT16 #ifndef RTGUI_USING_HZ_FILE const unsigned char hz16_font[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -16752,20 +16753,21 @@ const struct rtgui_font rtgui_font_hz16 = #else struct rtgui_hz_file_font hz16 = { - {RT_NULL}, /* cache root */ - 0, /* cache_size */ - 16, /* font_size */ - -1, /* fd */ - "/resource/hz16" /* font_fn */ + {RT_NULL}, /* cache root */ + 0, /* cache size */ + 16, /* font size */ + 32, /* font data size */ + -1, /* fd */ + "/resource/hzk16.fnt" /* font_fn */ }; -extern struct rtgui_hz_file_font_engine hz_file_font_engine; -const struct rtgui_font rtgui_font_hz16 = +struct rtgui_font rtgui_font_hz16 = { - "hz", /* family */ - 16, /* height */ - 1, /* refer count */ - &hz_file_font_engine,/* font engine */ - (void*)&hz16, /* font private data */ + "hz", /* family */ + 16, /* height */ + 1, /* refer count */ + &rtgui_hz_file_font_engine,/* font engine */ + (void*)&hz16, /* font private data */ }; #endif +#endif diff --git a/rtgui/common/image_xpm.c b/rtgui/common/image_xpm.c index bb7ce057f5..4b171fd674 100644 --- a/rtgui/common/image_xpm.c +++ b/rtgui/common/image_xpm.c @@ -565,7 +565,8 @@ color_none: } } - free_colorhash(colors_table); + free_colorhash(colors_table); + rtgui_filerw_close(file); return RT_TRUE; } diff --git a/rtgui/include/rtgui/font.h b/rtgui/include/rtgui/font.h index db5b6857fc..43af8fd6a3 100644 --- a/rtgui/include/rtgui/font.h +++ b/rtgui/include/rtgui/font.h @@ -49,6 +49,32 @@ struct rtgui_font_bitmap }; extern struct rtgui_font_engine bmp_font_engine; +#include +SPLAY_HEAD(cache_tree, hz_cache); +struct hz_cache +{ + SPLAY_ENTRY(hz_cache) hz_node; + + rt_uint16_t hz_id; +}; + +struct rtgui_hz_file_font +{ + struct cache_tree cache_root; + rt_uint16_t cache_size; + + /* font size */ + rt_uint16_t font_size; + rt_uint16_t font_data_size; + + /* file descriptor */ + int fd; + + /* font file name */ + const char* font_fn; +}; +extern struct rtgui_font_engine rtgui_hz_file_font_engine; + struct rtgui_font { /* font name */ diff --git a/rtgui/include/rtgui/rtgui_config.h b/rtgui/include/rtgui/rtgui_config.h index 7ae7f633a5..4cce06fab4 100644 --- a/rtgui/include/rtgui/rtgui_config.h +++ b/rtgui/include/rtgui/rtgui_config.h @@ -23,6 +23,9 @@ /* #define RTGUI_USING_MOUSE_CURSOR */ #define RTGUI_USING_FONT16 +#define RTGUI_USING_FONTHZ + +#define RTGUI_USING_HZ_FILE // #define RT_USING_STDIO_FILERW #define RT_USING_DFS_FILERW diff --git a/rtgui/include/rtgui/widgets/view.h b/rtgui/include/rtgui/widgets/view.h index 3dd6c43e63..c4033c603e 100644 --- a/rtgui/include/rtgui/widgets/view.h +++ b/rtgui/include/rtgui/widgets/view.h @@ -41,6 +41,8 @@ struct rtgui_view }; typedef struct rtgui_view rtgui_view_t; +rtgui_type_t *rtgui_view_type_get(void); + rtgui_view_t* rtgui_view_create(const char* title); void rtgui_view_destroy(rtgui_view_t* view); diff --git a/src/mem.c b/src/mem.c index 37ad734a3d..d403e5a45a 100644 --- a/src/mem.c +++ b/src/mem.c @@ -487,7 +487,16 @@ void rt_free(void *rmem) rt_sem_release(&heap_sem); } -#ifdef RT_MEM_STATS +#ifdef RT_MEM_STATS +void rt_memory_info(rt_uint32_t *total, + rt_uint32_t *used, + rt_uint32_t *max_used) +{ + if (total != RT_NULL) *total = mem_size_aligned; + if (used != RT_NULL) *used = used_mem; + if (max_used != RT_NULL) *max_used = max_mem; +} + #ifdef RT_USING_FINSH #include void list_mem()