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

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