-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.c
296 lines (258 loc) · 12 KB
/
window.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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 2007-2011 Elizabeth M. Smith, Sara Golemon, Tom Rogers |
+----------------------------------------------------------------------+
| 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: Elizabeth M. Smith <[email protected]> |
| Mark G. Skilbeck <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_wingdi.h"
#include "zend_exceptions.h"
PHP_WINGDI_API zend_class_entry *ce_wingdi_window;
static zend_object_handlers wingdi_window_object_handlers;
static zend_function wingdi_window_constructor_wrapper;
/* ----------------------------------------------------------------
Win\Gdi\Window Userland API
Window stuff that is GDI specific is implemented here, BeginPaint
and EndPaint, for example. The WM_* GDI constants, however, are
implemented by Win\Gui\Window.
------------------------------------------------------------------*/
/** {{{ proto Win\Gdi\DeviceContext Win\Gdi\Window::beginPaint(array & paint_data)
Prepares the specified window for painting. The paint_data reference variable
receives information about the painting.
*/
PHP_METHOD( WinGdiWindow, beginPaint )
{
wingdi_devicecontext_object * dc_object,
* dc_object_display;
wingdi_window_object * window_object = zend_object_store_get_object( getThis() TSRMLS_CC );
zval * paint_data,
* paint_data_rect = NULL,
* paint_data_hdc = NULL;
PAINTSTRUCT paint_struct;
HDC hdc;
WINGDI_ERROR_HANDLING();
if ( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "z", &paint_data ) == FAILURE )
return;
WINGDI_RESTORE_ERRORS();
hdc = BeginPaint( window_object->window_handle, &paint_struct );
if ( !hdc )
{
wingdi_create_error( GetLastError(), ce_wingdi_exception TSRMLS_CC );
return;
}
// Set up return value for a DC object
object_init_ex( return_value, ce_wingdi_devicecontext );
dc_object = ( wingdi_devicecontext_object * ) zend_objects_get_address( return_value TSRMLS_CC );
dc_object->hdc = hdc;
dc_object->constructed = 1;
// Empty whatever the paint_data zval contains, and store in it the data from paint_struct
zval_dtor( paint_data );
array_init( paint_data );
// Set up the hdc element object
MAKE_STD_ZVAL( paint_data_hdc );
object_init_ex( paint_data_hdc, ce_wingdi_devicecontext );
dc_object_display = ( wingdi_devicecontext_object * ) zend_objects_get_address( paint_data_hdc TSRMLS_CC );
dc_object_display->hdc = paint_struct.hdc;
dc_object_display->constructed = 1;
add_assoc_zval( paint_data, "hdc", paint_data_hdc );
add_assoc_bool( paint_data, "erase", paint_struct.fErase );
// paint_struct.rcPaint is a RECT structure which maps to a PHP array
MAKE_STD_ZVAL( paint_data_rect );
array_init( paint_data_rect );
add_next_index_long( paint_data_rect, paint_struct.rcPaint.left );
add_next_index_long( paint_data_rect, paint_struct.rcPaint.top );
add_next_index_long( paint_data_rect, paint_struct.rcPaint.right );
add_next_index_long( paint_data_rect, paint_struct.rcPaint.bottom );
add_assoc_zval( paint_data, "paint", paint_data_rect );
}
/** }}} */
/** {{{ proto bool Win\Gdi\Window::endPaint( paint_data );
Marks the end of painting in the specified window.
*/
PHP_METHOD( WinGdiWindow, endPaint )
{
wingdi_devicecontext_object * dc_object;
wingdi_window_object * window_object = zend_object_store_get_object( getThis() TSRMLS_CC );
HashTable * paint_data;
PAINTSTRUCT paint_struct;
WINGDI_ERROR_HANDLING();
if ( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "h", &paint_data ) == FAILURE )
return;
WINGDI_RESTORE_ERRORS();
// Error checking
// Not sure about the error messages
if ( ! zend_hash_exists( paint_data, "hdc", strlen( "hdc" ) + 1 ) )
{
php_error( E_ERROR, "no 'hdc' element found in array" );
return;
}
else if ( ! zend_hash_exists( paint_data, "erase", strlen( "erase" ) + 1 ) )
{
php_error( E_ERROR, "no 'erase' element found in array" );
return;
}
else if ( ! zend_hash_exists( paint_data, "paint", strlen( "paint" ) + 1 ) )
{
php_error( E_ERROR, "no 'paint' element found in array" );
return;
}
else
{
zval ** hdc_element,
** erase_element,
** paint_element,
** left = NULL, ** top = NULL, ** right = NULL, ** bottom = NULL;
HashTable * paint_rect;
zend_hash_find( paint_data, "hdc", strlen( "hdc" ) + 1, ( void ** ) &hdc_element );
dc_object = ( wingdi_devicecontext_object * ) zend_objects_get_address( * hdc_element TSRMLS_CC );
paint_struct.hdc = dc_object->hdc;
zend_hash_find( paint_data, "erase", strlen( "erase" ) + 1, ( void ** ) &erase_element );
paint_struct.fErase = Z_BVAL_PP( erase_element );
zend_hash_find( paint_data, "paint", strlen( "paint" ) + 1, ( void ** ) &paint_element );
if ( Z_TYPE_PP( paint_element ) != IS_ARRAY || zend_hash_num_elements( Z_ARRVAL_PP( paint_element ) ) < 4 )
{
php_error( E_ERROR, "expected an array of for elements for 'paint' element of array" );
return;
}
paint_rect = Z_ARRVAL_PP( paint_element );
// TODO: error checking
zend_hash_index_find( paint_rect, 0, ( void ** ) &left );
zend_hash_index_find( paint_rect, 1, ( void ** ) &top );
zend_hash_index_find( paint_rect, 2, ( void ** ) &right );
zend_hash_index_find( paint_rect, 3, ( void ** ) &bottom );
paint_struct.rcPaint.left = Z_LVAL_PP( left );
paint_struct.rcPaint.top = Z_LVAL_PP( top );
paint_struct.rcPaint.right = Z_LVAL_PP( right );
paint_struct.rcPaint.bottom = Z_LVAL_PP( bottom );
RETURN_BOOL( EndPaint( window_object->window_handle, &paint_struct ) );
}
}
/** }}} */
/* No methods for this yet */
static const zend_function_entry wingdi_window_functions[] = {
PHP_ME( WinGdiWindow, beginPaint, NULL, ZEND_ACC_PUBLIC )
PHP_ME( WinGdiWindow, endPaint, NULL, ZEND_ACC_PUBLIC )
{NULL, NULL, NULL}
};
/* ----------------------------------------------------------------
Win\Gdi\Window Custom Object magic
------------------------------------------------------------------*/
/* {{{ wingdi_window_construction_wrapper
wraps around the constructor to make sure parent::__construct is always called */
static void wingdi_window_construction_wrapper(INTERNAL_FUNCTION_PARAMETERS) {
zval *this = getThis();
wingdi_window_object *tobj;
zend_class_entry *this_ce;
zend_function *zf;
zend_fcall_info fci = {0};
zend_fcall_info_cache fci_cache = {0};
zval *retval_ptr = NULL;
unsigned i;
tobj = zend_object_store_get_object(this TSRMLS_CC);
zf = zend_get_std_object_handlers()->get_constructor(this TSRMLS_CC);
this_ce = Z_OBJCE_P(this);
fci.size = sizeof(fci);
fci.function_table = &this_ce->function_table;
fci.object_ptr = this;
/* fci.function_name = ; not necessary to bother */
fci.retval_ptr_ptr = &retval_ptr;
fci.param_count = ZEND_NUM_ARGS();
fci.params = emalloc(fci.param_count * sizeof *fci.params);
/* Or use _zend_get_parameters_array_ex instead of loop: */
for (i = 0; i < fci.param_count; i++) {
fci.params[i] = (zval **) (zend_vm_stack_top(TSRMLS_C) - 1 -
(fci.param_count - i));
}
fci.object_ptr = this;
fci.no_separation = 0;
fci_cache.initialized = 1;
fci_cache.called_scope = EG(current_execute_data)->called_scope;
fci_cache.calling_scope = EG(current_execute_data)->current_scope;
fci_cache.function_handler = zf;
fci_cache.object_ptr = this;
zend_call_function(&fci, &fci_cache TSRMLS_CC);
if (!EG(exception) && tobj->is_constructed == 0)
zend_throw_exception_ex(NULL, 0 TSRMLS_CC,
"parent::__construct() must be called in %s::__construct()", this_ce->name);
efree(fci.params);
zval_ptr_dtor(&retval_ptr);
}
/* }}} */
/* {{{ wingdi_window_get_constructor
gets the constructor for the class */
static zend_function *wingdi_window_get_constructor(zval *object TSRMLS_DC)
{
/* Could always return constr_wrapper_fun, but it's uncessary to call the
* wrapper if instantiating the superclass */
if (Z_OBJCE_P(object) == ce_wingdi_window)
return zend_get_std_object_handlers()->
get_constructor(object TSRMLS_CC);
else
return &wingdi_window_constructor_wrapper;
}
/* {{{ wingdi_window_object_free
frees up the window handle underneath */
static void wingdi_window_object_free(void *object TSRMLS_DC)
{
wingdi_window_object *window_object = (wingdi_window_object *)object;
zend_object_std_dtor(&window_object->std TSRMLS_CC);
if(window_object->window_handle) {
// TODO: FREE ME
}
efree(window_object);
}
/* }}} */
/* {{{ wingdi_window_object_create
pretty basic item with internal window handle */
static zend_object_value wingdi_window_object_create(zend_class_entry *ce TSRMLS_DC)
{
zend_object_value retval;
wingdi_window_object *window_object;
window_object = ecalloc(1, sizeof(wingdi_window_object));
zend_object_std_init((zend_object *) window_object, ce TSRMLS_CC);
window_object->window_handle = NULL;
window_object->is_constructed = FALSE;
object_properties_init(&window_object->std, ce);
retval.handle = zend_objects_store_put(window_object,
(zend_objects_store_dtor_t) zend_objects_destroy_object,
(zend_objects_free_object_storage_t) wingdi_window_object_free,
NULL TSRMLS_CC);
retval.handlers = &wingdi_window_object_handlers;
return retval;
}
/* }}} */
/* ----------------------------------------------------------------
Win\Gdi\Bitmap LifeCycle Functions
------------------------------------------------------------------*/
PHP_MINIT_FUNCTION(wingdi_window)
{
zend_class_entry ce;
memcpy(&wingdi_window_object_handlers, zend_get_std_object_handlers(),
sizeof wingdi_window_object_handlers);
wingdi_window_object_handlers.get_constructor = wingdi_window_get_constructor;
INIT_NS_CLASS_ENTRY(ce, PHP_WINGDI_NS, "Window", wingdi_window_functions);
ce_wingdi_window = zend_register_internal_class(&ce TSRMLS_CC);
ce_wingdi_window->create_object = wingdi_window_object_create;
ce_wingdi_window->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
wingdi_window_constructor_wrapper.type = ZEND_INTERNAL_FUNCTION;
wingdi_window_constructor_wrapper.common.function_name = "internal_construction_wrapper";
wingdi_window_constructor_wrapper.common.scope = ce_wingdi_window;
wingdi_window_constructor_wrapper.common.fn_flags = ZEND_ACC_PROTECTED;
wingdi_window_constructor_wrapper.common.prototype = NULL;
wingdi_window_constructor_wrapper.common.required_num_args = 0;
wingdi_window_constructor_wrapper.common.arg_info = NULL;
wingdi_window_constructor_wrapper.internal_function.handler = wingdi_window_construction_wrapper;
wingdi_window_constructor_wrapper.internal_function.module = EG(current_module);
return SUCCESS;
}