source: libcaca/trunk/caca/driver_win32.c @ 612

Last change on this file since 612 was 612, checked in by Sam Hocevar, 14 years ago
  • UTF-8 support in the Win32 driver.
  • Property svn:keywords set to Id
File size: 9.7 KB
Line 
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
12/** \file driver_win32.c
13 *  \version \$Id: driver_win32.c 612 2006-03-15 10:02:08Z sam $
14 *  \author Sam Hocevar <sam@zoy.org>
15 *  \brief Win32 driver
16 *
17 *  This file contains the libcaca Win32 input and output driver
18 */
19
20#include "config.h"
21
22#if defined(USE_WIN32)
23
24#include <windows.h>
25
26#include <stdlib.h>
27
28#include "caca.h"
29#include "caca_internals.h"
30#include "cucul.h"
31#include "cucul_internals.h"
32
33/*
34 * Global variables
35 */
36
37static int const win32_fg_palette[] =
38{
39    0,
40    FOREGROUND_BLUE,
41    FOREGROUND_GREEN,
42    FOREGROUND_GREEN | FOREGROUND_BLUE,
43    FOREGROUND_RED,
44    FOREGROUND_RED | FOREGROUND_BLUE,
45    FOREGROUND_RED | FOREGROUND_GREEN,
46    FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
47    FOREGROUND_INTENSITY,
48    FOREGROUND_INTENSITY | FOREGROUND_BLUE,
49    FOREGROUND_INTENSITY | FOREGROUND_GREEN,
50    FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE,
51    FOREGROUND_INTENSITY | FOREGROUND_RED,
52    FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE,
53    FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN,
54    FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
55};
56
57static int const win32_bg_palette[] =
58{
59    0,
60    BACKGROUND_BLUE,
61    BACKGROUND_GREEN,
62    BACKGROUND_GREEN | BACKGROUND_BLUE,
63    BACKGROUND_RED,
64    BACKGROUND_RED | BACKGROUND_BLUE,
65    BACKGROUND_RED | BACKGROUND_GREEN,
66    BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE,
67    BACKGROUND_INTENSITY,
68    BACKGROUND_INTENSITY | BACKGROUND_BLUE,
69    BACKGROUND_INTENSITY | BACKGROUND_GREEN,
70    BACKGROUND_INTENSITY | BACKGROUND_GREEN | BACKGROUND_BLUE,
71    BACKGROUND_INTENSITY | BACKGROUND_RED,
72    BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE,
73    BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN,
74    BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE
75};
76
77struct driver_private
78{
79    HANDLE hin, hout;
80    HANDLE front, back;
81    CHAR_INFO *buffer;
82    CONSOLE_CURSOR_INFO cci;
83};
84
85static int win32_init_graphics(caca_t *kk)
86{
87    CONSOLE_SCREEN_BUFFER_INFO csbi;
88    COORD size;
89
90    kk->drv.p = malloc(sizeof(struct driver_private));
91
92    /* This call is allowed to fail in case we already have a console */
93    AllocConsole();
94
95    kk->drv.p->hin = GetStdHandle(STD_INPUT_HANDLE);
96    kk->drv.p->hout = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE,
97                                 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
98                                 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
99    if(kk->drv.p->hout == INVALID_HANDLE_VALUE)
100        return -1;
101
102    GetConsoleCursorInfo(kk->drv.p->hout, &kk->drv.p->cci);
103    kk->drv.p->cci.bVisible = FALSE;
104    SetConsoleCursorInfo(kk->drv.p->hout, &kk->drv.p->cci);
105
106    SetConsoleMode(kk->drv.p->hout, ENABLE_MOUSE_INPUT);
107
108    kk->drv.p->front =
109        CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
110                                  0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
111    if(!kk->drv.p->front || kk->drv.p->front == INVALID_HANDLE_VALUE)
112        return -1;
113
114    kk->drv.p->back =
115        CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
116                                  0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
117    if(!kk->drv.p->back || kk->drv.p->back == INVALID_HANDLE_VALUE)
118        return -1;
119
120    if(!GetConsoleScreenBufferInfo(kk->drv.p->hout, &csbi))
121        return -1;
122
123    /* Sample code to get the biggest possible window */
124    //size = GetLargestConsoleWindowSize(kk->drv.p->hout);
125    _cucul_set_size(kk->qq, csbi.srWindow.Right - csbi.srWindow.Left + 1,
126                            csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
127    size.X = kk->qq->width;
128    size.Y = kk->qq->height;
129    SetConsoleScreenBufferSize(kk->drv.p->front, size);
130    SetConsoleScreenBufferSize(kk->drv.p->back, size);
131
132    SetConsoleMode(kk->drv.p->front, 0);
133    SetConsoleMode(kk->drv.p->back, 0);
134
135    GetConsoleCursorInfo(kk->drv.p->front, &kk->drv.p->cci);
136    kk->drv.p->cci.dwSize = 0;
137    kk->drv.p->cci.bVisible = FALSE;
138    SetConsoleCursorInfo(kk->drv.p->front, &kk->drv.p->cci);
139    SetConsoleCursorInfo(kk->drv.p->back, &kk->drv.p->cci);
140
141    SetConsoleActiveScreenBuffer(kk->drv.p->front);
142
143    kk->drv.p->buffer = malloc(kk->qq->width * kk->qq->height
144                               * sizeof(CHAR_INFO));
145    if(kk->drv.p->buffer == NULL)
146        return -1;
147
148    return 0;
149}
150
151static int win32_end_graphics(caca_t *kk)
152{
153    SetConsoleActiveScreenBuffer(kk->drv.p->hout);
154    CloseHandle(kk->drv.p->back);
155    CloseHandle(kk->drv.p->front);
156
157    SetConsoleTextAttribute(kk->drv.p->hout, FOREGROUND_INTENSITY
158                                             | FOREGROUND_RED
159                                             | FOREGROUND_GREEN
160                                             | FOREGROUND_BLUE);
161    kk->drv.p->cci.bVisible = TRUE;
162    SetConsoleCursorInfo(kk->drv.p->hout, &kk->drv.p->cci);
163    CloseHandle(kk->drv.p->hout);
164
165    free(kk->drv.p);
166
167    return 0;
168}
169
170static int win32_set_window_title(caca_t *kk, char const *title)
171{
172    SetConsoleTitle(title);
173    return 0;
174}
175
176static unsigned int win32_get_window_width(caca_t *kk)
177{
178    /* FIXME */
179
180    /* Fallback to a 6x10 font */
181    return kk->qq->width * 6;
182}
183
184static unsigned int win32_get_window_height(caca_t *kk)
185{
186    /* FIXME */
187
188    /* Fallback to a 6x10 font */
189    return kk->qq->height * 10;
190}
191
192static void win32_display(caca_t *kk)
193{
194    COORD size, pos;
195    SMALL_RECT rect;
196    unsigned int i;
197
198    /* Render everything to our back buffer */
199    for(i = 0; i < kk->qq->width * kk->qq->height; i++)
200    {
201        uint32_t c = kk->qq->chars[i];
202
203#if 0
204        if(c > 0x00000020 && c < 0x00000080)
205            kk->drv.p->buffer[i].Char.AsciiChar = (uint8_t)c;
206        else
207            kk->drv.p->buffer[i].Char.AsciiChar = ' ';
208#else
209        if(c > 0x00000020 && c < 0x00010000)
210            kk->drv.p->buffer[i].Char.UnicodeChar = (uint16_t)c;
211        else
212            kk->drv.p->buffer[i].Char.UnicodeChar = (uint16_t)' ';
213#endif
214
215        kk->drv.p->buffer[i].Attributes =
216                win32_fg_palette[kk->qq->attr[i] & 0xf]
217                 | win32_bg_palette[kk->qq->attr[i] >> 4];
218    }
219
220    /* Blit the back buffer to the front buffer */
221    size.X = kk->qq->width;
222    size.Y = kk->qq->height;
223    pos.X = pos.Y = 0;
224    rect.Left = rect.Top = 0;
225    rect.Right = kk->qq->width - 1;
226    rect.Bottom = kk->qq->height - 1;
227#if 0
228    WriteConsoleOutput(kk->drv.p->front, kk->drv.p->buffer, size, pos, &rect);
229#else
230    WriteConsoleOutputW(kk->drv.p->front, kk->drv.p->buffer, size, pos, &rect);
231#endif
232}
233
234static void win32_handle_resize(caca_t *kk)
235{
236    /* FIXME: I don't know what to do here. */
237    kk->resize.w = kk->qq->width;
238    kk->resize.h = kk->qq->height;
239}
240
241static unsigned int win32_get_event(caca_t *kk)
242{
243    INPUT_RECORD rec;
244    DWORD num;
245
246    for( ; ; )
247    {
248        GetNumberOfConsoleInputEvents(kk->drv.p->hin, &num);
249        if(num == 0)
250            break;
251
252        ReadConsoleInput(kk->drv.p->hin, &rec, 1, &num);
253        if(rec.EventType == KEY_EVENT)
254        {
255            unsigned int event;
256
257            if(rec.Event.KeyEvent.bKeyDown)
258                event = CACA_EVENT_KEY_PRESS;
259            else
260                event = CACA_EVENT_KEY_RELEASE;
261
262            if(rec.Event.KeyEvent.uChar.AsciiChar)
263                return event | rec.Event.KeyEvent.uChar.AsciiChar;
264        }
265
266        if(rec.EventType == MOUSE_EVENT)
267        {
268            if(rec.Event.MouseEvent.dwEventFlags == 0)
269            {
270                if(rec.Event.MouseEvent.dwButtonState & 0x01)
271                    return CACA_EVENT_MOUSE_PRESS | 0x000001;
272
273                if(rec.Event.MouseEvent.dwButtonState & 0x02)
274                    return CACA_EVENT_MOUSE_PRESS | 0x000002;
275            }
276            else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_MOVED)
277            {
278                COORD pos = rec.Event.MouseEvent.dwMousePosition;
279
280                if(kk->mouse.x == (unsigned int)pos.X &&
281                   kk->mouse.y == (unsigned int)pos.Y)
282                    continue;
283
284                kk->mouse.x = pos.X;
285                kk->mouse.y = pos.Y;
286
287                return CACA_EVENT_MOUSE_MOTION
288                        | (kk->mouse.x << 12) | kk->mouse.y;
289            }
290#if 0
291            else if(rec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)
292            {
293                cout << rec.Event.MouseEvent.dwMousePosition.X << "," <<
294                        rec.Event.MouseEvent.dwMousePosition.Y << "  " << flush;
295            }
296            else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_WHEELED)
297            {
298                SetConsoleCursorPosition(hOut,
299                                         WheelWhere);
300                if(rec.Event.MouseEvent.dwButtonState & 0xFF000000)
301                    cout << "Down" << flush;
302                else
303                    cout << "Up  " << flush;
304            }
305#endif
306        }
307
308        /* Unknown event */
309        return CACA_EVENT_NONE;
310    }
311
312    /* No event */
313    return CACA_EVENT_NONE;
314}
315
316/*
317 * Driver initialisation
318 */
319
320void win32_init_driver(caca_t *kk)
321{
322    kk->drv.driver = CACA_DRIVER_WIN32;
323
324    kk->drv.init_graphics = win32_init_graphics;
325    kk->drv.end_graphics = win32_end_graphics;
326    kk->drv.set_window_title = win32_set_window_title;
327    kk->drv.get_window_width = win32_get_window_width;
328    kk->drv.get_window_height = win32_get_window_height;
329    kk->drv.display = win32_display;
330    kk->drv.handle_resize = win32_handle_resize;
331    kk->drv.get_event = win32_get_event;
332}
333
334#endif /* USE_WIN32 */
335
Note: See TracBrowser for help on using the repository browser.