No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

263 líneas
5.7 KiB

  1. /*
  2. * libcaca ASCII-Art library
  3. * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  19. * 02111-1307 USA
  20. */
  21. /** \file caca.c
  22. * \version \$Id$
  23. * \author Sam Hocevar <sam@zoy.org>
  24. * \brief Main \e libcaca functions
  25. *
  26. * This file contains the main functions used by \e libcaca applications to
  27. * initialise the library, get the screen properties, set the framerate and
  28. * so on.
  29. */
  30. #include "config.h"
  31. #if defined(USE_SLANG)
  32. # include <slang.h>
  33. #elif defined(USE_NCURSES)
  34. # include <curses.h>
  35. #elif defined(USE_CONIO)
  36. # include <dos.h>
  37. # include <conio.h>
  38. # if defined(SCREENUPDATE_IN_PC_H)
  39. # include <pc.h>
  40. # endif
  41. #else
  42. # error "no graphics library detected"
  43. #endif
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include "caca.h"
  47. #include "caca_internals.h"
  48. static void caca_init_terminal(void);
  49. char *_caca_empty_line;
  50. char *_caca_scratch_line;
  51. #if defined(USE_NCURSES)
  52. static mmask_t oldmask;
  53. int _caca_attr[16*16];
  54. #endif
  55. #if defined(USE_CONIO)
  56. static struct text_info ti;
  57. char *_caca_screen;
  58. #endif
  59. int caca_init(void)
  60. {
  61. #if defined(USE_NCURSES)
  62. mmask_t newmask;
  63. #endif
  64. caca_init_terminal();
  65. #if defined(USE_SLANG)
  66. /* Initialize slang library */
  67. SLsig_block_signals();
  68. SLtt_get_terminfo();
  69. if(SLkp_init() == -1)
  70. {
  71. SLsig_unblock_signals();
  72. return -1;
  73. }
  74. SLang_init_tty(-1, 0, 1);
  75. if(SLsmg_init_smg() == -1)
  76. {
  77. SLsig_unblock_signals();
  78. return -1;
  79. }
  80. SLsig_unblock_signals();
  81. SLsmg_cls();
  82. SLtt_set_cursor_visibility(0);
  83. SLkp_define_keysym("\e[M", 1001);
  84. SLtt_set_mouse_mode(1, 0);
  85. SLsmg_refresh();
  86. /* Disable scrolling so that hashmap scrolling optimization code
  87. * does not cause ugly refreshes due to slow terminals */
  88. SLtt_Term_Cannot_Scroll = 1;
  89. #elif defined(USE_NCURSES)
  90. initscr();
  91. keypad(stdscr, TRUE);
  92. nonl();
  93. cbreak();
  94. noecho();
  95. nodelay(stdscr, TRUE);
  96. curs_set(0);
  97. /* Activate mouse */
  98. newmask = ALL_MOUSE_EVENTS;
  99. mousemask(newmask, &oldmask);
  100. #elif defined(USE_CONIO)
  101. _wscroll = 0;
  102. _setcursortype(_NOCURSOR);
  103. clrscr();
  104. #endif
  105. if(_caca_init_graphics())
  106. return -1;
  107. return 0;
  108. }
  109. unsigned int caca_get_width(void)
  110. {
  111. #if defined(USE_SLANG)
  112. return SLtt_Screen_Cols;
  113. #elif defined(USE_NCURSES)
  114. return COLS;
  115. #elif defined(USE_CONIO)
  116. return ti.screenwidth;
  117. #endif
  118. }
  119. unsigned int caca_get_height(void)
  120. {
  121. #if defined(USE_SLANG)
  122. return SLtt_Screen_Rows;
  123. #elif defined(USE_NCURSES)
  124. return LINES;
  125. #else
  126. return ti.screenheight;
  127. #endif
  128. }
  129. const char *caca_get_color_name(enum caca_color color)
  130. {
  131. static const char *color_names[] =
  132. {
  133. "black",
  134. "blue",
  135. "green",
  136. "cyan",
  137. "red",
  138. "magenta",
  139. "brown",
  140. "light gray",
  141. "dark gray",
  142. "light blue",
  143. "light green",
  144. "light cyan",
  145. "light red",
  146. "light magenta",
  147. "yellow",
  148. "white",
  149. };
  150. if(color < 0 || color > 15)
  151. return "unknown";
  152. return color_names[color];
  153. }
  154. const char *caca_get_dithering_name(enum caca_dithering dithering)
  155. {
  156. static const char *dithering_names[] =
  157. {
  158. "no",
  159. "2x2 ordered",
  160. "4x4 ordered",
  161. "8x8 ordered",
  162. "random"
  163. };
  164. if(dithering < 0 || dithering > 4)
  165. return "unknown";
  166. return dithering_names[dithering];
  167. }
  168. void caca_end(void)
  169. {
  170. #if defined(USE_SLANG)
  171. SLtt_set_mouse_mode(0, 0);
  172. SLtt_set_cursor_visibility(1);
  173. SLang_reset_tty();
  174. SLsmg_reset_smg();
  175. #elif defined(USE_NCURSES)
  176. mousemask(oldmask, NULL);
  177. curs_set(1);
  178. endwin();
  179. #elif defined(USE_CONIO)
  180. _wscroll = 1;
  181. textcolor((enum COLORS)WHITE);
  182. textbackground((enum COLORS)BLACK);
  183. gotoxy(caca_get_width(), caca_get_height());
  184. cputs("\r\n");
  185. _setcursortype(_NORMALCURSOR);
  186. #endif
  187. }
  188. static void caca_init_terminal(void)
  189. {
  190. #if defined(HAVE_GETENV) && defined(HAVE_PUTENV)
  191. char *term, *colorterm, *other;
  192. term = getenv("TERM");
  193. colorterm = getenv("COLORTERM");
  194. if(term && !strcmp(term, "xterm"))
  195. {
  196. /* If we are using gnome-terminal, it's really a 16 colour terminal */
  197. if(colorterm && !strcmp(colorterm, "gnome-terminal"))
  198. {
  199. #if defined(USE_NCURSES)
  200. SCREEN *screen;
  201. screen = newterm("xterm-16color", stdout, stdin);
  202. if(screen == NULL)
  203. return;
  204. endwin();
  205. #endif
  206. (void)putenv("TERM=xterm-16color");
  207. return;
  208. }
  209. /* Ditto if we are using Konsole */
  210. other = getenv("KONSOLE_DCOP_SESSION");
  211. if(other)
  212. {
  213. #if defined(USE_NCURSES)
  214. SCREEN *screen;
  215. screen = newterm("xterm-16color", stdout, stdin);
  216. if(screen == NULL)
  217. return;
  218. endwin();
  219. #endif
  220. (void)putenv("TERM=xterm-16color");
  221. return;
  222. }
  223. }
  224. #endif
  225. }