Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 723:a13e5529c52a
prev708:0b8b5eaa3b52
next736:a02d1475ccfd
author nkeynes
date Sun Jul 06 03:16:21 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Add apple-specific binding for gettext
file annotate diff log raw
nkeynes@30
     1
/**
nkeynes@586
     2
 * $Id$
nkeynes@30
     3
 *
nkeynes@30
     4
 * Main program, initializes dreamcast and gui, then passes control off to
nkeynes@537
     5
 * the main loop. 
nkeynes@30
     6
 *
nkeynes@30
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@30
     8
 *
nkeynes@30
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@30
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@30
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@30
    12
 * (at your option) any later version.
nkeynes@30
    13
 *
nkeynes@30
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@30
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@30
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@30
    17
 * GNU General Public License for more details.
nkeynes@1
    18
 */
nkeynes@1
    19
nkeynes@678
    20
#include <stdlib.h>
nkeynes@68
    21
#include <unistd.h>
nkeynes@94
    22
#include <getopt.h>
nkeynes@669
    23
#include <glib/gi18n.h>
nkeynes@669
    24
#include "lxdream.h"
nkeynes@105
    25
#include "syscall.h"
nkeynes@422
    26
#include "mem.h"
nkeynes@27
    27
#include "dreamcast.h"
nkeynes@422
    28
#include "display.h"
nkeynes@422
    29
#include "loader.h"
nkeynes@450
    30
#include "gui.h"
nkeynes@106
    31
#include "aica/audio.h"
nkeynes@422
    32
#include "gdrom/gdrom.h"
nkeynes@144
    33
#include "maple/maple.h"
nkeynes@586
    34
#include "sh4/sh4.h"
nkeynes@11
    35
nkeynes@723
    36
#ifdef APPLE_BUILD
nkeynes@723
    37
#include <AppKit/AppKit.h>
nkeynes@723
    38
#endif
nkeynes@723
    39
nkeynes@700
    40
char *option_list = "a:A:c:dhHl:m:npt:T:uvV:x?";
nkeynes@700
    41
struct option longopts[] = {
nkeynes@700
    42
        { "aica", required_argument, NULL, 'a' },
nkeynes@700
    43
        { "audio", required_argument, NULL, 'A' },
nkeynes@700
    44
        { "config", required_argument, NULL, 'c' },
nkeynes@700
    45
        { "debugger", no_argument, NULL, 'D' },
nkeynes@700
    46
        { "help", no_argument, NULL, 'h' },
nkeynes@700
    47
        { "headless", no_argument, NULL, 'H' },
nkeynes@700
    48
        { "log", required_argument, NULL,'l' }, 
nkeynes@700
    49
        { "multiplier", required_argument, NULL, 'm' },
nkeynes@700
    50
        { "run-time", required_argument, NULL, 't' },
nkeynes@700
    51
        { "trace", required_argument, NULL, 'T' },
nkeynes@700
    52
        { "unsafe", no_argument, NULL, 'u' },
nkeynes@700
    53
        { "video", no_argument, NULL, 'V' },
nkeynes@700
    54
        { "version", no_argument, NULL, 'v' }, 
nkeynes@700
    55
        { NULL, 0, 0, 0 } };
nkeynes@68
    56
char *aica_program = NULL;
nkeynes@531
    57
char *display_driver_name = NULL;
nkeynes@531
    58
char *audio_driver_name = NULL;
nkeynes@586
    59
char *trace_regions = NULL;
nkeynes@68
    60
gboolean start_immediately = FALSE;
nkeynes@586
    61
gboolean no_start = FALSE;
nkeynes@77
    62
gboolean headless = FALSE;
nkeynes@402
    63
gboolean use_xlat = TRUE;
nkeynes@392
    64
gboolean show_debugger = FALSE;
nkeynes@414
    65
extern uint32_t sh4_cpu_multiplier;
nkeynes@68
    66
nkeynes@700
    67
void print_version()
nkeynes@700
    68
{
nkeynes@700
    69
    printf( "lxdream " APP_VERSION "\n" );
nkeynes@700
    70
}
nkeynes@700
    71
nkeynes@700
    72
void print_usage()
nkeynes@700
    73
{
nkeynes@700
    74
    print_version();
nkeynes@700
    75
    printf( "Usage: lxdream [options] [disc-file] [program-file]\n\n" );
nkeynes@700
    76
    
nkeynes@700
    77
    printf( "Options:\n" );
nkeynes@700
    78
    printf( "   -a, --aica=PROGFILE    %s\n", _("Run the AICA SPU only, with the supplied program") );
nkeynes@700
    79
    printf( "   -A, --audio=DRIVER     %s\n", _("Use the specified audio driver (? to list)") );
nkeynes@700
    80
    printf( "   -c, --config=CONFFILE  %s\n", _("Load configuration from CONFFILE") );
nkeynes@700
    81
    printf( "   -d, --debugger         %s\n", _("Start in debugger mode") );
nkeynes@700
    82
    printf( "   -h, --help             %s\n", _("Display this usage information") );
nkeynes@700
    83
    printf( "   -H, --headless         %s\n", _("Run in headless (no video) mode") );
nkeynes@700
    84
    printf( "   -l, --log=LEVEL        %s\n", _("Set the output log level") );
nkeynes@700
    85
    printf( "   -m, --multiplier=SCALE %s\n", _("Set the SH4 multiplier (1.0 = fullspeed)") );
nkeynes@700
    86
    printf( "   -n                     %s\n", _("Don't start running immediately") );
nkeynes@700
    87
    printf( "   -p                     %s\n", _("Start running immediately on startup") );
nkeynes@700
    88
    printf( "   -t, --run-time=SECONDS %s\n", _("Run for the specified number of seconds") );
nkeynes@700
    89
    printf( "   -T, --trace=REGIONS    %s\n", _("Output trace information for the named regions") );
nkeynes@700
    90
    printf( "   -u, --unsafe           %s\n", _("Allow unsafe dcload syscalls") );
nkeynes@700
    91
    printf( "   -v, --version          %s\n", _("Print the lxdream version string") );
nkeynes@700
    92
    printf( "   -V, --video=DRIVER     %s\n", _("Use the specified video driver (? to list)") );
nkeynes@700
    93
    printf( "   -x                     %s\n", _("Disable the SH4 translator") );
nkeynes@700
    94
}
nkeynes@700
    95
nkeynes@723
    96
void bind_gettext_domain()
nkeynes@723
    97
{
nkeynes@723
    98
#ifdef ENABLE_NLS
nkeynes@723
    99
#ifdef APPLE_BUILD
nkeynes@723
   100
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
nkeynes@723
   101
    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
nkeynes@723
   102
    bindtextdomain( PACKAGE, [resourcePath UTF8String] );
nkeynes@723
   103
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
nkeynes@723
   104
    bind_textdomain_codeset( PACKAGE, "UTF-8" );
nkeynes@723
   105
#endif
nkeynes@723
   106
    [pool release];    
nkeynes@723
   107
#else    
nkeynes@723
   108
    bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
nkeynes@723
   109
#endif
nkeynes@723
   110
    textdomain(PACKAGE);
nkeynes@723
   111
    
nkeynes@723
   112
#endif
nkeynes@723
   113
}
nkeynes@723
   114
nkeynes@30
   115
int main (int argc, char *argv[])
nkeynes@1
   116
{
nkeynes@669
   117
    int opt;
nkeynes@372
   118
    double t;
nkeynes@531
   119
    gboolean display_ok;
nkeynes@689
   120
    uint32_t time_secs, time_nanos;
nkeynes@495
   121
nkeynes@495
   122
    install_crash_handler();
nkeynes@723
   123
    bind_gettext_domain();
nkeynes@531
   124
    display_ok = gui_parse_cmdline(&argc, &argv);
nkeynes@464
   125
nkeynes@94
   126
    while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
nkeynes@68
   127
	switch( opt ) {
nkeynes@68
   128
	case 'a': /* AICA only mode - argument is an AICA program */
nkeynes@68
   129
	    aica_program = optarg;
nkeynes@68
   130
	    break;
nkeynes@700
   131
    case 'A': /* Audio driver */
nkeynes@700
   132
        audio_driver_name = optarg;
nkeynes@700
   133
        if( strcmp(audio_driver_name, "?") == 0 ) {
nkeynes@700
   134
            print_version();
nkeynes@700
   135
            print_audio_drivers(stdout);
nkeynes@700
   136
            exit(0);
nkeynes@700
   137
        }
nkeynes@700
   138
        break;
nkeynes@144
   139
	case 'c': /* Config file */
nkeynes@450
   140
	    lxdream_set_config_filename(optarg);
nkeynes@144
   141
	    break;
nkeynes@700
   142
	case 'd': /* Launch w/ debugger */
nkeynes@392
   143
	    show_debugger = TRUE;
nkeynes@392
   144
	    break;
nkeynes@700
   145
    case 'h': /* help */
nkeynes@700
   146
    case '?':
nkeynes@700
   147
        print_usage();
nkeynes@700
   148
        exit(0);
nkeynes@700
   149
        break;
nkeynes@700
   150
    case 'H': /* Headless - shorthand for -V null */
nkeynes@700
   151
        display_driver_name = "null";
nkeynes@700
   152
        break;
nkeynes@700
   153
    case 'l': /* Log verbosity */
nkeynes@700
   154
        if( !set_global_log_level(optarg) ) {
nkeynes@700
   155
            ERROR( "Unrecognized log level '%s'", optarg );
nkeynes@700
   156
        }
nkeynes@700
   157
        break;
nkeynes@414
   158
	case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
nkeynes@414
   159
	    t = strtod(optarg, NULL);
nkeynes@414
   160
	    sh4_cpu_multiplier = (int)(1000.0/t);
nkeynes@414
   161
	    break;
nkeynes@586
   162
	case 'n': /* Don't start immediately */
nkeynes@586
   163
	    no_start = TRUE;
nkeynes@586
   164
	    start_immediately = FALSE;
nkeynes@586
   165
	    break;
nkeynes@68
   166
	case 'p': /* Start immediately */
nkeynes@68
   167
	    start_immediately = TRUE;
nkeynes@586
   168
	    no_start = FALSE;
nkeynes@182
   169
	    break;
nkeynes@689
   170
	case 't': /* Time limit + auto quit */
nkeynes@372
   171
	    t = strtod(optarg, NULL);
nkeynes@372
   172
	    time_secs = (uint32_t)t;
nkeynes@372
   173
	    time_nanos = (int)((t - time_secs) * 1000000000);
nkeynes@689
   174
	    dreamcast_set_run_time( time_secs, time_nanos );
nkeynes@689
   175
	    dreamcast_set_exit_on_stop( TRUE );
nkeynes@379
   176
	    break;
nkeynes@586
   177
	case 'T': /* trace regions */
nkeynes@586
   178
	    trace_regions = optarg;
nkeynes@698
   179
	    set_global_log_level("trace");
nkeynes@586
   180
	    break;
nkeynes@700
   181
    case 'u': /* Allow unsafe dcload syscalls */
nkeynes@700
   182
        dcload_set_allow_unsafe(TRUE);
nkeynes@700
   183
        break;
nkeynes@700
   184
	case 'v': 
nkeynes@700
   185
	    print_version();
nkeynes@700
   186
	    exit(0);
nkeynes@586
   187
	    break;
nkeynes@700
   188
    case 'V': /* Video driver */
nkeynes@700
   189
        display_driver_name = optarg;
nkeynes@700
   190
        if( strcmp(display_driver_name,"?") == 0 ) {
nkeynes@700
   191
            print_version();
nkeynes@700
   192
            print_display_drivers(stdout);
nkeynes@700
   193
            exit(0);
nkeynes@700
   194
        }
nkeynes@700
   195
        break;
nkeynes@402
   196
	case 'x': /* Disable translator */
nkeynes@402
   197
	    use_xlat = FALSE;
nkeynes@379
   198
	    break;
nkeynes@68
   199
	}
nkeynes@68
   200
    }
nkeynes@30
   201
nkeynes@450
   202
    lxdream_load_config( );
nkeynes@691
   203
    gdrom_list_init();
nkeynes@691
   204
    
nkeynes@68
   205
    if( aica_program == NULL ) {
nkeynes@700
   206
        dreamcast_init();
nkeynes@68
   207
    } else {
nkeynes@700
   208
        dreamcast_configure_aica_only();
nkeynes@700
   209
        mem_load_block( aica_program, 0x00800000, 2048*1024 );
nkeynes@68
   210
    }
nkeynes@586
   211
    mem_set_trace( trace_regions, TRUE );
nkeynes@1
   212
nkeynes@697
   213
    audio_init_driver( audio_driver_name, 44100, AUDIO_FMT_16ST );
nkeynes@697
   214
    
nkeynes@700
   215
    headless = display_driver_name != NULL && strcasecmp( display_driver_name, "null" ) == 0;
nkeynes@106
   216
    if( headless ) {
nkeynes@700
   217
        display_set_driver( &display_null_driver );
nkeynes@106
   218
    } else {
nkeynes@700
   219
        gui_init(show_debugger);
nkeynes@435
   220
nkeynes@700
   221
        display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
nkeynes@700
   222
        if( display_driver == NULL ) {
nkeynes@700
   223
            ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
nkeynes@700
   224
            exit(2);
nkeynes@700
   225
        } else if( display_set_driver( display_driver ) == FALSE ) {
nkeynes@700
   226
            ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
nkeynes@700
   227
                    display_driver->name );
nkeynes@700
   228
            exit(2);
nkeynes@700
   229
        }
nkeynes@106
   230
    }
nkeynes@106
   231
nkeynes@144
   232
    maple_reattach_all();
nkeynes@180
   233
    INFO( "%s! ready...", APP_NAME );
nkeynes@446
   234
nkeynes@446
   235
    for( ; optind < argc; optind++ ) {
nkeynes@700
   236
        gboolean ok = gdrom_mount_image(argv[optind]);
nkeynes@700
   237
        if( !ok ) {
nkeynes@700
   238
            ok = file_load_magic( argv[optind] );
nkeynes@700
   239
        }
nkeynes@700
   240
        if( !ok ) {
nkeynes@700
   241
            ERROR( "Unrecognized file '%s'", argv[optind] );
nkeynes@700
   242
        }
nkeynes@700
   243
        if( !no_start ) {
nkeynes@700
   244
            start_immediately = ok;
nkeynes@700
   245
        }
nkeynes@144
   246
    }
nkeynes@144
   247
nkeynes@464
   248
    if( gdrom_get_current_disc() == NULL ) {
nkeynes@700
   249
        const gchar *disc_file = lxdream_get_config_value( CONFIG_GDROM );
nkeynes@678
   250
        if( disc_file != NULL ) {
nkeynes@678
   251
            gdrom_mount_image( disc_file );
nkeynes@678
   252
        }
nkeynes@464
   253
    }
nkeynes@464
   254
nkeynes@379
   255
    sh4_set_use_xlat( use_xlat );
nkeynes@379
   256
nkeynes@689
   257
    if( headless ) {
nkeynes@689
   258
        dreamcast_run();
nkeynes@689
   259
    } else {
nkeynes@681
   260
        gui_main_loop( start_immediately && dreamcast_can_run() );
nkeynes@77
   261
    }
nkeynes@671
   262
    dreamcast_shutdown();
nkeynes@68
   263
    return 0;
nkeynes@1
   264
}
nkeynes@1
   265
.