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

Last change on this file since 550 was 550, checked in by Sam Hocevar, 14 years ago
  • Finished moving everything driver-specific to the apropriate driver_*.c files. Phew. Now they just need a few comments.
  • Property svn:keywords set to Id
File size: 9.3 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 550 2006-03-08 10:33:01Z 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        kk->drv.p->buffer[i].Char.AsciiChar = kk->qq->chars[i] & 0x7f;
202        kk->drv.p->buffer[i].Attributes =
203                win32_fg_palette[kk->qq->attr[i] & 0xf]
204                 | win32_bg_palette[kk->qq->attr[i] >> 4];
205    }
206
207    /* Blit the back buffer to the front buffer */
208    size.X = kk->qq->width;
209    size.Y = kk->qq->height;
210    pos.X = pos.Y = 0;
211    rect.Left = rect.Top = 0;
212    rect.Right = kk->qq->width - 1;
213    rect.Bottom = kk->qq->height - 1;
214    WriteConsoleOutput(kk->drv.p->front, kk->drv.p->buffer, size, pos, &rect);
215}
216
217static void win32_handle_resize(caca_t *kk, unsigned int *new_width,
218                                            unsigned int *new_height)
219{
220    /* Nothing to do here. */
221    *new_width = kk->qq->width;
222    *new_height = kk->qq->height;
223}
224
225static unsigned int win32_get_event(caca_t *kk)
226{
227    INPUT_RECORD rec;
228    DWORD num;
229
230    for( ; ; )
231    {
232        GetNumberOfConsoleInputEvents(kk->drv.p->hin, &num);
233        if(num == 0)
234            break;
235
236        ReadConsoleInput(kk->drv.p->hin, &rec, 1, &num);
237        if(rec.EventType == KEY_EVENT)
238        {
239            unsigned int event;
240
241            if(rec.Event.KeyEvent.bKeyDown)
242                event = CACA_EVENT_KEY_PRESS;
243            else
244                event = CACA_EVENT_KEY_RELEASE;
245
246            if(rec.Event.KeyEvent.uChar.AsciiChar)
247                return event | rec.Event.KeyEvent.uChar.AsciiChar;
248        }
249
250        if(rec.EventType == MOUSE_EVENT)
251        {
252            if(rec.Event.MouseEvent.dwEventFlags == 0)
253            {
254                if(rec.Event.MouseEvent.dwButtonState & 0x01)
255                    return CACA_EVENT_MOUSE_PRESS | 0x000001;
256
257                if(rec.Event.MouseEvent.dwButtonState & 0x02)
258                    return CACA_EVENT_MOUSE_PRESS | 0x000002;
259            }
260            else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_MOVED)
261            {
262                COORD pos = rec.Event.MouseEvent.dwMousePosition;
263
264                if(kk->mouse_x == (unsigned int)pos.X &&
265                   kk->mouse_y == (unsigned int)pos.Y)
266                    continue;
267
268                kk->mouse_x = pos.X;
269                kk->mouse_y = pos.Y;
270
271                return CACA_EVENT_MOUSE_MOTION | (kk->mouse_x << 12) | kk->mouse_y;
272            }
273#if 0
274            else if(rec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)
275            {
276                cout << rec.Event.MouseEvent.dwMousePosition.X << "," <<
277                        rec.Event.MouseEvent.dwMousePosition.Y << "  " << flush;
278            }
279            else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_WHEELED)
280            {
281                SetConsoleCursorPosition(hOut,
282                                         WheelWhere);
283                if(rec.Event.MouseEvent.dwButtonState & 0xFF000000)
284                    cout << "Down" << flush;
285                else
286                    cout << "Up  " << flush;
287            }
288#endif
289        }
290
291        /* Unknown event */
292        return CACA_EVENT_NONE;
293    }
294
295    /* No event */
296    return CACA_EVENT_NONE;
297}
298
299/*
300 * Driver initialisation
301 */
302
303void win32_init_driver(caca_t *kk)
304{
305    kk->drv.driver = CACA_DRIVER_WIN32;
306
307    kk->drv.init_graphics = win32_init_graphics;
308    kk->drv.end_graphics = win32_end_graphics;
309    kk->drv.set_window_title = win32_set_window_title;
310    kk->drv.get_window_width = win32_get_window_width;
311    kk->drv.get_window_height = win32_get_window_height;
312    kk->drv.display = win32_display;
313    kk->drv.handle_resize = win32_handle_resize;
314    kk->drv.get_event = win32_get_event;
315}
316
317#endif /* USE_WIN32 */
318
Note: See TracBrowser for help on using the repository browser.