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

Last change on this file since 672 was 672, checked in by Sam Hocevar, 15 years ago
  • Slightly changed header comments.
  • Property svn:keywords set to Id
File size: 9.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 *  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 672 2006-03-22 21:15:57Z 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 unsigned int win32_get_event(caca_t *kk)
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                event = CACA_EVENT_KEY_PRESS;
256            else
257                event = CACA_EVENT_KEY_RELEASE;
258
259            if(rec.Event.KeyEvent.uChar.AsciiChar)
260                return event | rec.Event.KeyEvent.uChar.AsciiChar;
261        }
262
263        if(rec.EventType == MOUSE_EVENT)
264        {
265            if(rec.Event.MouseEvent.dwEventFlags == 0)
266            {
267                if(rec.Event.MouseEvent.dwButtonState & 0x01)
268                    return CACA_EVENT_MOUSE_PRESS | 0x000001;
269
270                if(rec.Event.MouseEvent.dwButtonState & 0x02)
271                    return CACA_EVENT_MOUSE_PRESS | 0x000002;
272            }
273            else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_MOVED)
274            {
275                COORD pos = rec.Event.MouseEvent.dwMousePosition;
276
277                if(kk->mouse.x == (unsigned int)pos.X &&
278                   kk->mouse.y == (unsigned int)pos.Y)
279                    continue;
280
281                kk->mouse.x = pos.X;
282                kk->mouse.y = pos.Y;
283
284                return CACA_EVENT_MOUSE_MOTION
285                        | (kk->mouse.x << 12) | kk->mouse.y;
286            }
287#if 0
288            else if(rec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)
289            {
290                cout << rec.Event.MouseEvent.dwMousePosition.X << "," <<
291                        rec.Event.MouseEvent.dwMousePosition.Y << "  " << flush;
292            }
293            else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_WHEELED)
294            {
295                SetConsoleCursorPosition(hOut,
296                                         WheelWhere);
297                if(rec.Event.MouseEvent.dwButtonState & 0xFF000000)
298                    cout << "Down" << flush;
299                else
300                    cout << "Up  " << flush;
301            }
302#endif
303        }
304
305        /* Unknown event */
306        return CACA_EVENT_NONE;
307    }
308
309    /* No event */
310    return CACA_EVENT_NONE;
311}
312
313/*
314 * Driver initialisation
315 */
316
317void win32_init_driver(caca_t *kk)
318{
319    kk->drv.driver = CACA_DRIVER_WIN32;
320
321    kk->drv.init_graphics = win32_init_graphics;
322    kk->drv.end_graphics = win32_end_graphics;
323    kk->drv.set_window_title = win32_set_window_title;
324    kk->drv.get_window_width = win32_get_window_width;
325    kk->drv.get_window_height = win32_get_window_height;
326    kk->drv.display = win32_display;
327    kk->drv.handle_resize = win32_handle_resize;
328    kk->drv.get_event = win32_get_event;
329}
330
331#endif /* USE_WIN32 */
332
Note: See TracBrowser for help on using the repository browser.