Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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