sdl_fb.c 8.2 KB

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