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

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