sdl_fb.c 6.9 KB

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