microui  4.1.0
microui
microui_event_decoder.c
Go to the documentation of this file.
1 /*
2  * C
3  *
4  * Copyright 2021-2024 MicroEJ Corp. All rights reserved.
5  * Use of this source code is governed by a BSD-style license that can be found with this software.
6  */
7 
18 // -----------------------------------------------------------------------------
19 // Includes
20 // -----------------------------------------------------------------------------
21 
22 // calls Microui events decoder functions
23 #include "microui_event_decoder.h"
24 
25 #ifdef MICROUIEVENTDECODER_ENABLED
26 
27 // -----------------------------------------------------------------------------
28 // Macros and Defines
29 // -----------------------------------------------------------------------------
30 
31 /*
32  * @brief Default traces.
33  */
34 #define DESCRIBE_EVENT_GENERATOR(event) (LLUI_DEBUG_TRACE(" (event generator %u)", EVENT_GENERATOR_ID(event)))
35 #define DESCRIBE_EOL() (LLUI_DEBUG_TRACE("\n"))
36 
37 /*
38  * @brief Decodes MicroUI event.
39  */
40 #define EVENT_DATA(event) ((uint16_t)(event))
41 #define EVENT_GENERATOR_ID(event) ((uint8_t)((event) >> 16))
42 
43 /*
44  * @brief Decodes MicroUI Command event.
45  */
46 #define COMMAND_GET(event) EVENT_DATA(event)
47 
48 /*
49  * @brief Decodes MicroUI Buttons event.
50  */
51 #define BUTTON_ACTION_PRESSED 0
52 #define BUTTON_ACTION_RELEASED 1
53 #define BUTTON_ACTION_LONG 2
54 #define BUTTON_ACTION_REPEATED 3
55 #define BUTTON_ACTION_CLICK 4
56 #define BUTTON_ACTION_DOUBLECLICK 5
57 #define BUTTON_ACTION(event) ((uint8_t)((event) >> 8))
58 #define BUTTON_ID(event) ((uint8_t)(event))
59 
60 /*
61  * @brief Decodes MicroUI Pointer event.
62  */
63 #define POINTER_ACTION_MOVE 6
64 #define POINTER_ACTION_DRAG 7
65 #define POINTER_X(data) (((data) >> 16) & 0xfff)
66 #define POINTER_Y(data) ((data) & 0xfff)
67 #define POINTER_TYPE(data) (((data) >> 31) & 0x1)
68 
69 /*
70  * @brief Decodes MicroUI user event.
71  */
72 #define USEREVENT_SIZE(event) EVENT_DATA(event)
73 
74 // -----------------------------------------------------------------------------
75 // Internal functions
76 // -----------------------------------------------------------------------------
77 
78 static void decode_event_command(uint32_t event, uint32_t index,
79  MICROUI_EVENT_DECODER_decode_event_data *fct_data_decoder) {
80  (void)index;
81  (void)fct_data_decoder;
82 
83  LLUI_DEBUG_TRACE("Command ");
84 
85  uint8_t command = (uint8_t)COMMAND_GET(event);
86 
87  switch (command) {
88  case 0:
89  LLUI_DEBUG_TRACE("ESC");
90  break;
91 
92  case 1:
93  LLUI_DEBUG_TRACE("BACK");
94  break;
95 
96  case 2:
97  LLUI_DEBUG_TRACE("UP");
98  break;
99 
100  case 3:
101  LLUI_DEBUG_TRACE("DOWN");
102  break;
103 
104  case 4:
105  LLUI_DEBUG_TRACE("LEFT");
106  break;
107 
108  case 5:
109  LLUI_DEBUG_TRACE("RIGHT");
110  break;
111 
112  case 6:
113  LLUI_DEBUG_TRACE("SELECT");
114  break;
115 
116  case 7:
117  LLUI_DEBUG_TRACE("CANCEL");
118  break;
119 
120  case 8:
121  LLUI_DEBUG_TRACE("HELP");
122  break;
123 
124  case 9:
125  LLUI_DEBUG_TRACE("MENU");
126  break;
127 
128  case 10:
129  LLUI_DEBUG_TRACE("EXIT");
130  break;
131 
132  case 11:
133  LLUI_DEBUG_TRACE("START");
134  break;
135 
136  case 12:
137  LLUI_DEBUG_TRACE("STOP");
138  break;
139 
140  case 13:
141  LLUI_DEBUG_TRACE("PAUSE");
142  break;
143 
144  case 14:
145  LLUI_DEBUG_TRACE("RESUME");
146  break;
147 
148  case 15:
149  LLUI_DEBUG_TRACE("COPY");
150  break;
151 
152  case 16:
153  LLUI_DEBUG_TRACE("CUT");
154  break;
155 
156  case 17:
157  LLUI_DEBUG_TRACE("PASTE");
158  break;
159 
160  case 18:
161  LLUI_DEBUG_TRACE("CLOCKWISE");
162  break;
163 
164  case 19:
165  LLUI_DEBUG_TRACE("ANTICLOCKWISE");
166  break;
167 
168  case 20:
169  LLUI_DEBUG_TRACE("PREVIOUS");
170  break;
171 
172  case 21:
173  LLUI_DEBUG_TRACE("NEXT");
174  break;
175 
176  default:
177  LLUI_DEBUG_TRACE("0x%02x", command);
178  break;
179  }
180 
181  DESCRIBE_EVENT_GENERATOR(event);
182  DESCRIBE_EOL();
183 }
184 
185 static void decode_event_button(uint32_t event, uint32_t index,
186  MICROUI_EVENT_DECODER_decode_event_data *fct_data_decoder) {
187  (void)index;
188  (void)fct_data_decoder;
189 
190  LLUI_DEBUG_TRACE("Button %u ", BUTTON_ID(event));
191  uint8_t action = BUTTON_ACTION(event);
192 
193  switch (action) {
194  case BUTTON_ACTION_PRESSED:
195  LLUI_DEBUG_TRACE("pressed");
196  break;
197 
198  case BUTTON_ACTION_RELEASED:
199  LLUI_DEBUG_TRACE("released");
200  break;
201 
202  case BUTTON_ACTION_LONG:
203  LLUI_DEBUG_TRACE("long");
204  break;
205 
206  case BUTTON_ACTION_REPEATED:
207  LLUI_DEBUG_TRACE("repeated");
208  break;
209 
210  case BUTTON_ACTION_CLICK:
211  LLUI_DEBUG_TRACE("click");
212  break;
213 
214  case BUTTON_ACTION_DOUBLECLICK:
215  LLUI_DEBUG_TRACE("double-click");
216  break;
217 
218  default:
219  LLUI_DEBUG_TRACE("unknown action: %u", action);
220  break;
221  }
222 
223  DESCRIBE_EVENT_GENERATOR(event);
224  DESCRIBE_EOL();
225 }
226 
227 #ifdef MICROUIEVENTDECODER_EVENTGEN_TOUCH
228 /*
229  * @brief Input pointer event holds a data that contains x and y.
230  */
231 static void decode_event_pointer_data(uint32_t event, uint32_t data, uint32_t index) {
232  (void)event;
233 
234  LLUI_DEBUG_TRACE("[%02u: 0x%08x] at %u,%u (", index, data, POINTER_X(data), POINTER_Y(data));
235  if (0 == POINTER_TYPE(data)) {
236  LLUI_DEBUG_TRACE("absolute)");
237  } else {
238  LLUI_DEBUG_TRACE("relative)");
239  }
240 
241  DESCRIBE_EOL();
242 }
243 
244 #endif // MICROUIEVENTDECODER_EVENTGEN_TOUCH
245 
246 static void decode_event_pointer(uint32_t event, uint32_t index,
247  MICROUI_EVENT_DECODER_decode_event_data *fct_data_decoder) {
248  (void)index;
249  (void)fct_data_decoder;
250 
251  LLUI_DEBUG_TRACE("Pointer ");
252  uint8_t action = BUTTON_ACTION(event);
253 
254  switch (action) {
255  case BUTTON_ACTION_PRESSED:
256  LLUI_DEBUG_TRACE("pressed");
257  break;
258 
259  case BUTTON_ACTION_RELEASED:
260  LLUI_DEBUG_TRACE("released");
261  break;
262 
263  case BUTTON_ACTION_LONG:
264  LLUI_DEBUG_TRACE("long");
265  break;
266 
267  case BUTTON_ACTION_REPEATED:
268  LLUI_DEBUG_TRACE("repeated");
269  break;
270 
271  case BUTTON_ACTION_CLICK:
272  LLUI_DEBUG_TRACE("click");
273  break;
274 
275  case BUTTON_ACTION_DOUBLECLICK:
276  LLUI_DEBUG_TRACE("double-click");
277  break;
278 
279  case POINTER_ACTION_MOVE:
280  LLUI_DEBUG_TRACE("moved");
281  break;
282 
283  case POINTER_ACTION_DRAG:
284  LLUI_DEBUG_TRACE("dragged");
285  break;
286 
287  default:
288  LLUI_DEBUG_TRACE("unknown action: %u", action);
289  break;
290  }
291 
292  DESCRIBE_EVENT_GENERATOR(event);
293  DESCRIBE_EOL();
294 }
295 
296 static void decode_event_state(uint32_t event, uint32_t index,
297  MICROUI_EVENT_DECODER_decode_event_data *fct_data_decoder) {
298  (void)index;
299  (void)fct_data_decoder;
300 
301  LLUI_DEBUG_TRACE("TODO %s 0x%08x\n", __FUNCTION__, event);
302 }
303 
304 /*
305  * @brief Decodes the input events: the events sent by the BSP. To decode the events,
306  * the decoder must know the event generators identifier defined during the MicroEJ
307  * platform build. These identifiers are available in microui_constants.h.
308  *
309  * @see microui_event_decoder_conf.h
310  */
311 static void decode_event_input(uint32_t event, uint32_t index,
312  MICROUI_EVENT_DECODER_decode_event_data *fct_data_decoder) {
313  LLUI_DEBUG_TRACE("Input event: ");
314 
315  uint8_t generator_id = EVENT_GENERATOR_ID(event);
316 
317  switch (generator_id) {
318 #ifdef MICROUIEVENTDECODER_EVENTGEN_COMMAND
319  case MICROUI_EVENTGEN_COMMANDS:
320  decode_event_command(event, index, fct_data_decoder);
321  break;
322 #endif // MICROUIEVENTDECODER_EVENTGEN_COMMAND
323 
324 #ifdef MICROUIEVENTDECODER_EVENTGEN_BUTTONS
325  case MICROUIEVENTDECODER_EVENTGEN_BUTTONS:
326  decode_event_button(event, index, fct_data_decoder);
327  break;
328 #endif // MICROUIEVENTDECODER_EVENTGEN_BUTTONS
329 
330 #ifdef MICROUIEVENTDECODER_EVENTGEN_TOUCH
331  case MICROUIEVENTDECODER_EVENTGEN_TOUCH:
332  *fct_data_decoder = decode_event_pointer_data;
333  decode_event_pointer(event, index, fct_data_decoder);
334  break;
335 #endif // MICROUIEVENTDECODER_EVENTGEN_TOUCH
336 
337  default:
338  LLUI_DEBUG_TRACE("unknown ");
339  DESCRIBE_EVENT_GENERATOR(event);
340  DESCRIBE_EOL();
341  break;
342  }
343 }
344 
345 static void decode_event_user_data(uint32_t event, uint32_t data, uint32_t index) {
346  (void)event;
347  LLUI_DEBUG_TRACE(" [%02u] 0x%08x\n", index, data);
348 }
349 
350 static void decode_event_user(uint32_t event, uint32_t index,
351  MICROUI_EVENT_DECODER_decode_event_data *fct_data_decoder) {
352  (void)index;
353  LLUI_DEBUG_TRACE("User input event");
354 
355  uint8_t size = (uint8_t)USEREVENT_SIZE(event);
356  if (size > (uint32_t)1) {
357  LLUI_DEBUG_TRACE("s (size = %u)", size);
358  }
359  DESCRIBE_EVENT_GENERATOR(event);
360  LLUI_DEBUG_TRACE(": ");
361 
362  *fct_data_decoder = decode_event_user_data;
363 }
364 
365 // -----------------------------------------------------------------------------
366 // API
367 // -----------------------------------------------------------------------------
368 
369 void MICROUI_EVENT_DECODER_describe_dump_start(void) {
370  LLUI_DEBUG_TRACE("============================== MicroUI FIFO Dump ===============================\n");
371 }
372 
373 void MICROUI_EVENT_DECODER_describe_dump_past(void) {
374  LLUI_DEBUG_TRACE("---------------------------------- Old Events ----------------------------------\n");
375 }
376 
377 void MICROUI_EVENT_DECODER_describe_dump_future(void) {
378  LLUI_DEBUG_TRACE("---------------------------------- New Events ----------------------------------\n");
379 }
380 
381 void MICROUI_EVENT_DECODER_describe_dump_events_objects(void) {
382  LLUI_DEBUG_TRACE("--------------------------- New Events' Java objects ---------------------------\n");
383 }
384 
385 void MICROUI_EVENT_DECODER_describe_dump_end(void) {
386  LLUI_DEBUG_TRACE("================================================================================\n");
387 }
388 
389 void MICROUI_EVENT_DECODER_drop_data(uint32_t data, uint32_t index) {
390  LLUI_DEBUG_TRACE("[%02u: 0x%08x] garbage\n", index, data);
391 }
392 
393 void MICROUI_EVENT_DECODER_decode_event(uint32_t event, uint32_t index,
394  MICROUI_EVENT_DECODER_decode_event_data *fct_data_decoder) {
395  LLUI_DEBUG_TRACE("[%02u: 0x%08x] ", index, event);
396 
397  uint8_t event_type = (uint8_t)(event >> 24);
398 
399  switch (event_type) {
400  case 0x00:
401  decode_event_command(event, index, fct_data_decoder);
402  break;
403 
404  case 0x01:
405  decode_event_button(event, index, fct_data_decoder);
406  break;
407 
408  case 0x02:
409  decode_event_pointer(event, index, fct_data_decoder);
410  break;
411 
412  case 0x03:
413  decode_event_state(event, index, fct_data_decoder);
414  break;
415 
416  case 0x04:
417  // not used
418  break;
419 
420  case 0x05:
421  LLUI_DEBUG_TRACE("Call serially (Runnable index = %u)\n", EVENT_DATA(event));
422  break;
423 
424  case 0x06:
425  LLUI_DEBUG_TRACE("MicroUI STOP\n");
426  break;
427 
428  case 0x07:
429  decode_event_input(event, index, fct_data_decoder);
430  break;
431 
432  case 0x08:
433  LLUI_DEBUG_TRACE("Display SHOW Displayable (Displayable index = %u)\n", EVENT_DATA(event));
434  break;
435 
436  case 0x09:
437  LLUI_DEBUG_TRACE("Display HIDE Displayable (Displayable index = %u)\n", EVENT_DATA(event));
438  break;
439 
440  case 0x0a:
441  // not used
442  break;
443 
444  case 0x0b:
445  LLUI_DEBUG_TRACE("Display FLUSH\n");
446  break;
447 
448  case 0x0c:
449  LLUI_DEBUG_TRACE("Display FORCE FLUSH\n");
450  break;
451 
452  case 0x0d:
453  LLUI_DEBUG_TRACE("Display REPAINT Displayable (Displayable index = %u)\n", EVENT_DATA(event));
454  break;
455 
456  case 0x0e:
457  LLUI_DEBUG_TRACE("Display REPAINT Current Displayable\n");
458  break;
459 
460  case 0x0f:
461  LLUI_DEBUG_TRACE("Display KF SWITCH Display\n");
462  break;
463 
464  default:
465  decode_event_user(event, index, fct_data_decoder);
466  break;
467  }
468 }
469 
470 #endif // MICROUIEVENTDECODER_ENABLED
471 
472 // -----------------------------------------------------------------------------
473 // EOF
474 // -----------------------------------------------------------------------------