sdl_fb.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. #include <signal.h>
  123. #endif
  124. #include <stdio.h>
  125. #include <rtgui/event.h>
  126. #include <rtgui/kbddef.h>
  127. #include <rtgui/rtgui_server.h>
  128. #include <rtgui/rtgui_system.h>
  129. #ifdef _WIN32
  130. static HANDLE sdl_ok_event = NULL;
  131. static DWORD WINAPI sdl_loop(LPVOID lpParam)
  132. #else
  133. static void *sdl_loop(void *lpParam)
  134. #endif
  135. {
  136. int quit = 0;
  137. SDL_Event event;
  138. int button_state = 0;
  139. rt_device_t device;
  140. #ifndef _WIN32
  141. sigset_t sigmask, oldmask;
  142. /* set the getchar without buffer */
  143. sigfillset(&sigmask);
  144. pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
  145. #endif
  146. sdlfb_hw_init();
  147. device = rt_device_find("sdl");
  148. rtgui_graphic_set_device(device);
  149. #ifdef _WIN32
  150. SetEvent(sdl_ok_event);
  151. #endif
  152. /* handle SDL event */
  153. while (!quit)
  154. {
  155. SDL_WaitEvent(&event);
  156. switch (event.type)
  157. {
  158. case SDL_MOUSEMOTION:
  159. {
  160. struct rtgui_event_mouse emouse;
  161. emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
  162. emouse.parent.sender = RT_NULL;
  163. emouse.wid = RT_NULL;
  164. emouse.x = ((SDL_MouseMotionEvent *)&event)->x;
  165. emouse.y = ((SDL_MouseMotionEvent *)&event)->y;
  166. /* init mouse button */
  167. emouse.button = button_state;
  168. /* send event to server */
  169. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  170. }
  171. break;
  172. case SDL_MOUSEBUTTONDOWN:
  173. case SDL_MOUSEBUTTONUP:
  174. {
  175. struct rtgui_event_mouse emouse;
  176. SDL_MouseButtonEvent *mb;
  177. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  178. emouse.parent.sender = RT_NULL;
  179. emouse.wid = RT_NULL;
  180. mb = (SDL_MouseButtonEvent *)&event;
  181. emouse.x = mb->x;
  182. emouse.y = mb->y;
  183. /* init mouse button */
  184. emouse.button = 0;
  185. /* set emouse button */
  186. if (mb->button & (1 << (SDL_BUTTON_LEFT - 1)))
  187. {
  188. emouse.button |= RTGUI_MOUSE_BUTTON_LEFT;
  189. }
  190. else if (mb->button & (1 << (SDL_BUTTON_RIGHT - 1)))
  191. {
  192. emouse.button |= RTGUI_MOUSE_BUTTON_RIGHT;
  193. }
  194. else if (mb->button & (1 << (SDL_BUTTON_MIDDLE - 1)))
  195. {
  196. emouse.button |= RTGUI_MOUSE_BUTTON_MIDDLE;
  197. }
  198. if (mb->type == SDL_MOUSEBUTTONDOWN)
  199. {
  200. emouse.button |= RTGUI_MOUSE_BUTTON_DOWN;
  201. button_state = emouse.button;
  202. }
  203. else
  204. {
  205. emouse.button |= RTGUI_MOUSE_BUTTON_UP;
  206. button_state = 0;
  207. }
  208. /* send event to server */
  209. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  210. }
  211. break;
  212. case SDL_KEYUP:
  213. {
  214. struct rtgui_event_kbd ekbd;
  215. ekbd.parent.type = RTGUI_EVENT_KBD;
  216. ekbd.parent.sender = RT_NULL;
  217. ekbd.type = RTGUI_KEYUP;
  218. ekbd.wid = RT_NULL;
  219. ekbd.mod = event.key.keysym.mod;
  220. ekbd.key = event.key.keysym.sym;
  221. /* FIXME: unicode */
  222. ekbd.unicode = 0;
  223. /* send event to server */
  224. rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd));
  225. }
  226. break;
  227. case SDL_KEYDOWN:
  228. {
  229. struct rtgui_event_kbd ekbd;
  230. ekbd.parent.type = RTGUI_EVENT_KBD;
  231. ekbd.parent.sender = RT_NULL;
  232. ekbd.type = RTGUI_KEYDOWN;
  233. ekbd.wid = RT_NULL;
  234. ekbd.mod = event.key.keysym.mod;
  235. ekbd.key = event.key.keysym.sym;
  236. /* FIXME: unicode */
  237. ekbd.unicode = 0;
  238. /* send event to server */
  239. rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd));
  240. }
  241. break;
  242. case SDL_QUIT:
  243. SDL_Quit();
  244. quit = 1;
  245. break;
  246. default:
  247. break;
  248. }
  249. if (quit)
  250. break;
  251. }
  252. //exit(0);
  253. return 0;
  254. }
  255. /* start sdl thread */
  256. void rt_hw_sdl_start(void)
  257. {
  258. #ifdef _WIN32
  259. HANDLE thread;
  260. DWORD thread_id;
  261. sdl_ok_event = CreateEvent(NULL,
  262. FALSE,
  263. FALSE,
  264. NULL);
  265. if (sdl_ok_event == NULL)
  266. {
  267. printf("error, create SDL event failed\n");
  268. exit(-1);
  269. }
  270. /* create thread that loop sdl event */
  271. thread = CreateThread(NULL,
  272. 0,
  273. (LPTHREAD_START_ROUTINE)sdl_loop,
  274. 0,
  275. CREATE_SUSPENDED,
  276. &thread_id);
  277. if (thread == NULL)
  278. {
  279. //Display Error Message
  280. return;
  281. }
  282. ResumeThread(thread);
  283. /* wait until SDL LCD device is registered and seted */
  284. WaitForSingleObject(sdl_ok_event, INFINITE);
  285. #else
  286. /* Linux */
  287. pthread_t pid;
  288. int res;
  289. res = pthread_create(&pid, NULL, &sdl_loop, NULL);
  290. if (res)
  291. {
  292. printf("pthread create sdl thread faild, <%d>\n", res);
  293. exit(EXIT_FAILURE);
  294. }
  295. #endif
  296. }