Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

369 lignes
11 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 "cucul.h"
  23. #include "caca.h"
  24. #include "caca_internals.h"
  25. /*
  26. * Global variables
  27. */
  28. static int const win32_fg_palette[] =
  29. {
  30. 0,
  31. FOREGROUND_BLUE,
  32. FOREGROUND_GREEN,
  33. FOREGROUND_GREEN | FOREGROUND_BLUE,
  34. FOREGROUND_RED,
  35. FOREGROUND_RED | FOREGROUND_BLUE,
  36. FOREGROUND_RED | FOREGROUND_GREEN,
  37. FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
  38. FOREGROUND_INTENSITY,
  39. FOREGROUND_INTENSITY | FOREGROUND_BLUE,
  40. FOREGROUND_INTENSITY | FOREGROUND_GREEN,
  41. FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE,
  42. FOREGROUND_INTENSITY | FOREGROUND_RED,
  43. FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE,
  44. FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN,
  45. FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
  46. };
  47. static int const win32_bg_palette[] =
  48. {
  49. 0,
  50. BACKGROUND_BLUE,
  51. BACKGROUND_GREEN,
  52. BACKGROUND_GREEN | BACKGROUND_BLUE,
  53. BACKGROUND_RED,
  54. BACKGROUND_RED | BACKGROUND_BLUE,
  55. BACKGROUND_RED | BACKGROUND_GREEN,
  56. BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE,
  57. BACKGROUND_INTENSITY,
  58. BACKGROUND_INTENSITY | BACKGROUND_BLUE,
  59. BACKGROUND_INTENSITY | BACKGROUND_GREEN,
  60. BACKGROUND_INTENSITY | BACKGROUND_GREEN | BACKGROUND_BLUE,
  61. BACKGROUND_INTENSITY | BACKGROUND_RED,
  62. BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE,
  63. BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN,
  64. BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
  65. };
  66. struct driver_private
  67. {
  68. HANDLE hin, hout, screen;
  69. CHAR_INFO *buffer;
  70. CONSOLE_CURSOR_INFO cci;
  71. };
  72. static int win32_init_graphics(caca_display_t *dp)
  73. {
  74. int width = cucul_get_canvas_width(dp->cv);
  75. int height = cucul_get_canvas_height(dp->cv);
  76. CONSOLE_SCREEN_BUFFER_INFO csbi;
  77. SMALL_RECT rect;
  78. COORD size;
  79. dp->drv.p = malloc(sizeof(struct driver_private));
  80. /* This call is allowed to fail in case we already have a console */
  81. AllocConsole();
  82. dp->drv.p->hin = GetStdHandle(STD_INPUT_HANDLE);
  83. dp->drv.p->hout = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE,
  84. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
  85. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  86. if(dp->drv.p->hout == INVALID_HANDLE_VALUE)
  87. return -1;
  88. GetConsoleCursorInfo(dp->drv.p->hout, &dp->drv.p->cci);
  89. dp->drv.p->cci.bVisible = FALSE;
  90. SetConsoleCursorInfo(dp->drv.p->hout, &dp->drv.p->cci);
  91. SetConsoleMode(dp->drv.p->hout, ENABLE_MOUSE_INPUT);
  92. dp->drv.p->screen =
  93. CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
  94. 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
  95. if(!dp->drv.p->screen || dp->drv.p->screen == INVALID_HANDLE_VALUE)
  96. return -1;
  97. /* Set the new console size */
  98. size.X = width ? width : 80;
  99. size.Y = height ? height : 25;
  100. SetConsoleScreenBufferSize(dp->drv.p->screen, size);
  101. rect.Left = rect.Top = 0;
  102. rect.Right = size.X - 1;
  103. rect.Bottom = size.Y - 1;
  104. SetConsoleWindowInfo(dp->drv.p->screen, TRUE, &rect);
  105. /* Report our new size to libcucul */
  106. if(!GetConsoleScreenBufferInfo(dp->drv.p->screen, &csbi))
  107. return -1;
  108. dp->resize.allow = 1;
  109. cucul_set_canvas_size(dp->cv,
  110. csbi.srWindow.Right - csbi.srWindow.Left + 1,
  111. csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
  112. width = cucul_get_canvas_width(dp->cv);
  113. height = cucul_get_canvas_height(dp->cv);
  114. dp->resize.allow = 0;
  115. SetConsoleMode(dp->drv.p->screen, 0);
  116. GetConsoleCursorInfo(dp->drv.p->screen, &dp->drv.p->cci);
  117. dp->drv.p->cci.dwSize = 0;
  118. dp->drv.p->cci.bVisible = FALSE;
  119. SetConsoleCursorInfo(dp->drv.p->screen, &dp->drv.p->cci);
  120. SetConsoleActiveScreenBuffer(dp->drv.p->screen);
  121. dp->drv.p->buffer = malloc(width * height
  122. * sizeof(CHAR_INFO));
  123. if(dp->drv.p->buffer == NULL)
  124. return -1;
  125. return 0;
  126. }
  127. static int win32_end_graphics(caca_display_t *dp)
  128. {
  129. SetConsoleActiveScreenBuffer(dp->drv.p->hout);
  130. CloseHandle(dp->drv.p->screen);
  131. SetConsoleTextAttribute(dp->drv.p->hout, FOREGROUND_INTENSITY
  132. | FOREGROUND_RED
  133. | FOREGROUND_GREEN
  134. | FOREGROUND_BLUE);
  135. dp->drv.p->cci.bVisible = TRUE;
  136. SetConsoleCursorInfo(dp->drv.p->hout, &dp->drv.p->cci);
  137. CloseHandle(dp->drv.p->hout);
  138. free(dp->drv.p);
  139. return 0;
  140. }
  141. static int win32_set_display_title(caca_display_t *dp, char const *title)
  142. {
  143. SetConsoleTitle(title);
  144. return 0;
  145. }
  146. static int win32_get_display_width(caca_display_t const *dp)
  147. {
  148. /* FIXME */
  149. /* Fallback to a 6x10 font */
  150. return cucul_get_canvas_width(dp->cv) * 6;
  151. }
  152. static int win32_get_display_height(caca_display_t const *dp)
  153. {
  154. /* FIXME */
  155. /* Fallback to a 6x10 font */
  156. return cucul_get_canvas_height(dp->cv) * 10;
  157. }
  158. static void win32_display(caca_display_t *dp)
  159. {
  160. COORD size, pos;
  161. SMALL_RECT rect;
  162. CHAR_INFO *buffer = dp->drv.p->buffer;
  163. uint32_t const *cvchars = (uint32_t const *)cucul_get_canvas_chars(dp->cv);
  164. uint32_t const *cvattrs = (uint32_t const *)cucul_get_canvas_attrs(dp->cv);
  165. int width = cucul_get_canvas_width(dp->cv);
  166. int height = cucul_get_canvas_height(dp->cv);
  167. int n;
  168. /* Render everything to our screen buffer */
  169. for(n = height * width; n--; )
  170. {
  171. uint32_t ch = *cvchars++;
  172. uint8_t fg = cucul_attr_to_ansi_fg(*cvattrs);
  173. uint8_t bg = cucul_attr_to_ansi_bg(*cvattrs);
  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 == CUCUL_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 : CUCUL_LIGHTGRAY]
  188. | win32_bg_palette[bg < 0x10 ? bg : CUCUL_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 = cucul_get_canvas_width(dp->cv);
  209. dp->resize.h = cucul_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. }
  236. if(rec.EventType == MOUSE_EVENT)
  237. {
  238. if(rec.Event.MouseEvent.dwEventFlags == 0)
  239. {
  240. if(rec.Event.MouseEvent.dwButtonState & 0x01)
  241. {
  242. ev->type = CACA_EVENT_MOUSE_PRESS;
  243. ev->data.mouse.button = 1;
  244. return 1;
  245. }
  246. if(rec.Event.MouseEvent.dwButtonState & 0x02)
  247. {
  248. ev->type = CACA_EVENT_MOUSE_PRESS;
  249. ev->data.mouse.button = 2;
  250. return 1;
  251. }
  252. }
  253. else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_MOVED)
  254. {
  255. COORD pos = rec.Event.MouseEvent.dwMousePosition;
  256. if(dp->mouse.x == pos.X && dp->mouse.y == pos.Y)
  257. continue;
  258. dp->mouse.x = pos.X;
  259. dp->mouse.y = pos.Y;
  260. ev->type = CACA_EVENT_MOUSE_MOTION;
  261. ev->data.mouse.x = dp->mouse.x;
  262. ev->data.mouse.y = dp->mouse.y;
  263. return 1;
  264. }
  265. #if 0
  266. else if(rec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)
  267. {
  268. cout << rec.Event.MouseEvent.dwMousePosition.X << "," <<
  269. rec.Event.MouseEvent.dwMousePosition.Y << " " << flush;
  270. }
  271. else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_WHEELED)
  272. {
  273. SetConsoleCursorPosition(hOut,
  274. WheelWhere);
  275. if(rec.Event.MouseEvent.dwButtonState & 0xFF000000)
  276. cout << "Down" << flush;
  277. else
  278. cout << "Up " << flush;
  279. }
  280. #endif
  281. }
  282. /* Unknown event */
  283. ev->type = CACA_EVENT_NONE;
  284. return 0;
  285. }
  286. /* No event */
  287. ev->type = CACA_EVENT_NONE;
  288. return 0;
  289. }
  290. /*
  291. * Driver initialisation
  292. */
  293. int win32_install(caca_display_t *dp)
  294. {
  295. dp->drv.id = CACA_DRIVER_WIN32;
  296. dp->drv.driver = "win32";
  297. dp->drv.init_graphics = win32_init_graphics;
  298. dp->drv.end_graphics = win32_end_graphics;
  299. dp->drv.set_display_title = win32_set_display_title;
  300. dp->drv.get_display_width = win32_get_display_width;
  301. dp->drv.get_display_height = win32_get_display_height;
  302. dp->drv.display = win32_display;
  303. dp->drv.handle_resize = win32_handle_resize;
  304. dp->drv.get_event = win32_get_event;
  305. dp->drv.set_mouse = NULL;
  306. dp->drv.set_cursor = NULL;
  307. return 0;
  308. }
  309. #endif /* USE_WIN32 */