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.
 
 
 
 
 
 

115 lines
2.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 "cucul-canvas.h"
  15. #include "common.h"
  16. VALUE cDisplay;
  17. void display_free(void *display)
  18. {
  19. caca_free_display((caca_display_t *)display);
  20. }
  21. static VALUE display_alloc(VALUE klass)
  22. {
  23. VALUE obj;
  24. obj = Data_Wrap_Struct(klass, 0, display_free, NULL);
  25. return obj;
  26. }
  27. static VALUE display_initialize(VALUE self, VALUE cv)
  28. {
  29. caca_display_t *display;
  30. if(CLASS_OF(cv) != cCanvas)
  31. {
  32. rb_raise(rb_eArgError, "Argument is not a Cucul::Canvas");
  33. }
  34. display = caca_create_display(DATA_PTR(cv));
  35. if(display == NULL)
  36. {
  37. rb_raise(rb_eRuntimeError, strerror(errno));
  38. }
  39. _SELF = display;
  40. return self;
  41. }
  42. static VALUE display_refresh(VALUE self)
  43. {
  44. caca_refresh_display(_SELF);
  45. return self;
  46. }
  47. static VALUE set_time(VALUE self, VALUE t)
  48. {
  49. caca_set_display_time(_SELF, UINT2NUM(t));
  50. return t;
  51. }
  52. static VALUE set_time2(VALUE self, VALUE t)
  53. {
  54. set_time(self, t);
  55. return self;
  56. }
  57. static VALUE get_time(VALUE self)
  58. {
  59. return NUM2UINT(caca_get_display_time(_SELF));
  60. }
  61. static VALUE get_width(VALUE self)
  62. {
  63. return NUM2UINT(caca_get_display_width(_SELF));
  64. }
  65. static VALUE get_height(VALUE self)
  66. {
  67. return NUM2UINT(caca_get_display_height(_SELF));
  68. }
  69. static VALUE set_title(VALUE self, VALUE t)
  70. {
  71. if(caca_set_display_title(_SELF, StringValuePtr(t))<0)
  72. {
  73. rb_raise(rb_eRuntimeError, strerror(errno));
  74. }
  75. return t;
  76. }
  77. static VALUE set_title2(VALUE self, VALUE t)
  78. {
  79. set_title(self, t);
  80. return self;
  81. }
  82. void Init_caca_display(VALUE mCaca)
  83. {
  84. cDisplay = rb_define_class_under(mCaca, "Display", rb_cObject);
  85. rb_define_alloc_func(cDisplay, display_alloc);
  86. rb_define_method(cDisplay, "initialize", display_initialize, 1);
  87. rb_define_method(cDisplay, "refresh", display_refresh, 0);
  88. rb_define_method(cDisplay, "time=", set_time, 1);
  89. rb_define_method(cDisplay, "set_time", set_time2, 1);
  90. rb_define_method(cDisplay, "time", get_time, 0);
  91. rb_define_method(cDisplay, "width", get_width, 0);
  92. rb_define_method(cDisplay, "height", get_height, 0);
  93. rb_define_method(cDisplay, "title=", set_title, 1);
  94. rb_define_method(cDisplay, "set_title", set_title2, 1);
  95. }