@@ -123,12 +123,58 @@ static VALUE set_mouse2(VALUE self, VALUE visible) | |||||
static VALUE get_event(VALUE self, VALUE event_mask, VALUE timeout) | static VALUE get_event(VALUE self, VALUE event_mask, VALUE timeout) | ||||
{ | { | ||||
caca_event_t ev; | caca_event_t ev; | ||||
VALUE e; | |||||
if(caca_get_event(_SELF, NUM2UINT(event_mask), &ev, NUM2INT(timeout)) == 0) | if(caca_get_event(_SELF, NUM2UINT(event_mask), &ev, NUM2INT(timeout)) == 0) | ||||
{ | { | ||||
return Qnil; | 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) | 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_y", get_mouse_y, 0); | ||||
rb_define_method(cDisplay, "mouse=", set_mouse, 1); | rb_define_method(cDisplay, "mouse=", set_mouse, 1); | ||||
rb_define_method(cDisplay, "set_mouse", set_mouse2, 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); | |||||
} | } |
@@ -15,8 +15,58 @@ | |||||
#include "common.h" | #include "common.h" | ||||
VALUE cEvent; | 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) | void Init_caca_event(VALUE mCaca) | ||||
{ | { | ||||
cEvent = rb_define_class_under(mCaca, "Event", rb_cObject); | 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)); | |||||
} | } |
@@ -4,6 +4,14 @@ | |||||
#include <ruby.h> | #include <ruby.h> | ||||
extern VALUE cEvent; | 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); | extern void Init_caca_event(VALUE); | ||||
#endif | #endif |
@@ -13,10 +13,12 @@ | |||||
#include <caca.h> | #include <caca.h> | ||||
#include "caca-display.h" | #include "caca-display.h" | ||||
#include "caca-event.h" | |||||
void Init_caca() | void Init_caca() | ||||
{ | { | ||||
VALUE mCaca = rb_define_module("Caca"); | VALUE mCaca = rb_define_module("Caca"); | ||||
Init_caca_display(mCaca); | Init_caca_display(mCaca); | ||||
Init_caca_event(mCaca); | |||||
} | } |
@@ -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 |