Skip to content

Commit cdfe916

Browse files
committed
Add some documenation
- proper mouse movement when display is rotated - add simple way to get current monotonic clock time
1 parent c0b6205 commit cdfe916

File tree

5 files changed

+74
-27
lines changed

5 files changed

+74
-27
lines changed

include/collection.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <stdlib.h>
66
#include <errno.h>
77
#include <stdbool.h>
8+
#include <stdint.h>
89

910
#include <pthread.h>
1011

@@ -407,9 +408,14 @@ static inline void *memdup(const void *restrict src, const size_t n) {
407408
#define min(a, b) ((a) < (b) ? (a) : (b))
408409
#define max(a, b) ((a) > (b) ? (a) : (b))
409410

411+
/**
412+
* @brief Get the current time of the system monotonic clock.
413+
* @returns time in nanoseconds.
414+
*/
410415
static inline uint64_t get_monotonic_time(void) {
411-
/// TODO: Implement
412-
return 0;
416+
struct timespec time;
417+
clock_gettime(CLOCK_MONOTONIC, &time);
418+
return time.tv_nsec + time.tv_sec*1000000000ull;
413419
}
414420

415421
#endif

include/flutter-pi.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,17 @@ struct libudev {
213213
.skewY = 0, .scaleY = 1, .transY = 0, \
214214
.pers0 = -sin(((double) (deg))/180.0*M_PI), .pers1 = 0, .pers2 = cos(((double) (deg))/180.0*M_PI)})
215215

216+
/**
217+
* A flutter transformation that rotates any coords around the z-axis, counter-clockwise.
218+
*/
216219
#define FLUTTER_ROTZ_TRANSFORMATION(deg) ((FlutterTransformation) \
217220
{.scaleX = cos(((double) (deg))/180.0*M_PI), .skewX = -sin(((double) (deg))/180.0*M_PI), .transX = 0, \
218221
.skewY = sin(((double) (deg))/180.0*M_PI), .scaleY = cos(((double) (deg))/180.0*M_PI), .transY = 0, \
219222
.pers0 = 0, .pers1 = 0, .pers2 = 1})
220223

224+
/**
225+
* A transformation that is the result of multiplying a with b.
226+
*/
221227
#define FLUTTER_MULTIPLIED_TRANSFORMATIONS(a, b) ((FlutterTransformation) \
222228
{.scaleX = a.scaleX * b.scaleX + a.skewX * b.skewY + a.transX * b.pers0, \
223229
.skewX = a.scaleX * b.skewX + a.skewX * b.scaleY + a.transX * b.pers1, \
@@ -229,12 +235,24 @@ struct libudev {
229235
.pers1 = a.pers0 * b.skewX + a.pers1 * b.scaleY + a.pers2 * b.pers1, \
230236
.pers2 = a.pers0 * b.transX + a.pers1 * b.transY + a.pers2 * b.pers2})
231237

238+
/**
239+
* A transformation that is the result of adding a with b.
240+
*/
232241
#define FLUTTER_ADDED_TRANSFORMATIONS(a, b) ((FlutterTransformation) \
233242
{.scaleX = a.scaleX + b.scaleX, .skewX = a.skewX + b.skewX, .transX = a.transX + b.transX, \
234243
.skewY = a.skewY + b.skewY, .scaleY = a.scaleY + b.scaleY, .transY = a.transY + b.transY, \
235244
.pers0 = a.pers0 + b.pers0, .pers1 = a.pers1 + b.pers1, .pers2 = a.pers2 + b.pers2 \
236245
})
237246

247+
/**
248+
* A transformation that is the result of transponating a.
249+
*/
250+
#define FLUTTER_TRANSPONATED_TRANSFORMATION(a) ((FlutterTransformation) \
251+
{.scaleX = a.scaleX, .skewX = a.skewY, .transX = a.pers0, \
252+
.skewY = a.skewX, .scaleY = a.scaleY, .transY = a.pers1, \
253+
.pers0 = a.transX, .pers1 = a.transY, .pers2 = a.pers2, \
254+
})
255+
238256
static inline void apply_flutter_transformation(
239257
const FlutterTransformation t,
240258
double *px,

include/user_input.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ struct user_input *user_input_new(
9898
const struct user_input_interface *interface,
9999
void *userdata,
100100
const FlutterTransformation *display_to_view_transform,
101+
const FlutterTransformation *view_to_display_transform,
101102
unsigned int display_width,
102103
unsigned int display_height
103104
);
@@ -116,10 +117,11 @@ void user_input_destroy(struct user_input *input);
116117
* @param display_to_view_transform will be copied internally.
117118
*/
118119
void user_input_set_transform(
119-
struct user_input *input,
120-
const FlutterTransformation *display_to_view_transform,
121-
unsigned int display_width,
122-
unsigned int display_height
120+
struct user_input *input,
121+
const FlutterTransformation *display_to_view_transform,
122+
const FlutterTransformation *view_to_display_transform,
123+
unsigned int display_width,
124+
unsigned int display_height
123125
);
124126

125127
/**

src/flutter-pi.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1180,10 +1180,11 @@ int flutterpi_fill_view_properties(
11801180
}
11811181

11821182
if (flutterpi.user_input != NULL) {
1183-
// update the user input with the new transform
1183+
// update the user input with the new transforms
11841184
user_input_set_transform(
11851185
flutterpi.user_input,
11861186
&flutterpi.view.display_to_view_transform,
1187+
&flutterpi.view.view_to_display_transform,
11871188
flutterpi.display.width,
11881189
flutterpi.display.height
11891190
);
@@ -2585,6 +2586,7 @@ static int init_user_input(void) {
25852586
&user_input_interface,
25862587
&flutterpi,
25872588
&flutterpi.view.display_to_view_transform,
2589+
&flutterpi.view.view_to_display_transform,
25882590
flutterpi.display.width,
25892591
flutterpi.display.height
25902592
);

src/user_input.c

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct user_input {
4747
* used in the flutter pointer events
4848
*/
4949
FlutterTransformation display_to_view_transform;
50-
FlutterTransformation display_to_view_transform_nontranslating;
50+
FlutterTransformation view_to_display_transform_nontranslating;
5151
unsigned int display_width;
5252
unsigned int display_height;
5353

@@ -96,6 +96,7 @@ struct user_input *user_input_new(
9696
const struct user_input_interface *interface,
9797
void *userdata,
9898
const FlutterTransformation *display_to_view_transform,
99+
const FlutterTransformation *view_to_display_transform,
99100
unsigned int display_width,
100101
unsigned int display_height
101102
) {
@@ -143,16 +144,19 @@ struct user_input *user_input_new(
143144
#endif
144145

145146
input->interface = *interface;
147+
input->userdata = userdata;
148+
146149
input->libinput = libinput;
147150
input->kbdcfg = kbdcfg;
148151
input->next_unused_flutter_device_id = 0;
149-
150-
input->display_to_view_transform = *display_to_view_transform;
151-
input->display_to_view_transform_nontranslating = *display_to_view_transform;
152-
input->display_to_view_transform_nontranslating.transX = 0.0;
153-
input->display_to_view_transform_nontranslating.transY = 0.0;
154-
input->display_width = display_width;
155-
input->display_height = display_height;
152+
153+
user_input_set_transform(
154+
input,
155+
display_to_view_transform,
156+
view_to_display_transform,
157+
display_width,
158+
display_height
159+
);
156160

157161
input->n_cursor_devices = 0;
158162
input->cursor_flutter_device_id = -1;
@@ -184,14 +188,18 @@ void user_input_destroy(struct user_input *input) {
184188
}
185189

186190
void user_input_set_transform(
187-
struct user_input *input,
188-
const FlutterTransformation *display_to_view_transform,
189-
unsigned int display_width,
190-
unsigned int display_height
191+
struct user_input *input,
192+
const FlutterTransformation *display_to_view_transform,
193+
const FlutterTransformation *view_to_display_transform,
194+
unsigned int display_width,
195+
unsigned int display_height
191196
) {
192-
input->display_to_view_transform = *display_to_view_transform;
193-
input->display_width = display_width;
194-
input->display_height = display_height;
197+
input->display_to_view_transform = *display_to_view_transform;
198+
input->view_to_display_transform_nontranslating = *view_to_display_transform;
199+
input->view_to_display_transform_nontranslating.transX = 0.0;
200+
input->view_to_display_transform_nontranslating.transY = 0.0;
201+
input->display_width = display_width;
202+
input->display_height = display_height;
195203
}
196204

197205
int user_input_get_fd(struct user_input *input) {
@@ -235,6 +243,9 @@ static void emit_pointer_events(struct user_input *input, const FlutterPointerEv
235243

236244
// advance events so it points to the first remaining unemitted event
237245
events += to_copy;
246+
247+
// advance the number of stored pointer events
248+
input->n_collected_flutter_pointer_events += to_copy;
238249
}
239250
}
240251

@@ -329,6 +340,8 @@ static int on_device_added(struct user_input *input, struct libinput_event *even
329340
libinput_device_set_user_data(device, NULL);
330341
free(data);
331342
}
343+
344+
return 0;
332345
}
333346

334347
static int on_device_removed(struct user_input *input, struct libinput_event *event, uint64_t timestamp) {
@@ -367,6 +380,8 @@ static int on_device_removed(struct user_input *input, struct libinput_event *ev
367380
}
368381

369382
libinput_device_set_user_data(device, NULL);
383+
384+
return 0;
370385
}
371386

372387
static int on_key_event(struct user_input *input, struct libinput_event *event) {
@@ -426,10 +441,12 @@ static int on_key_event(struct user_input *input, struct libinput_event *event)
426441
if (codepoint < 0x80) {
427442
// we emit UTF8 unconditionally here,
428443
// maybe we should check if codepoint is a control character?
429-
input->interface.on_utf8_character(
430-
input->userdata,
431-
(uint8_t[1]) {codepoint}
432-
);
444+
if (isprint(codepoint)) {
445+
input->interface.on_utf8_character(
446+
input->userdata,
447+
(uint8_t[1]) {codepoint}
448+
);
449+
}
433450
} else if (codepoint < 0x800) {
434451
input->interface.on_utf8_character(
435452
input->userdata,
@@ -494,7 +511,7 @@ static int on_mouse_motion_event(struct user_input *input, struct libinput_event
494511
// we want the mouse to move in different directions as well.
495512
// the dx and dy is still in display (not view) coordinates though,
496513
// we only changed the movement direction.
497-
apply_flutter_transformation(input->display_to_view_transform_nontranslating, &dx, &dy);
514+
apply_flutter_transformation(input->view_to_display_transform_nontranslating, &dx, &dy);
498515

499516
new_cursor_x = input->cursor_x + dx;
500517
new_cursor_y = input->cursor_y + dy;
@@ -590,6 +607,8 @@ static int on_mouse_motion_absolute_event(struct user_input *input, struct libin
590607
),
591608
1
592609
);
610+
611+
return 0;
593612
}
594613

595614
static int on_mouse_button_event(struct user_input *input, struct libinput_event *event) {

0 commit comments

Comments
 (0)