From 99140ead7c84b84bf9fd14b224e2f0e94dea11b6 Mon Sep 17 00:00:00 2001 From: Pascal Terjan Date: Sun, 18 Nov 2007 12:43:48 +0000 Subject: [PATCH] * Add Caca and Caca::Display --- ruby/Makefile.am | 7 ++- ruby/caca-display.c | 114 ++++++++++++++++++++++++++++++++++++++++++++ ruby/caca-display.h | 9 ++++ ruby/caca.c | 22 +++++++++ 4 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 ruby/caca-display.c create mode 100644 ruby/caca-display.h create mode 100644 ruby/caca.c diff --git a/ruby/Makefile.am b/ruby/Makefile.am index 0754492..b213b6f 100644 --- a/ruby/Makefile.am +++ b/ruby/Makefile.am @@ -2,7 +2,7 @@ rubylibdir=$(RUBY_SITEARCHDIR) if USE_RUBY -rubylib_LTLIBRARIES = cucul.la +rubylib_LTLIBRARIES = caca.la cucul.la TESTS = test endif @@ -11,6 +11,11 @@ cucul_la_SOURCES = cucul.c cucul-canvas.c cucul-dither.c cucul-font.c cucul_la_LDFLAGS = -module -avoid-version -shared -L$(RUBY_LIBDIR) -l$(RUBY_SO_NAME) cucul_la_LIBADD = ../cucul/libcucul.la +caca_la_CPPFLAGS = -I$(top_srcdir)/caca -I$(RUBY_ARCHDIR) +caca_la_SOURCES = caca.c caca-display.c +caca_la_LDFLAGS = -module -avoid-version -shared -L$(RUBY_LIBDIR) -l$(RUBY_SO_NAME) +caca_la_LIBADD = ../caca/libcaca.la + EXTRA_DIST = cucul-canvas.h \ cucul-dither.h \ cucul-font.h \ diff --git a/ruby/caca-display.c b/ruby/caca-display.c new file mode 100644 index 0000000..d02d523 --- /dev/null +++ b/ruby/caca-display.c @@ -0,0 +1,114 @@ +/* + * libcaca Ruby bindings + * Copyright (c) 2007 Pascal Terjan + * + * This library is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. + */ + +#include +#include +#include +#include "cucul-canvas.h" +#include "common.h" + +VALUE cDisplay; + +void display_free(void *display) +{ + caca_free_display((caca_display_t *)display); +} + +static VALUE display_alloc(VALUE klass) +{ + VALUE obj; + obj = Data_Wrap_Struct(klass, 0, display_free, NULL); + return obj; +} + +static VALUE display_initialize(VALUE self, VALUE cv) +{ + caca_display_t *display; + + if(CLASS_OF(cv) != cCanvas) + { + rb_raise(rb_eArgError, "Argument is not a Cucul::Canvas"); + } + + display = caca_create_display(DATA_PTR(cv)); + if(display == NULL) + { + rb_raise(rb_eRuntimeError, strerror(errno)); + } + + _SELF = display; + + return self; +} + +static VALUE display_refresh(VALUE self) +{ + caca_refresh_display(_SELF); + return self; +} + +static VALUE set_time(VALUE self, VALUE t) +{ + caca_set_display_time(_SELF, UINT2NUM(t)); + return t; +} + +static VALUE set_time2(VALUE self, VALUE t) +{ + set_time(self, t); + return self; +} + +static VALUE get_time(VALUE self) +{ + return NUM2UINT(caca_get_display_time(_SELF)); +} + +static VALUE get_width(VALUE self) +{ + return NUM2UINT(caca_get_display_width(_SELF)); +} + +static VALUE get_height(VALUE self) +{ + return NUM2UINT(caca_get_display_height(_SELF)); +} + +static VALUE set_title(VALUE self, VALUE t) +{ + if(caca_set_display_title(_SELF, StringValuePtr(t))<0) + { + rb_raise(rb_eRuntimeError, strerror(errno)); + } + return t; +} + +static VALUE set_title2(VALUE self, VALUE t) +{ + set_title(self, t); + return self; +} + +void Init_caca_display(VALUE mCaca) +{ + cDisplay = rb_define_class_under(mCaca, "Display", rb_cObject); + rb_define_alloc_func(cDisplay, display_alloc); + + rb_define_method(cDisplay, "initialize", display_initialize, 1); + rb_define_method(cDisplay, "refresh", display_refresh, 0); + rb_define_method(cDisplay, "time=", set_time, 1); + rb_define_method(cDisplay, "set_time", set_time2, 1); + rb_define_method(cDisplay, "time", get_time, 0); + rb_define_method(cDisplay, "width", get_width, 0); + rb_define_method(cDisplay, "height", get_height, 0); + rb_define_method(cDisplay, "title=", set_title, 1); + rb_define_method(cDisplay, "set_title", set_title2, 1); +} diff --git a/ruby/caca-display.h b/ruby/caca-display.h new file mode 100644 index 0000000..a4c2634 --- /dev/null +++ b/ruby/caca-display.h @@ -0,0 +1,9 @@ +#ifndef __CACA_DISPLAY_H__ +#define __CACA_DISPLAY_H__ + +#include + +extern VALUE cDisplay; +extern void Init_caca_display(VALUE); + +#endif diff --git a/ruby/caca.c b/ruby/caca.c new file mode 100644 index 0000000..11cf430 --- /dev/null +++ b/ruby/caca.c @@ -0,0 +1,22 @@ +/* + * libcaca Ruby bindings + * Copyright (c) 2007 Pascal Terjan + * + * This library is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. + */ + +#include +#include + +#include "caca-display.h" + +void Init_caca() +{ + VALUE mCaca = rb_define_module("Caca"); + + Init_caca_display(mCaca); +}