1 /**
2 * @file lv_msg.h
3 *
4 */
5
6 #ifndef LV_MSG_H
7 #define LV_MSG_H
8
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12
13 /*********************
14 * INCLUDES
15 *********************/
16 #include "../../../core/lv_obj.h"
17 #if LV_USE_MSG
18
19 /*********************
20 * DEFINES
21 *********************/
22 #define LV_MSG_ID_ANY UINT32_MAX
23 LV_EXPORT_CONST_INT(LV_MSG_ID_ANY);
24
25 /**********************
26 * TYPEDEFS
27 **********************/
28
29 typedef struct {
30 uint32_t id; /*Identifier of the message*/
31 void * user_data; /*Set the the user_data set in `lv_msg_subscribe`*/
32 void * _priv_data; /*Used internally*/
33 const void * payload; /*Pointer to the data of the message*/
34 } lv_msg_t;
35
36 typedef void (*lv_msg_subscribe_cb_t)(void * s, lv_msg_t * msg);
37
38 typedef void (*lv_msg_request_cb_t)(void * r, uint32_t msg_id);
39
40 /**********************
41 * GLOBAL PROTOTYPES
42 **********************/
43
44 /**
45 * Called internally to initialize the message module
46 */
47 void lv_msg_init(void);
48
49 /**
50 * Subscribe to an `msg_id`
51 * @param msg_id the message ID to listen to
52 * @param cb callback to call if a message with `msg_id` was sent
53 * @param user_data arbitrary data which will be available in `cb` too
54 * @return pointer to a "subscribe object". It can be used the unsubscribe.
55 */
56 void * lv_msg_subsribe(uint32_t msg_id, lv_msg_subscribe_cb_t cb, void * user_data);
57
58 /**
59 * Subscribe an `lv_obj` to a message.
60 * `LV_EVENT_MSG_RECEIVED` will be triggered if a message with matching ID was sent
61 * @param msg_id the message ID to listen to
62 * @param obj pointer to an `lv_obj`
63 * @param user_data arbitrary data which will be available in `cb` too
64 * @return pointer to a "subscribe object". It can be used the unsubscribe.
65 */
66 void * lv_msg_subsribe_obj(uint32_t msg_id, lv_obj_t * obj, void * user_data);
67
68 /**
69 * Cancel a previous subscription
70 * @param s pointer to a "subscibe object".
71 * Return value of `lv_msg_subsribe` or `lv_msg_subsribe_obj`
72 */
73 void lv_msg_unsubscribe(void * s);
74
75 /**
76 * Unsubscribe an object from a message ID
77 * @param msg_id the message ID to unsubcribe from or `LV_MSG_ID_ANY` for any message ID
78 * @param obj the object to unsubscribe or NULL for any object
79 * @return number of unsubscriptions
80 */
81 uint32_t lv_msg_unsubscribe_obj(uint32_t msg_id, lv_obj_t * obj);
82
83 /**
84 * Send a message with a given ID and payload
85 * @param msg_id ID of the message to send
86 * @param data pointer to the data to send
87 */
88 void lv_msg_send(uint32_t msg_id, const void * payload);
89
90 /**
91 * Get the ID of a message object. Typically used in the subscriber callback.
92 * @param m pointer to a message object
93 * @return the ID of the message
94 */
95 uint32_t lv_msg_get_id(lv_msg_t * m);
96
97 /**
98 * Get the payload of a message object. Typically used in the subscriber callback.
99 * @param m pointer to a message object
100 * @return the payload of the message
101 */
102 const void * lv_msg_get_payload(lv_msg_t * m);
103
104 /**
105 * Get the user data of a message object. Typically used in the subscriber callback.
106 * @param m pointer to a message object
107 * @return the user data of the message
108 */
109 void * lv_msg_get_user_data(lv_msg_t * m);
110
111 /**
112 * Get the message object from an event object. Can be used in `LV_EVENT_MSG_RECEIVED` events.
113 * @param e pointer to an event object
114 * @return the message object or NULL if called with unrelated event code.
115 */
116 lv_msg_t * lv_event_get_msg(lv_event_t * e);
117
118 /*Fix typo*/
lv_msg_subscribe(uint32_t msg_id,lv_msg_subscribe_cb_t cb,void * user_data)119 static inline void * lv_msg_subscribe(uint32_t msg_id, lv_msg_subscribe_cb_t cb, void * user_data)
120 {
121 return lv_msg_subsribe(msg_id, cb, user_data);
122 }
123
lv_msg_subscribe_obj(uint32_t msg_id,lv_obj_t * obj,void * user_data)124 static inline void * lv_msg_subscribe_obj(uint32_t msg_id, lv_obj_t * obj, void * user_data)
125 {
126 return lv_msg_subsribe_obj(msg_id, obj, user_data);
127 }
128
129 /**********************
130 * GLOBAL VARIABLES
131 **********************/
132
133 extern lv_event_code_t LV_EVENT_MSG_RECEIVED;
134
135 /**********************
136 * MACROS
137 **********************/
138
139 #endif /*LV_USE_MSG*/
140
141 #ifdef __cplusplus
142 } /*extern "C"*/
143 #endif
144
145 #endif /*LV_MSG_H*/
146