4 * Miscellaneous utility functions.
6 * Copyright (c) 2005 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #define HAVE_EXECINFO_H 1
33 #include "dreamcast.h"
37 char *msg_levels[] = { "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE" };
38 int global_msg_level = EMIT_WARN;
40 static void report_crash( int signo, siginfo_t *info, void *ptr )
44 fprintf( stderr, "--- Aborting with signal %d ---\n", signo );
45 // Get gdb to print a nice backtrace for us
46 snprintf( buf, 128, "gdb -batch -f --quiet --pid=%d -ex bt", getpid() );
52 void install_crash_handler(void)
56 sa.sa_sigaction = report_crash;
57 sigemptyset(&sa.sa_mask);
58 sa.sa_flags = SA_RESETHAND|SA_SIGINFO;
59 sigaction( SIGSEGV, &sa, NULL );
63 void fwrite_string( const char *s, FILE *f )
67 fwrite( &len, sizeof(len), 1, f );
70 fwrite( &len, sizeof(len), 1, f );
71 fwrite( s, len, 1, f );
75 int fread_string( char *s, int maxlen, FILE *f )
78 fread( &len, sizeof(len), 1, f );
80 fread( s, len > maxlen ? maxlen : len, 1, f );
85 int fwrite_gzip( void *p, size_t sz, size_t count, FILE *f )
87 uLongf size = sz*count;
88 uLongf csize = ((int)(size*1.001))+13;
89 unsigned char *tmp = g_malloc0( csize );
90 int status = compress( tmp, &csize, p, size );
91 assert( status == Z_OK );
92 uint32_t wsize = (uint32_t)csize;
93 fwrite( &wsize, sizeof(wsize), 1, f );
94 int written = fwrite( tmp, csize, 1, f );
97 /* Could be finer-grained, but this is enough to know it succeeded/failed */
105 int fread_gzip( void *p, size_t sz, size_t count, FILE *f )
107 uLongf size = sz*count;
111 fread( &csize, sizeof(csize), 1, f );
112 assert( csize <= (size*2) );
113 tmp = g_malloc0( csize );
114 fread( tmp, csize, 1, f );
115 int status = uncompress( p, &size, tmp, csize );
117 if( status == Z_OK ) {
120 fprintf( stderr, "Error reading compressed data\n" );
125 void fwrite_dump( unsigned char *data, unsigned int length, FILE *f )
128 for( i =0; i<length; i+=16 ) {
129 fprintf( f, "%08X:", i);
130 for( j=i; j<i+16; j++ ) {
134 fprintf( f, " %02X", (unsigned int)(data[j]) );
139 for( j=i; j<i+16 && j<length; j++ ) {
140 fprintf( f, "%c", isprint(data[j]) ? data[j] : '.' );
146 void fwrite_dump32( unsigned int *data, unsigned int length, FILE *f )
148 fwrite_dump32v( data, length, 8, f );
151 void fwrite_dump32v( unsigned int *data, unsigned int length, int wordsPerLine, FILE *f )
154 for( i =0; i<length>>2; i+=wordsPerLine ) {
155 fprintf( f, "%08X:", i);
156 for( j=i; j<i+wordsPerLine; j++ ) {
158 fprintf( f, " %08X", (unsigned int)(data[j]) );
166 gboolean write_png_to_stream( FILE *f, frame_buffer_t buffer )
170 png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
175 png_infop info_ptr = png_create_info_struct(png_ptr);
177 png_destroy_write_struct(&png_ptr, NULL);
181 if( setjmp(png_jmpbuf(png_ptr)) ) {
182 png_destroy_write_struct(&png_ptr, &info_ptr);
185 png_init_io( png_ptr, f );
186 switch( buffer->colour_format ) {
188 coltype = PNG_COLOR_TYPE_RGB;
190 case COLFMT_BGRA8888:
191 coltype = PNG_COLOR_TYPE_RGB_ALPHA;
194 coltype = PNG_COLOR_TYPE_RGB;
197 coltype = PNG_COLOR_TYPE_RGB;
199 png_set_IHDR(png_ptr, info_ptr, buffer->width, buffer->height,
200 8, coltype, PNG_INTERLACE_NONE,
201 PNG_COMPRESSION_TYPE_DEFAULT,
202 PNG_FILTER_TYPE_DEFAULT );
203 png_write_info(png_ptr, info_ptr);
204 if( buffer->colour_format == COLFMT_BGR0888 ) {
205 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
207 png_set_bgr(png_ptr);
208 if( buffer->inverted ) {
209 p = (png_bytep)(buffer->data + (buffer->height*buffer->rowstride) - buffer->rowstride);
210 for(i=0; i<buffer->height; i++ ) {
211 png_write_row(png_ptr, p);
212 p-=buffer->rowstride;
215 p = (png_bytep)buffer->data;
216 for(i=0; i<buffer->height; i++ ) {
217 png_write_row(png_ptr, p);
218 p+=buffer->rowstride;
221 png_write_end(png_ptr, info_ptr);
222 png_destroy_write_struct(&png_ptr, &info_ptr);
226 frame_buffer_t read_png_from_stream( FILE *f )
230 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
236 png_infop info_ptr = png_create_info_struct(png_ptr);
238 png_destroy_read_struct(&png_ptr, NULL, NULL);
242 png_infop end_info = png_create_info_struct(png_ptr);
244 png_destroy_read_struct(&png_ptr, &info_ptr, NULL );
248 if( setjmp(png_jmpbuf(png_ptr)) ) {
249 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
253 png_init_io(png_ptr, f);
254 png_read_info(png_ptr, info_ptr);
256 png_uint_32 width, height;
257 int bit_depth, color_type, interlace_type,
258 compression_type, filter_method;
259 png_get_IHDR(png_ptr, info_ptr, &width, &height,
260 &bit_depth, &color_type, &interlace_type,
261 &compression_type, &filter_method);
262 assert( interlace_type == PNG_INTERLACE_NONE );
263 int rowbytes = png_get_rowbytes(png_ptr, info_ptr);
264 int channels = png_get_channels(png_ptr, info_ptr);
265 frame_buffer_t buffer = g_malloc( sizeof(struct frame_buffer) + rowbytes*height );
266 buffer->data = (unsigned char *)(buffer+1);
267 buffer->width = width;
268 buffer->height = height;
269 buffer->rowstride = rowbytes;
270 buffer->address = -1;
271 buffer->size = rowbytes*height;
272 buffer->inverted = FALSE;
273 if( channels == 4 ) {
274 buffer->colour_format = COLFMT_BGRA8888;
275 } else if( channels == 3 ) {
276 buffer->colour_format = COLFMT_RGB888;
279 p = (png_bytep)buffer->data;
280 for( i=0; i<height; i++ ) {
281 png_read_row(png_ptr, p, NULL );
285 png_read_end(png_ptr, end_info);
286 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
290 int get_log_level_from_string( const gchar *str )
292 switch( tolower(str[0]) ) {
293 case 'd': return EMIT_DEBUG;
294 case 'e': return EMIT_ERR;
295 case 'f': return EMIT_FATAL;
296 case 'i': return EMIT_INFO;
297 case 't': return EMIT_TRACE;
298 case 'w': return EMIT_WARN;
303 gboolean set_global_log_level( const gchar *str )
305 int l = get_log_level_from_string(str);
309 global_msg_level = l;
314 void log_message( void *ptr, int level, const gchar *source, const char *msg, ... )
317 time_t tm = time(NULL);
320 if( level > global_msg_level ) {
325 gchar *text = g_strdup_vprintf( msg, ap );
328 if( level <= EMIT_ERR ) {
329 if( gui_error_dialog( text ) ) {
331 // If we're running, halt on error to avoid potentially flooding
332 // the user with error messages.
333 if( dreamcast_is_running() ) {
341 strftime( buf, sizeof(buf), "%H:%M:%S", localtime(&tm) );
342 fprintf( stderr, "%s %08X %-5s %s\n", buf, sh4r.pc, msg_levels[level], text );
.