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.

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