You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

262 line
5.6 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. "none",
  159. "ordered 4x4",
  160. "ordered 8x8",
  161. "random"
  162. };
  163. if(dithering < 0 || dithering > 3)
  164. return "unknown";
  165. return dithering_names[dithering];
  166. }
  167. void caca_end(void)
  168. {
  169. #if defined(USE_SLANG)
  170. SLtt_set_mouse_mode(0, 0);
  171. SLtt_set_cursor_visibility(1);
  172. SLang_reset_tty();
  173. SLsmg_reset_smg();
  174. #elif defined(USE_NCURSES)
  175. mousemask(oldmask, NULL);
  176. curs_set(1);
  177. endwin();
  178. #elif defined(USE_CONIO)
  179. _wscroll = 1;
  180. textcolor((enum COLORS)WHITE);
  181. textbackground((enum COLORS)BLACK);
  182. gotoxy(caca_get_width(), caca_get_height());
  183. cputs("\r\n");
  184. _setcursortype(_NORMALCURSOR);
  185. #endif
  186. }
  187. static void caca_init_terminal(void)
  188. {
  189. #if defined(HAVE_GETENV) && defined(HAVE_PUTENV)
  190. char *term, *colorterm, *other;
  191. term = getenv("TERM");
  192. colorterm = getenv("COLORTERM");
  193. if(term && !strcmp(term, "xterm"))
  194. {
  195. /* If we are using gnome-terminal, it's really a 16 colour terminal */
  196. if(colorterm && !strcmp(colorterm, "gnome-terminal"))
  197. {
  198. #if defined(USE_NCURSES)
  199. SCREEN *screen;
  200. screen = newterm("xterm-16color", stdout, stdin);
  201. if(screen == NULL)
  202. return;
  203. endwin();
  204. #endif
  205. (void)putenv("TERM=xterm-16color");
  206. return;
  207. }
  208. /* Ditto if we are using Konsole */
  209. other = getenv("KONSOLE_DCOP_SESSION");
  210. if(other)
  211. {
  212. #if defined(USE_NCURSES)
  213. SCREEN *screen;
  214. screen = newterm("xterm-16color", stdout, stdin);
  215. if(screen == NULL)
  216. return;
  217. endwin();
  218. #endif
  219. (void)putenv("TERM=xterm-16color");
  220. return;
  221. }
  222. }
  223. #endif
  224. }