luat_msgbus_win32.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /**
  2. * @file luat_msgbus_win32.c
  3. * @author wendal (wendal1985@gamil.com)
  4. * @brief 基于win32的ThreadMessage机制实现的msgbus
  5. * @version 0.1
  6. * @date 2022-03-27
  7. *
  8. * @copyright Copyright (c) 2022 OpenLuat & AirM2M
  9. *
  10. */
  11. #include "luat_base.h"
  12. #include "luat_msgbus.h"
  13. #include "luat_timer.h"
  14. #include "luat_mem.h"
  15. #include <stdbool.h>
  16. #include "windows.h"
  17. #include "windowsx.h"
  18. #define LUAT_LOG_TAG "msgbus"
  19. #include "luat_log.h"
  20. static DWORD luat_main_thread_id;
  21. void luat_msgbus_init(void) {
  22. luat_main_thread_id = GetCurrentThreadId();
  23. // LLOGD("main thread id %d", luat_main_thread_id);
  24. }
  25. uint32_t luat_msgbus_put(rtos_msg_t* msg, size_t timeout) {
  26. rtos_msg_t* tmp = luat_heap_malloc(sizeof(rtos_msg_t));
  27. memcpy(tmp, msg, sizeof(rtos_msg_t));
  28. PostThreadMessageA(luat_main_thread_id, WM_COMMAND, (WPARAM)tmp, 0);
  29. return 0;
  30. }
  31. uint32_t luat_msgbus_get(rtos_msg_t* rtmsg, size_t timeout) {
  32. MSG msg;
  33. rtos_msg_t* tmp;
  34. bool ret = FALSE;
  35. if ((ret = GetMessageA(&msg,NULL,0,0)) == 0) {
  36. exit(0);
  37. return 0;
  38. }
  39. int mouse_x, mouse_y;
  40. bool mouse_pressed;
  41. switch (msg.message)
  42. {
  43. case WM_COMMAND:
  44. tmp = (rtos_msg_t*)msg.wParam;
  45. if (tmp != NULL) {
  46. memcpy(rtmsg, tmp, sizeof(rtos_msg_t));
  47. luat_heap_free(tmp);
  48. return 0;
  49. }
  50. break;
  51. default:
  52. DispatchMessage(&msg);
  53. break;
  54. }
  55. return 1;
  56. }
  57. uint32_t luat_msgbus_freesize(void) {
  58. return 1;
  59. }
  60. uint8_t luat_msgbus_is_empty(void) {
  61. return 1;
  62. }