FreeWRL/FreeX3D  3.0.0
resources.h
1 /*
2 
3  FreeWRL support library.
4  Resources handling: URL, files, ...
5 
6 */
7 
8 /****************************************************************************
9  This file is part of the FreeWRL/FreeX3D Distribution.
10 
11  Copyright 2009 CRC Canada. (http://www.crc.gc.ca)
12 
13  FreeWRL/FreeX3D is free software: you can redistribute it and/or modify
14  it under the terms of the GNU Lesser Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  FreeWRL/FreeX3D is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  GNU General Public License for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with FreeWRL/FreeX3D. If not, see <http://www.gnu.org/licenses/>.
25 ****************************************************************************/
26 
27 #ifndef __LIBFREEWRL_RESOURCES_H__
28 #define __LIBFREEWRL_RESOURCES_H__
29 
30 #include "list.h"
31 
32 // OLD_IPHONE_AQUA #ifdef AQUA
33 // OLD_IPHONE_AQUA #include <system_threads.h>
34 // OLD_IPHONE_AQUA #endif
35 
36 #ifdef _MSC_VER
37 #include <system_threads.h>
38 #endif
39 
40 #if defined(_ANDROID) || defined(ANDROIDNDK)
41 #include <system_threads.h>
42 #endif
43 
44 /* is this file name relative to another path, or is it really, really, a direct file name? */
45 #if defined(_MSC_VER)
46 #define IF_cleanedURL_IS_ABSOLUTE if(strchr(cleanedURL,':')) // if(cleanedURL[0] != '\0' && cleanedURL[1]== ':')
47 
48 #else
49 
50 #define IF_cleanedURL_IS_ABSOLUTE \
51  DEBUG_RES("resource_identify = we have base cleanedurl = %s\n", cleanedURL); \
52  if (cleanedURL[0] == '/')
53 #endif
54 
55 typedef enum resource_type {
56  rest_invalid,
57  rest_url,
58  rest_file,
59  rest_multi,
60  rest_string /* inline VRML/X3D code */
61 } resource_type_t;
62 
63 typedef enum resource_status {
64  ress_none, /* never processed */
65  ress_starts_good, /* path/url identification succeeded */
66  ress_invalid, /* path/url identification failed */
67  ress_downloaded, /* download succeeded (or local file available) */
68  ress_failed, /* download failed */
69  ress_loaded, /* loader succeeded */
70  ress_not_loaded, /* loader failed */
71  ress_parsed, /* parser succeeded */
72  ress_not_parsed /* parser failed */
73 } resource_status_t;
74 
75 typedef enum resource_media_type {
76  resm_unknown,
77  resm_vrml,
78  resm_x3d,
79  resm_image,
80  resm_movie,
81  resm_script,
82  resm_pshader,
83  resm_fshader,
84  resm_audio,
85  resm_x3z,
86  resm_external, //June 2016 html > frontend anchoring
87 } resource_media_type_t;
88 
89 typedef enum resource_actions {
90  resa_default = 0, //all by default: download, load, parse/process: not using actions
91  resa_identify = 1,
92  resa_download = 2, //will & and | as bit flags so declare 1,2,4,8.. power of 2 or 1<<n
93  resa_load = 4,
94  resa_process = 8,
95  //resa_place,
96  //resa_ remove, delete ?? ... see parser_process_res()
97 } resource_actions_t;
98 
99 typedef struct resource_item {
100 
101  /* Parent */
102  struct resource_item *parent;
103  s_list_t *children;
104 
105  bool network;
106  bool new_root;
107 
108  /* Request */
109  resource_type_t type;
110  resource_status_t status;
111  resource_actions_t actions; //if 0, do default which is all actions: download, load, parse,
112  //else do specific requested actions (which are bitwise |= resource_actions_t's)
113  //resource_actions_t successful; //you could have more bitwise flags to track what steps succeeded, which were attempted etc.
114  //resource_actions_t attempted;
115 
116  /* Resource has passed all the steps */
117  //July30,2014 dug9: clarification (and possible change) of the meaning of complete:
118  // work thread (and FE/ML download/load threads) has attempted all requested actions which are possible,
119  // and is finished with the resitem (some actions may have failed - test with last status ie ress_failed etc)
120  bool complete;
121  //for vrml/x3d media types:
122  void *ectx; //for parsing brotos - the X3D_Proto executionContext to put __ROUTES, __nodes, __subContext
123  void *whereToPlaceData; // usually X3D_Node*, except for Scripts and Shaders, it's Shader_Script* (which is a sub-struct)
124  int offsetFromWhereToPlaceData; //usually field offset (not used for Scripts/Shaders)
125  int textureNumber; //Dec 2014 textures don't use wheretoplacedata now, because the *tti might be zapped during inline unload before the image is downloaded ie Mars dataset
126  /* We can be feed with a Multi_String list of requests */
127  s_list_t *m_request;
128 
129  /* Verbatim request : requested path/url */
130  char *URLrequest;
131 
132  /* Base:
133  (base url or base path of the main world)
134  - if parent != NULL, use parent's base,
135  - else use own's base
136 
137  MUST be complete: either have a trailing '/'
138  either be the end of a valid URI that can
139  be appended with a file:
140  'http://host/cgi?request_file='
141 
142  This last example requires that we can
143  parse the main url and extract that 'base'.
144  Is there a 'base' declaration in X3D spec ?
145  */
146  char *URLbase;
147 
148  /* Temporary directory:
149  (each main file/world has its own temp dir)
150  - if parent != NULL, use parent's dir,
151  - else use own's dir
152  */
153  char *temp_dir;
154 
155  /* if we have a # character in it (eg, proto, Anchor) we'll have a mallocd bit of
156  memory here */
157  char *afterPoundCharacters;
158 
159 
160 /*
161  * Note on temp dir: to improve user experience, each time a temporary
162  * file is created to mirror a network resource, we
163  * use the temporary directory created specificaly
164  * for this main file/world : this way the user (or
165  * a future menu in FreeWRL) can pack all that stuff
166  * more easily.
167  */
168 
169  /* Parsed request:
170  - complete url to network file
171  - complete path to local file
172  */
173  char *parsed_request;
174 
175  /* Cached files: first is actual file to read,
176  other are intermediate: zipped file,
177  file not in good format, ...
178  */
179  char *actual_file;
180  void *cached_files;
181 
182  /* Openned files: to be able to close them. */
183  void *openned_files;
184 
185  /* Convenient */
186  char four_first_bytes[4];
187 
188  resource_media_type_t media_type;
189  int treat_as_root; //bandaid for .x3z doc.x3d to be seen as root res equivalent
190  pthread_t *_loadThread; //pthread_t * used for async_loading in middleLayer ML
191  void *tg; //gglobal context
192  int (*_loadFunc)(void *); //used for some experiments where the backend loads, but the frontend injects a load function
194 
195 extern resource_item_t *root_res;
196 
197 bool resource_init_base(resource_item_t *root_res);
198 
199 resource_item_t* resource_create_single(const char *request);
200 
201 /* Quick hack to not be forced to include Structs.h */
202 typedef struct Multi_String s_Multi_String_t;
203 resource_item_t* resource_create_multi(s_Multi_String_t *request);
204 resource_item_t* resource_create_multi0(s_Multi_String_t *request);
205 
206 resource_item_t* resource_create_from_string(const char *string);
207 
208 void push_resource_request(const char *request);
209 void resource_identify(resource_item_t *base, resource_item_t *resresource_identify);
210 bool resource_fetch(void *res);
211 
212 void resitem_enqueue(s_list_t *resitem);
213 bool resource_load(resource_item_t *res);
214 void resource_identify_type(resource_item_t *res);
215 void resource_destroy(resource_item_t *res);
216 void destroy_root_res();
217 
218 void resource_remove_child(resource_item_t *parent, resource_item_t *child);
219 
220 void send_resource_to_parser(resource_item_t *res);
221 void send_resource_to_parser_async(resource_item_t *res);
222 
223 bool send_resource_to_parser_if_available(resource_item_t *res);
224 
225 void fwl_resitem_setStatus(void *resp, int status);
226 
227 void resource_push_multi_request(struct Multi_String *request);
228 void resource_wait(resource_item_t *res);
229 
230 void resource_get_valid_url_from_multi(resource_item_t *parentPath, resource_item_t *res);
231 
232 void resource_dump(resource_item_t *res);
233 void resource_tree_dump(int level, resource_item_t *root);
234 void resource_tree_list_files(int level, resource_item_t *root);
235 void resource_tree_destroy();
236 
237 char *resourceStatusToString(int status);
238 char *resourceTypeToString(int type);
239 char *resourceMediaTypeToString(int type);
240 
241 /* Initial URL loaded : replace IS_WORLD_LOADED */
242 extern bool resource_is_root_loaded();
243 void frontenditem_enqueue(s_list_t *item);
244 
245 void popInputResource();
246 void pushInputResource(resource_item_t *url);
247 resource_item_t *getInputResource();
248 
249 bool imagery_load(resource_item_t *res);
250 bool movie_load(resource_item_t *res);
251 
252 #endif /* __LIBFREEWRL_RESOURCES_H__ */
Definition: list.h:37