@@ -308,6 +308,7 @@ typedef struct _webui_window_t {
308308 size_t num ; // Window number
309309 const char * html_elements [WEBUI_MAX_IDS ];
310310 bool has_all_events ;
311+ void * cb_context [WEBUI_MAX_IDS ];
311312 void (* cb [WEBUI_MAX_IDS ])(webui_event_t * e );
312313 void (* cb_interface [WEBUI_MAX_IDS ])(size_t , size_t , char * , size_t , size_t );
313314 bool ws_block ;
@@ -1730,6 +1731,59 @@ bool webui_show_browser(size_t window, const char* content, size_t browser) {
17301731 return _webui_show (win , NULL , content , browser );
17311732}
17321733
1734+ void * webui_get_context (webui_event_t * e ) {
1735+
1736+ #ifdef WEBUI_LOG
1737+ printf ("[User] webui_get_context()\n" );
1738+ #endif
1739+
1740+ // Dereference
1741+ if (_webui_mutex_is_exit_now (WEBUI_MUTEX_NONE ) || _webui .wins [e -> window ] == NULL )
1742+ return 0 ;
1743+ _webui_window_t * win = _webui .wins [e -> window ];
1744+
1745+ // Search
1746+ size_t cb_index = 0 ;
1747+ if (_webui_get_cb_index (win , e -> element , & cb_index )) {
1748+ // Get context
1749+ #ifdef WEBUI_LOG
1750+ printf ("[User] webui_get_context() -> Found context at %p\n" , win -> cb_context [cb_index ]);
1751+ #endif
1752+ return win -> cb_context [cb_index ];
1753+ }
1754+
1755+ return NULL ;
1756+ }
1757+
1758+ void webui_set_context (size_t window , const char * element , void * context ) {
1759+
1760+ #ifdef WEBUI_LOG
1761+ printf ("[User] webui_set_context([%zu])\n" , window );
1762+ printf ("[User] webui_set_context() -> Element: [%s]\n" , element );
1763+ printf ("[User] webui_set_context() -> Context: [%p]\n" , context );
1764+ #endif
1765+
1766+ // Initialization
1767+ _webui_init ();
1768+
1769+ // Dereference
1770+ if (_webui_mutex_is_exit_now (WEBUI_MUTEX_NONE ) || _webui .wins [window ] == NULL )
1771+ return ;
1772+ _webui_window_t * win = _webui .wins [window ];
1773+
1774+ // Get bind Index
1775+ // We should use `webui_bind()` with NULL to make `webui_set_context()`
1776+ // works fine if user call it before or after `webui_bind()`.
1777+ size_t cb_index = webui_bind (window , element , NULL );
1778+
1779+ // Set context
1780+ win -> cb_context [cb_index ] = context ;
1781+
1782+ #ifdef WEBUI_LOG
1783+ printf ("[User] webui_set_context() -> Context saved at %zu\n" , cb_index );
1784+ #endif
1785+ }
1786+
17331787size_t webui_bind (size_t window , const char * element , void (* func )(webui_event_t * e )) {
17341788
17351789 #ifdef WEBUI_LOG
@@ -1746,41 +1800,48 @@ size_t webui_bind(size_t window, const char* element, void(*func)(webui_event_t*
17461800
17471801 // Search
17481802 size_t cb_index = 0 ;
1749- bool exist = _webui_get_cb_index (win , element , & cb_index );
1803+ bool exist = _webui_get_cb_index (win , element , & cb_index );
17501804
1751- // All events
1805+ // Free old ID
1806+ size_t index = (exist ? cb_index : _webui .cb_count ++ );
1807+ _webui_free_mem ((void * )win -> html_elements [index ]);
1808+
1809+ // All events binding
17521810 if (_webui_is_empty (element )) {
1753- win -> has_all_events = true;
1754- size_t index = (exist ? cb_index : _webui .cb_count ++ );
1811+ // Empty Element ID Binding (New / Update)
17551812 win -> html_elements [index ] = "" ;
1756- win -> cb [index ] = func ;
1757- #ifdef WEBUI_LOG
1758- printf ("[User] webui_bind() -> Save bind (all events) at %zu\n" , index );
1759- #endif
1813+ if (func != NULL ) {
1814+ win -> has_all_events = true;
1815+ win -> cb [index ] = func ;
1816+ #ifdef WEBUI_LOG
1817+ printf ("[User] webui_bind() -> Save bind (all events) at %zu\n" , index );
1818+ #endif
1819+ }
17601820 return index ;
1761- }
1762-
1763- // New bind
1764- const char * element_cpy = (const char * )_webui_str_dup (element );
1765- size_t index = (exist ? cb_index : _webui .cb_count ++ );
1766- win -> html_elements [index ] = element_cpy ;
1767- win -> cb [index ] = func ;
1768- #ifdef WEBUI_LOG
1769- printf ("[User] webui_bind() -> Save bind at %zu\n" , index );
1770- #endif
1821+ } else {
1822+ // Non-empty Element ID Binding (New / Update)
1823+ const char * element_cpy = (const char * )_webui_str_dup (element );
1824+ win -> html_elements [index ] = element_cpy ;
1825+ if (func != NULL ) {
1826+ win -> cb [index ] = func ;
1827+ #ifdef WEBUI_LOG
1828+ printf ("[User] webui_bind() -> Save bind at %zu\n" , index );
1829+ #endif
17711830
1772- // Send to all connected clients the new binding ID
1773- // Packet Protocol Format:
1831+ // Send to all connected clients the new binding ID
1832+ // Packet Protocol Format:
17741833
1775- // [...]
1776- // [CMD]
1777- // [NewElement]
1778- // Send the packet
1834+ // [...]
1835+ // [CMD]
1836+ // [NewElement]
1837+ // Send the packet
17791838
1780- _webui_send_all (
1781- win , 0 , WEBUI_CMD_ADD_ID ,
1782- element , _webui_strlen (element_cpy )
1783- );
1839+ _webui_send_all (
1840+ win , 0 , WEBUI_CMD_ADD_ID ,
1841+ element , _webui_strlen (element_cpy )
1842+ );
1843+ }
1844+ }
17841845
17851846 return index ;
17861847}
0 commit comments