Browse Source

- Finish Caca::Event (a.k.a. J'ai fait caca aujourd'hui)

tags/v0.99.beta14
Pascal Terjan pterjan 17 years ago
parent
commit
117a70f1ee
5 changed files with 141 additions and 3 deletions
  1. +49
    -3
      ruby/caca-display.c
  2. +50
    -0
      ruby/caca-event.c
  3. +8
    -0
      ruby/caca-event.h
  4. +2
    -0
      ruby/caca.c
  5. +32
    -0
      ruby/lib/caca.rb

+ 49
- 3
ruby/caca-display.c View File

@@ -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);
}

+ 50
- 0
ruby/caca-event.c View File

@@ -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));

}

+ 8
- 0
ruby/caca-event.h View File

@@ -4,6 +4,14 @@
#include <ruby.h>

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

+ 2
- 0
ruby/caca.c View File

@@ -13,10 +13,12 @@
#include <caca.h>

#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);
}

+ 32
- 0
ruby/lib/caca.rb View File

@@ -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

Loading…
Cancel
Save