sdl2.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include <stdio.h>
  2. #include "sdl2.h"
  3. #include "minilisp.h"
  4. #include "alloc.h"
  5. #include "stream.h"
  6. #include "compiler_new.h"
  7. #include <time.h>
  8. #include <unistd.h>
  9. #define WIDTH 800
  10. #define HEIGHT 600
  11. #define BPP 2
  12. #define DEPTH 8*BPP
  13. #define SCALE 1
  14. SDL_Surface* win_surf;
  15. SDL_Surface* pixels_surf;
  16. SDL_Window* win;
  17. void sdl_cleanup() {
  18. SDL_Quit();
  19. }
  20. static int sdl_initialized = 0;
  21. void* sdl_init(int fullscreen)
  22. {
  23. if (sdl_initialized) return pixels_surf->pixels;
  24. sdl_initialized = 1;
  25. SDL_Init(SDL_INIT_VIDEO);
  26. win = SDL_CreateWindow("sledge", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH*SCALE, HEIGHT*SCALE, (fullscreen?SDL_WINDOW_FULLSCREEN:0));
  27. win_surf = SDL_GetWindowSurface(win);
  28. if (0) {
  29. pixels_surf = win_surf;
  30. } else {
  31. pixels_surf = SDL_CreateRGBSurface(0,WIDTH,HEIGHT,DEPTH,0xf800,0x7e0,0x1f,0);
  32. }
  33. printf("pixels_surf: %p\r\n\r\n",pixels_surf);
  34. printf("win_surf: %p\r\n\r\n",win_surf);
  35. memset(pixels_surf->pixels,0xff,WIDTH*HEIGHT*BPP);
  36. atexit(sdl_cleanup);
  37. return pixels_surf->pixels;
  38. }
  39. static int sdl_key = 0;
  40. static int sdl_modifiers = 0;
  41. void* sdl_get_fb() {
  42. return pixels_surf->pixels;
  43. }
  44. long sdl_get_fbsize() {
  45. return WIDTH*HEIGHT*BPP;
  46. }
  47. Cell* fbfs_open() {
  48. sdl_init(0);
  49. return alloc_int(1);
  50. }
  51. Cell* fbfs_read(Cell* stream) {
  52. Stream* s = (Stream*)stream->ar.addr;
  53. char* path = s->path->ar.addr;
  54. if (!strcmp(path+12,"/width")) {
  55. return alloc_int(WIDTH);
  56. }
  57. else if (!strcmp(path+12,"/height")) {
  58. return alloc_int(HEIGHT);
  59. }
  60. else if (!strcmp(path+12,"/depth")) {
  61. return alloc_int(BPP);
  62. }
  63. else if (!strcmp(path+12,"/")) {
  64. return
  65. alloc_cons(alloc_string_copy("/width"),
  66. alloc_cons(alloc_string_copy("/height"),
  67. alloc_cons(alloc_string_copy("/depth"),alloc_nil())));
  68. }
  69. else {
  70. return alloc_int(0);
  71. }
  72. }
  73. Cell* fbfs_write(Cell* arg) {
  74. sdl_init(0);
  75. SDL_Event event;
  76. if (SDL_PollEvent(&event))
  77. {
  78. if (event.type==SDL_QUIT) exit(0);
  79. }
  80. SDL_Rect sr = {0,0,WIDTH,HEIGHT};
  81. SDL_Rect dr = {0,0,WIDTH*SCALE,HEIGHT*SCALE};
  82. if (SCALE!=1) {
  83. SDL_BlitScaled(pixels_surf,&sr,win_surf,&dr);
  84. } else {
  85. SDL_BlitSurface(pixels_surf,NULL,win_surf,NULL);
  86. }
  87. // TODO only if changes happened
  88. SDL_UpdateWindowSurface(win);
  89. SDL_Delay(20);
  90. return arg;
  91. }
  92. Cell* fbfs_mmap(Cell* arg) {
  93. sdl_init(0);
  94. Cell* fbtest = alloc_num_bytes(0);
  95. fbtest->ar.addr = sdl_get_fb();
  96. fbtest->dr.size = sdl_get_fbsize();
  97. //printf("fbtest->addr: %p\n",fbtest->addr);
  98. //printf("fbtest->size: %lx\n",fbtest->size);
  99. return fbtest;
  100. }
  101. void sdl_mount_fbfs() {
  102. fs_mount_builtin("/framebuffer", fbfs_open, fbfs_read, fbfs_write, 0, fbfs_mmap);
  103. }
  104. Cell* keyfs_open() {
  105. return alloc_int(1);
  106. }
  107. static int mouse_buttons=0;
  108. static int mouse_x=0;
  109. static int mouse_y=0;
  110. static int last_mouse_x=0;
  111. static int last_mouse_y=0;
  112. Cell* keyfs_read() {
  113. sdl_key = 0;
  114. SDL_Event event;
  115. if (SDL_PollEvent(&event))
  116. {
  117. //printf("sdl event! %d\n",event.type);
  118. switch (event.type)
  119. {
  120. case SDL_QUIT:
  121. exit(0);
  122. break;
  123. case SDL_TEXTINPUT:
  124. case SDL_KEYDOWN:
  125. if (event.type == SDL_KEYDOWN) {
  126. sdl_modifiers = event.key.keysym.mod;
  127. //printf("key: %d, mod: %x\r\n",event.key.keysym.sym,event.key.keysym.mod);
  128. sdl_key = event.key.keysym.sym;
  129. } else {
  130. sdl_modifiers = 0;
  131. sdl_key = event.text.text[0];
  132. }
  133. if (sdl_key<200) {
  134. } else {
  135. switch (sdl_key) {
  136. case 1073741906: sdl_key = 17; break; // DC1 cursor up
  137. case 1073741905: sdl_key = 18; break; // DC2 cursor down
  138. case 1073741904: sdl_key = 19; break; // DC3 cursor left
  139. case 1073741903: sdl_key = 20; break; // DC4 cursor right
  140. default: sdl_key = 0;
  141. }
  142. }
  143. if (sdl_modifiers&1 || sdl_modifiers&2) {
  144. if (sdl_key>='a' && sdl_key<='z') {
  145. sdl_key+=('A'-'a');
  146. }
  147. else {
  148. switch (sdl_key) {
  149. case 223: sdl_key = '?'; break;
  150. case '1': sdl_key = '!'; break;
  151. case '2': sdl_key = '"'; break;
  152. case '3': sdl_key = '~'; break;
  153. case '4': sdl_key = '$'; break;
  154. case '5': sdl_key = '%'; break;
  155. case '6': sdl_key = '&'; break;
  156. case '7': sdl_key = '/'; break;
  157. case '8': sdl_key = '('; break;
  158. case '9': sdl_key = ')'; break;
  159. case '0': sdl_key = '='; break;
  160. case '<': sdl_key = '>'; break;
  161. case '+': sdl_key = '*'; break;
  162. case '#': sdl_key = '\''; break;
  163. case ',': sdl_key = ';'; break;
  164. case '.': sdl_key = ':'; break;
  165. case '-': sdl_key = '_'; break;
  166. default: break;
  167. }
  168. }
  169. }
  170. break;
  171. case SDL_MOUSEMOTION:
  172. mouse_x = event.motion.x;
  173. mouse_y = event.motion.y;
  174. mouse_buttons = event.motion.state;
  175. break;
  176. }
  177. }
  178. switch (sdl_key) {
  179. case 13: sdl_key = 10; break;
  180. case 8: sdl_key = 127; break;
  181. }
  182. Cell* res = alloc_string_copy(" ");
  183. ((uint8_t*)res->ar.addr)[0] = sdl_key;
  184. sdl_key = 0;
  185. return res;
  186. }
  187. void sdl_mount_keyfs() {
  188. fs_mount_builtin("/keyboard", keyfs_open, keyfs_read, 0, 0, 0);
  189. }
  190. Cell* mouse_open(Cell* cpath) {
  191. if (!cpath || cpath->tag!=TAG_STR) {
  192. printf("[usbmouse] open error: non-string path given\r\n");
  193. return alloc_nil();
  194. }
  195. return alloc_int(1);
  196. }
  197. Cell* mouse_read(Cell* stream) {
  198. mouse_buttons = SDL_GetMouseState(&mouse_x, &mouse_y);
  199. int mouse_dx = mouse_x - last_mouse_x;
  200. int mouse_dy = mouse_y - last_mouse_y;
  201. Cell* res = alloc_cons(alloc_cons(alloc_int(mouse_x/SCALE),alloc_int(mouse_y/SCALE)),alloc_int(mouse_buttons));
  202. last_mouse_x = mouse_x;
  203. last_mouse_y = mouse_y;
  204. return res;
  205. }
  206. Cell* mouse_write(Cell* arg) {
  207. return NULL;
  208. }
  209. Cell* mouse_mmap(Cell* arg) {
  210. return alloc_nil();
  211. }
  212. void sdl_mount_mousefs() {
  213. fs_mount_builtin("/mouse", mouse_open, mouse_read, mouse_write, 0, mouse_mmap);
  214. }
  215. void dev_sdl_init() {
  216. sdl_mount_fbfs();
  217. sdl_mount_keyfs();
  218. sdl_mount_mousefs();
  219. }