Search
lxdream.org :: lxdream/src/util.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/util.c
changeset 1065:bc1cc0c54917
prev1042:0fd066956482
prev564:dc7b5ffb0535
next1091:186558374345
author nkeynes
date Sun Jul 05 13:52:50 2009 +1000 (12 years ago)
permissions -rw-r--r--
last change No-op merge lxdream-mmu to remove head (actually merged long ago)
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Miscellaneous utility functions.
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     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.
    12  *
    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.
    17  */
    19 #define HAVE_EXECINFO_H 1
    21 #include <assert.h>
    22 #include <ctype.h>
    23 #include <stdarg.h>
    24 #include <stdio.h>
    25 #include <stdlib.h>
    26 #include <signal.h>
    27 #include <time.h>
    28 #include <zlib.h>
    29 #include <glib.h>
    30 #include <png.h>
    31 #include "dream.h"
    32 #include "display.h"
    33 #include "dreamcast.h"
    34 #include "gui.h"
    35 #include "sh4/sh4.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 )
    41 {
    42     char buf[128];
    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() );
    47     system(buf);
    49     abort();
    50 }
    52 void install_crash_handler(void)
    53 {
    54     struct sigaction sa;
    56     sa.sa_sigaction = report_crash;
    57     sigemptyset(&sa.sa_mask);
    58     sa.sa_flags = SA_RESETHAND|SA_SIGINFO;
    59     sigaction( SIGSEGV, &sa, NULL );
    60 }
    63 void fwrite_string( const char *s, FILE *f )
    64 {
    65     uint32_t len = 0;
    66     if( s == NULL ) {
    67         fwrite( &len, sizeof(len), 1, f );
    68     } else {
    69         len = strlen(s)+1;
    70         fwrite( &len, sizeof(len), 1, f );
    71         fwrite( s, len, 1, f );
    72     }
    73 }
    75 int fread_string( char *s, int maxlen, FILE *f ) 
    76 {
    77     uint32_t len;
    78     fread( &len, sizeof(len), 1, f );
    79     if( len != 0 ) {
    80         fread( s, len > maxlen ? maxlen : len, 1, f );
    81     }
    82     return len;
    83 }
    85 int fwrite_gzip( void *p, size_t sz, size_t count, FILE *f )
    86 {
    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 );
    95     g_free(tmp);
    97     /* Could be finer-grained, but this is enough to know it succeeded/failed */
    98     if( written == 1 ) {
    99         return count;
   100     } else {
   101         return 0;
   102     }
   103 }
   105 int fread_gzip( void *p, size_t sz, size_t count, FILE *f )
   106 {
   107     uLongf size = sz*count;
   108     uint32_t csize;
   109     unsigned char *tmp;
   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 );
   116     g_free(tmp);
   117     if( status == Z_OK ) {
   118         return count;
   119     } else {
   120         fprintf( stderr, "Error reading compressed data\n" );
   121         return 0;
   122     }
   123 }
   125 void fwrite_dump( unsigned char *data, unsigned int length, FILE *f ) 
   126 {
   127     unsigned int i, j;
   128     for( i =0; i<length; i+=16 ) {
   129         fprintf( f, "%08X:", i);
   130         for( j=i; j<i+16; j++ ) {
   131             if( (j % 4) == 0 )
   132                 fprintf( f, " " );
   133             if( j < length )
   134                 fprintf( f, " %02X", (unsigned int)(data[j]) );
   135             else
   136                 fprintf( f, "   " );
   137         }
   138         fprintf( f, "  " );
   139         for( j=i; j<i+16 && j<length; j++ ) {
   140             fprintf( f, "%c", isprint(data[j]) ? data[j] : '.' );
   141         }
   142         fprintf( f, "\n" );
   143     }
   144 }
   146 void fwrite_dump32( unsigned int *data, unsigned int length, FILE *f ) 
   147 {
   148     fwrite_dump32v( data, length, 8, f );
   149 }
   151 void fwrite_dump32v( unsigned int *data, unsigned int length, int wordsPerLine, FILE *f ) 
   152 {
   153     unsigned int i, j;
   154     for( i =0; i<length>>2; i+=wordsPerLine ) {
   155         fprintf( f, "%08X:", i);
   156         for( j=i; j<i+wordsPerLine; j++ ) {
   157             if( j < length )
   158                 fprintf( f, " %08X", (unsigned int)(data[j]) );
   159             else
   160                 fprintf( f, "         " );
   161         }
   162         fprintf( f, "\n" );
   163     }
   164 }
   166 gboolean write_png_to_stream( FILE *f, frame_buffer_t buffer )
   167 {
   168     int coltype, i;
   169     png_bytep p;
   170     png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
   171     if (!png_ptr) {
   172         return FALSE;
   173     }
   175     png_infop info_ptr = png_create_info_struct(png_ptr);
   176     if (!info_ptr) {
   177         png_destroy_write_struct(&png_ptr, NULL);
   178         return FALSE;
   179     }
   181     if( setjmp(png_jmpbuf(png_ptr)) ) {
   182         png_destroy_write_struct(&png_ptr, &info_ptr);
   183         return FALSE;
   184     }
   185     png_init_io( png_ptr, f );
   186     switch( buffer->colour_format ) {
   187     case COLFMT_BGR888:
   188         coltype = PNG_COLOR_TYPE_RGB;
   189         break;
   190     case COLFMT_BGRA8888:
   191         coltype = PNG_COLOR_TYPE_RGB_ALPHA;
   192         break;
   193     case COLFMT_BGR0888:
   194         coltype = PNG_COLOR_TYPE_RGB;
   195         break;
   196     default:
   197         coltype = PNG_COLOR_TYPE_RGB;
   198     }
   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);
   206     }
   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;
   213         }
   214     } else {
   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;
   219         }
   220     }
   221     png_write_end(png_ptr, info_ptr);
   222     png_destroy_write_struct(&png_ptr, &info_ptr);
   223     return TRUE;
   224 }
   226 frame_buffer_t read_png_from_stream( FILE *f )
   227 {
   228     png_bytep p;
   229     int i;
   230     png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 
   231             NULL, NULL, NULL);
   232     if (!png_ptr) {
   233         return NULL;
   234     }
   236     png_infop info_ptr = png_create_info_struct(png_ptr);
   237     if (!info_ptr) {
   238         png_destroy_read_struct(&png_ptr, NULL, NULL);
   239         return NULL;
   240     }
   242     png_infop end_info = png_create_info_struct(png_ptr);
   243     if (!end_info) {
   244         png_destroy_read_struct(&png_ptr, &info_ptr, NULL );
   245         return NULL;
   246     }
   248     if( setjmp(png_jmpbuf(png_ptr)) ) {
   249         png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
   250         return NULL;
   251     }
   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;
   277     }
   279     p = (png_bytep)buffer->data;
   280     for( i=0; i<height; i++ ) {
   281         png_read_row(png_ptr, p, NULL );
   282         p += rowbytes;
   283     }
   285     png_read_end(png_ptr, end_info);
   286     png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
   287     return buffer;
   288 }
   290 int get_log_level_from_string( const gchar *str )
   291 {
   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;
   299     default: return -1;
   300     }
   301 }
   303 gboolean set_global_log_level( const gchar *str ) 
   304 {
   305     int l = get_log_level_from_string(str);
   306     if( l == -1 ) {
   307         return FALSE;
   308     } else {
   309         global_msg_level = l;
   310         return TRUE;
   311     }
   312 }
   314 void log_message( void *ptr, int level, const gchar *source, const char *msg, ... )
   315 {
   316     char buf[20];
   317     time_t tm = time(NULL);
   318     va_list ap;
   320     if( level > global_msg_level ) {
   321         return; // ignored
   322     }
   324     va_start(ap, msg);
   325     gchar *text = g_strdup_vprintf( msg, ap );
   326     va_end(ap);
   328     if( level <= EMIT_ERR ) {
   329         if( gui_error_dialog( text ) ) {
   330             g_free(text);
   331             // If we're running, halt on error to avoid potentially flooding
   332             // the user with error messages.
   333             if( dreamcast_is_running() ) {
   334                 dreamcast_stop();
   335             }
   336             return;
   337         }
   338     }
   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 );
   343     g_free(text);
   344 }
.