Added Ecore::Evas::EcoreEvas#has_alpha? and #has_alpha=.
[ruby-ecore.git] / src / ecore_job / rb_job.c
1 /*
2  * $Id: rb_job.c 351 2006-02-10 15:25:40Z tilman $
3  *
4  * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
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 *real;
31         VALUE callback;
32         bool deleted;
33 } RbJob;
34
35 static void on_job (void *data)
36 {
37         RbJob *job = data;
38
39         rb_funcall (job->callback, rb_intern ("call"), 0);
40         job->deleted = true;
41 }
42
43 static void c_mark (RbJob *job)
44 {
45         rb_gc_mark (job->callback);
46 }
47
48 static void c_free (RbJob *job)
49 {
50         if (job->real && !job->deleted)
51                 ecore_job_del (job->real);
52
53         ecore_shutdown ();
54
55         free (job);
56 }
57
58 static VALUE c_alloc (VALUE klass)
59 {
60         RbJob *job = NULL;
61
62         ecore_init ();
63
64         return Data_Make_Struct (klass, RbJob, c_mark, c_free, job);
65 }
66
67 /*
68  * call-seq:
69  *  Ecore::Job::Job.new { block } => job
70  *
71  * Creates an Ecore::Job::Job object.
72  * After execution, the object will be deleted.
73  */
74 static VALUE c_init (VALUE self)
75 {
76         RbJob *job = NULL;
77
78         if (!rb_block_given_p ())
79                 rb_raise (rb_eStandardError, "block missing");
80
81         Data_Get_Struct (self, RbJob, job);
82
83         job->callback = rb_block_proc ();
84         job->deleted = false;
85         job->real = ecore_job_add (on_job, job);
86
87         return self;
88 }
89
90 /*
91  * call-seq:
92  *  job.delete => nil
93  *
94  * Deletes <i>job</i>.
95  */
96 static VALUE c_delete (VALUE self)
97 {
98         VALUE ret = Qfalse;
99         RbJob *job = NULL;
100
101         Data_Get_Struct (self, RbJob, job);
102
103         if (job->real && !job->deleted) {
104                 ecore_job_del (job->real);
105                 job->deleted = true;
106                 job->real = NULL;
107                 ret = Qtrue;
108         }
109
110         return ret;
111 }
112
113 void Init_Job (void)
114 {
115         VALUE c = rb_define_class_under (mJob, "Job", rb_cObject);
116
117         rb_define_alloc_func (c, c_alloc);
118         rb_define_method (c, "initialize", c_init, 0);
119         rb_define_method (c, "delete", c_delete, 0);
120 }