sdl_fb.c 8.3 KB

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