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_conio.c 859 2006-04-24 20:35:59Z sam $ |
---|
7 | * |
---|
8 | * This library is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the Do What The Fuck You Want To |
---|
10 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
12 | */ |
---|
13 | |
---|
14 | /* |
---|
15 | * This file contains the libcaca DOS/conio.h input and output driver |
---|
16 | */ |
---|
17 | |
---|
18 | #include "config.h" |
---|
19 | #include "common.h" |
---|
20 | |
---|
21 | #if defined(USE_CONIO) |
---|
22 | |
---|
23 | #include <dos.h> |
---|
24 | #include <conio.h> |
---|
25 | #if defined(SCREENUPDATE_IN_PC_H) |
---|
26 | # include <pc.h> |
---|
27 | #endif |
---|
28 | |
---|
29 | #include <stdlib.h> |
---|
30 | |
---|
31 | #include "caca.h" |
---|
32 | #include "caca_internals.h" |
---|
33 | #include "cucul.h" |
---|
34 | #include "cucul_internals.h" |
---|
35 | |
---|
36 | struct driver_private |
---|
37 | { |
---|
38 | struct text_info ti; |
---|
39 | char *screen; |
---|
40 | }; |
---|
41 | |
---|
42 | static int conio_init_graphics(caca_display_t *dp) |
---|
43 | { |
---|
44 | dp->drv.p = malloc(sizeof(struct driver_private)); |
---|
45 | |
---|
46 | _wscroll = 0; |
---|
47 | _setcursortype(_NOCURSOR); |
---|
48 | clrscr(); |
---|
49 | |
---|
50 | gettextinfo(&dp->drv.p->ti); |
---|
51 | dp->drv.p->screen = malloc(2 * dp->drv.p->ti.screenwidth |
---|
52 | * dp->drv.p->ti.screenheight * sizeof(char)); |
---|
53 | if(dp->drv.p->screen == NULL) |
---|
54 | return -1; |
---|
55 | # if defined(SCREENUPDATE_IN_PC_H) |
---|
56 | ScreenRetrieve(dp->drv.p->screen); |
---|
57 | # else |
---|
58 | /* FIXME */ |
---|
59 | # endif |
---|
60 | _cucul_set_canvas_size(dp->cv, dp->drv.p->ti.screenwidth, |
---|
61 | dp->drv.p->ti.screenheight); |
---|
62 | return 0; |
---|
63 | } |
---|
64 | |
---|
65 | static int conio_end_graphics(caca_display_t *dp) |
---|
66 | { |
---|
67 | _wscroll = 1; |
---|
68 | textcolor((enum COLORS)WHITE); |
---|
69 | textbackground((enum COLORS)BLACK); |
---|
70 | gotoxy(dp->cv->width, dp->cv->height); |
---|
71 | cputs("\r\n"); |
---|
72 | _setcursortype(_NORMALCURSOR); |
---|
73 | |
---|
74 | free(dp->drv.p->screen); |
---|
75 | free(dp->drv.p); |
---|
76 | |
---|
77 | return 0; |
---|
78 | } |
---|
79 | |
---|
80 | static int conio_set_display_title(caca_display_t *dp, char const *title) |
---|
81 | { |
---|
82 | return 0; |
---|
83 | } |
---|
84 | |
---|
85 | static unsigned int conio_get_display_width(caca_display_t *dp) |
---|
86 | { |
---|
87 | /* Fallback to a 6x10 font */ |
---|
88 | return dp->cv->width * 6; |
---|
89 | } |
---|
90 | |
---|
91 | static unsigned int conio_get_display_height(caca_display_t *dp) |
---|
92 | { |
---|
93 | /* Fallback to a 6x10 font */ |
---|
94 | return dp->cv->height * 10; |
---|
95 | } |
---|
96 | |
---|
97 | static void conio_display(caca_display_t *dp) |
---|
98 | { |
---|
99 | char *screen = dp->drv.p->screen; |
---|
100 | uint32_t *attr = dp->cv->attr; |
---|
101 | uint32_t *chars = dp->cv->chars; |
---|
102 | int n; |
---|
103 | |
---|
104 | for(n = dp->cv->height * dp->cv->width; n--; ) |
---|
105 | { |
---|
106 | *screen++ = _cucul_utf32_to_cp437(*chars++); |
---|
107 | *screen++ = _cucul_argb32_to_ansi8(*attr++); |
---|
108 | } |
---|
109 | # if defined(SCREENUPDATE_IN_PC_H) |
---|
110 | ScreenUpdate(dp->drv.p->screen); |
---|
111 | # else |
---|
112 | /* FIXME */ |
---|
113 | # endif |
---|
114 | } |
---|
115 | |
---|
116 | static void conio_handle_resize(caca_display_t *dp) |
---|
117 | { |
---|
118 | /* We know nothing about our window */ |
---|
119 | dp->resize.w = dp->cv->width; |
---|
120 | dp->resize.h = dp->cv->height; |
---|
121 | } |
---|
122 | |
---|
123 | static int conio_get_event(caca_display_t *dp, caca_event_t *ev) |
---|
124 | { |
---|
125 | unsigned char ch; |
---|
126 | caca_event_t release; |
---|
127 | |
---|
128 | if(!_conio_kbhit()) |
---|
129 | { |
---|
130 | ev->type = CACA_EVENT_NONE; |
---|
131 | return 0; |
---|
132 | } |
---|
133 | |
---|
134 | ch = getch(); |
---|
135 | |
---|
136 | ev->type = CACA_EVENT_KEY_PRESS; |
---|
137 | ev->data.key.ch = ch; |
---|
138 | ev->data.key.ucs4 = (uint32_t)ch; |
---|
139 | ev->data.key.utf8[0] = ch; |
---|
140 | ev->data.key.utf8[1] = '\0'; |
---|
141 | |
---|
142 | release = *ev; |
---|
143 | release.type = CACA_EVENT_KEY_RELEASE; |
---|
144 | _push_event(dp, &release); |
---|
145 | |
---|
146 | return 1; |
---|
147 | } |
---|
148 | |
---|
149 | /* |
---|
150 | * Driver initialisation |
---|
151 | */ |
---|
152 | |
---|
153 | int conio_install(caca_display_t *dp) |
---|
154 | { |
---|
155 | dp->drv.driver = CACA_DRIVER_CONIO; |
---|
156 | |
---|
157 | dp->drv.init_graphics = conio_init_graphics; |
---|
158 | dp->drv.end_graphics = conio_end_graphics; |
---|
159 | dp->drv.set_display_title = conio_set_display_title; |
---|
160 | dp->drv.get_display_width = conio_get_display_width; |
---|
161 | dp->drv.get_display_height = conio_get_display_height; |
---|
162 | dp->drv.display = conio_display; |
---|
163 | dp->drv.handle_resize = conio_handle_resize; |
---|
164 | dp->drv.get_event = conio_get_event; |
---|
165 | dp->drv.set_mouse = NULL; |
---|
166 | |
---|
167 | return 0; |
---|
168 | } |
---|
169 | |
---|
170 | #endif /* USE_CONIO */ |
---|
171 | |
---|