25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

202 satır
5.3 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. caca_event_t ev;
  104. VALUE e;
  105. event_mask = rb_funcall(event_mask, rb_intern("to_i"), 0);
  106. if(caca_get_event(_SELF, NUM2UINT(event_mask), &ev, NUM2INT(timeout)) == 0)
  107. {
  108. return Qnil;
  109. }
  110. switch(ev.type)
  111. {
  112. case CACA_EVENT_KEY_PRESS:
  113. e = rb_funcall(cEventKeyPress, rb_intern("new"), 3,
  114. UINT2NUM(ev.data.key.ch),
  115. ULONG2NUM(ev.data.key.utf32),
  116. rb_str_new(ev.data.key.utf8, 8));
  117. break;
  118. case CACA_EVENT_KEY_RELEASE:
  119. e = rb_funcall(cEventKeyRelease, rb_intern("new"), 3,
  120. UINT2NUM(ev.data.key.ch),
  121. ULONG2NUM(ev.data.key.utf32),
  122. rb_str_new(ev.data.key.utf8, 8));
  123. break;
  124. case CACA_EVENT_MOUSE_PRESS:
  125. e = rb_funcall(cEventMousePress, rb_intern("new"), 3,
  126. UINT2NUM(ev.data.mouse.x),
  127. UINT2NUM(ev.data.mouse.y),
  128. UINT2NUM(ev.data.mouse.button));
  129. break;
  130. case CACA_EVENT_MOUSE_RELEASE:
  131. e = rb_funcall(cEventMouseRelease, rb_intern("new"), 3,
  132. UINT2NUM(ev.data.mouse.x),
  133. UINT2NUM(ev.data.mouse.y),
  134. UINT2NUM(ev.data.mouse.button));
  135. break;
  136. case CACA_EVENT_MOUSE_MOTION:
  137. e = rb_funcall(cEventMouseMotion, rb_intern("new"), 3,
  138. UINT2NUM(ev.data.mouse.x),
  139. UINT2NUM(ev.data.mouse.y),
  140. Qnil);
  141. break;
  142. case CACA_EVENT_RESIZE:
  143. e = rb_funcall(cEventResize, rb_intern("new"), 2,
  144. UINT2NUM(ev.data.resize.w),
  145. UINT2NUM(ev.data.resize.h));
  146. break;
  147. case CACA_EVENT_QUIT:
  148. e = rb_funcall(cEventQuit, rb_intern("new"), 0);
  149. break;
  150. default:
  151. rb_raise(rb_eRuntimeError, "Invalid event received !");
  152. }
  153. return e;
  154. }
  155. void Init_caca_display(VALUE mCaca)
  156. {
  157. cDisplay = rb_define_class_under(mCaca, "Display", rb_cObject);
  158. rb_define_alloc_func(cDisplay, display_alloc);
  159. rb_define_method(cDisplay, "initialize", display_initialize, 1);
  160. rb_define_method(cDisplay, "refresh", display_refresh, 0);
  161. rb_define_method(cDisplay, "time=", set_time, 1);
  162. rb_define_method(cDisplay, "set_time", set_time2, 1);
  163. rb_define_method(cDisplay, "time", get_time, 0);
  164. rb_define_method(cDisplay, "width", get_width, 0);
  165. rb_define_method(cDisplay, "height", get_height, 0);
  166. rb_define_method(cDisplay, "title=", set_title, 1);
  167. rb_define_method(cDisplay, "set_title", set_title2, 1);
  168. rb_define_method(cDisplay, "mouse_x", get_mouse_x, 0);
  169. rb_define_method(cDisplay, "mouse_y", get_mouse_y, 0);
  170. rb_define_method(cDisplay, "mouse=", set_mouse, 1);
  171. rb_define_method(cDisplay, "set_mouse", set_mouse2, 1);
  172. rb_define_method(cDisplay, "get_event", get_event, 2);
  173. }