sdl2.c 6.0 KB

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