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