Initial commit.
[ruby-ecore.git] / src / ecore_job / rb_job.c
1 /*
2  * $Id$
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 <Ecore_Job.h>
24 #include <stdbool.h>
25
26 #include "rb_ecore_job.h"
27
28 typedef struct {
29         Ecore_Job *job;
30         void *cb;
31         bool deleted;
32 } RbEcoreJob;
33
34 VALUE cEcoreJob;
35
36 static void on_job (void *data)
37 {
38         RbEcoreJob *job = data;
39
40         rb_funcall ((VALUE) job->cb, rb_intern ("call"), 0);
41         job->deleted = true;
42 }
43
44 static VALUE c_init (VALUE self)
45 {
46         RbEcoreJob *job = NULL;
47
48         Data_Get_Struct (self, RbEcoreJob, job);
49
50         job->cb = (void *) rb_block_proc ();
51         job->job = ecore_job_add (on_job, job);
52
53         return self;
54 }
55
56 static void c_free (RbEcoreJob *job)
57 {
58         if (job->job && !job->deleted)
59                 ecore_job_del (job->job);
60
61         free (job);
62 }
63
64 static VALUE c_new (VALUE klass)
65 {
66         VALUE self;
67         RbEcoreJob *job;
68
69         self = Data_Make_Struct (klass, RbEcoreJob, NULL, c_free, job);
70
71         rb_obj_call_init (self, 0, NULL);
72
73         return self;
74 }
75
76 static VALUE c_delete (VALUE self)
77 {
78         VALUE ret = Qfalse;
79         RbEcoreJob *job = NULL;
80
81         Data_Get_Struct (self, RbEcoreJob, job);
82
83         if (job->job && !job->deleted) {
84                 ecore_job_del (job->job);
85                 job->deleted = true;
86                 job->job = NULL;
87                 ret = Qtrue;
88         }
89
90         return ret;
91 }
92
93 void Init_Job (void)
94 {
95         cEcoreJob = rb_define_class_under (mJob, "Job", rb_cObject);
96
97         rb_define_singleton_method (cEcoreJob, "new", c_new, 0);
98         rb_define_method (cEcoreJob, "initialize", c_init, 0);
99         rb_define_method (cEcoreJob, "delete", c_delete, 0);
100 }
101