[35] | 1 | /* |
---|
[672] | 2 | * libcaca Colour ASCII-Art library |
---|
[699] | 3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
[268] | 4 | * All Rights Reserved |
---|
[35] | 5 | * |
---|
[769] | 6 | * $Id: caca.c 773 2006-04-14 12:10:18Z sam $ |
---|
| 7 | * |
---|
[268] | 8 | * This library is free software; you can redistribute it and/or |
---|
[522] | 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. |
---|
[35] | 12 | */ |
---|
[17] | 13 | |
---|
[769] | 14 | /* |
---|
[268] | 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. |
---|
[205] | 18 | */ |
---|
| 19 | |
---|
[63] | 20 | #include "config.h" |
---|
| 21 | |
---|
[565] | 22 | #if !defined(__KERNEL__) |
---|
| 23 | # include <stdlib.h> |
---|
| 24 | # include <string.h> |
---|
| 25 | #endif |
---|
[17] | 26 | |
---|
[524] | 27 | #include "cucul.h" |
---|
| 28 | #include "cucul_internals.h" |
---|
[185] | 29 | #include "caca.h" |
---|
| 30 | #include "caca_internals.h" |
---|
[17] | 31 | |
---|
[539] | 32 | static int caca_init_driver(caca_t *kk); |
---|
[227] | 33 | |
---|
[540] | 34 | /** \brief Attach a caca graphical context to a cucul backend context. |
---|
| 35 | * |
---|
| 36 | * Create a graphical context using device-dependent features (ncurses for |
---|
| 37 | * terminals, an X11 window, a DOS command window...) that attaches to a |
---|
| 38 | * libcucul canvas. Everything that gets drawn in the libcucul canvas can |
---|
| 39 | * then be displayed by the libcaca driver. |
---|
| 40 | * |
---|
| 41 | * \param qq The cucul backend context. |
---|
| 42 | * \return The caca graphical context or NULL if an error occurred. |
---|
| 43 | */ |
---|
[524] | 44 | caca_t * caca_attach(cucul_t * qq) |
---|
[17] | 45 | { |
---|
[524] | 46 | caca_t *kk = malloc(sizeof(caca_t)); |
---|
| 47 | |
---|
[540] | 48 | kk->qq = qq; |
---|
[232] | 49 | |
---|
[540] | 50 | if(caca_init_driver(kk)) |
---|
[539] | 51 | { |
---|
| 52 | free(kk); |
---|
[524] | 53 | return NULL; |
---|
[265] | 54 | } |
---|
[79] | 55 | |
---|
[550] | 56 | if(kk->drv.init_graphics(kk)) |
---|
[335] | 57 | { |
---|
[539] | 58 | free(kk); |
---|
| 59 | return NULL; |
---|
[483] | 60 | } |
---|
[213] | 61 | |
---|
[540] | 62 | /* Attached! */ |
---|
| 63 | kk->qq->refcount++; |
---|
| 64 | |
---|
| 65 | /* Graphics stuff */ |
---|
| 66 | kk->delay = 0; |
---|
| 67 | kk->rendertime = 0; |
---|
| 68 | |
---|
| 69 | /* Events stuff */ |
---|
| 70 | #if defined(USE_SLANG) || defined(USE_NCURSES) |
---|
[527] | 71 | kk->events.key_timer.last_sec = 0; |
---|
| 72 | kk->events.key_timer.last_usec = 0; |
---|
| 73 | kk->events.last_key_ticks = 0; |
---|
| 74 | kk->events.autorepeat_ticks = 0; |
---|
[681] | 75 | kk->events.last_key_event.type = CACA_EVENT_NONE; |
---|
[540] | 76 | #endif |
---|
[681] | 77 | #if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO) || defined(USE_GL) |
---|
[593] | 78 | kk->events.queue = 0; |
---|
| 79 | #endif |
---|
[527] | 80 | |
---|
| 81 | kk->timer.last_sec = 0; |
---|
| 82 | kk->timer.last_usec = 0; |
---|
| 83 | kk->lastticks = 0; |
---|
| 84 | |
---|
[553] | 85 | /* Mouse position */ |
---|
[551] | 86 | kk->mouse.x = kk->qq->width / 2; |
---|
| 87 | kk->mouse.y = kk->qq->height / 2; |
---|
[541] | 88 | |
---|
[553] | 89 | /* Resize events */ |
---|
| 90 | kk->resize.resized = 0; |
---|
[306] | 91 | |
---|
[524] | 92 | return kk; |
---|
[17] | 93 | } |
---|
| 94 | |
---|
[540] | 95 | /** \brief Detach a caca graphical context from a cucul backend context. |
---|
| 96 | * |
---|
| 97 | * Detach a graphical context from its cucul backend and destroy it. The |
---|
| 98 | * libcucul canvas continues to exist and other graphical contexts can be |
---|
| 99 | * attached to it afterwards. |
---|
| 100 | * |
---|
[773] | 101 | * \param kk The libcaca graphical context. |
---|
[540] | 102 | */ |
---|
[524] | 103 | void caca_detach(caca_t *kk) |
---|
[17] | 104 | { |
---|
[550] | 105 | kk->drv.end_graphics(kk); |
---|
[524] | 106 | kk->qq->refcount--; |
---|
| 107 | free(kk); |
---|
[17] | 108 | } |
---|
| 109 | |
---|
[268] | 110 | /* |
---|
| 111 | * XXX: The following functions are local. |
---|
| 112 | */ |
---|
| 113 | |
---|
[539] | 114 | static int caca_init_driver(caca_t *kk) |
---|
[265] | 115 | { |
---|
| 116 | #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP) |
---|
| 117 | char *var = getenv("CACA_DRIVER"); |
---|
[586] | 118 | |
---|
[265] | 119 | /* If the environment variable was set, use it */ |
---|
| 120 | if(var && *var) |
---|
[623] | 121 | { |
---|
[335] | 122 | #if defined(USE_WIN32) |
---|
[684] | 123 | if(!strcasecmp(var, "win32")) return win32_install(kk); |
---|
[335] | 124 | #endif |
---|
[265] | 125 | #if defined(USE_CONIO) |
---|
[684] | 126 | if(!strcasecmp(var, "conio")) return conio_install(kk); |
---|
[265] | 127 | #endif |
---|
[285] | 128 | #if defined(USE_X11) |
---|
[684] | 129 | if(!strcasecmp(var, "x11")) return x11_install(kk); |
---|
[265] | 130 | #endif |
---|
[483] | 131 | #if defined(USE_GL) |
---|
[684] | 132 | if(!strcasecmp(var, "gl")) return gl_install(kk); |
---|
[483] | 133 | #endif |
---|
[699] | 134 | if(!strcasecmp(var, "raw")) return raw_install(kk); |
---|
[265] | 135 | #if defined(USE_SLANG) |
---|
[684] | 136 | if(!strcasecmp(var, "slang")) return slang_install(kk); |
---|
[265] | 137 | #endif |
---|
[285] | 138 | #if defined(USE_NCURSES) |
---|
[684] | 139 | if(!strcasecmp(var, "ncurses")) return ncurses_install(kk); |
---|
[265] | 140 | #endif |
---|
[565] | 141 | #if defined(USE_VGA) |
---|
[684] | 142 | if(!strcasecmp(var, "vga")) return vga_install(kk); |
---|
[565] | 143 | #endif |
---|
[684] | 144 | return -1; |
---|
[265] | 145 | } |
---|
| 146 | #endif |
---|
| 147 | |
---|
[335] | 148 | #if defined(USE_WIN32) |
---|
[684] | 149 | if(win32_install(kk) == 0) return 0; |
---|
[335] | 150 | #endif |
---|
[265] | 151 | #if defined(USE_CONIO) |
---|
[684] | 152 | if(conio_install(kk) == 0) return 0; |
---|
[265] | 153 | #endif |
---|
[565] | 154 | #if defined(USE_VGA) |
---|
[684] | 155 | if(vga_install(kk) == 0) return 0; |
---|
[565] | 156 | #endif |
---|
[285] | 157 | #if defined(USE_X11) |
---|
[684] | 158 | if(x11_install(kk) == 0) return 0; |
---|
[265] | 159 | #endif |
---|
[483] | 160 | #if defined(USE_GL) |
---|
[684] | 161 | if(gl_install(kk) == 0) return 0; |
---|
[483] | 162 | #endif |
---|
[265] | 163 | #if defined(USE_SLANG) |
---|
[684] | 164 | if(slang_install(kk) == 0) return 0; |
---|
[265] | 165 | #endif |
---|
[285] | 166 | #if defined(USE_NCURSES) |
---|
[684] | 167 | if(ncurses_install(kk) == 0) return 0; |
---|
[265] | 168 | #endif |
---|
[487] | 169 | |
---|
[539] | 170 | return -1; |
---|
[265] | 171 | } |
---|
| 172 | |
---|