sdl_fb.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include <rtthread.h>
  2. #include <sdl.h>
  3. #include <rtdevice.h>
  4. #include <rtgui/driver.h>
  5. #define SDL_SCREEN_WIDTH 800
  6. #define SDL_SCREEN_HEIGHT 480
  7. struct sdlfb_device
  8. {
  9. struct rt_device parent;
  10. SDL_Surface *screen;
  11. rt_uint16_t width;
  12. rt_uint16_t height;
  13. };
  14. struct sdlfb_device _device;
  15. /* common device interface */
  16. static rt_err_t sdlfb_init(rt_device_t dev)
  17. {
  18. return RT_EOK;
  19. }
  20. static rt_err_t sdlfb_open(rt_device_t dev, rt_uint16_t oflag)
  21. {
  22. return RT_EOK;
  23. }
  24. static rt_err_t sdlfb_close(rt_device_t dev)
  25. {
  26. SDL_Quit();
  27. return RT_EOK;
  28. }
  29. static rt_mutex_t sdllock;
  30. static rt_err_t sdlfb_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  31. {
  32. struct sdlfb_device *device;
  33. rt_mutex_take(sdllock, RT_WAITING_FOREVER);
  34. device = (struct sdlfb_device *)dev;
  35. RT_ASSERT(device != RT_NULL);
  36. RT_ASSERT(device->screen != RT_NULL);
  37. switch (cmd)
  38. {
  39. case RTGRAPHIC_CTRL_GET_INFO:
  40. {
  41. struct rt_device_graphic_info *info;
  42. info = (struct rt_device_graphic_info *) args;
  43. info->bits_per_pixel = 16;
  44. info->pixel_format = RTGRAPHIC_PIXEL_FORMAT_RGB565P;
  45. info->framebuffer = device->screen->pixels;
  46. info->width = device->screen->w;
  47. info->height = device->screen->h;
  48. }
  49. break;
  50. case RTGRAPHIC_CTRL_RECT_UPDATE:
  51. {
  52. struct rt_device_rect_info *rect;
  53. rect = (struct rt_device_rect_info *)args;
  54. /* SDL_UpdateRect(_device.screen, rect->x, rect->y, rect->x + rect->w, rect->y + rect->h); */
  55. SDL_UpdateRect(_device.screen, 0, 0, device->width, device->height);
  56. }
  57. break;
  58. case RTGRAPHIC_CTRL_SET_MODE:
  59. {
  60. #if 0
  61. struct rt_device_rect_info *rect;
  62. rect = (struct rt_device_rect_info *)args;
  63. if ((_device.width == rect->width) && (_device.height == rect->height)) return -RT_ERROR;
  64. _device.width = rect->width;
  65. _device.height = rect->height;
  66. if (_device.screen != RT_NULL)
  67. {
  68. SDL_FreeSurface(_device.screen);
  69. /* re-create screen surface */
  70. _device.screen = SDL_SetVideoMode(_device.width, _device.height, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
  71. if (_device.screen == NULL)
  72. {
  73. fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
  74. exit(1);
  75. }
  76. SDL_WM_SetCaption("RT-Thread/GUI Simulator", NULL);
  77. }
  78. #endif
  79. }
  80. break;
  81. }
  82. rt_mutex_release(sdllock);
  83. return RT_EOK;
  84. }
  85. static void sdlfb_hw_init(void)
  86. {
  87. /* set video driver for VC++ debug */
  88. //_putenv("SDL_VIDEODRIVER=windib");
  89. //if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO) < 0)
  90. if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
  91. {
  92. fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
  93. exit(1);
  94. }
  95. _device.parent.init = sdlfb_init;
  96. _device.parent.open = sdlfb_open;
  97. _device.parent.close = sdlfb_close;
  98. _device.parent.read = RT_NULL;
  99. _device.parent.write = RT_NULL;
  100. _device.parent.control = sdlfb_control;
  101. _device.width = SDL_SCREEN_WIDTH;
  102. _device.height = SDL_SCREEN_HEIGHT;
  103. _device.screen = SDL_SetVideoMode(_device.width, _device.height, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
  104. if (_device.screen == NULL)
  105. {
  106. fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
  107. exit(1);
  108. }
  109. SDL_WM_SetCaption("RT-Thread/GUI Simulator", NULL);
  110. rt_device_register(RT_DEVICE(&_device), "sdl", RT_DEVICE_FLAG_RDWR);
  111. sdllock = rt_mutex_create("fb", RT_IPC_FLAG_FIFO);
  112. }
  113. #include <windows.h>
  114. #include <mmsystem.h>
  115. #include <stdio.h>
  116. #include <sdl.h>
  117. #include <rtgui/event.h>
  118. #include <rtgui/kbddef.h>
  119. #include <rtgui/rtgui_server.h>
  120. #include <rtgui/rtgui_system.h>
  121. static DWORD WINAPI sdl_loop(LPVOID lpParam)
  122. {
  123. int quit = 0;
  124. SDL_Event event;
  125. int button_state = 0;
  126. rt_device_t device;
  127. sdlfb_hw_init();
  128. device = rt_device_find("sdl");
  129. rtgui_graphic_set_device(device);
  130. /* handle SDL event */
  131. while (!quit)
  132. {
  133. SDL_WaitEvent(&event);
  134. switch (event.type)
  135. {
  136. case SDL_MOUSEMOTION:
  137. #if 0
  138. {
  139. struct rtgui_event_mouse emouse;
  140. emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
  141. emouse.parent.sender = RT_NULL;
  142. emouse.wid = RT_NULL;
  143. emouse.x = ((SDL_MouseMotionEvent *)&event)->x;
  144. emouse.y = ((SDL_MouseMotionEvent *)&event)->y;
  145. /* init mouse button */
  146. emouse.button = button_state;
  147. /* send event to server */
  148. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  149. }
  150. #endif
  151. break;
  152. case SDL_MOUSEBUTTONDOWN:
  153. case SDL_MOUSEBUTTONUP:
  154. {
  155. struct rtgui_event_mouse emouse;
  156. SDL_MouseButtonEvent *mb;
  157. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  158. emouse.parent.sender = RT_NULL;
  159. emouse.wid = RT_NULL;
  160. mb = (SDL_MouseButtonEvent *)&event;
  161. emouse.x = mb->x;
  162. emouse.y = mb->y;
  163. /* init mouse button */
  164. emouse.button = 0;
  165. /* set emouse button */
  166. if (mb->button & (1 << (SDL_BUTTON_LEFT - 1)))
  167. {
  168. emouse.button |= RTGUI_MOUSE_BUTTON_LEFT;
  169. }
  170. else if (mb->button & (1 << (SDL_BUTTON_RIGHT - 1)))
  171. {
  172. emouse.button |= RTGUI_MOUSE_BUTTON_RIGHT;
  173. }
  174. else if (mb->button & (1 << (SDL_BUTTON_MIDDLE - 1)))
  175. {
  176. emouse.button |= RTGUI_MOUSE_BUTTON_MIDDLE;
  177. }
  178. if (mb->type == SDL_MOUSEBUTTONDOWN)
  179. {
  180. emouse.button |= RTGUI_MOUSE_BUTTON_DOWN;
  181. button_state = emouse.button;
  182. }
  183. else
  184. {
  185. emouse.button |= RTGUI_MOUSE_BUTTON_UP;
  186. button_state = 0;
  187. }
  188. /* send event to server */
  189. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  190. }
  191. break;
  192. case SDL_KEYUP:
  193. {
  194. struct rtgui_event_kbd ekbd;
  195. ekbd.parent.type = RTGUI_EVENT_KBD;
  196. ekbd.parent.sender = RT_NULL;
  197. ekbd.type = RTGUI_KEYUP;
  198. ekbd.wid = RT_NULL;
  199. ekbd.mod = event.key.keysym.mod;
  200. ekbd.key = event.key.keysym.sym;
  201. /* FIXME: unicode */
  202. ekbd.unicode = 0;
  203. /* send event to server */
  204. rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd));
  205. }
  206. break;
  207. case SDL_KEYDOWN:
  208. {
  209. struct rtgui_event_kbd ekbd;
  210. ekbd.parent.type = RTGUI_EVENT_KBD;
  211. ekbd.parent.sender = RT_NULL;
  212. ekbd.type = RTGUI_KEYDOWN;
  213. ekbd.wid = RT_NULL;
  214. ekbd.mod = event.key.keysym.mod;
  215. ekbd.key = event.key.keysym.sym;
  216. /* FIXME: unicode */
  217. ekbd.unicode = 0;
  218. /* send event to server */
  219. rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd));
  220. }
  221. break;
  222. case SDL_QUIT:
  223. SDL_Quit();
  224. quit = 1;
  225. break;
  226. default:
  227. break;
  228. }
  229. if (quit)
  230. break;
  231. }
  232. //exit(0);
  233. return 0;
  234. }
  235. /* start sdl thread */
  236. void rt_hw_sdl_start(void)
  237. {
  238. HANDLE thread;
  239. DWORD thread_id;
  240. /* create thread that loop sdl event */
  241. thread = CreateThread(NULL,
  242. 0,
  243. (LPTHREAD_START_ROUTINE)sdl_loop,
  244. 0,
  245. CREATE_SUSPENDED,
  246. &thread_id);
  247. if (thread == NULL)
  248. {
  249. //Display Error Message
  250. return;
  251. }
  252. ResumeThread(thread);
  253. }