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.
 
 
 
 
 
 

221 line
5.9 KiB

  1. /*
  2. * libcaca Ruby bindings
  3. * Copyright (c) 2007 Pascal Terjan <pterjan@linuxfr.org>
  4. *
  5. * This library is free software. It comes without any warranty, to
  6. * the extent permitted by applicable law. You can redistribute it
  7. * and/or modify it under the terms of the Do What The Fuck You Want
  8. * To Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. #include <ruby.h>
  12. #include <caca.h>
  13. #include <errno.h>
  14. #include "caca-event.h"
  15. #include "cucul-canvas.h"
  16. #include "common.h"
  17. VALUE cDisplay;
  18. void display_free(void *display)
  19. {
  20. caca_free_display((caca_display_t *)display);
  21. }
  22. static VALUE display_alloc(VALUE klass)
  23. {
  24. VALUE obj;
  25. obj = Data_Wrap_Struct(klass, 0, display_free, NULL);
  26. return obj;
  27. }
  28. static VALUE display_initialize(int argc, VALUE* argv, VALUE self)
  29. {
  30. caca_display_t *display;
  31. cucul_canvas_t *canvas;
  32. VALUE cv;
  33. rb_scan_args(argc, argv, "01", &cv);
  34. if(NIL_P(cv))
  35. {
  36. display = caca_create_display(NULL);
  37. canvas = caca_get_canvas(display);
  38. cv = canvas_create(canvas);
  39. }
  40. else
  41. {
  42. if(CLASS_OF(cv) != cCanvas)
  43. {
  44. rb_raise(rb_eArgError, "Argument is not a Cucul::Canvas");
  45. }
  46. display = caca_create_display(DATA_PTR(cv));
  47. }
  48. if(display == NULL)
  49. {
  50. rb_raise(rb_eRuntimeError, strerror(errno));
  51. }
  52. _SELF = display;
  53. rb_iv_set(self, "@canvas", cv);
  54. return self;
  55. }
  56. static VALUE display_refresh(VALUE self)
  57. {
  58. caca_refresh_display(_SELF);
  59. return self;
  60. }
  61. static VALUE set_time(VALUE self, VALUE t)
  62. {
  63. caca_set_display_time(_SELF, UINT2NUM(t));
  64. return t;
  65. }
  66. static VALUE set_time2(VALUE self, VALUE t)
  67. {
  68. set_time(self, t);
  69. return self;
  70. }
  71. static VALUE get_time(VALUE self)
  72. {
  73. return NUM2UINT(caca_get_display_time(_SELF));
  74. }
  75. static VALUE get_width(VALUE self)
  76. {
  77. return NUM2UINT(caca_get_display_width(_SELF));
  78. }
  79. static VALUE get_height(VALUE self)
  80. {
  81. return NUM2UINT(caca_get_display_height(_SELF));
  82. }
  83. static VALUE set_title(VALUE self, VALUE t)
  84. {
  85. if(caca_set_display_title(_SELF, StringValuePtr(t))<0)
  86. {
  87. rb_raise(rb_eRuntimeError, strerror(errno));
  88. }
  89. return t;
  90. }
  91. static VALUE set_title2(VALUE self, VALUE t)
  92. {
  93. set_title(self, t);
  94. return self;
  95. }
  96. static VALUE get_mouse_x(VALUE self)
  97. {
  98. return NUM2UINT(caca_get_mouse_x(_SELF));
  99. }
  100. static VALUE get_mouse_y(VALUE self)
  101. {
  102. return NUM2UINT(caca_get_mouse_y(_SELF));
  103. }
  104. static VALUE set_mouse(VALUE self, VALUE visible)
  105. {
  106. caca_set_display_time(_SELF, visible);
  107. return visible;
  108. }
  109. static VALUE set_mouse2(VALUE self, VALUE visible)
  110. {
  111. set_mouse(self, visible);
  112. return self;
  113. }
  114. static VALUE get_event(VALUE self, VALUE event_mask, VALUE timeout)
  115. {
  116. char utf8[8];
  117. caca_event_t ev;
  118. VALUE e;
  119. event_mask = rb_funcall(event_mask, rb_intern("to_i"), 0);
  120. if(caca_get_event(_SELF, NUM2UINT(event_mask), &ev, NUM2INT(timeout)) == 0)
  121. {
  122. return Qnil;
  123. }
  124. switch(caca_get_event_type(&ev))
  125. {
  126. case CACA_EVENT_KEY_PRESS:
  127. caca_get_event_key_utf8(&ev, utf8);
  128. e = rb_funcall(cEventKeyPress, rb_intern("new"), 3,
  129. UINT2NUM(caca_get_event_key_ch(&ev)),
  130. ULONG2NUM(caca_get_event_key_utf32(&ev)),
  131. rb_str_new(utf8, 8));
  132. break;
  133. case CACA_EVENT_KEY_RELEASE:
  134. caca_get_event_key_utf8(&ev, utf8);
  135. e = rb_funcall(cEventKeyRelease, rb_intern("new"), 3,
  136. UINT2NUM(caca_get_event_key_ch(&ev)),
  137. ULONG2NUM(caca_get_event_key_utf32(&ev)),
  138. rb_str_new(utf8, 8));
  139. break;
  140. case CACA_EVENT_MOUSE_PRESS:
  141. e = rb_funcall(cEventMousePress, rb_intern("new"), 3,
  142. UINT2NUM(caca_get_event_mouse_x(&ev)),
  143. UINT2NUM(caca_get_event_mouse_y(&ev)),
  144. UINT2NUM(caca_get_event_mouse_button(&ev)));
  145. break;
  146. case CACA_EVENT_MOUSE_RELEASE:
  147. e = rb_funcall(cEventMouseRelease, rb_intern("new"), 3,
  148. UINT2NUM(caca_get_event_mouse_x(&ev)),
  149. UINT2NUM(caca_get_event_mouse_y(&ev)),
  150. UINT2NUM(caca_get_event_mouse_button(&ev)));
  151. break;
  152. case CACA_EVENT_MOUSE_MOTION:
  153. e = rb_funcall(cEventMouseMotion, rb_intern("new"), 3,
  154. UINT2NUM(caca_get_event_mouse_x(&ev)),
  155. UINT2NUM(caca_get_event_mouse_y(&ev)),
  156. Qnil);
  157. break;
  158. case CACA_EVENT_RESIZE:
  159. e = rb_funcall(cEventResize, rb_intern("new"), 2,
  160. UINT2NUM(caca_get_event_resize_width(&ev)),
  161. UINT2NUM(caca_get_event_resize_height(&ev)));
  162. break;
  163. case CACA_EVENT_QUIT:
  164. e = rb_funcall(cEventQuit, rb_intern("new"), 0);
  165. break;
  166. default:
  167. rb_raise(rb_eRuntimeError, "Invalid event received !");
  168. }
  169. return e;
  170. }
  171. void Init_caca_display(VALUE mCaca)
  172. {
  173. cDisplay = rb_define_class_under(mCaca, "Display", rb_cObject);
  174. rb_define_alloc_func(cDisplay, display_alloc);
  175. rb_define_method(cDisplay, "initialize", display_initialize, -1);
  176. rb_define_method(cDisplay, "refresh", display_refresh, 0);
  177. rb_define_method(cDisplay, "time=", set_time, 1);
  178. rb_define_method(cDisplay, "set_time", set_time2, 1);
  179. rb_define_method(cDisplay, "time", get_time, 0);
  180. rb_define_method(cDisplay, "width", get_width, 0);
  181. rb_define_method(cDisplay, "height", get_height, 0);
  182. rb_define_method(cDisplay, "title=", set_title, 1);
  183. rb_define_method(cDisplay, "set_title", set_title2, 1);
  184. rb_define_method(cDisplay, "mouse_x", get_mouse_x, 0);
  185. rb_define_method(cDisplay, "mouse_y", get_mouse_y, 0);
  186. rb_define_method(cDisplay, "mouse=", set_mouse, 1);
  187. rb_define_method(cDisplay, "set_mouse", set_mouse2, 1);
  188. rb_define_method(cDisplay, "get_event", get_event, 2);
  189. }