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

Last change on this file since 681 was 681, checked in by Sam Hocevar, 14 years ago
  • Massive rework of the event handling code, as per the TODO list.
  • Property svn:keywords set to Id
File size: 9.9 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 *  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 681 2006-03-23 18:36:59Z 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#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_t *kk)
86{
87    CONSOLE_SCREEN_BUFFER_INFO csbi;
88    SMALL_RECT rect;
89    COORD size;
90
91    kk->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    kk->drv.p->hin = GetStdHandle(STD_INPUT_HANDLE);
97    kk->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(kk->drv.p->hout == INVALID_HANDLE_VALUE)
101        return -1;
102
103    GetConsoleCursorInfo(kk->drv.p->hout, &kk->drv.p->cci);
104    kk->drv.p->cci.bVisible = FALSE;
105    SetConsoleCursorInfo(kk->drv.p->hout, &kk->drv.p->cci);
106
107    SetConsoleMode(kk->drv.p->hout, ENABLE_MOUSE_INPUT);
108
109    kk->drv.p->screen =
110        CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
111                                  0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
112    if(!kk->drv.p->screen || kk->drv.p->screen == INVALID_HANDLE_VALUE)
113        return -1;
114
115    /* Set the new console size */
116    size.X = kk->qq->width;
117    size.Y = kk->qq->height;
118    SetConsoleScreenBufferSize(kk->drv.p->screen, size);
119
120    rect.Left = rect.Top = 0;
121    rect.Right = kk->qq->width - 1;
122    rect.Bottom = kk->qq->height - 1;
123    SetConsoleWindowInfo(kk->drv.p->screen, TRUE, &rect);
124
125    /* Report our new size to libcucul */
126    if(!GetConsoleScreenBufferInfo(kk->drv.p->screen, &csbi))
127        return -1;
128
129    _cucul_set_size(kk->qq, csbi.srWindow.Right - csbi.srWindow.Left + 1,
130                            csbi.srWindow.Bottom - csbi.srWindow.Top + 1);
131
132    SetConsoleMode(kk->drv.p->screen, 0);
133
134    GetConsoleCursorInfo(kk->drv.p->screen, &kk->drv.p->cci);
135    kk->drv.p->cci.dwSize = 0;
136    kk->drv.p->cci.bVisible = FALSE;
137    SetConsoleCursorInfo(kk->drv.p->screen, &kk->drv.p->cci);
138
139    SetConsoleActiveScreenBuffer(kk->drv.p->screen);
140
141    kk->drv.p->buffer = malloc(kk->qq->width * kk->qq->height
142                               * sizeof(CHAR_INFO));
143    if(kk->drv.p->buffer == NULL)
144        return -1;
145
146    return 0;
147}
148
149static int win32_end_graphics(caca_t *kk)
150{
151    SetConsoleActiveScreenBuffer(kk->drv.p->hout);
152    CloseHandle(kk->drv.p->screen);
153
154    SetConsoleTextAttribute(kk->drv.p->hout, FOREGROUND_INTENSITY
155                                             | FOREGROUND_RED
156                                             | FOREGROUND_GREEN
157                                             | FOREGROUND_BLUE);
158    kk->drv.p->cci.bVisible = TRUE;
159    SetConsoleCursorInfo(kk->drv.p->hout, &kk->drv.p->cci);
160    CloseHandle(kk->drv.p->hout);
161
162    free(kk->drv.p);
163
164    return 0;
165}
166
167static int win32_set_window_title(caca_t *kk, char const *title)
168{
169    SetConsoleTitle(title);
170    return 0;
171}
172
173static unsigned int win32_get_window_width(caca_t *kk)
174{
175    /* FIXME */
176
177    /* Fallback to a 6x10 font */
178    return kk->qq->width * 6;
179}
180
181static unsigned int win32_get_window_height(caca_t *kk)
182{
183    /* FIXME */
184
185    /* Fallback to a 6x10 font */
186    return kk->qq->height * 10;
187}
188
189static void win32_display(caca_t *kk)
190{
191    COORD size, pos;
192    SMALL_RECT rect;
193    unsigned int i;
194
195    /* Render everything to our screen buffer */
196    for(i = 0; i < kk->qq->width * kk->qq->height; i++)
197    {
198        uint32_t c = kk->qq->chars[i];
199
200#if 0
201        if(c > 0x00000020 && c < 0x00000080)
202            kk->drv.p->buffer[i].Char.AsciiChar = (uint8_t)c;
203        else
204            kk->drv.p->buffer[i].Char.AsciiChar = ' ';
205#else
206        if(c > 0x00000020 && c < 0x00010000)
207            kk->drv.p->buffer[i].Char.UnicodeChar = (uint16_t)c;
208        else
209            kk->drv.p->buffer[i].Char.UnicodeChar = (uint16_t)' ';
210#endif
211
212        kk->drv.p->buffer[i].Attributes =
213                win32_fg_palette[kk->qq->attr[i] & 0xf]
214                 | win32_bg_palette[kk->qq->attr[i] >> 4];
215    }
216
217    /* Blit the screen buffer */
218    size.X = kk->qq->width;
219    size.Y = kk->qq->height;
220    pos.X = pos.Y = 0;
221    rect.Left = rect.Top = 0;
222    rect.Right = kk->qq->width - 1;
223    rect.Bottom = kk->qq->height - 1;
224#if 0
225    WriteConsoleOutput(kk->drv.p->screen, kk->drv.p->buffer, size, pos, &rect);
226#else
227    WriteConsoleOutputW(kk->drv.p->screen, kk->drv.p->buffer, size, pos, &rect);
228#endif
229}
230
231static void win32_handle_resize(caca_t *kk)
232{
233    /* FIXME: I don't know what to do here. */
234    kk->resize.w = kk->qq->width;
235    kk->resize.h = kk->qq->height;
236}
237
238static int win32_get_event(caca_t *kk, struct caca_event *ev)
239{
240    INPUT_RECORD rec;
241    DWORD num;
242
243    for( ; ; )
244    {
245        GetNumberOfConsoleInputEvents(kk->drv.p->hin, &num);
246        if(num == 0)
247            break;
248
249        ReadConsoleInput(kk->drv.p->hin, &rec, 1, &num);
250        if(rec.EventType == KEY_EVENT)
251        {
252            unsigned int event;
253
254            if(rec.Event.KeyEvent.bKeyDown)
255                ev->type = CACA_EVENT_KEY_PRESS;
256            else
257                ev->type = CACA_EVENT_KEY_RELEASE;
258
259            if(rec.Event.KeyEvent.uChar.AsciiChar)
260            {
261                ev->data.key.c = rec.Event.KeyEvent.uChar.AsciiChar;
262                ev->data.key.ucs4 = (uint32_t)ev->data.key.c;
263                ev->data.key.utf8[0] = ev->data.key.c;
264                ev->data.key.utf8[1] = '\0';
265
266                return 1;
267            }
268        }
269
270        if(rec.EventType == MOUSE_EVENT)
271        {
272            if(rec.Event.MouseEvent.dwEventFlags == 0)
273            {
274                if(rec.Event.MouseEvent.dwButtonState & 0x01)
275                {
276                    ev->type = CACA_EVENT_MOUSE_PRESS;
277                    ev->data.mouse.button = 1;
278                    return 1;
279                }
280
281                if(rec.Event.MouseEvent.dwButtonState & 0x02)
282                {
283                    ev->type = CACA_EVENT_MOUSE_PRESS;
284                    ev->data.mouse.button = 2;
285                    return 1;
286                }
287            }
288            else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_MOVED)
289            {
290                COORD pos = rec.Event.MouseEvent.dwMousePosition;
291
292                if(kk->mouse.x == (unsigned int)pos.X &&
293                   kk->mouse.y == (unsigned int)pos.Y)
294                    continue;
295
296                kk->mouse.x = pos.X;
297                kk->mouse.y = pos.Y;
298
299                ev->type = CACA_EVENT_MOUSE_MOTION;
300                ev->data.mouse.x = kk->mouse.x;
301                ev->data.mouse.y = kk->mouse.y;
302                return 1;
303            }
304#if 0
305            else if(rec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)
306            {
307                cout << rec.Event.MouseEvent.dwMousePosition.X << "," <<
308                        rec.Event.MouseEvent.dwMousePosition.Y << "  " << flush;
309            }
310            else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_WHEELED)
311            {
312                SetConsoleCursorPosition(hOut,
313                                         WheelWhere);
314                if(rec.Event.MouseEvent.dwButtonState & 0xFF000000)
315                    cout << "Down" << flush;
316                else
317                    cout << "Up  " << flush;
318            }
319#endif
320        }
321
322        /* Unknown event */
323        ev->type = CACA_EVENT_NONE;
324        return 0;
325    }
326
327    /* No event */
328    ev->type = CACA_EVENT_NONE;
329    return 0;
330}
331
332/*
333 * Driver initialisation
334 */
335
336void win32_init_driver(caca_t *kk)
337{
338    kk->drv.driver = CACA_DRIVER_WIN32;
339
340    kk->drv.init_graphics = win32_init_graphics;
341    kk->drv.end_graphics = win32_end_graphics;
342    kk->drv.set_window_title = win32_set_window_title;
343    kk->drv.get_window_width = win32_get_window_width;
344    kk->drv.get_window_height = win32_get_window_height;
345    kk->drv.display = win32_display;
346    kk->drv.handle_resize = win32_handle_resize;
347    kk->drv.get_event = win32_get_event;
348}
349
350#endif /* USE_WIN32 */
351
Note: See TracBrowser for help on using the repository browser.