-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathphp_midgard_g_mainloop.c
174 lines (141 loc) · 5.43 KB
/
php_midgard_g_mainloop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* Copyright (C) 2007 Piotr Pokora <[email protected]>
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "php_midgard.h"
#include "php_midgard_gobject.h"
#include "php_midgard__helpers.h"
#include <zend_exceptions.h>
/* GObject wrapper for GMainLoop */
#define MIDGARD_TYPE_G_MAIN_LOOP (midgard_g_main_loop_get_type())
#define MIDGARD_G_MAIN_LOOP(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), MIDGARD_TYPE_G_MAIN_LOOP, MidgardGMainLoop))
#define MIDGARD_G_MAIN_LOOP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), MIDGARD_TYPE_G_MAIN_LOOP, MidgardGMainLoopClass))
#define MIDGARD_IS_G_MAIN_LOOP(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), MIDGARD_TYPE_G_MAIN_LOOP))
#define MIDGARD_IS_G_MAIN_LOOP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), MIDGARD_TYPE_G_MAIN_LOOP))
#define MIDGARD_G_MAIN_LOOP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), MIDGARD_TYPE_G_MAIN_LOOP, MidgardGMainLoopClass))
typedef struct _MidgardGMainLoop MidgardGMainLoop;
typedef struct _MidgardGMainLoopClass MidgardGMainLoopClass;
struct _MidgardGMainLoop{
GObject parent;
/* private field */
GMainLoop *g_m_loop;
};
struct _MidgardGMainLoopClass{
GObjectClass parent;
};
GType midgard_g_main_loop_get_type(void);
MidgardGMainLoop *midgard_g_main_loop_new(void)
{
MidgardGMainLoop *self = g_object_new(MIDGARD_TYPE_G_MAIN_LOOP, NULL);
self->g_m_loop = g_main_loop_new(NULL, FALSE);
return self;
}
static void _midgard_g_main_loop_finalize(GObject *object)
{
MidgardGMainLoop *self = (MidgardGMainLoop *) object;
if (self->g_m_loop)
g_main_loop_unref (self->g_m_loop);
self->g_m_loop = NULL;
}
static void _midgard_g_main_loop_class_init(gpointer g_class, gpointer g_class_data)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(g_class);
gobject_class->finalize = _midgard_g_main_loop_finalize;
}
GType midgard_g_main_loop_get_type(void)
{
static GType type = 0;
if (type == 0) {
static const GTypeInfo info = {
sizeof (MidgardGMainLoopClass),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) _midgard_g_main_loop_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (MidgardDbus),
0, /* n_preallocs */
NULL /* instance_init */
};
type = g_type_register_static (G_TYPE_OBJECT, "midgard_g_main_loop", &info, 0);
}
return type;
}
zend_class_entry *php_midgard_g_mainloop_class;
#define _GET_MAINLOOP_OBJECT \
zval *zval_object = getThis(); \
MidgardGMainLoop *mainloop = MIDGARD_G_MAIN_LOOP(__php_gobject_ptr(zval_object)); \
if (!mainloop) \
php_error(E_ERROR, "Can not find underlying main loop instance");
/* Object constructor */
static PHP_METHOD(midgard_g_mainloop, __construct)
{
RETVAL_FALSE;
if (zend_parse_parameters_none() == FAILURE)
return;
zval *zval_object = getThis();
GObject *gobject;
gobject = __php_gobject_ptr(zval_object);
if (!gobject) {
MidgardGMainLoop *mainloop = midgard_g_main_loop_new();
MGD_PHP_SET_GOBJECT(zval_object, mainloop);
}
}
ZEND_BEGIN_ARG_INFO(arginfo_midgard_g_mainloop___construct, 0)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_g_mainloop, run)
{
if (zend_parse_parameters_none() == FAILURE)
return;
_GET_MAINLOOP_OBJECT;
g_main_loop_run(mainloop->g_m_loop);
}
ZEND_BEGIN_ARG_INFO(arginfo_midgard_g_mainloop_run, 0)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_g_mainloop, is_running)
{
RETVAL_FALSE;
if (zend_parse_parameters_none() == FAILURE)
return;
_GET_MAINLOOP_OBJECT;
RETURN_BOOL(g_main_loop_is_running(mainloop->g_m_loop));
}
ZEND_BEGIN_ARG_INFO(arginfo_midgard_g_mainloop_is_running, 0)
ZEND_END_ARG_INFO()
static PHP_METHOD(midgard_g_mainloop, quit)
{
if (zend_parse_parameters_none() == FAILURE)
return;
_GET_MAINLOOP_OBJECT;
g_main_loop_quit(mainloop->g_m_loop);
}
ZEND_BEGIN_ARG_INFO(arginfo_midgard_g_mainloop_quit, 0)
ZEND_END_ARG_INFO()
/* Initialize ZEND&PHP class */
PHP_MINIT_FUNCTION(midgard2_g_mainloop)
{
static zend_function_entry midgard_g_mainloop_methods[] = {
PHP_ME(midgard_g_mainloop, __construct, arginfo_midgard_g_mainloop___construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(midgard_g_mainloop, run, arginfo_midgard_g_mainloop_run, ZEND_ACC_PUBLIC)
PHP_ME(midgard_g_mainloop, is_running, arginfo_midgard_g_mainloop_is_running, ZEND_ACC_PUBLIC)
PHP_ME(midgard_g_mainloop, quit, arginfo_midgard_g_mainloop_quit, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
static zend_class_entry php_midgard_g_mainloop_class_entry;
INIT_CLASS_ENTRY(php_midgard_g_mainloop_class_entry, "midgard_g_main_loop", midgard_g_mainloop_methods);
php_midgard_g_mainloop_class = zend_register_internal_class(&php_midgard_g_mainloop_class_entry TSRMLS_CC);
/* Set function to initialize underlying data */
php_midgard_g_mainloop_class->create_object = php_midgard_gobject_new;
return SUCCESS;
}