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.
 
 
 
 
 
 

294 lines
7.6 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2010 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * This library is free software. It comes without any warranty, to
  7. * the extent permitted by applicable law. You can redistribute it
  8. * and/or modify it under the terms of the Do What The Fuck You Want
  9. * To Public License, Version 2, as published by Sam Hocevar. See
  10. * http://sam.zoy.org/wtfpl/COPYING for more details.
  11. */
  12. /*
  13. * This file contains glue code for applications using the pre-1.0
  14. * libcaca API.
  15. */
  16. #include "config.h"
  17. #if !defined(__KERNEL__)
  18. # include <stdlib.h>
  19. #endif
  20. #include "caca0.h"
  21. /* These variables are needed to emulate old non-thread safe behaviour */
  22. caca_canvas_t *__caca0_cv = NULL;
  23. caca_display_t *__caca0_dp = NULL;
  24. unsigned char __caca0_fg = CACA_LIGHTGRAY;
  25. unsigned char __caca0_bg = CACA_BLACK;
  26. char __caca0_utf8[2] = " ";
  27. /* These functions are needed, too */
  28. int __caca0_init(void);
  29. void __caca0_end(void);
  30. unsigned int __caca0_get_event(unsigned int, int);
  31. unsigned int __caca0_sqrt(unsigned int);
  32. int __caca0_get_feature(int);
  33. void __caca0_set_feature(int);
  34. char const *__caca0_get_feature_name(int);
  35. caca_canvas_t *__caca0_load_sprite(char const *);
  36. caca_dither_t *__caca0_create_bitmap(unsigned int, unsigned int,
  37. unsigned int, unsigned int, unsigned long int, unsigned long int,
  38. unsigned long int, unsigned long int);
  39. void __caca0_free_bitmap(caca_dither_t *);
  40. extern char const *__caca0_get_color_name(unsigned char);
  41. /* Emulation functions */
  42. int __caca0_init(void)
  43. {
  44. __caca0_cv = caca_create_canvas(0, 0);
  45. if(!__caca0_cv)
  46. return -1;
  47. __caca0_dp = caca_create_display(__caca0_cv);
  48. if(!__caca0_dp)
  49. {
  50. caca_free_canvas(__caca0_cv);
  51. __caca0_cv = NULL;
  52. return -1;
  53. }
  54. __caca0_fg = CACA_LIGHTGRAY;
  55. __caca0_bg = CACA_BLACK;
  56. return 0;
  57. }
  58. void __caca0_end(void)
  59. {
  60. caca_free_display(__caca0_dp);
  61. __caca0_dp = NULL;
  62. caca_free_canvas(__caca0_cv);
  63. __caca0_cv = NULL;
  64. }
  65. unsigned int __caca0_get_event(unsigned int m, int t)
  66. {
  67. caca_event_t ev;
  68. int ret;
  69. ret = caca_get_event(__caca0_dp, (m >> 24) & 0x7f, &ev, t);
  70. if(!ret)
  71. return 0x00000000;
  72. switch(caca_get_event_type(&ev))
  73. {
  74. case CACA_EVENT_KEY_PRESS:
  75. return 0x01000000 | caca_get_event_key_ch(&ev);
  76. case CACA_EVENT_KEY_RELEASE:
  77. return 0x02000000 | caca_get_event_key_ch(&ev);
  78. case CACA_EVENT_MOUSE_PRESS:
  79. return 0x04000000 | caca_get_event_mouse_button(&ev);
  80. case CACA_EVENT_MOUSE_RELEASE:
  81. return 0x08000000 | caca_get_event_mouse_button(&ev);
  82. case CACA_EVENT_MOUSE_MOTION:
  83. return 0x10000000 | ((caca_get_event_mouse_x(&ev) & 0xfff) << 12)
  84. | (caca_get_event_mouse_y(&ev) & 0xfff);
  85. case CACA_EVENT_RESIZE:
  86. return 0x20000000;
  87. default:
  88. break;
  89. }
  90. return 0x00000000;
  91. }
  92. unsigned int __caca0_sqrt(unsigned int a)
  93. {
  94. if(a == 0)
  95. return 0;
  96. if(a < 1000000000)
  97. {
  98. unsigned int x = a < 10 ? 1
  99. : a < 1000 ? 10
  100. : a < 100000 ? 100
  101. : a < 10000000 ? 1000
  102. : 10000;
  103. /* Newton's method. Three iterations would be more than enough. */
  104. x = (x * x + a) / x / 2;
  105. x = (x * x + a) / x / 2;
  106. x = (x * x + a) / x / 2;
  107. x = (x * x + a) / x / 2;
  108. return x;
  109. }
  110. return 2 * __caca0_sqrt(a / 4);
  111. }
  112. static char const *features[] =
  113. {
  114. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  115. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  116. NULL, "16", "full16", NULL, NULL, NULL, NULL, NULL,
  117. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  118. NULL, "none", "prefilter", NULL, NULL, NULL, NULL, NULL,
  119. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  120. NULL, "none", "ordered2", "ordered4", "ordered8", "random"
  121. };
  122. static caca_dither_t **bitmaps = NULL;
  123. static unsigned int nbitmaps = 0;
  124. static int background = 0x12;
  125. static int antialiasing = 0x22;
  126. static int dithering = 0x33;
  127. int __caca0_get_feature(int feature)
  128. {
  129. if(feature == 0x10)
  130. return background;
  131. if(feature == 0x20)
  132. return antialiasing;
  133. if(feature == 0x30)
  134. return dithering;
  135. return 0xffff; /* CACA_FEATURE_UNKNOWN */
  136. }
  137. void __caca0_set_feature(int feature)
  138. {
  139. unsigned int i;
  140. switch(feature)
  141. {
  142. case 0x10: feature = 0x12; /* CACA_BACKGROUND_SOLID */
  143. case 0x11: case 0x12:
  144. background = feature;
  145. for(i = 0; i < nbitmaps; i++)
  146. caca_set_dither_color(bitmaps[i], features[feature]);
  147. break;
  148. case 0x20: feature = 0x22; /* CACA_ANTIALIASING_PREFILTER */
  149. case 0x21: case 0x22:
  150. antialiasing = feature;
  151. for(i = 0; i < nbitmaps; i++)
  152. caca_set_dither_antialias(bitmaps[i], features[feature]);
  153. break;
  154. case 0x30: feature = 0x33; /* CACA_DITHERING_ORDERED4 */
  155. case 0x31: case 0x32: case 0x33: case 0x34: case 0x35:
  156. dithering = feature;
  157. for(i = 0; i < nbitmaps; i++)
  158. caca_set_dither_algorithm(bitmaps[i], features[feature]);
  159. break;
  160. }
  161. }
  162. char const *__caca0_get_feature_name(int feature)
  163. {
  164. switch(feature)
  165. {
  166. case 0x11: return "black background";
  167. case 0x12: return "solid background";
  168. case 0x21: return "no antialiasing";
  169. case 0x22: return "prefilter antialiasing";
  170. case 0x31: return "no dithering";
  171. case 0x32: return "2x2 ordered dithering";
  172. case 0x33: return "4x4 ordered dithering";
  173. case 0x34: return "8x8 ordered dithering";
  174. case 0x35: return "random dithering";
  175. default: return "unknown";
  176. }
  177. }
  178. caca_canvas_t *__caca0_load_sprite(char const *file)
  179. {
  180. caca_canvas_t *cv;
  181. cv = caca_create_canvas(0, 0);;
  182. if(caca_import_canvas_from_file(cv, file, "") < 0)
  183. {
  184. caca_free_canvas(cv);
  185. return NULL;
  186. }
  187. return cv;
  188. }
  189. caca_dither_t *__caca0_create_bitmap(unsigned int bpp, unsigned int w,
  190. unsigned int h, unsigned int pitch,
  191. unsigned long int r, unsigned long int g,
  192. unsigned long int b, unsigned long int a)
  193. {
  194. caca_dither_t *d;
  195. d = caca_create_dither(bpp, w, h, pitch, r, g, b, a);
  196. if(!d)
  197. return NULL;
  198. caca_set_dither_color(d, features[background]);
  199. caca_set_dither_antialias(d, features[antialiasing]);
  200. caca_set_dither_algorithm(d, features[dithering]);
  201. /* Store bitmap in our list */
  202. nbitmaps++;
  203. bitmaps = realloc(bitmaps, nbitmaps * (sizeof(caca_dither_t *)));
  204. bitmaps[nbitmaps - 1] = d;
  205. return d;
  206. }
  207. void __caca0_free_bitmap(caca_dither_t *d)
  208. {
  209. unsigned int i, found = 0;
  210. caca_free_dither(d);
  211. /* Remove bitmap from our list */
  212. for(i = 0; i + 1 < nbitmaps; i++)
  213. {
  214. if(bitmaps[i] == d)
  215. found = 1;
  216. if(found)
  217. bitmaps[i] = bitmaps[i + 1];
  218. }
  219. nbitmaps--;
  220. }
  221. char const *__caca0_get_color_name(unsigned char color)
  222. {
  223. static char const *color_names[] =
  224. {
  225. "black",
  226. "blue",
  227. "green",
  228. "cyan",
  229. "red",
  230. "magenta",
  231. "brown",
  232. "light gray",
  233. "dark gray",
  234. "light blue",
  235. "light green",
  236. "light cyan",
  237. "light red",
  238. "light magenta",
  239. "yellow",
  240. "white",
  241. };
  242. if(color > 15)
  243. return "unknown";
  244. return color_names[(unsigned int)color];
  245. }