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

Last change on this file since 2051 was 2051, checked in by Sam Hocevar, 12 years ago
  • Adapt the Ruby bindings to the event API change.
  • Property svn:eol-style set to native
File size: 5.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    char utf8[8];
126    caca_event_t ev;
127    VALUE e;
128
129    event_mask = rb_funcall(event_mask, rb_intern("to_i"), 0);
130
131    if(caca_get_event(_SELF, NUM2UINT(event_mask), &ev, NUM2INT(timeout)) == 0)
132    {
133        return Qnil;
134    }
135
136    switch(caca_get_event_type(&ev))
137    {
138    case CACA_EVENT_KEY_PRESS:
139        caca_get_event_key_utf8(&ev, utf8);
140        e = rb_funcall(cEventKeyPress, rb_intern("new"), 3,
141                       UINT2NUM(caca_get_event_key_ch(&ev)),
142                       ULONG2NUM(caca_get_event_key_utf32(&ev)),
143                       rb_str_new(utf8, 8));
144        break;
145    case CACA_EVENT_KEY_RELEASE:
146        caca_get_event_key_utf8(&ev, utf8);
147        e = rb_funcall(cEventKeyRelease, rb_intern("new"), 3,
148                       UINT2NUM(caca_get_event_key_ch(&ev)),
149                       ULONG2NUM(caca_get_event_key_utf32(&ev)),
150                       rb_str_new(utf8, 8));
151        break;
152    case CACA_EVENT_MOUSE_PRESS:
153        e = rb_funcall(cEventMousePress, rb_intern("new"), 3,
154                       UINT2NUM(caca_get_event_mouse_x(&ev)),
155                       UINT2NUM(caca_get_event_mouse_y(&ev)),
156                       UINT2NUM(caca_get_event_mouse_button(&ev)));
157        break;
158    case CACA_EVENT_MOUSE_RELEASE:
159        e = rb_funcall(cEventMouseRelease, rb_intern("new"), 3,
160                       UINT2NUM(caca_get_event_mouse_x(&ev)),
161                       UINT2NUM(caca_get_event_mouse_y(&ev)),
162                       UINT2NUM(caca_get_event_mouse_button(&ev)));
163        break;
164    case CACA_EVENT_MOUSE_MOTION:
165        e = rb_funcall(cEventMouseMotion, rb_intern("new"), 3,
166                       UINT2NUM(caca_get_event_mouse_x(&ev)),
167                       UINT2NUM(caca_get_event_mouse_y(&ev)),
168                       Qnil);
169        break;
170    case CACA_EVENT_RESIZE:
171        e = rb_funcall(cEventResize, rb_intern("new"), 2,
172                       UINT2NUM(caca_get_event_resize_width(&ev)),
173                       UINT2NUM(caca_get_event_resize_height(&ev)));
174        break;
175    case CACA_EVENT_QUIT:
176        e = rb_funcall(cEventQuit, rb_intern("new"), 0);
177        break;
178    default:
179        rb_raise(rb_eRuntimeError, "Invalid event received !");
180    }
181
182    return e;
183}
184
185void Init_caca_display(VALUE mCaca)
186{
187    cDisplay = rb_define_class_under(mCaca, "Display", rb_cObject);
188    rb_define_alloc_func(cDisplay, display_alloc);
189
190    rb_define_method(cDisplay, "initialize", display_initialize, 1);
191    rb_define_method(cDisplay, "refresh", display_refresh, 0);
192    rb_define_method(cDisplay, "time=", set_time, 1);
193    rb_define_method(cDisplay, "set_time", set_time2, 1);
194    rb_define_method(cDisplay, "time", get_time, 0);
195    rb_define_method(cDisplay, "width", get_width, 0);
196    rb_define_method(cDisplay, "height", get_height, 0);
197    rb_define_method(cDisplay, "title=", set_title, 1);
198    rb_define_method(cDisplay, "set_title", set_title2, 1);
199    rb_define_method(cDisplay, "mouse_x", get_mouse_x, 0);
200    rb_define_method(cDisplay, "mouse_y", get_mouse_y, 0);
201    rb_define_method(cDisplay, "mouse=", set_mouse, 1);
202    rb_define_method(cDisplay, "set_mouse", set_mouse2, 1);
203    rb_define_method(cDisplay, "get_event", get_event, 2);
204}
Note: See TracBrowser for help on using the repository browser.