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

Last change on this file since 2092 was 2092, checked in by Pascal Terjan, 12 years ago
  • Allow calling Caca::Display.new without parameter
  • Property svn:eol-style set to native
File size: 5.7 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(int argc, VALUE* argv, VALUE self)
34{
35    caca_display_t *display;
36    VALUE cv;
37
38    rb_scan_args(argc, argv, "01", &cv);
39
40    if(NIL_P(cv))
41    {
42        display = caca_create_display(NULL);
43    }
44    else
45    {
46        if(CLASS_OF(cv) != cCanvas)
47        {
48            rb_raise(rb_eArgError, "Argument is not a Cucul::Canvas");
49        }
50
51        display = caca_create_display(DATA_PTR(cv));
52    }
53
54    if(display == NULL)
55    {
56        rb_raise(rb_eRuntimeError, strerror(errno));
57    }
58
59    _SELF = display;
60
61    return self;
62}
63
64static VALUE display_refresh(VALUE self)
65{
66    caca_refresh_display(_SELF);
67    return self;
68}
69
70static VALUE set_time(VALUE self, VALUE t)
71{
72    caca_set_display_time(_SELF, UINT2NUM(t));
73    return t;
74}
75
76static VALUE set_time2(VALUE self, VALUE t)
77{
78    set_time(self, t);
79    return self;
80}
81
82static VALUE get_time(VALUE self)
83{
84    return NUM2UINT(caca_get_display_time(_SELF));
85}
86
87static VALUE get_width(VALUE self)
88{
89    return NUM2UINT(caca_get_display_width(_SELF));
90}
91
92static VALUE get_height(VALUE self)
93{
94    return NUM2UINT(caca_get_display_height(_SELF));
95}
96
97static VALUE set_title(VALUE self, VALUE t)
98{
99    if(caca_set_display_title(_SELF, StringValuePtr(t))<0)
100    {
101        rb_raise(rb_eRuntimeError, strerror(errno));
102    }
103    return t;
104}
105
106static VALUE set_title2(VALUE self, VALUE t)
107{
108    set_title(self, t);
109    return self;
110}
111
112static VALUE get_mouse_x(VALUE self)
113{
114    return NUM2UINT(caca_get_mouse_x(_SELF));
115}
116
117static VALUE get_mouse_y(VALUE self)
118{
119    return NUM2UINT(caca_get_mouse_y(_SELF));
120}
121
122static VALUE set_mouse(VALUE self, VALUE visible)
123{
124    caca_set_display_time(_SELF, visible);
125    return visible;
126}
127
128static VALUE set_mouse2(VALUE self, VALUE visible)
129{
130    set_mouse(self, visible);
131    return self;
132}
133
134static VALUE get_event(VALUE self, VALUE event_mask, VALUE timeout)
135{
136    char utf8[8];
137    caca_event_t ev;
138    VALUE e;
139
140    event_mask = rb_funcall(event_mask, rb_intern("to_i"), 0);
141
142    if(caca_get_event(_SELF, NUM2UINT(event_mask), &ev, NUM2INT(timeout)) == 0)
143    {
144        return Qnil;
145    }
146
147    switch(caca_get_event_type(&ev))
148    {
149    case CACA_EVENT_KEY_PRESS:
150        caca_get_event_key_utf8(&ev, utf8);
151        e = rb_funcall(cEventKeyPress, rb_intern("new"), 3,
152                       UINT2NUM(caca_get_event_key_ch(&ev)),
153                       ULONG2NUM(caca_get_event_key_utf32(&ev)),
154                       rb_str_new(utf8, 8));
155        break;
156    case CACA_EVENT_KEY_RELEASE:
157        caca_get_event_key_utf8(&ev, utf8);
158        e = rb_funcall(cEventKeyRelease, rb_intern("new"), 3,
159                       UINT2NUM(caca_get_event_key_ch(&ev)),
160                       ULONG2NUM(caca_get_event_key_utf32(&ev)),
161                       rb_str_new(utf8, 8));
162        break;
163    case CACA_EVENT_MOUSE_PRESS:
164        e = rb_funcall(cEventMousePress, rb_intern("new"), 3,
165                       UINT2NUM(caca_get_event_mouse_x(&ev)),
166                       UINT2NUM(caca_get_event_mouse_y(&ev)),
167                       UINT2NUM(caca_get_event_mouse_button(&ev)));
168        break;
169    case CACA_EVENT_MOUSE_RELEASE:
170        e = rb_funcall(cEventMouseRelease, rb_intern("new"), 3,
171                       UINT2NUM(caca_get_event_mouse_x(&ev)),
172                       UINT2NUM(caca_get_event_mouse_y(&ev)),
173                       UINT2NUM(caca_get_event_mouse_button(&ev)));
174        break;
175    case CACA_EVENT_MOUSE_MOTION:
176        e = rb_funcall(cEventMouseMotion, rb_intern("new"), 3,
177                       UINT2NUM(caca_get_event_mouse_x(&ev)),
178                       UINT2NUM(caca_get_event_mouse_y(&ev)),
179                       Qnil);
180        break;
181    case CACA_EVENT_RESIZE:
182        e = rb_funcall(cEventResize, rb_intern("new"), 2,
183                       UINT2NUM(caca_get_event_resize_width(&ev)),
184                       UINT2NUM(caca_get_event_resize_height(&ev)));
185        break;
186    case CACA_EVENT_QUIT:
187        e = rb_funcall(cEventQuit, rb_intern("new"), 0);
188        break;
189    default:
190        rb_raise(rb_eRuntimeError, "Invalid event received !");
191    }
192
193    return e;
194}
195
196void Init_caca_display(VALUE mCaca)
197{
198    cDisplay = rb_define_class_under(mCaca, "Display", rb_cObject);
199    rb_define_alloc_func(cDisplay, display_alloc);
200
201    rb_define_method(cDisplay, "initialize", display_initialize, -1);
202    rb_define_method(cDisplay, "refresh", display_refresh, 0);
203    rb_define_method(cDisplay, "time=", set_time, 1);
204    rb_define_method(cDisplay, "set_time", set_time2, 1);
205    rb_define_method(cDisplay, "time", get_time, 0);
206    rb_define_method(cDisplay, "width", get_width, 0);
207    rb_define_method(cDisplay, "height", get_height, 0);
208    rb_define_method(cDisplay, "title=", set_title, 1);
209    rb_define_method(cDisplay, "set_title", set_title2, 1);
210    rb_define_method(cDisplay, "mouse_x", get_mouse_x, 0);
211    rb_define_method(cDisplay, "mouse_y", get_mouse_y, 0);
212    rb_define_method(cDisplay, "mouse=", set_mouse, 1);
213    rb_define_method(cDisplay, "set_mouse", set_mouse2, 1);
214    rb_define_method(cDisplay, "get_event", get_event, 2);
215}
Note: See TracBrowser for help on using the repository browser.