FreeWRL / FreeX3D 4.3.0
internal.c
1/*
2
3 FreeWRL support library.
4 Internal functions: some very usefull functions are not always
5 present (example: strndup, ...).
6
7*/
8
9/****************************************************************************
10 This file is part of the FreeWRL/FreeX3D Distribution.
11
12 Copyright 2009 CRC Canada. (http://www.crc.gc.ca)
13
14 FreeWRL/FreeX3D is free software: you can redistribute it and/or modify
15 it under the terms of the GNU Lesser Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 FreeWRL/FreeX3D is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with FreeWRL/FreeX3D. If not, see <http://www.gnu.org/licenses/>.
26****************************************************************************/
27
28
29#include <config.h>
30#include <system.h>
31#include <internal.h>
32#include <libFreeWRL.h>
33#include <threads.h>
34
35#if defined(HAVE_STDARG_H)
36# include <stdarg.h>
37#endif
38
39#if defined(HAVE_ERRNO_H)
40# include <errno.h>
41#endif
42
43#if !defined(HAVE_STRNLEN)
44
45/* Find the length of STRING, but scan at most MAXLEN characters.
46 If no '\0' terminator is found in that many characters, return MAXLEN. */
47
48size_t __fw_strnlen(const char *s, size_t maxlen)
49{
50 const char *end = memchr(s, '\0', maxlen);
51 return end ? (size_t) (end - s) : maxlen;
52}
53
54#endif
55
56#if !defined(HAVE_STRNDUP)
57
58char *__fw_strndup(const char *s, size_t n)
59{
60 size_t len = strnlen(s, n);
61 char *new = MALLOC(char *, len + 1);
62
63 if (!new)
64 return NULL;
65
66 new[len] = '\0';
67 memcpy(new, s, len);
68 /* although we could return the output of memcpy, OSX cacks on it, so return mallocd area */
69 return new;
70}
71
72#endif
73
74
75void fw_perror(FILE *f, const char *format, ...)
76{
77 int e;
78 va_list ap;
79
80 va_start(ap, format);
81 e = errno;
82 vfprintf(f, format, ap);
83 va_end(ap);
84
85#ifdef HAVE_STRERROR
86 FPRINTF(f, "[System error: %s]\n", strerror(e));
87#else
88 FPRINTF(f, "[System error: %d]\n", e);
89#endif
90 fflush(f);
91}
92
93/* Global FreeWRL options (will become profiles ?) */
94
95//bool global_strictParsing = FALSE;
96//bool global_plugin_print = FALSE;
97//bool global_occlusion_disable = FALSE;
98//bool global_print_opengl_errors = FALSE;
99//bool global_trace_threads = FALSE;
100//
101
102
103void internalc_init(struct tinternalc* ic)
104{
105 //public
106ic->global_strictParsing = FALSE;
107ic->global_plugin_print = FALSE;
108ic->global_occlusion_disable = FALSE;
109ic->user_request_texture_size = 0;
110ic->global_print_opengl_errors = FALSE;
111ic->global_trace_threads = FALSE;
112
113 //private
114}
115
116
117/* Set up global environment, usually from environment variables */
118void fwl_set_strictParsing (bool flag) {
119 gglobal()->internalc.global_strictParsing = flag ;
120
121 //struct tinternalc *ic = &gglobal()->internalc;
122 //ic->global_strictParsing = flag ;
123
124 //getchar();
125}
126void fwl_set_plugin_print (bool flag) { gglobal()->internalc.global_plugin_print = flag ; }
127void fwl_set_occlusion_disable (bool flag) { gglobal()->internalc.global_occlusion_disable = flag; }
128void fwl_set_print_opengl_errors(bool flag) { gglobal()->internalc.global_print_opengl_errors = flag;}
129void fwl_set_trace_threads (bool flag) { gglobal()->internalc.global_trace_threads = flag;}
130
131void fwl_set_texture_size (unsigned int texture_size) {
132
133 // save this one.
135 ttglobal tg = gglobal();
136 tg->internalc.user_request_texture_size = texture_size;
137 rdr_caps = tg->display.rdr_caps;
138
139 // how does this fit with our current system?
140 // is it BIGGER than we can support in hardware?
141 // eg, are we asking for 2048, but system supports 1024 max?
142 if (texture_size > rdr_caps->system_max_texture_size)
143 rdr_caps->runtime_max_texture_size = rdr_caps->system_max_texture_size;
144 else
145 // it is ok, smaller than the system hardware support.
146 rdr_caps->runtime_max_texture_size = texture_size;
147
148 //ConsoleMessage ("user request texture size %d, system %d, runtime %d",texture_size,
149 //gglobal()->display.rdr_caps.system_max_texture_size,
150 //gglobal()->display.rdr_caps.runtime_max_texture_size);
151}
152unsigned int fwl_get_texture_size() {
154 ttglobal tg = gglobal();
155 rdr_caps = tg->display.rdr_caps;
156
157 return rdr_caps->runtime_max_texture_size;
158}
159#ifdef FREEWRL_THREAD_COLORIZED
160
161/* == Interal printf and fprintf function to output colors ==
162 See threads.c for details.
163*/
164
165int printf_with_colored_threads(const char *format, ...)
166{
167 int ret;
168 va_list args;
169 va_start( args, format );
170
171 printf("\033[22;%im", fw_thread_color(fw_thread_id()));
172
173 ret = vprintf( format, args );
174
175 printf("\033[22;%im", 39 /* Default color */);
176
177 va_end( args );
178
179 return ret;
180}
181
182int fprintf_with_colored_threads(FILE *stream, const char *format, ...)
183{
184 int ret;
185 va_list args;
186 va_start( args, format );
187
188 fprintf(stream, "\033[22;%im", fw_thread_color(fw_thread_id()));
189
190 ret = vfprintf( stream, format, args );
191
192 fprintf(stream, "\033[22;%im", 39 /* Default color */);
193
194 va_end( args );
195
196 return ret;
197}
198
199#endif