a40227ec275b04c01b6e495bc05e07ba306f76a0
[ruby-edje.git] / src / rb_messages.c
1 /*
2  * $Id: rb_messages.c 329 2005-04-28 10:41:17Z 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;
35 static VALUE cStrMsg, cIntMsg, cFloatMsg;
36
37 static VALUE c_msg_init (VALUE self)
38 {
39         rb_iv_set (self, "@id", UINT2NUM (0));
40
41         return self;
42 }
43
44 static VALUE c_str_msg_init (VALUE self, VALUE str)
45 {
46         Check_Type (str, T_STRING);
47
48         rb_call_super (0, NULL);
49
50         rb_iv_set (self, "@type", UINT2NUM (EDJE_MESSAGE_STRING));
51         rb_iv_set (self, "@value", str);
52
53         return self;
54 }
55
56 static VALUE c_str_msg_serialize (VALUE self)
57 {
58         static volatile Edje_Message_String ret;
59         VALUE s;
60
61         s = rb_iv_get (self, "@value");
62         ret.str = StringValuePtr (s);
63
64         return (VALUE) &ret;
65 }
66
67 static VALUE c_int_msg_init (VALUE self, VALUE val)
68 {
69         Check_Type (val, T_FIXNUM);
70
71         rb_call_super (0, NULL);
72
73         rb_iv_set (self, "@type", UINT2NUM (EDJE_MESSAGE_INT));
74         rb_iv_set (self, "@value", val);
75
76         return self;
77 }
78
79 static VALUE c_int_msg_serialize (VALUE self)
80 {
81         static volatile Edje_Message_Int ret;
82         VALUE i;
83
84         i = rb_iv_get (self, "@value");
85         ret.val = NUM2INT (i);
86
87         return (VALUE) &ret;
88 }
89
90 static VALUE c_float_msg_init (VALUE self, VALUE val)
91 {
92         Check_Type (val, T_FLOAT);
93
94         rb_call_super (0, NULL);
95
96         rb_iv_set (self, "@type", UINT2NUM (EDJE_MESSAGE_FLOAT));
97         rb_iv_set (self, "@value", val);
98
99         return self;
100 }
101
102 static VALUE c_float_msg_serialize (VALUE self)
103 {
104         static volatile Edje_Message_Float ret;
105         VALUE f;
106
107         f = rb_iv_get (self, "@value");
108         ret.val = NUM2DBL (f);
109
110         return (VALUE) &ret;
111 }
112
113 void Init_Messages (void)
114 {
115         cMsg = rb_define_class_under (mEdje, "Message", rb_cObject);
116
117         rb_define_method (cMsg, "initialize", c_msg_init, 0);
118
119         rb_define_attr (cMsg, "type", 1, 0);
120         rb_define_attr (cMsg, "id", 1, 1);
121         rb_define_attr (cMsg, "value", 1, 1);
122
123         /* StringMessage */
124         cStrMsg = rb_define_class_under (mEdje, "StringMessage", cMsg);
125         rb_define_method (cStrMsg, "initialize", c_str_msg_init, 1);
126         rb_define_method (cStrMsg, "serialize", c_str_msg_serialize, 0);
127
128         /* IntMessage */
129         cIntMsg = rb_define_class_under (mEdje, "IntMessage", cMsg);
130         rb_define_method (cIntMsg, "initialize", c_int_msg_init, 1);
131         rb_define_method (cIntMsg, "serialize", c_int_msg_serialize, 0);
132
133         /* FloatMessage */
134         cFloatMsg = rb_define_class_under (mEdje, "FloatMessage", cMsg);
135         rb_define_method (cFloatMsg, "initialize", c_float_msg_init, 1);
136         rb_define_method (cFloatMsg, "serialize", c_float_msg_serialize, 0);
137 }