25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

508 lines
17 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright © 2002—2021 Sam Hocevar <sam@hocevar.net>
  4. * 2012 Bastian Märkisch <bmaerkisch@web.de>
  5. * All Rights Reserved
  6. *
  7. * This library is free software. It comes without any warranty, to
  8. * the extent permitted by applicable law. You can redistribute it
  9. * and/or modify it under the terms of the Do What the Fuck You Want
  10. * to Public License, Version 2, as published by Sam Hocevar. See
  11. * http://www.wtfpl.net/ for more details.
  12. */
  13. /*
  14. * This file contains the libcaca Win32 input and output driver
  15. */
  16. #include "config.h"
  17. #if defined(USE_WIN32)
  18. #define _WIN32_WINNT 0x500 /* Require WinXP or later */
  19. #define WIN32_LEAN_AND_MEAN
  20. #include <windows.h>
  21. #ifdef __MINGW32__
  22. /* This is missing from the MinGW headers. */
  23. # if (_WIN32_WINNT >= 0x0500)
  24. BOOL WINAPI GetCurrentConsoleFont(HANDLE hConsoleOutput, BOOL bMaximumWindow,
  25. PCONSOLE_FONT_INFO lpConsoleCurrentFont);
  26. # endif
  27. #endif
  28. #ifndef MOUSE_HWHEELED
  29. # define MOUSE_HWHEELED 0x0008
  30. #endif
  31. #include <stdlib.h>
  32. #include <stdio.h>
  33. #include "caca.h"
  34. #include "caca_internals.h"
  35. /*
  36. * Global variables
  37. */
  38. static int const win32_fg_palette[] =
  39. {
  40. 0,
  41. FOREGROUND_BLUE,
  42. FOREGROUND_GREEN,
  43. FOREGROUND_GREEN | FOREGROUND_BLUE,
  44. FOREGROUND_RED,
  45. FOREGROUND_RED | FOREGROUND_BLUE,
  46. FOREGROUND_RED | FOREGROUND_GREEN,
  47. FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
  48. FOREGROUND_INTENSITY,
  49. FOREGROUND_INTENSITY | FOREGROUND_BLUE,
  50. FOREGROUND_INTENSITY | FOREGROUND_GREEN,
  51. FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE,
  52. FOREGROUND_INTENSITY | FOREGROUND_RED,
  53. FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE,
  54. FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN,
  55. FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
  56. };
  57. static int const win32_bg_palette[] =
  58. {
  59. 0,
  60. BACKGROUND_BLUE,
  61. BACKGROUND_GREEN,
  62. BACKGROUND_GREEN | BACKGROUND_BLUE,
  63. BACKGROUND_RED,
  64. BACKGROUND_RED | BACKGROUND_BLUE,
  65. BACKGROUND_RED | BACKGROUND_GREEN,
  66. BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE,
  67. BACKGROUND_INTENSITY,
  68. BACKGROUND_INTENSITY | BACKGROUND_BLUE,
  69. BACKGROUND_INTENSITY | BACKGROUND_GREEN,
  70. BACKGROUND_INTENSITY | BACKGROUND_GREEN | BACKGROUND_BLUE,
  71. BACKGROUND_INTENSITY | BACKGROUND_RED,
  72. BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE,
  73. BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN,
  74. BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
  75. };
  76. struct driver_private
  77. {
  78. HANDLE hin, hout, screen;
  79. CHAR_INFO *buffer;
  80. CONSOLE_CURSOR_INFO cci;
  81. DWORD mouse_state;
  82. DWORD mode;
  83. BOOL new_console;
  84. };
  85. static int win32_init_graphics(caca_display_t *dp)
  86. {
  87. int width = caca_get_canvas_width(dp->cv);
  88. int height = caca_get_canvas_height(dp->cv);
  89. CONSOLE_SCREEN_BUFFER_INFO csbi;
  90. CONSOLE_CURSOR_INFO cci_screen;
  91. SMALL_RECT rect;
  92. COORD size;
  93. dp->drv.p = malloc(sizeof(struct driver_private));
  94. /* This call is allowed to fail in case we already have a console. */
  95. dp->drv.p->new_console = AllocConsole();
  96. dp->drv.p->hin = GetStdHandle(STD_INPUT_HANDLE);
  97. dp->drv.p->hout = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE,
  98. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
  99. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  100. if(dp->drv.p->hout == INVALID_HANDLE_VALUE)
  101. return -1;
  102. /* Preserve state information */
  103. GetConsoleCursorInfo(dp->drv.p->hout, &dp->drv.p->cci);
  104. dp->drv.p->screen =
  105. CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
  106. 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
  107. if(!dp->drv.p->screen || dp->drv.p->screen == INVALID_HANDLE_VALUE)
  108. return -1;
  109. dp->drv.p->mouse_state = 0;
  110. /* Set the new console size, default to current screen size. */
  111. size.X = width ? width : 80;
  112. size.Y = height ? height : 25;
  113. if ((width <= 0) && (height <= 0))
  114. {
  115. CONSOLE_SCREEN_BUFFER_INFO info;
  116. if (GetConsoleScreenBufferInfo(dp->drv.p->hout, &info) != 0)
  117. size = info.dwSize;
  118. }
  119. SetConsoleScreenBufferSize(dp->drv.p->screen, size);
  120. rect.Left = rect.Top = 0;
  121. rect.Right = size.X - 1;
  122. rect.Bottom = size.Y - 1;
  123. SetConsoleWindowInfo(dp->drv.p->screen, TRUE, &rect);
  124. /* Report our new size to libcaca */
  125. if(!GetConsoleScreenBufferInfo(dp->drv.p->screen, &csbi))
  126. return -1;
  127. dp->resize.allow = 1;
  128. caca_set_canvas_size(dp->cv,
  129. csbi.srWindow.Right - csbi.srWindow.Left + 1,
  130. csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
  131. width = caca_get_canvas_width(dp->cv);
  132. height = caca_get_canvas_height(dp->cv);
  133. dp->resize.allow = 0;
  134. SetConsoleMode(dp->drv.p->screen, 0);
  135. /* We want mouse and window resize events. */
  136. GetConsoleMode(dp->drv.p->hin, &dp->drv.p->mode);
  137. SetConsoleMode(dp->drv.p->hin, ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
  138. cci_screen.dwSize = 1; /* must be > 0, see MSDN */
  139. cci_screen.bVisible = FALSE;
  140. SetConsoleCursorInfo(dp->drv.p->screen, &cci_screen);
  141. SetConsoleActiveScreenBuffer(dp->drv.p->screen);
  142. dp->drv.p->buffer = _caca_alloc2d(width, height, sizeof(CHAR_INFO));
  143. if(dp->drv.p->buffer == NULL)
  144. return -1;
  145. return 0;
  146. }
  147. static int win32_end_graphics(caca_display_t *dp)
  148. {
  149. SetConsoleActiveScreenBuffer(dp->drv.p->hout);
  150. CloseHandle(dp->drv.p->screen);
  151. /* Reset console parameters */
  152. SetConsoleMode(dp->drv.p->hin, dp->drv.p->mode);
  153. SetConsoleCursorInfo(dp->drv.p->hout, &dp->drv.p->cci);
  154. CloseHandle(dp->drv.p->hout);
  155. /* We only free the console if we successfully created a new one before */
  156. if (dp->drv.p->new_console)
  157. FreeConsole();
  158. free(dp->drv.p);
  159. return 0;
  160. }
  161. static int win32_set_display_title(caca_display_t *dp, char const *title)
  162. {
  163. SetConsoleTitle(title);
  164. return 0;
  165. }
  166. static int win32_get_display_width(caca_display_t const *dp)
  167. {
  168. /* Fallback to a 6x10 font */
  169. int font_width = 6;
  170. #if (_WIN32_WINNT >= 0x500)
  171. CONSOLE_FONT_INFO info;
  172. if (GetCurrentConsoleFont(dp->drv.p->screen, FALSE, &info) != 0)
  173. font_width = info.dwFontSize.X;
  174. #endif
  175. return font_width * caca_get_canvas_width(dp->cv);
  176. }
  177. static int win32_get_display_height(caca_display_t const *dp)
  178. {
  179. /* Fallback to a 6x10 font */
  180. int font_height = 10;
  181. #if (_WIN32_WINNT >= 0x500)
  182. CONSOLE_FONT_INFO info;
  183. if (GetCurrentConsoleFont(dp->drv.p->screen, FALSE, &info) != 0)
  184. font_height = info.dwFontSize.Y;
  185. #endif
  186. return font_height * caca_get_canvas_height(dp->cv);
  187. }
  188. static void win32_display(caca_display_t *dp)
  189. {
  190. COORD size, pos;
  191. SMALL_RECT rect;
  192. CHAR_INFO *buffer = dp->drv.p->buffer;
  193. uint32_t const *cvchars = caca_get_canvas_chars(dp->cv);
  194. uint32_t const *cvattrs = caca_get_canvas_attrs(dp->cv);
  195. int width = caca_get_canvas_width(dp->cv);
  196. int height = caca_get_canvas_height(dp->cv);
  197. int n;
  198. /* Render everything to our screen buffer */
  199. for(n = height * width; n--; )
  200. {
  201. uint32_t ch = *cvchars++;
  202. uint16_t bgfg = caca_attr_to_ansi(*cvattrs);
  203. uint8_t fg = bgfg & 0xf;
  204. uint8_t bg = bgfg >> 4;
  205. #if 0
  206. if(ch > 0x00000020 && ch < 0x00000080)
  207. dp->drv.p->buffer[i].Char.AsciiChar = (uint8_t)ch;
  208. else
  209. dp->drv.p->buffer[i].Char.AsciiChar = ' ';
  210. #else
  211. if(n && *cvchars == CACA_MAGIC_FULLWIDTH)
  212. ;
  213. else if(ch > 0x00000020 && ch < 0x00010000)
  214. buffer->Char.UnicodeChar = (uint16_t)ch;
  215. else
  216. buffer->Char.UnicodeChar = (uint16_t)' ';
  217. #endif
  218. buffer->Attributes = win32_fg_palette[fg < 0x10 ? fg : CACA_LIGHTGRAY]
  219. | win32_bg_palette[bg < 0x10 ? bg : CACA_BLACK];
  220. cvattrs++;
  221. buffer++;
  222. }
  223. /* Blit the screen buffer */
  224. size.X = width;
  225. size.Y = height;
  226. pos.X = pos.Y = 0;
  227. rect.Left = rect.Top = 0;
  228. rect.Right = width - 1;
  229. rect.Bottom = height - 1;
  230. #if 0
  231. WriteConsoleOutput(dp->drv.p->screen, dp->drv.p->buffer, size, pos, &rect);
  232. #else
  233. /* FIXME: would this benefit from dirty rectangles? */
  234. WriteConsoleOutputW(dp->drv.p->screen, dp->drv.p->buffer, size, pos, &rect);
  235. #endif
  236. }
  237. static void win32_handle_resize(caca_display_t *dp)
  238. {
  239. /* We only need to resize the internal buffer. */
  240. dp->drv.p->buffer = realloc(dp->drv.p->buffer,
  241. dp->resize.w * dp->resize.h * sizeof(CHAR_INFO));
  242. }
  243. static int win32_get_event(caca_display_t *dp, caca_privevent_t *ev)
  244. {
  245. INPUT_RECORD rec;
  246. DWORD num;
  247. for( ; ; )
  248. {
  249. HANDLE hin = GetStdHandle(STD_INPUT_HANDLE);
  250. if(hin == INVALID_HANDLE_VALUE)
  251. break;
  252. GetNumberOfConsoleInputEvents(hin, &num);
  253. if(num == 0)
  254. break;
  255. if(!ReadConsoleInput(hin, &rec, 1, &num) || (num == 0))
  256. break;
  257. if(rec.EventType == KEY_EVENT)
  258. {
  259. if(rec.Event.KeyEvent.bKeyDown)
  260. ev->type = CACA_EVENT_KEY_PRESS;
  261. else
  262. ev->type = CACA_EVENT_KEY_RELEASE;
  263. if(rec.Event.KeyEvent.uChar.AsciiChar)
  264. {
  265. ev->data.key.ch = rec.Event.KeyEvent.uChar.AsciiChar;
  266. ev->data.key.utf32 = (uint32_t)ev->data.key.ch;
  267. ev->data.key.utf8[0] = ev->data.key.ch;
  268. ev->data.key.utf8[1] = '\0';
  269. return 1;
  270. }
  271. else
  272. {
  273. switch (rec.Event.KeyEvent.wVirtualKeyCode)
  274. {
  275. case VK_TAB: ev->data.key.ch = '\t'; break;
  276. case VK_RETURN: ev->data.key.ch = '\r'; break;
  277. case VK_ESCAPE: ev->data.key.ch = '\033'; break;
  278. case VK_SPACE: ev->data.key.ch = ' '; break;
  279. case VK_DELETE: ev->data.key.ch = '\x7f'; break;
  280. case VK_LEFT: ev->data.key.ch = CACA_KEY_LEFT; break;
  281. case VK_RIGHT: ev->data.key.ch = CACA_KEY_RIGHT; break;
  282. case VK_UP: ev->data.key.ch = CACA_KEY_UP; break;
  283. case VK_DOWN: ev->data.key.ch = CACA_KEY_DOWN; break;
  284. case VK_INSERT: ev->data.key.ch = CACA_KEY_INSERT; break;
  285. case VK_HOME: ev->data.key.ch = CACA_KEY_HOME; break;
  286. case VK_END: ev->data.key.ch = CACA_KEY_END; break;
  287. case VK_PRIOR: ev->data.key.ch = CACA_KEY_PAGEUP; break;
  288. case VK_NEXT: ev->data.key.ch = CACA_KEY_PAGEDOWN; break;
  289. case VK_F1: ev->data.key.ch = CACA_KEY_F1; break;
  290. case VK_F2: ev->data.key.ch = CACA_KEY_F2; break;
  291. case VK_F3: ev->data.key.ch = CACA_KEY_F3; break;
  292. case VK_F4: ev->data.key.ch = CACA_KEY_F4; break;
  293. case VK_F5: ev->data.key.ch = CACA_KEY_F5; break;
  294. case VK_F6: ev->data.key.ch = CACA_KEY_F6; break;
  295. case VK_F7: ev->data.key.ch = CACA_KEY_F7; break;
  296. case VK_F8: ev->data.key.ch = CACA_KEY_F8; break;
  297. case VK_F9: ev->data.key.ch = CACA_KEY_F9; break;
  298. case VK_F10: ev->data.key.ch = CACA_KEY_F10; break;
  299. case VK_F11: ev->data.key.ch = CACA_KEY_F11; break;
  300. case VK_F12: ev->data.key.ch = CACA_KEY_F12; break;
  301. case VK_F13: ev->data.key.ch = CACA_KEY_F13; break;
  302. case VK_F14: ev->data.key.ch = CACA_KEY_F14; break;
  303. case VK_F15: ev->data.key.ch = CACA_KEY_F15; break;
  304. case VK_NUMPAD0: ev->data.key.ch = '0'; break;
  305. case VK_NUMPAD1: ev->data.key.ch = '1'; break;
  306. case VK_NUMPAD2: ev->data.key.ch = '2'; break;
  307. case VK_NUMPAD3: ev->data.key.ch = '3'; break;
  308. case VK_NUMPAD4: ev->data.key.ch = '4'; break;
  309. case VK_NUMPAD5: ev->data.key.ch = '5'; break;
  310. case VK_NUMPAD6: ev->data.key.ch = '6'; break;
  311. case VK_NUMPAD7: ev->data.key.ch = '7'; break;
  312. case VK_NUMPAD8: ev->data.key.ch = '8'; break;
  313. case VK_NUMPAD9: ev->data.key.ch = '9'; break;
  314. case VK_MULTIPLY: ev->data.key.ch = '*'; break;
  315. case VK_ADD: ev->data.key.ch = '+'; break;
  316. case VK_SEPARATOR: ev->data.key.ch = ','; break;
  317. case VK_SUBTRACT: ev->data.key.ch = '-'; break;
  318. case VK_DECIMAL: ev->data.key.ch = '.'; break;
  319. case VK_DIVIDE: ev->data.key.ch = '/'; break;
  320. default: ev->type = CACA_EVENT_NONE; return 0;
  321. }
  322. if((ev->data.key.ch > 0)
  323. &&
  324. (ev->data.key.ch <= 0x7f))
  325. {
  326. ev->data.key.utf32 = (uint32_t)ev->data.key.ch;
  327. ev->data.key.utf8[0] = ev->data.key.ch;
  328. ev->data.key.utf8[1] = '\0';
  329. }
  330. else
  331. {
  332. ev->data.key.utf32 = 0;
  333. ev->data.key.utf8[0] = '\0';
  334. }
  335. return 1;
  336. }
  337. }
  338. else if(rec.EventType == MOUSE_EVENT)
  339. {
  340. if((rec.Event.MouseEvent.dwEventFlags == 0) ||
  341. (rec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK))
  342. {
  343. DWORD mouse_state_changed =
  344. dp->drv.p->mouse_state ^ rec.Event.MouseEvent.dwButtonState;
  345. int button;
  346. DWORD mask = 0x01;
  347. int const mapping[] = {1, 3, 2, 8, 9};
  348. for (button = 1; button <= 5; button++)
  349. {
  350. if(mouse_state_changed == mask)
  351. {
  352. if(rec.Event.MouseEvent.dwButtonState & mask)
  353. ev->type = CACA_EVENT_MOUSE_PRESS;
  354. else
  355. ev->type = CACA_EVENT_MOUSE_RELEASE;
  356. ev->data.mouse.button = mapping[button - 1];
  357. dp->drv.p->mouse_state = rec.Event.MouseEvent.dwButtonState;
  358. return 1;
  359. }
  360. mask <<= 1;
  361. }
  362. }
  363. else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_MOVED)
  364. {
  365. COORD pos = rec.Event.MouseEvent.dwMousePosition;
  366. if(dp->mouse.x == pos.X && dp->mouse.y == pos.Y)
  367. continue;
  368. dp->mouse.x = pos.X;
  369. dp->mouse.y = pos.Y;
  370. ev->type = CACA_EVENT_MOUSE_MOTION;
  371. ev->data.mouse.x = dp->mouse.x;
  372. ev->data.mouse.y = dp->mouse.y;
  373. return 1;
  374. }
  375. else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_WHEELED)
  376. {
  377. ev->type = CACA_EVENT_MOUSE_PRESS;
  378. if((int16_t)HIWORD(rec.Event.MouseEvent.dwButtonState) > 0)
  379. ev->data.mouse.button = 4; /* up */
  380. else
  381. ev->data.mouse.button = 5; /* down */
  382. return 1;
  383. }
  384. else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_HWHEELED)
  385. {
  386. ev->type = CACA_EVENT_MOUSE_PRESS;
  387. if((int16_t)HIWORD(rec.Event.MouseEvent.dwButtonState) > 0)
  388. ev->data.mouse.button = 7; /* right */
  389. else
  390. ev->data.mouse.button = 6; /* left */
  391. return 1;
  392. }
  393. }
  394. else if(rec.EventType == WINDOW_BUFFER_SIZE_EVENT)
  395. {
  396. int width = caca_get_canvas_width(dp->cv);
  397. int height = caca_get_canvas_height(dp->cv);
  398. int w = rec.Event.WindowBufferSizeEvent.dwSize.X;
  399. int h = rec.Event.WindowBufferSizeEvent.dwSize.Y;
  400. if((w == 0) || (h == 0) || ((w == width) && (h == height)))
  401. continue;
  402. /* resize the canvas accordingly */
  403. ev->type = CACA_EVENT_RESIZE;
  404. dp->resize.w = w;
  405. dp->resize.h = h;
  406. dp->resize.resized = 1;
  407. return 1;
  408. }
  409. /* Unknown event */
  410. ev->type = CACA_EVENT_NONE;
  411. return 0;
  412. }
  413. /* No event */
  414. ev->type = CACA_EVENT_NONE;
  415. return 0;
  416. }
  417. /*
  418. * Driver initialisation
  419. */
  420. int win32_install(caca_display_t *dp)
  421. {
  422. dp->drv.id = CACA_DRIVER_WIN32;
  423. dp->drv.driver = "win32";
  424. dp->drv.init_graphics = win32_init_graphics;
  425. dp->drv.end_graphics = win32_end_graphics;
  426. dp->drv.set_display_title = win32_set_display_title;
  427. dp->drv.get_display_width = win32_get_display_width;
  428. dp->drv.get_display_height = win32_get_display_height;
  429. dp->drv.display = win32_display;
  430. dp->drv.handle_resize = win32_handle_resize;
  431. dp->drv.get_event = win32_get_event;
  432. dp->drv.set_mouse = NULL;
  433. dp->drv.set_cursor = NULL;
  434. return 0;
  435. }
  436. #endif /* USE_WIN32 */