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.
 
 
 
 
 
 

216 lines
5.7 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. VALUE cv;
  32. rb_scan_args(argc, argv, "01", &cv);
  33. if(NIL_P(cv))
  34. {
  35. display = caca_create_display(NULL);
  36. }
  37. else
  38. {
  39. if(CLASS_OF(cv) != cCanvas)
  40. {
  41. rb_raise(rb_eArgError, "Argument is not a Cucul::Canvas");
  42. }
  43. display = caca_create_display(DATA_PTR(cv));
  44. }
  45. if(display == NULL)
  46. {
  47. rb_raise(rb_eRuntimeError, strerror(errno));
  48. }
  49. _SELF = display;
  50. return self;
  51. }
  52. static VALUE display_refresh(VALUE self)
  53. {
  54. caca_refresh_display(_SELF);
  55. return self;
  56. }
  57. static VALUE set_time(VALUE self, VALUE t)
  58. {
  59. caca_set_display_time(_SELF, UINT2NUM(t));
  60. return t;
  61. }
  62. static VALUE set_time2(VALUE self, VALUE t)
  63. {
  64. set_time(self, t);
  65. return self;
  66. }
  67. static VALUE get_time(VALUE self)
  68. {
  69. return NUM2UINT(caca_get_display_time(_SELF));
  70. }
  71. static VALUE get_width(VALUE self)
  72. {
  73. return NUM2UINT(caca_get_display_width(_SELF));
  74. }
  75. static VALUE get_height(VALUE self)
  76. {
  77. return NUM2UINT(caca_get_display_height(_SELF));
  78. }
  79. static VALUE set_title(VALUE self, VALUE t)
  80. {
  81. if(caca_set_display_title(_SELF, StringValuePtr(t))<0)
  82. {
  83. rb_raise(rb_eRuntimeError, strerror(errno));
  84. }
  85. return t;
  86. }
  87. static VALUE set_title2(VALUE self, VALUE t)
  88. {
  89. set_title(self, t);
  90. return self;
  91. }
  92. static VALUE get_mouse_x(VALUE self)
  93. {
  94. return NUM2UINT(caca_get_mouse_x(_SELF));
  95. }
  96. static VALUE get_mouse_y(VALUE self)
  97. {
  98. return NUM2UINT(caca_get_mouse_y(_SELF));
  99. }
  100. static VALUE set_mouse(VALUE self, VALUE visible)
  101. {
  102. caca_set_display_time(_SELF, visible);
  103. return visible;
  104. }
  105. static VALUE set_mouse2(VALUE self, VALUE visible)
  106. {
  107. set_mouse(self, visible);
  108. return self;
  109. }
  110. static VALUE get_event(VALUE self, VALUE event_mask, VALUE timeout)
  111. {
  112. char utf8[8];
  113. caca_event_t ev;
  114. VALUE e;
  115. event_mask = rb_funcall(event_mask, rb_intern("to_i"), 0);
  116. if(caca_get_event(_SELF, NUM2UINT(event_mask), &ev, NUM2INT(timeout)) == 0)
  117. {
  118. return Qnil;
  119. }
  120. switch(caca_get_event_type(&ev))
  121. {
  122. case CACA_EVENT_KEY_PRESS:
  123. caca_get_event_key_utf8(&ev, utf8);
  124. e = rb_funcall(cEventKeyPress, rb_intern("new"), 3,
  125. UINT2NUM(caca_get_event_key_ch(&ev)),
  126. ULONG2NUM(caca_get_event_key_utf32(&ev)),
  127. rb_str_new(utf8, 8));
  128. break;
  129. case CACA_EVENT_KEY_RELEASE:
  130. caca_get_event_key_utf8(&ev, utf8);
  131. e = rb_funcall(cEventKeyRelease, rb_intern("new"), 3,
  132. UINT2NUM(caca_get_event_key_ch(&ev)),
  133. ULONG2NUM(caca_get_event_key_utf32(&ev)),
  134. rb_str_new(utf8, 8));
  135. break;
  136. case CACA_EVENT_MOUSE_PRESS:
  137. e = rb_funcall(cEventMousePress, rb_intern("new"), 3,
  138. UINT2NUM(caca_get_event_mouse_x(&ev)),
  139. UINT2NUM(caca_get_event_mouse_y(&ev)),
  140. UINT2NUM(caca_get_event_mouse_button(&ev)));
  141. break;
  142. case CACA_EVENT_MOUSE_RELEASE:
  143. e = rb_funcall(cEventMouseRelease, rb_intern("new"), 3,
  144. UINT2NUM(caca_get_event_mouse_x(&ev)),
  145. UINT2NUM(caca_get_event_mouse_y(&ev)),
  146. UINT2NUM(caca_get_event_mouse_button(&ev)));
  147. break;
  148. case CACA_EVENT_MOUSE_MOTION:
  149. e = rb_funcall(cEventMouseMotion, rb_intern("new"), 3,
  150. UINT2NUM(caca_get_event_mouse_x(&ev)),
  151. UINT2NUM(caca_get_event_mouse_y(&ev)),
  152. Qnil);
  153. break;
  154. case CACA_EVENT_RESIZE:
  155. e = rb_funcall(cEventResize, rb_intern("new"), 2,
  156. UINT2NUM(caca_get_event_resize_width(&ev)),
  157. UINT2NUM(caca_get_event_resize_height(&ev)));
  158. break;
  159. case CACA_EVENT_QUIT:
  160. e = rb_funcall(cEventQuit, rb_intern("new"), 0);
  161. break;
  162. default:
  163. rb_raise(rb_eRuntimeError, "Invalid event received !");
  164. }
  165. return e;
  166. }
  167. void Init_caca_display(VALUE mCaca)
  168. {
  169. cDisplay = rb_define_class_under(mCaca, "Display", rb_cObject);
  170. rb_define_alloc_func(cDisplay, display_alloc);
  171. rb_define_method(cDisplay, "initialize", display_initialize, -1);
  172. rb_define_method(cDisplay, "refresh", display_refresh, 0);
  173. rb_define_method(cDisplay, "time=", set_time, 1);
  174. rb_define_method(cDisplay, "set_time", set_time2, 1);
  175. rb_define_method(cDisplay, "time", get_time, 0);
  176. rb_define_method(cDisplay, "width", get_width, 0);
  177. rb_define_method(cDisplay, "height", get_height, 0);
  178. rb_define_method(cDisplay, "title=", set_title, 1);
  179. rb_define_method(cDisplay, "set_title", set_title2, 1);
  180. rb_define_method(cDisplay, "mouse_x", get_mouse_x, 0);
  181. rb_define_method(cDisplay, "mouse_y", get_mouse_y, 0);
  182. rb_define_method(cDisplay, "mouse=", set_mouse, 1);
  183. rb_define_method(cDisplay, "set_mouse", set_mouse2, 1);
  184. rb_define_method(cDisplay, "get_event", get_event, 2);
  185. }