From 6265bc3f4c4ef33894bfcfbb110bc76adde1ef8f Mon Sep 17 00:00:00 2001 From: Milan Toth Date: Mon, 27 Feb 2023 13:15:03 +0100 Subject: [PATCH] kineticUI update --- meson.build | 54 +++-- src/kinetic_ui/egl/ku_gl.c | 24 ++- src/kinetic_ui/egl/ku_gl_floatbuffer.c | 6 +- src/kinetic_ui/handler/vh_anim.c | 63 +++--- src/kinetic_ui/handler/vh_button.c | 68 +++--- src/kinetic_ui/handler/vh_cv_body.c | 13 +- src/kinetic_ui/handler/vh_cv_evnt.c | 58 ++++-- src/kinetic_ui/handler/vh_cv_scrl.c | 26 +-- src/kinetic_ui/handler/vh_drag.c | 23 ++- src/kinetic_ui/handler/vh_key.c | 16 +- src/kinetic_ui/handler/vh_knob.c | 29 ++- src/kinetic_ui/handler/vh_roll.c | 20 +- src/kinetic_ui/handler/vh_slider.c | 30 +-- src/kinetic_ui/handler/vh_table.c | 163 +++++++++------ src/kinetic_ui/handler/vh_tbl_body.c | 83 ++++---- src/kinetic_ui/handler/vh_tbl_evnt.c | 276 +++++++++++++++---------- src/kinetic_ui/handler/vh_tbl_head.c | 47 +++-- src/kinetic_ui/handler/vh_tbl_scrl.c | 192 ++++++++++++----- src/kinetic_ui/handler/vh_textinput.c | 115 ++++++----- src/kinetic_ui/handler/vh_touch.c | 17 +- src/kinetic_ui/ku_connector_wayland.c | 197 ++++++++++++------ src/kinetic_ui/ku_css.c | 2 +- src/kinetic_ui/ku_draw.c | 74 +++++-- src/kinetic_ui/ku_event.c | 14 +- src/kinetic_ui/ku_fontconfig.c | 9 +- src/kinetic_ui/ku_gen_css.c | 48 +++-- src/kinetic_ui/ku_gen_type.c | 2 +- src/kinetic_ui/ku_html.c | 4 +- src/kinetic_ui/ku_recorder.c | 12 +- src/kinetic_ui/ku_renderer_egl.c | 7 +- src/kinetic_ui/ku_renderer_soft.c | 2 +- src/kinetic_ui/ku_text.c | 42 ++-- src/kinetic_ui/ku_view.c | 121 ++++++----- src/kinetic_ui/ku_window.c | 131 ++++++------ src/kinetic_ui/texture/tg_css.c | 4 +- src/kinetic_ui/texture/tg_knob.c | 11 +- src/kinetic_ui/texture/tg_scaledimg.c | 6 +- src/kinetic_ui/texture/tg_text.c | 10 +- src/mt_core/mt_channel.c | 19 +- src/mt_core/mt_map.c | 23 +-- src/mt_core/mt_memory.c | 166 +++++---------- src/mt_core/mt_path.c | 179 ++++++++-------- src/mt_core/mt_string.c | 58 ++++-- src/mt_core/mt_vector.c | 154 +++++++------- src/mt_core/mt_wrapper.c | 4 +- src/{kinetic_ui => mt_core}/utf8.h | 0 src/mt_core_ext/ku_bitmap_ext.c | 91 ++++++++ src/mt_core_ext/mt_string_ext.c | 11 +- src/mt_core_test/mt_channel_test.c | 93 +++++++-- src/mt_core_test/mt_core_test.c | 21 ++ src/mt_core_test/mt_map_test.c | 127 ++++++++++-- src/mt_core_test/mt_memory_test.c | 135 ++++++++++++ src/mt_core_test/mt_path_test.c | 64 ++++++ src/mt_core_test/mt_string_test.c | 97 ++++++++- src/mt_core_test/mt_vector_test.c | 171 +++++++++++++++ src/mt_math_test/mt_math_2d_test.c | 8 + src/sov/draw.c | 5 +- src/sov/gen.c | 18 +- src/sov/sov.c | 24 +-- src/sov/tree.c | 13 +- src/tests/test_json.c | 7 - 61 files changed, 2296 insertions(+), 1211 deletions(-) rename src/{kinetic_ui => mt_core}/utf8.h (100%) create mode 100644 src/mt_core_ext/ku_bitmap_ext.c create mode 100644 src/mt_core_test/mt_core_test.c create mode 100644 src/mt_core_test/mt_memory_test.c create mode 100644 src/mt_core_test/mt_path_test.c create mode 100644 src/mt_core_test/mt_vector_test.c delete mode 100644 src/tests/test_json.c diff --git a/meson.build b/meson.build index 06f56da..e4d998c 100644 --- a/meson.build +++ b/meson.build @@ -1,9 +1,10 @@ project( 'sov', 'c', - version: '0.87b', + version: '0.9b', license: 'MIT', - default_options: ['c_std=gnu99'] + default_options: ['c_std=gnu99', + 'warning_level=3'] ) cc = meson.get_compiler('c') @@ -70,8 +71,8 @@ sov_version = '"@0@"'.format(meson.project_version()) pkg_datadir = join_paths(get_option('prefix'), get_option('datadir')) / 'sov' add_project_arguments('-DPKG_DATADIR="' + pkg_datadir + '"', '-DSOV_VERSION=@0@'.format(sov_version), + '-Wno-unused-parameter', language: 'c') -add_project_arguments('-Wno-unused-but-set-variable', language: 'c') if get_option('buildtype') == 'debug' add_project_arguments('-DDEBUG',language: 'c') @@ -114,9 +115,6 @@ com_sources = ['src/sov/jsmn.c', 'src/sov/ku_bitmap_ext.c', 'src/mt_core_ext/mt_string_ext.c', -# 'src/mt_core_ext/mt_map_ext.c', - -# 'src/mt_core/mt_channel.c', 'src/mt_core/mt_string.c', 'src/mt_core/mt_path.c', 'src/mt_core/mt_log.c', @@ -126,14 +124,7 @@ com_sources = ['src/sov/jsmn.c', 'src/mt_core/mt_time.c', 'src/mt_core/mt_vector.c', 'src/mt_core/mt_wrapper.c', - -# 'src/mt_math/mt_matrix_3d.c', 'src/mt_math/mt_matrix_4d.c', -# 'src/mt_math/mt_vector_2d.c', -# 'src/mt_math/mt_vector_3d.c', -# 'src/mt_math/mt_vector_4d.c', -# 'src/mt_math/mt_math_2d.c', -# 'src/mt_math/mt_math_3d.c', 'src/kinetic_ui/ku_event.c', 'src/kinetic_ui/ku_html.c', @@ -142,26 +133,12 @@ com_sources = ['src/sov/jsmn.c', 'src/kinetic_ui/ku_rect.c', 'src/kinetic_ui/ku_bitmap.c', 'src/kinetic_ui/texture/tg_css.c', -# 'src/kinetic_ui/texture/tg_knob.c', 'src/kinetic_ui/texture/tg_text.c', -# 'src/kinetic_ui/texture/tg_scaledimg.c', 'src/kinetic_ui/handler/vh_anim.c', 'src/kinetic_ui/handler/vh_button.c', 'src/kinetic_ui/handler/vh_drag.c', 'src/kinetic_ui/handler/vh_key.c', -# 'src/kinetic_ui/handler/vh_knob.c', -# 'src/kinetic_ui/handler/vh_roll.c', 'src/kinetic_ui/handler/vh_slider.c', -# 'src/kinetic_ui/handler/vh_textinput.c', -# 'src/kinetic_ui/handler/vh_touch.c', -# 'src/kinetic_ui/handler/vh_cv_body.c', -# 'src/kinetic_ui/handler/vh_cv_scrl.c', -# 'src/kinetic_ui/handler/vh_cv_evnt.c', -# 'src/kinetic_ui/handler/vh_tbl_body.c', -# 'src/kinetic_ui/handler/vh_tbl_evnt.c', -# 'src/kinetic_ui/handler/vh_tbl_scrl.c', -# 'src/kinetic_ui/handler/vh_tbl_head.c', -# 'src/kinetic_ui/handler/vh_table.c', 'src/kinetic_ui/ku_view.c', 'src/kinetic_ui/egl/ku_gl_atlas.c', 'src/kinetic_ui/egl/ku_gl_floatbuffer.c', @@ -206,8 +183,27 @@ install_data('res/html/main.css', install_dir : pkg_datadir / 'html' ) install_data('contrib/systemd/sov.service', install_dir : get_option('libdir') / 'systemd' / 'user' ) install_data('contrib/systemd/sov.socket', install_dir : get_option('libdir') / 'systemd' / 'user' ) -jsontest = executable('maintest', 'src/tests/test_json.c') +mt_core_test = executable( + 'mt_core_test', + ['src/mt_core/mt_log.c', + 'src/mt_core/mt_memory.c', + 'src/mt_core/mt_vector.c', + 'src/mt_core/mt_map.c', + 'src/mt_core/mt_string.c', + 'src/mt_core/mt_channel.c', + 'src/mt_core/mt_path.c', + 'src/mt_core_test/mt_core_test.c', + 'src/mt_core_test/mt_memory_test.c', + 'src/mt_core_test/mt_vector_test.c', + 'src/mt_core_test/mt_map_test.c', + 'src/mt_core_test/mt_string_test.c', + 'src/mt_core_test/mt_channel_test.c', + 'src/mt_core_test/mt_path_test.c' + ], + include_directories: ['src/mt_core']) + +test('MT_CORE_TEST', mt_core_test) + scripttests = find_program('tst/runtests.sh') -test('JSON TEST', jsontest) test('SCRIPTED TESTS', scripttests, timeout: 0, workdir : meson.current_source_dir(), args : [meson.current_build_dir(),meson.current_source_dir()]) diff --git a/src/kinetic_ui/egl/ku_gl.c b/src/kinetic_ui/egl/ku_gl.c index 4ef1226..709aff0 100644 --- a/src/kinetic_ui/egl/ku_gl.c +++ b/src/kinetic_ui/egl/ku_gl.c @@ -76,7 +76,7 @@ glsha_t ku_gl_create_texture_shader() "{" " highp float alpha = vUv.w;" " highp vec4 col = texture2D(sampler[0], vUv.xy);" - " if (alpha < 1.0) col.w *= alpha;" + " if (alpha != 1.0) col.w = alpha;" " gl_FragColor = col;" "}"; @@ -136,8 +136,9 @@ gltex_t ku_gl_create_texture(int index, uint32_t w, uint32_t h) glActiveTexture(GL_TEXTURE0 + tex.index); glBindTexture(GL_TEXTURE_2D, tex.tx); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + /* !!! GL_LINEAR breaks session record/replay determinism, interpolated values can somehow differ */ + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); @@ -169,9 +170,7 @@ void ku_gl_init(int max_dev_width, int max_dev_height) { GLenum err = glewInit(); if (GLEW_OK != err) - { - printf("ERROR 5 %s\n", glewGetErrorString(err)); - } + mt_log_debug("GLEW Init error %s\n", glewGetErrorString(err)); kugl.shader = ku_gl_create_texture_shader(); kugl.buffer = ku_gl_create_vertex_buffer(); @@ -195,7 +194,8 @@ void ku_gl_init(int max_dev_width, int max_dev_height) /* don't let it be bigger than max size */ - if (binsize > value) binsize = value; + if (binsize > value) + binsize = value; mt_log_debug("texture size will be %i", binsize); @@ -235,7 +235,8 @@ void ku_gl_add_textures(mt_vector_t* views, int force) } /* reset atlas */ - if (kugl.atlas) REL(kugl.atlas); + if (kugl.atlas) + REL(kugl.atlas); kugl.atlas = ku_gl_atlas_new(kugl.texturesize, kugl.texturesize); } @@ -245,7 +246,7 @@ void ku_gl_add_textures(mt_vector_t* views, int force) int reset = 0; /* add texture to atlas */ - for (int index = 0; index < views->length; index++) + for (size_t index = 0; index < views->length; index++) { ku_view_t* view = views->data[index]; @@ -328,7 +329,8 @@ void ku_gl_add_textures(mt_vector_t* views, int force) glBindTexture(GL_TEXTURE_2D, 0); /* in case of reset do the whole thing once again with forced reset*/ - if (reset == 1) ku_gl_add_textures(views, 1); + if (reset == 1) + ku_gl_add_textures(views, 1); } /* upload vertexes */ @@ -338,7 +340,7 @@ void ku_gl_add_vertexes(mt_vector_t* views) ku_floatbuffer_reset(kugl.floatbuffer); /* add vertexes to buffer */ - for (int index = 0; index < views->length; index++) + for (size_t index = 0; index < views->length; index++) { ku_view_t* view = views->data[index]; diff --git a/src/kinetic_ui/egl/ku_gl_floatbuffer.c b/src/kinetic_ui/egl/ku_gl_floatbuffer.c index 71c85f3..52cff01 100644 --- a/src/kinetic_ui/egl/ku_gl_floatbuffer.c +++ b/src/kinetic_ui/egl/ku_gl_floatbuffer.c @@ -9,8 +9,8 @@ typedef struct ku_floatbuffer_t ku_floatbuffer_t; struct ku_floatbuffer_t { GLfloat* data; - uint32_t pos; - uint32_t cap; + size_t pos; + size_t cap; char changed; }; @@ -56,7 +56,7 @@ void ku_floatbuffer_reset(ku_floatbuffer_t* fb) void ku_floatbuffer_expand(ku_floatbuffer_t* fb) { - assert(fb->cap < UINT32_MAX / 2); + assert(fb->cap < SIZE_MAX / 2); fb->cap *= 2; fb->data = mt_memory_realloc(fb->data, sizeof(void*) * fb->cap); } diff --git a/src/kinetic_ui/handler/vh_anim.c b/src/kinetic_ui/handler/vh_anim.c index dd4a228..cdc62b7 100644 --- a/src/kinetic_ui/handler/vh_anim.c +++ b/src/kinetic_ui/handler/vh_anim.c @@ -69,9 +69,11 @@ void vh_anim_add(ku_view_t* view, void (*on_event)(vh_anim_event_t), void* userd #if __INCLUDE_LEVEL__ == 0 -void vh_anim_evt(ku_view_t* view, ku_event_t ev) +#include "mt_log.c" + +int vh_anim_evt(ku_view_t* view, ku_event_t ev) { - vh_anim_t* vh = view->handler_data; + vh_anim_t* vh = view->evt_han_data; if (ev.type == KU_EVENT_FRAME) { if (vh->anim_frame) @@ -100,16 +102,17 @@ void vh_anim_evt(ku_view_t* view, ku_event_t ev) else if (vh->type == AT_EASE) { // speed function based on cosine ( half circle ) - float angle = 3.14 + (3.14 / vh->fsteps) * delta; - float delta = (cos(angle) + 1.0) / 2.0; + float angle = (3.14 / 2 / vh->fsteps) * delta; + float fdelta = sin(angle); - cf.x = sf.x + (ef.x - sf.x) * delta; - cf.y = sf.y + (ef.y - sf.y) * delta; - cf.w = sf.w + (ef.w - sf.w) * delta; - cf.h = sf.h + (ef.h - sf.h) * delta; + cf.x = sf.x + (ef.x - sf.x) * fdelta; + cf.y = sf.y + (ef.y - sf.y) * fdelta; + cf.w = sf.w + (ef.w - sf.w) * fdelta; + cf.h = sf.h + (ef.h - sf.h) * fdelta; } - if (delta == vh->fsteps - 1) cf = ef; + if (delta == vh->fsteps - 1) + cf = ef; ku_view_set_frame(view, cf); @@ -119,7 +122,8 @@ void vh_anim_evt(ku_view_t* view, ku_event_t ev) { vh->anim_frame = 0; vh_anim_event_t event = {.id = VH_ANIM_END, .view = view, .userdata = vh->userdata}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } } } @@ -159,7 +163,8 @@ void vh_anim_evt(ku_view_t* view, ku_event_t ev) cr.h = sr.h + (er.h - sr.h) * delta; } - if (delta == vh->rsteps - 1) cr = er; + if (delta == vh->rsteps - 1) + cr = er; ku_view_set_region(view, cr); @@ -167,9 +172,10 @@ void vh_anim_evt(ku_view_t* view, ku_event_t ev) if (delta == vh->rsteps) { - ku_view_set_region(view, (ku_rect_t){-1, -1, -1. - 1}); + ku_view_set_region(view, (ku_rect_t){-1, -1, -1, -1}); vh_anim_event_t event = {.id = VH_ANIM_END, .view = view, .userdata = vh->userdata}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } } } @@ -195,7 +201,8 @@ void vh_anim_evt(ku_view_t* view, ku_event_t ev) ca = sa + (ea - sa) * delta; } - if (vh->astep == vh->asteps - 1) ca = ea; + if (vh->astep == vh->asteps - 1) + ca = ea; ku_view_set_texture_alpha(view, ca, 1); @@ -205,16 +212,19 @@ void vh_anim_evt(ku_view_t* view, ku_event_t ev) { vh->anim_alpha = 0; vh_anim_event_t event = {.id = VH_ANIM_END, .view = view, .userdata = vh->userdata}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } } } } + + return 1; } void vh_anim_frame(ku_view_t* view, ku_rect_t sf, ku_rect_t ef, int start, int steps, animtype_t type) { - vh_anim_t* vh = view->handler_data; + vh_anim_t* vh = view->evt_han_data; if (vh->fstep == vh->fsteps) { vh->sf = sf; @@ -229,7 +239,7 @@ void vh_anim_frame(ku_view_t* view, ku_rect_t sf, ku_rect_t ef, int start, int s void vh_anim_region(ku_view_t* view, ku_rect_t sr, ku_rect_t er, int start, int steps, animtype_t type) { - vh_anim_t* vh = view->handler_data; + vh_anim_t* vh = view->evt_han_data; if (vh->rstep == vh->rsteps) { vh->sr = sr; @@ -246,7 +256,7 @@ void vh_anim_region(ku_view_t* view, ku_rect_t sr, ku_rect_t er, int start, int void vh_anim_alpha(ku_view_t* view, float sa, float ea, int steps, animtype_t type) { - vh_anim_t* vh = view->handler_data; + vh_anim_t* vh = view->evt_han_data; if (vh->astep == vh->asteps) { @@ -264,14 +274,17 @@ void vh_anim_alpha(ku_view_t* view, float sa, float ea, int steps, animtype_t ty void vh_anim_finish(ku_view_t* view) { - vh_anim_t* vh = view->handler_data; + vh_anim_t* vh = view->evt_han_data; vh->astep = vh->asteps; vh->fstep = vh->fsteps; vh->rstep = vh->rsteps; - if (vh->anim_frame) ku_view_set_frame(view, vh->ef); - if (vh->anim_region) ku_view_set_region(view, vh->er); - if (vh->anim_alpha) ku_view_set_texture_alpha(view, vh->ea, 1); + if (vh->anim_frame) + ku_view_set_frame(view, vh->ef); + if (vh->anim_region) + ku_view_set_region(view, vh->er); + if (vh->anim_alpha) + ku_view_set_texture_alpha(view, vh->ea, 1); vh->anim_frame = 0; vh->anim_region = 0; @@ -285,14 +298,14 @@ void vh_anim_desc(void* p, int level) void vh_anim_add(ku_view_t* view, void (*on_event)(vh_anim_event_t), void* userdata) { - assert(view->handler == NULL && view->handler_data == NULL); + assert(view->evt_han == NULL && view->evt_han_data == NULL); vh_anim_t* vh = CAL(sizeof(vh_anim_t), NULL, vh_anim_desc); vh->on_event = on_event; vh->userdata = userdata; - view->handler = vh_anim_evt; - view->handler_data = vh; + view->evt_han = vh_anim_evt; + view->evt_han_data = vh; } #endif diff --git a/src/kinetic_ui/handler/vh_button.c b/src/kinetic_ui/handler/vh_button.c index d11eb57..6efb538 100644 --- a/src/kinetic_ui/handler/vh_button.c +++ b/src/kinetic_ui/handler/vh_button.c @@ -54,7 +54,7 @@ void vh_button_set_enabled(ku_view_t* view, int flag); void vh_button_on_anim(vh_anim_event_t event) { ku_view_t* btnview = (ku_view_t*) event.userdata; - vh_button_t* vh = btnview->handler_data; + vh_button_t* vh = btnview->evt_han_data; // if offview alpha is 0 and state is released @@ -67,9 +67,9 @@ void vh_button_on_anim(vh_anim_event_t event) } } -void vh_button_evt(ku_view_t* view, ku_event_t ev) +int vh_button_evt(ku_view_t* view, ku_event_t ev) { - vh_button_t* vh = view->handler_data; + vh_button_t* vh = view->evt_han_data; if (vh->enabled) { @@ -79,8 +79,10 @@ void vh_button_evt(ku_view_t* view, ku_event_t ev) { vh->state = VH_BUTTON_DOWN; - if (vh->offview) vh_anim_alpha(vh->offview, 1.0, 0.0, 10, AT_LINEAR); - if (vh->onview) vh_anim_alpha(vh->onview, 0.0, 1.0, 10, AT_LINEAR); + if (vh->offview) + vh_anim_alpha(vh->offview, 1.0, 0.0, 10, AT_LINEAR); + if (vh->onview) + vh_anim_alpha(vh->onview, 0.0, 1.0, 10, AT_LINEAR); } } else if (ev.type == KU_EVENT_MOUSE_UP) @@ -90,36 +92,44 @@ void vh_button_evt(ku_view_t* view, ku_event_t ev) if (vh->state == VH_BUTTON_UP) { vh->state = VH_BUTTON_DOWN; - if (vh->offview) vh_anim_alpha(vh->offview, 1.0, 0.0, 10, AT_LINEAR); - if (vh->onview) vh_anim_alpha(vh->onview, 0.0, 1.0, 10, AT_LINEAR); + if (vh->offview) + vh_anim_alpha(vh->offview, 1.0, 0.0, 10, AT_LINEAR); + if (vh->onview) + vh_anim_alpha(vh->onview, 0.0, 1.0, 10, AT_LINEAR); } else { vh->state = VH_BUTTON_UP; - if (vh->offview) vh_anim_alpha(vh->offview, 0.0, 1.0, 10, AT_LINEAR); - if (vh->onview) vh_anim_alpha(vh->onview, 1.0, 0.0, 10, AT_LINEAR); + if (vh->offview) + vh_anim_alpha(vh->offview, 0.0, 1.0, 10, AT_LINEAR); + if (vh->onview) + vh_anim_alpha(vh->onview, 1.0, 0.0, 10, AT_LINEAR); } vh_button_event_t event = {.id = VH_BUTTON_EVENT, .view = view, .vh = vh, .ev = ev}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } else { vh->state = VH_BUTTON_UP; vh_button_event_t event = {.id = VH_BUTTON_EVENT, .view = view, .vh = vh, .ev = ev}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); /* if (vh->offview) vh_anim_alpha(vh->offview, 1.0, 0.0, 10, AT_LINEAR); */ /* if (vh->onview) vh_anim_alpha(vh->onview, 0.0, 1.0, 10, AT_LINEAR); */ } } } + + return 0; } void vh_button_set_state(ku_view_t* view, vh_button_state_t state) { - vh_button_t* vh = view->handler_data; + vh_button_t* vh = view->evt_han_data; if (vh->enabled) { @@ -127,13 +137,17 @@ void vh_button_set_state(ku_view_t* view, vh_button_state_t state) if (state) { - if (vh->offview) vh_anim_alpha(vh->offview, 1.0, 0.0, 10, AT_LINEAR); - if (vh->onview) vh_anim_alpha(vh->onview, 0.0, 1.0, 10, AT_LINEAR); + if (vh->offview) + vh_anim_alpha(vh->offview, 1.0, 0.0, 10, AT_LINEAR); + if (vh->onview) + vh_anim_alpha(vh->onview, 0.0, 1.0, 10, AT_LINEAR); } else { - if (vh->offview) vh_anim_alpha(vh->offview, 0.0, 1.0, 10, AT_LINEAR); - if (vh->onview) vh_anim_alpha(vh->onview, 1.0, 0.0, 10, AT_LINEAR); + if (vh->offview) + vh_anim_alpha(vh->offview, 0.0, 1.0, 10, AT_LINEAR); + if (vh->onview) + vh_anim_alpha(vh->onview, 1.0, 0.0, 10, AT_LINEAR); } } } @@ -150,7 +164,7 @@ void vh_button_desc(void* p, int level) void vh_button_add(ku_view_t* view, vh_button_type_t type, void (*on_event)(vh_button_event_t)) { - assert(view->handler == NULL && view->handler_data == NULL); + assert(view->evt_han == NULL && view->evt_han_data == NULL); vh_button_t* vh = CAL(sizeof(vh_button_t), vh_button_del, vh_button_desc); vh->on_event = on_event; @@ -170,26 +184,28 @@ void vh_button_add(ku_view_t* view, vh_button_type_t type, void (*on_event)(vh_b vh_anim_add(vh->onview, vh_button_on_anim, view); } - if (vh->offview) ku_view_set_texture_alpha(vh->offview, 1.0, 0); - if (vh->onview) ku_view_set_texture_alpha(vh->onview, 0.0, 0); + if (vh->offview) + ku_view_set_texture_alpha(vh->offview, 1.0, 0); + if (vh->onview) + ku_view_set_texture_alpha(vh->onview, 0.0, 0); - view->handler = vh_button_evt; - view->handler_data = vh; - view->needs_touch = 1; - view->blocks_touch = 1; + view->evt_han = vh_button_evt; + view->evt_han_data = vh; } void vh_button_set_enabled(ku_view_t* view, int flag) { - vh_button_t* vh = view->handler_data; + vh_button_t* vh = view->evt_han_data; if (flag) { - if (vh->enabled == 0 && vh->offview) vh_anim_alpha(vh->offview, 0.3, 1.0, 10, AT_LINEAR); + if (vh->enabled == 0 && vh->offview) + vh_anim_alpha(vh->offview, 0.3, 1.0, 10, AT_LINEAR); } else { - if (vh->enabled == 1 && vh->offview) vh_anim_alpha(vh->offview, 1.0, 0.3, 10, AT_LINEAR); + if (vh->enabled == 1 && vh->offview) + vh_anim_alpha(vh->offview, 1.0, 0.3, 10, AT_LINEAR); } vh->enabled = flag; diff --git a/src/kinetic_ui/handler/vh_cv_body.c b/src/kinetic_ui/handler/vh_cv_body.c index 2f2cd31..c11b6d5 100644 --- a/src/kinetic_ui/handler/vh_cv_body.c +++ b/src/kinetic_ui/handler/vh_cv_body.c @@ -67,7 +67,7 @@ void vh_cv_body_attach( ku_view_t* view, void* userdata) { - assert(view->handler == NULL && view->handler_data == NULL); + assert(view->evt_han == NULL && view->evt_han_data == NULL); vh_cv_body_t* vh = CAL(sizeof(vh_cv_body_t), vh_cv_body_del, vh_cv_body_desc); vh->userdata = userdata; @@ -77,7 +77,7 @@ void vh_cv_body_attach( vh->cw = 1; vh->ch = 1; - view->handler_data = vh; + view->evt_han_data = vh; } void vh_cv_body_set_content_size( @@ -85,7 +85,7 @@ void vh_cv_body_set_content_size( int cw, int ch) { - vh_cv_body_t* vh = view->handler_data; + vh_cv_body_t* vh = view->evt_han_data; vh->cw = cw; vh->ch = ch; @@ -127,7 +127,7 @@ void vh_cv_body_move( float dx, float dy) { - vh_cv_body_t* vh = view->handler_data; + vh_cv_body_t* vh = view->evt_han_data; ku_rect_t frame = vh->content->frame.local; frame.x += dx; @@ -141,7 +141,7 @@ void vh_cv_body_zoom( int x, int y) { - vh_cv_body_t* vh = view->handler_data; + vh_cv_body_t* vh = view->evt_han_data; ku_rect_t gf = vh->content->frame.global; ku_rect_t lf = vh->content->frame.local; @@ -157,7 +157,8 @@ void vh_cv_body_zoom( float rh = ph / gf.h; vh->zoom = z; - if (vh->zoom < 0.001) vh->zoom = 0.001; + if (vh->zoom < 0.001) + vh->zoom = 0.001; float nw = vh->cw * vh->zoom; float nh = vh->ch * vh->zoom; diff --git a/src/kinetic_ui/handler/vh_cv_evnt.c b/src/kinetic_ui/handler/vh_cv_evnt.c index b1c7583..a67f36d 100644 --- a/src/kinetic_ui/handler/vh_cv_evnt.c +++ b/src/kinetic_ui/handler/vh_cv_evnt.c @@ -11,11 +11,15 @@ enum vh_cv_evnt_event_id { VH_CV_EVENT_RESIZE, VH_CV_EVENT_CLICK, + VH_CV_EVENT_KEY_DOWN, + VH_CV_EVENT_KEY_UP }; typedef struct _vh_cv_evnt_event_t { enum vh_cv_evnt_event_id id; + ku_view_t* view; + ku_event_t ev; } vh_cv_evnt_event_t; typedef struct _vh_cv_evnt_t @@ -48,13 +52,13 @@ void vh_cv_evnt_zoom(ku_view_t* view, float delta); #define SCROLLBAR 20.0 -void vh_cv_evnt_evt(ku_view_t* view, ku_event_t ev) +int vh_cv_evnt_evt(ku_view_t* view, ku_event_t ev) { - vh_cv_evnt_t* vh = view->handler_data; + vh_cv_evnt_t* vh = view->evt_han_data; if (ev.type == KU_EVENT_FRAME) { - vh_cv_body_t* bvh = vh->tbody_view->handler_data; + vh_cv_body_t* bvh = vh->tbody_view->evt_han_data; ku_rect_t cf = bvh->content->frame.local; vh->sx *= 0.8; @@ -72,7 +76,8 @@ void vh_cv_evnt_evt(ku_view_t* view, ku_event_t ev) if (hth >= view->frame.local.h) { - if (top > 0.001) dy -= top / 5.0; // scroll back top item + if (top > 0.001) + dy -= top / 5.0; // scroll back top item if (bot < view->frame.local.h - 0.001) { dy += (view->frame.local.h - bot) / 5.0; // scroll back bottom item @@ -86,7 +91,8 @@ void vh_cv_evnt_evt(ku_view_t* view, ku_event_t ev) if (wth >= view->frame.local.w) { - if (lft > 0.01) dx -= lft / 5.0; + if (lft > 0.01) + dx -= lft / 5.0; if (rgt < view->frame.local.w - 0.01) { dx += (view->frame.local.w - rgt) / 5.0; @@ -108,14 +114,17 @@ void vh_cv_evnt_evt(ku_view_t* view, ku_event_t ev) vh_cv_body_zoom(vh->tbody_view, zoom, vh->mx, vh->my); vh_cv_evnt_event_t event = {.id = VH_CV_EVENT_RESIZE}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } - if (vh->tscrl_view && vh->scroll_visible) vh_cv_scrl_update(vh->tscrl_view); + if (vh->tscrl_view && vh->scroll_visible) + vh_cv_scrl_update(vh->tscrl_view); - vh_cv_scrl_t* svh = vh->tscrl_view->handler_data; + vh_cv_scrl_t* svh = vh->tscrl_view->evt_han_data; - if (svh->state > 0) vh_cv_scrl_update(vh->tscrl_view); + if (svh->state > 0) + vh_cv_scrl_update(vh->tscrl_view); } else if (ev.type == KU_EVENT_SCROLL) { @@ -185,7 +194,8 @@ void vh_cv_evnt_evt(ku_view_t* view, ku_event_t ev) vh_cv_scrl_scroll_h(vh->tscrl_view, ev.x - view->frame.global.x); } vh_cv_evnt_event_t event = {.id = VH_CV_EVENT_CLICK}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } else if (ev.type == KU_EVENT_MOUSE_UP) { @@ -193,12 +203,28 @@ void vh_cv_evnt_evt(ku_view_t* view, ku_event_t ev) } else if (ev.type == KU_EVENT_FOCUS) { - if (vh->tscrl_view) vh_cv_scrl_show(vh->tscrl_view); + if (vh->tscrl_view) + vh_cv_scrl_show(vh->tscrl_view); } else if (ev.type == KU_EVENT_UNFOCUS) { - if (vh->tscrl_view) vh_cv_scrl_hide(vh->tscrl_view); + if (vh->tscrl_view) + vh_cv_scrl_hide(vh->tscrl_view); + } + else if (ev.type == KU_EVENT_KEY_DOWN) + { + vh_cv_evnt_event_t event = {.id = VH_CV_EVENT_KEY_DOWN, .view = view, .ev = ev}; + if (vh->on_event) + (*vh->on_event)(event); } + else if (ev.type == KU_EVENT_KEY_UP) + { + vh_cv_evnt_event_t event = {.id = VH_CV_EVENT_KEY_UP, .view = view, .ev = ev}; + if (vh->on_event) + (*vh->on_event)(event); + } + + return 0; } void vh_cv_evnt_del(void* p) @@ -217,7 +243,7 @@ void vh_cv_evnt_attach( void* userdata, void (*on_event)(vh_cv_evnt_event_t)) { - assert(view->handler == NULL && view->handler_data == NULL); + assert(view->evt_han == NULL && view->evt_han_data == NULL); vh_cv_evnt_t* vh = CAL(sizeof(vh_cv_evnt_t), vh_cv_evnt_del, vh_cv_evnt_desc); vh->userdata = userdata; @@ -226,13 +252,13 @@ void vh_cv_evnt_attach( vh->on_event = on_event; vh->zoom = 1.0; - view->handler_data = vh; - view->handler = vh_cv_evnt_evt; + view->evt_han_data = vh; + view->evt_han = vh_cv_evnt_evt; } void vh_cv_evnt_zoom(ku_view_t* view, float delta) { - vh_cv_evnt_t* vh = view->handler_data; + vh_cv_evnt_t* vh = view->evt_han_data; vh->zoom += delta; vh->tbody_view->frame.dim_changed = 1; diff --git a/src/kinetic_ui/handler/vh_cv_scrl.c b/src/kinetic_ui/handler/vh_cv_scrl.c index b7a56b2..ff42793 100644 --- a/src/kinetic_ui/handler/vh_cv_scrl.c +++ b/src/kinetic_ui/handler/vh_cv_scrl.c @@ -11,7 +11,7 @@ typedef struct _vh_cv_scrl_t ku_view_t* hori_v; int state; // 0 scroll 1 open 2 close int steps; - uint32_t item_cnt; + size_t item_cnt; void* userdata; } vh_cv_scrl_t; @@ -23,7 +23,7 @@ void vh_cv_scrl_attach( void vh_cv_scrl_update(ku_view_t* view); void vh_cv_scrl_show(ku_view_t* view); void vh_cv_scrl_hide(ku_view_t* view); -void vh_cv_scrl_set_item_count(ku_view_t* view, uint32_t count); +void vh_cv_scrl_set_item_count(ku_view_t* view, size_t count); void vh_cv_scrl_scroll_v(ku_view_t* view, int y); void vh_cv_scrl_scroll_h(ku_view_t* view, int x); @@ -60,20 +60,20 @@ void vh_cv_scrl_attach( ku_view_set_texture_alpha(vh->hori_v, 0.0, 0); ku_view_set_texture_alpha(vh->vert_v, 0.0, 0); - view->handler_data = vh; + view->evt_han_data = vh; } -void vh_cv_scrl_set_item_count(ku_view_t* view, uint32_t count) +void vh_cv_scrl_set_item_count(ku_view_t* view, size_t count) { - vh_cv_scrl_t* vh = view->handler_data; + vh_cv_scrl_t* vh = view->evt_han_data; vh->item_cnt = count; } void vh_cv_scrl_update(ku_view_t* view) { - vh_cv_scrl_t* vh = view->handler_data; - vh_cv_body_t* bvh = vh->tbody_view->handler_data; + vh_cv_scrl_t* vh = view->evt_han_data; + vh_cv_body_t* bvh = vh->tbody_view->evt_han_data; ku_rect_t vf = view->frame.local; ku_rect_t cf = bvh->content->frame.local; @@ -137,7 +137,7 @@ void vh_cv_scrl_update(ku_view_t* view) void vh_cv_scrl_show(ku_view_t* view) { - vh_cv_scrl_t* vh = view->handler_data; + vh_cv_scrl_t* vh = view->evt_han_data; vh->state = 1; vh->steps = 0; @@ -147,15 +147,15 @@ void vh_cv_scrl_show(ku_view_t* view) void vh_cv_scrl_hide(ku_view_t* view) { - vh_cv_scrl_t* vh = view->handler_data; + vh_cv_scrl_t* vh = view->evt_han_data; vh->state = 2; vh->steps = 0; } void vh_cv_scrl_scroll_v(ku_view_t* view, int y) { - /* vh_cv_scrl_t* vh = view->handler_data; */ - /* vh_cv_body_t* bvh = vh->tbody_view->handler_data; */ + /* vh_cv_scrl_t* vh = view->evt_han_data; */ + /* vh_cv_body_t* bvh = vh->tbody_view->evt_han_data; */ /* if (bvh->items->length > 0 && vh->item_cnt > 0) */ /* { */ @@ -186,8 +186,8 @@ void vh_cv_scrl_scroll_v(ku_view_t* view, int y) void vh_cv_scrl_scroll_h(ku_view_t* view, int x) { - /* vh_cv_scrl_t* vh = view->handler_data; */ - /* vh_cv_body_t* bvh = vh->tbody_view->handler_data; */ + /* vh_cv_scrl_t* vh = view->evt_han_data; */ + /* vh_cv_body_t* bvh = vh->tbody_view->evt_han_data; */ /* if (bvh->items->length > 0 && vh->item_cnt > 0) */ /* { */ diff --git a/src/kinetic_ui/handler/vh_drag.c b/src/kinetic_ui/handler/vh_drag.c index 70192cb..d38cc5c 100644 --- a/src/kinetic_ui/handler/vh_drag.c +++ b/src/kinetic_ui/handler/vh_drag.c @@ -33,11 +33,11 @@ void vh_drag_drag(ku_view_t* view, ku_view_t* item); #if __INCLUDE_LEVEL__ == 0 -void vh_drag_evt(ku_view_t* view, ku_event_t ev) +int vh_drag_evt(ku_view_t* view, ku_event_t ev) { if (ev.type == KU_EVENT_MOUSE_MOVE && ev.drag) { - vh_drag_t* vh = view->handler_data; + vh_drag_t* vh = view->evt_han_data; if (vh->dragged_view) { @@ -47,22 +47,26 @@ void vh_drag_evt(ku_view_t* view, ku_event_t ev) ku_view_set_frame(vh->dragged_view, frame); vh_drag_event_t event = {.id = VH_DRAG_MOVE, .vh = vh, .view = view, .dragged_view = vh->dragged_view}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } } if (ev.type == KU_EVENT_MOUSE_UP && ev.drag) { - vh_drag_t* vh = view->handler_data; + vh_drag_t* vh = view->evt_han_data; if (vh->dragged_view) { vh_drag_event_t event = {.id = VH_DRAG_DROP, .vh = vh, .view = view, .dragged_view = vh->dragged_view}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); REL(vh->dragged_view); vh->dragged_view = NULL; } } + + return 1; } void vh_drag_del(void* p) @@ -78,19 +82,18 @@ void vh_drag_desc(void* p, int level) void vh_drag_attach(ku_view_t* view, void (*on_event)(vh_drag_event_t)) { - assert(view->handler == NULL && view->handler_data == NULL); + assert(view->evt_han == NULL && view->evt_han_data == NULL); vh_drag_t* vh = CAL(sizeof(vh_drag_t), vh_drag_del, vh_drag_desc); vh->on_event = on_event; - view->needs_touch = 1; - view->handler_data = vh; - view->handler = vh_drag_evt; + view->evt_han_data = vh; + view->evt_han = vh_drag_evt; } void vh_drag_drag(ku_view_t* view, ku_view_t* item) { - vh_drag_t* vh = view->handler_data; + vh_drag_t* vh = view->evt_han_data; if (vh->dragged_view) { diff --git a/src/kinetic_ui/handler/vh_key.c b/src/kinetic_ui/handler/vh_key.c index 0c955e0..a9a44cf 100644 --- a/src/kinetic_ui/handler/vh_key.c +++ b/src/kinetic_ui/handler/vh_key.c @@ -23,14 +23,17 @@ void vh_key_add(ku_view_t* view, void (*on_event)(vh_key_event_t)); #if __INCLUDE_LEVEL__ == 0 -void vh_key_evt(ku_view_t* view, ku_event_t ev) +int vh_key_evt(ku_view_t* view, ku_event_t ev) { if (ev.type == KU_EVENT_KEY_DOWN) { - vh_key_t* vh = view->handler_data; + vh_key_t* vh = view->evt_han_data; vh_key_event_t event = {.ev = ev, .vh = vh, .view = view}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } + + return 0; } void vh_key_del(void* p) @@ -44,14 +47,13 @@ void vh_key_desc(void* p, int level) void vh_key_add(ku_view_t* view, void (*on_event)(vh_key_event_t)) { - assert(view->handler == NULL && view->handler_data == NULL); + assert(view->evt_han == NULL && view->evt_han_data == NULL); vh_key_t* vh = CAL(sizeof(vh_key_t), vh_key_del, vh_key_desc); vh->on_event = on_event; - view->needs_key = 1; - view->handler_data = vh; - view->handler = vh_key_evt; + view->evt_han_data = vh; + view->evt_han = vh_key_evt; } #endif diff --git a/src/kinetic_ui/handler/vh_knob.c b/src/kinetic_ui/handler/vh_knob.c index a74230a..59f1887 100644 --- a/src/kinetic_ui/handler/vh_knob.c +++ b/src/kinetic_ui/handler/vh_knob.c @@ -21,18 +21,19 @@ typedef struct _vh_knob_t void (*ratio_changed)(ku_view_t* view, float ratio); } vh_knob_t; -void vh_knob_evt(ku_view_t* view, ku_event_t ev) +int vh_knob_evt(ku_view_t* view, ku_event_t ev) { if (ev.type == KU_EVENT_MOUSE_DOWN || (ev.type == KU_EVENT_MOUSE_MOVE && ev.drag)) { - vh_knob_t* vh = view->handler_data; + vh_knob_t* vh = view->evt_han_data; float dx = ev.x - (view->frame.global.x + view->frame.global.w / 2.0); float dy = ev.y - (view->frame.global.y + view->frame.global.h / 2.0); float angle = atan2(dy, dx); float r = sqrt(dx * dx + dy * dy); - if (angle < 0) angle += 6.28; + if (angle < 0) + angle += 6.28; if (r < view->frame.global.w / 2.0) { @@ -42,20 +43,26 @@ void vh_knob_evt(ku_view_t* view, ku_event_t ev) } else if (ev.type == KU_EVENT_SCROLL) { - vh_knob_t* vh = view->handler_data; + vh_knob_t* vh = view->evt_han_data; tg_knob_t* tg = view->tex_gen_data; float angle = tg->angle - ev.dy / 50.0; - if (angle < 0) angle += 6.28; + if (angle < 0) + angle += 6.28; - if (tg->angle < 3 * 3.14 / 2 && tg->angle > 3.14 && angle > 3 * 3.14 / 2) angle = tg->angle; - if (tg->angle > 3 * 3.14 / 2 && tg->angle < 2 * 3.14 && angle < 3 * 3.14 / 2) angle = tg->angle; + if (tg->angle < 3 * 3.14 / 2 && tg->angle > 3.14 && angle > 3 * 3.14 / 2) + angle = tg->angle; + if (tg->angle > 3 * 3.14 / 2 && tg->angle < 2 * 3.14 && angle < 3 * 3.14 / 2) + angle = tg->angle; - if (angle > 6.28) angle -= 6.28; + if (angle > 6.28) + angle -= 6.28; tg_knob_set_angle(view, angle); (*vh->ratio_changed)(view, angle); } + + return 1; } void vh_knob_desc(void* p, int level) @@ -65,14 +72,14 @@ void vh_knob_desc(void* p, int level) void vh_knob_add(ku_view_t* view, void (*ratio_changed)(ku_view_t* view, float ratio)) { - assert(view->handler == NULL && view->handler_data == NULL); + assert(view->evt_han == NULL && view->evt_han_data == NULL); vh_knob_t* vh = CAL(sizeof(vh_knob_t), NULL, vh_knob_desc); vh->ratio_changed = ratio_changed; - view->handler_data = vh; - view->handler = vh_knob_evt; + view->evt_han_data = vh; + view->evt_han = vh_knob_evt; } #endif diff --git a/src/kinetic_ui/handler/vh_roll.c b/src/kinetic_ui/handler/vh_roll.c index c5f471e..ae43082 100644 --- a/src/kinetic_ui/handler/vh_roll.c +++ b/src/kinetic_ui/handler/vh_roll.c @@ -31,11 +31,11 @@ void vh_roll_add(ku_view_t* view, void (*on_event)(vh_roll_event_t)); #if __INCLUDE_LEVEL__ == 0 -void vh_roll_evt(ku_view_t* view, ku_event_t ev) +int vh_roll_evt(ku_view_t* view, ku_event_t ev) { if (ev.type == KU_EVENT_MOUSE_MOVE) { - vh_roll_t* vh = view->handler_data; + vh_roll_t* vh = view->evt_han_data; ku_rect_t frame = view->frame.global; if (!vh->active) @@ -48,13 +48,14 @@ void vh_roll_evt(ku_view_t* view, ku_event_t ev) vh->active = 1; vh_roll_event_t event = {.id = VH_ROLL_IN, .view = view, .vh = vh}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } } } else if (ev.type == KU_EVENT_MOUSE_MOVE_OUT) { - vh_roll_t* vh = view->handler_data; + vh_roll_t* vh = view->evt_han_data; ku_rect_t frame = view->frame.global; if (vh->active) @@ -66,10 +67,13 @@ void vh_roll_evt(ku_view_t* view, ku_event_t ev) { vh->active = 0; vh_roll_event_t event = {.id = VH_ROLL_OUT, .view = view, .vh = vh}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } } } + + return 1; } void vh_roll_del(void* p) @@ -83,13 +87,13 @@ void vh_roll_desc(void* p, int level) void vh_roll_add(ku_view_t* view, void (*on_event)(vh_roll_event_t)) { - assert(view->handler == NULL && view->handler_data == NULL); + assert(view->evt_han == NULL && view->evt_han_data == NULL); vh_roll_t* vh = CAL(sizeof(vh_roll_t), vh_roll_del, vh_roll_desc); vh->on_event = on_event; - view->handler_data = vh; - view->handler = vh_roll_evt; + view->evt_han_data = vh; + view->evt_han = vh_roll_evt; } #endif diff --git a/src/kinetic_ui/handler/vh_slider.c b/src/kinetic_ui/handler/vh_slider.c index 13c2e7b..9fbdc52 100644 --- a/src/kinetic_ui/handler/vh_slider.c +++ b/src/kinetic_ui/handler/vh_slider.c @@ -31,9 +31,9 @@ void vh_slider_set_enabled(ku_view_t* view, int flag); #include "vh_anim.c" #include -void vh_slider_evt(ku_view_t* view, ku_event_t ev) +int vh_slider_evt(ku_view_t* view, ku_event_t ev) { - vh_slider_t* vh = view->handler_data; + vh_slider_t* vh = view->evt_han_data; if (vh->enabled) { @@ -48,14 +48,17 @@ void vh_slider_evt(ku_view_t* view, ku_event_t ev) ku_view_set_frame(bar, frame); vh_slider_event_t event = {.view = view, .ratio = vh->ratio, .vh = vh}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } else if (ev.type == KU_EVENT_SCROLL) { float ratio = vh->ratio - ev.dx / 50.0; - if (ratio < 0) ratio = 0; - if (ratio > 1) ratio = 1; + if (ratio < 0) + ratio = 0; + if (ratio > 1) + ratio = 1; vh->ratio = ratio; ku_view_t* bar = view->views->data[0]; @@ -64,14 +67,17 @@ void vh_slider_evt(ku_view_t* view, ku_event_t ev) ku_view_set_frame(bar, frame); vh_slider_event_t event = {.view = view, .ratio = vh->ratio, .vh = vh}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } } + + return 0; } void vh_slider_set(ku_view_t* view, float ratio) { - vh_slider_t* vh = view->handler_data; + vh_slider_t* vh = view->evt_han_data; vh->ratio = ratio; ku_view_t* bar = view->views->data[0]; ku_rect_t frame = bar->frame.local; @@ -81,7 +87,7 @@ void vh_slider_set(ku_view_t* view, float ratio) float vh_slider_get_ratio(ku_view_t* view) { - vh_slider_t* vh = view->handler_data; + vh_slider_t* vh = view->evt_han_data; return vh->ratio; } @@ -96,14 +102,14 @@ void vh_slider_del(void* p) void vh_slider_add(ku_view_t* view, void (*on_event)(vh_slider_event_t)) { - assert(view->handler == NULL && view->handler_data == NULL); + assert(view->evt_han == NULL && view->evt_han_data == NULL); vh_slider_t* vh = CAL(sizeof(vh_slider_t), vh_slider_del, vh_slider_desc); vh->on_event = on_event; vh->enabled = 1; - view->handler_data = vh; - view->handler = vh_slider_evt; + view->evt_han_data = vh; + view->evt_han = vh_slider_evt; ku_view_t* bar = view->views->data[0]; vh_anim_add(bar, NULL, NULL); @@ -111,7 +117,7 @@ void vh_slider_add(ku_view_t* view, void (*on_event)(vh_slider_event_t)) void vh_slider_set_enabled(ku_view_t* view, int flag) { - vh_slider_t* vh = view->handler_data; + vh_slider_t* vh = view->evt_han_data; if (flag) { diff --git a/src/kinetic_ui/handler/vh_table.c b/src/kinetic_ui/handler/vh_table.c index a8226a5..354f3af 100644 --- a/src/kinetic_ui/handler/vh_table.c +++ b/src/kinetic_ui/handler/vh_table.c @@ -37,8 +37,9 @@ typedef struct _vh_table_event_t struct _vh_table_t { char* id; // unique id for item generation - uint32_t cnt; // item count for item generation + size_t cnt; // item count for item generation ku_view_t* view; + int show_selected; // use selected style for selected views mt_vector_t* items; // data items mt_vector_t* cache; // item cache @@ -78,6 +79,10 @@ void vh_table_show_scrollbar( ku_view_t* view, int flag); +void vh_table_show_selected( + ku_view_t* view, + int flag); + mt_vector_t* vh_table_get_fields( ku_view_t* view); @@ -104,17 +109,17 @@ mt_vector_t* vh_table_get_fields( void vh_table_head_update_cells(vh_table_t* vh, int fixed_index, int fixed_pos) { float scale = vh->body_v->style.scale; - for (int ri = 0; ri < vh->body_v->views->length; ri++) + for (size_t ri = 0; ri < vh->body_v->views->length; ri++) { ku_view_t* rowview = vh->body_v->views->data[ri]; float wth = 0; - for (int ci = 0; ci < rowview->views->length; ci++) + for (size_t ci = 0; ci < rowview->views->length; ci++) { ku_view_t* cellview = rowview->views->data[ci]; ku_rect_t frame = cellview->frame.local; mt_number_t* sizep = vh->fields->data[ci * 2 + 1]; - frame.x = ci == fixed_index ? (float) fixed_pos : wth; + frame.x = (int32_t) ci == fixed_index ? (float) fixed_pos : wth; frame.w = (float) sizep->intv * scale; ku_view_set_frame(cellview, frame); wth += (frame.w + 2) * scale; @@ -183,7 +188,7 @@ void vh_table_head_reorder(ku_view_t* hview, int ind1, int ind2, void* userdata) vh->fields->data[ind2 * 2] = field1; vh->fields->data[ind2 * 2 + 1] = size1; - for (int ri = 0; ri < vh->body_v->views->length; ri++) + for (size_t ri = 0; ri < vh->body_v->views->length; ri++) { ku_view_t* rowview = vh->body_v->views->data[ri]; @@ -227,7 +232,7 @@ ku_view_t* vh_table_head_create( /* create header fields/cells */ - for (int i = 0; i < vh->fields->length; i += 2) + for (size_t i = 0; i < vh->fields->length; i += 2) { char* field = vh->fields->data[i]; mt_number_t* size = vh->fields->data[i + 1]; @@ -269,7 +274,7 @@ ku_view_t* vh_table_item_create( if (vh->items) { - if (index > -1 && index < vh->items->length) + if (index > -1 && index < (int32_t) vh->items->length) { mt_map_t* data = vh->items->data[index]; @@ -277,7 +282,7 @@ ku_view_t* vh_table_item_create( { /* get cached item */ rowview = RET(vh->cache->data[0]); - mt_vector_rem_at_index(vh->cache, 0); + mt_vector_rem_index(vh->cache, 0); } else { @@ -291,7 +296,7 @@ ku_view_t* vh_table_item_create( /* create cells */ int wth = 0; - for (int i = 0; i < vh->fields->length; i += 2) + for (size_t i = 0; i < vh->fields->length; i += 2) { char* field = vh->fields->data[i]; mt_number_t* size = vh->fields->data[i + 1]; @@ -314,16 +319,17 @@ ku_view_t* vh_table_item_create( /* select style */ textstyle_t style = index % 2 == 0 ? vh->rowastyle : vh->rowbstyle; - if (vh->selected_items->length > 0) + if (vh->selected_items->length > 0 && vh->show_selected) { - uint32_t pos = mt_vector_index_of_data(vh->selected_items, data); - if (pos < UINT32_MAX) style = vh->rowsstyle; + size_t pos = mt_vector_index_of_data(vh->selected_items, data); + if (pos < SIZE_MAX) + style = vh->rowsstyle; } /* update cells */ int wth = 0; - for (int i = 0; i < vh->fields->length; i += 2) + for (size_t i = 0; i < vh->fields->length; i += 2) { char* field = vh->fields->data[i]; mt_number_t* size = vh->fields->data[i + 1]; @@ -375,26 +381,26 @@ void vh_table_evnt_event(vh_tbl_evnt_event_t event) mt_map_t* data = vh->items->data[event.index]; - uint32_t pos = mt_vector_index_of_data(vh->selected_items, data); + size_t pos = mt_vector_index_of_data(vh->selected_items, data); // TODO this is duplicated in CONTEXT event, simplify - if (pos == UINT32_MAX) + if (pos == SIZE_MAX) { /* reset selected if control is not down */ if (!event.ev.ctrl_down) { mt_vector_reset(vh->selected_items); - vh_tbl_body_t* bvh = vh->body_v->handler_data; + vh_tbl_body_t* bvh = vh->body_v->evt_han_data; - for (int index = 0; index < bvh->items->length; index++) + for (size_t index = 0; index < bvh->items->length; index++) { ku_view_t* item = bvh->items->data[index]; textstyle_t style = index % 2 == 0 ? vh->rowastyle : vh->rowbstyle; - for (int i = 0; i < item->views->length; i++) + for (size_t i = 0; i < item->views->length; i++) { ku_view_t* cellview = item->views->data[i]; tg_text_set_style(cellview, style); @@ -404,10 +410,13 @@ void vh_table_evnt_event(vh_tbl_evnt_event_t event) VADD(vh->selected_items, data); - for (int i = 0; i < event.rowview->views->length; i++) + if (vh->show_selected) { - ku_view_t* cellview = event.rowview->views->data[i]; - tg_text_set_style(cellview, vh->rowsstyle); + for (size_t i = 0; i < event.rowview->views->length; i++) + { + ku_view_t* cellview = event.rowview->views->data[i]; + tg_text_set_style(cellview, vh->rowsstyle); + } } } else @@ -416,7 +425,7 @@ void vh_table_evnt_event(vh_tbl_evnt_event_t event) textstyle_t style = event.index % 2 == 0 ? vh->rowastyle : vh->rowbstyle; - for (int i = 0; i < event.rowview->views->length; i++) + for (size_t i = 0; i < event.rowview->views->length; i++) { ku_view_t* cellview = event.rowview->views->data[i]; tg_text_set_style(cellview, style); @@ -436,32 +445,32 @@ void vh_table_evnt_event(vh_tbl_evnt_event_t event) } else if (event.id == VH_TBL_EVENT_CONTEXT) { - if (event.index > -1) + if (event.index > -1 && vh->selected_index != event.index) { vh->selected_index = event.index; mt_map_t* data = vh->items->data[event.index]; - uint32_t pos = mt_vector_index_of_data(vh->selected_items, data); + size_t pos = mt_vector_index_of_data(vh->selected_items, data); // TODO this is duplicated in CONTEXT event, simplify - if (pos == UINT32_MAX) + if (pos == SIZE_MAX) { /* reset selected if control is not down */ if (!event.ev.ctrl_down) { mt_vector_reset(vh->selected_items); - vh_tbl_body_t* bvh = vh->body_v->handler_data; + vh_tbl_body_t* bvh = vh->body_v->evt_han_data; - for (int index = 0; index < bvh->items->length; index++) + for (size_t index = 0; index < bvh->items->length; index++) { ku_view_t* item = bvh->items->data[index]; textstyle_t style = index % 2 == 0 ? vh->rowastyle : vh->rowbstyle; - for (int i = 0; i < item->views->length; i++) + for (size_t i = 0; i < item->views->length; i++) { ku_view_t* cellview = item->views->data[i]; tg_text_set_style(cellview, style); @@ -471,10 +480,13 @@ void vh_table_evnt_event(vh_tbl_evnt_event_t event) VADD(vh->selected_items, data); - for (int i = 0; i < event.rowview->views->length; i++) + if (vh->show_selected) { - ku_view_t* cellview = event.rowview->views->data[i]; - tg_text_set_style(cellview, vh->rowsstyle); + for (size_t i = 0; i < event.rowview->views->length; i++) + { + ku_view_t* cellview = event.rowview->views->data[i]; + tg_text_set_style(cellview, vh->rowsstyle); + } } } else @@ -483,7 +495,7 @@ void vh_table_evnt_event(vh_tbl_evnt_event_t event) textstyle_t style = event.index % 2 == 0 ? vh->rowastyle : vh->rowbstyle; - for (int i = 0; i < event.rowview->views->length; i++) + for (size_t i = 0; i < event.rowview->views->length; i++) { ku_view_t* cellview = event.rowview->views->data[i]; tg_text_set_style(cellview, style); @@ -548,8 +560,10 @@ void vh_table_evnt_event(vh_tbl_evnt_event_t event) if (event.ev.keycode == XKB_KEY_Down || event.ev.keycode == XKB_KEY_Up) { - if (event.ev.keycode == XKB_KEY_Down) vh_table_select(vh->view, vh->selected_index + 1, event.ev.shift_down); - if (event.ev.keycode == XKB_KEY_Up) vh_table_select(vh->view, vh->selected_index - 1, event.ev.shift_down); + if (event.ev.keycode == XKB_KEY_Down) + vh_table_select(vh->view, vh->selected_index + 1, event.ev.shift_down); + if (event.ev.keycode == XKB_KEY_Up) + vh_table_select(vh->view, vh->selected_index - 1, event.ev.shift_down); tevent = (vh_table_event_t){ .table = vh, @@ -687,7 +701,8 @@ void vh_table_attach( ku_view_t* rowb = view->views->data[3]; ku_view_t* rows = view->views->data[4]; ku_view_t* headrow = NULL; - if (view->views->length == 6) headrow = view->views->data[5]; + if (view->views->length == 6) + headrow = view->views->data[5]; /* extract needed styles */ textstyle_t rowastyle = ku_gen_textstyle_parse(rowa); @@ -711,7 +726,8 @@ void vh_table_attach( ku_view_t* scrl_v = ku_view_new(scrlid, rect); ku_view_t* evnt_v = ku_view_new(evntid, rect); ku_view_t* head_v = NULL; - if (view->views->length == 6) head_v = ku_view_new(headid, headrow->frame.local); + if (view->views->length == 6) + head_v = ku_view_new(headid, headrow->frame.local); REL(headid); REL(layrid); @@ -736,17 +752,20 @@ void vh_table_attach( /* set mask */ layr_v->style.masked = 1; - if (head_v) head_v->style.masked = 1; + if (head_v) + head_v->style.masked = 1; /* set scale */ layr_v->style.scale = scale; body_v->style.scale = scale; scrl_v->style.scale = scale; evnt_v->style.scale = scale; - if (head_v) head_v->style.scale = scale; + if (head_v) + head_v->style.scale = scale; /* attach header view as first view */ - if (head_v) ku_view_add_subview(view, head_v); + if (head_v) + ku_view_add_subview(view, head_v); /* attach layers view as first or second view */ ku_view_add_subview(view, layr_v); @@ -769,7 +788,8 @@ void vh_table_attach( ku_view_remove_from_parent(rowa); ku_view_remove_from_parent(rowb); ku_view_remove_from_parent(rows); - if (headrow) ku_view_remove_from_parent(headrow); + if (headrow) + ku_view_remove_from_parent(headrow); vh_table_t* vh = CAL(sizeof(vh_table_t), vh_table_del, vh_table_desc); vh->id = mt_string_new_cstring(view->id); @@ -778,8 +798,9 @@ void vh_table_attach( vh->selected_items = VNEW(); vh->on_event = on_event; vh->view = view; + vh->show_selected = 1; - view->handler_data = vh; + view->evt_han_data = vh; vh->layr_v = layr_v; vh->body_v = body_v; @@ -787,10 +808,14 @@ void vh_table_attach( vh->scrl_v = scrl_v; vh->head_v = head_v; - if (rowastyle.line_height == 0) rowastyle.line_height = 20; - if (rowbstyle.line_height == 0) rowbstyle.line_height = 20; - if (rowsstyle.line_height == 0) rowsstyle.line_height = 20; - if (headstyle.line_height == 0) headstyle.line_height = 20; + if (rowastyle.line_height == 0) + rowastyle.line_height = 20; + if (rowbstyle.line_height == 0) + rowbstyle.line_height = 20; + if (rowsstyle.line_height == 0) + rowsstyle.line_height = 20; + if (headstyle.line_height == 0) + headstyle.line_height = 20; vh->rowastyle = rowastyle; vh->rowbstyle = rowbstyle; @@ -838,15 +863,16 @@ void vh_table_set_data( ku_view_t* view, mt_vector_t* data) { - vh_table_t* vh = view->handler_data; + vh_table_t* vh = view->evt_han_data; - if (vh->items) REL(vh->items); + if (vh->items) + REL(vh->items); vh->items = RET(data); vh->selected_index = 0; mt_vector_reset(vh->selected_items); - if (vh->selected_index < vh->items->length) + if (vh->selected_index < (int32_t) vh->items->length) { mt_map_t* sel = vh->items->data[vh->selected_index]; VADD(vh->selected_items, sel); @@ -855,7 +881,8 @@ void vh_table_set_data( vh_tbl_body_reset(vh->body_v); vh_tbl_body_move(vh->body_v, 0, 0); - if (vh->scrl_v) vh_tbl_scrl_set_item_count(vh->scrl_v, data->length); + if (vh->scrl_v) + vh_tbl_scrl_set_item_count(vh->scrl_v, data->length); } /* select index */ @@ -865,14 +892,16 @@ void vh_table_select( int32_t index, int add) { - vh_table_t* vh = view->handler_data; - vh_tbl_body_t* bvh = vh->body_v->handler_data; + vh_table_t* vh = view->evt_han_data; + vh_tbl_body_t* bvh = vh->body_v->evt_han_data; float scale = view->style.scale; vh->selected_index = index; - if (vh->selected_index < 0) vh->selected_index = 0; - if (vh->selected_index > vh->items->length - 1) vh->selected_index = vh->items->length - 1; + if (vh->selected_index < 0) + vh->selected_index = 0; + if (vh->selected_index > (int32_t) vh->items->length - 1) + vh->selected_index = (int32_t) vh->items->length - 1; if (bvh->bot_index < vh->selected_index) { @@ -897,22 +926,24 @@ void vh_table_select( vh_tbl_body_vjump(vh->body_v, vh->selected_index - 1, 1); } - if (add == 0) mt_vector_reset(vh->selected_items); + if (add == 0) + mt_vector_reset(vh->selected_items); mt_map_t* sel = vh->items->data[vh->selected_index]; VADD(vh->selected_items, sel); /* color item */ - for (int i = 0; i < bvh->items->length; i++) + for (size_t i = 0; i < bvh->items->length; i++) { int realindex = bvh->head_index + i; mt_map_t* data = vh->items->data[realindex]; ku_view_t* item = bvh->items->data[i]; textstyle_t style = realindex % 2 == 0 ? vh->rowastyle : vh->rowbstyle; - if (mt_vector_index_of_data(vh->selected_items, data) < UINT32_MAX) style = vh->rowsstyle; + if (mt_vector_index_of_data(vh->selected_items, data) < SIZE_MAX) + style = vh->rowsstyle; - for (int i = 0; i < item->views->length; i++) + for (size_t i = 0; i < item->views->length; i++) { ku_view_t* cellview = item->views->data[i]; cellview->style.scale = scale; @@ -920,12 +951,13 @@ void vh_table_select( } } - if (vh->scrl_v) vh_tbl_scrl_update(vh->scrl_v); + if (vh->scrl_v) + vh_tbl_scrl_update(vh->scrl_v); } mt_vector_t* vh_table_get_fields(ku_view_t* view) { - vh_table_t* vh = view->handler_data; + vh_table_t* vh = view->evt_han_data; return vh->fields; } @@ -933,8 +965,17 @@ void vh_table_show_scrollbar( ku_view_t* view, int flag) { - vh_table_t* vh = view->handler_data; - if (vh->scrl_v) vh_tbl_scrl_enable(vh->scrl_v, flag); + vh_table_t* vh = view->evt_han_data; + if (vh->scrl_v) + vh_tbl_scrl_enable(vh->scrl_v, flag); +} + +void vh_table_show_selected( + ku_view_t* view, + int flag) +{ + vh_table_t* vh = view->evt_han_data; + vh->show_selected = flag; } #endif diff --git a/src/kinetic_ui/handler/vh_tbl_body.c b/src/kinetic_ui/handler/vh_tbl_body.c index b01722b..c402b61 100644 --- a/src/kinetic_ui/handler/vh_tbl_body.c +++ b/src/kinetic_ui/handler/vh_tbl_body.c @@ -12,21 +12,21 @@ typedef struct _vh_tbl_body_t float head_xpos; // horizontal position of head - int full; // list is full, no more elements needed - int head_index; // index of upper element - int tail_index; // index of lower element + int full; // list is full, no more elements needed + int32_t head_index; // index of upper element + int32_t tail_index; // index of lower element - int top_index; // index of visible top element - int bot_index; // index of visible bottom element + int32_t top_index; // index of visible top element + int32_t bot_index; // index of visible bottom element - ku_view_t* (*item_create)(ku_view_t* tview, int index, void* userdata); + ku_view_t* (*item_create)(ku_view_t* tview, int32_t index, void* userdata); void (*item_recycle)(ku_view_t* tview, ku_view_t* item, void* userdata); } vh_tbl_body_t; void vh_tbl_body_attach( ku_view_t* view, - ku_view_t* (*item_create)(ku_view_t* tview, int index, void* userdata), + ku_view_t* (*item_create)(ku_view_t* tview, int32_t index, void* userdata), void (*item_recycle)(ku_view_t* tview, ku_view_t* item, void* userdata), void* userdata); @@ -44,10 +44,10 @@ void vh_tbl_body_hjump( void vh_tbl_body_vjump( ku_view_t* view, - int topindex, + int32_t topindex, int aligntop); -ku_view_t* vh_tbl_body_item_for_index(ku_view_t* view, int index); +ku_view_t* vh_tbl_body_item_for_index(ku_view_t* view, int32_t index); #endif @@ -61,7 +61,7 @@ void vh_tbl_body_del(void* p) // remove items - for (int index = 0; + for (size_t index = 0; index < vh->items->length; index++) { @@ -79,11 +79,11 @@ void vh_tbl_body_desc(void* p, int level) void vh_tbl_body_attach( ku_view_t* view, - ku_view_t* (*item_create)(ku_view_t* tview, int index, void* userdata), + ku_view_t* (*item_create)(ku_view_t* tview, int32_t index, void* userdata), void (*item_recycle)(ku_view_t* tview, ku_view_t* item, void* userdata), void* userdata) { - assert(view->handler == NULL && view->handler_data == NULL); + assert(view->evt_han == NULL && view->evt_han_data == NULL); vh_tbl_body_t* vh = CAL(sizeof(vh_tbl_body_t), vh_tbl_body_del, vh_tbl_body_desc); vh->userdata = userdata; @@ -91,7 +91,7 @@ void vh_tbl_body_attach( vh->item_create = item_create; vh->item_recycle = item_recycle; - view->handler_data = vh; + view->evt_han_data = vh; } void vh_tbl_body_move( @@ -99,14 +99,14 @@ void vh_tbl_body_move( float dx, float dy) { - vh_tbl_body_t* vh = view->handler_data; + vh_tbl_body_t* vh = view->evt_han_data; vh->full = 0; // repos items vh->head_xpos += dx; - for (int index = 0; + for (size_t index = 0; index < vh->items->length; index++) { @@ -116,7 +116,8 @@ void vh_tbl_body_move( frame.x = vh->head_xpos; frame.y += dy; - if (frame.w < view->frame.local.w) frame.w = view->frame.local.w; + if (frame.w < view->frame.local.w) + frame.w = view->frame.local.w; ku_view_set_frame(iview, frame); } @@ -222,7 +223,8 @@ void vh_tbl_body_move( { vh->head_index += 1; ku_view_remove_from_parent(head); - if (vh->item_recycle) (*vh->item_recycle)(view, head, vh->userdata); + if (vh->item_recycle) + (*vh->item_recycle)(view, head, vh->userdata); VREM(vh->items, head); } @@ -232,7 +234,8 @@ void vh_tbl_body_move( { vh->tail_index -= 1; ku_view_remove_from_parent(tail); - if (vh->item_recycle) (*vh->item_recycle)(view, tail, vh->userdata); + if (vh->item_recycle) + (*vh->item_recycle)(view, tail, vh->userdata); VREM(vh->items, tail); } } @@ -242,29 +245,32 @@ void vh_tbl_body_move( // get top and bot indexes vh->top_index = vh->head_index; - for (int index = 0; + for (size_t index = 0; index < vh->items->length; index++) { ku_view_t* iview = vh->items->data[index]; ku_rect_t frame = iview->frame.local; - if (frame.y < 0) vh->top_index = vh->head_index + index; - if (frame.y < view->frame.local.h) vh->bot_index = vh->head_index + index; + if (frame.y < 0) + vh->top_index = vh->head_index + index; + if (frame.y < view->frame.local.h) + vh->bot_index = vh->head_index + index; } } void vh_tbl_body_reset( ku_view_t* view) { - vh_tbl_body_t* vh = view->handler_data; + vh_tbl_body_t* vh = view->evt_han_data; - for (int index = 0; + for (size_t index = 0; index < vh->items->length; index++) { ku_view_t* iview = vh->items->data[index]; - if (vh->item_recycle) (*vh->item_recycle)(view, iview, vh->userdata); + if (vh->item_recycle) + (*vh->item_recycle)(view, iview, vh->userdata); ku_view_remove_from_parent(iview); } @@ -280,11 +286,11 @@ void vh_tbl_body_hjump( ku_view_t* view, float x) { - vh_tbl_body_t* vh = view->handler_data; + vh_tbl_body_t* vh = view->evt_han_data; vh->head_xpos = x; - for (int index = 0; + for (size_t index = 0; index < vh->items->length; index++) { @@ -297,21 +303,22 @@ void vh_tbl_body_hjump( void vh_tbl_body_vjump( ku_view_t* view, - int topindex, + int32_t topindex, int aligntop) { - vh_tbl_body_t* vh = view->handler_data; + vh_tbl_body_t* vh = view->evt_han_data; // invalidate items int count = vh->bot_index - vh->top_index + 1; - for (int index = 0; + for (size_t index = 0; index < vh->items->length; index++) { ku_view_t* iview = vh->items->data[index]; - if (vh->item_recycle) (*vh->item_recycle)(view, iview, vh->userdata); + if (vh->item_recycle) + (*vh->item_recycle)(view, iview, vh->userdata); ku_view_remove_from_parent(iview); } @@ -324,7 +331,8 @@ void vh_tbl_body_vjump( topindex -= count; int margin = vh->tail_index - vh->bot_index; topindex += margin; - if (topindex < 0) topindex = 0; + if (topindex < 0) + topindex = 0; vh->head_index = topindex; vh->tail_index = topindex; vh->top_index = topindex; @@ -334,7 +342,8 @@ void vh_tbl_body_vjump( { int margin = vh->top_index - vh->head_index; topindex -= margin; - if (topindex < 0) topindex = 0; + if (topindex < 0) + topindex = 0; vh->head_index = topindex; vh->tail_index = topindex; vh->top_index = topindex; @@ -344,12 +353,14 @@ void vh_tbl_body_vjump( vh_tbl_body_move(view, 0, 0); } -ku_view_t* vh_tbl_body_item_for_index(ku_view_t* view, int index) +ku_view_t* vh_tbl_body_item_for_index(ku_view_t* view, int32_t index) { - vh_tbl_body_t* vh = view->handler_data; + vh_tbl_body_t* vh = view->evt_han_data; - if (index < vh->head_index || index > vh->tail_index) return NULL; - if (vh->head_index + index > vh->items->length - 1) return NULL; + if (index < vh->head_index || index > vh->tail_index) + return NULL; + if (vh->head_index + index > (int32_t) vh->items->length - 1) + return NULL; return vh->items->data[vh->head_index + index]; } diff --git a/src/kinetic_ui/handler/vh_tbl_evnt.c b/src/kinetic_ui/handler/vh_tbl_evnt.c index 86534e4..82ad8af 100644 --- a/src/kinetic_ui/handler/vh_tbl_evnt.c +++ b/src/kinetic_ui/handler/vh_tbl_evnt.c @@ -2,6 +2,7 @@ #define vh_tbl_evnt_h #include "ku_view.c" +#include "mt_log.c" #include "vh_tbl_body.c" #include "vh_tbl_head.c" #include "vh_tbl_scrl.c" @@ -37,14 +38,12 @@ struct _vh_tbl_evnt_t ku_view_t* tscrl_view; ku_view_t* thead_view; void* userdata; - int scroll_drag; - int scroll_visible; ku_view_t* selected_item; int selected_index; + int inertia_x; + int inertia_y; float sx; float sy; - float scroll_drag_x; - float scroll_drag_y; void (*on_event)(vh_tbl_evnt_event_t event); }; @@ -62,141 +61,179 @@ void vh_tbl_evnt_attach( #define SCROLLBAR 20.0 -void vh_tbl_evnt_evt(ku_view_t* view, ku_event_t ev) +float linear_slowdown(float speed, float pos, float max) { - vh_tbl_evnt_t* vh = view->handler_data; - - if (ev.type == KU_EVENT_FRAME) + if (speed > 0.0) { - vh_tbl_body_t* bvh = vh->tbody_view->handler_data; + pos = pos > max ? max : pos; + speed *= 1.0 - pos / max; + } - if (bvh->items && bvh->items->length > 0) - { - if (vh->sy > 0.0001 || vh->sy < -0.0001 || vh->sx > 0.0001 || vh->sx < -0.0001) - { - ku_view_t* head = mt_vector_head(bvh->items); - ku_view_t* tail = mt_vector_tail(bvh->items); + return speed; +} + +void vh_tbl_evnt_move(ku_view_t* view) +{ + vh_tbl_evnt_t* vh = view->evt_han_data; + vh_tbl_body_t* bvh = vh->tbody_view->evt_han_data; + + if (bvh->items && bvh->items->length > 0) + { + /* if (vh->sy > 0.0001 || vh->sy < -0.0001 || vh->sx > 0.0001 || vh->sx < -0.0001) */ + /* { */ + ku_view_t* head = mt_vector_head(bvh->items); + ku_view_t* tail = mt_vector_tail(bvh->items); - float top = head->frame.local.y; - float bot = tail->frame.local.y + tail->frame.local.h; - float hth = bot - top; - float wth = head->frame.local.w; - float lft = head->frame.local.x; - float rgt = head->frame.local.x + head->frame.local.w; + float top = head->frame.local.y; + float bot = tail->frame.local.y + tail->frame.local.h; + float hth = bot - top; + float wth = head->frame.local.w; + float lft = head->frame.local.x; + float rgt = head->frame.local.x + head->frame.local.w; - vh->sx *= 0.8; - vh->sy *= 0.8; + float dx = vh->sx; + float dy = vh->sy; - float dx = vh->sx; - float dy = vh->sy; + float top_gap = 0.001; + float bot_gap = view->frame.local.h - SCROLLBAR - 0.001; - if (top > 0.001) dy -= top; // scroll back top item - else if (bot < view->frame.local.h - 0.001 - SCROLLBAR) + if (top > top_gap) + { + vh->sy = linear_slowdown(vh->sy, top, 200.0); + dy = vh->sy; + dy += -top / 8.0; + } + else if (bot < bot_gap) + { + if (dy < 0.0) + { + if (hth > bot_gap) // scroll back to bottom item { - if (hth > view->frame.local.h - 0.0001 - SCROLLBAR) dy += (view->frame.local.h - SCROLLBAR) - bot; // scroll back bottom item - else dy -= top; // scroll back top item + vh->sy = -1 * linear_slowdown(-vh->sy, view->frame.local.h - bot, 200.0); + dy = vh->sy; + dy += (bot_gap - bot) / 8.0; } - - if (lft > 0.001) dx -= lft; - else if (rgt < view->frame.local.w - 0.001 - SCROLLBAR) + else // scroll back to top item { - if (wth > view->frame.local.w - 0.001 - SCROLLBAR) dx += (view->frame.local.w - SCROLLBAR) - rgt; - else dx -= lft; + vh->sy = -1 * linear_slowdown(-vh->sy, -top, 200.0); + dy = vh->sy; + dy += -top / 8.0; } - - vh_tbl_body_move(vh->tbody_view, dx, dy); - - if (vh->thead_view) vh_tbl_head_move(vh->thead_view, dx); - if (vh->tscrl_view && vh->scroll_visible == 1) vh_tbl_scrl_update(vh->tscrl_view); } + } + + float lft_gap = 0.001; + float rgt_gap = view->frame.local.w - SCROLLBAR - 0.001; - if (vh->tscrl_view) + if (lft > lft_gap) + { + vh->sx = linear_slowdown(vh->sx, lft, 200.0); + dx = vh->sx; + dx += -lft / 8.0; + } + else if (rgt < rgt_gap) + { + if (dx < 0) { - vh_tbl_scrl_t* svh = vh->tscrl_view->handler_data; - if (svh->state > 0) vh_tbl_scrl_update(vh->tscrl_view); + if (wth > rgt_gap) // scroll back to right edge + { + vh->sx = -1 * linear_slowdown(-vh->sx, view->frame.local.w - rgt, 200.0); + dx = vh->sx; + dx += (rgt_gap - rgt) / 8.0; + } + else // scroll back to left edge + { + vh->sx = -1 * linear_slowdown(-vh->sx, -lft, 200.0); + dx = vh->sx; + dx += -lft / 8.0; + } } } + + vh_tbl_body_move(vh->tbody_view, dx, dy); + + if (vh->thead_view) + vh_tbl_head_move(vh->thead_view, dx); + if (vh->tscrl_view) + vh_tbl_scrl_update(vh->tscrl_view); + //} + + if (vh->tscrl_view) + { + vh_tbl_scrl_t* svh = vh->tscrl_view->evt_han_data; + if (svh->state > 0) + vh_tbl_scrl_update(vh->tscrl_view); + } + } +} + +int vh_tbl_evnt_evt(ku_view_t* view, ku_event_t ev) +{ + vh_tbl_evnt_t* vh = view->evt_han_data; + + if (ev.type == KU_EVENT_FRAME) + { + if (vh->inertia_x || vh->inertia_y) + { + vh->sx *= 0.99; + vh->sy *= 0.99; + vh_tbl_evnt_move(view); + } } else if (ev.type == KU_EVENT_SCROLL) { - vh->sx -= ev.dx; - vh->sy += ev.dy; + vh->sx = -ev.dx; + vh->sy = ev.dy; + vh_tbl_evnt_move(view); // cause dirty rect which causes frame events to flow for later animation vh->tbody_view->frame.dim_changed = 1; } + else if (ev.type == KU_EVENT_SCROLL_X_END) + { + if (fabs(vh->sx) > 2.0) + { + vh->sx *= 1.5; + vh->inertia_x = 1; + // cause dirty rect which causes frame events to flow for later animation + vh->tbody_view->frame.dim_changed = 1; + } + } + else if (ev.type == KU_EVENT_SCROLL_Y_END) + { + if (fabs(vh->sy) > 2.0) + { + vh->sy *= 1.5; + vh->inertia_y = 1; + // cause dirty rect which causes frame events to flow for later animation + vh->tbody_view->frame.dim_changed = 1; + } + } else if (ev.type == KU_EVENT_RESIZE) { vh_tbl_body_move(vh->tbody_view, 0, 0); } else if (ev.type == KU_EVENT_MOUSE_MOVE) { - // show scroll - if (vh->scroll_visible == 0) - { - vh->scroll_visible = 1; - if (vh->tscrl_view) vh_tbl_scrl_show(vh->tscrl_view); - } - if (vh->scroll_drag) - { - if (ev.x > view->frame.global.x + view->frame.global.w - SCROLLBAR) - { - if (vh->tscrl_view) vh_tbl_scrl_scroll_v(vh->tscrl_view, ev.y - vh->scroll_drag_y); - } - if (ev.y > view->frame.global.y + view->frame.global.h - SCROLLBAR) - { - if (vh->tscrl_view) vh_tbl_scrl_scroll_h(vh->tscrl_view, ev.x - vh->scroll_drag_x); - } - } if (vh->selected_item && ev.drag) { vh_tbl_evnt_event_t event = {.id = VH_TBL_EVENT_DRAG, .view = view, .rowview = vh->selected_item, .index = 0, .ev = ev, .userdata = vh->userdata}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); vh->selected_item = NULL; } } - else if (ev.type == KU_EVENT_MOUSE_MOVE_OUT) - { - // hide scroll - if (vh->scroll_visible == 1) - { - vh->scroll_visible = 0; - if (vh->tscrl_view) vh_tbl_scrl_hide(vh->tscrl_view); - } - } else if (ev.type == KU_EVENT_MOUSE_DOWN) { - if (ev.x > view->frame.global.x + view->frame.global.w - SCROLLBAR) - { - if (vh->tscrl_view) - { - vh_tbl_scrl_t* svh = vh->tscrl_view->handler_data; - vh->scroll_drag = 1; - vh->scroll_drag_y = ev.y - svh->hori_v->frame.global.y; - - vh_tbl_scrl_scroll_v(vh->tscrl_view, ev.y - vh->scroll_drag_y); - } - } - if (ev.y > view->frame.global.y + view->frame.global.h - SCROLLBAR) - { - if (vh->tscrl_view) - { - vh_tbl_scrl_t* svh = vh->tscrl_view->handler_data; - vh->scroll_drag = 1; - vh->scroll_drag_x = ev.x - svh->hori_v->frame.global.x; - - vh_tbl_scrl_scroll_h(vh->tscrl_view, ev.x - vh->scroll_drag_x); - } - } if (ev.x < view->frame.global.x + view->frame.global.w - SCROLLBAR && ev.y < view->frame.global.y + view->frame.global.h - SCROLLBAR) { - vh_tbl_body_t* bvh = vh->tbody_view->handler_data; + vh_tbl_body_t* bvh = vh->tbody_view->evt_han_data; ku_view_t* context_item = NULL; int context_index = -1; - for (int index = 0; index < bvh->items->length; index++) + for (size_t index = 0; index < bvh->items->length; index++) { ku_view_t* item = bvh->items->data[index]; if (ev.x > item->frame.global.x && @@ -218,7 +255,8 @@ void vh_tbl_evnt_evt(ku_view_t* view, ku_event_t ev) .index = vh->selected_index, .ev = ev, .userdata = vh->userdata}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } if (ev.button == 3) { @@ -236,7 +274,8 @@ void vh_tbl_evnt_evt(ku_view_t* view, ku_event_t ev) .index = vh->selected_index, .ev = ev, .userdata = vh->userdata}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } break; @@ -251,7 +290,8 @@ void vh_tbl_evnt_evt(ku_view_t* view, ku_event_t ev) .index = context_index, .ev = ev, .userdata = vh->userdata}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } } } @@ -261,9 +301,9 @@ void vh_tbl_evnt_evt(ku_view_t* view, ku_event_t ev) { if (!vh->selected_item) { - vh_tbl_body_t* bvh = vh->tbody_view->handler_data; + vh_tbl_body_t* bvh = vh->tbody_view->evt_han_data; - int index = 0; + size_t index = 0; // ku_view_t* item = NULL; for (index = 0; index < bvh->items->length; index++) { @@ -278,29 +318,44 @@ void vh_tbl_evnt_evt(ku_view_t* view, ku_event_t ev) } vh_tbl_evnt_event_t event = {.id = VH_TBL_EVENT_DROP, .view = view, .rowview = vh->selected_item, .index = bvh->head_index + index, .ev = ev, .userdata = vh->userdata}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } } - vh->scroll_drag = 0; } else if (ev.type == KU_EVENT_KEY_DOWN) { vh_tbl_evnt_event_t event = {.id = VH_TBL_EVENT_KEY_DOWN, .view = view, .rowview = vh->selected_item, .index = 0, .ev = ev, .userdata = vh->userdata}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } else if (ev.type == KU_EVENT_KEY_UP) { vh_tbl_evnt_event_t event = {.id = VH_TBL_EVENT_KEY_UP, .view = view, .rowview = vh->selected_item, .index = 0, .ev = ev, .userdata = vh->userdata}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } else if (ev.type == KU_EVENT_FOCUS) { - if (vh->tscrl_view) vh_tbl_scrl_show(vh->tscrl_view); + if (vh->tscrl_view) + vh_tbl_scrl_show(vh->tscrl_view); } else if (ev.type == KU_EVENT_UNFOCUS) { - if (vh->tscrl_view) vh_tbl_scrl_hide(vh->tscrl_view); + if (vh->tscrl_view) + vh_tbl_scrl_hide(vh->tscrl_view); } + else if (ev.type == KU_EVENT_HOLD_START) + { + vh->inertia_x = 0; + vh->inertia_y = 0; + } + + /* TODO refactor ku_window's key and text handling */ + if (ev.type == KU_EVENT_KEY_DOWN || ev.type == KU_EVENT_KEY_UP) + return 0; + else + return (vh->tscrl_view != NULL); } void vh_tbl_evnt_del(void* p) @@ -320,7 +375,7 @@ void vh_tbl_evnt_attach( void (*on_event)(vh_tbl_evnt_event_t event), void* userdata) { - assert(view->handler == NULL && view->handler_data == NULL); + assert(view->evt_han == NULL && view->evt_han_data == NULL); vh_tbl_evnt_t* vh = CAL(sizeof(vh_tbl_evnt_t), vh_tbl_evnt_del, vh_tbl_evnt_desc); vh->on_event = on_event; @@ -329,11 +384,8 @@ void vh_tbl_evnt_attach( vh->tscrl_view = tscrl_view; vh->thead_view = thead_view; - view->handler_data = vh; - view->handler = vh_tbl_evnt_evt; - - view->needs_key = 1; - view->needs_touch = 1; + view->evt_han_data = vh; + view->evt_han = vh_tbl_evnt_evt; } #endif diff --git a/src/kinetic_ui/handler/vh_tbl_head.c b/src/kinetic_ui/handler/vh_tbl_head.c index 5c03545..4b6068e 100644 --- a/src/kinetic_ui/handler/vh_tbl_head.c +++ b/src/kinetic_ui/handler/vh_tbl_head.c @@ -36,9 +36,9 @@ void vh_tbl_head_jump(ku_view_t* hview, float x); void vh_tbl_head_align(ku_view_t* view) { - vh_tbl_head_t* vh = view->handler_data; + vh_tbl_head_t* vh = view->evt_han_data; int pos = 0; - for (int index = 0; index < vh->head->views->length; index++) + for (size_t index = 0; index < vh->head->views->length; index++) { ku_view_t* sv = vh->head->views->data[index]; ku_rect_t svfl = sv->frame.local; @@ -48,16 +48,16 @@ void vh_tbl_head_align(ku_view_t* view) } } -void vh_tbl_head_evt(ku_view_t* view, ku_event_t ev) +int vh_tbl_head_evt(ku_view_t* view, ku_event_t ev) { - vh_tbl_head_t* vh = view->handler_data; + vh_tbl_head_t* vh = view->evt_han_data; if (vh->head) { if (ev.type == KU_EVENT_MOUSE_DOWN) { // look for - for (int index = 0; index < vh->head->views->length; index++) + for (size_t index = 0; index < vh->head->views->length; index++) { ku_view_t* sv = vh->head->views->data[index]; ku_rect_t svf = sv->frame.global; @@ -94,14 +94,14 @@ void vh_tbl_head_evt(ku_view_t* view, ku_event_t ev) if (vh->resize == 0) { // look for - for (int index = 0; index < vh->head->views->length; index++) + for (size_t index = 0; index < vh->head->views->length; index++) { ku_view_t* sv = vh->head->views->data[index]; ku_rect_t svf = sv->frame.global; // inside if (ev.x > svf.x && ev.x < svf.x + svf.w) { - if (index != vh->active) + if ((int) index != vh->active) { // drop on different cell ku_view_t* cell1 = RET(vh->head->views->data[vh->active]); @@ -117,24 +117,28 @@ void vh_tbl_head_evt(ku_view_t* view, ku_event_t ev) vh_tbl_head_align(view); - if (vh->head_reorder) (*vh->head_reorder)(view, vh->active, index, vh->userdata); + if (vh->head_reorder) + (*vh->head_reorder)(view, vh->active, index, vh->userdata); break; } else { // self click - if (vh->head_reorder) (*vh->head_reorder)(view, -1, index, vh->userdata); + if (vh->head_reorder) + (*vh->head_reorder)(view, -1, index, vh->userdata); } } } - if (vh->head_move) (*vh->head_move)(view, -1, 0, vh->userdata); + if (vh->head_move) + (*vh->head_move)(view, -1, 0, vh->userdata); vh_tbl_head_align(view); } else { - if (vh->head_move) (*vh->head_resize)(view, -1, 0, vh->userdata); + if (vh->head_move) + (*vh->head_resize)(view, -1, 0, vh->userdata); } } @@ -145,7 +149,7 @@ void vh_tbl_head_evt(ku_view_t* view, ku_event_t ev) { if (vh->active > -1) { - if (vh->active < vh->head->views->length) + if (vh->active < (int) vh->head->views->length) { ku_view_t* sv = vh->head->views->data[vh->active]; ku_rect_t svfg = sv->frame.global; @@ -156,25 +160,29 @@ void vh_tbl_head_evt(ku_view_t* view, ku_event_t ev) svfl.w = ev.x - svfg.x; ku_view_set_frame(sv, svfl); vh_tbl_head_align(view); - if (vh->head_resize) (*vh->head_resize)(view, vh->active, svfl.w, vh->userdata); + if (vh->head_resize) + (*vh->head_resize)(view, vh->active, svfl.w, vh->userdata); } else { svfl.x = ev.x - vh->head->frame.global.x - vh->touchx; ku_view_set_frame(sv, svfl); - if (vh->head_move) (*vh->head_move)(view, vh->active, svfl.x, vh->userdata); + if (vh->head_move) + (*vh->head_move)(view, vh->active, svfl.x, vh->userdata); } } } } } + + return 0; } void vh_tbl_head_move( ku_view_t* view, float dx) { - vh_tbl_head_t* vh = view->handler_data; + vh_tbl_head_t* vh = view->evt_han_data; ku_rect_t frame = vh->head->frame.local; @@ -187,7 +195,7 @@ void vh_tbl_head_jump( ku_view_t* view, float x) { - vh_tbl_head_t* vh = view->handler_data; + vh_tbl_head_t* vh = view->evt_han_data; ku_rect_t frame = vh->head->frame.local; @@ -218,7 +226,7 @@ void vh_tbl_head_attach( void (*head_reorder)(ku_view_t* hview, int ind1, int ind2, void* userdata), void* userdata) { - assert(view->handler == NULL && view->handler_data == NULL); + assert(view->evt_han == NULL && view->evt_han_data == NULL); vh_tbl_head_t* vh = CAL(sizeof(vh_tbl_head_t), vh_tbl_head_del, vh_tbl_head_desc); vh->userdata = userdata; @@ -229,9 +237,8 @@ void vh_tbl_head_attach( vh->head = (*head_create)(view, userdata); // REL 0 vh->active = -1; - view->handler_data = vh; - view->handler = vh_tbl_head_evt; - view->needs_touch = 1; + view->evt_han_data = vh; + view->evt_han = vh_tbl_head_evt; ku_view_add_subview(view, vh->head); } diff --git a/src/kinetic_ui/handler/vh_tbl_scrl.c b/src/kinetic_ui/handler/vh_tbl_scrl.c index 9df3890..380767d 100644 --- a/src/kinetic_ui/handler/vh_tbl_scrl.c +++ b/src/kinetic_ui/handler/vh_tbl_scrl.c @@ -13,9 +13,16 @@ typedef struct _vh_tbl_scrl_t ku_view_t* hori_v; int state; // 0 scroll 1 open 2 close int steps; - uint32_t item_cnt; + size_t item_cnt; void* userdata; int enabled; + + int scroll_on_x; + int scroll_on_y; + int scroll_visible; + float scroll_drag_x; + float scroll_drag_y; + } vh_tbl_scrl_t; void vh_tbl_scrl_attach( @@ -27,7 +34,7 @@ void vh_tbl_scrl_attach( void vh_tbl_scrl_update(ku_view_t* view); void vh_tbl_scrl_show(ku_view_t* view); void vh_tbl_scrl_hide(ku_view_t* view); -void vh_tbl_scrl_set_item_count(ku_view_t* view, uint32_t count); +void vh_tbl_scrl_set_item_count(ku_view_t* view, size_t count); void vh_tbl_scrl_scroll_v(ku_view_t* view, int y); void vh_tbl_scrl_scroll_h(ku_view_t* view, int x); void vh_tbl_scrl_enable(ku_view_t* view, int flag); @@ -36,52 +43,21 @@ void vh_tbl_scrl_enable(ku_view_t* view, int flag); #if __INCLUDE_LEVEL__ == 0 -void vh_tbl_scrl_del(void* p) -{ - vh_tbl_scrl_t* vh = p; - REL(vh->vert_v); - REL(vh->hori_v); -} - -void vh_tbl_scrl_desc(void* p, int level) -{ - printf("vh_tbl_scrl"); -} - -void vh_tbl_scrl_attach( - ku_view_t* view, - ku_view_t* tbody_view, - ku_view_t* thead_view, - void* userdata) -{ - vh_tbl_scrl_t* vh = CAL(sizeof(vh_tbl_scrl_t), vh_tbl_scrl_del, vh_tbl_scrl_desc); - vh->userdata = userdata; - vh->tbody_view = tbody_view; - vh->thead_view = thead_view; - vh->enabled = 1; - - assert(view->views->length > 1); +#include "mt_log.c" - vh->vert_v = RET(view->views->data[0]); - vh->hori_v = RET(view->views->data[1]); - - ku_view_set_texture_alpha(vh->hori_v, 0.0, 0); - ku_view_set_texture_alpha(vh->vert_v, 0.0, 0); +#define SCROLLBAR 20.0 - view->handler_data = vh; -} - -void vh_tbl_scrl_set_item_count(ku_view_t* view, uint32_t count) +void vh_tbl_scrl_set_item_count(ku_view_t* view, size_t count) { - vh_tbl_scrl_t* vh = view->handler_data; + vh_tbl_scrl_t* vh = view->evt_han_data; vh->item_cnt = count; } void vh_tbl_scrl_update(ku_view_t* view) { - vh_tbl_scrl_t* vh = view->handler_data; - vh_tbl_body_t* bvh = vh->tbody_view->handler_data; + vh_tbl_scrl_t* vh = view->evt_han_data; + vh_tbl_body_t* bvh = vh->tbody_view->evt_han_data; if (bvh->items->length > 0 && vh->item_cnt > 0) { @@ -107,7 +83,8 @@ void vh_tbl_scrl_update(ku_view_t* view) float pos = (view->frame.local.h - vh->hori_v->frame.local.h) * pratio; float hth = (view->frame.local.h - vh->hori_v->frame.local.h) * sratio; - if (hth < 30.0) hth = 30.0; + if (hth < 30.0) + hth = 30.0; if (vh->state == 2) { @@ -141,7 +118,8 @@ void vh_tbl_scrl_update(ku_view_t* view) float pos = view->frame.local.w * pratio; float wth = view->frame.local.w * sratio; - if (wth < 30.0) wth = 30.0; + if (wth < 30.0) + wth = 30.0; if (vh->state == 2) { @@ -174,12 +152,11 @@ void vh_tbl_scrl_update(ku_view_t* view) void vh_tbl_scrl_show(ku_view_t* view) { - vh_tbl_scrl_t* vh = view->handler_data; - vh_tbl_body_t* bvh = vh->tbody_view->handler_data; + vh_tbl_scrl_t* vh = view->evt_han_data; + vh_tbl_body_t* bvh = vh->tbody_view->evt_han_data; if (vh->enabled) { - if (bvh->items->length > 0 && vh->item_cnt > 0) { vh->state = 1; @@ -192,7 +169,7 @@ void vh_tbl_scrl_show(ku_view_t* view) void vh_tbl_scrl_hide(ku_view_t* view) { - vh_tbl_scrl_t* vh = view->handler_data; + vh_tbl_scrl_t* vh = view->evt_han_data; if (vh->enabled) { @@ -206,8 +183,8 @@ void vh_tbl_scrl_hide(ku_view_t* view) void vh_tbl_scrl_scroll_v(ku_view_t* view, int y) { - vh_tbl_scrl_t* vh = view->handler_data; - vh_tbl_body_t* bvh = vh->tbody_view->handler_data; + vh_tbl_scrl_t* vh = view->evt_han_data; + vh_tbl_body_t* bvh = vh->tbody_view->evt_han_data; if (bvh->items->length > 0 && vh->item_cnt > 0) { @@ -221,8 +198,10 @@ void vh_tbl_scrl_scroll_v(ku_view_t* view, int y) float height = (view->frame.local.h - view->frame.local.h * sratio); float pratio = (float) y / height; - if (pratio < 0.0) pratio = 0.0; - if (pratio > 1.0) pratio = 1.0; + if (pratio < 0.0) + pratio = 0.0; + if (pratio > 1.0) + pratio = 1.0; int topindex = pratio * (vert_max - vert_vis); vh_tbl_body_vjump(vh->tbody_view, topindex, 1); @@ -238,8 +217,8 @@ void vh_tbl_scrl_scroll_v(ku_view_t* view, int y) void vh_tbl_scrl_scroll_h(ku_view_t* view, int x) { - vh_tbl_scrl_t* vh = view->handler_data; - vh_tbl_body_t* bvh = vh->tbody_view->handler_data; + vh_tbl_scrl_t* vh = view->evt_han_data; + vh_tbl_body_t* bvh = vh->tbody_view->evt_han_data; if (bvh->items->length > 0 && vh->item_cnt > 0) { @@ -255,13 +234,16 @@ void vh_tbl_scrl_scroll_h(ku_view_t* view, int x) float width = (view->frame.local.w - view->frame.local.w * sratio); float pratio = (float) x / width; - if (pratio < 0.0) pratio = 0.0; - if (pratio > 1.0) pratio = 1.0; + if (pratio < 0.0) + pratio = 0.0; + if (pratio > 1.0) + pratio = 1.0; float dx = pratio * (hori_max - hori_vis); vh_tbl_body_hjump(vh->tbody_view, -dx); - if (vh->thead_view) vh_tbl_head_jump(vh->thead_view, -dx); + if (vh->thead_view) + vh_tbl_head_jump(vh->thead_view, -dx); ku_rect_t frame = vh->hori_v->frame.local; frame.w = view->frame.local.w * sratio; @@ -274,8 +256,108 @@ void vh_tbl_scrl_scroll_h(ku_view_t* view, int x) void vh_tbl_scrl_enable(ku_view_t* view, int flag) { - vh_tbl_scrl_t* vh = view->handler_data; + vh_tbl_scrl_t* vh = view->evt_han_data; vh->enabled = flag; } +int vh_tbl_scrl_evt(ku_view_t* view, ku_event_t ev) +{ + vh_tbl_scrl_t* vh = view->evt_han_data; + + if (ev.type == KU_EVENT_FRAME) + { + vh_tbl_scrl_update(view); + /* frame update stops if there is no change in the ui, scroller reaches final position or alpha */ + } + else if (ev.type == KU_EVENT_MOUSE_MOVE) + { + // show scroll + if (vh->scroll_visible == 0) + { + vh->scroll_visible = 1; + vh_tbl_scrl_show(view); + } + + if (vh->scroll_on_y) + vh_tbl_scrl_scroll_v(view, ev.y - view->frame.global.y); + + if (vh->scroll_on_x) + vh_tbl_scrl_scroll_h(view, ev.x - view->frame.global.x); + } + else if (ev.type == KU_EVENT_MOUSE_MOVE_OUT) + { + // hide scroll + if (vh->scroll_visible == 1) + { + vh->scroll_visible = 0; + vh_tbl_scrl_hide(view); + } + } + else if (ev.type == KU_EVENT_MOUSE_DOWN) + { + if (vh->enabled) + { + if (ev.x > view->frame.global.x + view->frame.global.w - SCROLLBAR) + { + vh_tbl_scrl_t* svh = view->evt_han_data; + vh->scroll_on_y = 1; + vh->scroll_drag_y = ev.y - svh->hori_v->frame.global.y; + + vh_tbl_scrl_scroll_v(view, ev.y - vh->scroll_drag_y); + } + if (ev.y > view->frame.global.y + view->frame.global.h - SCROLLBAR) + { + vh_tbl_scrl_t* svh = view->evt_han_data; + vh->scroll_on_x = 1; + vh->scroll_drag_x = ev.x - svh->hori_v->frame.global.x; + + vh_tbl_scrl_scroll_h(view, ev.x - vh->scroll_drag_x); + } + } + } + else if (ev.type == KU_EVENT_MOUSE_UP) + { + vh->scroll_on_x = 0; + vh->scroll_on_y = 0; + } + + return 0; +} + +void vh_tbl_scrl_del(void* p) +{ + vh_tbl_scrl_t* vh = p; + REL(vh->vert_v); + REL(vh->hori_v); +} + +void vh_tbl_scrl_desc(void* p, int level) +{ + printf("vh_tbl_scrl"); +} + +void vh_tbl_scrl_attach( + ku_view_t* view, + ku_view_t* tbody_view, + ku_view_t* thead_view, + void* userdata) +{ + vh_tbl_scrl_t* vh = CAL(sizeof(vh_tbl_scrl_t), vh_tbl_scrl_del, vh_tbl_scrl_desc); + vh->userdata = userdata; + vh->tbody_view = tbody_view; + vh->thead_view = thead_view; + vh->enabled = 1; + + assert(view->views->length > 1); + + vh->vert_v = RET(view->views->data[0]); + vh->hori_v = RET(view->views->data[1]); + + ku_view_set_texture_alpha(vh->hori_v, 0.0, 0); + ku_view_set_texture_alpha(vh->vert_v, 0.0, 0); + + view->evt_han = vh_tbl_scrl_evt; + view->evt_han_data = vh; +} + #endif diff --git a/src/kinetic_ui/handler/vh_textinput.c b/src/kinetic_ui/handler/vh_textinput.c index 741fa4b..aa0684d 100644 --- a/src/kinetic_ui/handler/vh_textinput.c +++ b/src/kinetic_ui/handler/vh_textinput.c @@ -37,7 +37,7 @@ void glyphchain_init(glyphchain_t* chain); void glyphchain_reset(glyphchain_t* chain); void glyphchain_destroy(glyphchain_t* chain); void glyphchain_add(glyphchain_t* chain, uint32_t cp, char* id, void* data); -void glyphchain_rem(glyphchain_t* chain, uint32_t index); +void glyphchain_rem(glyphchain_t* chain, size_t index); struct _vh_textinput_t { @@ -45,13 +45,13 @@ struct _vh_textinput_t ku_view_t* holder_v; // placeholder text view ku_rect_t frame_s; // starting frame for autosize minimal values - int glyph_index; + size_t glyph_index; textstyle_t style; char active; - int limit; - int mouse_out_deact; - int new_glyph_index; + size_t limit; + int mouse_out_deact; + size_t new_glyph_index; glyphchain_t glyphchain; @@ -72,6 +72,7 @@ void vh_textinput_activate(ku_view_t* view, char state); #include "ku_bitmap.c" #include "ku_draw.c" #include "ku_gen_textstyle.c" +#include "mt_log.c" #include "mt_string.c" #include "mt_vector.c" #include "tg_css.c" @@ -83,9 +84,10 @@ void vh_textinput_on_anim(vh_anim_event_t event) { ku_view_t* tiview = event.userdata; - vh_textinput_t* data = tiview->handler_data; + vh_textinput_t* data = tiview->evt_han_data; - if (mt_vector_index_of_data(data->glyphchain.views, event.view) == UINT32_MAX) ku_view_remove_from_parent(event.view); + if (mt_vector_index_of_data(data->glyphchain.views, event.view) == SIZE_MAX) + ku_view_remove_from_parent(event.view); } void glyphchain_init(glyphchain_t* chain) @@ -129,7 +131,7 @@ void glyphchain_add(glyphchain_t* chain, uint32_t cp, char* id, void* data) chain->length = utf8len(chain->text); } -void glyphchain_rem(glyphchain_t* chain, uint32_t index) +void glyphchain_rem(glyphchain_t* chain, size_t index) { if (index < chain->length) { @@ -137,7 +139,7 @@ void glyphchain_rem(glyphchain_t* chain, uint32_t index) chain->length = utf8len(chain->text); - mt_vector_rem_at_index(chain->views, index); + mt_vector_rem_index(chain->views, index); } } @@ -148,7 +150,7 @@ glyph_t* vh_textinput_glyphs_from_string(char* text, size_t* count) glyph_t* glyphs = malloc(sizeof(glyph_t) * length); // REL 0 utf8_int32_t cp; - for (int i = 0; i < length; i++) + for (size_t i = 0; i < length; i++) { part = utf8codepoint(part, &cp); glyphs[i].cp = cp; @@ -161,7 +163,7 @@ glyph_t* vh_textinput_glyphs_from_string(char* text, size_t* count) void vh_textinput_upd(ku_view_t* view) { - vh_textinput_t* data = view->handler_data; + vh_textinput_t* data = view->evt_han_data; ku_rect_t frame = view->frame.local; if (data->glyphchain.length > 0) @@ -169,7 +171,8 @@ void vh_textinput_upd(ku_view_t* view) size_t count = 0; glyph_t* glyphs = vh_textinput_glyphs_from_string(data->glyphchain.text, &count); - if (data->glyphchain.length != count) printf("GLYPHCHAIN LENGTH MISMATCH %lu %lu\n", count, data->glyphchain.length); + if (data->glyphchain.length != count) + printf("GLYPHCHAIN LENGTH MISMATCH %lu %lu\n", count, data->glyphchain.length); int nw; int nh; @@ -187,7 +190,8 @@ void vh_textinput_upd(ku_view_t* view) } if (nw <= frame.w) { - if (nw <= data->frame_s.w) nw = data->frame_s.w; + if (nw <= data->frame_s.w) + nw = data->frame_s.w; frame.w = nw; ku_view_set_frame(view, frame); } @@ -198,13 +202,14 @@ void vh_textinput_upd(ku_view_t* view) } if (nh <= frame.h) { - if (nh <= data->frame_s.h) nh = data->frame_s.h; + if (nh <= data->frame_s.h) + nh = data->frame_s.h; frame.h = nh; ku_view_set_frame(view, frame); } } - for (int i = 0; i < count; i++) + for (size_t i = 0; i < count; i++) { glyph_t g = glyphs[i]; @@ -278,7 +283,8 @@ void vh_textinput_upd(ku_view_t* view) // update cursor position glyph_t last = {0}; - if (count > 0) last = glyphs[count - 1]; + if (count > 0) + last = glyphs[count - 1]; ku_rect_t crsr_f = {0}; crsr_f.x = last.x + last.w + 1; @@ -312,22 +318,28 @@ void vh_textinput_upd(ku_view_t* view) { crsr_f.x = data->style.margin_left; crsr_f.y = data->style.margin || data->style.margin_top; - if (data->style.valign == VA_CENTER) crsr_f.y = frame.h / 2 - crsr_f.h / 2; - if (data->style.valign == VA_BOTTOM) crsr_f.y = frame.h - data->style.margin_bottom - crsr_f.h; + if (data->style.valign == VA_CENTER) + crsr_f.y = frame.h / 2 - crsr_f.h / 2; + if (data->style.valign == VA_BOTTOM) + crsr_f.y = frame.h - data->style.margin_bottom - crsr_f.h; } if (data->style.align == TA_RIGHT) { crsr_f.x = frame.w - data->style.margin_right - crsr_f.w; crsr_f.y = data->style.margin_top; - if (data->style.valign == VA_CENTER) crsr_f.y = frame.h / 2 - crsr_f.h / 2; - if (data->style.valign == VA_BOTTOM) crsr_f.y = frame.h - data->style.margin_bottom - crsr_f.h; + if (data->style.valign == VA_CENTER) + crsr_f.y = frame.h / 2 - crsr_f.h / 2; + if (data->style.valign == VA_BOTTOM) + crsr_f.y = frame.h - data->style.margin_bottom - crsr_f.h; } if (data->style.align == TA_CENTER) { crsr_f.x = frame.w / 2 - crsr_f.w / 2; crsr_f.y = data->style.margin_top; - if (data->style.valign == VA_CENTER) crsr_f.y = frame.h / 2 - crsr_f.h / 2; - if (data->style.valign == VA_BOTTOM) crsr_f.y = frame.h - data->style.margin_bottom - crsr_f.h; + if (data->style.valign == VA_CENTER) + crsr_f.y = frame.h / 2 - crsr_f.h / 2; + if (data->style.valign == VA_BOTTOM) + crsr_f.y = frame.h - data->style.margin_bottom - crsr_f.h; } vh_anim_finish(data->cursor_v); @@ -346,9 +358,9 @@ void vh_textinput_upd(ku_view_t* view) void vh_textinput_activate(ku_view_t* view, char state) { - assert(view && view->handler_data != NULL); + assert(view && view->evt_han_data != NULL); - vh_textinput_t* data = view->handler_data; + vh_textinput_t* data = view->evt_han_data; if (state) { @@ -380,9 +392,9 @@ void vh_textinput_activate(ku_view_t* view, char state) vh_textinput_upd(view); } -void vh_textinput_evt(ku_view_t* view, ku_event_t ev) +int vh_textinput_evt(ku_view_t* view, ku_event_t ev) { - vh_textinput_t* data = view->handler_data; + vh_textinput_t* data = view->evt_han_data; if (ev.type == KU_EVENT_MOUSE_DOWN) { @@ -391,7 +403,8 @@ void vh_textinput_evt(ku_view_t* view, ku_event_t ev) vh_textinput_activate(view, 1); vh_textinput_event_t event = {.id = VH_TEXTINPUT_ACTIVATE, .vh = data, .text = data->glyphchain.text, .view = view}; - if (data->on_event) (*data->on_event)(event); + if (data->on_event) + (*data->on_event)(event); } else if (ev.type == KU_EVENT_MOUSE_DOWN_OUT) { @@ -406,7 +419,8 @@ void vh_textinput_evt(ku_view_t* view, ku_event_t ev) { vh_textinput_activate(view, 0); vh_textinput_event_t event = {.id = VH_TEXTINPUT_DEACTIVATE, .vh = data, .text = data->glyphchain.text, .view = view}; - if (data->on_event) (*data->on_event)(event); + if (data->on_event) + (*data->on_event)(event); } } } @@ -425,7 +439,8 @@ void vh_textinput_evt(ku_view_t* view, ku_event_t ev) vh_textinput_upd(view); vh_textinput_event_t event = {.id = VH_TEXTINPUT_TEXT, .vh = data, .text = data->glyphchain.text, .view = view}; - if (data->on_event) (*data->on_event)(event); + if (data->on_event) + (*data->on_event)(event); } } else if (ev.type == KU_EVENT_KEY_DOWN) @@ -449,19 +464,22 @@ void vh_textinput_evt(ku_view_t* view, ku_event_t ev) vh_textinput_upd(view); vh_textinput_event_t event = {.id = VH_TEXTINPUT_TEXT, .vh = data, .text = data->glyphchain.text, .view = view}; - if (data->on_event) (*data->on_event)(event); + if (data->on_event) + (*data->on_event)(event); } if (ev.keycode == XKB_KEY_Return) { vh_textinput_event_t event = {.id = VH_TEXTINPUT_RETURN, .vh = data, .text = data->glyphchain.text, .view = view}; - if (data->on_event) (*data->on_event)(event); + if (data->on_event) + (*data->on_event)(event); } if (ev.keycode == XKB_KEY_Escape) { vh_textinput_activate(view, 0); vh_textinput_event_t event = {.id = VH_TEXTINPUT_DEACTIVATE, .vh = data, .text = data->glyphchain.text, .view = view}; - if (data->on_event) (*data->on_event)(event); + if (data->on_event) + (*data->on_event)(event); } } else if (ev.type == KU_EVENT_FOCUS) @@ -476,6 +494,8 @@ void vh_textinput_evt(ku_view_t* view, ku_event_t ev) { /* vh_textinput_upd(view); */ } + + return 0; } void vh_textinput_del(void* p) @@ -495,7 +515,7 @@ void vh_textinput_desc(void* p, int level) void vh_textinput_add(ku_view_t* view, char* text, char* phtext, void (*on_event)(vh_textinput_event_t)) { - assert(view->handler == NULL && view->handler_data == NULL); + assert(view->evt_han == NULL && view->evt_han_data == NULL); float scale = view->style.scale; @@ -517,12 +537,8 @@ void vh_textinput_add(ku_view_t* view, char* text, char* phtext, void (*on_event data->mouse_out_deact = 1; - view->needs_key = 1; // backspace event - view->needs_text = 1; // unicode text event - view->blocks_key = 1; - - view->handler = vh_textinput_evt; - view->handler_data = data; + view->evt_han = vh_textinput_evt; + view->evt_han_data = data; data->style.backcolor = 0; @@ -554,8 +570,6 @@ void vh_textinput_add(ku_view_t* view, char* text, char* phtext, void (*on_event vh_anim_add(data->holder_v, NULL, NULL); - data->holder_v->blocks_touch = 0; - ku_view_add_subview(view, data->holder_v); // view setup @@ -570,7 +584,7 @@ void vh_textinput_add(ku_view_t* view, char* text, char* phtext, void (*on_event { char* part = text; utf8_int32_t cp; - for (int i = 0; i < utf8len(text); i++) + for (size_t i = 0; i < utf8len(text); i++) { part = utf8codepoint(part, &cp); glyphchain_add(&data->glyphchain, cp, view->id, view); @@ -589,11 +603,11 @@ void vh_textinput_add(ku_view_t* view, char* text, char* phtext, void (*on_event void vh_textinput_set_text(ku_view_t* view, char* text) { - vh_textinput_t* data = view->handler_data; + vh_textinput_t* data = view->evt_han_data; // remove glyphs - for (int i = data->glyphchain.length - 1; i > -1; i--) + for (size_t i = data->glyphchain.length; i-- > 0;) { ku_view_t* gv = data->glyphchain.views->data[i]; @@ -620,34 +634,37 @@ void vh_textinput_set_text(ku_view_t* view, char* text) { char* part = text; utf8_int32_t cp; - for (int i = 0; i < utf8len(text); i++) + for (size_t i = 0; i < utf8len(text); i++) { part = utf8codepoint(part, &cp); glyphchain_add(&data->glyphchain, cp, view->id, view); } + + ku_view_set_texture_alpha(data->holder_v, 0, 1); } vh_textinput_upd(view); vh_textinput_event_t event = {.id = VH_TEXTINPUT_TEXT, .vh = data, .text = data->glyphchain.text, .view = view}; - if (data->on_event) (*data->on_event)(event); + if (data->on_event) + (*data->on_event)(event); } char* vh_textinput_get_text(ku_view_t* view) { - vh_textinput_t* data = view->handler_data; + vh_textinput_t* data = view->evt_han_data; return data->glyphchain.text; } void vh_textinput_set_deactivate_on_mouse_out(ku_view_t* view, int flag) { - vh_textinput_t* data = view->handler_data; + vh_textinput_t* data = view->evt_han_data; data->mouse_out_deact = flag; } void vh_textinput_set_limit(ku_view_t* view, int limit) { - vh_textinput_t* data = view->handler_data; + vh_textinput_t* data = view->evt_han_data; data->limit = limit; } diff --git a/src/kinetic_ui/handler/vh_touch.c b/src/kinetic_ui/handler/vh_touch.c index 9b09f2f..2898c9b 100644 --- a/src/kinetic_ui/handler/vh_touch.c +++ b/src/kinetic_ui/handler/vh_touch.c @@ -30,14 +30,17 @@ void vh_touch_add(ku_view_t* view, void (*on_event)(vh_touch_event_t)); #include "ku_event.c" -void vh_touch_evt(ku_view_t* view, ku_event_t ev) +int vh_touch_evt(ku_view_t* view, ku_event_t ev) { if (ev.type == KU_EVENT_MOUSE_DOWN) { - vh_touch_t* vh = view->handler_data; + vh_touch_t* vh = view->evt_han_data; vh_touch_event_t event = {.id = VH_TOUCH_EVENT, .vh = vh, .view = view}; - if (vh->on_event) (*vh->on_event)(event); + if (vh->on_event) + (*vh->on_event)(event); } + + return 0; } void vh_touch_del(void* p) @@ -52,15 +55,13 @@ void vh_touch_desc(void* p, int level) void vh_touch_add(ku_view_t* view, void (*on_event)(vh_touch_event_t)) { - assert(view->handler == NULL && view->handler_data == NULL); + assert(view->evt_han == NULL && view->evt_han_data == NULL); vh_touch_t* vh = CAL(sizeof(vh_touch_t), vh_touch_del, vh_touch_desc); vh->on_event = on_event; - view->handler = vh_touch_evt; - view->handler_data = vh; - - view->needs_touch = 1; + view->evt_han = vh_touch_evt; + view->evt_han_data = vh; } #endif diff --git a/src/kinetic_ui/ku_connector_wayland.c b/src/kinetic_ui/ku_connector_wayland.c index d0397bc..0d5db55 100644 --- a/src/kinetic_ui/ku_connector_wayland.c +++ b/src/kinetic_ui/ku_connector_wayland.c @@ -111,15 +111,15 @@ enum wl_window_type typedef struct _wl_window_t wl_window_t; struct _wl_window_t { - int scale; - int width; - int height; - int buffer_width; - int buffer_height; - int fullscreen; - int hidden; - int margin; - char anchor[4]; + int scale; + uint32_t width; + uint32_t height; + int buffer_width; + int buffer_height; + int fullscreen; + int hidden; + int margin; + char anchor[4]; int shown; /* surface_enter is called, scaling is set */ int inited; /* egl is swapped or buffer is attached for the first time to trigger surface_enter event */ @@ -136,6 +136,8 @@ struct _wl_window_t struct zwp_pointer_gesture_pinch_v1* pinch; struct wl_pointer* wl_pointer; + int hold; /* hold gesture is active */ + /* backing buffer for native window */ struct wl_buffer* buffer; /* wl buffer for surface */ @@ -150,9 +152,9 @@ struct _wl_window_t /* needed for resize events */ - int new_scale; - int new_width; - int new_height; + int new_scale; + uint32_t new_width; + uint32_t new_height; /* egl window related */ @@ -440,7 +442,7 @@ static const struct wl_callback_listener wl_surface_frame_listener = { static void ku_wayland_layer_surface_configure(void* data, struct zwlr_layer_surface_v1* surface, uint32_t serial, uint32_t width, uint32_t height) { - mt_log_debug("layer surface configure serial %u width %i height %i", serial, width, height); + /* mt_log_debug("layer surface configure serial %u width %i height %i", serial, width, height); */ zwlr_layer_surface_v1_ack_configure(surface, serial); @@ -484,7 +486,7 @@ static void ku_wayland_layer_surface_configure(void* data, struct zwlr_layer_sur static void ku_wayland_layer_surface_closed(void* _data, struct zwlr_layer_surface_v1* surface) { - mt_log_debug("layer surface configure"); + /* mt_log_debug("layer surface configure"); */ } struct zwlr_layer_surface_v1_listener layer_surface_listener = { @@ -496,7 +498,7 @@ struct zwlr_layer_surface_v1_listener layer_surface_listener = { void xdg_toplevel_configure(void* data, struct xdg_toplevel* xdg_toplevel, int32_t width, int32_t height, struct wl_array* states) { - mt_log_debug("xdg toplevel configure w %i h %i", width, height); + /* mt_log_debug("xdg toplevel configure w %i h %i", width, height); */ wl_window_t* info = data; @@ -515,7 +517,7 @@ void xdg_toplevel_close(void* data, struct xdg_toplevel* xdg_toplevel) void xdg_toplevel_configure_bounds(void* data, struct xdg_toplevel* xdg_toplevel, int32_t width, int32_t height) { - mt_log_debug("xdg toplevel configure bounds w %i h %i", width, height); + /* mt_log_debug("xdg toplevel configure bounds w %i h %i", width, height); */ } void xdg_toplevel_wm_capabilities(void* data, struct xdg_toplevel* xdg_toplevel, struct wl_array* capabilities) @@ -538,13 +540,14 @@ static void xdg_surface_configure(void* data, struct xdg_surface* xdg_surface, u wl_window_t* info = data; - mt_log_debug("xdg surface configure %i %i %i", info->type, info->width, info->height); + /* mt_log_debug("xdg surface configure %i %i %i", info->type, info->width, info->height); */ if (info->inited == 0) { info->inited = 1; - if (info->type == WL_WINDOW_EGL) eglSwapBuffers(wlc.windows[0]->egldisplay, wlc.windows[0]->eglsurface); + if (info->type == WL_WINDOW_EGL) + eglSwapBuffers(wlc.windows[0]->egldisplay, wlc.windows[0]->eglsurface); if (info->type == WL_WINDOW_NATIVE) { ku_wayland_create_buffer(info, info->width, info->height); @@ -567,7 +570,8 @@ static void xdg_surface_configure(void* data, struct xdg_surface* xdg_surface, u info->bitmap.h = info->buffer_height; wl_egl_window_resize(info->eglwindow, info->buffer_width, info->buffer_height, 0, 0); } - if (info->type == WL_WINDOW_NATIVE) ku_wayland_create_buffer(info, info->buffer_width, info->buffer_height); + if (info->type == WL_WINDOW_NATIVE) + ku_wayland_create_buffer(info, info->buffer_width, info->buffer_height); /* we shouldn't send events before surface enter ( and ku_event_window_shown ) event */ if (info->shown) @@ -927,7 +931,7 @@ wl_window_t* ku_wayland_create_generic_layer(struct monitor_info* monitor, int w info->margin = margin; info->monitor = monitor; info->hidden = 1; - memcpy(info->anchor, anchor, 4); + memcpy(info->anchor, anchor, strlen(anchor) < 5 ? strlen(anchor) : 4); info->type = WL_WINDOW_LAYER; @@ -991,12 +995,49 @@ void ku_wayland_request_frame(wl_window_t* info) static void gesture_hold_begin(void* data, struct zwp_pointer_gesture_hold_v1* hold, uint32_t serial, uint32_t time, struct wl_surface* surface, uint32_t fingers) { - mt_log_debug("hold start"); + if (fingers == 2) + { + for (int index = 0; index < wlc.window_count; index++) + { + wl_window_t* window = wlc.windows[index]; + + if (window->surface == surface && window->monitor) + { + window->hold = 1; + + ku_event_t event = init_event(); + event.type = KU_EVENT_HOLD_START; + event.x = window->pointer.px; + event.y = window->pointer.py; + event.ctrl_down = wlc.keyboard.control; + event.shift_down = wlc.keyboard.shift; + + (*wlc.update)(event); + } + } + } } static void gesture_hold_end(void* data, struct zwp_pointer_gesture_hold_v1* hold, uint32_t serial, uint32_t time, int32_t cancelled) { - mt_log_debug("hold end"); + for (int index = 0; index < wlc.window_count; index++) + { + wl_window_t* window = wlc.windows[index]; + + if (window->hold) + { + window->hold = 0; + + ku_event_t event = init_event(); + event.type = KU_EVENT_HOLD_END; + event.x = window->pointer.px; + event.y = window->pointer.py; + event.ctrl_down = wlc.keyboard.control; + event.shift_down = wlc.keyboard.shift; + + (*wlc.update)(event); + } + } } static const struct zwp_pointer_gesture_hold_v1_listener gesture_hold_listener = { @@ -1169,8 +1210,6 @@ void ku_wayland_pointer_handle_button(void* data, struct wl_pointer* wl_pointer, if (window->wl_pointer == wl_pointer) { - window->pointer.drag = window->pointer.down; - ku_event_t event = init_event(); event.x = window->pointer.px; event.y = window->pointer.py; @@ -1236,7 +1275,24 @@ void ku_wayland_pointer_handle_axis_source(void* data, struct wl_pointer* wl_poi void ku_wayland_pointer_handle_axis_stop(void* data, struct wl_pointer* wl_pointer, uint time, uint axis) { - /* mt_log_debug("pointer handle axis stop"); */ + /* mt_log_debug("pointer handle axis STOP %i", axis); */ + + for (int index = 0; index < wlc.window_count; index++) + { + wl_window_t* window = wlc.windows[index]; + + if (window->wl_pointer == wl_pointer) + { + ku_event_t event = init_event(); + event.type = axis == 1 ? KU_EVENT_SCROLL_X_END : KU_EVENT_SCROLL_Y_END; + event.x = window->pointer.px; + event.y = window->pointer.py; + event.ctrl_down = wlc.keyboard.control; + event.shift_down = wlc.keyboard.shift; + + (*wlc.update)(event); + } + } } void ku_wayland_pointer_handle_axis_discrete(void* data, struct wl_pointer* wl_pointer, uint axis, int discrete) @@ -1291,7 +1347,10 @@ static void keyboard_enter(void* data, struct wl_keyboard* wl_keyboard, uint32_t static void keyboard_leave(void* data, struct wl_keyboard* wl_keyboard, uint32_t serial, struct wl_surface* surface) { - /* mt_log_debug("keyboard leave"); */ + /* stop repeater */ + + struct itimerspec spec = {0}; + timerfd_settime(wlc.keyboard.rep_timer_fd, 0, &spec, NULL); } static void keyboard_key(void* data, struct wl_keyboard* wl_keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t _key_state) @@ -1327,6 +1386,18 @@ static void keyboard_key(void* data, struct wl_keyboard* wl_keyboard, uint32_t s } } + /* send key down/up event */ + + ku_event_t event = init_event(); + event.keycode = sym; + event.type = key_state == WL_KEYBOARD_KEY_STATE_PRESSED ? KU_EVENT_KEY_DOWN : KU_EVENT_KEY_UP; + event.ctrl_down = wlc.keyboard.control; + event.shift_down = wlc.keyboard.shift; + event.repeat = wlc.keyboard.rep_event.repeat; + (*wlc.update)(event); + + wlc.keyboard.rep_event = event; + if (key_state == WL_KEYBOARD_KEY_STATE_PRESSED && wlc.keyboard.rep_period > 0) { /* start repeater */ @@ -1342,23 +1413,8 @@ static void keyboard_key(void* data, struct wl_keyboard* wl_keyboard, uint32_t s struct itimerspec spec = {0}; timerfd_settime(wlc.keyboard.rep_timer_fd, 0, &spec, NULL); - - ku_event_t event = wlc.keyboard.rep_event; - event.type = KU_EVENT_KEY_UP; - event.repeat = 1; - (*wlc.update)(event); + wlc.keyboard.rep_event.repeat = 0; } - - /* send key down/up event */ - - ku_event_t event = init_event(); - event.keycode = sym; - event.type = key_state == WL_KEYBOARD_KEY_STATE_PRESSED ? KU_EVENT_KEY_DOWN : KU_EVENT_KEY_UP; - event.ctrl_down = wlc.keyboard.control; - event.shift_down = wlc.keyboard.shift; - (*wlc.update)(event); - - wlc.keyboard.rep_event = event; } static void keyboard_repeat() @@ -1544,29 +1600,51 @@ static void ku_wayland_seat_handle_capabilities(void* data, struct wl_seat* wl_s { /* mt_log_debug("seat handle capabilities %i", caps); */ - if (caps & WL_SEAT_CAPABILITY_KEYBOARD) + if ((caps & WL_SEAT_CAPABILITY_KEYBOARD)) { - wlc.keyboard.kbd = wl_seat_get_keyboard(wl_seat); - wl_keyboard_add_listener(wlc.keyboard.kbd, &keyboard_listener, NULL); + if (wlc.keyboard.kbd == NULL) + { + wlc.keyboard.kbd = wl_seat_get_keyboard(wl_seat); + wl_keyboard_add_listener(wlc.keyboard.kbd, &keyboard_listener, NULL); + } + } + else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD)) + { + if (wlc.keyboard.kbd) + { + wl_keyboard_destroy(wlc.keyboard.kbd); + wlc.keyboard.kbd = NULL; + } } if (caps & WL_SEAT_CAPABILITY_POINTER) { - wlc.ipointer = wl_seat_get_pointer(wl_seat); - wl_pointer_add_listener(wlc.ipointer, &pointer_listener, NULL); - - if (wlc.pointer_manager) + if (wlc.ipointer == NULL) { - wlc.pinch_gesture = zwp_pointer_gestures_v1_get_pinch_gesture(wlc.pointer_manager, wlc.ipointer); - zwp_pointer_gesture_pinch_v1_add_listener(wlc.pinch_gesture, &gesture_pinch_listener, wl_seat); + wlc.ipointer = wl_seat_get_pointer(wl_seat); + wl_pointer_add_listener(wlc.ipointer, &pointer_listener, NULL); - if (wlc.pointer_manager_version >= ZWP_POINTER_GESTURES_V1_GET_HOLD_GESTURE_SINCE_VERSION) + if (wlc.pointer_manager) { - wlc.hold_gesture = zwp_pointer_gestures_v1_get_hold_gesture(wlc.pointer_manager, wlc.ipointer); - zwp_pointer_gesture_hold_v1_add_listener(wlc.hold_gesture, &gesture_hold_listener, wl_seat); + wlc.pinch_gesture = zwp_pointer_gestures_v1_get_pinch_gesture(wlc.pointer_manager, wlc.ipointer); + zwp_pointer_gesture_pinch_v1_add_listener(wlc.pinch_gesture, &gesture_pinch_listener, wl_seat); + + if (wlc.pointer_manager_version >= ZWP_POINTER_GESTURES_V1_GET_HOLD_GESTURE_SINCE_VERSION) + { + wlc.hold_gesture = zwp_pointer_gestures_v1_get_hold_gesture(wlc.pointer_manager, wlc.ipointer); + zwp_pointer_gesture_hold_v1_add_listener(wlc.hold_gesture, &gesture_hold_listener, wl_seat); + } } } } + else if (!(caps & WL_SEAT_CAPABILITY_POINTER)) + { + if (wlc.ipointer) + { + wl_pointer_destroy(wlc.ipointer); + wlc.ipointer = NULL; + } + } } static void ku_wayland_seat_handle_name(void* data, struct wl_seat* wl_seat, const char* name) @@ -1598,7 +1676,7 @@ static void ku_wayland_handle_global( } else if (strcmp(interface, wl_seat_interface.name) == 0) { - wlc.seat = wl_registry_bind(registry, name, &wl_seat_interface, 4); + wlc.seat = wl_registry_bind(registry, name, &wl_seat_interface, 6); wl_seat_add_listener(wlc.seat, &seat_listener, NULL); } else if (strcmp(interface, wl_shm_interface.name) == 0) @@ -1610,7 +1688,8 @@ static void ku_wayland_handle_global( } else if (strcmp(interface, wl_output_interface.name) == 0) { - if (wlc.monitor_count >= 16) return; + if (wlc.monitor_count >= 16) + return; struct monitor_info* monitor = malloc(sizeof(struct monitor_info)); memset(monitor->name, 0, MAX_MONITOR_NAME_LEN); @@ -1646,15 +1725,15 @@ static void ku_wayland_handle_global( } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) { - wlc.xdg_wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1); + wlc.xdg_wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, 2); xdg_wm_base_add_listener(wlc.xdg_wm_base, &xdg_wm_base_listener, NULL); } else if (strcmp(interface, zwp_pointer_gestures_v1_interface.name) == 0) { + wlc.pointer_manager_version = version; if (version >= 3) { - wlc.pointer_manager_version = version; - wlc.pointer_manager = wl_registry_bind(registry, name, &zwp_pointer_gestures_v1_interface, 3); + wlc.pointer_manager = wl_registry_bind(registry, name, &zwp_pointer_gestures_v1_interface, 3); } } } diff --git a/src/kinetic_ui/ku_css.c b/src/kinetic_ui/ku_css.c index da00dfd..8e84294 100644 --- a/src/kinetic_ui/ku_css.c +++ b/src/kinetic_ui/ku_css.c @@ -109,7 +109,7 @@ prop_t* ku_css_new_parse(char* css) ku_css_analyze_classes(css, props); - for (int i = 0; i < cnt; i++) + for (uint32_t i = 0; i < cnt; i++) { // prop_t p = props[i]; // printf("extracted prop %.*s %.*s %.*s\n", p.class.len, css + p.class.pos, p.key.len, css + p.key.pos, p.value.len, css + p.value.pos); diff --git a/src/kinetic_ui/ku_draw.c b/src/kinetic_ui/ku_draw.c index a8989d0..a1c5bdb 100644 --- a/src/kinetic_ui/ku_draw.c +++ b/src/kinetic_ui/ku_draw.c @@ -22,6 +22,7 @@ void ku_draw_rect(ku_bitmap_t* bm, int sx, int sy, int w, int h, uint32_t color, void ku_draw_blend_argb(ku_bitmap_t* bm, int nx, int ny, ku_bitmap_t* nbm); void ku_draw_insert(ku_bitmap_t* base, ku_bitmap_t* src, int sx, int sy); void ku_draw_insert_argb(ku_bitmap_t* base, uint8_t* src, int w, int h, int sx, int sy); +void ku_draw_insert_rgba(ku_bitmap_t* base, uint8_t* src, int w, int h, int sx, int sy); void ku_draw_insert_rgb(ku_bitmap_t* base, uint8_t* src, int w, int h, int sx, int sy); void ku_draw_blend_8(ku_bitmap_t* bm, int nx, int ny, uint32_t color, unsigned char* ndata, int nw, int nh); void ku_draw_blend_8_1(ku_bitmap_t* bm, int nx, int ny, uint32_t color, unsigned char* ndata, int nw, int nh); @@ -35,6 +36,7 @@ void ku_draw_insert_bitmap(ku_bitmap_t* base, ku_bitmap_t* src, int sx, int sy); #if __INCLUDE_LEVEL__ == 0 +#include "mt_log.c" #include void ku_draw_circle(ku_bitmap_t* bitmap, float cx, float cy, float r, float edge, uint32_t c) @@ -580,9 +582,11 @@ void ku_draw_insert_rgb(ku_bitmap_t* base, uint8_t* src, int w, int h, int sx, i void ku_draw_insert_argb(ku_bitmap_t* base, uint8_t* src, int w, int h, int sx, int sy) { int bx = sx + w; - if (bx > base->w) bx = base->w; + if (bx > base->w) + bx = base->w; int by = sy + h; - if (by > base->h) by = base->h; + if (by > base->h) + by = base->h; uint8_t* sdata = src; // src data uint8_t* bdata = base->data; // base data @@ -610,18 +614,59 @@ void ku_draw_insert_argb(ku_bitmap_t* base, uint8_t* src, int w, int h, int sx, } } +void ku_draw_insert_rgba(ku_bitmap_t* base, uint8_t* src, int w, int h, int sx, int sy) +{ + int bx = sx + w; + if (bx > base->w) + bx = base->w; + int by = sy + h; + if (by > base->h) + by = base->h; + + uint8_t* sdata = src; // src data + uint8_t* bdata = base->data; // base data + + for (int y = sy; y < by; y++) + { + for (int x = sx; x < bx; x++) + { + if (x > -1 && y > -1) + { + int si = ((y - sy) * w + (x - sx)) * 4; // src index + int bi = (y * base->w + x) * 4; // base index + + uint8_t a = sdata[si]; + uint8_t r = sdata[si + 1]; + uint8_t g = sdata[si + 2]; + uint8_t b = sdata[si + 3]; + + bdata[bi] = r; + bdata[bi + 1] = g; + bdata[bi + 2] = b; + bdata[bi + 3] = a; + } + } + } +} + void ku_draw_insert_bitmap(ku_bitmap_t* base, ku_bitmap_t* src, int sx, int sy) { - if (sx < 0) sx = 0; - if (sy < 0) sy = 0; - if (sx >= base->w) return; - if (sy >= base->h) return; + if (sx < 0) + sx = 0; + if (sy < 0) + sy = 0; + if (sx >= base->w) + return; + if (sy >= base->h) + return; int ex = sx + src->w; int ey = sy + src->h; - if (ex >= base->w) ex = base->w; - if (ey >= base->h) ey = base->h; + if (ex >= base->w) + ex = base->w; + if (ey >= base->h) + ey = base->h; uint32_t* b = (uint32_t*) base->data; uint32_t* s = (uint32_t*) src->data; @@ -853,7 +898,8 @@ uint32_t getpixel(image_t* image, unsigned int x, unsigned int y) float max(float a, float b) { return (a < b) ? a : b; -}; +} + float lerp(float s, float e, float t) { return s + (e - s) * t; @@ -889,9 +935,9 @@ void ku_draw_scale(ku_bitmap_t* srcbmp, ku_bitmap_t* dstbmp) int newWidth = dst->w; int newHeight = dst->h; int x, y; - for (x = 0, y = 0; y < newHeight; x++) + for (x = 0, y = 0; y < newHeight - 1; x++) { - if (x > newWidth) + if (x == newWidth) { x = 0; y++; @@ -899,10 +945,11 @@ void ku_draw_scale(ku_bitmap_t* srcbmp, ku_bitmap_t* dstbmp) // float gx = x / (float)(newWidth) * (src->w - 1); // float gy = y / (float)(newHeight) * (src->h - 1); // Image should be clamped at the edges and not scaled. - float gx = max(x / (float) (newWidth) * (src->w) - 0.5f, src->w - 1); - float gy = max(y / (float) (newHeight) * (src->h) - 0.5, src->h - 1); + float gx = max(x / (float) (newWidth) * (src->w) - 0.5f, src->w - 2); + float gy = max(y / (float) (newHeight) * (src->h) - 0.5, src->h - 2); int gxi = (int) gx; int gyi = (int) gy; + uint32_t result = 0; uint32_t c00 = getpixel(src, gxi, gyi); uint32_t c10 = getpixel(src, gxi + 1, gyi); @@ -914,6 +961,7 @@ void ku_draw_scale(ku_bitmap_t* srcbmp, ku_bitmap_t* dstbmp) //((uint8_t*)&result)[i] = blerp( ((uint8_t*)&c00)[i], ((uint8_t*)&c10)[i], ((uint8_t*)&c01)[i], ((uint8_t*)&c11)[i], gxi - gx, gyi - gy); // this is shady result |= (uint8_t) blerp(getByte(c00, i), getByte(c10, i), getByte(c01, i), getByte(c11, i), gx - gxi, gy - gyi) << (8 * i); } + putpixel(dst, x, y, result); } } diff --git a/src/kinetic_ui/ku_event.c b/src/kinetic_ui/ku_event.c index 8926156..a56da75 100644 --- a/src/kinetic_ui/ku_event.c +++ b/src/kinetic_ui/ku_event.c @@ -18,11 +18,15 @@ enum evtype KU_EVENT_MOUSE_DOWN_OUT, KU_EVENT_MOUSE_UP_OUT, KU_EVENT_SCROLL, + KU_EVENT_SCROLL_X_END, + KU_EVENT_SCROLL_Y_END, KU_EVENT_KEY_DOWN, KU_EVENT_KEY_UP, KU_EVENT_TEXT, KU_EVENT_WINDOW_SHOWN, KU_EVENT_PINCH, + KU_EVENT_HOLD_START, + KU_EVENT_HOLD_END, KU_EVENT_STDIN, KU_EVENT_FOCUS, KU_EVENT_UNFOCUS, @@ -90,13 +94,14 @@ ku_event_t ku_event_read(FILE* file); #include "mt_log.c" /* frame type x y w h dx dy ratio drag dclick button time time_frame keycode repeat ctrl_down shift_down text */ -char* ku_event_format = "%i %i %i %i %i %i %f %f %f %i %i %i %u %f %u %i %i %i %s\n"; +char* ku_print_format = "%i %i %i %i %i %i %f %f %f %i %i %i %u %f %u %i %i %i '%s'\n"; +char* ku_scan_format = "%i %i %i %i %i %i %f %f %f %i %i %i %u %f %u %i %i %i '%[^']'\n"; void ku_event_write(FILE* file, ku_event_t ev) { fprintf( file, - ku_event_format, + ku_print_format, ev.frame, ev.type, ev.x, @@ -123,7 +128,7 @@ ku_event_t ku_event_read(FILE* file) ku_event_t ev = {0}; int res = fscanf( file, - ku_event_format, + ku_scan_format, &ev.frame, &ev.type, &ev.x, @@ -144,7 +149,8 @@ ku_event_t ku_event_read(FILE* file) &ev.shift_down, &ev.text); - if (res < 0) mt_log_error("Couldn't scanf file"); + if (res < 0) + mt_log_error("Couldn't scanf file"); return ev; } diff --git a/src/kinetic_ui/ku_fontconfig.c b/src/kinetic_ui/ku_fontconfig.c index ffe93d2..f1f2f02 100644 --- a/src/kinetic_ui/ku_fontconfig.c +++ b/src/kinetic_ui/ku_fontconfig.c @@ -9,11 +9,11 @@ void ku_fontconfig_delete(); #if __INCLUDE_LEVEL__ == 0 #include -#ifdef __linux__ -#include +#ifdef __linux__ + #include #endif -#define _POSIX_C_SOURCE 200112L +// #define _POSIX_C_SOURCE 200112L #include "mt_map.c" #include "mt_memory.c" #include "mt_string.c" @@ -27,7 +27,8 @@ char* ku_fontconfig_path(char* face_desc) char* filename = NULL; if (face_desc) { - if (ku_fontconfig_cache == NULL) ku_fontconfig_cache = MNEW(); + if (ku_fontconfig_cache == NULL) + ku_fontconfig_cache = MNEW(); filename = MGET(ku_fontconfig_cache, face_desc); if (filename == NULL) diff --git a/src/kinetic_ui/ku_gen_css.c b/src/kinetic_ui/ku_gen_css.c index 20fa4c1..c6a9818 100644 --- a/src/kinetic_ui/ku_gen_css.c +++ b/src/kinetic_ui/ku_gen_css.c @@ -23,7 +23,7 @@ void ku_gen_css_apply_style(ku_view_t* view, mt_map_t* style, char* imgpath) mt_vector_t* keys = VNEW(); // REL 0 mt_map_keys(style, keys); - for (int index = 0; index < keys->length; index++) + for (size_t index = 0; index < keys->length; index++) { char* key = keys->data[index]; char* val = MGET(style, key); @@ -69,23 +69,34 @@ void ku_gen_css_apply_style(ku_view_t* view, mt_map_t* style, char* imgpath) } else if (strcmp(key, "word-wrap") == 0) { - if (strstr(val, "normal") != NULL) view->style.word_wrap = 0; - if (strstr(val, "break-word") != NULL) view->style.word_wrap = 1; - if (strstr(val, "initial") != NULL) view->style.word_wrap = 2; - if (strstr(val, "inherit") != NULL) view->style.word_wrap = 3; + if (strstr(val, "normal") != NULL) + view->style.word_wrap = 0; + if (strstr(val, "break-word") != NULL) + view->style.word_wrap = 1; + if (strstr(val, "initial") != NULL) + view->style.word_wrap = 2; + if (strstr(val, "inherit") != NULL) + view->style.word_wrap = 3; } else if (strcmp(key, "text-align") == 0) { - if (strstr(val, "left") != NULL) view->style.text_align = 0; - if (strstr(val, "center") != NULL) view->style.text_align = 1; - if (strstr(val, "right") != NULL) view->style.text_align = 2; - if (strstr(val, "justify") != NULL) view->style.text_align = 3; + if (strstr(val, "left") != NULL) + view->style.text_align = 0; + if (strstr(val, "center") != NULL) + view->style.text_align = 1; + if (strstr(val, "right") != NULL) + view->style.text_align = 2; + if (strstr(val, "justify") != NULL) + view->style.text_align = 3; } else if (strcmp(key, "vertical-align") == 0) { - if (strstr(val, "middle") != NULL) view->style.vertical_align = 0; - if (strstr(val, "top") != NULL) view->style.vertical_align = 1; - if (strstr(val, "bottom") != NULL) view->style.vertical_align = 2; + if (strstr(val, "middle") != NULL) + view->style.vertical_align = 0; + if (strstr(val, "top") != NULL) + view->style.vertical_align = 1; + if (strstr(val, "bottom") != NULL) + view->style.vertical_align = 2; } else if (strcmp(key, "width") == 0) { @@ -253,7 +264,8 @@ void ku_gen_css_apply_style(ku_view_t* view, mt_map_t* style, char* imgpath) view->style.shadow_blur = atoi(val); char* color = strstr(val + 1, " "); - if (color) view->style.shadow_color = (int) strtol(color + 2, NULL, 16); + if (color) + view->style.shadow_color = (int) strtol(color + 2, NULL, 16); } else if (strcmp(key, "align-items") == 0) { @@ -269,14 +281,6 @@ void ku_gen_css_apply_style(ku_view_t* view, mt_map_t* style, char* imgpath) view->style.cjustify = JC_CENTER; } } - // TODO remove non standard CSS - else if (strcmp(key, "blocks") == 0) - { - if (strcmp(val, "no") == 0) - { - view->blocks_touch = 0; - } - } } /* printf("style for %s: ", view->id); */ /* ku_view_desc_style(view->style); */ @@ -290,7 +294,7 @@ void ku_gen_css_apply(mt_vector_t* views, char* csspath, char* imgpath) mt_map_t* styles = ku_css_new(csspath); mt_map_t* style; - for (int index = 0; index < views->length; index++) + for (size_t index = 0; index < views->length; index++) { ku_view_t* view = views->data[index]; diff --git a/src/kinetic_ui/ku_gen_type.c b/src/kinetic_ui/ku_gen_type.c index 8313a39..72b2137 100644 --- a/src/kinetic_ui/ku_gen_type.c +++ b/src/kinetic_ui/ku_gen_type.c @@ -17,7 +17,7 @@ void ku_gen_type_apply(mt_vector_t* views, void (*button_event)(vh_button_event_ void ku_gen_type_apply(mt_vector_t* views, void (*button_event)(vh_button_event_t), void (*slider_event)(vh_slider_event_t)) { - for (int index = 0; index < views->length; index++) + for (size_t index = 0; index < views->length; index++) { ku_view_t* view = views->data[index]; diff --git a/src/kinetic_ui/ku_html.c b/src/kinetic_ui/ku_html.c index dec966c..41ef2d5 100644 --- a/src/kinetic_ui/ku_html.c +++ b/src/kinetic_ui/ku_html.c @@ -134,7 +134,7 @@ html_range_t ku_html_extract_string(char* str, uint32_t pos, uint32_t len) int start = 0; int end = 0; int in = 0; - for (int i = pos; i < pos + len; i++) + for (uint32_t i = pos; i < pos + len; i++) { char c = str[i]; if (c == '"') @@ -174,7 +174,7 @@ html_range_t ku_html_extract_value(tag_t tag, char* key, char* html) void ku_html_analyze_tags(char* html, tag_t* tags, uint32_t count) { int l = 0; // level - for (int i = 0; i < count; i++) + for (uint32_t i = 0; i < count; i++) { tags[i].level = l++; diff --git a/src/kinetic_ui/ku_recorder.c b/src/kinetic_ui/ku_recorder.c index fdb7615..696c077 100644 --- a/src/kinetic_ui/ku_recorder.c +++ b/src/kinetic_ui/ku_recorder.c @@ -25,7 +25,7 @@ enum ku_recorder_mode_t struct ku_recorder_t { - int index; + size_t index; FILE* file; mt_vector_t* eventqueue; void (*update)(ku_event_t); @@ -51,7 +51,8 @@ void ku_recorder_replay(char* path) { kurec.mode = KU_REC_MODE_REPLAY; FILE* file = fopen(path, "r"); - if (!file) printf("evrec player : cannot open file %s\n", path); + if (!file) + printf("evrec player : cannot open file %s\n", path); kurec.file = file; @@ -59,10 +60,11 @@ void ku_recorder_replay(char* path) { ku_event_t ev = ku_event_read(file); VADDR(kurec.eventqueue, HEAP(ev)); - if (feof(file)) break; + if (feof(file)) + break; } - printf("%i events read\n", kurec.eventqueue->length); + printf("%li events read\n", kurec.eventqueue->length); } void ku_recorder_destroy() @@ -82,7 +84,7 @@ void ku_recorder_update_record(ku_event_t ev) if (ev.type == KU_EVENT_FRAME || ev.type == KU_EVENT_TIME || ev.type == KU_EVENT_WINDOW_SHOWN) { /* record and send waiting events */ - for (int index = 0; index < kurec.eventqueue->length; index++) + for (size_t index = 0; index < kurec.eventqueue->length; index++) { ku_event_t* event = (ku_event_t*) kurec.eventqueue->data[index]; event->frame = ev.frame; diff --git a/src/kinetic_ui/ku_renderer_egl.c b/src/kinetic_ui/ku_renderer_egl.c index 2c5f422..fdd9f37 100644 --- a/src/kinetic_ui/ku_renderer_egl.c +++ b/src/kinetic_ui/ku_renderer_egl.c @@ -48,7 +48,7 @@ void ku_renderer_egl_render(mt_vector_t* views, ku_bitmap_t* bitmap, ku_rect_t d masks[0] = (ku_rect_t){0, 0, bitmap->w, bitmap->h}; dirty = masks[0]; - for (int i = 0; i < views->length; i++) + for (size_t i = 0; i < views->length; i++) { ku_view_t* view = views->data[i]; @@ -85,7 +85,8 @@ void ku_renderer_egl_render(mt_vector_t* views, ku_bitmap_t* bitmap, ku_rect_t d mask = srcmsk; - if (srcmsk.x != srcmsk.z) ku_gl_render_quad(bitmap, i, mask); + if (srcmsk.x != srcmsk.z) + ku_gl_render_quad(bitmap, i, mask); } else { @@ -106,7 +107,7 @@ void ku_renderer_egl_render(mt_vector_t* views, ku_bitmap_t* bitmap, ku_rect_t d /* draw views */ - /* for (int i = 0; i < views->length; i++) */ + /* for (size_t i = 0; i < views->length; i++) */ /* { */ /* ku_view_t* view = views->data[i]; */ diff --git a/src/kinetic_ui/ku_renderer_soft.c b/src/kinetic_ui/ku_renderer_soft.c index 8e6b5da..5030f72 100644 --- a/src/kinetic_ui/ku_renderer_soft.c +++ b/src/kinetic_ui/ku_renderer_soft.c @@ -36,7 +36,7 @@ void ku_renderer_software_render(mt_vector_t* views, ku_bitmap_t* bitmap, ku_rec /* draw views into bitmap */ - for (int i = 0; i < views->length; i++) + for (size_t i = 0; i < views->length; i++) { ku_view_t* view = views->data[i]; diff --git a/src/kinetic_ui/ku_text.c b/src/kinetic_ui/ku_text.c index 89cf689..558effc 100644 --- a/src/kinetic_ui/ku_text.c +++ b/src/kinetic_ui/ku_text.c @@ -120,7 +120,7 @@ void ku_text_destroy() mt_vector_t* paths = VNEW(); // REL 0 mt_map_keys(txt_ft.fonts, paths); - for (int i = 0; i < paths->length; i++) + for (size_t i = 0; i < paths->length; i++) { char* path = paths->data[i]; @@ -426,7 +426,8 @@ void ku_text_shift_glyphs(glyph_t* glyphs, int count, textstyle_t style) void ku_text_render_glyph(glyph_t g, textstyle_t style, ku_bitmap_t* bitmap) { - if ((style.backcolor & 0xFF) > 0) ku_draw_rect(bitmap, 0, 0, bitmap->w, bitmap->h, style.backcolor, 0); + if ((style.backcolor & 0xFF) > 0) + ku_draw_rect(bitmap, 0, 0, bitmap->w, bitmap->h, style.backcolor, 0); mt_wrapper_t* facewrp = MGET(txt_ft.fonts, style.font); mt_wrapper_t* libwrp = MGET(txt_ft.libs, style.font); @@ -435,7 +436,8 @@ void ku_text_render_glyph(glyph_t g, textstyle_t style, ku_bitmap_t* bitmap) ku_text_font_load(style.font); facewrp = MGET(txt_ft.fonts, style.font); libwrp = MGET(txt_ft.libs, style.font); - if (!facewrp) return; + if (!facewrp) + return; } FT_Face font = facewrp->data; FT_Library library = libwrp->data; @@ -443,7 +445,7 @@ void ku_text_render_glyph(glyph_t g, textstyle_t style, ku_bitmap_t* bitmap) // don't write bitmap in case of empty glyphs ( space ) if (g.w > 0 && g.h > 0) { - int size = g.w * g.h; + size_t size = g.w * g.h; // increase glyph baking bitMap size if needed if (size > txt_ft.gcount) @@ -453,10 +455,16 @@ void ku_text_render_glyph(glyph_t g, textstyle_t style, ku_bitmap_t* bitmap) } int error = FT_Load_Char(font, g.cp, FT_LOAD_RENDER); - if (error) { printf("FT Load Char error\n"); } + if (error) + { + printf("FT Load Char error\n"); + } error = FT_Render_Glyph(font->glyph, FT_RENDER_MODE_NORMAL); - if (error) { printf("FT_Render_Glyph error\n"); } + if (error) + { + printf("FT_Render_Glyph error\n"); + } FT_Bitmap fontmap = font->glyph->bitmap; @@ -485,7 +493,8 @@ void ku_text_render_glyph(glyph_t g, textstyle_t style, ku_bitmap_t* bitmap) void ku_text_render_glyphs(glyph_t* glyphs, int count, textstyle_t style, ku_bitmap_t* bitmap) { - if ((style.backcolor & 0xFF) > 0) ku_draw_rect(bitmap, 0, 0, bitmap->w, bitmap->h, style.backcolor, 0); + if ((style.backcolor & 0xFF) > 0) + ku_draw_rect(bitmap, 0, 0, bitmap->w, bitmap->h, style.backcolor, 0); mt_wrapper_t* facewrp = MGET(txt_ft.fonts, style.font); mt_wrapper_t* libwrp = MGET(txt_ft.libs, style.font); @@ -494,7 +503,8 @@ void ku_text_render_glyphs(glyph_t* glyphs, int count, textstyle_t style, ku_bit ku_text_font_load(style.font); facewrp = MGET(txt_ft.fonts, style.font); libwrp = MGET(txt_ft.libs, style.font); - if (!facewrp) return; + if (!facewrp) + return; } FT_Face font = facewrp->data; FT_Library library = libwrp->data; @@ -507,7 +517,7 @@ void ku_text_render_glyphs(glyph_t* glyphs, int count, textstyle_t style, ku_bit // don't write bitmap in case of empty glyphs ( space ) if (g.w > 0 && g.h > 0) { - int size = g.w * g.h; + size_t size = g.w * g.h; // increase glyph baking bitmap size if needed if (size > txt_ft.gcount) @@ -517,10 +527,16 @@ void ku_text_render_glyphs(glyph_t* glyphs, int count, textstyle_t style, ku_bit } int error = FT_Load_Char(font, g.cp, FT_LOAD_RENDER); - if (error) { printf("FT Load Char error\n"); } + if (error) + { + printf("FT Load Char error\n"); + } error = FT_Render_Glyph(font->glyph, FT_RENDER_MODE_NORMAL); - if (error) { printf("FT_Render_Glyph error\n"); } + if (error) + { + printf("FT_Render_Glyph error\n"); + } FT_Bitmap fontmap = font->glyph->bitmap; @@ -579,7 +595,7 @@ void ku_text_render(char* text, textstyle_t style, ku_bitmap_t* bitmap) glyph_t* glyphs = malloc(sizeof(glyph_t) * count); // REL 0 utf8_int32_t cp; - for (int i = 0; i < count; i++) + for (size_t i = 0; i < count; i++) { part = utf8codepoint(part, &cp); glyphs[i].cp = cp; @@ -601,7 +617,7 @@ void ku_text_measure(char* text, textstyle_t style, int w, int h, int* nw, int* glyph_t* glyphs = malloc(sizeof(glyph_t) * count); // REL 0 utf8_int32_t cp; - for (int i = 0; i < count; i++) + for (size_t i = 0; i < count; i++) { part = utf8codepoint(part, &cp); glyphs[i].cp = cp; diff --git a/src/kinetic_ui/ku_view.c b/src/kinetic_ui/ku_view.c index e9857cb..36e3e6c 100644 --- a/src/kinetic_ui/ku_view.c +++ b/src/kinetic_ui/ku_view.c @@ -136,15 +136,6 @@ struct _ku_view_t { char rearrange; /* subview structure changed, window needs rearrange */ - char needs_key; /* accepts key events */ - char needs_text; /* accepts text events */ - char needs_time; /* accepts time events */ - char needs_touch; /* accepts touch events */ - - char blocks_key; /* blocks key events */ - char blocks_touch; /* blocks touch events */ - char blocks_scroll; /* blocks scroll events */ - char* id; /* identifier for handling view */ char* class; /* css class(es) */ char* script; /* script */ @@ -158,10 +149,10 @@ struct _ku_view_t vstyle_t style; texture_t texture; - void (*handler)(ku_view_t*, ku_event_t); /* view handler for view */ - void (*tex_gen)(ku_view_t*); /* texture generator for view */ - void* handler_data; /* data for event handler */ - void* tex_gen_data; /* data for texture generator */ + int (*evt_han)(ku_view_t*, ku_event_t); /* event handler for view */ + int (*tex_gen)(ku_view_t*); /* texture generator for view */ + void* evt_han_data; /* data for event handler */ + void* tex_gen_data; /* data for texture generator */ }; ku_view_t* ku_view_new(char* id, ku_rect_t frame); @@ -183,7 +174,6 @@ void ku_view_set_masked(ku_view_t* view, char masked); void ku_view_set_frame(ku_view_t* view, ku_rect_t frame); void ku_view_set_region(ku_view_t* view, ku_rect_t frame); void ku_view_set_style(ku_view_t* view, vstyle_t style); -void ku_view_set_block_touch(ku_view_t* view, char block, char recursive); void ku_view_set_texture_bmp(ku_view_t* view, ku_bitmap_t* tex); void ku_view_set_texture_alpha(ku_view_t* view, float alpha, char recur); void ku_view_invalidate_texture(ku_view_t* view); @@ -210,16 +200,30 @@ void ku_view_del(void* pointer) { ku_view_t* view = (ku_view_t*) pointer; - if (view->handler_data) REL(view->handler_data); - if (view->tex_gen_data) REL(view->tex_gen_data); - - if (view->texture.bitmap) REL(view->texture.bitmap); // not all views has texture - if (view->class) REL(view->class); - if (view->type) REL(view->type); - if (view->text) REL(view->text); - if (view->script) REL(view->script); + if (view->evt_han_data) + REL(view->evt_han_data); + if (view->tex_gen_data) + REL(view->tex_gen_data); + + if (view->texture.bitmap) + REL(view->texture.bitmap); // not all views has texture + if (view->class) + REL(view->class); + if (view->type) + REL(view->type); + if (view->text) + REL(view->text); + if (view->script) + REL(view->script); REL(view->id); + + for (size_t index = 0; index < view->views->length; index++) + { + ku_view_t* sview = view->views->data[index]; + sview->parent = NULL; + } + REL(view->views); } @@ -232,10 +236,8 @@ ku_view_t* ku_view_new(char* id, ku_rect_t frame) view->frame.global = frame; view->texture.alpha = 1.0; view->texture.resizable = 1; - view->needs_touch = 1; - // view->blocks_touch = 1; - view->frame.region = (ku_rect_t){-1, -1, -1. - 1}; + view->frame.region = (ku_rect_t){-1, -1, -1, -1}; // reset margins @@ -289,7 +291,7 @@ void ku_view_set_masked(ku_view_t* view, char masked) void ku_view_add_subview(ku_view_t* view, ku_view_t* subview) { - for (int i = 0; i < view->views->length; i++) + for (size_t i = 0; i < view->views->length; i++) { ku_view_t* sview = view->views->data[i]; if (strcmp(sview->id, subview->id) == 0) @@ -313,7 +315,7 @@ void ku_view_add_subview(ku_view_t* view, ku_view_t* subview) void ku_view_insert_subview(ku_view_t* view, ku_view_t* subview, uint32_t index) { - for (int i = 0; i < view->views->length; i++) + for (size_t i = 0; i < view->views->length; i++) { ku_view_t* sview = view->views->data[i]; if (strcmp(sview->id, subview->id) == 0) @@ -337,6 +339,8 @@ void ku_view_insert_subview(ku_view_t* view, ku_view_t* subview, uint32_t index) void ku_view_remove_subview(ku_view_t* view, ku_view_t* subview) { + ku_view_set_parent(subview, NULL); + char success = VREM(view->views, subview); if (success == 1) @@ -345,8 +349,6 @@ void ku_view_remove_subview(ku_view_t* view, ku_view_t* subview) ku_view_t* tview = view; while (tview->parent != NULL) tview = tview->parent; tview->rearrange = 1; - - ku_view_set_parent(subview, NULL); } } @@ -357,7 +359,7 @@ void ku_view_remove_from_parent(ku_view_t* view) void ku_view_set_parent(ku_view_t* view, ku_view_t* parent) { - view->parent = parent; + if (view) view->parent = parent; } void ku_view_coll_touched(ku_view_t* view, ku_event_t ev, mt_vector_t* queue) @@ -367,8 +369,8 @@ void ku_view_coll_touched(ku_view_t* view, ku_event_t ev, mt_vector_t* queue) ev.y <= view->frame.global.y + view->frame.global.h && ev.y >= view->frame.global.y) { - mt_vector_add_unique_data(queue, view); - for (int i = 0; i < view->views->length; i++) + mt_vector_add(queue, view); + for (size_t i = 0; i < view->views->length; i++) { ku_view_t* v = view->views->data[i]; ku_view_coll_touched(v, ev, queue); @@ -378,31 +380,35 @@ void ku_view_coll_touched(ku_view_t* view, ku_event_t ev, mt_vector_t* queue) ku_view_t* ku_view_get_subview(ku_view_t* view, char* id) { - if (strcmp(view->id, id) == 0) return view; - for (int i = 0; i < view->views->length; i++) + if (strcmp(view->id, id) == 0) + return view; + for (size_t i = 0; i < view->views->length; i++) { ku_view_t* sv = view->views->data[i]; ku_view_t* re = ku_view_get_subview(sv, id); - if (re) return re; + if (re) + return re; } return NULL; } void ku_view_evt(ku_view_t* view, ku_event_t ev) { - for (int i = 0; i < view->views->length; i++) + for (size_t i = 0; i < view->views->length; i++) { ku_view_t* v = view->views->data[i]; ku_view_evt(v, ev); } - if (view->handler) (*view->handler)(view, ev); + if (view->evt_han) + (*view->evt_han)(view, ev); } void ku_view_calc_global(ku_view_t* view) { ku_rect_t frame_parent = {0}; - if (view->parent != NULL) frame_parent = view->parent->frame.global; + if (view->parent != NULL) + frame_parent = view->parent->frame.global; ku_rect_t frame_global = view->frame.local; ku_rect_t old_global = view->frame.global; @@ -413,11 +419,12 @@ void ku_view_calc_global(ku_view_t* view) // notify about pos change if (fabs(frame_global.x - old_global.x) > 0.001 || - fabs(frame_global.y - old_global.y) > 0.001) view->frame.pos_changed = 1; + fabs(frame_global.y - old_global.y) > 0.001) + view->frame.pos_changed = 1; view->frame.global = frame_global; - for (int i = 0; i < view->views->length; i++) + for (size_t i = 0; i < view->views->length; i++) { ku_view_t* v = view->views->data[i]; ku_view_calc_global(v); @@ -452,23 +459,10 @@ void ku_view_set_region(ku_view_t* view, ku_rect_t region) view->frame.reg_changed = 1; } -void ku_view_set_block_touch(ku_view_t* view, char block, char recursive) -{ - view->blocks_touch = block; - - if (recursive) - { - for (int i = 0; i < view->views->length; i++) - { - ku_view_t* v = view->views->data[i]; - ku_view_set_block_touch(v, block, recursive); - } - } -} - void ku_view_set_texture_bmp(ku_view_t* view, ku_bitmap_t* bitmap) { - if (view->texture.bitmap) REL(view->texture.bitmap); + if (view->texture.bitmap) + REL(view->texture.bitmap); view->texture.bitmap = RET(bitmap); view->texture.ready = 1; view->texture.changed = 1; @@ -481,7 +475,7 @@ void ku_view_set_texture_alpha(ku_view_t* view, float alpha, char recur) if (recur) { - for (int i = 0; i < view->views->length; i++) + for (size_t i = 0; i < view->views->length; i++) { ku_view_t* v = view->views->data[i]; ku_view_set_texture_alpha(v, alpha, recur); @@ -525,7 +519,7 @@ void ku_view_layout(ku_view_t* view, float scale) if (view->style.flexdir == FD_ROW) { // calculate width of all fixed width views - for (int i = 0; i < view->views->length; i++) + for (size_t i = 0; i < view->views->length; i++) { ku_view_t* v = view->views->data[i]; if (v->style.width > 0) @@ -540,7 +534,7 @@ void ku_view_layout(ku_view_t* view, float scale) if (view->style.flexdir == FD_COL) { // calculate height of all fixed height views - for (int i = 0; i < view->views->length; i++) + for (size_t i = 0; i < view->views->length; i++) { ku_view_t* v = view->views->data[i]; if (v->style.height > 0) @@ -554,7 +548,7 @@ void ku_view_layout(ku_view_t* view, float scale) } } - for (int i = 0; i < view->views->length; i++) + for (size_t i = 0; i < view->views->length; i++) { ku_view_t* v = view->views->data[i]; ku_rect_t frame = v->frame.local; @@ -665,7 +659,7 @@ void ku_view_layout(ku_view_t* view, float scale) ku_view_set_frame(v, frame); } - for (int i = 0; i < view->views->length; i++) + for (size_t i = 0; i < view->views->length; i++) { ku_view_t* v = view->views->data[i]; ku_view_layout(v, scale); @@ -699,13 +693,14 @@ void ku_view_describe(void* pointer, int level) if (view->parent) { ku_view_draw_delimiter(view->parent); - if (mt_vector_index_of_data(view->parent->views, view) == view->parent->views->length - 1) arrow = "└── "; + if (mt_vector_index_of_data(view->parent->views, view) == view->parent->views->length - 1) + arrow = "└── "; printf("%s", arrow); } - printf("%s [x:%.2f y:%.2f w:%.2f h:%.2f eh:%i tg:%i rc:%zu]\n", view->id, view->frame.local.x, view->frame.local.y, view->frame.local.w, view->frame.local.h, view->handler != NULL, view->tex_gen != NULL, mt_memory_retaincount(view)); + printf("%s [x:%.2f y:%.2f w:%.2f h:%.2f eh:%i tg:%i rc:%zu]\n", view->id, view->frame.local.x, view->frame.local.y, view->frame.local.w, view->frame.local.h, view->evt_han != NULL, view->tex_gen != NULL, mt_memory_retaincount(view)); - for (int i = 0; i < view->views->length; i++) ku_view_describe(view->views->data[i], level + 1); + for (size_t i = 0; i < view->views->length; i++) ku_view_describe(view->views->data[i], level + 1); } void ku_view_desc_style(vstyle_t l) diff --git a/src/kinetic_ui/ku_window.c b/src/kinetic_ui/ku_window.c index c4c10c0..cb3e083 100644 --- a/src/kinetic_ui/ku_window.c +++ b/src/kinetic_ui/ku_window.c @@ -37,6 +37,7 @@ ku_rect_t ku_window_update(ku_window_t* window, uint32_t time); #if __INCLUDE_LEVEL__ == 0 #include "ku_gl.c" +#include "mt_log.c" #include "mt_math_2d.c" #include "mt_time.c" #include "mt_vector.c" @@ -100,7 +101,7 @@ void ku_window_remove(ku_window_t* win, ku_view_t* view) void ku_window_activate(ku_window_t* win, ku_view_t* view, int flag) { if (flag) - mt_vector_add_unique_data(win->ptrqueue, view); + mt_vector_add(win->ptrqueue, view); else mt_vector_rem(win->ptrqueue, view); } @@ -137,21 +138,19 @@ void ku_window_event(ku_window_t* win, ku_event_t ev) { ku_event_t outev = ev; outev.type = KU_EVENT_MOUSE_MOVE_OUT; - for (int i = win->movqueue->length - 1; i > -1; i--) + + for (size_t i = win->movqueue->length; i-- > 0;) { ku_view_t* v = win->movqueue->data[i]; - if (v->needs_touch) + if (!(ev.x < v->frame.global.x && + ev.x > v->frame.global.x + v->frame.global.w && + ev.y < v->frame.global.y && + ev.y > v->frame.global.y + v->frame.global.h)) { - if (ev.x > v->frame.global.x && - ev.x < v->frame.global.x + v->frame.global.w && - ev.y > v->frame.global.y && - ev.y < v->frame.global.y + v->frame.global.h) - { - } - else + if (v->evt_han) { - if (v->handler) (*v->handler)(v, outev); - if (v->blocks_touch) break; + if (!(*v->evt_han)(v, outev)) + break; } } } @@ -159,75 +158,77 @@ void ku_window_event(ku_window_t* win, ku_event_t ev) mt_vector_reset(win->movqueue); ku_view_coll_touched(win->root, ev, win->movqueue); - for (int i = win->movqueue->length - 1; i > -1; i--) + for (size_t i = win->movqueue->length; i-- > 0;) { ku_view_t* v = win->movqueue->data[i]; - if (v->needs_touch && v->parent) + if (v->evt_han) { - if (v->handler) (*v->handler)(v, ev); - if (v->blocks_touch) break; + if (!(*v->evt_han)(v, ev)) + break; } } } else if (ev.type == KU_EVENT_MOUSE_DOWN || ev.type == KU_EVENT_MOUSE_UP) { ku_event_t outev = ev; - if (ev.type == KU_EVENT_MOUSE_DOWN) outev.type = KU_EVENT_MOUSE_DOWN_OUT; - if (ev.type == KU_EVENT_MOUSE_UP) outev.type = KU_EVENT_MOUSE_UP_OUT; - if (ev.type == KU_EVENT_MOUSE_DOWN) + outev.type = KU_EVENT_MOUSE_DOWN_OUT; + if (ev.type == KU_EVENT_MOUSE_UP) + outev.type = KU_EVENT_MOUSE_UP_OUT; + + /* binding this to only mouse down disables drop on different table */ + /* but enables out of mouse input field activation */ + if (ev.type == KU_EVENT_MOUSE_DOWN || + (ev.type == KU_EVENT_MOUSE_UP && ev.drag)) { - for (int i = win->ptrqueue->length - 1; i > -1; i--) + for (size_t i = win->ptrqueue->length; i-- > 0;) { ku_view_t* v = win->ptrqueue->data[i]; - if (v->needs_touch) + if (v->evt_han) { - if (v->handler) (*v->handler)(v, outev); - if (v->blocks_touch) break; + if (!(*v->evt_han)(v, outev)) + break; } } mt_vector_reset(win->ptrqueue); + ku_view_coll_touched(win->root, ev, win->ptrqueue); } - ku_view_coll_touched(win->root, ev, win->ptrqueue); - for (int i = win->ptrqueue->length - 1; i > -1; i--) + for (size_t i = win->ptrqueue->length; i-- > 0;) { ku_view_t* v = win->ptrqueue->data[i]; - if (v->needs_touch && v->parent) + if (v->evt_han) { - if (v->handler) (*v->handler)(v, ev); - if (v->blocks_touch) break; + if (!(*v->evt_han)(v, ev)) + break; } } } - else if (ev.type == KU_EVENT_SCROLL) + else if (ev.type == KU_EVENT_SCROLL || ev.type == KU_EVENT_SCROLL_X_END || ev.type == KU_EVENT_SCROLL_Y_END || ev.type == KU_EVENT_PINCH || ev.type == KU_EVENT_HOLD_START || ev.type == KU_EVENT_HOLD_END) { mt_vector_reset(win->ptrqueue); ku_view_coll_touched(win->root, ev, win->ptrqueue); - for (int i = win->ptrqueue->length - 1; i > -1; i--) + for (size_t i = win->ptrqueue->length; i-- > 0;) { ku_view_t* v = win->ptrqueue->data[i]; - if (v->needs_touch && v->parent) + if (v->evt_han) { - if (v->handler) (*v->handler)(v, ev); - if (v->blocks_scroll) break; + if (!(*v->evt_han)(v, ev)) + break; } } } - else if (ev.type == KU_EVENT_PINCH) + else if (ev.type == KU_EVENT_TEXT) { - mt_vector_reset(win->ptrqueue); - ku_view_coll_touched(win->root, ev, win->ptrqueue); - - for (int i = win->ptrqueue->length - 1; i > -1; i--) + for (size_t i = win->ptrqueue->length; i-- > 0;) { ku_view_t* v = win->ptrqueue->data[i]; - if (v->needs_touch && v->parent) + if (v->evt_han) { - if (v->handler) (*v->handler)(v, ev); - if (v->blocks_scroll) break; + if (!(*v->evt_han)(v, ev)) + break; } } } @@ -239,13 +240,14 @@ void ku_window_event(ku_window_t* win, ku_event_t ev) { ku_view_t* v = win->focusable->data[0]; win->focused = v; - if (v->handler) (*v->handler)(v, (ku_event_t){.type = KU_EVENT_FOCUS}); - mt_vector_add_unique_data(win->ptrqueue, v); + if (v->evt_han) + (*v->evt_han)(v, (ku_event_t){.type = KU_EVENT_FOCUS}); + mt_vector_add(win->ptrqueue, v); } else { /* unfocus first */ - int index = 0; + size_t index = 0; for (index = 0; index < win->focusable->length; index++) { ku_view_t* v = win->focusable->data[index]; @@ -253,7 +255,8 @@ void ku_window_event(ku_window_t* win, ku_event_t ev) if (win->focused == v) { win->focused = NULL; - if (v->handler) (*v->handler)(v, (ku_event_t){.type = KU_EVENT_UNFOCUS}); + if (v->evt_han) + (*v->evt_han)(v, (ku_event_t){.type = KU_EVENT_UNFOCUS}); mt_vector_rem(win->ptrqueue, v); break; } @@ -261,35 +264,25 @@ void ku_window_event(ku_window_t* win, ku_event_t ev) /* focus next in queue */ index++; - if (index == win->focusable->length) index = 0; + if (index == win->focusable->length) + index = 0; ku_view_t* v = win->focusable->data[index]; win->focused = v; - if (v->handler) (*v->handler)(v, (ku_event_t){.type = KU_EVENT_FOCUS}); - mt_vector_add_unique_data(win->ptrqueue, v); + if (v->evt_han) + (*v->evt_han)(v, (ku_event_t){.type = KU_EVENT_FOCUS}); + mt_vector_add(win->ptrqueue, v); } } - for (int i = win->ptrqueue->length - 1; i > -1; i--) + for (size_t i = win->ptrqueue->length; i-- > 0;) { ku_view_t* v = win->ptrqueue->data[i]; - if (v->needs_key && v->parent) - { - if (v->handler) (*v->handler)(v, ev); - if (v->blocks_key) break; - } - } - } - else if (ev.type == KU_EVENT_TEXT) - { - for (int i = win->ptrqueue->length - 1; i > -1; i--) - { - ku_view_t* v = win->ptrqueue->data[i]; - if (v->needs_text) + if (v->evt_han) { - if (v->handler) (*v->handler)(v, ev); - break; + if (!(*v->evt_han)(v, ev)) + break; } } } @@ -298,9 +291,10 @@ void ku_window_event(ku_window_t* win, ku_event_t ev) void ku_window_rearrange(ku_window_t* win, ku_view_t* view, mt_vector_t* views) { VADD(views, view); - if (view->style.unmask == 1) view->style.unmask = 0; // reset unmasking + if (view->style.unmask == 1) + view->style.unmask = 0; // reset unmasking mt_vector_t* vec = view->views; - for (int i = 0; i < vec->length; i++) ku_window_rearrange(win, vec->data[i], views); + for (size_t i = 0; i < vec->length; i++) ku_window_rearrange(win, vec->data[i], views); if (view->style.masked) { ku_view_t* last = views->data[views->length - 1]; @@ -322,11 +316,12 @@ ku_rect_t ku_window_update(ku_window_t* win, uint32_t time) win->root->rearrange = 0; } - for (int i = 0; i < win->views->length; i++) + for (size_t i = 0; i < win->views->length; i++) { ku_view_t* view = win->views->data[i]; - if (view->texture.ready == 0) ku_view_gen_texture(view); + if (view->texture.ready == 0) + ku_view_gen_texture(view); if (view->texture.changed) { diff --git a/src/kinetic_ui/texture/tg_css.c b/src/kinetic_ui/texture/tg_css.c index 04c1835..615c92c 100644 --- a/src/kinetic_ui/texture/tg_css.c +++ b/src/kinetic_ui/texture/tg_css.c @@ -27,7 +27,7 @@ void tg_css_add(ku_view_t* view); #include "mt_log.c" #include "mt_string.c" -void tg_css_gen(ku_view_t* view) +int tg_css_gen(ku_view_t* view) { if (view->frame.local.w >= 1.0 && view->frame.local.h >= 1.0) @@ -96,6 +96,8 @@ void tg_css_gen(ku_view_t* view) view->texture.ready = 1; } } + + return 1; } void tg_css_add(ku_view_t* view) diff --git a/src/kinetic_ui/texture/tg_knob.c b/src/kinetic_ui/texture/tg_knob.c index 4e6c7bd..0f68fb1 100644 --- a/src/kinetic_ui/texture/tg_knob.c +++ b/src/kinetic_ui/texture/tg_knob.c @@ -20,7 +20,7 @@ void tg_knob_set_angle(ku_view_t* view, float angle); #include "ku_draw.c" -void tg_knob_gen(ku_view_t* view) +int tg_knob_gen(ku_view_t* view) { tg_knob_t* tg = view->tex_gen_data; @@ -65,7 +65,8 @@ void tg_knob_gen(ku_view_t* view) REL(bmp); // REL 0 } - if (tg->angle < 0) tg->angle += 6.28; + if (tg->angle < 0) + tg->angle += 6.28; ku_draw_insert(view->texture.bitmap, tg->back, 0, 0); @@ -83,6 +84,8 @@ void tg_knob_gen(ku_view_t* view) view->texture.changed = 1; view->texture.ready = 1; } + + return 1; } void tg_knob_del(void* p) @@ -106,10 +109,6 @@ void tg_knob_add(ku_view_t* view) view->tex_gen_data = tg; view->tex_gen = tg_knob_gen; - - view->needs_touch = 1; - view->blocks_touch = 1; - view->blocks_scroll = 1; } void tg_knob_set_angle(ku_view_t* view, float angle) diff --git a/src/kinetic_ui/texture/tg_scaledimg.c b/src/kinetic_ui/texture/tg_scaledimg.c index ac30ffa..477df34 100644 --- a/src/kinetic_ui/texture/tg_scaledimg.c +++ b/src/kinetic_ui/texture/tg_scaledimg.c @@ -16,7 +16,7 @@ typedef struct _tg_scaledimg_t } tg_scaledimg_t; void tg_scaledimg_add(ku_view_t* view, int w, int h); -void tg_scaledimg_gen(ku_view_t* view); +int tg_scaledimg_gen(ku_view_t* view); void tg_scaledimg_set_content_size(ku_view_t* view, int w, int h); #endif @@ -26,7 +26,7 @@ void tg_scaledimg_set_content_size(ku_view_t* view, int w, int h); #include "ku_draw.c" #include "mt_string.c" -void tg_scaledimg_gen(ku_view_t* view) +int tg_scaledimg_gen(ku_view_t* view) { tg_scaledimg_t* gen = view->tex_gen_data; ku_bitmap_t* bm = view->texture.bitmap; @@ -47,6 +47,8 @@ void tg_scaledimg_gen(ku_view_t* view) } view->texture.ready = 1; + + return 1; } void tg_scaledimg_set_content_size(ku_view_t* view, int w, int h) diff --git a/src/kinetic_ui/texture/tg_text.c b/src/kinetic_ui/texture/tg_text.c index 9d2ab74..3d5c7de 100644 --- a/src/kinetic_ui/texture/tg_text.c +++ b/src/kinetic_ui/texture/tg_text.c @@ -34,7 +34,7 @@ void tg_text_set_style(ku_view_t* view, textstyle_t style); int tg_text_index = 0; -void tg_text_gen(ku_view_t* view) +int tg_text_gen(ku_view_t* view) { tg_text_t* gen = view->tex_gen_data; if (view->frame.local.w > 0 && view->frame.local.h > 0) @@ -42,8 +42,10 @@ void tg_text_gen(ku_view_t* view) if (gen->scale != view->style.scale) { float ratio; - if (gen->scale == 0.0) ratio = view->style.scale; - else ratio = view->style.scale / gen->scale; + if (gen->scale == 0.0) + ratio = view->style.scale; + else + ratio = view->style.scale / gen->scale; gen->style.size *= ratio; gen->scale = view->style.scale; } @@ -69,6 +71,8 @@ void tg_text_gen(ku_view_t* view) REL(fontmap); // REL 0 } + + return 1; } void tg_text_del(void* p) diff --git a/src/mt_core/mt_channel.c b/src/mt_core/mt_channel.c index f4210ee..84ccfc7 100644 --- a/src/mt_core/mt_channel.c +++ b/src/mt_core/mt_channel.c @@ -7,11 +7,10 @@ #ifndef mt_channel_h #define mt_channel_h -/* TODO separate unit tests */ - #include "mt_memory.c" #include #include +#include #include #include @@ -30,7 +29,6 @@ mt_channel_t* mt_channel_new(uint32_t size); void mt_channel_del(void* pointer); char mt_channel_send(mt_channel_t* ch, void* data); void* mt_channel_recv(mt_channel_t* ch); -void mt_channel_test(void); #endif @@ -84,10 +82,13 @@ char mt_channel_send(mt_channel_t* ch, void* data) if (ch->flags[ch->wpos] == 0) { - ch->flags[ch->wpos] = 1; // set flag, it doesn't have to be atomic, only the last bit counts - ch->boxes[ch->wpos] = data; + /* swap these rows to cause a race condition and a failing test */ + ch->boxes[ch->wpos] = data; // first store data + ch->flags[ch->wpos] = 1; // then set flag, it doesn't have to be atomic, only the last bit counts + ch->wpos += 1; // increment write index, doesn't have to be atomic, this thread uses it only - if (ch->wpos == ch->size) ch->wpos = 0; + if (ch->wpos == ch->size) + ch->wpos = 0; return 1; } @@ -105,9 +106,11 @@ void* mt_channel_recv(mt_channel_t* ch) ch->boxes[ch->rpos] = NULL; // empty box ch->flags[ch->rpos] = 0; // set flag, it doesn't have to be atomic, only the last bit counts - ch->rpos += 1; // increment read index, it doesn't have to be atomic, this thread - if (ch->rpos == ch->size) ch->rpos = 0; + ch->rpos += 1; // increment read index, it doesn't have to be atomic, this thread uses it only + + if (ch->rpos == ch->size) + ch->rpos = 0; return result; } diff --git a/src/mt_core/mt_map.c b/src/mt_core/mt_map.c index fe9502a..86bef7f 100644 --- a/src/mt_core/mt_map.c +++ b/src/mt_core/mt_map.c @@ -1,8 +1,6 @@ #ifndef mt_map_h #define mt_map_h -/* TODO separate unit tests */ - #include "mt_vector.c" #include #include @@ -23,16 +21,16 @@ struct pair_t typedef struct bucket_t bucket_t; struct bucket_t { - unsigned int count; - pair_t* pairs; + size_t count; + pair_t* pairs; }; typedef struct _mt_map_t mt_map_t; struct _mt_map_t { - unsigned int count_real; - unsigned int count; - bucket_t* buckets; + size_t count_real; + size_t count; + bucket_t* buckets; }; mt_map_t* mt_map_new(void); @@ -44,13 +42,8 @@ void* mt_map_get(mt_map_t* map, const char* key); void mt_map_del(mt_map_t* map, const char* key); void mt_map_keys(mt_map_t* map, mt_vector_t* res); void mt_map_values(mt_map_t* map, mt_vector_t* res); -void mt_map_print_keys(mt_map_t* map); void mt_map_describe(void* p, int level); -#ifdef DEBUG -void mt_map_test(void); -#endif - #endif #if __INCLUDE_LEVEL__ == 0 @@ -156,7 +149,7 @@ void mt_map_resize(mt_map_t* map) mt_vector_t* oldkeys = VNEW(); mt_map_keys(map, oldkeys); - for (uint32_t index = 0; index < oldkeys->length; index++) + for (size_t index = 0; index < oldkeys->length; index++) { char* key = oldkeys->data[index]; void* value = mt_map_get(map, key); @@ -423,7 +416,7 @@ void mt_map_printkeys(mt_map_t* map) mt_vector_t* keys = VNEW(); mt_map_keys(map, keys); printf(" \n"); - for (int index = 0; index < keys->length; index++) + for (size_t index = 0; index < keys->length; index++) printf(" %s", (char*) keys->data[index]); } @@ -433,7 +426,7 @@ void mt_map_describe(void* p, int level) mt_vector_t* keys = VNEW(); // REL 0 mt_map_keys(map, keys); printf("{"); - for (int index = 0; index < keys->length; index++) + for (size_t index = 0; index < keys->length; index++) { char* key = (char*) keys->data[index]; printf("\n%*s(K)%s\n%*s(V)", level, " ", key, level, " "); diff --git a/src/mt_core/mt_memory.c b/src/mt_core/mt_memory.c index 140a8b3..c7c7ca0 100644 --- a/src/mt_core/mt_memory.c +++ b/src/mt_core/mt_memory.c @@ -6,15 +6,12 @@ #include #include #include - -/* TODO separate unit tests */ - -#undef MT_MEMORY_DEBUG +#include #define CAL(X, Y, Z) mt_memory_calloc(X, Y, Z); #define RET(X) mt_memory_retain(X) #define REL(X) mt_memory_release(X) -#define HEAP(X) mt_memory_stack_to_heap(sizeof(X), NULL, NULL, (uint8_t*) &X) +#define HEAP(X) mt_memory_stack_to_heap(sizeof(X), NULL, NULL, (char*) &X) void* mt_memory_alloc(size_t size, void (*destructor)(void*), void (*descriptor)(void*, int)); void* mt_memory_calloc(size_t size, void (*destructor)(void*), void (*descriptor)(void*, int)); @@ -22,76 +19,41 @@ void* mt_memory_realloc(void* pointer, size_t size); void* mt_memory_retain(void* pointer); char mt_memory_release(void* pointer); size_t mt_memory_retaincount(void* pointer); -void* mt_memory_stack_to_heap(size_t size, void (*destructor)(void*), void (*descriptor)(void*, int), uint8_t* data); +void* mt_memory_stack_to_heap(size_t size, void (*destructor)(void*), void (*descriptor)(void*, int), char* data); void mt_memory_describe(void* pointer, int level); -#ifdef MT_MEMORY_DEBUG -void mt_memory_stats(); -#endif - #endif #if __INCLUDE_LEVEL__ == 0 -#include -#include - struct mt_memory_head { -#ifdef MT_MEMORY_DEBUG - uint32_t index; /* allocation index for debugging/statistics */ -#endif + char id[2]; void (*destructor)(void*); void (*descriptor)(void*, int); - int32_t retaincount; + size_t retaincount; }; -#ifdef MT_MEMORY_DEBUG - - #define MT_MEMORY_DEBUG_SIZE 2000000 /* maximum areas to store */ - #define MT_MEMORY_DEBUG_INDEX 0 /* head index to stop at error */ - -struct mt_memory_head* mt_memory_heads[MT_MEMORY_DEBUG_SIZE] = {0}; -static uint32_t mt_memory_index = 1; /* live object counter for debugging */ - -void mt_memory_trace( - char* id, struct mt_memory_head* head) -{ - printf("*** %i %s : %i ***\n", head->index, id, head->retaincount); - - void* array[128]; - int size = backtrace(array, 128); - char** strings = backtrace_symbols(array, size); - for (int i = 0; i < size; ++i) printf("%s\n", strings[i]); - free(strings); -} - -#endif - void* mt_memory_alloc( size_t size, /* size of data to store */ void (*destructor)(void*), /* optional destructor */ void (*descriptor)(void*, int)) /* optional descriptor for describing memory area */ { - uint8_t* bytes = malloc(sizeof(struct mt_memory_head) + size); + char* bytes = malloc(sizeof(struct mt_memory_head) + size); if (bytes != NULL) { struct mt_memory_head* head = (struct mt_memory_head*) bytes; + head->id[0] = 'm'; + head->id[1] = 't'; head->destructor = destructor; head->descriptor = descriptor; head->retaincount = 1; -#ifdef MT_MEMORY_DEBUG - head->index = mt_memory_index; - mt_memory_heads[mt_memory_index] = head; - if (head->index == MT_MEMORY_DEBUG_INDEX) mt_memory_trace("ALLOC", head); - mt_memory_index++; -#endif - return bytes + sizeof(struct mt_memory_head); } - else return NULL; + else + return NULL; } void* mt_memory_calloc( @@ -99,104 +61,100 @@ void* mt_memory_calloc( void (*destructor)(void*), /* optional destructor */ void (*descriptor)(void*, int)) /* optional descriptor for describing memory area */ { - uint8_t* bytes = calloc(1, sizeof(struct mt_memory_head) + size); + char* bytes = calloc(1, sizeof(struct mt_memory_head) + size); + if (bytes != NULL) { struct mt_memory_head* head = (struct mt_memory_head*) bytes; + head->id[0] = 'm'; + head->id[1] = 't'; head->destructor = destructor; head->descriptor = descriptor; head->retaincount = 1; -#ifdef MT_MEMORY_DEBUG - head->index = mt_memory_index; - mt_memory_heads[mt_memory_index] = head; - if (head->index == MT_MEMORY_DEBUG_INDEX) mt_memory_trace("CALLOC", head); - mt_memory_index++; -#endif - return bytes + sizeof(struct mt_memory_head); } - else return NULL; + else + return NULL; } void* mt_memory_stack_to_heap( size_t size, void (*destructor)(void*), void (*descriptor)(void*, int), - uint8_t* data) + char* data) { - uint8_t* bytes = mt_memory_alloc(size, destructor, descriptor); + char* bytes = mt_memory_alloc(size, destructor, descriptor); if (bytes != NULL) { memcpy(bytes, data, size); return bytes; } - else return NULL; + else + return NULL; } void* mt_memory_realloc(void* pointer, size_t size) { assert(pointer != NULL); - uint8_t* bytes = (uint8_t*) pointer; + char* bytes = (char*) pointer; bytes -= sizeof(struct mt_memory_head); bytes = realloc(bytes, sizeof(struct mt_memory_head) + size); if (bytes != NULL) { -#ifdef MT_MEMORY_DEBUG - struct mt_memory_head* head = (struct mt_memory_head*) bytes; - mt_memory_heads[head->index] = head; -#endif return bytes + sizeof(struct mt_memory_head); } - else return NULL; + else + return NULL; } void* mt_memory_retain(void* pointer) { assert(pointer != NULL); - uint8_t* bytes = (uint8_t*) pointer; + char* bytes = (char*) pointer; bytes -= sizeof(struct mt_memory_head); struct mt_memory_head* head = (struct mt_memory_head*) bytes; + assert(head->id[0] == 'm'); + assert(head->id[1] == 't'); + if (head->retaincount < SIZE_MAX) { head->retaincount += 1; -#ifdef MT_MEMORY_DEBUG - if (head->index == MT_MEMORY_DEBUG_INDEX) mt_memory_trace("RETAIN", head); -#endif return pointer; } - else return NULL; + else + return NULL; } char mt_memory_release(void* pointer) { assert(pointer != NULL); - uint8_t* bytes = (uint8_t*) pointer; + char* bytes = (char*) pointer; bytes -= sizeof(struct mt_memory_head); struct mt_memory_head* head = (struct mt_memory_head*) bytes; - head->retaincount -= 1; - -#ifdef MT_MEMORY_DEBUG - if (head->index == MT_MEMORY_DEBUG_INDEX) mt_memory_trace("RELEASE", head); - if (head->retaincount == -1) mt_memory_trace("RELEASE RETAINCOUNT -1!!!", head); -#endif + assert(head->id[0] == 'm'); + assert(head->id[1] == 't'); + assert(head->retaincount > 0); - assert(head->retaincount > -1); + head->retaincount -= 1; if (head->retaincount == 0) { - if (head->destructor != NULL) head->destructor(pointer); - /* don't clean up to catch overrelease or leaks */ -#ifndef MT_MEMORY_DEBUG + if (head->destructor != NULL) + head->destructor(pointer); + + head->id[0] = '\0'; + head->id[1] = '\0'; + free(bytes); -#endif + return 1; } @@ -207,10 +165,13 @@ size_t mt_memory_retaincount(void* pointer) { assert(pointer != NULL); - uint8_t* bytes = (uint8_t*) pointer; + char* bytes = (char*) pointer; bytes -= sizeof(struct mt_memory_head); struct mt_memory_head* head = (struct mt_memory_head*) bytes; + assert(head->id[0] == 'm'); + assert(head->id[1] == 't'); + return head->retaincount; } @@ -218,10 +179,13 @@ void mt_memory_describe(void* pointer, int level) { assert(pointer != NULL); - uint8_t* bytes = (uint8_t*) pointer; + char* bytes = (char*) pointer; bytes -= sizeof(struct mt_memory_head); struct mt_memory_head* head = (struct mt_memory_head*) bytes; + assert(head->id[0] == 'm'); + assert(head->id[1] == 't'); + if (head->descriptor != NULL) { head->descriptor(pointer, ++level); @@ -232,36 +196,4 @@ void mt_memory_describe(void* pointer, int level) } } -#ifdef MT_MEMORY_DEBUG - -void mt_memory_stats() -{ - printf("\n***MEM STATS***\n"); - - // print block statistics - - int problem = 0; - - for (int index = 1; index < mt_memory_index; index++) - { - if (MT_MEMORY_DEBUG_INDEX == 0 || MT_MEMORY_DEBUG_INDEX == index) - { - if (mt_memory_heads[index]->retaincount < 0) - { - problem = 1; - printf("OVERRELEASE at %i : %i\n", index, mt_memory_heads[index]->retaincount); - } - if (mt_memory_heads[index]->retaincount > 0) - { - problem = 1; - printf("LEAK at %i : %i\n", index, mt_memory_heads[index]->retaincount); - } - } - } - - if (problem == 0) printf("Everything seems all right\n"); -} - -#endif - #endif diff --git a/src/mt_core/mt_path.c b/src/mt_core/mt_path.c index 5736b47..4934fb5 100644 --- a/src/mt_core/mt_path.c +++ b/src/mt_core/mt_path.c @@ -1,16 +1,13 @@ #ifndef mt_path_h #define mt_path_h -/* TODO separate unit tests */ - #include "mt_string.c" char* mt_path_new_append(char* root, char* component); char* mt_path_new_remove_last_component(char* path); char* mt_path_new_extension(char* path); char* mt_path_new_filename(char* path); -char* mt_path_new_normalize(char* path, char* execpath); -char* mt_path_new_normalize1(char* path); +char* mt_path_new_normalize(char* path); #endif @@ -18,10 +15,12 @@ char* mt_path_new_normalize1(char* path); #include #include -#ifdef __linux__ -#include +#include +#ifdef __linux__ + #include #endif +#include "mt_log.c" #include "mt_memory.c" char* mt_path_new_append(char* root, char* component) @@ -34,6 +33,7 @@ char* mt_path_new_append(char* root, char* component) char* mt_path_new_remove_last_component(char* path) { + /* TODO use POSIX dirname */ int index; for (index = strlen(path) - 2; index > 0; index--) { @@ -50,36 +50,46 @@ char* mt_path_new_remove_last_component(char* path) memcpy(str, path, index); return str; } - else return mt_string_new_cstring("/"); + else + return mt_string_new_cstring("/"); } char* mt_path_new_extension(char* path) { - int index; - for (index = strlen(path) - 1; index > -1; --index) - { - if (path[index] == '.') - { - index++; - break; - } - } - - int len = strlen(path) - index; - char* ext = CAL(len + 1, NULL, mt_string_describe); - memcpy(ext, path + index, len); - return ext; + char* ext = strrchr(path, '.'); + char* res = NULL; + if (ext) + res = mt_string_new_cstring(ext + 1); + + /* int index; */ + /* for (index = strlen(path) - 1; index > -1; --index) */ + /* { */ + /* if (path[index] == '.') */ + /* { */ + /* index++; */ + /* break; */ + /* } */ + /* } */ + + /* int len = strlen(path) - index; */ + /* char* ext = CAL(len + 1, NULL, mt_string_describe); */ + /* memcpy(ext, path + index, len); */ + return res; } char* mt_path_new_filename(char* path) { + /* TODO use POSIX basename */ + int dotindex; for (dotindex = strlen(path) - 1; dotindex > -1; --dotindex) { - if (path[dotindex] == '.') break; + if (path[dotindex] == '.') + break; } - if (dotindex == -1) dotindex = strlen(path) - 1; + if (dotindex == -1) + dotindex = strlen(path) - 1; int slashindex; for (slashindex = strlen(path) - 1; slashindex > -1; --slashindex) @@ -90,83 +100,74 @@ char* mt_path_new_filename(char* path) break; } } - if (slashindex == -1) slashindex = 0; + if (slashindex == -1) + slashindex = 0; int len = dotindex - slashindex; char* title = CAL(len + 1, NULL, mt_string_describe); memcpy(title, path + slashindex, len); return title; } -char* mt_path_new_normalize(char* path, char* execpath) +char* mt_path_new_normalize(char* path) { - char* result = NULL; + char* extpath = mt_string_new_cstring(""); + char* newpath = CAL(PATH_MAX, NULL, NULL); - if (path[0] == '~') // if starts with tilde, insert home dir - result = mt_string_new_format(PATH_MAX + NAME_MAX, "%s%s", getenv("HOME"), path + 1); - else if (path[0] != '/') // if doesn't start with / insert base dir - result = mt_string_new_format(PATH_MAX + NAME_MAX, "%s/%s", execpath, path); - else - result = mt_string_new_cstring(path); - - // if ends with '/' remove it - if (result[strlen(result) - 1] == '/') result[strlen(result) - 1] = '\0'; - - return result; -} - -char* mt_path_new_normalize1(char* path) -{ - mt_vector_t* tokens = mt_string_tokenize(path, "/"); - char* result = NULL; - - if (tokens->length > 0) + if (path[0] == '~') { - result = mt_string_new_cstring(""); - mt_vector_t* newtok = VNEW(); - - for (int index = 0; index < tokens->length; index++) - { - char* token = tokens->data[index]; - if (token[0] == '~') - { - // replace tilde with home - VADDR(newtok, mt_string_new_cstring(getenv("HOME"))); - } - else if (strlen(token) == 2 && token[0] == '.' && token[1] == '.') - { - // delete last token - mt_vector_rem_at_index(newtok, newtok->length - 1); - } - else if (strlen(token) == 1 && token[0] == '.') - { - // do nothing at current dir - } - else - { - VADD(newtok, token); - } - } - - /* assemble new tokens */ - - for (int index = 0; index < newtok->length; index++) - { - char* token = newtok->data[index]; - result = mt_string_append(result, "/"); - result = mt_string_append(result, token); - } - - if (newtok->length == 0) result = mt_string_new_cstring("/"); - - REL(newtok); + /* replace tilde with home dir */ + extpath = mt_string_append(extpath, getenv("HOME")); + extpath = mt_string_append_sub(extpath, path, 1, strlen(path) - 1); } else - { - result = mt_string_new_cstring("/"); - } - - REL(tokens); - return result; + extpath = mt_string_append(extpath, path); + + realpath(extpath, newpath); + + REL(extpath); + + /* char cwd[PATH_MAX] = {"~"}; */ + /* getcwd(cwd, sizeof(cwd)); */ + + /* char* newpath = NULL; */ + + /* if (path[0] == '~') */ + /* { */ + /* /\* replace tilde with home dir *\/ */ + /* newpath = mt_string_new_cstring(getenv("HOME")); */ + /* newpath = mt_string_append_sub(newpath, path, 1, strlen(path) - 1); */ + /* } */ + /* else if (path[0] == '\0') */ + /* { */ + /* /\* empty path to root path *\/ */ + /* newpath = mt_string_new_cstring("/"); */ + /* } */ + /* else if (path[0] != '/') */ + /* { */ + /* /\* insert working directory in case of relative path *\/ */ + /* newpath = mt_string_new_format(PATH_MAX, "%s/%s", cwd, path); */ + /* } */ + /* else */ + /* { */ + /* newpath = mt_string_new_cstring(path); */ + /* } */ + + /* /\* remove last component in case of double dot *\/ */ + + /* size_t len = strlen(newpath); */ + /* if (len > 3 && newpath[len - 1] == '.' && newpath[len - 2] == '.') */ + /* { */ + /* for (size_t index = len - 3; index-- > 0;) */ + /* { */ + /* if (newpath[index] == '/') */ + /* { */ + /* newpath[index] = '\0'; */ + /* break; */ + /* } */ + /* } */ + /* } */ + + return newpath; } #endif diff --git a/src/mt_core/mt_string.c b/src/mt_core/mt_string.c index 123fd11..a778cd7 100644 --- a/src/mt_core/mt_string.c +++ b/src/mt_core/mt_string.c @@ -1,8 +1,6 @@ #ifndef mt_string_h #define mt_string_h -/* TODO separate unit tests */ - #include "mt_vector.c" #include #include @@ -11,13 +9,16 @@ #define STRNC(x) mt_string_new_cstring(x) #define STRNF(x, ...) mt_string_new_format(x, __VA_ARGS__) -char* mt_string_new_format(int size, char* format, ...); -char* mt_string_new_cstring(char* string); -char* mt_string_reset(char* str); -char* mt_string_append(char* str, char* add); -char* mt_string_append_cp(char* str, uint32_t cp); -char* mt_string_append_sub(char* str, char* add, int from, int len); -char* mt_string_delete_utf_codepoints(char* str, int from, int len); +char* mt_string_new_format(int size, char* format, ...); +char* mt_string_new_cstring(char* string); +char* mt_string_new_substring(char* string, size_t from, size_t len); +char* mt_string_reset(char* str); + +char* mt_string_append(char* str, char* add); +char* mt_string_append_cp(char* str, uint32_t cp); +char* mt_string_append_sub(char* str, char* add, size_t from, size_t len); + +char* mt_string_delete_utf_codepoints(char* str, size_t from, size_t len); mt_vector_t* mt_string_tokenize(char* str, char* del); void mt_string_describe(void* p, int level); void mt_string_describe_utf(char* str); @@ -59,6 +60,20 @@ char* mt_string_new_cstring(char* string) return result; } +/* TODO calculate UTF8 length */ + +char* mt_string_new_substring(char* string, size_t from, size_t len) +{ + char* result = NULL; + if (string != NULL && len > from) + { + result = CAL((strlen(string) + 1) * sizeof(char), mt_string_del, mt_string_describe); + memcpy(result, string + from, len); + result[len] = '\0'; + } + return result; +} + char* mt_string_reset(char* str) { str[0] = '\0'; @@ -69,19 +84,22 @@ char* mt_string_append(char* str, char* add) { size_t needed = strlen(str) + strlen(add) + 1; - if (strlen(str) < needed) str = mt_memory_realloc(str, needed); + if (strlen(str) < needed) + str = mt_memory_realloc(str, needed); str = utf8cat(str, add); return str; } + char* mt_string_append_cp(char* str, uint32_t cp) { size_t size = utf8size(str); size_t cpsize = utf8codepointsize(cp); size_t needed = strlen(str) + size + 1; - if (strlen(str) < needed) str = mt_memory_realloc(str, needed); + if (strlen(str) < needed) + str = mt_memory_realloc(str, needed); char* end = str + size - 1; @@ -91,30 +109,32 @@ char* mt_string_append_cp(char* str, uint32_t cp) return str; } -char* mt_string_append_sub(char* str, char* add, int from, int len) +char* mt_string_append_sub(char* str, char* add, size_t from, size_t len) { size_t needed = strlen(str) + len + 1; int oldsize = strlen(str); - if (strlen(str) < needed) str = mt_memory_realloc(str, needed); + if (strlen(str) < needed) + str = mt_memory_realloc(str, needed); memcpy(str + oldsize, add + from, len); str[needed - 1] = '\0'; return str; } -char* mt_string_delete_utf_codepoints(char* str, int from, int len) +char* mt_string_delete_utf_codepoints(char* str, size_t from, size_t len) { char* res = STRNC(""); char* curr = str; utf8_int32_t cp; - for (int index = 0; index < utf8len(str); index++) + for (size_t index = 0; index < utf8len(str); index++) { curr = utf8codepoint(curr, &cp); - if (index < from || index > from + len) res = mt_string_append_cp(res, cp); + if (index < from || index > from + len) + res = mt_string_append_cp(res, cp); } strcpy(str, res); @@ -155,21 +175,21 @@ void mt_string_describe_utf(char* str) { char* part = str; utf8_int32_t cp; - for (int i = 0; i < utf8len(str); i++) + for (size_t i = 0; i < utf8len(str); i++) { part = utf8codepoint(part, &cp); printf("%.4i \t| ", cp); } printf("\n"); part = str; - for (int i = 0; i < utf8len(str); i++) + for (size_t i = 0; i < utf8len(str); i++) { part = utf8codepoint(part, &cp); printf("%c \t| ", cp); } part = str; printf("\n"); - for (int i = 0; i < utf8len(str); i++) + for (size_t i = 0; i < utf8len(str); i++) { part = utf8codepoint(part, &cp); printf("%zu \t| ", utf8codepointsize(cp)); diff --git a/src/mt_core/mt_vector.c b/src/mt_core/mt_vector.c index 2891d84..cd7c283 100644 --- a/src/mt_core/mt_vector.c +++ b/src/mt_core/mt_vector.c @@ -1,8 +1,6 @@ #ifndef mt_vector_h #define mt_vector_h -/* TODO separate unit tests */ - #include "mt_memory.c" #include "mt_time.c" #include @@ -20,37 +18,39 @@ typedef struct _mt_vector_t mt_vector_t; struct _mt_vector_t { - void** data; - void** next; - uint32_t length; - uint32_t length_real; + void** data; + void** next; + size_t length; + size_t length_real; }; mt_vector_t* mt_vector_new(void); void mt_vector_reset(mt_vector_t* vector); -void mt_vector_dec_retcount(mt_vector_t* vector); -void mt_vector_add(mt_vector_t* vector, void* data); -void mt_vector_add_rel(mt_vector_t* vector, void* data); -void mt_vector_ins(mt_vector_t* vector, void* data, size_t index); -void mt_vector_add_in_vector(mt_vector_t* mt_vector_a, mt_vector_t* mt_vector_b); -void mt_vector_add_unique_data(mt_vector_t* vector, void* data); -void mt_vector_ins_unique_data(mt_vector_t* vector, void* data, size_t index); -void mt_vector_replace_at_index(mt_vector_t* vector, void* data, size_t index); -char mt_vector_rem(mt_vector_t* vector, void* data); -char mt_vector_rem_at_index(mt_vector_t* vector, uint32_t index); -void mt_vector_rem_in_range(mt_vector_t* vector, uint32_t start, uint32_t end); -void mt_vector_rem_in_vector(mt_vector_t* mt_vector_a, mt_vector_t* mt_vector_b); -void mt_vector_reverse(mt_vector_t* vector); +void mt_vector_describe(void* p, int level); + void* mt_vector_head(mt_vector_t* vector); void* mt_vector_tail(mt_vector_t* vector); -uint32_t mt_vector_index_of_data(mt_vector_t* vector, void* data); void mt_vector_sort(mt_vector_t* vector, int (*comp)(void* left, void* right)); +void mt_vector_reverse(mt_vector_t* vector); +size_t mt_vector_index_of_data(mt_vector_t* vector, void* data); -void mt_vector_describe(void* p, int level); +void mt_vector_add(mt_vector_t* vector, void* data); +void mt_vector_ins(mt_vector_t* vector, void* data, size_t index); +void mt_vector_add_rel(mt_vector_t* vector, void* data); +void mt_vector_ins_rel(mt_vector_t* vector, void* data, size_t index); + +char mt_vector_rem(mt_vector_t* vector, void* data); +char mt_vector_rem_index(mt_vector_t* vector, size_t index); +void mt_vector_rem_range(mt_vector_t* vector, size_t start, size_t end); + +void mt_vector_add_in_vector(mt_vector_t* mt_vector_a, mt_vector_t* mt_vector_b); +void mt_vector_rem_in_vector(mt_vector_t* mt_vector_a, mt_vector_t* mt_vector_b); #endif #if __INCLUDE_LEVEL__ == 0 +#include "mt_log.c" + void mt_vector_del(void* vector); void mt_vector_describe_data(void* p, int level); void mt_vector_describe_mtvn(void* p, int level); @@ -71,7 +71,7 @@ mt_vector_t* mt_vector_new() void mt_vector_del(void* pointer) { mt_vector_t* vector = pointer; - for (uint32_t index = 0; index < vector->length; index++) + for (size_t index = 0; index < vector->length; index++) REL(vector->data[index]); REL(vector->data); } @@ -80,20 +80,11 @@ void mt_vector_del(void* pointer) void mt_vector_reset(mt_vector_t* vector) { - for (uint32_t index = 0; index < vector->length; index++) + for (size_t index = 0; index < vector->length; index++) REL(vector->data[index]); vector->length = 0; } -/* decreases retain count of items. use when you add items inline and don't want to release every item - one by one. Be careful with it, don't release them til dealloc!*/ - -void mt_vector_dec_retcount(mt_vector_t* vector) -{ - for (uint32_t index = 0; index < vector->length; index++) - REL(vector->data[index]); -} - /* expands storage */ void mt_vector_expand(mt_vector_t* vector) @@ -127,7 +118,8 @@ void mt_vector_add_rel(mt_vector_t* vector, void* data) void mt_vector_ins(mt_vector_t* vector, void* data, size_t index) { - if (index > vector->length) index = vector->length; + if (index > vector->length) + index = vector->length; RET(data); mt_vector_expand(vector); memmove(vector->data + index + 1, vector->data + index, (vector->length - index) * sizeof(void*)); @@ -135,48 +127,33 @@ void mt_vector_ins(mt_vector_t* vector, void* data, size_t index) vector->length += 1; } +/* adds and release data at given index */ + +void mt_vector_ins_rel(mt_vector_t* vector, void* data, size_t index) +{ + mt_vector_ins(vector, data, index); + REL(data); +} + /* adds all items in vector to vector */ void mt_vector_add_in_vector(mt_vector_t* mt_vector_a, mt_vector_t* mt_vector_b) { - for (uint32_t index = 0; index < mt_vector_b->length; index++) RET(mt_vector_b->data[index]); + for (size_t index = 0; index < mt_vector_b->length; index++) RET(mt_vector_b->data[index]); mt_vector_a->length_real += mt_vector_b->length_real; mt_vector_a->data = mt_memory_realloc(mt_vector_a->data, sizeof(void*) * mt_vector_a->length_real); memcpy(mt_vector_a->data + mt_vector_a->length, mt_vector_b->data, mt_vector_b->length * sizeof(void*)); mt_vector_a->length += mt_vector_b->length; } -/* adds single unique data */ - -void mt_vector_add_unique_data(mt_vector_t* vector, void* data) -{ - if (mt_vector_index_of_data(vector, data) == UINT32_MAX) mt_vector_add(vector, data); -} - -/* adds single unique data at index */ - -void mt_vector_ins_unique_data(mt_vector_t* vector, void* data, size_t index) -{ - if (mt_vector_index_of_data(vector, data) == UINT32_MAX) mt_vector_ins(vector, data, index); -} - -/* replaces data at given index */ - -void mt_vector_replace_at_index(mt_vector_t* vector, void* data, size_t index) -{ - REL(vector->data[index]); - RET(data); - vector->data[index] = data; -} - /* removes single data, returns 1 if data is removed and released during removal */ char mt_vector_rem(mt_vector_t* vector, void* data) { - uint32_t index = mt_vector_index_of_data(vector, data); - if (index < UINT32_MAX) + size_t index = mt_vector_index_of_data(vector, data); + if (index < SIZE_MAX) { - mt_vector_rem_at_index(vector, index); + mt_vector_rem_index(vector, index); return 1; } return 0; @@ -184,7 +161,7 @@ char mt_vector_rem(mt_vector_t* vector, void* data) /* removes single data at index, returns 1 if data is removed and released during removal */ -char mt_vector_rem_at_index(mt_vector_t* vector, uint32_t index) +char mt_vector_rem_index(mt_vector_t* vector, size_t index) { if (index < vector->length) { @@ -204,9 +181,9 @@ char mt_vector_rem_at_index(mt_vector_t* vector, uint32_t index) /* removes data in range */ -void mt_vector_rem_in_range(mt_vector_t* vector, uint32_t start, uint32_t end) +void mt_vector_rem_range(mt_vector_t* vector, size_t start, size_t end) { - for (uint32_t index = start; index < end; index++) + for (size_t index = start; index < end; index++) REL(vector->data[index]); memmove(vector->data + start, vector->data + end + 1, (vector->length - end - 1) * sizeof(void*)); vector->length -= end - start + 1; @@ -216,7 +193,7 @@ void mt_vector_rem_in_range(mt_vector_t* vector, uint32_t start, uint32_t end) void mt_vector_rem_in_vector(mt_vector_t* mt_vector_a, mt_vector_t* mt_vector_b) { - for (int index = 0; index < mt_vector_b->length; index++) + for (size_t index = 0; index < mt_vector_b->length; index++) { mt_vector_rem(mt_vector_a, mt_vector_b->data[index]); } @@ -226,12 +203,12 @@ void mt_vector_rem_in_vector(mt_vector_t* mt_vector_a, mt_vector_t* mt_vector_b) void mt_vector_reverse(mt_vector_t* vector) { - int length = vector->length; - for (int index = length - 1; index > -1; index--) + size_t length = vector->length; + for (size_t index = length; index-- > 0;) { mt_vector_add(vector, vector->data[index]); } - mt_vector_rem_in_range(vector, 0, length - 1); + mt_vector_rem_range(vector, 0, length - 1); } /* returns head item of vector */ @@ -254,15 +231,16 @@ void* mt_vector_tail(mt_vector_t* vector) return NULL; } -/* returns index of data or UINT32_MAX if not found */ +/* returns index of data or SIZE_MAX if not found */ -uint32_t mt_vector_index_of_data(mt_vector_t* vector, void* data) +size_t mt_vector_index_of_data(mt_vector_t* vector, void* data) { - for (int index = 0; index < vector->length; index++) + for (size_t index = 0; index < vector->length; index++) { - if (vector->data[index] == data) return index; + if (vector->data[index] == data) + return index; } - return UINT32_MAX; + return SIZE_MAX; } // mt vector node for sorting @@ -276,7 +254,7 @@ struct _mtvn_t }; mtvn_t* cache; -uint32_t cachei; +size_t cachei; // TODO use node pool @@ -303,16 +281,18 @@ void mt_vector_sort_ins(mtvn_t* node, void* data, int (*comp)(void* left, void* } } -void mt_vector_sort_ord(mtvn_t* node, mt_vector_t* vector, int* index) +void mt_vector_sort_ord(mtvn_t* node, mt_vector_t* vector, size_t* index) { - if (node->l) mt_vector_sort_ord(node->l, vector, index); + if (node->l) + mt_vector_sort_ord(node->l, vector, index); vector->data[*index] = node->c; *index += 1; // cleanup node mtvn_t* right = node->r; - if (right) mt_vector_sort_ord(right, vector, index); + if (right) + mt_vector_sort_ord(right, vector, index); } // sorts values in vector, needs a comparator function @@ -320,24 +300,28 @@ void mt_vector_sort_ord(mtvn_t* node, mt_vector_t* vector, int* index) void mt_vector_sort(mt_vector_t* vector, int (*comp)(void* left, void* right)) { - /* create cache */ - /* TODO make it local to make it thread safe */ + if (vector->length > 1) + { + /* create cache */ + /* TODO make it local to make it thread safe */ - cache = CAL(sizeof(mtvn_t) * vector->length, NULL, NULL); - cachei = 1; + cache = CAL(sizeof(mtvn_t) * vector->length, NULL, NULL); + cachei = 1; - for (int index = 0; index < vector->length; index++) mt_vector_sort_ins(cache, vector->data[index], comp); - int index = 0; + for (size_t index = 0; index < vector->length; index++) mt_vector_sort_ins(cache, vector->data[index], comp); - mt_vector_sort_ord(cache, vector, &index); + size_t index = 0; - REL(cache); + mt_vector_sort_ord(cache, vector, &index); + + REL(cache); + } } void mt_vector_describe(void* pointer, int level) { mt_vector_t* vector = pointer; - for (uint32_t index = 0; index < vector->length; index++) + for (size_t index = 0; index < vector->length; index++) { mt_memory_describe(vector->data[index], level + 1); printf("\n"); diff --git a/src/mt_core/mt_wrapper.c b/src/mt_core/mt_wrapper.c index 94e36fb..d56ba62 100644 --- a/src/mt_core/mt_wrapper.c +++ b/src/mt_core/mt_wrapper.c @@ -1,8 +1,8 @@ +/* wraps a non-mtmemory managed pointer */ + #ifndef mt_wrapper_h #define mt_wrapper_h -/* TODO separate unit tests */ - #include typedef struct diff --git a/src/kinetic_ui/utf8.h b/src/mt_core/utf8.h similarity index 100% rename from src/kinetic_ui/utf8.h rename to src/mt_core/utf8.h diff --git a/src/mt_core_ext/ku_bitmap_ext.c b/src/mt_core_ext/ku_bitmap_ext.c new file mode 100644 index 0000000..bad76e2 --- /dev/null +++ b/src/mt_core_ext/ku_bitmap_ext.c @@ -0,0 +1,91 @@ +#ifndef bm_util_h +#define bm_util_h + +#include "ku_bitmap.c" +#include + +ku_bitmap_t* bm_new_flip_y(ku_bitmap_t* bm); +void bm_write(ku_bitmap_t* bm, char* path); + +#endif + +#if __INCLUDE_LEVEL__ == 0 + +ku_bitmap_t* bm_new_flip_y(ku_bitmap_t* bm) +{ + ku_bitmap_t* tmp = ku_bitmap_new(bm->w, bm->h); + for (int y = 0; y < bm->h; y++) + { + int src_y = bm->h - y - 1; + memcpy(tmp->data + y * bm->w * 4, bm->data + src_y * bm->w * 4, bm->w * 4); + } + return tmp; +} + +void bm_write(ku_bitmap_t* bm, char* path) +{ + int w = bm->w; + int h = bm->h; + + FILE* f; + unsigned char* img = NULL; + int filesize = 54 + 3 * w * h; // w is your image width, h is image height, both int + + img = (unsigned char*) malloc(3 * w * h); + memset(img, 0, 3 * w * h); + + for (int i = 0; i < w; i++) + { + for (int j = 0; j < h; j++) + { + int index = j * w * 4 + i * 4; + + int x = i; + int y = j; + + int r = bm->data[index]; + int g = bm->data[index + 1]; + int b = bm->data[index + 2]; + + if (r > 255) r = 255; + if (g > 255) g = 255; + if (b > 255) b = 255; + + img[(x + y * w) * 3 + 2] = (unsigned char) (r); + img[(x + y * w) * 3 + 1] = (unsigned char) (g); + img[(x + y * w) * 3 + 0] = (unsigned char) (b); + } + } + + unsigned char bmpfileheader[14] = {'B', 'M', 0, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0}; + unsigned char bmpinfoheader[40] = {40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0}; + unsigned char bmppad[3] = {0, 0, 0}; + + bmpfileheader[2] = (unsigned char) (filesize); + bmpfileheader[3] = (unsigned char) (filesize >> 8); + bmpfileheader[4] = (unsigned char) (filesize >> 16); + bmpfileheader[5] = (unsigned char) (filesize >> 24); + + bmpinfoheader[4] = (unsigned char) (w); + bmpinfoheader[5] = (unsigned char) (w >> 8); + bmpinfoheader[6] = (unsigned char) (w >> 16); + bmpinfoheader[7] = (unsigned char) (w >> 24); + bmpinfoheader[8] = (unsigned char) (h); + bmpinfoheader[9] = (unsigned char) (h >> 8); + bmpinfoheader[10] = (unsigned char) (h >> 16); + bmpinfoheader[11] = (unsigned char) (h >> 24); + + f = fopen(path, "wb"); + fwrite(bmpfileheader, 1, 14, f); + fwrite(bmpinfoheader, 1, 40, f); + for (int i = 0; i < h; i++) + { + fwrite(img + (w * (h - i - 1) * 3), 3, w, f); + fwrite(bmppad, 1, (4 - (w * 3) % 4) % 4, f); + } + + free(img); + fclose(f); +} + +#endif diff --git a/src/mt_core_ext/mt_string_ext.c b/src/mt_core_ext/mt_string_ext.c index e0a2cee..2c8cf8f 100644 --- a/src/mt_core_ext/mt_string_ext.c +++ b/src/mt_core_ext/mt_string_ext.c @@ -185,17 +185,18 @@ char* consonants = "bcdefghijklmnpqrstvwxyz"; char* mt_string_new_readablec(uint32_t length) { char* result = CAL(sizeof(char) * (length + 1), NULL, mt_string_describe); - for (int index = 0; index < length; index += 2) + for (size_t index = 0; index < length; index += 2) { result[index] = consonants[rand() % strlen(consonants)]; - if (index + 1 < length) result[index + 1] = vowels[rand() % strlen(vowels)]; + if (index + 1 < length) + result[index + 1] = vowels[rand() % strlen(vowels)]; } return result; } void mt_string_tolower(char* str) { - for (int index = 0; index < strlen(str); index++) + for (size_t index = 0; index < strlen(str); index++) { str[index] = tolower(str[index]); } @@ -211,7 +212,7 @@ char* mt_string_alphanumeric = char* mt_string_new_alphanumeric(uint32_t length) { char* result = CAL(sizeof(char) * (length + 1), NULL, mt_string_describe); - for (int index = 0; index < length; index++) + for (size_t index = 0; index < length; index++) { result[index] = mt_string_alphanumeric[rand() % strlen(mt_string_alphanumeric)]; } @@ -246,7 +247,7 @@ char* mt_string_unescape(char* str) size_t length = strlen(str); char* result = CAL((length + 1) * sizeof(char), NULL, mt_string_describe); int ni = 0; - for (int index = 0; index < length; index++) + for (size_t index = 0; index < length; index++) { if (str[index] == '\\') { diff --git a/src/mt_core_test/mt_channel_test.c b/src/mt_core_test/mt_channel_test.c index 7c681b7..6b4ee33 100644 --- a/src/mt_core_test/mt_channel_test.c +++ b/src/mt_core_test/mt_channel_test.c @@ -1,67 +1,116 @@ +#ifndef mt_channel_test_h +#define mt_channel_test_h -// -// TEST -/* TODO move this outside */ -// +void mt_channel_test_main(); -#define kChTestThreads 10 +#endif -void send_test(mt_channel_t* ch) +#if __INCLUDE_LEVEL__ == 0 + +#include "mt_channel.c" +#include "mt_log.c" +#include "mt_map.c" + +#define kChTestThreads 20 +#define kChTestMax 60000 + +static int success = 1; + +void* send_test(void* chp) { - uint32_t counter = 0; + mt_channel_t* ch = chp; + uint32_t counter = 0; while (1) { + if (success == 0) + break; + uint32_t* number = CAL(sizeof(uint32_t), NULL, NULL); *number = counter; - char success = mt_channel_send(ch, number); - if (success == 0) + char sent = mt_channel_send(ch, number); + + if (sent == 0) + { REL(number); + } else + { counter += 1; - if (counter == UINT32_MAX - 1) - counter = 0; + if (counter == kChTestMax) + break; + } + // struct timespec time; // time.tv_sec = 0; // time.tv_nsec = rand() % 100000; // nanosleep(&time , (struct timespec *)NULL); } + + return NULL; } -void recv_test(mt_channel_t* ch) +void* recv_test(void* chp) { - uint32_t last = 0; + mt_channel_t* ch = chp; + uint32_t last = 0; while (1) { + if (success == 0) + break; + uint32_t* number = mt_channel_recv(ch); if (number != NULL) { if (*number != last) - printf("index error!!!"); + { + success = 0; + /* printf("index error!!! %u %u %i\n", *number, last, success); */ + break; + } + REL(number); last += 1; - if (last == UINT32_MAX - 1) - last = 0; - if (last % 100000 == 0) - printf("%zx OK %u %u", (size_t) ch, last, UINT32_MAX); + + if (last == kChTestMax) + break; + // struct timespec time; // time.tv_sec = 0; // time.tv_nsec = rand() % 100000; // nanosleep(&time , (struct timespec *)NULL); } } + + return NULL; } mt_channel_t** testarray; -void mt_channel_test() +void mt_channel_test_main() { + mt_log_debug("testing mt_channel"); + testarray = CAL(sizeof(mt_channel_t) * kChTestThreads, NULL, NULL); + pthread_t* threads = CAL(sizeof(pthread_t) * kChTestThreads * 2, NULL, NULL); + + uint32_t threadidx = 0; + for (int index = 0; index < kChTestThreads; index++) { testarray[index] = mt_channel_new(100); - /* pthread_t thread; */ - /* pthread_create(&thread, NULL, (void*)send_test, testarray[index]); */ - /* pthread_create(&thread, NULL, (void*)recv_test, testarray[index]); */ + pthread_create(&threads[threadidx++], NULL, send_test, testarray[index]); + pthread_create(&threads[threadidx++], NULL, recv_test, testarray[index]); + } + + for (uint32_t index = 0; index < threadidx; index++) + { + pthread_join(threads[index], NULL); } + + REL(threads); + + assert(success == 1); } + +#endif diff --git a/src/mt_core_test/mt_core_test.c b/src/mt_core_test/mt_core_test.c new file mode 100644 index 0000000..15d7649 --- /dev/null +++ b/src/mt_core_test/mt_core_test.c @@ -0,0 +1,21 @@ +#include "mt_channel_test.c" +#include "mt_log.c" +#include "mt_map_test.c" +#include "mt_memory_test.c" +#include "mt_path_test.c" +#include "mt_string_test.c" +#include "mt_vector_test.c" + +int main(int argc, char* argv[]) +{ + mt_log_set_level(MT_LOG_DEBUG); + + mt_memory_test_main(); + mt_vector_test_main(); + mt_map_test_main(); + mt_string_test_main(); + mt_channel_test_main(); + mt_path_test_main(); + + return 0; +} diff --git a/src/mt_core_test/mt_map_test.c b/src/mt_core_test/mt_map_test.c index 296ea36..25fee63 100644 --- a/src/mt_core_test/mt_map_test.c +++ b/src/mt_core_test/mt_map_test.c @@ -1,25 +1,112 @@ +#ifndef mt_map_test_h +#define mt_map_test_h -// tests map +void mt_map_test_main(); -#ifdef DEBUG -void mt_map_test() +#endif + +#if __INCLUDE_LEVEL__ == 0 + +#include "mt_log.c" +#include "mt_map.c" + +void mt_map_test_main() { - printf("MAP TEST SESSION START"); - printf("1 CREATE EMPTY"); - mt_map_t* m1 = mt_map_new(); - printf("2 DELETE EMPTY"); - REL(m1); - printf("3 ADDING DATA"); - mt_map_t* m2 = mt_map_new(); - mt_map_put(m2, "fakk", "fakkvalue"); - mt_map_put(m2, "makk", "makkvalue"); - mt_map_put(m2, "takk", "takkvalue"); - mt_map_put(m2, "kakk", "kakkvalue"); - printf("4 GETTING DATA"); - printf(" VALUE FOR makk : %s", (char*) mt_map_get(m2, "makk")); - printf("5 SETTING DATA TO NULL"); - mt_map_put(m2, "takk", NULL); - printf(" VALUE FOR takk : %s", (char*) mt_map_get(m2, "takk")); - printf("MAP TEST SESSION END"); + char* text1 = "Test text 1"; + char* text2 = "Test text 2"; + + char* ttext1 = mt_memory_stack_to_heap(strlen(text1) + 1, NULL, NULL, (char*) text1); + char* ttext2 = mt_memory_stack_to_heap(strlen(text2) + 1, NULL, NULL, (char*) text2); + + /* checking if map is allocated correctly */ + + mt_log_debug("testing mt_map_new"); + + mt_map_t* map1 = mt_map_new(); + + assert(map1 != NULL); + assert(map1->count == 0); + + /* checking if put works correctly */ + + mt_log_debug("testing mt_map_put"); + + mt_map_put(map1, "text1", ttext1); + + char* ctext1 = mt_map_get(map1, "text1"); + + assert(ttext1 == ctext1); + + /* checking if put release working correctly by checking retain counter */ + + mt_log_debug("testing mt_map_put_rel"); + + mt_map_put_rel(map1, "text2", ttext2); + + char* ctext2 = mt_map_get(map1, "text2"); + + assert(ttext2 == ctext2); + assert(mt_memory_retaincount(ttext2) == 1); + + /* checking if map_keys working correctly */ + + mt_log_debug("testing mt_map_keys"); + + mt_vector_t* keys = mt_vector_new(); + + mt_map_keys(map1, keys); + + assert(keys->length == 2); + + for (size_t index = 0; index < keys->length; index++) + { + char* key = keys->data[index]; + + assert(strcmp(key, "text1") == 0 || strcmp(key, "text2") == 0); + } + + REL(keys); + + /* checking if map_values working correctly */ + + mt_log_debug("testing mt_map_values"); + + mt_vector_t* vals = mt_vector_new(); + + mt_map_values(map1, vals); + + assert(vals->length == 2); + + for (size_t index = 0; index < vals->length; index++) + { + char* val = vals->data[index]; + + assert(val == ttext1 || val == ttext2); + } + + REL(vals); + + /* checking if map_del working correctly */ + + mt_log_debug("testing mt_map_del"); + + mt_map_del(map1, "text2"); + + assert(map1->count == 1); + assert(mt_map_get(map1, "text2") == NULL); + + /* checking if map_reset working correctly */ + + mt_log_debug("testing mt_map_reset"); + + mt_map_reset(map1); + + assert(map1->count == 0); + + /* cleanup, run with leak sanitizer on to check for hidden leaks */ + + REL(ttext1); + REL(map1); } + #endif diff --git a/src/mt_core_test/mt_memory_test.c b/src/mt_core_test/mt_memory_test.c new file mode 100644 index 0000000..ceb4d85 --- /dev/null +++ b/src/mt_core_test/mt_memory_test.c @@ -0,0 +1,135 @@ +#ifndef mt_memory_test_h +#define mt_memory_test_h + +void mt_memory_test_main(); + +#endif + +#if __INCLUDE_LEVEL__ == 0 + +#include "mt_log.c" +#include "mt_memory.c" +#include + +void test_alloc() +{ + mt_log_debug("testing mt_memory_alloc"); + + char* atext = NULL; + + atext = mt_memory_alloc(sizeof(char) * 80, NULL, NULL); + + assert(atext != NULL); + assert(mt_memory_retaincount(atext) == 1); + + mt_memory_release(atext); +} + +void test_calloc() +{ + mt_log_debug("testing mt_memory_calloc"); + + char* ctext = NULL; + + ctext = mt_memory_calloc(sizeof(char) * 80, NULL, NULL); + + assert(ctext != NULL); + assert(mt_memory_retaincount(ctext) == 1); + + mt_memory_release(ctext); +} + +void test_stack_to_heap() +{ + mt_log_debug("testing mt_memory_stack_to_heap"); + + char* text = "This is a test string"; + char* ttext = mt_memory_stack_to_heap(strlen(text) + 1, NULL, NULL, (char*) text); + + assert(ttext != NULL); + assert(mt_memory_retaincount(ttext) == 1); + assert(strcmp(ttext, text) == 0); + + mt_memory_release(ttext); +} + +void test_realloc() +{ + mt_log_debug("testing mt_memory_realloc"); + + char* text0 = "This is a test string"; + char* text1 = "Another text string"; + char* ttext = mt_memory_calloc(strlen(text0) + 1, NULL, NULL); + memcpy(ttext, text0, strlen(text0)); + + ttext = mt_memory_realloc(ttext, strlen(text0) + strlen(text1) + 1); + memcpy(ttext + strlen(text0), text1, strlen(text1)); + + assert(ttext != NULL); + assert(mt_memory_retaincount(ttext) == 1); + assert(strstr(ttext, text0) != NULL); + assert(strstr(ttext, text1) != NULL); + + mt_memory_release(ttext); +} + +int destruct_count = 1; + +void destruct() +{ + destruct_count -= 1; +} + +void test_destructor() +{ + mt_log_debug("testing mt_memory descructor"); + + char* atext = NULL; + + atext = mt_memory_alloc(sizeof(char) * 80, destruct, NULL); + + assert(atext != NULL); + assert(mt_memory_retaincount(atext) == 1); + + mt_memory_release(atext); + + assert(destruct_count == 0); +} + +int describe_count = 1; + +void describe() +{ + describe_count -= 1; +} + +void test_descriptor() +{ + mt_log_debug("testing mt_memory descriptor"); + + char* atext = NULL; + + atext = mt_memory_alloc(sizeof(char) * 80, NULL, describe); + + assert(atext != NULL); + assert(mt_memory_retaincount(atext) == 1); + + mt_memory_describe(atext, 0); + mt_memory_release(atext); + + assert(describe_count == 0); +} + +/* run test with clang leak sanitizer */ + +void mt_memory_test_main() +{ + test_alloc(); + test_calloc(); + test_stack_to_heap(); + test_realloc(); + test_destructor(); + test_descriptor(); +} + +#endif diff --git a/src/mt_core_test/mt_path_test.c b/src/mt_core_test/mt_path_test.c new file mode 100644 index 0000000..31a4fa6 --- /dev/null +++ b/src/mt_core_test/mt_path_test.c @@ -0,0 +1,64 @@ +#ifndef mt_path_test_h +#define mt_path_test_h + +void mt_path_test_main(); + +#endif + +#if __INCLUDE_LEVEL__ == 0 + +#include "mt_log.c" +#include "mt_path.c" +#include +#include + +void mt_path_test_main() +{ + /* checking if mt_path_new_format works */ + + mt_log_debug("testing mt_path_new_append"); + + char* path1 = mt_path_new_append("root", "exec/"); + + assert(strcmp(path1, "root/exec/") == 0); + + /* checking if mt_path_new_remove_last_component works */ + + mt_log_debug("testing mt_path_new_remove_last_component"); + + char* path2 = mt_path_new_remove_last_component("/home/milgra/whatever.ext"); + + assert(strcmp(path2, "/home/milgra/") == 0); + + /* checking if mt_path_new_extension works */ + + mt_log_debug("testing mt_path_new_extension"); + + char* path3 = mt_path_new_extension("/home/milgra/whatever.ext"); + + assert(strcmp(path3, "ext") == 0); + + /* checking if mt_path_new_filename works */ + + mt_log_debug("testing mt_path_new_filename"); + + char* path4 = mt_path_new_filename("/home/milgra/whatever.ext"); + + assert(strcmp(path4, "whatever") == 0); + + /* checking if mt_path_new_normalize works */ + + mt_log_debug("testing mt_path_new_normalize"); + + char* path5 = mt_path_new_normalize("//milcsi/../whatever.ext"); + + assert(strcmp(path5, "//whatever.ext")); + + REL(path1); + REL(path2); + REL(path3); + REL(path4); + REL(path5); +} + +#endif diff --git a/src/mt_core_test/mt_string_test.c b/src/mt_core_test/mt_string_test.c index 05c1321..d039853 100644 --- a/src/mt_core_test/mt_string_test.c +++ b/src/mt_core_test/mt_string_test.c @@ -1,6 +1,99 @@ +#ifndef mt_string_test_h +#define mt_string_test_h + +void mt_string_test_main(); + +#endif + +#if __INCLUDE_LEVEL__ == 0 + +#include "mt_log.c" #include "mt_string.c" -void mt_string_test() +void mt_string_test_main() { - char* str = "000 Állítólag svájcban"; + /* char* str = "000 Állítólag svájcban"; */ + + /* checking if mt_string_new_format works */ + + mt_log_debug("testing mt_string_new_format"); + + char* str1 = mt_string_new_format(100, "%s-%i-%x", "milcsi", 100, 64); + + assert(strcmp(str1, "milcsi-100-40") == 0); + + /* checking if mt_string_new_cstring works */ + + mt_log_debug("testing mt_string_new_cstring"); + + char* str2 = mt_string_new_cstring("another cstring"); + + assert(strcmp(str2, "another cstring") == 0); + + /* checking if mt_string_new_substring works */ + + mt_log_debug("testing mt_string_new_substring"); + + char* str3 = mt_string_new_substring("another cstring", 3, 6); + + assert(strcmp(str3, "ther c") == 0); + + /* checking if mt_string_reset works */ + + mt_log_debug("testing mt_string_reset"); + + str1 = mt_string_reset(str1); + + assert(strlen(str1) == 0); + + /* checking if mt_string_append works */ + + mt_log_debug("testing mt_string_append"); + + str1 = mt_string_append(str1, "new string"); + str1 = mt_string_append(str1, "|old string"); + + assert(strcmp(str1, "new string|old string") == 0); + + /* checking if mt_string_append works */ + + mt_log_debug("testing mt_string_append_cp"); + + str1 = mt_string_append_cp(str1, 588); + + assert(strcmp(str1, "new string|old stringɌ") == 0); + + /* checking if mt_string_append_sub works */ + + mt_log_debug("testing mt_string_append_sub"); + + str2 = mt_string_append_sub(str2, "WHAT THE", 2, 3); + + assert(strcmp(str2, "another cstringAT ") == 0); + + /* checking if mt_string_delete_utf_codepoints works */ + + mt_log_debug("testing mt_string_delete_utf_codepoints"); + + str1 = mt_string_delete_utf_codepoints(str1, 4, 18); + + assert(strcmp(str1, "new ") == 0); + + /* checking if mt_string_tokenize works */ + + mt_log_debug("testing mt_string_tokenize"); + + mt_vector_t* vec = mt_string_tokenize("ONE TWO THREE", " "); + + assert(vec->length == 3); + assert(strcmp(vec->data[0], "ONE") == 0); + assert(strcmp(vec->data[1], "TWO") == 0); + assert(strcmp(vec->data[2], "THREE") == 0); + + REL(vec); + REL(str1); + REL(str2); + REL(str3); } + +#endif diff --git a/src/mt_core_test/mt_vector_test.c b/src/mt_core_test/mt_vector_test.c new file mode 100644 index 0000000..df16daa --- /dev/null +++ b/src/mt_core_test/mt_vector_test.c @@ -0,0 +1,171 @@ +#ifndef mt_vector_test_h +#define mt_vector_test_h + +void mt_vector_test_main(); + +#endif + +#if __INCLUDE_LEVEL__ == 0 + +#include "mt_log.c" +#include "mt_vector.c" + +void mt_vector_test_main() +{ + char* text1 = "Test text 1"; + char* text2 = "Test text 2"; + char* text3 = "Test text 3"; + char* text4 = "Test text 4"; + + char* ttext1 = mt_memory_stack_to_heap(strlen(text1) + 1, NULL, NULL, (char*) text1); + char* ttext2 = mt_memory_stack_to_heap(strlen(text2) + 1, NULL, NULL, (char*) text2); + char* ttext3 = mt_memory_stack_to_heap(strlen(text3) + 1, NULL, NULL, (char*) text3); + char* ttext4 = mt_memory_stack_to_heap(strlen(text4) + 1, NULL, NULL, (char*) text4); + + /* checking if vector allocated correctly */ + + mt_log_debug("testing mt_vector_new"); + + mt_vector_t* vec1 = mt_vector_new(); + + assert(vec1 != NULL); + assert(vec1->length == 0); + + /* checking if add works correctly */ + + mt_log_debug("testing mt_vector_add"); + + mt_vector_add(vec1, ttext1); + + assert(vec1->length == 1); + assert(vec1->data[0] == ttext1); + + /* checking if add release works correctly */ + + mt_log_debug("testing mt_vector_add_rel"); + + mt_vector_add_rel(vec1, ttext2); + + assert(vec1->length == 2); + assert(vec1->data[1] == ttext2); + assert(mt_memory_retaincount(ttext2) == 1); + + /* checking if insert works correctly */ + + mt_log_debug("testing mt_vector_insert"); + + mt_vector_ins(vec1, ttext3, 1); + + assert(vec1->length == 3); + assert(vec1->data[1] == ttext3); + + /* checking if insert release works correctly */ + + mt_log_debug("testing mt_vector_insert_rel"); + + mt_vector_ins_rel(vec1, ttext4, 2); + + assert(vec1->length == 4); + assert(vec1->data[2] == ttext4); + assert(mt_memory_retaincount(ttext4) == 1); + + RET(ttext4); + + /* checking if remove data works correctly */ + + mt_log_debug("testing mt_vector_rem"); + + mt_vector_rem(vec1, ttext1); + + assert(vec1->length == 3); + assert(vec1->data[0] == ttext3); + + /* checking if remove index works correctly */ + + mt_log_debug("testing mt_vector_rem_index"); + + mt_vector_rem_index(vec1, 1); + + assert(vec1->length == 2); + assert(vec1->data[1] == ttext2); + + /* checking if remove range works correctly */ + + mt_log_debug("testing mt_vector_rem_range"); + + mt_vector_rem_range(vec1, 0, 1); + + assert(vec1->length == 0); + + /* checking add in vector */ + + mt_vector_t* vec2 = mt_vector_new(); + mt_vector_add(vec2, ttext1); + mt_vector_add(vec2, ttext2); + mt_vector_add(vec2, ttext3); + mt_vector_add(vec2, ttext4); + + mt_log_debug("testing mt_vector_add_in_vector"); + + mt_vector_add_in_vector(vec1, vec2); + + assert(vec1->length == 4); + assert(vec1->data[3] == ttext4); + + /* checking remove in vector */ + + mt_log_debug("testing mt_vector_rem_in_vector"); + + mt_vector_rem_in_vector(vec1, vec2); + + assert(vec1->length == 0); + + /* checking head and tail */ + + mt_vector_add(vec1, ttext1); + mt_vector_add(vec1, ttext2); + mt_vector_add(vec1, ttext3); + mt_vector_add(vec1, ttext4); + + mt_log_debug("testing mt_vector_head"); + + assert(mt_vector_head(vec1) == ttext1); + + mt_log_debug("testing mt_vector_tail"); + + assert(mt_vector_tail(vec1) == ttext4); + + /* checking reverse */ + + mt_log_debug("testing mt_vector_reverse"); + + mt_vector_reverse(vec1); + + assert(vec1->length == 4); + assert(vec1->data[0] == ttext4); + assert(vec1->data[3] == ttext1); + + /* checking sort */ + + mt_log_debug("testing mt_vector_sort"); + + mt_vector_sort(vec1, (int (*)(void*, void*)) strcmp); + + assert(vec1->length == 4); + assert(vec1->data[0] == ttext1); + assert(vec1->data[3] == ttext4); + + /* checking index of */ + + assert(mt_vector_index_of_data(vec1, ttext2) == 1); + + REL(vec1); + REL(vec2); + REL(ttext1); + REL(ttext2); + REL(ttext3); + REL(ttext4); + REL(ttext4); +} + +#endif diff --git a/src/mt_math_test/mt_math_2d_test.c b/src/mt_math_test/mt_math_2d_test.c index e69de29..60d140a 100644 --- a/src/mt_math_test/mt_math_2d_test.c +++ b/src/mt_math_test/mt_math_2d_test.c @@ -0,0 +1,8 @@ +#include "mt_log.c" + +int main(int argc, char* argv[]) +{ + mt_log_set_level(MT_LOG_DEBUG); + + return 0; +} diff --git a/src/sov/draw.c b/src/sov/draw.c index e301667..10f1290 100644 --- a/src/sov/draw.c +++ b/src/sov/draw.c @@ -51,10 +51,7 @@ int main(int argc, char** argv) /* init config */ - char cwd[PATH_MAX]; - if (getcwd(cwd, sizeof(cwd)) == NULL) printf("Cannot get working directory\n"); - - char* cfg_path = mt_path_new_normalize(cfg_par, cwd); + char* cfg_path = mt_path_new_normalize(cfg_par); char* css_path = mt_path_new_append(cfg_path, "html/main.css"); // REL 6 char* html_path = mt_path_new_append(cfg_path, "html/main.html"); // REL 7 char* img_path = mt_path_new_append(cfg_path, "img"); // REL 6 diff --git a/src/sov/gen.c b/src/sov/gen.c index 06be3c2..df4807e 100644 --- a/src/sov/gen.c +++ b/src/sov/gen.c @@ -76,7 +76,7 @@ void gen_destroy() { /* reset window */ - for (int index = base->views->length - 1; index > -1; index--) + for (size_t index = base->views->length; index-- > 0;) { ku_view_t* view = base->views->data[index]; ku_view_remove_from_parent(view); @@ -115,7 +115,7 @@ void gen_render( /* reset window */ - for (int index = base->views->length - 1; index > -1; index--) + for (size_t index = base->views->length; index-- > 0;) { ku_view_t* view = base->views->data[index]; ku_view_remove_from_parent(view); @@ -128,7 +128,7 @@ void gen_render( /* add rows */ - int wsi = 0; + size_t wsi = 0; for (int rowi = 0; rowi < rows; rowi++) { @@ -147,7 +147,7 @@ void gen_render( for (int coli = 0; coli < cols; coli++) { char name[100] = {0}; - snprintf(name, 100, "workspace%i", wsi); + snprintf(name, 100, "workspace%li", wsi); ku_view_t* wsview = ku_view_new(name, (ku_rect_t){0, 0, 100, 100}); wsview->style = workspace->style; tg_css_add(wsview); @@ -164,7 +164,7 @@ void gen_render( char numname[100] = {0}; char numnumb[10] = {0}; - snprintf(numname, 100, "number%i%i", wsi, 0); + snprintf(numname, 100, "number%li%i", wsi, 0); if (use_name) snprintf(numnumb, 10, "%s", ws->name); else @@ -203,7 +203,7 @@ void gen_render( { sway_workspace_t* ws = workspaces->data[wsi]; - for (int wii = 0; wii < ws->windows->length; wii++) + for (size_t wii = 0; wii < ws->windows->length; wii++) { sway_window_t* wi = ws->windows->data[wii]; @@ -235,9 +235,9 @@ void gen_render( char winname[100] = {0}; char titlename[100] = {0}; char contentname[100] = {0}; - snprintf(winname, 100, "window%i", wii); - snprintf(titlename, 100, "title%i", wii); - snprintf(contentname, 100, "content%i", wii); + snprintf(winname, 100, "window%li", wii); + snprintf(titlename, 100, "title%li", wii); + snprintf(contentname, 100, "content%li", wii); ku_view_t* winview = ku_view_new(winname, (ku_rect_t){wix, wiy, wiw, wih}); winview->style = window->style; diff --git a/src/sov/sov.c b/src/sov/sov.c index 14a7075..a1972ca 100644 --- a/src/sov/sov.c +++ b/src/sov/sov.c @@ -93,7 +93,7 @@ void create_layers() mt_vector_t* workspaces = VNEW(); // REL 1 - for (int index = 0; index < sov.workspaces->length; index++) + for (size_t index = 0; index < sov.workspaces->length; index++) { sway_workspace_t* ws = sov.workspaces->data[index]; if (strcmp(ws->output, monitor->name) == 0) VADD(workspaces, ws); @@ -137,7 +137,7 @@ void update(ku_event_t ev) mt_vector_t* workspaces = VNEW(); - for (int index = 0; index < sov.workspaces->length; index++) + for (size_t index = 0; index < sov.workspaces->length; index++) { sway_workspace_t* ws = sov.workspaces->data[index]; if (strcmp(ws->output, info->monitor->name) == 0) VADD(workspaces, ws); @@ -185,7 +185,7 @@ void update(ku_event_t ev) if (sov.wlwindows->length > 0) { - for (int w = 0; w < sov.wlwindows->length; w++) + for (size_t w = 0; w < sov.wlwindows->length; w++) { wl_window_t* window = sov.wlwindows->data[w]; ku_wayland_delete_window(window); @@ -215,7 +215,7 @@ void update(ku_event_t ev) if (sov.wlwindows->length > 0) { - for (int w = 0; w < sov.wlwindows->length; w++) + for (size_t w = 0; w < sov.wlwindows->length; w++) { wl_window_t* window = sov.wlwindows->data[w]; ku_wayland_delete_window(window); @@ -328,8 +328,8 @@ int main(int argc, char** argv) char cwd[PATH_MAX]; if (getcwd(cwd, sizeof(cwd)) == NULL) printf("Cannot get working directory\n"); - char* cfg_path_loc = cfg_par ? mt_path_new_normalize(cfg_par, cwd) : mt_path_new_normalize(CFG_PATH_LOC, getenv("HOME")); // REL 1 - char* cfg_path_glo = STRNC(PKG_DATADIR); // REL 2 + char* cfg_path_loc = cfg_par ? mt_path_new_normalize(cfg_par) : mt_path_new_normalize(CFG_PATH_LOC); // REL + char* cfg_path_glo = STRNC(PKG_DATADIR); // REL if (cfg_par) REL(cfg_par); @@ -342,9 +342,9 @@ int main(int argc, char** argv) else sov.cfg_path = cfg_path_glo; - sov.css_path = mt_path_new_append(sov.cfg_path, "html/main.css"); // REL 6 - sov.html_path = mt_path_new_append(sov.cfg_path, "html/main.html"); // REL 7 - sov.img_path = mt_path_new_append(sov.cfg_path, "img"); // REL 6 + sov.css_path = mt_path_new_append(sov.cfg_path, "html/main.css"); // REL + sov.html_path = mt_path_new_append(sov.cfg_path, "html/main.html"); // REL + sov.img_path = mt_path_new_append(sov.cfg_path, "img"); // REL printf("style path : %s\n", sov.cfg_path); printf("css path : %s\n", sov.css_path); @@ -361,14 +361,14 @@ int main(int argc, char** argv) /* init text rendering */ - ku_text_init(); // DESTROY 1 + ku_text_init(); // DESTROY ku_wayland_init(init, update, destroy, 0); /* cleanup */ - REL(cfg_path_glo); // REL 2 - REL(cfg_path_loc); // REL 1 + REL(cfg_path_glo); + REL(cfg_path_loc); REL(sov.css_path); REL(sov.html_path); REL(sov.img_path); diff --git a/src/sov/tree.c b/src/sov/tree.c index 8dba6ea..0f1064c 100644 --- a/src/sov/tree.c +++ b/src/sov/tree.c @@ -41,6 +41,7 @@ void sway_workspace_del(void* p) { sway_workspace_t* ws = p; REL(ws->windows); + if (ws->name) REL(ws->name); if (ws->output) REL(ws->output); } @@ -80,11 +81,11 @@ sway_window_t* sway_window_new() char* sway_get_value(mt_vector_t* vec, int pos, char* path) { - for (int index = pos; index < vec->length; index++) + for (size_t index = pos; index < vec->length; index++) { if (strcmp(vec->data[index], path) == 0) return vec->data[index + 1]; } - for (int index = pos; index > -1; index--) + for (size_t index = pos; index-- > 0;) { if (strcmp(vec->data[index], path) == 0) return vec->data[index + 1]; } @@ -98,7 +99,7 @@ void tree_reader_extract(char* ws_json, char* tree_json, mt_vector_t* workspaces // find workspaces - for (int index = 0; index < json->length; index += 2) + for (size_t index = 0; index < json->length; index += 2) { char* key = json->data[index]; char* type_prt = strstr(key, "/type"); @@ -117,7 +118,7 @@ void tree_reader_extract(char* ws_json, char* tree_json, mt_vector_t* workspaces char* ok = mt_string_new_format(pathlen + 20, "%soutput", path); // REL 7 char* nk = mt_string_new_format(pathlen + 20, "%snum", path); // REL 8 char* fk = mt_string_new_format(pathlen + 20, "%sfocused", path); // REL 9 - char* nmk = mt_string_new_format(pathlen + 20, "%sname", path); // REL 7 + char* nmk = mt_string_new_format(pathlen + 20, "%sname", path); // REL 10 char* x = sway_get_value(json, index, wx); char* y = sway_get_value(json, index, wy); @@ -162,7 +163,7 @@ void tree_reader_extract(char* ws_json, char* tree_json, mt_vector_t* workspaces int curr_wspc_n = 0; - for (int index = 0; index < json->length; index += 2) + for (size_t index = 0; index < json->length; index += 2) { char* key = json->data[index]; char* type_prt = strstr(key, "type"); @@ -218,7 +219,7 @@ void tree_reader_extract(char* ws_json, char* tree_json, mt_vector_t* workspaces mt_log_debug("Found window, appid %s title %s %i %i %i %i", wi->appid, wi->title, wi->x, wi->y, wi->width, wi->height); - for (int wsi = 0; wsi < workspaces->length; wsi++) + for (size_t wsi = 0; wsi < workspaces->length; wsi++) { sway_workspace_t* ws = workspaces->data[wsi]; diff --git a/src/tests/test_json.c b/src/tests/test_json.c deleted file mode 100644 index 9926e5c..0000000 --- a/src/tests/test_json.c +++ /dev/null @@ -1,7 +0,0 @@ -#include - -int main(int argc, char* argv[]) -{ - printf("TEST OKAY\n"); - return 0; -}