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

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