Skip to content

Commit

Permalink
fixup! feat: DPI awareness
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft committed Sep 4, 2024
1 parent bc507a4 commit d6847ad
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 142 deletions.
238 changes: 119 additions & 119 deletions lib/platform/src/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,173 +15,173 @@
#include "events.h"

static struct app_events_t {
/** list_t<app_event_t> */
list_t queue;
/** list_t<app_event_t> */
list_t queue;

/** list_t<app_listener_t> */
list_t listeners;
/** list_t<app_listener_t> */
list_t listeners;

app_event_dispatcher_t dispatcher;
app_event_dispatcher_t dispatcher;
} app_events;

int app_touch_event_init(app_event_t *e, touch_point_t *points, int n_points)
{
e->type = APP_EVENT_TOUCH;
e->touch.n_points = n_points;
e->touch.points = malloc(sizeof(touch_point_t) * n_points);
if (!e->touch.points) {
return -ENOMEM;
}
for (n_points -= 1; n_points >= 0; --n_points) {
e->touch.points[n_points] = points[n_points];
}
return 0;
e->type = APP_EVENT_TOUCH;
e->touch.n_points = n_points;
e->touch.points = malloc(sizeof(touch_point_t) * n_points);
if (!e->touch.points) {
return -ENOMEM;
}
for (n_points -= 1; n_points >= 0; --n_points) {
e->touch.points[n_points] = points[n_points];
}
return 0;
}

int app_composition_event_init(app_event_t *e, const wchar_t *text, size_t len)
{
if (len == 0) {
len = wcslen(text);
}
e->type = APP_EVENT_COMPOSITION;
e->text.length = len;
e->text.text = malloc(sizeof(wchar_t) * (len + 1));
if (!e->text.text) {
return -ENOMEM;
}
wcscpy(e->text.text, text);
return 0;
if (len == 0) {
len = wcslen(text);
}
e->type = APP_EVENT_COMPOSITION;
e->text.length = len;
e->text.text = malloc(sizeof(wchar_t) * (len + 1));
if (!e->text.text) {
return -ENOMEM;
}
wcscpy(e->text.text, text);
return 0;
}

int app_event_copy(app_event_t *dest, app_event_t *src)
{
switch (src->type) {
case APP_EVENT_TOUCH:
return app_touch_event_init(dest, src->touch.points,
src->touch.n_points);
case APP_EVENT_TEXTINPUT:
return app_composition_event_init(dest, src->text.text, 0);
default:
*dest = *src;
break;
}
return 0;
switch (src->type) {
case APP_EVENT_TOUCH:
return app_touch_event_init(dest, src->touch.points,
src->touch.n_points);
case APP_EVENT_TEXTINPUT:
return app_composition_event_init(dest, src->text.text, 0);
default:
*dest = *src;
break;
}
return 0;
}

void app_event_destroy(app_event_t *e)
{
switch (e->type) {
case APP_EVENT_TOUCH:
if (e->touch.points) {
free(e->touch.points);
}
e->touch.points = NULL;
e->touch.n_points = 0;
break;
case APP_EVENT_COMPOSITION:
if (e->text.text) {
free(e->text.text);
}
e->text.text = NULL;
e->text.length = 0;
break;
default:
break;
}
e->type = APP_EVENT_NONE;
switch (e->type) {
case APP_EVENT_TOUCH:
if (e->touch.points) {
free(e->touch.points);
}
e->touch.points = NULL;
e->touch.n_points = 0;
break;
case APP_EVENT_COMPOSITION:
if (e->text.text) {
free(e->text.text);
}
e->text.text = NULL;
e->text.length = 0;
break;
default:
break;
}
e->type = APP_EVENT_NONE;
}

int app_post_event(app_event_t *e)
{
app_event_t *ev;

ev = malloc(sizeof(app_event_t));
if (!ev) {
return -ENOMEM;
}
app_event_copy(ev, e);
list_append(&app_events.queue, ev);
return 0;
app_event_t *ev;

ev = malloc(sizeof(app_event_t));
if (!ev) {
return -ENOMEM;
}
app_event_copy(ev, e);
list_append(&app_events.queue, ev);
return 0;
}

int app_add_event_listener(int event_type, app_event_handler_t handler,
void *data)
void *data)
{
app_event_listener_t *listener;

listener = malloc(sizeof(app_event_listener_t));
if (!listener) {
return -ENOMEM;
}
listener->handler = handler;
listener->data = data;
listener->type = event_type;
list_append(&app_events.listeners, listener);
return 0;
app_event_listener_t *listener;

listener = malloc(sizeof(app_event_listener_t));
if (!listener) {
return -ENOMEM;
}
listener->handler = handler;
listener->data = data;
listener->type = event_type;
list_append(&app_events.listeners, listener);
return 0;
}

int app_remove_event_listener(int event_type, app_event_handler_t handler)
{
list_node_t *node, *prev;
app_event_listener_t *listener;

for (list_each(node, &app_events.listeners)) {
prev = node->prev;
listener = node->data;
if (listener->handler == handler &&
listener->type == event_type) {
list_delete_node(&app_events.listeners, node);
free(listener);
node = prev;
return 0;
}
}
return -1;
list_node_t *node, *prev;
app_event_listener_t *listener;

for (list_each(node, &app_events.listeners)) {
prev = node->prev;
listener = node->data;
if (listener->handler == handler &&
listener->type == event_type) {
list_delete_node(&app_events.listeners, node);
free(listener);
node = prev;
return 0;
}
}
return -1;
}

int app_process_event(app_event_t *e)
{
int count = 0;
list_node_t *node;
app_event_listener_t *listener;

for (list_each(node, &app_events.listeners)) {
listener = node->data;
if (listener->type == e->type) {
listener->handler(e, listener->data);
++count;
}
}
if (app_events.dispatcher) {
app_events.dispatcher(e);
}
return count;
int count = 0;
list_node_t *node;
app_event_listener_t *listener;

for (list_each(node, &app_events.listeners)) {
listener = node->data;
if (listener->type == e->type) {
listener->handler(e, listener->data);
++count;
}
}
if (app_events.dispatcher) {
app_events.dispatcher(e);
}
return count;
}

void app_process_events(void)
{
app_event_t *e;

while ((e = list_get(&app_events.queue, 0)) != NULL) {
list_delete(&app_events.queue, 0);
app_process_event(e);
app_event_destroy(e);
free(e);
}
app_event_t *e;

while ((e = list_get(&app_events.queue, 0)) != NULL) {
list_delete(&app_events.queue, 0);
app_process_event(e);
app_event_destroy(e);
free(e);
}
}

void app_set_event_dispatcher(app_event_dispatcher_t dispatcher)
{
app_events.dispatcher = dispatcher;
app_events.dispatcher = dispatcher;
}

void app_init_events(void)
{
list_create(&app_events.queue);
list_create(&app_events.queue);
}

void app_destroy_events(void)
{
app_set_event_dispatcher(NULL);
list_destroy(&app_events.queue, free);
app_set_event_dispatcher(NULL);
list_destroy(&app_events.queue, free);
}
12 changes: 8 additions & 4 deletions lib/ui-server/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -720,10 +720,14 @@ void ui_server_update(void)
list_node_t *node;
float dpi = 1.f * ui_metrics.dpi;

for (list_each(node, &ui_server.connections)) {
conn = node->data;
ui_metrics.dpi = 1.f * app_window_get_dpi(conn->window);
ui_updater_update(conn->updater, conn->widget);
if (ui_server.connections.length > 0) {
for (list_each(node, &ui_server.connections)) {
conn = node->data;
ui_metrics.dpi = 1.f * app_window_get_dpi(conn->window);
ui_updater_update(conn->updater, conn->widget);
}
} else {
ui_update();
}
ui_metrics.dpi = dpi;
}
Expand Down
8 changes: 5 additions & 3 deletions lib/ui/src/ui_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* LICENSE.TXT file in the root directory of this source tree.
*/

#include <stdio.h>
#include <locale.h>
#include <assert.h>
#include <string.h>
#include <css/selector.h>
Expand Down Expand Up @@ -367,9 +369,6 @@ ui_widget_t *ui_widget_at(ui_widget_t *widget, int ix, int iy)
return target == widget ? NULL : target;
}

#include <locale.h>
#include <stdio.h>

static void _ui_print_tree(ui_widget_t *w, int depth, const wchar_t *prefix)
{
ui_widget_t *child;
Expand Down Expand Up @@ -421,7 +420,9 @@ static void _ui_print_tree(ui_widget_t *w, int depth, const wchar_t *prefix)
void ui_print_tree(ui_widget_t *w)
{
css_selector_node_t *node;
char *ctype = setlocale(LC_CTYPE, NULL);

setlocale(LC_CTYPE, "");
w = w ? w : ui_root();
node = ui_widget_create_selector_node(w);
logger_error("%s, xy:(%g,%g), size:(%g,%g), visible: %s\n",
Expand All @@ -430,4 +431,5 @@ void ui_print_tree(ui_widget_t *w)
ui_widget_is_visible(w) ? "true" : "false");
css_selector_node_destroy(node);
_ui_print_tree(w, 0, L" ");
setlocale(LC_CTYPE, ctype);
}
6 changes: 4 additions & 2 deletions src/lcui_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ static void lcui_dispatch_ui_touch_event(app_event_t *app_event)
default:
break;
}
e.touch.points[i].x = (float)round(app_event->touch.points[i].x / scale);
e.touch.points[i].y = (float)round(app_event->touch.points[i].y / scale);
e.touch.points[i].x =
(float)round(app_event->touch.points[i].x / scale);
e.touch.points[i].y =
(float)round(app_event->touch.points[i].y / scale);
}
ui_dispatch_event(&e);
ui_event_destroy(&e);
Expand Down
Loading

0 comments on commit d6847ad

Please sign in to comment.