1 | /* |
---|
2 | * libcaca Colour ASCII-Art library |
---|
3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: caca.c 1362 2006-11-12 15:26:13Z 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 main functions used by \e libcaca applications to |
---|
16 | * initialise the library, get the screen properties, set the framerate and |
---|
17 | * so on. |
---|
18 | */ |
---|
19 | |
---|
20 | #include "config.h" |
---|
21 | #include "common.h" |
---|
22 | |
---|
23 | #if !defined(__KERNEL__) |
---|
24 | # include <stdlib.h> |
---|
25 | # include <string.h> |
---|
26 | # include <stdio.h> |
---|
27 | # if defined(USE_PLUGINS) |
---|
28 | # if defined(HAVE_DLFCN_H) |
---|
29 | # include <dlfcn.h> |
---|
30 | # endif |
---|
31 | # endif |
---|
32 | #endif |
---|
33 | |
---|
34 | #include "cucul.h" |
---|
35 | #include "cucul_internals.h" |
---|
36 | #include "caca.h" |
---|
37 | #include "caca_internals.h" |
---|
38 | |
---|
39 | #if defined(USE_PLUGINS) |
---|
40 | # define gl_install(p) caca_plugin_install("gl", p) |
---|
41 | # define x11_install(p) caca_plugin_install("x11", p) |
---|
42 | #endif |
---|
43 | |
---|
44 | static int caca_select_driver(caca_display_t *); |
---|
45 | #if defined(USE_PLUGINS) |
---|
46 | static int caca_plugin_install(char const *, caca_display_t *); |
---|
47 | #endif |
---|
48 | |
---|
49 | /** \brief Attach a caca graphical context to a cucul canvas. |
---|
50 | * |
---|
51 | * Create a graphical context using device-dependent features (ncurses for |
---|
52 | * terminals, an X11 window, a DOS command window...) that attaches to a |
---|
53 | * libcucul canvas. Everything that gets drawn in the libcucul canvas can |
---|
54 | * then be displayed by the libcaca driver. |
---|
55 | * |
---|
56 | * If an error occurs, NULL is returned and \b errno is set accordingly: |
---|
57 | * - \c ENOMEM Not enough memory. |
---|
58 | * - \c ENODEV Graphical device could not be initialised. |
---|
59 | * |
---|
60 | * \param cv The cucul cavas. |
---|
61 | * \return The caca graphical context or NULL if an error occurred. |
---|
62 | */ |
---|
63 | caca_display_t * caca_create_display(cucul_canvas_t * cv) |
---|
64 | { |
---|
65 | caca_display_t *dp = malloc(sizeof(caca_display_t)); |
---|
66 | |
---|
67 | if(!dp) |
---|
68 | { |
---|
69 | seterrno(ENOMEM); |
---|
70 | return NULL; |
---|
71 | } |
---|
72 | |
---|
73 | dp->cv = cv; |
---|
74 | #if defined(USE_PLUGINS) |
---|
75 | dp->plugin = NULL; |
---|
76 | #endif |
---|
77 | |
---|
78 | if(caca_select_driver(dp)) |
---|
79 | { |
---|
80 | #if defined(USE_PLUGINS) |
---|
81 | if(dp->plugin) |
---|
82 | dlclose(dp->plugin); |
---|
83 | #endif |
---|
84 | free(dp); |
---|
85 | seterrno(ENODEV); |
---|
86 | return NULL; |
---|
87 | } |
---|
88 | |
---|
89 | if(dp->drv.init_graphics(dp)) |
---|
90 | { |
---|
91 | #if defined(USE_PLUGINS) |
---|
92 | if(dp->plugin) |
---|
93 | dlclose(dp->plugin); |
---|
94 | #endif |
---|
95 | free(dp); |
---|
96 | seterrno(ENODEV); |
---|
97 | return NULL; |
---|
98 | } |
---|
99 | |
---|
100 | /* Attached! */ |
---|
101 | dp->cv->refcount++; |
---|
102 | |
---|
103 | /* Graphics stuff */ |
---|
104 | dp->delay = 0; |
---|
105 | dp->rendertime = 0; |
---|
106 | |
---|
107 | /* Events stuff */ |
---|
108 | #if defined(USE_SLANG) || defined(USE_NCURSES) |
---|
109 | dp->events.key_timer.last_sec = 0; |
---|
110 | dp->events.key_timer.last_usec = 0; |
---|
111 | dp->events.last_key_ticks = 0; |
---|
112 | dp->events.autorepeat_ticks = 0; |
---|
113 | dp->events.last_key_event.type = CACA_EVENT_NONE; |
---|
114 | #endif |
---|
115 | #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL) |
---|
116 | dp->events.queue = 0; |
---|
117 | #endif |
---|
118 | |
---|
119 | dp->timer.last_sec = 0; |
---|
120 | dp->timer.last_usec = 0; |
---|
121 | dp->lastticks = 0; |
---|
122 | |
---|
123 | /* Mouse position */ |
---|
124 | dp->mouse.x = dp->cv->width / 2; |
---|
125 | dp->mouse.y = dp->cv->height / 2; |
---|
126 | |
---|
127 | /* Resize events */ |
---|
128 | dp->resize.resized = 0; |
---|
129 | |
---|
130 | return dp; |
---|
131 | } |
---|
132 | |
---|
133 | /** \brief Detach a caca graphical context from a cucul backend context. |
---|
134 | * |
---|
135 | * Detach a graphical context from its cucul backend and destroy it. The |
---|
136 | * libcucul canvas continues to exist and other graphical contexts can be |
---|
137 | * attached to it afterwards. |
---|
138 | * |
---|
139 | * This function never fails. |
---|
140 | * |
---|
141 | * \param dp The libcaca graphical context. |
---|
142 | * \return This function always returns 0. |
---|
143 | */ |
---|
144 | int caca_free_display(caca_display_t *dp) |
---|
145 | { |
---|
146 | dp->drv.end_graphics(dp); |
---|
147 | #if defined(USE_PLUGINS) |
---|
148 | if(dp->plugin) |
---|
149 | dlclose(dp->plugin); |
---|
150 | #endif |
---|
151 | dp->cv->refcount--; |
---|
152 | free(dp); |
---|
153 | |
---|
154 | return 0; |
---|
155 | } |
---|
156 | |
---|
157 | /* |
---|
158 | * XXX: The following functions are local. |
---|
159 | */ |
---|
160 | |
---|
161 | static int caca_select_driver(caca_display_t *dp) |
---|
162 | { |
---|
163 | #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP) |
---|
164 | char *var = getenv("CACA_DRIVER"); |
---|
165 | |
---|
166 | /* If the environment variable was set, use it */ |
---|
167 | if(var && *var) |
---|
168 | { |
---|
169 | #if defined(USE_WIN32) |
---|
170 | if(!strcasecmp(var, "win32")) return win32_install(dp); |
---|
171 | #endif |
---|
172 | #if defined(USE_CONIO) |
---|
173 | if(!strcasecmp(var, "conio")) return conio_install(dp); |
---|
174 | #endif |
---|
175 | #if defined(USE_X11) |
---|
176 | if(!strcasecmp(var, "x11")) return x11_install(dp); |
---|
177 | #endif |
---|
178 | #if defined(USE_GL) |
---|
179 | if(!strcasecmp(var, "gl")) return gl_install(dp); |
---|
180 | #endif |
---|
181 | #if !defined(__KERNEL__) |
---|
182 | if(!strcasecmp(var, "raw")) return raw_install(dp); |
---|
183 | #endif |
---|
184 | #if defined(USE_SLANG) |
---|
185 | if(!strcasecmp(var, "slang")) return slang_install(dp); |
---|
186 | #endif |
---|
187 | #if defined(USE_NCURSES) |
---|
188 | if(!strcasecmp(var, "ncurses")) return ncurses_install(dp); |
---|
189 | #endif |
---|
190 | #if defined(USE_VGA) |
---|
191 | if(!strcasecmp(var, "vga")) return vga_install(dp); |
---|
192 | #endif |
---|
193 | return -1; |
---|
194 | } |
---|
195 | #endif |
---|
196 | |
---|
197 | #if defined(USE_WIN32) |
---|
198 | if(win32_install(dp) == 0) return 0; |
---|
199 | #endif |
---|
200 | #if defined(USE_CONIO) |
---|
201 | if(conio_install(dp) == 0) return 0; |
---|
202 | #endif |
---|
203 | #if defined(USE_VGA) |
---|
204 | if(vga_install(dp) == 0) return 0; |
---|
205 | #endif |
---|
206 | #if defined(USE_X11) |
---|
207 | if(x11_install(dp) == 0) return 0; |
---|
208 | #endif |
---|
209 | #if defined(USE_GL) |
---|
210 | if(gl_install(dp) == 0) return 0; |
---|
211 | #endif |
---|
212 | /* ncurses has a higher priority than slang because it has better colour |
---|
213 | * support across terminal types, despite being slightly slower. */ |
---|
214 | #if defined(USE_NCURSES) |
---|
215 | if(ncurses_install(dp) == 0) return 0; |
---|
216 | #endif |
---|
217 | #if defined(USE_SLANG) |
---|
218 | if(slang_install(dp) == 0) return 0; |
---|
219 | #endif |
---|
220 | |
---|
221 | return -1; |
---|
222 | } |
---|
223 | |
---|
224 | #if defined(USE_PLUGINS) |
---|
225 | static int caca_plugin_install(char const *name, caca_display_t *dp) |
---|
226 | { |
---|
227 | char buf[512]; |
---|
228 | int (*sym) (caca_display_t *); |
---|
229 | |
---|
230 | sprintf(buf, "%s/lib%s_plugin.so", PLUGINDIR, name); |
---|
231 | dp->plugin = dlopen(buf, RTLD_NOW); |
---|
232 | if(!dp->plugin) |
---|
233 | { |
---|
234 | sprintf(buf, "lib%s_plugin.so", name); |
---|
235 | dp->plugin = dlopen(buf, RTLD_NOW); |
---|
236 | if(!dp->plugin) |
---|
237 | return -1; |
---|
238 | } |
---|
239 | |
---|
240 | sprintf(buf, "%s_install", name); |
---|
241 | sym = dlsym(dp->plugin, buf); |
---|
242 | if(!sym) |
---|
243 | { |
---|
244 | dlclose(dp->plugin); |
---|
245 | return -1; |
---|
246 | } |
---|
247 | |
---|
248 | return sym(dp); |
---|
249 | } |
---|
250 | #endif |
---|
251 | |
---|