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

325 lines
9.4 KiB

  1. /*
  2. * libcaca ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file driver_win32.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief Win32 driver
  15. *
  16. * This file contains the libcaca Win32 input and output driver
  17. */
  18. #include "config.h"
  19. #if defined(USE_WIN32)
  20. #include <windows.h>
  21. #include <stdlib.h>
  22. #include "caca.h"
  23. #include "caca_internals.h"
  24. #include "cucul.h"
  25. #include "cucul_internals.h"
  26. /*
  27. * Global variables
  28. */
  29. static int const win32_fg_palette[] =
  30. {
  31. 0,
  32. FOREGROUND_BLUE,
  33. FOREGROUND_GREEN,
  34. FOREGROUND_GREEN | FOREGROUND_BLUE,
  35. FOREGROUND_RED,
  36. FOREGROUND_RED | FOREGROUND_BLUE,
  37. FOREGROUND_RED | FOREGROUND_GREEN,
  38. FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
  39. FOREGROUND_INTENSITY,
  40. FOREGROUND_INTENSITY | FOREGROUND_BLUE,
  41. FOREGROUND_INTENSITY | FOREGROUND_GREEN,
  42. FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE,
  43. FOREGROUND_INTENSITY | FOREGROUND_RED,
  44. FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE,
  45. FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN,
  46. FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
  47. };
  48. static int const win32_bg_palette[] =
  49. {
  50. 0,
  51. BACKGROUND_BLUE,
  52. BACKGROUND_GREEN,
  53. BACKGROUND_GREEN | BACKGROUND_BLUE,
  54. BACKGROUND_RED,
  55. BACKGROUND_RED | BACKGROUND_BLUE,
  56. BACKGROUND_RED | BACKGROUND_GREEN,
  57. BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE,
  58. BACKGROUND_INTENSITY,
  59. BACKGROUND_INTENSITY | BACKGROUND_BLUE,
  60. BACKGROUND_INTENSITY | BACKGROUND_GREEN,
  61. BACKGROUND_INTENSITY | BACKGROUND_GREEN | BACKGROUND_BLUE,
  62. BACKGROUND_INTENSITY | BACKGROUND_RED,
  63. BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE,
  64. BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN,
  65. BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
  66. };
  67. struct driver_private
  68. {
  69. HANDLE hin, hout;
  70. HANDLE front, back;
  71. CHAR_INFO *buffer;
  72. CONSOLE_CURSOR_INFO cci;
  73. };
  74. static int win32_init_graphics(caca_t *kk)
  75. {
  76. CONSOLE_SCREEN_BUFFER_INFO csbi;
  77. COORD size;
  78. kk->drv.p = malloc(sizeof(struct driver_private));
  79. /* This call is allowed to fail in case we already have a console */
  80. AllocConsole();
  81. kk->drv.p->hin = GetStdHandle(STD_INPUT_HANDLE);
  82. kk->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(kk->drv.p->hout == INVALID_HANDLE_VALUE)
  86. return -1;
  87. GetConsoleCursorInfo(kk->drv.p->hout, &kk->drv.p->cci);
  88. kk->drv.p->cci.bVisible = FALSE;
  89. SetConsoleCursorInfo(kk->drv.p->hout, &kk->drv.p->cci);
  90. SetConsoleMode(kk->drv.p->hout, ENABLE_MOUSE_INPUT);
  91. kk->drv.p->front =
  92. CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
  93. 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
  94. if(!kk->drv.p->front || kk->drv.p->front == INVALID_HANDLE_VALUE)
  95. return -1;
  96. kk->drv.p->back =
  97. CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
  98. 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
  99. if(!kk->drv.p->back || kk->drv.p->back == INVALID_HANDLE_VALUE)
  100. return -1;
  101. if(!GetConsoleScreenBufferInfo(kk->drv.p->hout, &csbi))
  102. return -1;
  103. /* Sample code to get the biggest possible window */
  104. //size = GetLargestConsoleWindowSize(kk->drv.p->hout);
  105. cucul_set_size(kk->qq, csbi.srWindow.Right - csbi.srWindow.Left + 1,
  106. csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
  107. size.X = kk->qq->width;
  108. size.Y = kk->qq->height;
  109. SetConsoleScreenBufferSize(kk->drv.p->front, size);
  110. SetConsoleScreenBufferSize(kk->drv.p->back, size);
  111. SetConsoleMode(kk->drv.p->front, 0);
  112. SetConsoleMode(kk->drv.p->back, 0);
  113. GetConsoleCursorInfo(kk->drv.p->front, &kk->drv.p->cci);
  114. kk->drv.p->cci.dwSize = 0;
  115. kk->drv.p->cci.bVisible = FALSE;
  116. SetConsoleCursorInfo(kk->drv.p->front, &kk->drv.p->cci);
  117. SetConsoleCursorInfo(kk->drv.p->back, &kk->drv.p->cci);
  118. SetConsoleActiveScreenBuffer(kk->drv.p->front);
  119. kk->drv.p->buffer = malloc(kk->qq->width * kk->qq->height
  120. * sizeof(CHAR_INFO));
  121. if(kk->drv.p->buffer == NULL)
  122. return -1;
  123. return 0;
  124. }
  125. static int win32_end_graphics(caca_t *kk)
  126. {
  127. SetConsoleActiveScreenBuffer(kk->drv.p->hout);
  128. CloseHandle(kk->drv.p->back);
  129. CloseHandle(kk->drv.p->front);
  130. SetConsoleTextAttribute(kk->drv.p->hout, FOREGROUND_INTENSITY
  131. | FOREGROUND_RED
  132. | FOREGROUND_GREEN
  133. | FOREGROUND_BLUE);
  134. kk->drv.p->cci.bVisible = TRUE;
  135. SetConsoleCursorInfo(kk->drv.p->hout, &kk->drv.p->cci);
  136. CloseHandle(kk->drv.p->hout);
  137. free(kk->drv.p);
  138. return 0;
  139. }
  140. static int win32_set_window_title(caca_t *kk, char const *title)
  141. {
  142. SetConsoleTitle(title);
  143. return 0;
  144. }
  145. static unsigned int win32_get_window_width(caca_t *kk)
  146. {
  147. /* FIXME */
  148. /* Fallback to a 6x10 font */
  149. return kk->qq->width * 6;
  150. }
  151. static unsigned int win32_get_window_height(caca_t *kk)
  152. {
  153. /* FIXME */
  154. /* Fallback to a 6x10 font */
  155. return kk->qq->height * 10;
  156. }
  157. static void win32_display(caca_t *kk)
  158. {
  159. COORD size, pos;
  160. SMALL_RECT rect;
  161. unsigned int i;
  162. /* Render everything to our back buffer */
  163. for(i = 0; i < kk->qq->width * kk->qq->height; i++)
  164. {
  165. uint32_t c = kk->qq->chars[i];
  166. if(c > 0x00000020 && c < 0x00000080)
  167. kk->drv.p->buffer[i].Char.AsciiChar = (char)c;
  168. else
  169. kk->drv.p->buffer[i].Char.AsciiChar = ' ';
  170. kk->drv.p->buffer[i].Attributes =
  171. win32_fg_palette[kk->qq->attr[i] & 0xf]
  172. | win32_bg_palette[kk->qq->attr[i] >> 4];
  173. }
  174. /* Blit the back buffer to the front buffer */
  175. size.X = kk->qq->width;
  176. size.Y = kk->qq->height;
  177. pos.X = pos.Y = 0;
  178. rect.Left = rect.Top = 0;
  179. rect.Right = kk->qq->width - 1;
  180. rect.Bottom = kk->qq->height - 1;
  181. WriteConsoleOutput(kk->drv.p->front, kk->drv.p->buffer, size, pos, &rect);
  182. }
  183. static void win32_handle_resize(caca_t *kk)
  184. {
  185. /* FIXME: I don't know what to do here. */
  186. kk->resize.w = kk->qq->width;
  187. kk->resize.h = kk->qq->height;
  188. }
  189. static unsigned int win32_get_event(caca_t *kk)
  190. {
  191. INPUT_RECORD rec;
  192. DWORD num;
  193. for( ; ; )
  194. {
  195. GetNumberOfConsoleInputEvents(kk->drv.p->hin, &num);
  196. if(num == 0)
  197. break;
  198. ReadConsoleInput(kk->drv.p->hin, &rec, 1, &num);
  199. if(rec.EventType == KEY_EVENT)
  200. {
  201. unsigned int event;
  202. if(rec.Event.KeyEvent.bKeyDown)
  203. event = CACA_EVENT_KEY_PRESS;
  204. else
  205. event = CACA_EVENT_KEY_RELEASE;
  206. if(rec.Event.KeyEvent.uChar.AsciiChar)
  207. return event | rec.Event.KeyEvent.uChar.AsciiChar;
  208. }
  209. if(rec.EventType == MOUSE_EVENT)
  210. {
  211. if(rec.Event.MouseEvent.dwEventFlags == 0)
  212. {
  213. if(rec.Event.MouseEvent.dwButtonState & 0x01)
  214. return CACA_EVENT_MOUSE_PRESS | 0x000001;
  215. if(rec.Event.MouseEvent.dwButtonState & 0x02)
  216. return CACA_EVENT_MOUSE_PRESS | 0x000002;
  217. }
  218. else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_MOVED)
  219. {
  220. COORD pos = rec.Event.MouseEvent.dwMousePosition;
  221. if(kk->mouse.x == (unsigned int)pos.X &&
  222. kk->mouse.y == (unsigned int)pos.Y)
  223. continue;
  224. kk->mouse.x = pos.X;
  225. kk->mouse.y = pos.Y;
  226. return CACA_EVENT_MOUSE_MOTION
  227. | (kk->mouse.x << 12) | kk->mouse.y;
  228. }
  229. #if 0
  230. else if(rec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)
  231. {
  232. cout << rec.Event.MouseEvent.dwMousePosition.X << "," <<
  233. rec.Event.MouseEvent.dwMousePosition.Y << " " << flush;
  234. }
  235. else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_WHEELED)
  236. {
  237. SetConsoleCursorPosition(hOut,
  238. WheelWhere);
  239. if(rec.Event.MouseEvent.dwButtonState & 0xFF000000)
  240. cout << "Down" << flush;
  241. else
  242. cout << "Up " << flush;
  243. }
  244. #endif
  245. }
  246. /* Unknown event */
  247. return CACA_EVENT_NONE;
  248. }
  249. /* No event */
  250. return CACA_EVENT_NONE;
  251. }
  252. /*
  253. * Driver initialisation
  254. */
  255. void win32_init_driver(caca_t *kk)
  256. {
  257. kk->drv.driver = CACA_DRIVER_WIN32;
  258. kk->drv.init_graphics = win32_init_graphics;
  259. kk->drv.end_graphics = win32_end_graphics;
  260. kk->drv.set_window_title = win32_set_window_title;
  261. kk->drv.get_window_width = win32_get_window_width;
  262. kk->drv.get_window_height = win32_get_window_height;
  263. kk->drv.display = win32_display;
  264. kk->drv.handle_resize = win32_handle_resize;
  265. kk->drv.get_event = win32_get_event;
  266. }
  267. #endif /* USE_WIN32 */