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.
 
 
 
 
 
 

196 lines
5.1 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software; you can redistribute it and/or
  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.
  12. */
  13. /*
  14. * This file contains glue code for applications using the pre-1.0
  15. * libcaca API.
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #if !defined(__KERNEL__)
  20. # include <stdio.h>
  21. #endif
  22. #include "caca.h"
  23. #include "caca_internals.h"
  24. /* These variables are needed to emulate old non-thread safe behaviour */
  25. cucul_canvas_t *__caca0_cv = NULL;
  26. caca_display_t *__caca0_dp = NULL;
  27. unsigned char __caca0_fg;
  28. unsigned char __caca0_bg;
  29. char __caca0_utf8[2] = " ";
  30. /* These functions are needed, too */
  31. int __caca0_init(void);
  32. void __caca0_end(void);
  33. unsigned int __caca0_get_event(unsigned int, int);
  34. int __caca0_get_feature(int);
  35. void __caca0_set_feature(int);
  36. char const *__caca0_get_feature_name(int);
  37. cucul_canvas_t *__caca0_load_sprite(char const *);
  38. /* Emulation functions */
  39. int __caca0_init(void)
  40. {
  41. __caca0_cv = cucul_create_canvas(0, 0);
  42. if(!__caca0_cv)
  43. return -1;
  44. __caca0_dp = caca_create_display(__caca0_cv);
  45. if(!__caca0_dp)
  46. {
  47. cucul_free_canvas(__caca0_cv);
  48. __caca0_cv = NULL;
  49. return -1;
  50. }
  51. __caca0_fg = CUCUL_COLOR_LIGHTGRAY;
  52. __caca0_bg = CUCUL_COLOR_BLACK;
  53. return 0;
  54. }
  55. void __caca0_end(void)
  56. {
  57. caca_free_display(__caca0_dp);
  58. __caca0_dp = NULL;
  59. cucul_free_canvas(__caca0_cv);
  60. __caca0_cv = NULL;
  61. }
  62. unsigned int __caca0_get_event(unsigned int m, int t)
  63. {
  64. caca_event_t ev;
  65. int ret;
  66. ret = caca_get_event(__caca0_dp, (m >> 24) & 0x7f, &ev, t);
  67. if(!ret)
  68. return 0x00000000;
  69. switch(ev.type)
  70. {
  71. case CACA_EVENT_KEY_PRESS:
  72. return 0x01000000 | ev.data.key.ch;
  73. case CACA_EVENT_KEY_RELEASE:
  74. return 0x02000000 | ev.data.key.ch;
  75. case CACA_EVENT_MOUSE_PRESS:
  76. return 0x04000000 | ev.data.mouse.button;
  77. case CACA_EVENT_MOUSE_RELEASE:
  78. return 0x08000000 | ev.data.mouse.button;
  79. case CACA_EVENT_MOUSE_MOTION:
  80. return 0x10000000 | ((ev.data.mouse.x & 0xfff) << 12)
  81. | (ev.data.mouse.y & 0xfff);
  82. case CACA_EVENT_RESIZE:
  83. return 0x20000000;
  84. default:
  85. break;
  86. }
  87. return 0x00000000;
  88. }
  89. enum caca_feature
  90. {
  91. CACA_BACKGROUND = 0x10,
  92. CACA_BACKGROUND_BLACK = 0x11,
  93. CACA_BACKGROUND_SOLID = 0x12,
  94. #define CACA_BACKGROUND_MIN 0x11
  95. #define CACA_BACKGROUND_MAX 0x12
  96. CACA_ANTIALIASING = 0x20,
  97. CACA_ANTIALIASING_NONE = 0x21,
  98. CACA_ANTIALIASING_PREFILTER = 0x22,
  99. #define CACA_ANTIALIASING_MIN 0x21
  100. #define CACA_ANTIALIASING_MAX 0x22
  101. CACA_DITHERING = 0x30,
  102. CACA_DITHERING_NONE = 0x31,
  103. CACA_DITHERING_ORDERED2 = 0x32,
  104. CACA_DITHERING_ORDERED4 = 0x33,
  105. CACA_DITHERING_ORDERED8 = 0x34,
  106. CACA_DITHERING_RANDOM = 0x35,
  107. #define CACA_DITHERING_MIN 0x31
  108. #define CACA_DITHERING_MAX 0x35
  109. CACA_FEATURE_UNKNOWN = 0xffff
  110. };
  111. int __caca0_get_feature(int feature)
  112. {
  113. return feature;
  114. }
  115. void __caca0_set_feature(int feature)
  116. {
  117. switch(feature)
  118. {
  119. case CACA_BACKGROUND:
  120. feature = CACA_BACKGROUND_SOLID;
  121. case CACA_BACKGROUND_BLACK:
  122. case CACA_BACKGROUND_SOLID:
  123. //_caca_background = feature;
  124. break;
  125. case CACA_ANTIALIASING:
  126. feature = CACA_ANTIALIASING_PREFILTER;
  127. case CACA_ANTIALIASING_NONE:
  128. case CACA_ANTIALIASING_PREFILTER:
  129. //_caca_antialiasing = feature;
  130. break;
  131. case CACA_DITHERING:
  132. feature = CACA_DITHERING_ORDERED4;
  133. case CACA_DITHERING_NONE:
  134. case CACA_DITHERING_ORDERED2:
  135. case CACA_DITHERING_ORDERED4:
  136. case CACA_DITHERING_ORDERED8:
  137. case CACA_DITHERING_RANDOM:
  138. //_caca_dithering = feature;
  139. break;
  140. }
  141. }
  142. char const *__caca0_get_feature_name(int feature)
  143. {
  144. switch(feature)
  145. {
  146. case CACA_BACKGROUND_BLACK: return "black background";
  147. case CACA_BACKGROUND_SOLID: return "solid background";
  148. case CACA_ANTIALIASING_NONE: return "no antialiasing";
  149. case CACA_ANTIALIASING_PREFILTER: return "prefilter antialiasing";
  150. case CACA_DITHERING_NONE: return "no dithering";
  151. case CACA_DITHERING_ORDERED2: return "2x2 ordered dithering";
  152. case CACA_DITHERING_ORDERED4: return "4x4 ordered dithering";
  153. case CACA_DITHERING_ORDERED8: return "8x8 ordered dithering";
  154. case CACA_DITHERING_RANDOM: return "random dithering";
  155. default: return "unknown";
  156. }
  157. }
  158. cucul_canvas_t *__caca0_load_sprite(char const *file)
  159. {
  160. cucul_buffer_t *buf;
  161. cucul_canvas_t *cv;
  162. buf = cucul_load_file(file);
  163. if(!buf)
  164. return NULL;
  165. cv = cucul_import_canvas(buf, "");
  166. cucul_free_buffer(buf);
  167. if(!cv)
  168. return NULL;
  169. return cv;
  170. }