FreeWRL/FreeX3D  3.0.0
cdllFreeWRL.c
1 /* dllFreeWRL.cpp : Defines the exported functions for the DLL application.
2  general notes:
3  Your main program -or html page- defines a single process and main thread.
4  If you want to have more than one instance of freewrl (separate window and content)
5  in the same process, then you need to connect the 'context' to the thread
6  functionality. But your main program is all in one thread. So you can't just use
7  your main thread to select a context.
8  Here we'll use a pointer to iglobal as a context handle.
9 
10 */
11 #include "cdllFreeWRL.h"
12 
13 #include <config.h>
14 #include "system.h"
15 #include "libFreeWRL.h"
16 // a few function prototypes from around libfreewrl
17 void fwl_setConsole_writePrimitive(int ibool);
18 void statusbar_set_window_size(int width, int height);
19 int statusbar_handle_mouse(int mev, int butnum, int mouseX, int mouseY);
20 int getCursorStyle();
21 void *fwl_frontenditem_dequeue();
22 char* fwl_resitem_getURL(void *res);
23 int fwl_resitem_getStatus(void *res);
24 int fwl_resitem_getType(void *res);
25 int fwl_resitem_getMediaType(void *res);
26 void fwl_resitem_enqueuNextMulti(void *res);
27 void fwl_resitem_setLocalPath(void *res, char* path);
28 void fwl_resitem_enqueue(void *res);
29 int file2blob(void *res);
30 #ifdef SSR_SERVER
31 //SSR (Server-side rendering)
32 void SSRserver_enqueue_request_and_wait(void *fwctx, void *request);
33 #endif //SSR_SERVER
34 
35 #include <malloc.h>
36 
37 
38 // This is the constructor of a class that has been exported.
39 // see dllFreeWRL.h for the class definition
40 DLLFREEWRL_API void * dllFreeWRL_dllFreeWRL()
41 {
42  /*STA -single threaded app- frontends -like web pages in a browser, .net forms, xaml apps-
43  can have multiple freewrl instances in different sub-windows, all running
44  in the same frontend thread. But then we can't rely on thread-lookup
45  to find which freewrl instance. But the frontend developer will have
46  a pointer to each instance. Then we look up the freewrl instance from that,
47  using this->globalcontexthandle, fwl_setCurrentHandle(), fwl_clearCurrentHandle(),
48  for the frontend-thread-synchronized part (functions called from the STA), and then
49  worker threads within libfreewrl can use thread lookup to get the global instance
50  for the backend parts. No thread locking is needed in the frontend-thread-sync part
51  -like here in dllfreewrl.cpp- because the frontend developer will program against
52  one dllfreewrl instance at a time due to it being STA.
53  If converting this cdllfreewrl C++ to flat C interface, then add an extra
54  parameter void* fwglobal to all the functions, do fwl_init_instance in the constructor
55  and have it return the gglobal as void *, and the frontend programmer will hold
56  the fwglobal pointer between calls.
57  */
58  //this->globalcontexthandle = 0;
59  return fwl_init_instance(); //before setting any structs we need a struct allocated
60 }
61 DLLFREEWRL_API void dllFreeWRL_setDensityFactor(void *fwctx, float density_factor){
62  fwl_setCurrentHandle(fwctx, __FILE__, __LINE__);
63  fwl_setDensityFactor(density_factor);
64  fwl_clearCurrentHandle();
65  return;
66 }
67 
68 // handle - window handle or null
69 // - if you have a window already created, you should pass in the handle,
70 // - else pass null and a window will be created for you
71 DLLFREEWRL_API void dllFreeWRL_onInit(void *fwctx, int width, int height, void* windowhandle, int bEai, int frontend_handles_display_thread)
72 {
73  int ok;
74  struct freewrl_params *params;
75  //if( !fwl_setCurrentHandle(handle) ){
76  //this->globalcontexthandle = fwl_init_instance(); //before setting any structs we need a struct allocated
77  fwl_setCurrentHandle(fwctx, __FILE__, __LINE__);
78  /* Before we parse the command line, setup the FreeWRL default parameters */
79  params = (freewrl_params_t*) malloc( sizeof(freewrl_params_t));
80  memset(params,0,sizeof(freewrl_params_t));
81  /* Default values */
82  params->width = width; //600;
83  params->height = height; //400;
84  //params->eai = bEai;
85  params->fullscreen = 0;
86  params->winToEmbedInto = (long)windowhandle;
87  params->frontend_handles_display_thread = frontend_handles_display_thread;
88  ok = fwl_initFreeWRL(params);
89 #ifndef FRONTEND_HANDLES_DISPLAY_THREAD
90  if(ok)
91  if(!frontend_handles_display_thread)
92  fwl_initializeDisplayThread();
93 #endif
94 //#ifdef STATUSBAR_HUD
95 // statusbar_set_window_size(width, height);
96 //#else
97  fwl_setScreenDim(width, height);
98 //#endif
99  fwl_clearCurrentHandle();
100  return;
101 }
102 DLLFREEWRL_API void dllFreeWRL_setTempFolder(void *fwctx, char *tmpFolder)
103 {
104  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
105  fwl_tmpFileLocation(tmpFolder);
106  }
107  fwl_clearCurrentHandle();
108 }
109 DLLFREEWRL_API void dllFreeWRL_setFontFolder(void *fwctx,char *fontFolder)
110 {
111  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
112  fwl_fontFileLocation(fontFolder);
113  }
114  fwl_clearCurrentHandle();
115 }
116 DLLFREEWRL_API void * dllFreeWRL_dllFreeWRL1(int width, int height, void* windowhandle, int bEai)
117 {
118  void *fwctx;
119  fwctx = fwl_init_instance(); //before setting any structs we need a struct allocated
120  dllFreeWRL_onInit(fwctx, width, height, windowhandle, bEai, FALSE);
121  return fwctx;
122 }
123 DLLFREEWRL_API void *dllFreeWRL_dllFreeWRL2(char* scene_url, int width, int height, void* windowhandle, int bEai)
124 {
125  void *fwctx;
126  fwctx = fwl_init_instance(); //before setting any structs we need a struct allocated
127  dllFreeWRL_onInit(fwctx, width, height, windowhandle, bEai, FALSE);
128  dllFreeWRL_onLoad(fwctx,scene_url);
129  return fwctx;
130 }
131 
132 DLLFREEWRL_API void dllFreeWRL_onLoad(void *fwctx, char* scene_url)
133 {
134  char * url;
135  url = strdup(scene_url);
136  if(fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
137  fwl_replaceWorldNeeded(url);
138  }
139  fwl_clearCurrentHandle();
140 }
141 
142 
143 DLLFREEWRL_API void dllFreeWRL_onResize(void *fwctx, int width,int height){
144  if(fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
145 //#ifdef STATUSBAR_HUD
146 // statusbar_set_window_size(width,height);
147 //#else
148  fwl_setScreenDim(width,height);
149 //#endif
150  }
151  fwl_clearCurrentHandle();
152 }
153 
154 DLLFREEWRL_API int dllFreeWRL_onMouse(void *fwctx, int mouseAction,int mouseButton,int x, int y){
155 
156  /*void fwl_handle_aqua(const int mev, const unsigned int button, int x, int y);*/
157  /* butnum=1 left butnum=3 right (butnum=2 middle, not used by freewrl) */
158  int cursorStyle = 0;
159  if(fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
160  cursorStyle = fwl_handle_mouse(mouseAction,mouseButton,x,y,0);
161  }
162  fwl_clearCurrentHandle();
163  return cursorStyle;
164 }
165 DLLFREEWRL_API int dllFreeWRL_onTouch(void *fwctx, int touchAction, unsigned int ID, int x, int y) {
166 
167  /*void fwl_handle_aqua(const int mev, const unsigned int button, int x, int y);*/
168  /* butnum=1 left butnum=3 right (butnum=2 middle, not used by freewrl) */
169  int cursorStyle = 0;
170  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)) {
171  cursorStyle = fwl_handle_touch(touchAction, ID, x, y, 0);
172  }
173  fwl_clearCurrentHandle();
174  return cursorStyle;
175 }
176 DLLFREEWRL_API void dllFreeWRL_onGyro(void *fwctx, float rx, float ry, float rz) {
177 
178  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)) {
179  fwl_handle_gyro(rx, ry, rz);
180  }
181  fwl_clearCurrentHandle();
182  return ;
183 }
184 DLLFREEWRL_API void dllFreeWRL_onAccelerometer(void *fwctx, float ax, float ay, float az) {
185 
186  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)) {
187  fwl_handle_accelerometer(ax, ay, az);
188  }
189  fwl_clearCurrentHandle();
190  return;
191 }
192 DLLFREEWRL_API void dllFreeWRL_onMagnetic(void *fwctx, float azimuth, float pitch, float roll) {
193 
194  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)) {
195  fwl_handle_magnetic(azimuth, pitch, roll);
196  }
197  fwl_clearCurrentHandle();
198  return;
199 }
200 DLLFREEWRL_API void dllFreeWRL_onKey(void *fwctx, int keyAction,int keyValue){
201  int kp = keyValue;
202  int ka = keyAction;
203  if(fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
204  switch(keyAction)
205  {
206  case KEYDOWN:
207  if(kp & 1 << 30)
208  break; //ignor - its an auto-repeat
209  case KEYUP:
210  //switch (kp)
211  //{
212  // case VK_OEM_1:
213  // kp = ';'; //could be : or ; but tolower won't lowercase it, but returns same character if it can't
214  // break;
215  // default:
216  // break;
217  //}
218  fwl_do_keyPress(kp, ka);
219  break;
220 
221  case KEYPRESS: //WM_CHAR:
222  fwl_do_keyPress(kp,ka);
223  break;
224  }
225  }
226  fwl_clearCurrentHandle();
227 }
228 DLLFREEWRL_API void dllFreeWRL_onClose(void *fwctx)
229 {
230 
231  /* when finished: as of early 2014 dug9 changed the _displayThread so now fwl_doQuit() is asynchronous meaning
232  it returns here immediately, but it takes a while for libfreewrl to finish parking threads, deleting resources
233  */
234  if(fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
235  //fwl_doQuit();
236  fwl_doQuitAndWait();
237  }
238  fwl_clearCurrentHandle();
239 }
240 DLLFREEWRL_API void dllFreeWRL_print(void *fwctx, char *str)
241 {
242  if(fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
243  ConsoleMessage(str);
244  }
245  fwl_clearCurrentHandle();
246 }
247 DLLFREEWRL_API void dllFreeWRL_onDraw(void *fwctx)
248 {
249  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
250  fwl_draw();
251  }
252  fwl_clearCurrentHandle();
253 }
254 
255 DLLFREEWRL_API int dllFreeWRL_getUpdatedCursorStyle(void *fwctx)
256 {
257  int cstyle = 0;
258  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
259  cstyle = getCursorStyle();
260  }
261  fwl_clearCurrentHandle();
262  return cstyle;
263 }
264 #if !defined(NULL)
265 #define NULL (char*)0
266 #endif
267 DLLFREEWRL_API void* dllFreeWRL_frontenditem_dequeue(void *fwctx)
268 {
269  void *item = NULL;
270  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
271  item = fwl_frontenditem_dequeue();
272  }
273  fwl_clearCurrentHandle();
274  return item;
275 }
276 DLLFREEWRL_API char* dllFreeWRL_resitem_getURL(void *fwctx, void *res){
277  char *url = NULL;
278  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
279  url = fwl_resitem_getURL(res);
280  }
281  fwl_clearCurrentHandle();
282  return url;
283 }
284 DLLFREEWRL_API int dllFreeWRL_resitem_getStatus(void *fwctx, void *res){
285  int status;
286  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
287  status = fwl_resitem_getStatus(res);
288  }
289  fwl_clearCurrentHandle();
290  return status;
291 }
292 DLLFREEWRL_API void dllFreeWRL_resitem_setStatus(void *fwctx, void *res, int status){
293  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)) {
294  fwl_resitem_setStatus(res, status);
295  }
296  fwl_clearCurrentHandle();
297 
298 }
299 DLLFREEWRL_API int dllFreeWRL_resitem_getType(void *fwctx, void *res){
300  int status;
301  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
302  status = fwl_resitem_getType(res);
303  }
304  fwl_clearCurrentHandle();
305  return status;
306 }
307 DLLFREEWRL_API int dllFreeWRL_resitem_getMediaType(void *fwctx, void *res) {
308  int status;
309  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)) {
310  status = fwl_resitem_getMediaType(res);
311  }
312  fwl_clearCurrentHandle();
313  return status;
314 }
315 
316 DLLFREEWRL_API void dllFreeWRL_resitem_enqueuNextMulti(void *fwctx, void *res){
317  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
318  fwl_resitem_enqueuNextMulti(res);
319  }
320  fwl_clearCurrentHandle();
321 }
322 DLLFREEWRL_API void dllFreeWRL_resitem_setLocalPath(void *fwctx, void *res, char* path){
323  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
324  fwl_resitem_setLocalPath(res,path);
325  }
326  fwl_clearCurrentHandle();
327 }
328 DLLFREEWRL_API void dllFreeWRL_resitem_load(void *fwctx, void *res){
329  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
330  if (file2blob(res))
331  fwl_resitem_enqueue(res);
332  }
333  fwl_clearCurrentHandle();
334 }
335 DLLFREEWRL_API void dllFreeWRL_resitem_enqueue(void *fwctx, void *res){
336  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
337  fwl_resitem_enqueue(res);
338  }
339  fwl_clearCurrentHandle();
340 }
341 
342 #ifdef SSR_SERVER
343 DLLFREEWRL_API void dllFreeWRL_SSRserver_enqueue_request_and_wait(void *fwctx, void *request){
344  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
345  SSRserver_enqueue_request_and_wait(fwctx, request);
346  }
347  fwl_clearCurrentHandle();
348 }
349 #endif //SSR_SERVER
350 
351 DLLFREEWRL_API void dllFreeWRL_commandline(void *fwctx, char *cmdline){
352  if (fwl_setCurrentHandle(fwctx, __FILE__, __LINE__)){
353  fwl_commandline(cmdline);
354  }
355  fwl_clearCurrentHandle();
356 }
357 
Initialization.
Definition: libFreeWRL.h:71