c9de10a8c14fedc7f82963683f5b9cdd872c394a
[profile/ivi/ecore.git] / src / lib / ecore / ecore_job.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #include <stdlib.h>
6
7 #include "Ecore.h"
8 #include "ecore_private.h"
9
10 static Eina_Bool _ecore_job_event_handler(void *data, int type, void *ev);
11 static void _ecore_job_event_free(void *data, void *ev);
12
13 static int ecore_event_job_type = 0;
14 static Ecore_Event_Handler* _ecore_job_handler = NULL;
15
16 struct _Ecore_Job
17 {
18    ECORE_MAGIC;
19    Ecore_Event  *event;
20    Ecore_Cb func;
21    void         *data;
22 };
23
24 void
25 _ecore_job_init(void)
26 {
27    ecore_event_job_type = ecore_event_type_new();
28    _ecore_job_handler = ecore_event_handler_add(ecore_event_job_type, _ecore_job_event_handler, NULL);
29 }
30
31 void
32 _ecore_job_shutdown(void)
33 {
34    ecore_event_handler_del(_ecore_job_handler);
35    _ecore_job_handler = NULL;
36 }
37
38 /**
39  * @addtogroup Ecore_Group Ecore - Main Loop and Job Functions.
40  *
41  * @{
42  */
43
44 /**
45  * @addtogroup Ecore_Job_Group Ecore Job functions
46  *
47  * You can queue jobs that are to be done by the main loop when the current
48  * event is dealt with.
49  *
50  * @{
51  */
52
53 /**
54  * Add a job to the event queue.
55  * @param   func The function to call when the job gets handled.
56  * @param   data Data pointer to be passed to the job function when the job is
57  *               handled.
58  * @return  The handle of the job.  @c NULL is returned if the job could not be
59  *          added to the queue.
60  * @note    Once the job has been executed, the job handle is invalid.
61  */
62 EAPI Ecore_Job *
63 ecore_job_add(Ecore_Cb func, const void *data)
64 {
65    Ecore_Job *job;
66    
67    if (!func) return NULL;
68
69    job = calloc(1, sizeof(Ecore_Job));
70    if (!job) return NULL;
71    ECORE_MAGIC_SET(job, ECORE_MAGIC_JOB);
72    job->event = ecore_event_add(ecore_event_job_type, job, _ecore_job_event_free, NULL);
73    if (!job->event)
74      {
75         free(job);
76         return NULL;
77      }
78    job->func = func;
79    job->data = (void *)data;
80    return job;
81 }
82
83 /**
84  * Delete a queued job that has not yet been executed.
85  * @param   job  Handle of the job to delete.
86  * @return  The data pointer that was to be passed to the job.
87  */
88 EAPI void *
89 ecore_job_del(Ecore_Job *job)
90 {
91    void *data;
92    
93    if (!ECORE_MAGIC_CHECK(job, ECORE_MAGIC_JOB))
94      {
95         ECORE_MAGIC_FAIL(job, ECORE_MAGIC_JOB,
96                          "ecore_job_del");
97         return NULL;
98      }
99    data = job->data;
100    ECORE_MAGIC_SET(job, ECORE_MAGIC_NONE);
101    ecore_event_del(job->event);
102    return data;
103 }
104
105 /**
106  * @}
107  */
108
109 /**
110  * @}
111  */
112
113 static Eina_Bool
114 _ecore_job_event_handler(void *data __UNUSED__, int type __UNUSED__, void *ev)
115 {
116    Ecore_Job *job;
117    
118    job = ev;
119    job->func(job->data);
120    return ECORE_CALLBACK_CANCEL;
121 }
122
123 static void
124 _ecore_job_event_free(void *data __UNUSED__, void *ev)
125 {
126    free(ev);
127 }