Search
lxdream.org :: lxdream/src/util.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/util.c
changeset 502:c4ecae2b1b5e
prev495:4db4702b0778
next535:7338e020b09b
author nkeynes
date Thu Nov 08 11:54:16 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Add sh4ptr_t type, start converting bare pointer refs to it
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@502
     2
 * $Id: util.c,v 1.14 2007-11-08 11:54:16 nkeynes Exp $
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@481
    33
#include "gui.h"
nkeynes@437
    34
#include "sh4/sh4core.h"
nkeynes@437
    35
nkeynes@437
    36
char *msg_levels[] = { "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE" };
nkeynes@502
    37
int global_msg_level = EMIT_WARN;
nkeynes@495
    38
nkeynes@495
    39
static void report_crash( int signo, siginfo_t *info, void *ptr )
nkeynes@495
    40
{
nkeynes@495
    41
    char buf[128];
nkeynes@495
    42
nkeynes@495
    43
    fprintf( stderr, "--- Aborting with signal %d ---\n", signo );
nkeynes@495
    44
    // Get gdb to print a nice backtrace for us
nkeynes@495
    45
    snprintf( buf, 128, "gdb -batch -f --quiet --pid=%d -ex bt", getpid() );
nkeynes@495
    46
    system(buf);
nkeynes@495
    47
nkeynes@495
    48
    abort();
nkeynes@495
    49
}
nkeynes@495
    50
nkeynes@495
    51
void install_crash_handler(void)
nkeynes@495
    52
{
nkeynes@495
    53
    struct sigaction sa;
nkeynes@495
    54
nkeynes@495
    55
    sa.sa_sigaction = report_crash;
nkeynes@495
    56
    sigemptyset(&sa.sa_mask);
nkeynes@495
    57
    sa.sa_flags = SA_RESETHAND|SA_SIGINFO;
nkeynes@495
    58
    sigaction( SIGSEGV, &sa, NULL );
nkeynes@495
    59
}
nkeynes@495
    60
nkeynes@17
    61
nkeynes@422
    62
void fwrite_string( const char *s, FILE *f )
nkeynes@17
    63
{
nkeynes@17
    64
    uint32_t len = 0;
nkeynes@17
    65
    if( s == NULL ) {
nkeynes@17
    66
	fwrite( &len, sizeof(len), 1, f );
nkeynes@17
    67
    } else {
nkeynes@17
    68
	len = strlen(s)+1;
nkeynes@17
    69
	fwrite( &len, sizeof(len), 1, f );
nkeynes@17
    70
	fwrite( s, len, 1, f );
nkeynes@17
    71
    }
nkeynes@17
    72
}
nkeynes@17
    73
nkeynes@17
    74
int fread_string( char *s, int maxlen, FILE *f ) 
nkeynes@17
    75
{
nkeynes@17
    76
    uint32_t len;
nkeynes@17
    77
    fread( &len, sizeof(len), 1, f );
nkeynes@17
    78
    if( len != 0 ) {
nkeynes@17
    79
	fread( s, len > maxlen ? maxlen : len, 1, f );
nkeynes@17
    80
    }
nkeynes@17
    81
    return len;
nkeynes@17
    82
}
nkeynes@117
    83
nkeynes@477
    84
void fwrite_gzip( void *p, size_t sz, size_t count, FILE *f )
nkeynes@477
    85
{
nkeynes@477
    86
    uLongf size = sz*count;
nkeynes@477
    87
    uLongf csize = ((int)(size*1.001))+13;
nkeynes@477
    88
    unsigned char *tmp = g_malloc0( csize );
nkeynes@477
    89
    int status = compress( tmp, &csize, p, size );
nkeynes@477
    90
    assert( status == Z_OK );
nkeynes@477
    91
    fwrite( &csize, sizeof(csize), 1, f );
nkeynes@477
    92
    fwrite( tmp, csize, 1, f );
nkeynes@477
    93
    g_free(tmp);
nkeynes@477
    94
}
nkeynes@477
    95
nkeynes@477
    96
int fread_gzip( void *p, size_t sz, size_t count, FILE *f )
nkeynes@477
    97
{
nkeynes@477
    98
    uLongf size = sz*count;
nkeynes@477
    99
    uLongf csize;
nkeynes@477
   100
    unsigned char *tmp;
nkeynes@477
   101
nkeynes@477
   102
    fread( &csize, sizeof(csize), 1, f );
nkeynes@477
   103
    assert( csize <= (size*2) );
nkeynes@477
   104
    tmp = g_malloc0( csize );
nkeynes@477
   105
    fread( tmp, csize, 1, f );
nkeynes@477
   106
    int status = uncompress( p, &size, tmp, csize );
nkeynes@477
   107
    g_free(tmp);
nkeynes@477
   108
    if( status == Z_OK ) {
nkeynes@477
   109
	return count;
nkeynes@477
   110
    } else {
nkeynes@477
   111
	fprintf( stderr, "Error reading compressed data\n" );
nkeynes@477
   112
	return 0;
nkeynes@477
   113
    }
nkeynes@477
   114
}
nkeynes@477
   115
nkeynes@117
   116
void fwrite_dump( unsigned char *data, unsigned int length, FILE *f ) 
nkeynes@117
   117
{
nkeynes@117
   118
    unsigned int i, j;
nkeynes@117
   119
    for( i =0; i<length; i+=16 ) {
nkeynes@117
   120
	fprintf( f, "%08X:", i);
nkeynes@117
   121
	for( j=i; j<i+16; j++ ) {
nkeynes@117
   122
	    if( (j % 4) == 0 )
nkeynes@117
   123
		fprintf( f, " " );
nkeynes@117
   124
	    if( j < length )
nkeynes@117
   125
		fprintf( f, " %02X", (unsigned int)(data[j]) );
nkeynes@117
   126
	    else
nkeynes@117
   127
		fprintf( f, "   " );
nkeynes@117
   128
	}
nkeynes@117
   129
	fprintf( f, "  " );
nkeynes@117
   130
	for( j=i; j<i+16 && j<length; j++ ) {
nkeynes@117
   131
	    fprintf( f, "%c", isprint(data[j]) ? data[j] : '.' );
nkeynes@117
   132
	}
nkeynes@117
   133
	fprintf( f, "\n" );
nkeynes@117
   134
    }
nkeynes@117
   135
}
nkeynes@187
   136
nkeynes@187
   137
void fwrite_dump32( unsigned int *data, unsigned int length, FILE *f ) 
nkeynes@187
   138
{
nkeynes@220
   139
    fwrite_dump32v( data, length, 8, f );
nkeynes@220
   140
}
nkeynes@220
   141
nkeynes@220
   142
void fwrite_dump32v( unsigned int *data, unsigned int length, int wordsPerLine, FILE *f ) 
nkeynes@220
   143
{
nkeynes@187
   144
    unsigned int i, j;
nkeynes@220
   145
    for( i =0; i<length>>2; i+=wordsPerLine ) {
nkeynes@187
   146
	fprintf( f, "%08X:", i);
nkeynes@220
   147
	for( j=i; j<i+wordsPerLine; j++ ) {
nkeynes@187
   148
	    if( j < length )
nkeynes@187
   149
		fprintf( f, " %08X", (unsigned int)(data[j]) );
nkeynes@187
   150
	    else
nkeynes@187
   151
		fprintf( f, "         " );
nkeynes@187
   152
	}
nkeynes@187
   153
	fprintf( f, "\n" );
nkeynes@187
   154
    }
nkeynes@187
   155
}
nkeynes@437
   156
nkeynes@477
   157
gboolean write_png_to_stream( FILE *f, frame_buffer_t buffer )
nkeynes@477
   158
{
nkeynes@477
   159
    int coltype, i;
nkeynes@477
   160
    png_bytep p;
nkeynes@477
   161
    png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
nkeynes@477
   162
    if (!png_ptr) {
nkeynes@477
   163
	return FALSE;
nkeynes@477
   164
    }
nkeynes@477
   165
nkeynes@477
   166
    png_infop info_ptr = png_create_info_struct(png_ptr);
nkeynes@477
   167
    if (!info_ptr) {
nkeynes@477
   168
	png_destroy_write_struct(&png_ptr, NULL);
nkeynes@477
   169
	return FALSE;
nkeynes@477
   170
    }
nkeynes@477
   171
    
nkeynes@477
   172
    if( setjmp(png_jmpbuf(png_ptr)) ) {
nkeynes@477
   173
	png_destroy_write_struct(&png_ptr, &info_ptr);
nkeynes@477
   174
	return FALSE;
nkeynes@477
   175
    }
nkeynes@477
   176
    png_init_io( png_ptr, f );
nkeynes@477
   177
    switch( buffer->colour_format ) {
nkeynes@477
   178
    case COLFMT_BGR888:
nkeynes@477
   179
	coltype = PNG_COLOR_TYPE_RGB;
nkeynes@477
   180
	break;
nkeynes@477
   181
    case COLFMT_BGRA8888:
nkeynes@477
   182
	coltype = PNG_COLOR_TYPE_RGB_ALPHA;
nkeynes@477
   183
	break;
nkeynes@477
   184
    case COLFMT_BGR0888:
nkeynes@477
   185
	coltype = PNG_COLOR_TYPE_RGB;
nkeynes@477
   186
	break;
nkeynes@477
   187
    default:
nkeynes@477
   188
	coltype = PNG_COLOR_TYPE_RGB;
nkeynes@477
   189
    }
nkeynes@477
   190
    png_set_IHDR(png_ptr, info_ptr, buffer->width, buffer->height,
nkeynes@477
   191
		 8, coltype, PNG_INTERLACE_NONE, 
nkeynes@477
   192
		 PNG_COMPRESSION_TYPE_DEFAULT, 
nkeynes@477
   193
		 PNG_FILTER_TYPE_DEFAULT );
nkeynes@477
   194
    png_write_info(png_ptr, info_ptr);
nkeynes@477
   195
    if( buffer->colour_format == COLFMT_BGR0888 ) {
nkeynes@477
   196
	png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
nkeynes@477
   197
    }
nkeynes@477
   198
    png_set_bgr(png_ptr);
nkeynes@477
   199
    if( buffer->inverted ) {
nkeynes@481
   200
	p = (png_bytep)(buffer->data + (buffer->height*buffer->rowstride) - buffer->rowstride);
nkeynes@477
   201
	for(i=0; i<buffer->height; i++ ) {
nkeynes@477
   202
	    png_write_row(png_ptr, p);
nkeynes@477
   203
	    p-=buffer->rowstride;
nkeynes@477
   204
	}
nkeynes@477
   205
    } else {
nkeynes@481
   206
	p = (png_bytep)buffer->data;
nkeynes@477
   207
	for(i=0; i<buffer->height; i++ ) {
nkeynes@477
   208
	    png_write_row(png_ptr, p);
nkeynes@477
   209
	    p+=buffer->rowstride;
nkeynes@477
   210
	}
nkeynes@477
   211
    }
nkeynes@477
   212
    png_write_end(png_ptr, info_ptr);
nkeynes@477
   213
    png_destroy_write_struct(&png_ptr, &info_ptr);
nkeynes@477
   214
    return TRUE;
nkeynes@477
   215
}
nkeynes@477
   216
nkeynes@477
   217
frame_buffer_t read_png_from_stream( FILE *f )
nkeynes@477
   218
{
nkeynes@477
   219
    png_bytep p;
nkeynes@477
   220
    int i;
nkeynes@477
   221
    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 
nkeynes@477
   222
						 NULL, NULL, NULL);
nkeynes@477
   223
    if (!png_ptr) {
nkeynes@477
   224
	return NULL;
nkeynes@477
   225
    }
nkeynes@477
   226
nkeynes@477
   227
    png_infop info_ptr = png_create_info_struct(png_ptr);
nkeynes@477
   228
    if (!info_ptr) {
nkeynes@477
   229
	png_destroy_read_struct(&png_ptr, NULL, NULL);
nkeynes@477
   230
	return NULL;
nkeynes@477
   231
    }
nkeynes@477
   232
    
nkeynes@477
   233
    png_infop end_info = png_create_info_struct(png_ptr);
nkeynes@477
   234
    if (!end_info) {
nkeynes@477
   235
	png_destroy_read_struct(&png_ptr, &info_ptr, NULL );
nkeynes@477
   236
	return NULL;
nkeynes@477
   237
    }
nkeynes@477
   238
nkeynes@477
   239
    if( setjmp(png_jmpbuf(png_ptr)) ) {
nkeynes@477
   240
	png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
nkeynes@477
   241
	return NULL;
nkeynes@477
   242
    }
nkeynes@477
   243
nkeynes@477
   244
    png_init_io(png_ptr, f);
nkeynes@477
   245
    png_read_info(png_ptr, info_ptr);
nkeynes@477
   246
    
nkeynes@477
   247
    png_uint_32 width, height;
nkeynes@477
   248
    int bit_depth, color_type, interlace_type,
nkeynes@477
   249
	compression_type, filter_method;
nkeynes@477
   250
    png_get_IHDR(png_ptr, info_ptr, &width, &height,
nkeynes@477
   251
		 &bit_depth, &color_type, &interlace_type,
nkeynes@477
   252
		 &compression_type, &filter_method);
nkeynes@477
   253
    assert( interlace_type == PNG_INTERLACE_NONE );
nkeynes@477
   254
    int rowbytes = png_get_rowbytes(png_ptr, info_ptr);
nkeynes@477
   255
    int channels = png_get_channels(png_ptr, info_ptr);
nkeynes@477
   256
    frame_buffer_t buffer = g_malloc( sizeof(struct frame_buffer) + rowbytes*height );
nkeynes@502
   257
    buffer->data = (unsigned char *)(buffer+1);
nkeynes@477
   258
    buffer->width = width;
nkeynes@477
   259
    buffer->height = height;
nkeynes@477
   260
    buffer->rowstride = rowbytes;
nkeynes@477
   261
    buffer->address = -1;
nkeynes@477
   262
    buffer->size = rowbytes*height;
nkeynes@477
   263
    buffer->inverted = FALSE;
nkeynes@477
   264
    if( channels == 4 ) {
nkeynes@477
   265
	buffer->colour_format = COLFMT_BGRA8888;
nkeynes@477
   266
    } else if( channels == 3 ) {
nkeynes@477
   267
	buffer->colour_format = COLFMT_RGB888;
nkeynes@477
   268
    }
nkeynes@477
   269
    
nkeynes@481
   270
    p = (png_bytep)buffer->data;
nkeynes@477
   271
    for( i=0; i<height; i++ ) {
nkeynes@477
   272
	png_read_row(png_ptr, p, NULL );
nkeynes@477
   273
	p += rowbytes;
nkeynes@477
   274
    }
nkeynes@477
   275
nkeynes@477
   276
    png_read_end(png_ptr, end_info);
nkeynes@477
   277
    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
nkeynes@477
   278
    return buffer;
nkeynes@477
   279
}
nkeynes@437
   280
nkeynes@437
   281
void log_message( void *ptr, int level, const gchar *source, const char *msg, ... )
nkeynes@437
   282
{
nkeynes@481
   283
    char buf[20];
nkeynes@437
   284
    time_t tm = time(NULL);
nkeynes@437
   285
    va_list ap;
nkeynes@437
   286
nkeynes@437
   287
    if( level > global_msg_level ) {
nkeynes@437
   288
	return; // ignored
nkeynes@437
   289
    }
nkeynes@447
   290
nkeynes@437
   291
    va_start(ap, msg);
nkeynes@437
   292
nkeynes@447
   293
    if( level <= EMIT_ERR ) {
nkeynes@447
   294
	gchar *text = g_strdup_vprintf( msg, ap );
nkeynes@447
   295
	if( gui_error_dialog( text ) ) {
nkeynes@447
   296
	    g_free(text);
nkeynes@447
   297
	    va_end(ap);
nkeynes@447
   298
	    return;
nkeynes@447
   299
	}
nkeynes@447
   300
	g_free(text);
nkeynes@447
   301
    }
nkeynes@447
   302
nkeynes@447
   303
nkeynes@437
   304
    strftime( buf, sizeof(buf), "%H:%M:%S", localtime(&tm) );
nkeynes@437
   305
    fprintf( stderr, "%s %08X %-5s ", buf, sh4r.pc, msg_levels[level] );
nkeynes@437
   306
    vfprintf( stderr, msg, ap );
nkeynes@447
   307
    va_end(ap);
nkeynes@437
   308
    fprintf( stderr, "\n" );
nkeynes@437
   309
}
.