forked from dustin-scott/ui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui.c
442 lines (356 loc) · 10.6 KB
/
ui.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: krakjoe |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_ui.h"
#include <ui.h>
#ifdef HAVE_UI_X_THREADS
# include <X11/Xlib.h>
#endif
#include <pthread.h>
#include <classes/exceptions.h>
#include <classes/executor.h>
#include <classes/point.h>
#include <classes/size.h>
#include <classes/control.h>
#include <classes/window.h>
#include <classes/tab.h>
#include <classes/form.h>
#include <classes/grid.h>
#include <classes/box.h>
#include <classes/check.h>
#include <classes/button.h>
#include <classes/cbutton.h>
#include <classes/label.h>
#include <classes/entry.h>
#include <classes/multi.h>
#include <classes/group.h>
#include <classes/spin.h>
#include <classes/slider.h>
#include <classes/progress.h>
#include <classes/separator.h>
#include <classes/combo.h>
#include <classes/ecombo.h>
#include <classes/radio.h>
#include <classes/picker.h>
#include <classes/menu.h>
#include <classes/item.h>
#include <classes/area.h>
#include <classes/pen.h>
#include <classes/path.h>
#include <classes/color.h>
#include <classes/brush.h>
#include <classes/stroke.h>
#include <classes/matrix.h>
#include <classes/descriptor.h>
#include <classes/font.h>
#include <classes/layout.h>
#define PHP_UI_LOOP 1<<0
#define PHP_UI_WAIT 1<<1
void php_ui_set_call(zend_object *object, const char *name, size_t nlength, zend_fcall_info *fci, zend_fcall_info_cache *fcc) {
zend_function *function = zend_hash_str_find_ptr(&object->ce->function_table, name, nlength);
if (!function || function->type == ZEND_INTERNAL_FUNCTION) {
return;
}
fci->size = sizeof(zend_fcall_info);
fci->object = object;
fci->no_separation = 1;
#if PHP_VERSION_ID < 70300
fcc->initialized = 1;
#endif
fcc->object = object;
fcc->function_handler = function;
fcc->calling_scope = object->ce;
fcc->called_scope = object->ce;
}
static inline zend_object* php_ui_top(zend_object *object) {
if (!object) {
return NULL;
}
do {
php_ui_control_t *control = php_ui_control_from(object);
if (instanceof_function(object->ce, uiWindow_ce)) {
return object;
}
if (!control->parent) {
break;
}
object = php_ui_control_from(object)->parent;
} while (object);
return NULL;
}
int php_ui_call(zend_fcall_info *fci, zend_fcall_info_cache *fcc) {
int result = zend_call_function(fci, fcc);
if (0) {
_php_ui_call_zend_exception_handler:
zend_try_exception_handler();
return result;
}
if (result != SUCCESS) {
if (EG(exception)) {
zend_object *top = php_ui_top(fci->object);
if (top) {
php_ui_window_t *window = php_ui_window_from(top);
if (window->uncaught.fci.size) {
zval ctrl, ex, rv;
zend_object *exception = EG(exception);
EG(exception) = NULL;
ZVAL_OBJ(&ctrl, fci->object);
ZVAL_OBJ(&ex, exception);
ZVAL_UNDEF(&rv);
window->uncaught.fci.retval = &rv;
zend_fcall_info_argn(&window->uncaught.fci, 2, &ctrl, &ex);
if (php_ui_call(&window->uncaught.fci, &window->uncaught.fcc) != SUCCESS) {
EG(exception) = exception;
} else {
if (EG(exception)) {
OBJ_RELEASE(EG(exception));
EG(exception) = NULL;
}
OBJ_RELEASE(exception);
zend_fcall_info_args_clear(&window->uncaught.fci, 1);
zval_ptr_dtor(&rv);
return SUCCESS;
}
return result;
}
}
goto _php_ui_call_zend_exception_handler;
}
}
return result;
}
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(ui)
{
#ifdef HAVE_UI_X_THREADS
XInitThreads();
#endif
REGISTER_NS_LONG_CONSTANT("UI", "Loop", PHP_UI_LOOP, CONST_CS|CONST_PERSISTENT);
REGISTER_NS_LONG_CONSTANT("UI", "Wait", PHP_UI_WAIT, CONST_CS|CONST_PERSISTENT);
PHP_MINIT(UI_Exceptions)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Executor)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Point)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Size)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Control)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Window)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Form)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Grid)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Tab)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Box)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Check)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Button)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_ColorButton)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Label)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Entry)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Multi)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Group)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Spin)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Slider)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Progress)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Separator)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Combo)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_EditableCombo)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Radio)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Picker)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Menu)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_MenuItem)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_Area)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_DrawPen)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_DrawPath)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_DrawColor)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_DrawBrush)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_DrawStroke)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_DrawMatrix)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_DrawTextFontDescriptor)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_DrawTextFont)(INIT_FUNC_ARGS_PASSTHRU);
PHP_MINIT(UI_DrawTextLayout)(INIT_FUNC_ARGS_PASSTHRU);
php_ui_control_finalize();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(ui)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(ui)
{
uiInitOptions options;
const char *initError = NULL;
#if defined(COMPILE_DL_UI) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
memset(&options, 0, sizeof(uiInitOptions));
initError = uiInit(&options);
if (initError) {
zend_error(E_ERROR,
"Cannot initialize libui: %s", initError);
uiFreeInitError(initError);
return FAILURE;
}
uiMainSteps();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(ui)
{
#if 0
uiUninit();
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(ui)
{
php_info_print_table_start();
php_info_print_table_header(2, "ui support", "enabled");
php_info_print_table_end();
}
/* }}} */
/* {{{ nothing happening here ... ignore this ... */
typedef struct _php_ui_object_t {
void *p;
zend_object std;
} php_ui_object_t;
int php_ui_serialize(zval *object, unsigned char **buffer, size_t *buflen, zend_serialize_data *data) {
void *address = ((char*) Z_OBJ_P(object) - XtOffsetOf(php_ui_object_t, std));
#ifdef _WIN64
(*buflen) = snprintf(NULL, 0, ":%I64u:", (unsigned __int64) address);
#else
(*buflen) = snprintf(NULL, 0, ":%lu:", (long unsigned int) address);
#endif
(*buffer) = emalloc((*buflen) + 1);
sprintf(
#ifdef _WIN64
(char*) (*buffer), ":%I64u:", (unsigned __int64) address);
#else
(char*) (*buffer), ":%lu:", (long unsigned int) address);
#endif
(*buffer)[(*buflen)] = 0;
return SUCCESS;
}
#define php_ui_object_from(o) ((php_ui_object_t*) ((char*) o - XtOffsetOf(php_ui_object_t, std)))
#define php_ui_object_fetch(o) php_ui_object_from(Z_OBJ_P(o))
int php_ui_unserialize(zval *object, zend_class_entry *ce, const unsigned char *buffer, size_t blen, zend_unserialize_data *data) {
php_ui_object_t *address = NULL;
php_ui_object_t *o;
#ifdef _WIN64
if (!sscanf((const char*) buffer, ":%I64u:", (unsigned __int64*)&address)) {
#else
if (!sscanf((const char*) buffer, ":%lu:", (long unsigned int*)&address)) {
#endif
return FAILURE;
}
object_init_ex(object, ce);
o = php_ui_object_fetch(object);
o->p = address->p;
return SUCCESS;
} /* }}} */
ZEND_BEGIN_ARG_INFO_EX(php_ui_run_info, 0, 0, 0)
ZEND_ARG_TYPE_INFO(0, flags, IS_LONG, 0)
ZEND_END_ARG_INFO()
/* {{{ proto void UI\run([int flags = 0])*/
PHP_FUNCTION(UI_run)
{
zend_long flags = 0;
if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "|l", &flags) != SUCCESS) {
return;
}
if (!(flags & PHP_UI_LOOP)) {
uiMain();
return;
}
uiMainStep((flags & PHP_UI_WAIT) == PHP_UI_WAIT);
} /* }}} */
ZEND_BEGIN_ARG_INFO_EX(php_ui_quit_info, 0, 0, 0)
ZEND_END_ARG_INFO()
/* {{{ */
PHP_FUNCTION(UI_quit)
{
uiQuit();
} /* }}} */
ZEND_BEGIN_ARG_INFO_EX(php_ui_draw_font_families_info, 0, 0, 0)
ZEND_END_ARG_INFO()
/* {{{ proto array UI\Draw\Text\fontFamilies(void) */
PHP_FUNCTION(fontFamilies)
{
uiDrawFontFamilies* families;
int familiy = 0;
if (zend_parse_parameters_none() != SUCCESS) {
return;
}
array_init(return_value);
families = uiDrawListFontFamilies();
for (familiy = 0; familiy < uiDrawFontFamiliesNumFamilies(families); familiy++) {
add_next_index_string(return_value, uiDrawFontFamiliesFamily(families, familiy));
}
uiDrawFreeFontFamilies(families);
} /* }}} */
/* {{{ ui_functions[]
*/
const zend_function_entry ui_functions[] = {
ZEND_NS_NAMED_FE("UI", run, zif_UI_run, php_ui_run_info)
ZEND_NS_NAMED_FE("UI", quit, zif_UI_quit, php_ui_quit_info)
ZEND_NS_FE("UI\\Draw\\Text\\Font", fontFamilies, php_ui_draw_font_families_info)
PHP_FE_END
};
/* }}} */
/* {{{ ui_module_entry
*/
zend_module_entry ui_module_entry = {
STANDARD_MODULE_HEADER,
"ui",
ui_functions,
PHP_MINIT(ui),
PHP_MSHUTDOWN(ui),
PHP_RINIT(ui),
PHP_RSHUTDOWN(ui),
PHP_MINFO(ui),
PHP_UI_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_UI
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(ui)
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/