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 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 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 | #endif |
---|
27 | |
---|
28 | #include "cucul.h" |
---|
29 | #include "cucul_internals.h" |
---|
30 | #include "caca.h" |
---|
31 | #include "caca_internals.h" |
---|
32 | |
---|
33 | static int caca_init_driver(caca_display_t *dp); |
---|
34 | |
---|
35 | /** \brief Attach a caca graphical context to a cucul canvas. |
---|
36 | * |
---|
37 | * Create a graphical context using device-dependent features (ncurses for |
---|
38 | * terminals, an X11 window, a DOS command window...) that attaches to a |
---|
39 | * libcucul canvas. Everything that gets drawn in the libcucul canvas can |
---|
40 | * then be displayed by the libcaca driver. |
---|
41 | * |
---|
42 | * \param cv The cucul cavas. |
---|
43 | * \return The caca graphical context or NULL if an error occurred. |
---|
44 | */ |
---|
45 | caca_display_t * caca_create_display(cucul_canvas_t * cv) |
---|
46 | { |
---|
47 | caca_display_t *dp = malloc(sizeof(caca_display_t)); |
---|
48 | |
---|
49 | dp->cv = cv; |
---|
50 | |
---|
51 | if(caca_init_driver(dp)) |
---|
52 | { |
---|
53 | free(dp); |
---|
54 | return NULL; |
---|
55 | } |
---|
56 | |
---|
57 | if(dp->drv.init_graphics(dp)) |
---|
58 | { |
---|
59 | free(dp); |
---|
60 | return NULL; |
---|
61 | } |
---|
62 | |
---|
63 | /* Attached! */ |
---|
64 | dp->cv->refcount++; |
---|
65 | |
---|
66 | /* Graphics stuff */ |
---|
67 | dp->delay = 0; |
---|
68 | dp->rendertime = 0; |
---|
69 | |
---|
70 | /* Events stuff */ |
---|
71 | #if defined(USE_SLANG) || defined(USE_NCURSES) |
---|
72 | dp->events.key_timer.last_sec = 0; |
---|
73 | dp->events.key_timer.last_usec = 0; |
---|
74 | dp->events.last_key_ticks = 0; |
---|
75 | dp->events.autorepeat_ticks = 0; |
---|
76 | dp->events.last_key_event.type = CACA_EVENT_NONE; |
---|
77 | #endif |
---|
78 | #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL) |
---|
79 | dp->events.queue = 0; |
---|
80 | #endif |
---|
81 | |
---|
82 | dp->timer.last_sec = 0; |
---|
83 | dp->timer.last_usec = 0; |
---|
84 | dp->lastticks = 0; |
---|
85 | |
---|
86 | /* Mouse position */ |
---|
87 | dp->mouse.x = dp->cv->width / 2; |
---|
88 | dp->mouse.y = dp->cv->height / 2; |
---|
89 | |
---|
90 | /* Resize events */ |
---|
91 | dp->resize.resized = 0; |
---|
92 | |
---|
93 | return dp; |
---|
94 | } |
---|
95 | |
---|
96 | /** \brief Detach a caca graphical context from a cucul backend context. |
---|
97 | * |
---|
98 | * Detach a graphical context from its cucul backend and destroy it. The |
---|
99 | * libcucul canvas continues to exist and other graphical contexts can be |
---|
100 | * attached to it afterwards. |
---|
101 | * |
---|
102 | * \param dp The libcaca graphical context. |
---|
103 | */ |
---|
104 | void caca_free_display(caca_display_t *dp) |
---|
105 | { |
---|
106 | dp->drv.end_graphics(dp); |
---|
107 | dp->cv->refcount--; |
---|
108 | free(dp); |
---|
109 | } |
---|
110 | |
---|
111 | /* |
---|
112 | * XXX: The following functions are local. |
---|
113 | */ |
---|
114 | |
---|
115 | static int caca_init_driver(caca_display_t *dp) |
---|
116 | { |
---|
117 | #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP) |
---|
118 | char *var = getenv("CACA_DRIVER"); |
---|
119 | |
---|
120 | /* If the environment variable was set, use it */ |
---|
121 | if(var && *var) |
---|
122 | { |
---|
123 | #if defined(USE_WIN32) |
---|
124 | if(!strcasecmp(var, "win32")) return win32_install(dp); |
---|
125 | #endif |
---|
126 | #if defined(USE_CONIO) |
---|
127 | if(!strcasecmp(var, "conio")) return conio_install(dp); |
---|
128 | #endif |
---|
129 | #if defined(USE_X11) |
---|
130 | if(!strcasecmp(var, "x11")) return x11_install(dp); |
---|
131 | #endif |
---|
132 | #if defined(USE_GL) |
---|
133 | if(!strcasecmp(var, "gl")) return gl_install(dp); |
---|
134 | #endif |
---|
135 | if(!strcasecmp(var, "raw")) return raw_install(dp); |
---|
136 | #if defined(USE_SLANG) |
---|
137 | if(!strcasecmp(var, "slang")) return slang_install(dp); |
---|
138 | #endif |
---|
139 | #if defined(USE_NCURSES) |
---|
140 | if(!strcasecmp(var, "ncurses")) return ncurses_install(dp); |
---|
141 | #endif |
---|
142 | #if defined(USE_VGA) |
---|
143 | if(!strcasecmp(var, "vga")) return vga_install(dp); |
---|
144 | #endif |
---|
145 | return -1; |
---|
146 | } |
---|
147 | #endif |
---|
148 | |
---|
149 | #if defined(USE_WIN32) |
---|
150 | if(win32_install(dp) == 0) return 0; |
---|
151 | #endif |
---|
152 | #if defined(USE_CONIO) |
---|
153 | if(conio_install(dp) == 0) return 0; |
---|
154 | #endif |
---|
155 | #if defined(USE_VGA) |
---|
156 | if(vga_install(dp) == 0) return 0; |
---|
157 | #endif |
---|
158 | #if defined(USE_X11) |
---|
159 | if(x11_install(dp) == 0) return 0; |
---|
160 | #endif |
---|
161 | #if defined(USE_GL) |
---|
162 | if(gl_install(dp) == 0) return 0; |
---|
163 | #endif |
---|
164 | #if defined(USE_SLANG) |
---|
165 | if(slang_install(dp) == 0) return 0; |
---|
166 | #endif |
---|
167 | #if defined(USE_NCURSES) |
---|
168 | if(ncurses_install(dp) == 0) return 0; |
---|
169 | #endif |
---|
170 | |
---|
171 | return -1; |
---|
172 | } |
---|
173 | |
---|