455e89b7e13c1da1b71d70207c4ca9ec33ff9015
[ruby-edje.git] / src / rb_messages.c
1 /*
2  * $Id: rb_messages.c 153 2004-12-09 18:40:44Z tilman $
3  *
4  * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <ruby.h>
22
23 #include <Edje.h>
24
25 #define __RB_MESSAGES_C
26 #include "rb_edje_main.h"
27 #include "rb_edje.h"
28 #include "rb_messages.h"
29
30 #define DEF_CONST(mod, prefix, name) \
31         rb_define_const ((mod), #name, \
32                          INT2FIX (prefix##name));
33
34 VALUE cMsg, cStrMsg, cIntMsg, cFloatMsg;
35
36 static VALUE c_msg_init (VALUE self)
37 {
38         rb_iv_set (self, "@id", UINT2NUM (0));
39
40         return self;
41 }
42
43 static VALUE c_str_msg_init (VALUE self, VALUE str)
44 {
45         Check_Type (str, T_STRING);
46
47         rb_call_super (0, NULL);
48
49         rb_iv_set (self, "@type", UINT2NUM (EDJE_MESSAGE_STRING));
50         rb_iv_set (self, "@value", str);
51
52         return self;
53 }
54
55 static VALUE c_str_msg_serialize (VALUE self)
56 {
57         static volatile Edje_Message_String ret;
58         VALUE s;
59
60         s = rb_iv_get (self, "@value");
61         ret.str = StringValuePtr (s);
62
63         return (VALUE) &ret;
64 }
65
66 static VALUE c_int_msg_init (VALUE self, VALUE val)
67 {
68         Check_Type (val, T_FIXNUM);
69
70         rb_call_super (0, NULL);
71
72         rb_iv_set (self, "@type", UINT2NUM (EDJE_MESSAGE_INT));
73         rb_iv_set (self, "@value", val);
74
75         return self;
76 }
77
78 static VALUE c_int_msg_serialize (VALUE self)
79 {
80         static volatile Edje_Message_Int ret;
81         VALUE i;
82
83         i = rb_iv_get (self, "@value");
84         ret.val = NUM2INT (i);
85
86         return (VALUE) &ret;
87 }
88
89 static VALUE c_float_msg_init (VALUE self, VALUE val)
90 {
91         Check_Type (val, T_FLOAT);
92
93         rb_call_super (0, NULL);
94
95         rb_iv_set (self, "@type", UINT2NUM (EDJE_MESSAGE_FLOAT));
96         rb_iv_set (self, "@value", val);
97
98         return self;
99 }
100
101 static VALUE c_float_msg_serialize (VALUE self)
102 {
103         static volatile Edje_Message_Float ret;
104         VALUE f;
105
106         f = rb_iv_get (self, "@value");
107         ret.val = NUM2DBL (f);
108
109         return (VALUE) &ret;
110 }
111
112 void Init_Messages (void)
113 {
114         cMsg = rb_define_class_under (mEdje, "Message", rb_cObject);
115
116         rb_define_method (cMsg, "initialize", c_msg_init, 0);
117
118         rb_define_attr (cMsg, "type", 1, 0);
119         rb_define_attr (cMsg, "id", 1, 1);
120         rb_define_attr (cMsg, "value", 1, 1);
121
122         /* StringMessage */
123         cStrMsg = rb_define_class_under (mEdje, "StringMessage", cMsg);
124         rb_define_method (cStrMsg, "initialize", c_str_msg_init, 1);
125         rb_define_method (cStrMsg, "serialize", c_str_msg_serialize, 0);
126
127         /* IntMessage */
128         cIntMsg = rb_define_class_under (mEdje, "IntMessage", cMsg);
129         rb_define_method (cIntMsg, "initialize", c_int_msg_init, 1);
130         rb_define_method (cIntMsg, "serialize", c_int_msg_serialize, 0);
131
132         /* FloatMessage */
133         cFloatMsg = rb_define_class_under (mEdje, "FloatMessage", cMsg);
134         rb_define_method (cFloatMsg, "initialize", c_float_msg_init, 1);
135         rb_define_method (cFloatMsg, "serialize", c_float_msg_serialize, 0);
136 }