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