Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

442 rader
15 KiB

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