diff --git a/ruby/caca-display.c b/ruby/caca-display.c index ac20527..fd69faa 100644 --- a/ruby/caca-display.c +++ b/ruby/caca-display.c @@ -123,12 +123,58 @@ static VALUE set_mouse2(VALUE self, VALUE visible) static VALUE get_event(VALUE self, VALUE event_mask, VALUE timeout) { caca_event_t ev; + VALUE e; + if(caca_get_event(_SELF, NUM2UINT(event_mask), &ev, NUM2INT(timeout)) == 0) { return Qnil; } - //FIXME - return Qnil; + + switch(ev.type) + { + case CACA_EVENT_KEY_PRESS: + e = rb_funcall(cEventKeyPress, rb_intern("new"), 3, + UINT2NUM(ev.data.key.ch), + ULONG2NUM(ev.data.key.utf32), + rb_str_new(ev.data.key.utf8, 8)); + break; + case CACA_EVENT_KEY_RELEASE: + e = rb_funcall(cEventKeyRelease, rb_intern("new"), 3, + UINT2NUM(ev.data.key.ch), + ULONG2NUM(ev.data.key.utf32), + rb_str_new(ev.data.key.utf8, 8)); + break; + case CACA_EVENT_MOUSE_PRESS: + e = rb_funcall(cEventMousePress, rb_intern("new"), 3, + UINT2NUM(ev.data.mouse.x), + UINT2NUM(ev.data.mouse.y), + UINT2NUM(ev.data.mouse.button)); + break; + case CACA_EVENT_MOUSE_RELEASE: + e = rb_funcall(cEventMouseRelease, rb_intern("new"), 3, + UINT2NUM(ev.data.mouse.x), + UINT2NUM(ev.data.mouse.y), + UINT2NUM(ev.data.mouse.button)); + break; + case CACA_EVENT_MOUSE_MOTION: + e = rb_funcall(cEventMouseMotion, rb_intern("new"), 3, + UINT2NUM(ev.data.mouse.x), + UINT2NUM(ev.data.mouse.y), + Qnil); + break; + case CACA_EVENT_RESIZE: + e = rb_funcall(cEventResize, rb_intern("new"), 2, + UINT2NUM(ev.data.resize.w), + UINT2NUM(ev.data.resize.h)); + break; + case CACA_EVENT_QUIT: + e = rb_funcall(cEventQuit, rb_intern("new"), 0); + break; + default: + rb_raise(rb_eRuntimeError, "Invalid event received !"); + } + + return e; } void Init_caca_display(VALUE mCaca) @@ -149,5 +195,5 @@ void Init_caca_display(VALUE mCaca) rb_define_method(cDisplay, "mouse_y", get_mouse_y, 0); rb_define_method(cDisplay, "mouse=", set_mouse, 1); rb_define_method(cDisplay, "set_mouse", set_mouse2, 1); - rb_define_method(cDisplay, "get_event", get_event, 3); + rb_define_method(cDisplay, "get_event", get_event, 2); } diff --git a/ruby/caca-event.c b/ruby/caca-event.c index fbfe108..d5ae0da 100644 --- a/ruby/caca-event.c +++ b/ruby/caca-event.c @@ -15,8 +15,58 @@ #include "common.h" VALUE cEvent; +static VALUE cEventKey; +VALUE cEventKeyPress; +VALUE cEventKeyRelease; +static VALUE cEventMouse; +VALUE cEventMousePress; +VALUE cEventMouseRelease; +VALUE cEventMouseMotion; +VALUE cEventResize; +VALUE cEventQuit; void Init_caca_event(VALUE mCaca) { cEvent = rb_define_class_under(mCaca, "Event", rb_cObject); + rb_define_const(cEvent, "TYPE", INT2FIX(CACA_EVENT_ANY)); + + cEventKey = rb_define_class_under(cEvent, "Key", cEvent); + rb_define_const(cEventKey, "TYPE", + INT2FIX(CACA_EVENT_KEY_PRESS| + CACA_EVENT_KEY_RELEASE)); + + cEventKeyPress = rb_define_class_under(cEventKey, "Press", cEventKey); + rb_define_const(cEventKeyPress, "TYPE", + INT2FIX(CACA_EVENT_KEY_PRESS)); + + cEventKeyRelease = rb_define_class_under(cEventKey, "Release", cEventKey); + rb_define_const(cEventKeyRelease, "TYPE", + INT2FIX(CACA_EVENT_KEY_RELEASE)); + + cEventMouse = rb_define_class_under(cEvent, "Mouse", cEvent); + rb_define_const(cEventMouse, "TYPE", + INT2FIX(CACA_EVENT_MOUSE_PRESS| + CACA_EVENT_MOUSE_RELEASE| + CACA_EVENT_MOUSE_MOTION)); + + cEventMousePress = rb_define_class_under(cEventMouse, "Press", cEventMouse); + rb_define_const(cEventMousePress, "TYPE", + INT2FIX(CACA_EVENT_MOUSE_PRESS)); + + cEventMouseRelease = rb_define_class_under(cEventMouse, "Release", cEventMouse); + rb_define_const(cEventMouseRelease, "TYPE", + INT2FIX(CACA_EVENT_MOUSE_RELEASE)); + + cEventMouseMotion = rb_define_class_under(cEventMouse, "Motion", cEventMouse); + rb_define_const(cEventMouseMotion, "TYPE", + INT2FIX(CACA_EVENT_MOUSE_MOTION)); + + cEventResize = rb_define_class_under(cEvent, "Resize", cEvent); + rb_define_const(cEventResize, "TYPE", + INT2FIX(CACA_EVENT_RESIZE)); + + cEventQuit = rb_define_class_under(cEvent, "Quit", cEvent); + rb_define_const(cEventQuit, "TYPE", + INT2FIX(CACA_EVENT_QUIT)); + } diff --git a/ruby/caca-event.h b/ruby/caca-event.h index 5b11fca..7947007 100644 --- a/ruby/caca-event.h +++ b/ruby/caca-event.h @@ -4,6 +4,14 @@ #include extern VALUE cEvent; +extern VALUE cEventKeyPress; +extern VALUE cEventKeyRelease; +extern VALUE cEventMouse; +extern VALUE cEventMousePress; +extern VALUE cEventMouseRelease; +extern VALUE cEventMouseMotion; +extern VALUE cEventResize; +extern VALUE cEventQuit; extern void Init_caca_event(VALUE); #endif diff --git a/ruby/caca.c b/ruby/caca.c index 11cf430..c19b6dd 100644 --- a/ruby/caca.c +++ b/ruby/caca.c @@ -13,10 +13,12 @@ #include #include "caca-display.h" +#include "caca-event.h" void Init_caca() { VALUE mCaca = rb_define_module("Caca"); Init_caca_display(mCaca); + Init_caca_event(mCaca); } diff --git a/ruby/lib/caca.rb b/ruby/lib/caca.rb new file mode 100644 index 0000000..8f4edbe --- /dev/null +++ b/ruby/lib/caca.rb @@ -0,0 +1,32 @@ +require 'cucul' +require 'caca.so' + +module Caca + class Event + def Event.to_i + const_get("TYPE") + end + def Event.|(i) + i = i.to_i + const_get("TYPE")|i + end + class Key + attr_reader :ch, :utf32, :utf8 + def initialize(ch, utf32, utf8) + @ch, @utf32, @utf8 = ch, utf32, utf8 + end + end + class Mouse + attr_reader :x, :y, :button + def initialize(x, y, button) + @x, @y, @button = x, y, button + end + end + class Resize + attr_reader :w, :h + def initialize(w, h) + @w, @h = w, h + end + end + end +end