source: libcaca/trunk/ruby/caca-display.c @ 2017

Last change on this file since 2017 was 2017, checked in by Pascal Terjan, 13 years ago
  • Add missing methods in Caca::Display and add empty Caca::Event
  • Property svn:eol-style set to native
File size: 3.6 KB
Line 
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
12#include <ruby.h>
13#include <caca.h>
14#include <errno.h>
15#include "caca-event.h"
16#include "cucul-canvas.h"
17#include "common.h"
18
19VALUE cDisplay;
20
21void display_free(void *display)
22{
23    caca_free_display((caca_display_t *)display);
24}
25
26static VALUE display_alloc(VALUE klass)
27{
28    VALUE obj;
29    obj = Data_Wrap_Struct(klass, 0, display_free, NULL);   
30    return obj;
31}
32
33static VALUE display_initialize(VALUE self, VALUE cv)
34{
35    caca_display_t *display;
36   
37    if(CLASS_OF(cv) != cCanvas)
38    {
39        rb_raise(rb_eArgError, "Argument is not a Cucul::Canvas");
40    }
41
42    display = caca_create_display(DATA_PTR(cv));
43    if(display == NULL)
44    {
45        rb_raise(rb_eRuntimeError, strerror(errno));
46    }
47
48    _SELF = display;
49
50    return self;
51}
52
53static VALUE display_refresh(VALUE self)
54{
55    caca_refresh_display(_SELF);
56    return self;
57}
58
59static VALUE set_time(VALUE self, VALUE t)
60{
61    caca_set_display_time(_SELF, UINT2NUM(t));
62    return t;
63}
64
65static VALUE set_time2(VALUE self, VALUE t)
66{
67    set_time(self, t);
68    return self;
69}
70
71static VALUE get_time(VALUE self)
72{
73    return NUM2UINT(caca_get_display_time(_SELF));
74}
75
76static VALUE get_width(VALUE self)
77{
78    return NUM2UINT(caca_get_display_width(_SELF));
79}
80
81static VALUE get_height(VALUE self)
82{
83    return NUM2UINT(caca_get_display_height(_SELF));
84}
85
86static VALUE set_title(VALUE self, VALUE t)
87{
88    if(caca_set_display_title(_SELF, StringValuePtr(t))<0)
89    {
90        rb_raise(rb_eRuntimeError, strerror(errno));
91    }
92    return t;
93}
94
95static VALUE set_title2(VALUE self, VALUE t)
96{
97    set_title(self, t);
98    return self;
99}
100
101static VALUE get_mouse_x(VALUE self)
102{
103    return NUM2UINT(caca_get_mouse_x(_SELF));
104}
105
106static VALUE get_mouse_y(VALUE self)
107{
108    return NUM2UINT(caca_get_mouse_y(_SELF));
109}
110
111static VALUE set_mouse(VALUE self, VALUE visible)
112{
113    caca_set_display_time(_SELF, visible);
114    return visible;
115}
116
117static VALUE set_mouse2(VALUE self, VALUE visible)
118{
119    set_mouse(self, visible);
120    return self;
121}
122
123static VALUE get_event(VALUE self, VALUE event_mask, VALUE timeout)
124{
125    caca_event_t ev;
126    if(caca_get_event(_SELF, NUM2UINT(event_mask), &ev, NUM2INT(timeout)) == 0)
127    {
128        return Qnil;
129    }
130    //FIXME
131    return Qnil;
132}
133
134void Init_caca_display(VALUE mCaca)
135{
136    cDisplay = rb_define_class_under(mCaca, "Display", rb_cObject);
137    rb_define_alloc_func(cDisplay, display_alloc);
138
139    rb_define_method(cDisplay, "initialize", display_initialize, 1);
140    rb_define_method(cDisplay, "refresh", display_refresh, 0);
141    rb_define_method(cDisplay, "time=", set_time, 1);
142    rb_define_method(cDisplay, "set_time", set_time2, 1);
143    rb_define_method(cDisplay, "time", get_time, 0);
144    rb_define_method(cDisplay, "width", get_width, 0);
145    rb_define_method(cDisplay, "height", get_height, 0);
146    rb_define_method(cDisplay, "title=", set_title, 1);
147    rb_define_method(cDisplay, "set_title", set_title2, 1);
148    rb_define_method(cDisplay, "mouse_x", get_mouse_x, 0);
149    rb_define_method(cDisplay, "mouse_y", get_mouse_y, 0);
150    rb_define_method(cDisplay, "mouse=", set_mouse, 1);
151    rb_define_method(cDisplay, "set_mouse", set_mouse2, 1);
152    rb_define_method(cDisplay, "get_event", get_event, 3);
153}
Note: See TracBrowser for help on using the repository browser.