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