FreeWRL/FreeX3D  3.0.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 
48 size_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 
58 char *__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 
75 void 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 
103 void internalc_init(struct tinternalc* ic)
104 {
105  //public
106 ic->global_strictParsing = FALSE;
107 ic->global_plugin_print = FALSE;
108 ic->global_occlusion_disable = FALSE;
109 ic->user_request_texture_size = 0;
110 ic->global_print_opengl_errors = FALSE;
111 ic->global_trace_threads = FALSE;
112 
113  //private
114 }
115 
116 
117 /* Set up global environment, usually from environment variables */
118 void 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 }
126 void fwl_set_plugin_print (bool flag) { gglobal()->internalc.global_plugin_print = flag ; }
127 void fwl_set_occlusion_disable (bool flag) { gglobal()->internalc.global_occlusion_disable = flag; }
128 void fwl_set_print_opengl_errors(bool flag) { gglobal()->internalc.global_print_opengl_errors = flag;}
129 void fwl_set_trace_threads (bool flag) { gglobal()->internalc.global_trace_threads = flag;}
130 
131 void fwl_set_texture_size (unsigned int texture_size) {
132 
133  // save this one.
134  s_renderer_capabilities_t *rdr_caps;
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 }
152 
153 #ifdef FREEWRL_THREAD_COLORIZED
154 
155 /* == Interal printf and fprintf function to output colors ==
156  See threads.c for details.
157 */
158 
159 int printf_with_colored_threads(const char *format, ...)
160 {
161  int ret;
162  va_list args;
163  va_start( args, format );
164 
165  printf("\033[22;%im", fw_thread_color(fw_thread_id()));
166 
167  ret = vprintf( format, args );
168 
169  printf("\033[22;%im", 39 /* Default color */);
170 
171  va_end( args );
172 
173  return ret;
174 }
175 
176 int fprintf_with_colored_threads(FILE *stream, const char *format, ...)
177 {
178  int ret;
179  va_list args;
180  va_start( args, format );
181 
182  fprintf(stream, "\033[22;%im", fw_thread_color(fw_thread_id()));
183 
184  ret = vfprintf( stream, format, args );
185 
186  fprintf(stream, "\033[22;%im", 39 /* Default color */);
187 
188  va_end( args );
189 
190  return ret;
191 }
192 
193 #endif