Search
lxdream.org :: lxdream/src/util.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/util.c
changeset 736:a02d1475ccfd
prev674:377d987db8f2
next768:b2a54f6864eb
author nkeynes
date Mon Jul 21 00:08:34 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Add gettext.h and build sanely without libintl if it's not available
Remove x86dasm's config.h & opintl.h (no longer needed and actually wrong)
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 "gui.h"
    34 #include "sh4/sh4.h"
    36 char *msg_levels[] = { "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE" };
    37 int global_msg_level = EMIT_WARN;
    39 static void report_crash( int signo, siginfo_t *info, void *ptr )
    40 {
    41     char buf[128];
    43     fprintf( stderr, "--- Aborting with signal %d ---\n", signo );
    44     // Get gdb to print a nice backtrace for us
    45     snprintf( buf, 128, "gdb -batch -f --quiet --pid=%d -ex bt", getpid() );
    46     system(buf);
    48     abort();
    49 }
    51 void install_crash_handler(void)
    52 {
    53     struct sigaction sa;
    55     sa.sa_sigaction = report_crash;
    56     sigemptyset(&sa.sa_mask);
    57     sa.sa_flags = SA_RESETHAND|SA_SIGINFO;
    58     sigaction( SIGSEGV, &sa, NULL );
    59 }
    62 void fwrite_string( const char *s, FILE *f )
    63 {
    64     uint32_t len = 0;
    65     if( s == NULL ) {
    66         fwrite( &len, sizeof(len), 1, f );
    67     } else {
    68         len = strlen(s)+1;
    69         fwrite( &len, sizeof(len), 1, f );
    70         fwrite( s, len, 1, f );
    71     }
    72 }
    74 int fread_string( char *s, int maxlen, FILE *f ) 
    75 {
    76     uint32_t len;
    77     fread( &len, sizeof(len), 1, f );
    78     if( len != 0 ) {
    79         fread( s, len > maxlen ? maxlen : len, 1, f );
    80     }
    81     return len;
    82 }
    84 void fwrite_gzip( void *p, size_t sz, size_t count, FILE *f )
    85 {
    86     uLongf size = sz*count;
    87     uLongf csize = ((int)(size*1.001))+13;
    88     unsigned char *tmp = g_malloc0( csize );
    89     int status = compress( tmp, &csize, p, size );
    90     assert( status == Z_OK );
    91     uint32_t wsize = (uint32_t)csize;
    92     fwrite( &wsize, sizeof(wsize), 1, f );
    93     fwrite( tmp, csize, 1, f );
    94     g_free(tmp);
    95 }
    97 int fread_gzip( void *p, size_t sz, size_t count, FILE *f )
    98 {
    99     uLongf size = sz*count;
   100     uint32_t csize;
   101     unsigned char *tmp;
   103     fread( &csize, sizeof(csize), 1, f );
   104     assert( csize <= (size*2) );
   105     tmp = g_malloc0( csize );
   106     fread( tmp, csize, 1, f );
   107     int status = uncompress( p, &size, tmp, csize );
   108     g_free(tmp);
   109     if( status == Z_OK ) {
   110         return count;
   111     } else {
   112         fprintf( stderr, "Error reading compressed data\n" );
   113         return 0;
   114     }
   115 }
   117 void fwrite_dump( unsigned char *data, unsigned int length, FILE *f ) 
   118 {
   119     unsigned int i, j;
   120     for( i =0; i<length; i+=16 ) {
   121         fprintf( f, "%08X:", i);
   122         for( j=i; j<i+16; j++ ) {
   123             if( (j % 4) == 0 )
   124                 fprintf( f, " " );
   125             if( j < length )
   126                 fprintf( f, " %02X", (unsigned int)(data[j]) );
   127             else
   128                 fprintf( f, "   " );
   129         }
   130         fprintf( f, "  " );
   131         for( j=i; j<i+16 && j<length; j++ ) {
   132             fprintf( f, "%c", isprint(data[j]) ? data[j] : '.' );
   133         }
   134         fprintf( f, "\n" );
   135     }
   136 }
   138 void fwrite_dump32( unsigned int *data, unsigned int length, FILE *f ) 
   139 {
   140     fwrite_dump32v( data, length, 8, f );
   141 }
   143 void fwrite_dump32v( unsigned int *data, unsigned int length, int wordsPerLine, FILE *f ) 
   144 {
   145     unsigned int i, j;
   146     for( i =0; i<length>>2; i+=wordsPerLine ) {
   147         fprintf( f, "%08X:", i);
   148         for( j=i; j<i+wordsPerLine; j++ ) {
   149             if( j < length )
   150                 fprintf( f, " %08X", (unsigned int)(data[j]) );
   151             else
   152                 fprintf( f, "         " );
   153         }
   154         fprintf( f, "\n" );
   155     }
   156 }
   158 gboolean write_png_to_stream( FILE *f, frame_buffer_t buffer )
   159 {
   160     int coltype, i;
   161     png_bytep p;
   162     png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
   163     if (!png_ptr) {
   164         return FALSE;
   165     }
   167     png_infop info_ptr = png_create_info_struct(png_ptr);
   168     if (!info_ptr) {
   169         png_destroy_write_struct(&png_ptr, NULL);
   170         return FALSE;
   171     }
   173     if( setjmp(png_jmpbuf(png_ptr)) ) {
   174         png_destroy_write_struct(&png_ptr, &info_ptr);
   175         return FALSE;
   176     }
   177     png_init_io( png_ptr, f );
   178     switch( buffer->colour_format ) {
   179     case COLFMT_BGR888:
   180         coltype = PNG_COLOR_TYPE_RGB;
   181         break;
   182     case COLFMT_BGRA8888:
   183         coltype = PNG_COLOR_TYPE_RGB_ALPHA;
   184         break;
   185     case COLFMT_BGR0888:
   186         coltype = PNG_COLOR_TYPE_RGB;
   187         break;
   188     default:
   189         coltype = PNG_COLOR_TYPE_RGB;
   190     }
   191     png_set_IHDR(png_ptr, info_ptr, buffer->width, buffer->height,
   192                  8, coltype, PNG_INTERLACE_NONE, 
   193                  PNG_COMPRESSION_TYPE_DEFAULT, 
   194                  PNG_FILTER_TYPE_DEFAULT );
   195     png_write_info(png_ptr, info_ptr);
   196     if( buffer->colour_format == COLFMT_BGR0888 ) {
   197         png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
   198     }
   199     png_set_bgr(png_ptr);
   200     if( buffer->inverted ) {
   201         p = (png_bytep)(buffer->data + (buffer->height*buffer->rowstride) - buffer->rowstride);
   202         for(i=0; i<buffer->height; i++ ) {
   203             png_write_row(png_ptr, p);
   204             p-=buffer->rowstride;
   205         }
   206     } else {
   207         p = (png_bytep)buffer->data;
   208         for(i=0; i<buffer->height; i++ ) {
   209             png_write_row(png_ptr, p);
   210             p+=buffer->rowstride;
   211         }
   212     }
   213     png_write_end(png_ptr, info_ptr);
   214     png_destroy_write_struct(&png_ptr, &info_ptr);
   215     return TRUE;
   216 }
   218 frame_buffer_t read_png_from_stream( FILE *f )
   219 {
   220     png_bytep p;
   221     int i;
   222     png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 
   223             NULL, NULL, NULL);
   224     if (!png_ptr) {
   225         return NULL;
   226     }
   228     png_infop info_ptr = png_create_info_struct(png_ptr);
   229     if (!info_ptr) {
   230         png_destroy_read_struct(&png_ptr, NULL, NULL);
   231         return NULL;
   232     }
   234     png_infop end_info = png_create_info_struct(png_ptr);
   235     if (!end_info) {
   236         png_destroy_read_struct(&png_ptr, &info_ptr, NULL );
   237         return NULL;
   238     }
   240     if( setjmp(png_jmpbuf(png_ptr)) ) {
   241         png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
   242         return NULL;
   243     }
   245     png_init_io(png_ptr, f);
   246     png_read_info(png_ptr, info_ptr);
   248     png_uint_32 width, height;
   249     int bit_depth, color_type, interlace_type,
   250     compression_type, filter_method;
   251     png_get_IHDR(png_ptr, info_ptr, &width, &height,
   252                  &bit_depth, &color_type, &interlace_type,
   253                  &compression_type, &filter_method);
   254     assert( interlace_type == PNG_INTERLACE_NONE );
   255     int rowbytes = png_get_rowbytes(png_ptr, info_ptr);
   256     int channels = png_get_channels(png_ptr, info_ptr);
   257     frame_buffer_t buffer = g_malloc( sizeof(struct frame_buffer) + rowbytes*height );
   258     buffer->data = (unsigned char *)(buffer+1);
   259     buffer->width = width;
   260     buffer->height = height;
   261     buffer->rowstride = rowbytes;
   262     buffer->address = -1;
   263     buffer->size = rowbytes*height;
   264     buffer->inverted = FALSE;
   265     if( channels == 4 ) {
   266         buffer->colour_format = COLFMT_BGRA8888;
   267     } else if( channels == 3 ) {
   268         buffer->colour_format = COLFMT_RGB888;
   269     }
   271     p = (png_bytep)buffer->data;
   272     for( i=0; i<height; i++ ) {
   273         png_read_row(png_ptr, p, NULL );
   274         p += rowbytes;
   275     }
   277     png_read_end(png_ptr, end_info);
   278     png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
   279     return buffer;
   280 }
   282 int get_log_level_from_string( const gchar *str )
   283 {
   284     switch( tolower(str[0]) ) {
   285     case 'd': return EMIT_DEBUG;
   286     case 'e': return EMIT_ERR;
   287     case 'f': return EMIT_FATAL;
   288     case 'i': return EMIT_INFO;
   289     case 't': return EMIT_TRACE;
   290     case 'w': return EMIT_WARN;
   291     default: return -1;
   292     }
   293 }
   295 gboolean set_global_log_level( const gchar *str ) 
   296 {
   297     int l = get_log_level_from_string(str);
   298     if( l == -1 ) {
   299         return FALSE;
   300     } else {
   301         global_msg_level = l;
   302         return TRUE;
   303     }
   304 }
   306 void log_message( void *ptr, int level, const gchar *source, const char *msg, ... )
   307 {
   308     char buf[20];
   309     time_t tm = time(NULL);
   310     va_list ap;
   312     if( level > global_msg_level ) {
   313         return; // ignored
   314     }
   316     va_start(ap, msg);
   317     gchar *text = g_strdup_vprintf( msg, ap );
   318     va_end(ap);
   320     if( level <= EMIT_ERR ) {
   321         if( gui_error_dialog( text ) ) {
   322             g_free(text);
   323             return;
   324         }
   325     }
   328     strftime( buf, sizeof(buf), "%H:%M:%S", localtime(&tm) );
   329     fprintf( stderr, "%s %08X %-5s %s\n", buf, sh4r.pc, msg_levels[level], text );
   330     g_free(text);
   331 }
.