Search
lxdream.org :: lxdream/src/util.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/util.c
changeset 1042:0fd066956482
prev768:b2a54f6864eb
next1065:bc1cc0c54917
author nkeynes
date Fri Jun 26 13:53:54 2009 +0000 (12 years ago)
permissions -rw-r--r--
last change Do the save-to-temp and rename thing when saving VMUs, for the sake of limiting corruption possibilities
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@586
     2
 * $Id$
nkeynes@31
     3
 *
nkeynes@31
     4
 * Miscellaneous utility functions.
nkeynes@31
     5
 *
nkeynes@31
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@31
     7
 *
nkeynes@31
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@31
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@31
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@31
    11
 * (at your option) any later version.
nkeynes@31
    12
 *
nkeynes@31
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@31
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@31
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@31
    16
 * GNU General Public License for more details.
nkeynes@31
    17
 */
nkeynes@31
    18
nkeynes@495
    19
#define HAVE_EXECINFO_H 1
nkeynes@495
    20
nkeynes@477
    21
#include <assert.h>
nkeynes@425
    22
#include <ctype.h>
nkeynes@437
    23
#include <stdarg.h>
nkeynes@437
    24
#include <stdio.h>
nkeynes@437
    25
#include <stdlib.h>
nkeynes@495
    26
#include <signal.h>
nkeynes@437
    27
#include <time.h>
nkeynes@477
    28
#include <zlib.h>
nkeynes@477
    29
#include <glib.h>
nkeynes@477
    30
#include <png.h>
nkeynes@17
    31
#include "dream.h"
nkeynes@477
    32
#include "display.h"
nkeynes@768
    33
#include "dreamcast.h"
nkeynes@481
    34
#include "gui.h"
nkeynes@586
    35
#include "sh4/sh4.h"
nkeynes@437
    36
nkeynes@437
    37
char *msg_levels[] = { "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE" };
nkeynes@502
    38
int global_msg_level = EMIT_WARN;
nkeynes@495
    39
nkeynes@495
    40
static void report_crash( int signo, siginfo_t *info, void *ptr )
nkeynes@495
    41
{
nkeynes@495
    42
    char buf[128];
nkeynes@495
    43
nkeynes@495
    44
    fprintf( stderr, "--- Aborting with signal %d ---\n", signo );
nkeynes@495
    45
    // Get gdb to print a nice backtrace for us
nkeynes@495
    46
    snprintf( buf, 128, "gdb -batch -f --quiet --pid=%d -ex bt", getpid() );
nkeynes@495
    47
    system(buf);
nkeynes@495
    48
nkeynes@495
    49
    abort();
nkeynes@495
    50
}
nkeynes@495
    51
nkeynes@495
    52
void install_crash_handler(void)
nkeynes@495
    53
{
nkeynes@495
    54
    struct sigaction sa;
nkeynes@495
    55
nkeynes@495
    56
    sa.sa_sigaction = report_crash;
nkeynes@495
    57
    sigemptyset(&sa.sa_mask);
nkeynes@495
    58
    sa.sa_flags = SA_RESETHAND|SA_SIGINFO;
nkeynes@495
    59
    sigaction( SIGSEGV, &sa, NULL );
nkeynes@495
    60
}
nkeynes@495
    61
nkeynes@17
    62
nkeynes@422
    63
void fwrite_string( const char *s, FILE *f )
nkeynes@17
    64
{
nkeynes@17
    65
    uint32_t len = 0;
nkeynes@17
    66
    if( s == NULL ) {
nkeynes@736
    67
        fwrite( &len, sizeof(len), 1, f );
nkeynes@17
    68
    } else {
nkeynes@736
    69
        len = strlen(s)+1;
nkeynes@736
    70
        fwrite( &len, sizeof(len), 1, f );
nkeynes@736
    71
        fwrite( s, len, 1, f );
nkeynes@17
    72
    }
nkeynes@17
    73
}
nkeynes@17
    74
nkeynes@17
    75
int fread_string( char *s, int maxlen, FILE *f ) 
nkeynes@17
    76
{
nkeynes@17
    77
    uint32_t len;
nkeynes@17
    78
    fread( &len, sizeof(len), 1, f );
nkeynes@17
    79
    if( len != 0 ) {
nkeynes@736
    80
        fread( s, len > maxlen ? maxlen : len, 1, f );
nkeynes@17
    81
    }
nkeynes@17
    82
    return len;
nkeynes@17
    83
}
nkeynes@117
    84
nkeynes@1042
    85
int fwrite_gzip( void *p, size_t sz, size_t count, FILE *f )
nkeynes@477
    86
{
nkeynes@477
    87
    uLongf size = sz*count;
nkeynes@477
    88
    uLongf csize = ((int)(size*1.001))+13;
nkeynes@477
    89
    unsigned char *tmp = g_malloc0( csize );
nkeynes@477
    90
    int status = compress( tmp, &csize, p, size );
nkeynes@477
    91
    assert( status == Z_OK );
nkeynes@674
    92
    uint32_t wsize = (uint32_t)csize;
nkeynes@674
    93
    fwrite( &wsize, sizeof(wsize), 1, f );
nkeynes@1042
    94
    int written = fwrite( tmp, csize, 1, f );
nkeynes@477
    95
    g_free(tmp);
nkeynes@1042
    96
    
nkeynes@1042
    97
    /* Could be finer-grained, but this is enough to know it succeeded/failed */
nkeynes@1042
    98
    if( written == 1 ) {
nkeynes@1042
    99
        return count;
nkeynes@1042
   100
    } else {
nkeynes@1042
   101
        return 0;
nkeynes@1042
   102
    }
nkeynes@477
   103
}
nkeynes@477
   104
nkeynes@477
   105
int fread_gzip( void *p, size_t sz, size_t count, FILE *f )
nkeynes@477
   106
{
nkeynes@477
   107
    uLongf size = sz*count;
nkeynes@674
   108
    uint32_t csize;
nkeynes@477
   109
    unsigned char *tmp;
nkeynes@477
   110
nkeynes@477
   111
    fread( &csize, sizeof(csize), 1, f );
nkeynes@477
   112
    assert( csize <= (size*2) );
nkeynes@477
   113
    tmp = g_malloc0( csize );
nkeynes@477
   114
    fread( tmp, csize, 1, f );
nkeynes@477
   115
    int status = uncompress( p, &size, tmp, csize );
nkeynes@477
   116
    g_free(tmp);
nkeynes@477
   117
    if( status == Z_OK ) {
nkeynes@736
   118
        return count;
nkeynes@477
   119
    } else {
nkeynes@736
   120
        fprintf( stderr, "Error reading compressed data\n" );
nkeynes@736
   121
        return 0;
nkeynes@477
   122
    }
nkeynes@477
   123
}
nkeynes@477
   124
nkeynes@117
   125
void fwrite_dump( unsigned char *data, unsigned int length, FILE *f ) 
nkeynes@117
   126
{
nkeynes@117
   127
    unsigned int i, j;
nkeynes@117
   128
    for( i =0; i<length; i+=16 ) {
nkeynes@736
   129
        fprintf( f, "%08X:", i);
nkeynes@736
   130
        for( j=i; j<i+16; j++ ) {
nkeynes@736
   131
            if( (j % 4) == 0 )
nkeynes@736
   132
                fprintf( f, " " );
nkeynes@736
   133
            if( j < length )
nkeynes@736
   134
                fprintf( f, " %02X", (unsigned int)(data[j]) );
nkeynes@736
   135
            else
nkeynes@736
   136
                fprintf( f, "   " );
nkeynes@736
   137
        }
nkeynes@736
   138
        fprintf( f, "  " );
nkeynes@736
   139
        for( j=i; j<i+16 && j<length; j++ ) {
nkeynes@736
   140
            fprintf( f, "%c", isprint(data[j]) ? data[j] : '.' );
nkeynes@736
   141
        }
nkeynes@736
   142
        fprintf( f, "\n" );
nkeynes@117
   143
    }
nkeynes@117
   144
}
nkeynes@187
   145
nkeynes@187
   146
void fwrite_dump32( unsigned int *data, unsigned int length, FILE *f ) 
nkeynes@187
   147
{
nkeynes@220
   148
    fwrite_dump32v( data, length, 8, f );
nkeynes@220
   149
}
nkeynes@220
   150
nkeynes@220
   151
void fwrite_dump32v( unsigned int *data, unsigned int length, int wordsPerLine, FILE *f ) 
nkeynes@220
   152
{
nkeynes@187
   153
    unsigned int i, j;
nkeynes@220
   154
    for( i =0; i<length>>2; i+=wordsPerLine ) {
nkeynes@736
   155
        fprintf( f, "%08X:", i);
nkeynes@736
   156
        for( j=i; j<i+wordsPerLine; j++ ) {
nkeynes@736
   157
            if( j < length )
nkeynes@736
   158
                fprintf( f, " %08X", (unsigned int)(data[j]) );
nkeynes@736
   159
            else
nkeynes@736
   160
                fprintf( f, "         " );
nkeynes@736
   161
        }
nkeynes@736
   162
        fprintf( f, "\n" );
nkeynes@187
   163
    }
nkeynes@187
   164
}
nkeynes@437
   165
nkeynes@477
   166
gboolean write_png_to_stream( FILE *f, frame_buffer_t buffer )
nkeynes@477
   167
{
nkeynes@477
   168
    int coltype, i;
nkeynes@477
   169
    png_bytep p;
nkeynes@477
   170
    png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
nkeynes@477
   171
    if (!png_ptr) {
nkeynes@736
   172
        return FALSE;
nkeynes@477
   173
    }
nkeynes@477
   174
nkeynes@477
   175
    png_infop info_ptr = png_create_info_struct(png_ptr);
nkeynes@477
   176
    if (!info_ptr) {
nkeynes@736
   177
        png_destroy_write_struct(&png_ptr, NULL);
nkeynes@736
   178
        return FALSE;
nkeynes@477
   179
    }
nkeynes@736
   180
nkeynes@477
   181
    if( setjmp(png_jmpbuf(png_ptr)) ) {
nkeynes@736
   182
        png_destroy_write_struct(&png_ptr, &info_ptr);
nkeynes@736
   183
        return FALSE;
nkeynes@477
   184
    }
nkeynes@477
   185
    png_init_io( png_ptr, f );
nkeynes@477
   186
    switch( buffer->colour_format ) {
nkeynes@477
   187
    case COLFMT_BGR888:
nkeynes@736
   188
        coltype = PNG_COLOR_TYPE_RGB;
nkeynes@736
   189
        break;
nkeynes@477
   190
    case COLFMT_BGRA8888:
nkeynes@736
   191
        coltype = PNG_COLOR_TYPE_RGB_ALPHA;
nkeynes@736
   192
        break;
nkeynes@477
   193
    case COLFMT_BGR0888:
nkeynes@736
   194
        coltype = PNG_COLOR_TYPE_RGB;
nkeynes@736
   195
        break;
nkeynes@477
   196
    default:
nkeynes@736
   197
        coltype = PNG_COLOR_TYPE_RGB;
nkeynes@477
   198
    }
nkeynes@477
   199
    png_set_IHDR(png_ptr, info_ptr, buffer->width, buffer->height,
nkeynes@736
   200
                 8, coltype, PNG_INTERLACE_NONE, 
nkeynes@736
   201
                 PNG_COMPRESSION_TYPE_DEFAULT, 
nkeynes@736
   202
                 PNG_FILTER_TYPE_DEFAULT );
nkeynes@477
   203
    png_write_info(png_ptr, info_ptr);
nkeynes@477
   204
    if( buffer->colour_format == COLFMT_BGR0888 ) {
nkeynes@736
   205
        png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
nkeynes@477
   206
    }
nkeynes@477
   207
    png_set_bgr(png_ptr);
nkeynes@477
   208
    if( buffer->inverted ) {
nkeynes@736
   209
        p = (png_bytep)(buffer->data + (buffer->height*buffer->rowstride) - buffer->rowstride);
nkeynes@736
   210
        for(i=0; i<buffer->height; i++ ) {
nkeynes@736
   211
            png_write_row(png_ptr, p);
nkeynes@736
   212
            p-=buffer->rowstride;
nkeynes@736
   213
        }
nkeynes@477
   214
    } else {
nkeynes@736
   215
        p = (png_bytep)buffer->data;
nkeynes@736
   216
        for(i=0; i<buffer->height; i++ ) {
nkeynes@736
   217
            png_write_row(png_ptr, p);
nkeynes@736
   218
            p+=buffer->rowstride;
nkeynes@736
   219
        }
nkeynes@477
   220
    }
nkeynes@477
   221
    png_write_end(png_ptr, info_ptr);
nkeynes@477
   222
    png_destroy_write_struct(&png_ptr, &info_ptr);
nkeynes@477
   223
    return TRUE;
nkeynes@477
   224
}
nkeynes@477
   225
nkeynes@477
   226
frame_buffer_t read_png_from_stream( FILE *f )
nkeynes@477
   227
{
nkeynes@477
   228
    png_bytep p;
nkeynes@477
   229
    int i;
nkeynes@477
   230
    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 
nkeynes@736
   231
            NULL, NULL, NULL);
nkeynes@477
   232
    if (!png_ptr) {
nkeynes@736
   233
        return NULL;
nkeynes@477
   234
    }
nkeynes@477
   235
nkeynes@477
   236
    png_infop info_ptr = png_create_info_struct(png_ptr);
nkeynes@477
   237
    if (!info_ptr) {
nkeynes@736
   238
        png_destroy_read_struct(&png_ptr, NULL, NULL);
nkeynes@736
   239
        return NULL;
nkeynes@477
   240
    }
nkeynes@736
   241
nkeynes@477
   242
    png_infop end_info = png_create_info_struct(png_ptr);
nkeynes@477
   243
    if (!end_info) {
nkeynes@736
   244
        png_destroy_read_struct(&png_ptr, &info_ptr, NULL );
nkeynes@736
   245
        return NULL;
nkeynes@477
   246
    }
nkeynes@477
   247
nkeynes@477
   248
    if( setjmp(png_jmpbuf(png_ptr)) ) {
nkeynes@736
   249
        png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
nkeynes@736
   250
        return NULL;
nkeynes@477
   251
    }
nkeynes@477
   252
nkeynes@477
   253
    png_init_io(png_ptr, f);
nkeynes@477
   254
    png_read_info(png_ptr, info_ptr);
nkeynes@736
   255
nkeynes@477
   256
    png_uint_32 width, height;
nkeynes@477
   257
    int bit_depth, color_type, interlace_type,
nkeynes@736
   258
    compression_type, filter_method;
nkeynes@477
   259
    png_get_IHDR(png_ptr, info_ptr, &width, &height,
nkeynes@736
   260
                 &bit_depth, &color_type, &interlace_type,
nkeynes@736
   261
                 &compression_type, &filter_method);
nkeynes@477
   262
    assert( interlace_type == PNG_INTERLACE_NONE );
nkeynes@477
   263
    int rowbytes = png_get_rowbytes(png_ptr, info_ptr);
nkeynes@477
   264
    int channels = png_get_channels(png_ptr, info_ptr);
nkeynes@477
   265
    frame_buffer_t buffer = g_malloc( sizeof(struct frame_buffer) + rowbytes*height );
nkeynes@502
   266
    buffer->data = (unsigned char *)(buffer+1);
nkeynes@477
   267
    buffer->width = width;
nkeynes@477
   268
    buffer->height = height;
nkeynes@477
   269
    buffer->rowstride = rowbytes;
nkeynes@477
   270
    buffer->address = -1;
nkeynes@477
   271
    buffer->size = rowbytes*height;
nkeynes@477
   272
    buffer->inverted = FALSE;
nkeynes@477
   273
    if( channels == 4 ) {
nkeynes@736
   274
        buffer->colour_format = COLFMT_BGRA8888;
nkeynes@477
   275
    } else if( channels == 3 ) {
nkeynes@736
   276
        buffer->colour_format = COLFMT_RGB888;
nkeynes@477
   277
    }
nkeynes@736
   278
nkeynes@481
   279
    p = (png_bytep)buffer->data;
nkeynes@477
   280
    for( i=0; i<height; i++ ) {
nkeynes@736
   281
        png_read_row(png_ptr, p, NULL );
nkeynes@736
   282
        p += rowbytes;
nkeynes@477
   283
    }
nkeynes@477
   284
nkeynes@477
   285
    png_read_end(png_ptr, end_info);
nkeynes@477
   286
    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
nkeynes@477
   287
    return buffer;
nkeynes@477
   288
}
nkeynes@437
   289
nkeynes@586
   290
int get_log_level_from_string( const gchar *str )
nkeynes@586
   291
{
nkeynes@586
   292
    switch( tolower(str[0]) ) {
nkeynes@586
   293
    case 'd': return EMIT_DEBUG;
nkeynes@586
   294
    case 'e': return EMIT_ERR;
nkeynes@586
   295
    case 'f': return EMIT_FATAL;
nkeynes@586
   296
    case 'i': return EMIT_INFO;
nkeynes@586
   297
    case 't': return EMIT_TRACE;
nkeynes@586
   298
    case 'w': return EMIT_WARN;
nkeynes@586
   299
    default: return -1;
nkeynes@586
   300
    }
nkeynes@586
   301
}
nkeynes@586
   302
nkeynes@586
   303
gboolean set_global_log_level( const gchar *str ) 
nkeynes@586
   304
{
nkeynes@586
   305
    int l = get_log_level_from_string(str);
nkeynes@586
   306
    if( l == -1 ) {
nkeynes@736
   307
        return FALSE;
nkeynes@586
   308
    } else {
nkeynes@736
   309
        global_msg_level = l;
nkeynes@736
   310
        return TRUE;
nkeynes@586
   311
    }
nkeynes@586
   312
}
nkeynes@586
   313
nkeynes@437
   314
void log_message( void *ptr, int level, const gchar *source, const char *msg, ... )
nkeynes@437
   315
{
nkeynes@481
   316
    char buf[20];
nkeynes@437
   317
    time_t tm = time(NULL);
nkeynes@437
   318
    va_list ap;
nkeynes@437
   319
nkeynes@437
   320
    if( level > global_msg_level ) {
nkeynes@736
   321
        return; // ignored
nkeynes@437
   322
    }
nkeynes@447
   323
nkeynes@437
   324
    va_start(ap, msg);
nkeynes@535
   325
    gchar *text = g_strdup_vprintf( msg, ap );
nkeynes@535
   326
    va_end(ap);
nkeynes@736
   327
nkeynes@447
   328
    if( level <= EMIT_ERR ) {
nkeynes@736
   329
        if( gui_error_dialog( text ) ) {
nkeynes@736
   330
            g_free(text);
nkeynes@768
   331
            // If we're running, halt on error to avoid potentially flooding
nkeynes@768
   332
            // the user with error messages.
nkeynes@768
   333
            if( dreamcast_is_running() ) {
nkeynes@768
   334
                dreamcast_stop();
nkeynes@768
   335
            }
nkeynes@736
   336
            return;
nkeynes@736
   337
        }
nkeynes@447
   338
    }
nkeynes@447
   339
nkeynes@447
   340
nkeynes@437
   341
    strftime( buf, sizeof(buf), "%H:%M:%S", localtime(&tm) );
nkeynes@535
   342
    fprintf( stderr, "%s %08X %-5s %s\n", buf, sh4r.pc, msg_levels[level], text );
nkeynes@535
   343
    g_free(text);
nkeynes@437
   344
}
.