You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

363 lines
10 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. #include "common.h"
  19. #if defined(USE_WIN32)
  20. #include <windows.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include "caca.h"
  24. #include "caca_internals.h"
  25. #include "cucul.h"
  26. #include "cucul_internals.h"
  27. /*
  28. * Global variables
  29. */
  30. static int const win32_fg_palette[] =
  31. {
  32. 0,
  33. FOREGROUND_BLUE,
  34. FOREGROUND_GREEN,
  35. FOREGROUND_GREEN | FOREGROUND_BLUE,
  36. FOREGROUND_RED,
  37. FOREGROUND_RED | FOREGROUND_BLUE,
  38. FOREGROUND_RED | FOREGROUND_GREEN,
  39. FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
  40. FOREGROUND_INTENSITY,
  41. FOREGROUND_INTENSITY | FOREGROUND_BLUE,
  42. FOREGROUND_INTENSITY | FOREGROUND_GREEN,
  43. FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE,
  44. FOREGROUND_INTENSITY | FOREGROUND_RED,
  45. FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE,
  46. FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN,
  47. FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
  48. };
  49. static int const win32_bg_palette[] =
  50. {
  51. 0,
  52. BACKGROUND_BLUE,
  53. BACKGROUND_GREEN,
  54. BACKGROUND_GREEN | BACKGROUND_BLUE,
  55. BACKGROUND_RED,
  56. BACKGROUND_RED | BACKGROUND_BLUE,
  57. BACKGROUND_RED | BACKGROUND_GREEN,
  58. BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE,
  59. BACKGROUND_INTENSITY,
  60. BACKGROUND_INTENSITY | BACKGROUND_BLUE,
  61. BACKGROUND_INTENSITY | BACKGROUND_GREEN,
  62. BACKGROUND_INTENSITY | BACKGROUND_GREEN | BACKGROUND_BLUE,
  63. BACKGROUND_INTENSITY | BACKGROUND_RED,
  64. BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE,
  65. BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN,
  66. BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
  67. };
  68. struct driver_private
  69. {
  70. HANDLE hin, hout, screen;
  71. CHAR_INFO *buffer;
  72. CONSOLE_CURSOR_INFO cci;
  73. };
  74. static int win32_init_graphics(caca_display_t *dp)
  75. {
  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 = dp->cv->width ? dp->cv->width : 80;
  99. size.Y = dp->cv->height ? dp->cv->height : 25;
  100. SetConsoleScreenBufferSize(dp->drv.p->screen, size);
  101. rect.Left = rect.Top = 0;
  102. rect.Right = dp->cv->width - 1;
  103. rect.Bottom = dp->cv->height - 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. __cucul_set_canvas_size(dp->cv,
  109. csbi.srWindow.Right - csbi.srWindow.Left + 1,
  110. csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
  111. SetConsoleMode(dp->drv.p->screen, 0);
  112. GetConsoleCursorInfo(dp->drv.p->screen, &dp->drv.p->cci);
  113. dp->drv.p->cci.dwSize = 0;
  114. dp->drv.p->cci.bVisible = FALSE;
  115. SetConsoleCursorInfo(dp->drv.p->screen, &dp->drv.p->cci);
  116. SetConsoleActiveScreenBuffer(dp->drv.p->screen);
  117. dp->drv.p->buffer = malloc(dp->cv->width * dp->cv->height
  118. * sizeof(CHAR_INFO));
  119. if(dp->drv.p->buffer == NULL)
  120. return -1;
  121. return 0;
  122. }
  123. static int win32_end_graphics(caca_display_t *dp)
  124. {
  125. SetConsoleActiveScreenBuffer(dp->drv.p->hout);
  126. CloseHandle(dp->drv.p->screen);
  127. SetConsoleTextAttribute(dp->drv.p->hout, FOREGROUND_INTENSITY
  128. | FOREGROUND_RED
  129. | FOREGROUND_GREEN
  130. | FOREGROUND_BLUE);
  131. dp->drv.p->cci.bVisible = TRUE;
  132. SetConsoleCursorInfo(dp->drv.p->hout, &dp->drv.p->cci);
  133. CloseHandle(dp->drv.p->hout);
  134. free(dp->drv.p);
  135. return 0;
  136. }
  137. static int win32_set_display_title(caca_display_t *dp, char const *title)
  138. {
  139. SetConsoleTitle(title);
  140. return 0;
  141. }
  142. static unsigned int win32_get_display_width(caca_display_t const *dp)
  143. {
  144. /* FIXME */
  145. /* Fallback to a 6x10 font */
  146. return dp->cv->width * 6;
  147. }
  148. static unsigned int win32_get_display_height(caca_display_t const *dp)
  149. {
  150. /* FIXME */
  151. /* Fallback to a 6x10 font */
  152. return dp->cv->height * 10;
  153. }
  154. static void win32_display(caca_display_t *dp)
  155. {
  156. COORD size, pos;
  157. SMALL_RECT rect;
  158. CHAR_INFO *buffer = dp->drv.p->buffer;
  159. uint32_t *attrs = dp->cv->attrs;
  160. uint32_t *chars = dp->cv->chars;
  161. unsigned int n;
  162. /* Render everything to our screen buffer */
  163. for(n = dp->cv->height * dp->cv->width; n--; )
  164. {
  165. uint32_t ch = *chars++;
  166. uint8_t fg = cucul_attr_to_ansi_fg(*attrs);
  167. uint8_t bg = cucul_attr_to_ansi_bg(*attrs);
  168. #if 0
  169. if(ch > 0x00000020 && ch < 0x00000080)
  170. dp->drv.p->buffer[i].Char.AsciiChar = (uint8_t)ch;
  171. else
  172. dp->drv.p->buffer[i].Char.AsciiChar = ' ';
  173. #else
  174. if(n && *chars == CUCUL_MAGIC_FULLWIDTH)
  175. ;
  176. else if(ch > 0x00000020 && ch < 0x00010000)
  177. buffer->Char.UnicodeChar = (uint16_t)ch;
  178. else
  179. buffer->Char.UnicodeChar = (uint16_t)' ';
  180. #endif
  181. buffer->Attributes = win32_fg_palette[fg < 0x10 ? fg : CUCUL_LIGHTGRAY]
  182. | win32_bg_palette[bg < 0x10 ? bg : CUCUL_BLACK];
  183. attrs++;
  184. buffer++;
  185. }
  186. /* Blit the screen buffer */
  187. size.X = dp->cv->width;
  188. size.Y = dp->cv->height;
  189. pos.X = pos.Y = 0;
  190. rect.Left = rect.Top = 0;
  191. rect.Right = dp->cv->width - 1;
  192. rect.Bottom = dp->cv->height - 1;
  193. #if 0
  194. WriteConsoleOutput(dp->drv.p->screen, dp->drv.p->buffer, size, pos, &rect);
  195. #else
  196. WriteConsoleOutputW(dp->drv.p->screen, dp->drv.p->buffer, size, pos, &rect);
  197. #endif
  198. }
  199. static void win32_handle_resize(caca_display_t *dp)
  200. {
  201. /* FIXME: I don't know what to do here. */
  202. dp->resize.w = dp->cv->width;
  203. dp->resize.h = dp->cv->height;
  204. }
  205. static int win32_get_event(caca_display_t *dp, caca_event_t *ev)
  206. {
  207. INPUT_RECORD rec;
  208. DWORD num;
  209. for( ; ; )
  210. {
  211. GetNumberOfConsoleInputEvents(dp->drv.p->hin, &num);
  212. if(num == 0)
  213. break;
  214. ReadConsoleInput(dp->drv.p->hin, &rec, 1, &num);
  215. if(rec.EventType == KEY_EVENT)
  216. {
  217. if(rec.Event.KeyEvent.bKeyDown)
  218. ev->type = CACA_EVENT_KEY_PRESS;
  219. else
  220. ev->type = CACA_EVENT_KEY_RELEASE;
  221. if(rec.Event.KeyEvent.uChar.AsciiChar)
  222. {
  223. ev->data.key.ch = rec.Event.KeyEvent.uChar.AsciiChar;
  224. ev->data.key.utf32 = (uint32_t)ev->data.key.ch;
  225. ev->data.key.utf8[0] = ev->data.key.ch;
  226. ev->data.key.utf8[1] = '\0';
  227. return 1;
  228. }
  229. }
  230. if(rec.EventType == MOUSE_EVENT)
  231. {
  232. if(rec.Event.MouseEvent.dwEventFlags == 0)
  233. {
  234. if(rec.Event.MouseEvent.dwButtonState & 0x01)
  235. {
  236. ev->type = CACA_EVENT_MOUSE_PRESS;
  237. ev->data.mouse.button = 1;
  238. return 1;
  239. }
  240. if(rec.Event.MouseEvent.dwButtonState & 0x02)
  241. {
  242. ev->type = CACA_EVENT_MOUSE_PRESS;
  243. ev->data.mouse.button = 2;
  244. return 1;
  245. }
  246. }
  247. else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_MOVED)
  248. {
  249. COORD pos = rec.Event.MouseEvent.dwMousePosition;
  250. if(dp->mouse.x == (unsigned int)pos.X &&
  251. dp->mouse.y == (unsigned int)pos.Y)
  252. continue;
  253. dp->mouse.x = pos.X;
  254. dp->mouse.y = pos.Y;
  255. ev->type = CACA_EVENT_MOUSE_MOTION;
  256. ev->data.mouse.x = dp->mouse.x;
  257. ev->data.mouse.y = dp->mouse.y;
  258. return 1;
  259. }
  260. #if 0
  261. else if(rec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)
  262. {
  263. cout << rec.Event.MouseEvent.dwMousePosition.X << "," <<
  264. rec.Event.MouseEvent.dwMousePosition.Y << " " << flush;
  265. }
  266. else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_WHEELED)
  267. {
  268. SetConsoleCursorPosition(hOut,
  269. WheelWhere);
  270. if(rec.Event.MouseEvent.dwButtonState & 0xFF000000)
  271. cout << "Down" << flush;
  272. else
  273. cout << "Up " << flush;
  274. }
  275. #endif
  276. }
  277. /* Unknown event */
  278. ev->type = CACA_EVENT_NONE;
  279. return 0;
  280. }
  281. /* No event */
  282. ev->type = CACA_EVENT_NONE;
  283. return 0;
  284. }
  285. /*
  286. * Driver initialisation
  287. */
  288. int win32_install(caca_display_t *dp)
  289. {
  290. dp->drv.driver = CACA_DRIVER_WIN32;
  291. dp->drv.init_graphics = win32_init_graphics;
  292. dp->drv.end_graphics = win32_end_graphics;
  293. dp->drv.set_display_title = win32_set_display_title;
  294. dp->drv.get_display_width = win32_get_display_width;
  295. dp->drv.get_display_height = win32_get_display_height;
  296. dp->drv.display = win32_display;
  297. dp->drv.handle_resize = win32_handle_resize;
  298. dp->drv.get_event = win32_get_event;
  299. dp->drv.set_mouse = NULL;
  300. dp->drv.set_cursor = NULL;
  301. return 0;
  302. }
  303. #endif /* USE_WIN32 */