| 1 | /* |
|---|
| 2 | * libcaca Colour ASCII-Art library |
|---|
| 3 | * Copyright (c) 2006-2009 Sam Hocevar <sam@hocevar.net> |
|---|
| 4 | * All Rights Reserved |
|---|
| 5 | * |
|---|
| 6 | * $Id$ |
|---|
| 7 | * |
|---|
| 8 | * This library is free software. It comes without any warranty, to |
|---|
| 9 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 10 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 11 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | /* |
|---|
| 16 | * This file contains the main functions used by \e libcaca applications to |
|---|
| 17 | * initialise the library, get the screen properties, set the framerate and |
|---|
| 18 | * so on. |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #include "config.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 "caca.h" |
|---|
| 35 | #include "caca_internals.h" |
|---|
| 36 | |
|---|
| 37 | #if defined(USE_PLUGINS) |
|---|
| 38 | # define gl_install(p) caca_plugin_install(p, "gl") |
|---|
| 39 | # define x11_install(p) caca_plugin_install(p, "x11") |
|---|
| 40 | #endif |
|---|
| 41 | |
|---|
| 42 | static int caca_can_resize(caca_display_t *); |
|---|
| 43 | static int caca_install_driver(caca_display_t *, char const *); |
|---|
| 44 | static int caca_uninstall_driver(caca_display_t *); |
|---|
| 45 | static int caca_select_driver(caca_display_t *, char const *); |
|---|
| 46 | #if defined(USE_PLUGINS) |
|---|
| 47 | static int caca_plugin_install(caca_display_t *, char const *); |
|---|
| 48 | #endif |
|---|
| 49 | |
|---|
| 50 | /** \brief Attach a caca graphical context to a caca canvas. |
|---|
| 51 | * |
|---|
| 52 | * Create a graphical context using device-dependent features (ncurses for |
|---|
| 53 | * terminals, an X11 window, a DOS command window...) that attaches to a |
|---|
| 54 | * libcaca canvas. Everything that gets drawn in the libcaca canvas can |
|---|
| 55 | * then be displayed by the libcaca driver. |
|---|
| 56 | * |
|---|
| 57 | * If no caca canvas is provided, a new one is created. Its handle can be |
|---|
| 58 | * retrieved using caca_get_canvas() and it is automatically destroyed when |
|---|
| 59 | * caca_free_display() is called. |
|---|
| 60 | * |
|---|
| 61 | * See also caca_create_display_with_driver(). |
|---|
| 62 | * |
|---|
| 63 | * If an error occurs, NULL is returned and \b errno is set accordingly: |
|---|
| 64 | * - \c ENOMEM Not enough memory. |
|---|
| 65 | * - \c ENODEV Graphical device could not be initialised. |
|---|
| 66 | * |
|---|
| 67 | * \param cv The caca canvas or NULL to create a canvas automatically. |
|---|
| 68 | * \return The caca graphical context or NULL if an error occurred. |
|---|
| 69 | */ |
|---|
| 70 | caca_display_t * caca_create_display(caca_canvas_t *cv) |
|---|
| 71 | { |
|---|
| 72 | return caca_create_display_with_driver(cv, NULL); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | /** \brief Attach a specific caca graphical context to a caca canvas. |
|---|
| 76 | * |
|---|
| 77 | * Create a graphical context using device-dependent features (ncurses for |
|---|
| 78 | * terminals, an X11 window, a DOS command window...) that attaches to a |
|---|
| 79 | * libcaca canvas. Everything that gets drawn in the libcaca canvas can |
|---|
| 80 | * then be displayed by the libcaca driver. |
|---|
| 81 | * |
|---|
| 82 | * If no caca canvas is provided, a new one is created. Its handle can be |
|---|
| 83 | * retrieved using caca_get_canvas() and it is automatically destroyed when |
|---|
| 84 | * caca_free_display() is called. |
|---|
| 85 | * |
|---|
| 86 | * If no driver name is provided, \e libcaca will try to autodetect the best |
|---|
| 87 | * output driver it can. |
|---|
| 88 | * |
|---|
| 89 | * See also caca_create_display(). |
|---|
| 90 | * |
|---|
| 91 | * If an error occurs, NULL is returned and \b errno is set accordingly: |
|---|
| 92 | * - \c ENOMEM Not enough memory. |
|---|
| 93 | * - \c ENODEV Graphical device could not be initialised. |
|---|
| 94 | * |
|---|
| 95 | * \param cv The caca canvas or NULL to create a canvas automatically. |
|---|
| 96 | * \param driver A string describing the desired output driver or NULL to |
|---|
| 97 | * choose the best driver automatically. |
|---|
| 98 | * \return The caca graphical context or NULL if an error occurred. |
|---|
| 99 | */ |
|---|
| 100 | caca_display_t * caca_create_display_with_driver(caca_canvas_t *cv, |
|---|
| 101 | char const *driver) |
|---|
| 102 | { |
|---|
| 103 | caca_display_t *dp = malloc(sizeof(caca_display_t)); |
|---|
| 104 | |
|---|
| 105 | if(!dp) |
|---|
| 106 | { |
|---|
| 107 | seterrno(ENOMEM); |
|---|
| 108 | return NULL; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | if((dp->autorelease = (cv == NULL))) |
|---|
| 112 | { |
|---|
| 113 | cv = caca_create_canvas(0, 0); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | dp->cv = cv; |
|---|
| 117 | |
|---|
| 118 | if(caca_manage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp)) |
|---|
| 119 | { |
|---|
| 120 | if(dp->autorelease) |
|---|
| 121 | caca_free_canvas(dp->cv); |
|---|
| 122 | free(dp); |
|---|
| 123 | seterrno(EBUSY); |
|---|
| 124 | return NULL; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | if(caca_install_driver(dp, driver)) |
|---|
| 128 | { |
|---|
| 129 | caca_unmanage_canvas(cv, (int (*)(void *))caca_can_resize, (void *)dp); |
|---|
| 130 | if(dp->autorelease) |
|---|
| 131 | caca_free_canvas(dp->cv); |
|---|
| 132 | free(dp); |
|---|
| 133 | seterrno(ENODEV); |
|---|
| 134 | return NULL; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | return dp; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | /** \brief Get available display drivers |
|---|
| 141 | * |
|---|
| 142 | * Return a list of available display drivers. The list is a NULL-terminated |
|---|
| 143 | * array of strings, interleaving a string containing the internal value for |
|---|
| 144 | * the display driver, and a string containing the natural language |
|---|
| 145 | * description for that driver. |
|---|
| 146 | * |
|---|
| 147 | * This function never fails. |
|---|
| 148 | * |
|---|
| 149 | * \return An array of strings. |
|---|
| 150 | */ |
|---|
| 151 | char const * const * caca_get_display_driver_list(void) |
|---|
| 152 | { |
|---|
| 153 | static char const * const list[] = |
|---|
| 154 | { |
|---|
| 155 | #if defined(USE_COCOA) |
|---|
| 156 | "cocoa", "Mac OS X Cocoa", |
|---|
| 157 | #endif |
|---|
| 158 | #if defined(USE_WIN32) |
|---|
| 159 | "win32", "Windows console", |
|---|
| 160 | #endif |
|---|
| 161 | #if defined(USE_CONIO) |
|---|
| 162 | "conio", "MS-DOS conio", |
|---|
| 163 | #endif |
|---|
| 164 | #if defined(USE_X11) |
|---|
| 165 | "x11", "X11 graphical window", |
|---|
| 166 | #endif |
|---|
| 167 | #if defined(USE_GL) |
|---|
| 168 | "gl", "OpenGL window", |
|---|
| 169 | #endif |
|---|
| 170 | #if defined(USE_SLANG) |
|---|
| 171 | "slang", "S-Lang console library", |
|---|
| 172 | #endif |
|---|
| 173 | #if defined(USE_NCURSES) |
|---|
| 174 | "ncurses", "ncurses console library", |
|---|
| 175 | #endif |
|---|
| 176 | #if defined(USE_VGA) |
|---|
| 177 | "vga", "direct VGA memory", |
|---|
| 178 | #endif |
|---|
| 179 | #if !defined(__KERNEL__) |
|---|
| 180 | "raw", "raw libcaca output", |
|---|
| 181 | "null", "null driver", |
|---|
| 182 | #endif |
|---|
| 183 | NULL, NULL |
|---|
| 184 | }; |
|---|
| 185 | |
|---|
| 186 | return list; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | /** \brief Return a caca graphical context's current output driver. |
|---|
| 190 | * |
|---|
| 191 | * Return the given display's current output driver. |
|---|
| 192 | * |
|---|
| 193 | * This function never fails. |
|---|
| 194 | * |
|---|
| 195 | * \param dp The caca display. |
|---|
| 196 | * \return A static string. |
|---|
| 197 | */ |
|---|
| 198 | char const * caca_get_display_driver(caca_display_t *dp) |
|---|
| 199 | { |
|---|
| 200 | return dp->drv.driver; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | /** \brief Set the output driver. |
|---|
| 204 | * |
|---|
| 205 | * Dynamically change the given display's output driver. |
|---|
| 206 | * |
|---|
| 207 | * FIXME: decide what to do in case of failure |
|---|
| 208 | * |
|---|
| 209 | * \param dp The caca display. |
|---|
| 210 | * \param driver A string describing the desired output driver or NULL to |
|---|
| 211 | * choose the best driver automatically. |
|---|
| 212 | * \return 0 in case of success, -1 if an error occurred. |
|---|
| 213 | */ |
|---|
| 214 | int caca_set_display_driver(caca_display_t *dp, char const *driver) |
|---|
| 215 | { |
|---|
| 216 | caca_uninstall_driver(dp); |
|---|
| 217 | if(caca_install_driver(dp, driver)) |
|---|
| 218 | { |
|---|
| 219 | seterrno(ENODEV); |
|---|
| 220 | return -1; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | return 0; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | /** \brief Detach a caca graphical context from a caca backend context. |
|---|
| 227 | * |
|---|
| 228 | * Detach a graphical context from its caca backend and destroy it. The |
|---|
| 229 | * libcaca canvas continues to exist and other graphical contexts can be |
|---|
| 230 | * attached to it afterwards. |
|---|
| 231 | * |
|---|
| 232 | * If the caca canvas was automatically created by caca_create_display(), |
|---|
| 233 | * it is automatically destroyed and any handle to it becomes invalid. |
|---|
| 234 | * |
|---|
| 235 | * This function never fails. |
|---|
| 236 | * |
|---|
| 237 | * \param dp The libcaca graphical context. |
|---|
| 238 | * \return This function always returns 0. |
|---|
| 239 | */ |
|---|
| 240 | int caca_free_display(caca_display_t *dp) |
|---|
| 241 | { |
|---|
| 242 | caca_uninstall_driver(dp); |
|---|
| 243 | caca_unmanage_canvas(dp->cv, (int (*)(void *))caca_can_resize, (void *)dp); |
|---|
| 244 | if(dp->autorelease) |
|---|
| 245 | caca_free_canvas(dp->cv); |
|---|
| 246 | free(dp); |
|---|
| 247 | |
|---|
| 248 | return 0; |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | /** \brief Get the canvas attached to a caca graphical context. |
|---|
| 252 | * |
|---|
| 253 | * Return a handle on the \e caca_canvas_t object that was either attached |
|---|
| 254 | * or created by caca_create_display(). |
|---|
| 255 | * |
|---|
| 256 | * This function never fails. |
|---|
| 257 | * |
|---|
| 258 | * \param dp The libcaca graphical context. |
|---|
| 259 | * \return The libcaca canvas. |
|---|
| 260 | */ |
|---|
| 261 | caca_canvas_t * caca_get_canvas(caca_display_t *dp) |
|---|
| 262 | { |
|---|
| 263 | return dp->cv; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | /** \brief Return the \e libcaca version. |
|---|
| 267 | * |
|---|
| 268 | * Return a read-only string with the \e libcaca version information. |
|---|
| 269 | * |
|---|
| 270 | * This function never fails. |
|---|
| 271 | * |
|---|
| 272 | * \return The \e libcaca version information. |
|---|
| 273 | */ |
|---|
| 274 | char const * caca_get_version(void) |
|---|
| 275 | { |
|---|
| 276 | return PACKAGE_VERSION; |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | /* |
|---|
| 280 | * XXX: The following functions are local. |
|---|
| 281 | */ |
|---|
| 282 | |
|---|
| 283 | static int caca_can_resize(caca_display_t *dp) |
|---|
| 284 | { |
|---|
| 285 | return dp->resize.allow; |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | static int caca_install_driver(caca_display_t *dp, char const *driver) |
|---|
| 289 | { |
|---|
| 290 | #if defined(USE_PLUGINS) |
|---|
| 291 | dp->plugin = NULL; |
|---|
| 292 | #endif |
|---|
| 293 | |
|---|
| 294 | if(caca_select_driver(dp, driver)) |
|---|
| 295 | { |
|---|
| 296 | #if defined(USE_PLUGINS) |
|---|
| 297 | if(dp->plugin) |
|---|
| 298 | dlclose(dp->plugin); |
|---|
| 299 | #endif |
|---|
| 300 | return -1; |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | if(dp->drv.init_graphics(dp)) |
|---|
| 304 | { |
|---|
| 305 | #if defined(USE_PLUGINS) |
|---|
| 306 | if(dp->plugin) |
|---|
| 307 | dlclose(dp->plugin); |
|---|
| 308 | #endif |
|---|
| 309 | return -1; |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | /* Graphics stuff */ |
|---|
| 313 | dp->delay = 0; |
|---|
| 314 | dp->rendertime = 0; |
|---|
| 315 | |
|---|
| 316 | /* Events stuff */ |
|---|
| 317 | #if defined(USE_SLANG) || defined(USE_NCURSES) |
|---|
| 318 | dp->events.key_timer.last_sec = 0; |
|---|
| 319 | dp->events.key_timer.last_usec = 0; |
|---|
| 320 | dp->events.last_key_ticks = 0; |
|---|
| 321 | dp->events.autorepeat_ticks = 0; |
|---|
| 322 | dp->events.last_key_event.type = CACA_EVENT_NONE; |
|---|
| 323 | #endif |
|---|
| 324 | #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL) |
|---|
| 325 | dp->events.queue = 0; |
|---|
| 326 | #endif |
|---|
| 327 | |
|---|
| 328 | dp->timer.last_sec = 0; |
|---|
| 329 | dp->timer.last_usec = 0; |
|---|
| 330 | #if defined PROF |
|---|
| 331 | _caca_init_stat(&dp->display_stat, "dp[%p] disp_sys time", dp); |
|---|
| 332 | _caca_init_stat(&dp->wait_stat, "dp[%p] disp_wait time", dp); |
|---|
| 333 | _caca_init_stat(&dp->ev_sys_stat, "dp[%p] ev_sys time", dp); |
|---|
| 334 | _caca_init_stat(&dp->ev_wait_stat, "dp[%p] ev_wait time", dp); |
|---|
| 335 | #endif |
|---|
| 336 | dp->lastticks = 0; |
|---|
| 337 | |
|---|
| 338 | /* Mouse position */ |
|---|
| 339 | dp->mouse.x = caca_get_canvas_width(dp->cv) / 2; |
|---|
| 340 | dp->mouse.y = caca_get_canvas_height(dp->cv) / 2; |
|---|
| 341 | |
|---|
| 342 | /* Resize events */ |
|---|
| 343 | dp->resize.resized = 0; |
|---|
| 344 | dp->resize.allow = 0; |
|---|
| 345 | |
|---|
| 346 | return 0; |
|---|
| 347 | } |
|---|
| 348 | |
|---|
| 349 | static int caca_uninstall_driver(caca_display_t *dp) |
|---|
| 350 | { |
|---|
| 351 | dp->drv.end_graphics(dp); |
|---|
| 352 | #if defined(USE_PLUGINS) |
|---|
| 353 | if(dp->plugin) |
|---|
| 354 | dlclose(dp->plugin); |
|---|
| 355 | #endif |
|---|
| 356 | #if defined PROF |
|---|
| 357 | _caca_fini_stat(&dp->display_stat); |
|---|
| 358 | _caca_fini_stat(&dp->wait_stat); |
|---|
| 359 | _caca_fini_stat(&dp->ev_wait_stat); |
|---|
| 360 | _caca_fini_stat(&dp->ev_sys_stat); |
|---|
| 361 | #endif |
|---|
| 362 | |
|---|
| 363 | return 0; |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | static int caca_select_driver(caca_display_t *dp, char const *driver) |
|---|
| 367 | { |
|---|
| 368 | char const *var = driver; |
|---|
| 369 | #if defined(HAVE_GETENV) |
|---|
| 370 | if(!var) |
|---|
| 371 | var = getenv("CACA_DRIVER"); |
|---|
| 372 | #endif |
|---|
| 373 | |
|---|
| 374 | #if defined(HAVE_STRCASECMP) |
|---|
| 375 | /* If the environment variable was set, use it */ |
|---|
| 376 | if(var && *var) |
|---|
| 377 | { |
|---|
| 378 | #if defined(USE_COCOA) |
|---|
| 379 | if(!strcasecmp(var, "cocoa")) return cocoa_install(dp); |
|---|
| 380 | #endif |
|---|
| 381 | #if defined(USE_WIN32) |
|---|
| 382 | if(!strcasecmp(var, "win32")) return win32_install(dp); |
|---|
| 383 | #endif |
|---|
| 384 | #if defined(USE_CONIO) |
|---|
| 385 | if(!strcasecmp(var, "conio")) return conio_install(dp); |
|---|
| 386 | #endif |
|---|
| 387 | #if defined(USE_X11) |
|---|
| 388 | if(!strcasecmp(var, "x11")) return x11_install(dp); |
|---|
| 389 | #endif |
|---|
| 390 | #if defined(USE_GL) |
|---|
| 391 | if(!strcasecmp(var, "gl")) return gl_install(dp); |
|---|
| 392 | #endif |
|---|
| 393 | #if !defined(__KERNEL__) |
|---|
| 394 | if(!strcasecmp(var, "raw")) return raw_install(dp); |
|---|
| 395 | #endif |
|---|
| 396 | #if defined(USE_SLANG) |
|---|
| 397 | if(!strcasecmp(var, "slang")) return slang_install(dp); |
|---|
| 398 | #endif |
|---|
| 399 | #if defined(USE_NCURSES) |
|---|
| 400 | if(!strcasecmp(var, "ncurses")) return ncurses_install(dp); |
|---|
| 401 | #endif |
|---|
| 402 | #if defined(USE_VGA) |
|---|
| 403 | if(!strcasecmp(var, "vga")) return vga_install(dp); |
|---|
| 404 | #endif |
|---|
| 405 | #if !defined(__KERNEL__) |
|---|
| 406 | if(!strcasecmp(var, "null")) return null_install(dp); |
|---|
| 407 | #endif |
|---|
| 408 | return -1; |
|---|
| 409 | } |
|---|
| 410 | #endif |
|---|
| 411 | |
|---|
| 412 | #if defined(USE_COCOA) |
|---|
| 413 | if(cocoa_install(dp) == 0) return 0; |
|---|
| 414 | #endif |
|---|
| 415 | #if defined(USE_WIN32) |
|---|
| 416 | if(win32_install(dp) == 0) return 0; |
|---|
| 417 | #endif |
|---|
| 418 | #if defined(USE_CONIO) |
|---|
| 419 | if(conio_install(dp) == 0) return 0; |
|---|
| 420 | #endif |
|---|
| 421 | #if defined(USE_VGA) |
|---|
| 422 | if(vga_install(dp) == 0) return 0; |
|---|
| 423 | #endif |
|---|
| 424 | #if defined(USE_X11) |
|---|
| 425 | if(x11_install(dp) == 0) return 0; |
|---|
| 426 | #endif |
|---|
| 427 | #if defined(USE_GL) |
|---|
| 428 | if(gl_install(dp) == 0) return 0; |
|---|
| 429 | #endif |
|---|
| 430 | /* ncurses has a higher priority than slang because it has better colour |
|---|
| 431 | * support across terminal types, despite being slightly slower. */ |
|---|
| 432 | #if defined(USE_NCURSES) |
|---|
| 433 | if(ncurses_install(dp) == 0) return 0; |
|---|
| 434 | #endif |
|---|
| 435 | #if defined(USE_SLANG) |
|---|
| 436 | if(slang_install(dp) == 0) return 0; |
|---|
| 437 | #endif |
|---|
| 438 | /* Of course we don't try "raw" or "null" if the user did not |
|---|
| 439 | * specifically ask for them. */ |
|---|
| 440 | |
|---|
| 441 | return -1; |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | #if defined(USE_PLUGINS) |
|---|
| 445 | static int caca_plugin_install(caca_display_t *dp, char const *driver) |
|---|
| 446 | { |
|---|
| 447 | char buf[512]; |
|---|
| 448 | int (*sym) (caca_display_t *); |
|---|
| 449 | |
|---|
| 450 | sprintf(buf, "%s/lib%s_plugin.so", PLUGINDIR, driver); |
|---|
| 451 | dp->plugin = dlopen(buf, RTLD_NOW); |
|---|
| 452 | if(!dp->plugin) |
|---|
| 453 | { |
|---|
| 454 | sprintf(buf, "lib%s_plugin.so", driver); |
|---|
| 455 | dp->plugin = dlopen(buf, RTLD_NOW); |
|---|
| 456 | if(!dp->plugin) |
|---|
| 457 | return -1; |
|---|
| 458 | } |
|---|
| 459 | |
|---|
| 460 | sprintf(buf, "%s_install", driver); |
|---|
| 461 | sym = dlsym(dp->plugin, buf); |
|---|
| 462 | if(!sym) |
|---|
| 463 | { |
|---|
| 464 | dlclose(dp->plugin); |
|---|
| 465 | return -1; |
|---|
| 466 | } |
|---|
| 467 | |
|---|
| 468 | return sym(dp); |
|---|
| 469 | } |
|---|
| 470 | #endif |
|---|
| 471 | |
|---|
| 472 | /* |
|---|
| 473 | * XXX: The following functions are aliases. |
|---|
| 474 | */ |
|---|
| 475 | |
|---|
| 476 | char const * cucul_get_version(void) CACA_ALIAS(caca_get_version); |
|---|
| 477 | |
|---|