summaryrefslogtreecommitdiff
path: root/ext/w32api/php_w32api.h
blob: 05a353265fc540c7dc16b032758a5fa852c620be (plain)
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
/*
   +----------------------------------------------------------------------+
   | PHP version 4.0                                                      |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2001 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 2.02 of the PHP license,      |
   | that is bundled with this package in the file LICENSE, and is        |
   | available at through the world-wide-web at                           |
   | http://www.php.net/license/2_02.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          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors: James Moore <jmoore@php.net>                                |
   +----------------------------------------------------------------------+
*/

/* $Id$ */

#if		HAVE_W32API

#ifndef PHP_W32API_H
#define PHP_W32API_H

/* ================================================================================================
 * Type Definitions
 * ================================================================================================
 */
typedef struct _w32api_lib_handle									w32api_lib_handle;
typedef struct _w32api_func_handle									w32api_func_handle;
typedef struct _w32api_type_handle									w32api_type_handle; 
typedef struct _w32api_type_instance								w32api_type_instance;
typedef struct _arguments											arguments;
typedef struct _argument											argument;
typedef union  _w32api_parser_function_definition_union				w32api_parser_function_definition_union; 
typedef struct _w32api_func_handle_ptr								w32api_func_handle_ptr;
typedef struct _w32api_type_handle_ptr								w32api_type_handle_ptr;
typedef union  _w32api_parser_type_definition_union					w32api_parser_type_definition_union;
typedef struct _member												member;
typedef struct _members												members;
typedef union  _w32api_result										w32api_result;
typedef struct _w32api_dynamic_param								w32api_dynamic_param; 

struct _w32api_lib_handle 
{
	HINSTANCE					handle;								/* Handle for our library */
	char						*library_name;						/* name of our library */
	int							ref_count;							/* reference counter */
};

struct _w32api_func_handle_ptr										/* Temporary structure */
{																	/* To work around problems */
	w32api_func_handle			*hnd;								/* at 3am.. Ill sort it out */
};																	/* When I can think straight */
																	
struct _w32api_type_handle_ptr										/* Ditto.. should really combine */
{																	/* These two into a union at least */
	w32api_type_handle			*hnd;
};

struct _w32api_type_handle 
{
	char						*type_name;							/* Name of our type */
	long						size;								/* Size of type */
	members						*member_list;						/* Pointer List of members */
	long						member_count;						/* Number of members */
};

struct _w32api_type_instance
{
	w32api_type_handle			*type;								/* pointer to w32api_type_handle */
	zval						**values;							/* First element of an array of ptr's to zvals */
};

struct _w32api_func_handle 
{
	FARPROC						handle;								/* Handle for our function */
	w32api_lib_handle			*lib;								/* Pointer to the library handle */
	char						*function_name;						/* Name of our function (Alias is store if supplied) */
	long						return_type_id;						/* ID of return type */
	char						*return_type_name;					/* Name of return type (if W32API_COMPLEX) */
	long						flags;								/* Flags for function */
	arguments					*argument_list;						/* Pointer List of arguments */
};

struct _arguments
{
	argument					*arg;								/* Current Argument */
	arguments					*next_arg;							/* Next Arugment */
	arguments					*prev_arg;							/* Previous Argument */
};

struct _argument
{
	long						type_id;							/* ID of the return type */
	char						*type_name;							/* Name of type (if W32API_COMPLEX) */
	char						*argument_name;						/* Name of argument, currently not used */
	long						flags;								/* Currently used for byref/byval */
};

struct _member
{
	char						*member_name;
	long						member_type_id;
	char						*member_type_name;
	long						flags;
	long						offset;
};

struct _members
{
	member						*member;
	members						*next_member;
	members						*prev_member;
};

union _w32api_result
{
	int							ival;							
    unsigned long				lval;			        
	DWORD						dwval;
    void						*ptr;							
    float						fval;				            
    double						dval;						    
    __int64						i64val;		  			        
};

struct _w32api_dynamic_param 
{
	long						flags;
	int							width;
	union {
		unsigned long			argument;				
		void					*argument_ptr;					
	};
};


union _w32api_parser_function_definition_union 
{
	char *s;
	arguments *arg;
};

union _w32api_parser_type_definition_union 
{
	char *s;
	members *type;
};

/* ================================================================================================
 * Constants
 * ================================================================================================
 */

/* Recognised Base types */
#define W32API_UNKNOWN									-1
#define W32API_NULL										1
#define W32API_INT										2
#define W32API_LONG										3
#define W32API_DOUBLE									4
#define W32API_FLOAT									5
#define W32API_STRING									6
#define W32API_BYTE										7
#define W32API_BOOL										8
#define W32API_COMPLEX									9

/* Function Flags */
#define W32API_ARGPTR									(1<<0)
#define W32API_BORLAND									(1<<1)
#define W32API_CDECL									(1<<2)
#define W32API_REAL4									(1<<3)
#define W32API_REAL8									(1<<4)

/* ================================================================================================
 * Utility Macros
 * ================================================================================================
 */
#define PROP_SET_ARGS									zend_property_reference *property_reference, pval *value
#define PROP_GET_ARGS									zend_property_reference *property_reference

#define W32API_PROP_SET_FUNCTION_N(class_name)			w32api_set_property_handler_##class_name
#define W32API_PROP_GET_FUNCTION_N(class_name)			w32api_get_property_handler_##class_name
#define W32API_CALL_FUNCTION_N(class_name)				w32api_call_handler_##class_name

#define W32API_PROP_SET_FUNCTION(class_name)			PHP_W32API_API int W32API_PROP_SET_FUNCTION_N(class_name)(PROP_SET_ARGS)
#define W32API_PROP_GET_FUNCTION(class_name)			PHP_W32API_API zval W32API_PROP_GET_FUNCTION_N(class_name)(PROP_GET_ARGS)
#define W32API_CALL_FUNCITON(class_name)				PHP_W32API_API void W32API_CALL_FUNCTION_N(class_name)(INTERNAL_FUNCTION_PARAMETERS, zend_property_reference *property_reference)

#define W32API_CLASS_FUNCTION(class_name, function_name) PHP_FUNCTION(##class_name##function_name)
#define W32API_CLASS_FN(class_name, function_name) PHP_FN(##class_name##function_name)
#define W32API_CLASS_FE(class_name, function_name, function_args) {#function_name, W32API_CLASS_FN(class_name, function_name), function_args},

/* ================================================================================================
 * Module exports, Global Variables and General Definitions
 * ================================================================================================
 */
extern zend_module_entry w32api_module_entry;
#define phpext_w32api_ptr &w32api_module_entry;

#define PHP_W32API_API __declspec(dllexport)

#ifdef ZTS
#include "TSRM.h"
#endif /* ZTS */

ZEND_BEGIN_MODULE_GLOBALS(w32api)
	zend_class_entry	*win32_ce;				/* The class entry for our win32 class */
	zend_class_entry	*type_ce;				/* The class entry for our type class */
	HashTable			*funcs;					/* Functions we registered */
	HashTable			*libraries;				/* Libraries we load using LoadLibrary */
	HashTable			*types;					/* Types we have registed with us */
	HashTable			*callbacks;				/* Callbacks we have registered with us */
	long				le_type_instance;		/* Resource hanlde for runtime instances */
ZEND_END_MODULE_GLOBALS(w32api)

ZEND_DECLARE_MODULE_GLOBALS(w32api)

#ifdef ZTS
#define WG(v) TSRMG(w32api_globals_id, zend_w32api_globals *, v)
#else
#define WG(v) (w32api_globals.v)
#endif


/* ================================================================================================
 * Startup, Shutdown and Info Functions
 * ================================================================================================
 */
PHP_MINIT_FUNCTION(w32api);
PHP_MSHUTDOWN_FUNCTION(w32api);
PHP_RINIT_FUNCTION(w32api);
PHP_RSHUTDOWN_FUNCTION(w32api);
PHP_MINFO_FUNCTION(w32api);
static void php_w32api_init_globals(zend_w32api_globals *w32api_globals);
static void php_w32api_hash_lib_dtor(void *data);
static void php_w32api_hash_func_dtor(void *data);
static void php_w32api_hash_callback_dtor(void *data);
static void php_w32api_hash_type_dtor(void *data);
static void w32api_type_instance_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC);

/* ================================================================================================
 * Win32 Class Functions
 * ================================================================================================
 */
int win32_class_init(TSRMLS_D);
int win32_class_rshutdown(TSRMLS_D);
W32API_CLASS_FUNCTION(win32, registerfunction);
W32API_CLASS_FUNCTION(win32, unregisterfunction);
W32API_CLASS_FUNCTION(win32, registercallback);
W32API_CLASS_FUNCTION(win32, definetype);
W32API_CLASS_FUNCTION(win32, gettypesize);
W32API_CLASS_FUNCTION(win32, inittype);
W32API_CLASS_FUNCTION(win32, decref);
W32API_CLASS_FUNCTION(win32, invokefunction);			


/* ================================================================================================
 * Type Class Functions
 * ================================================================================================
 */
int type_class_init(TSRMLS_DC);

W32API_CLASS_FUNCTION(type, clone);
W32API_PROP_SET_FUNCTION(type);
W32API_PROP_GET_FUNCTION(type);

/* ================================================================================================
 * Utility Functions
 * ================================================================================================
 */

static int php_w32api_load_function (char *definition, int definition_len, int flags TSRMLS_DC);
static void php_w32api_unload_function (w32api_func_handle **fh TSRMLS_DC);

static int php_w32api_load_library (char *library_name, w32api_lib_handle **lh TSRMLS_DC);
static void php_w32api_unload_library (w32api_lib_handle *lh, int flags TSRMLS_DC);

static int php_w32api_register_callback(char *function_definition, int function_definition_len TSRMLS_DC);

static void php_w32api_free_arguments(arguments *argument_list);
static void php_w32api_free_members(members *member_list);

static int php_w32api_get_type_id_from_name(char *type);
static unsigned char *php_w32api_do_arg_types(arguments **arguments);

void php_w32api_marshall_zval_to_c(argument *arg, w32api_dynamic_param *dp, zval *pzval TSRMLS_DC);

static void php_w32api_init_type(w32api_type_handle *th, zval *obj TSRMLS_DC);
static int php_w32api_do_prop_get(zval *object, zval *return_value, zend_llist_element **element TSRMLS_DC);
static int php_w32api_do_prop_set(zval *object, zval *value, zend_llist_element **element TSRMLS_DC);
static void *php_w32api_complex_marshall_zval_to_c(zval *pzval, int *width, void *data TSRMLS_DC);

/* ================================================================================================
 * Parser & Scanner Functions
 * ================================================================================================
 */
#define w32api_parser_load_alias_function w32api_parser_load_function_ex
#define w32api_parser_load_function(return_type, function_name, arguments, library_name) w32api_parser_load_function_ex (return_type, function_name, NULL, arguments, library_name)
#define w32api_parser_make_argument_byref(arg_type, arg_name) w32api_parser_make_argument(arg_type, arg_name, BYREF_FORCE)
#define w32api_parser_make_argument_byval(arg_type, arg_name) w32api_parser_make_argument(arg_type, arg_name, BYREF_NONE)

#define w32api_parser_type_make_byref_value(member_name, member_type) w32api_parser_type_make_value(member_name, member_type, BYREF_FORCE)
#define w32api_parser_type_make_byval_value(member_name, member_type) w32api_parser_type_make_value(member_name, member_type, BYREF_NONE)

w32api_func_handle *w32api_parser_load_function_ex(char *return_type, char *function_name, char *alias_name, arguments *argument_list, char *library_name);
arguments *w32api_parser_make_argument(char *arg_type, char *arg_name, int byref);
arguments *w32api_parser_join_arguments(arguments *lval, arguments *rval);
int w32api_function_definition_error(char *s);

w32api_type_handle *w32api_parser_register_type(char *type_name, members *member_list);
members *w32api_parser_type_make_value(char *member_name, char *type_name, long flags);
members *w32api_parser_type_join_values(members *lval, members *rval);
int w32api_type_definition_error(char *s);

struct yy_buffer_state *w32api_function_definition_scan_bytes(char *bytes, int len);	/* Definied in w32api_function_definition_scanner.c */
int w32api_function_definition_parse(void *fh);											/* Definied in w32api_function_definition_parser.c */

struct yy_buffer_state *w32api_type_definition_scan_bytes(char *bytes, int len);		/* Definied in w32api_type_definition_scanner.c */
int w32api_type_definition_parse(void *th);												/* Definied in w32api_type_definition_parser.c */


/* ================================================================================================
 * Various Debugging Functions
 * ================================================================================================
 */
#ifndef NDEBUG
W32API_CLASS_FUNCTION(win32, dump_library_hash);
W32API_CLASS_FUNCTION(win32, dump_function_hash);
W32API_CLASS_FUNCTION(win32, dump_callback_hash);
W32API_CLASS_FUNCTION(win32, dump_type_hash);

int php_w32api_dump_library_hash_cb(void *pData TSRMLS_DC);
int php_w32api_dump_function_hash_cb(void *pData TSRMLS_DC);
int php_w32api_dump_callback_hash_cb(void *pData TSRMLS_DC);
int php_w32api_dump_type_hash_cb(void *pData TSRMLS_DC);

void php_w32api_print_arguments(arguments *argument_list);
void php_w32api_print_members(members *member_list);

#endif  /* ifndef	NDEBUG */ 

#endif	/* ifndef	PHP_W32API_H */
#endif  /* if		HAVE_W32API  */