sdl_fb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. #include <rtthread.h>
  2. #include <stdio.h>
  3. #ifdef _WIN32
  4. #include <sdl.h>
  5. #else
  6. #include <SDL/SDL.h>
  7. #endif
  8. #include <rtdevice.h>
  9. #include <rtgui/driver.h>
  10. #define SDL_SCREEN_WIDTH 480
  11. #define SDL_SCREEN_HEIGHT 320
  12. extern void rt_hw_exit(void);
  13. #define SDL_SCREEN_FORMAT SDL_PIXELFORMAT_RGB565
  14. struct sdlfb_device
  15. {
  16. struct rt_device parent;
  17. SDL_Renderer *renderer; /* window renderer */
  18. SDL_Surface *surface; /* screen surface */
  19. rt_uint16_t width;
  20. rt_uint16_t height;
  21. };
  22. struct sdlfb_device _device;
  23. /* common device interface */
  24. static rt_err_t sdlfb_init(rt_device_t dev)
  25. {
  26. return RT_EOK;
  27. }
  28. static rt_err_t sdlfb_open(rt_device_t dev, rt_uint16_t oflag)
  29. {
  30. return RT_EOK;
  31. }
  32. static rt_err_t sdlfb_close(rt_device_t dev)
  33. {
  34. SDL_Quit();
  35. return RT_EOK;
  36. }
  37. int sdlfb_info(int *format, int *bpp)
  38. {
  39. int bit_pp; /* texture bits per pixel */
  40. Uint32 Rmask, Gmask, Bmask, Amask; /* masks for pixel format passed into OpenGL */
  41. /* Grab info about format that will be passed into OpenGL */
  42. SDL_PixelFormatEnumToMasks(SDL_SCREEN_FORMAT, &bit_pp, &Rmask, &Gmask,
  43. &Bmask, &Amask);
  44. *bpp = bit_pp;
  45. switch (SDL_SCREEN_FORMAT)
  46. {
  47. case SDL_PIXELFORMAT_RGB565:
  48. *format = RTGRAPHIC_PIXEL_FORMAT_RGB565;
  49. break;
  50. case SDL_PIXELFORMAT_RGB888:
  51. *format = RTGRAPHIC_PIXEL_FORMAT_RGB888;
  52. break;
  53. case SDL_PIXELFORMAT_ARGB8888:
  54. *format = RTGRAPHIC_PIXEL_FORMAT_ARGB888;
  55. break;
  56. case SDL_PIXELFORMAT_ABGR8888:
  57. *format = RTGRAPHIC_PIXEL_FORMAT_ABGR888;
  58. }
  59. return 0;
  60. }
  61. static rt_mutex_t sdllock;
  62. static rt_err_t sdlfb_control(rt_device_t dev, int cmd, void *args)
  63. {
  64. struct sdlfb_device *device;
  65. rt_mutex_take(sdllock, RT_WAITING_FOREVER);
  66. device = (struct sdlfb_device *)dev;
  67. RT_ASSERT(device != RT_NULL);
  68. switch (cmd)
  69. {
  70. case RTGRAPHIC_CTRL_GET_INFO:
  71. {
  72. int format, bpp;
  73. struct rt_device_graphic_info *info;
  74. sdlfb_info(&format, &bpp);
  75. info = (struct rt_device_graphic_info *) args;
  76. info->bits_per_pixel = bpp;
  77. info->pixel_format = format;
  78. info->framebuffer = (rt_uint8_t*)device->surface->pixels;
  79. info->width = device->width;
  80. info->height = device->height;
  81. }
  82. break;
  83. case RTGRAPHIC_CTRL_RECT_UPDATE:
  84. {
  85. SDL_Texture * texture;
  86. struct rt_device_rect_info *rect;
  87. SDL_Rect _rect = { 0, 0, SDL_SCREEN_WIDTH, SDL_SCREEN_HEIGHT };
  88. rect = (struct rt_device_rect_info *)args;
  89. SDL_RenderClear(_device.renderer);
  90. texture = SDL_CreateTextureFromSurface(_device.renderer, _device.surface);
  91. SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
  92. SDL_RenderCopy(_device.renderer, texture, NULL, &_rect);
  93. SDL_RenderPresent(_device.renderer);
  94. SDL_DestroyTexture(texture);
  95. }
  96. break;
  97. case RTGRAPHIC_CTRL_SET_MODE:
  98. {
  99. break;
  100. }
  101. break;
  102. }
  103. rt_mutex_release(sdllock);
  104. return RT_EOK;
  105. }
  106. Uint16 pixels[16 * 16] = { // ...or with raw pixel data:
  107. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
  108. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
  109. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
  110. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
  111. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
  112. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
  113. 0x0fff, 0x0aab, 0x0789, 0x0bcc, 0x0eee, 0x09aa, 0x099a, 0x0ddd,
  114. 0x0fff, 0x0eee, 0x0899, 0x0fff, 0x0fff, 0x1fff, 0x0dde, 0x0dee,
  115. 0x0fff, 0xabbc, 0xf779, 0x8cdd, 0x3fff, 0x9bbc, 0xaaab, 0x6fff,
  116. 0x0fff, 0x3fff, 0xbaab, 0x0fff, 0x0fff, 0x6689, 0x6fff, 0x0dee,
  117. 0xe678, 0xf134, 0x8abb, 0xf235, 0xf678, 0xf013, 0xf568, 0xf001,
  118. 0xd889, 0x7abc, 0xf001, 0x0fff, 0x0fff, 0x0bcc, 0x9124, 0x5fff,
  119. 0xf124, 0xf356, 0x3eee, 0x0fff, 0x7bbc, 0xf124, 0x0789, 0x2fff,
  120. 0xf002, 0xd789, 0xf024, 0x0fff, 0x0fff, 0x0002, 0x0134, 0xd79a,
  121. 0x1fff, 0xf023, 0xf000, 0xf124, 0xc99a, 0xf024, 0x0567, 0x0fff,
  122. 0xf002, 0xe678, 0xf013, 0x0fff, 0x0ddd, 0x0fff, 0x0fff, 0xb689,
  123. 0x8abb, 0x0fff, 0x0fff, 0xf001, 0xf235, 0xf013, 0x0fff, 0xd789,
  124. 0xf002, 0x9899, 0xf001, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0xe789,
  125. 0xf023, 0xf000, 0xf001, 0xe456, 0x8bcc, 0xf013, 0xf002, 0xf012,
  126. 0x1767, 0x5aaa, 0xf013, 0xf001, 0xf000, 0x0fff, 0x7fff, 0xf124,
  127. 0x0fff, 0x089a, 0x0578, 0x0fff, 0x089a, 0x0013, 0x0245, 0x0eff,
  128. 0x0223, 0x0dde, 0x0135, 0x0789, 0x0ddd, 0xbbbc, 0xf346, 0x0467,
  129. 0x0fff, 0x4eee, 0x3ddd, 0x0edd, 0x0dee, 0x0fff, 0x0fff, 0x0dee,
  130. 0x0def, 0x08ab, 0x0fff, 0x7fff, 0xfabc, 0xf356, 0x0457, 0x0467,
  131. 0x0fff, 0x0bcd, 0x4bde, 0x9bcc, 0x8dee, 0x8eff, 0x8fff, 0x9fff,
  132. 0xadee, 0xeccd, 0xf689, 0xc357, 0x2356, 0x0356, 0x0467, 0x0467,
  133. 0x0fff, 0x0ccd, 0x0bdd, 0x0cdd, 0x0aaa, 0x2234, 0x4135, 0x4346,
  134. 0x5356, 0x2246, 0x0346, 0x0356, 0x0467, 0x0356, 0x0467, 0x0467,
  135. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
  136. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
  137. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff,
  138. 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff, 0x0fff
  139. };
  140. static void sdlfb_hw_init(void)
  141. {
  142. SDL_Window *win;
  143. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
  144. {
  145. fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
  146. exit(1);
  147. }
  148. _device.parent.init = sdlfb_init;
  149. _device.parent.open = sdlfb_open;
  150. _device.parent.close = sdlfb_close;
  151. _device.parent.read = RT_NULL;
  152. _device.parent.write = RT_NULL;
  153. _device.parent.control = sdlfb_control;
  154. _device.width = SDL_SCREEN_WIDTH;
  155. _device.height = SDL_SCREEN_HEIGHT;
  156. {
  157. int bpp; /* texture bits per pixel */
  158. Uint32 Rmask, Gmask, Bmask, Amask; /* masks for pixel format passed into OpenGL */
  159. /* Grab info about format that will be passed into OpenGL */
  160. SDL_PixelFormatEnumToMasks(SDL_SCREEN_FORMAT, &bpp, &Rmask, &Gmask,
  161. &Bmask, &Amask);
  162. _device.surface = SDL_CreateRGBSurface(0, SDL_SCREEN_WIDTH, SDL_SCREEN_HEIGHT,
  163. bpp, Rmask, Gmask, Bmask, Amask);
  164. }
  165. win = SDL_CreateWindow("RT-Thread/Persimmon",
  166. SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  167. SDL_SCREEN_WIDTH, SDL_SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
  168. if (win == NULL)
  169. {
  170. fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
  171. exit(1);
  172. }
  173. /* set the icon of Window */
  174. {
  175. SDL_Surface *surface;
  176. surface = SDL_CreateRGBSurfaceFrom(pixels, 16, 16, 16, 16 * 2, 0x0f00, 0x00f0, 0x000f, 0xf000);
  177. SDL_SetWindowIcon(win, surface);
  178. }
  179. _device.renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
  180. rt_device_register(RT_DEVICE(&_device), "sdl", RT_DEVICE_FLAG_RDWR);
  181. sdllock = rt_mutex_create("fb", RT_IPC_FLAG_FIFO);
  182. }
  183. #ifdef _WIN32
  184. #include <windows.h>
  185. #include <mmsystem.h>
  186. #else
  187. #include <pthread.h>
  188. #include <signal.h>
  189. #endif
  190. #include <stdio.h>
  191. #include <rtgui/event.h>
  192. #include <rtgui/kbddef.h>
  193. #include <rtgui/rtgui_server.h>
  194. #include <rtgui/rtgui_system.h>
  195. #ifdef _WIN32
  196. static HANDLE sdl_ok_event = NULL;
  197. static DWORD WINAPI sdl_loop(LPVOID lpParam)
  198. #else
  199. static pthread_mutex_t sdl_ok_mutex;
  200. static pthread_cond_t sdl_ok_event;
  201. static void *sdl_loop(void *lpParam)
  202. #endif
  203. {
  204. int quit = 0;
  205. SDL_Event event;
  206. int state = 0;
  207. rt_device_t device;
  208. int timeout = 1000000;
  209. int last_x = -1, last_y = -1;
  210. int motion_x, motion_y;
  211. int motion_button = 0;
  212. int motion_tick = 50;
  213. int mouse_id = 1;
  214. #ifndef _WIN32
  215. sigset_t sigmask, oldmask;
  216. /* set the getchar without buffer */
  217. sigfillset(&sigmask);
  218. pthread_sigmask(SIG_BLOCK, &sigmask, &oldmask);
  219. pthread_mutex_lock(&sdl_ok_mutex);
  220. #endif
  221. sdlfb_hw_init();
  222. device = rt_device_find("sdl");
  223. RT_ASSERT(device);
  224. rtgui_graphic_set_device(device);
  225. #ifdef _WIN32
  226. SetEvent(sdl_ok_event);
  227. #else
  228. pthread_cond_signal(&sdl_ok_event);
  229. pthread_mutex_unlock(&sdl_ok_mutex);
  230. #endif
  231. /* handle SDL event */
  232. while (!quit)
  233. {
  234. if (motion_button & RTGUI_MOUSE_BUTTON_DOWN)
  235. {
  236. timeout = motion_tick - SDL_GetTicks();
  237. }
  238. else
  239. {
  240. timeout = 1000 * 1000;
  241. }
  242. int tick = SDL_GetTicks();
  243. SDL_WaitEventTimeout(&event, timeout);
  244. if (SDL_GetTicks() >= motion_tick && (motion_button & RTGUI_MOUSE_BUTTON_DOWN)) /* whether timeout */
  245. {
  246. /* sendout motion event */
  247. struct rtgui_event_mouse emouse;
  248. emouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
  249. emouse.parent.sender = RT_NULL;
  250. emouse.wid = RT_NULL;
  251. emouse.id = mouse_id;
  252. emouse.ts = rt_tick_get();
  253. if ((motion_x > 0 && motion_x < SDL_SCREEN_WIDTH) &&
  254. (motion_y > 0 && motion_y < SDL_SCREEN_HEIGHT))
  255. {
  256. emouse.x = motion_x;
  257. emouse.y = motion_y;
  258. /* init mouse button */
  259. emouse.button = motion_button;
  260. /* send event to server */
  261. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  262. }
  263. /* reset motion tick */
  264. motion_tick = 50 + SDL_GetTicks();
  265. }
  266. switch (event.type)
  267. {
  268. case SDL_MOUSEMOTION:
  269. {
  270. /* save to (x,y) in the motion */
  271. motion_x = ((SDL_MouseMotionEvent *)&event)->x;
  272. motion_y = ((SDL_MouseMotionEvent *)&event)->y;
  273. }
  274. break;
  275. case SDL_MOUSEBUTTONDOWN:
  276. case SDL_MOUSEBUTTONUP:
  277. {
  278. int x, y;
  279. struct rtgui_event_mouse emouse;
  280. SDL_MouseButtonEvent *mb;
  281. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  282. emouse.parent.sender = RT_NULL;
  283. emouse.wid = RT_NULL;
  284. if (event.type == SDL_MOUSEBUTTONDOWN && state == 0)
  285. mouse_id = rt_tick_get();
  286. emouse.id = mouse_id;
  287. emouse.ts = rt_tick_get();
  288. mb = (SDL_MouseButtonEvent *)&event;
  289. x = mb->x;
  290. y = mb->y;
  291. if ((x > 0 && x < SDL_SCREEN_WIDTH) &&
  292. (y > 0 && y < SDL_SCREEN_HEIGHT))
  293. {
  294. emouse.x = x;
  295. emouse.y = y;
  296. /* init mouse button */
  297. emouse.button = 0;
  298. /* set emouse button */
  299. if (mb->button & (1 << (SDL_BUTTON_LEFT - 1)))
  300. {
  301. emouse.button |= RTGUI_MOUSE_BUTTON_LEFT;
  302. }
  303. else if (mb->button & (1 << (SDL_BUTTON_RIGHT - 1)))
  304. {
  305. emouse.button |= RTGUI_MOUSE_BUTTON_RIGHT;
  306. }
  307. else if (mb->button & (1 << (SDL_BUTTON_MIDDLE - 1)))
  308. {
  309. emouse.button |= RTGUI_MOUSE_BUTTON_MIDDLE;
  310. }
  311. if (mb->type == SDL_MOUSEBUTTONDOWN)
  312. {
  313. emouse.button |= RTGUI_MOUSE_BUTTON_DOWN;
  314. motion_button = emouse.button;
  315. /* set motion timeout tick */
  316. motion_tick = 50 + SDL_GetTicks();
  317. if (state == 0)
  318. {
  319. /* send event to server */
  320. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  321. last_x = -1; last_y = -1;
  322. state = 1;
  323. }
  324. }
  325. else
  326. {
  327. emouse.button |= RTGUI_MOUSE_BUTTON_UP;
  328. motion_button = 0;
  329. if (state == 1)
  330. {
  331. /* send event to server */
  332. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  333. last_x = -1; last_y = -1;
  334. state = 0;
  335. }
  336. }
  337. }
  338. }
  339. break;
  340. case SDL_KEYUP:
  341. {
  342. struct rtgui_event_kbd ekbd;
  343. ekbd.parent.type = RTGUI_EVENT_KBD;
  344. ekbd.parent.sender = RT_NULL;
  345. ekbd.type = RTGUI_KEYUP;
  346. ekbd.wid = RT_NULL;
  347. ekbd.mod = event.key.keysym.mod;
  348. ekbd.key = event.key.keysym.sym;
  349. /* FIXME: unicode */
  350. ekbd.unicode = 0;
  351. /* send event to server */
  352. rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd));
  353. }
  354. break;
  355. case SDL_KEYDOWN:
  356. {
  357. struct rtgui_event_kbd ekbd;
  358. ekbd.parent.type = RTGUI_EVENT_KBD;
  359. ekbd.parent.sender = RT_NULL;
  360. ekbd.type = RTGUI_KEYDOWN;
  361. ekbd.wid = RT_NULL;
  362. ekbd.mod = event.key.keysym.mod;
  363. ekbd.key = event.key.keysym.sym;
  364. /* FIXME: unicode */
  365. ekbd.unicode = 0;
  366. /* send event to server */
  367. rtgui_server_post_event(&ekbd.parent, sizeof(struct rtgui_event_kbd));
  368. }
  369. break;
  370. case SDL_QUIT:
  371. SDL_Quit();
  372. quit = 1;
  373. break;
  374. default:
  375. break;
  376. }
  377. if (quit)
  378. {
  379. exit(1);
  380. break;
  381. }
  382. }
  383. rt_hw_exit();
  384. return 0;
  385. }
  386. /* start sdl thread */
  387. void rt_hw_sdl_start(void)
  388. {
  389. #ifdef _WIN32
  390. HANDLE thread;
  391. DWORD thread_id;
  392. sdl_ok_event = CreateEvent(NULL,
  393. FALSE,
  394. FALSE,
  395. NULL);
  396. if (sdl_ok_event == NULL)
  397. {
  398. printf("error, create SDL event failed\n");
  399. exit(-1);
  400. }
  401. /* create thread that loop sdl event */
  402. thread = CreateThread(NULL,
  403. 0,
  404. (LPTHREAD_START_ROUTINE)sdl_loop,
  405. 0,
  406. CREATE_SUSPENDED,
  407. &thread_id);
  408. if (thread == NULL)
  409. {
  410. //Display Error Message
  411. return;
  412. }
  413. ResumeThread(thread);
  414. /* wait until SDL LCD device is registered and seted */
  415. WaitForSingleObject(sdl_ok_event, INFINITE);
  416. #else
  417. /* Linux */
  418. pthread_t pid;
  419. int res;
  420. pthread_mutex_init(&sdl_ok_mutex, NULL);
  421. pthread_cond_init(&sdl_ok_event, NULL);
  422. res = pthread_create(&pid, NULL, &sdl_loop, NULL);
  423. if (res)
  424. {
  425. printf("pthread create sdl thread faild, <%d>\n", res);
  426. exit(EXIT_FAILURE);
  427. }
  428. pthread_mutex_lock(&sdl_ok_mutex);
  429. pthread_cond_wait(&sdl_ok_event, &sdl_ok_mutex);
  430. pthread_mutex_destroy(&sdl_ok_mutex);
  431. pthread_cond_destroy(&sdl_ok_event);
  432. #endif
  433. }