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