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.
 
 
 
 
 
 

154 lines
3.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. caca_event_t ev;
  104. if(caca_get_event(_SELF, NUM2UINT(event_mask), &ev, NUM2INT(timeout)) == 0)
  105. {
  106. return Qnil;
  107. }
  108. //FIXME
  109. return Qnil;
  110. }
  111. void Init_caca_display(VALUE mCaca)
  112. {
  113. cDisplay = rb_define_class_under(mCaca, "Display", rb_cObject);
  114. rb_define_alloc_func(cDisplay, display_alloc);
  115. rb_define_method(cDisplay, "initialize", display_initialize, 1);
  116. rb_define_method(cDisplay, "refresh", display_refresh, 0);
  117. rb_define_method(cDisplay, "time=", set_time, 1);
  118. rb_define_method(cDisplay, "set_time", set_time2, 1);
  119. rb_define_method(cDisplay, "time", get_time, 0);
  120. rb_define_method(cDisplay, "width", get_width, 0);
  121. rb_define_method(cDisplay, "height", get_height, 0);
  122. rb_define_method(cDisplay, "title=", set_title, 1);
  123. rb_define_method(cDisplay, "set_title", set_title2, 1);
  124. rb_define_method(cDisplay, "mouse_x", get_mouse_x, 0);
  125. rb_define_method(cDisplay, "mouse_y", get_mouse_y, 0);
  126. rb_define_method(cDisplay, "mouse=", set_mouse, 1);
  127. rb_define_method(cDisplay, "set_mouse", set_mouse2, 1);
  128. rb_define_method(cDisplay, "get_event", get_event, 3);
  129. }