Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 446:def93c34c84c
prev435:7a5d71e8560b
next450:207461e79f21
author nkeynes
date Tue Oct 16 12:28:42 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Change command line handling to not require -d
file annotate diff log raw
nkeynes@30
     1
/**
nkeynes@446
     2
 * $Id: main.c,v 1.29 2007-10-16 12:28:42 nkeynes Exp $
nkeynes@30
     3
 *
nkeynes@30
     4
 * Main program, initializes dreamcast and gui, then passes control off to
nkeynes@30
     5
 * the gtk main loop (currently). 
nkeynes@30
     6
 *
nkeynes@30
     7
 * FIXME: Remove explicit GTK/Gnome references from this file
nkeynes@30
     8
 *
nkeynes@30
     9
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@30
    10
 *
nkeynes@30
    11
 * This program is free software; you can redistribute it and/or modify
nkeynes@30
    12
 * it under the terms of the GNU General Public License as published by
nkeynes@30
    13
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@30
    14
 * (at your option) any later version.
nkeynes@30
    15
 *
nkeynes@30
    16
 * This program is distributed in the hope that it will be useful,
nkeynes@30
    17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@30
    18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@30
    19
 * GNU General Public License for more details.
nkeynes@1
    20
 */
nkeynes@1
    21
nkeynes@1
    22
#ifdef HAVE_CONFIG_H
nkeynes@1
    23
#  include <config.h>
nkeynes@1
    24
#endif
nkeynes@68
    25
#include <unistd.h>
nkeynes@94
    26
#include <getopt.h>
nkeynes@435
    27
#include "gui.h"
nkeynes@27
    28
#include "dream.h"
nkeynes@105
    29
#include "syscall.h"
nkeynes@422
    30
#include "mem.h"
nkeynes@27
    31
#include "dreamcast.h"
nkeynes@422
    32
#include "display.h"
nkeynes@422
    33
#include "loader.h"
nkeynes@106
    34
#include "aica/audio.h"
nkeynes@422
    35
#include "gdrom/gdrom.h"
nkeynes@144
    36
#include "maple/maple.h"
nkeynes@422
    37
#include "sh4/sh4core.h"
nkeynes@11
    38
nkeynes@77
    39
#define S3M_PLAYER "s3mplay.bin"
nkeynes@77
    40
nkeynes@414
    41
char *option_list = "a:m:s:A:V:puhbd:c:t:xD";
nkeynes@94
    42
struct option longopts[1] = { { NULL, 0, 0, 0 } };
nkeynes@68
    43
char *aica_program = NULL;
nkeynes@77
    44
char *s3m_file = NULL;
nkeynes@144
    45
char *disc_file = NULL;
nkeynes@144
    46
char *display_driver_name = "gtk";
nkeynes@106
    47
char *audio_driver_name = "esd";
nkeynes@144
    48
char *config_file = DEFAULT_CONFIG_FILENAME;
nkeynes@68
    49
gboolean start_immediately = FALSE;
nkeynes@77
    50
gboolean headless = FALSE;
nkeynes@87
    51
gboolean without_bios = FALSE;
nkeynes@402
    52
gboolean use_xlat = TRUE;
nkeynes@392
    53
gboolean show_debugger = FALSE;
nkeynes@372
    54
uint32_t time_secs = 0;
nkeynes@372
    55
uint32_t time_nanos = 0;
nkeynes@414
    56
extern uint32_t sh4_cpu_multiplier;
nkeynes@68
    57
nkeynes@106
    58
audio_driver_t audio_driver_list[] = { &audio_null_driver,
nkeynes@106
    59
				       &audio_esd_driver,
nkeynes@106
    60
				       NULL };
nkeynes@106
    61
nkeynes@144
    62
display_driver_t display_driver_list[] = { &display_null_driver,
nkeynes@144
    63
					   &display_gtk_driver,
nkeynes@144
    64
					   NULL };
nkeynes@106
    65
nkeynes@30
    66
int main (int argc, char *argv[])
nkeynes@1
    67
{
nkeynes@106
    68
    int opt, i;
nkeynes@372
    69
    double t;
nkeynes@1
    70
#ifdef ENABLE_NLS
nkeynes@68
    71
    bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
nkeynes@68
    72
    textdomain (PACKAGE);
nkeynes@1
    73
#endif
nkeynes@435
    74
    gboolean ui_initialized = gui_parse_cmdline(&argc, &argv);
nkeynes@68
    75
  
nkeynes@94
    76
    while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
nkeynes@68
    77
	switch( opt ) {
nkeynes@68
    78
	case 'a': /* AICA only mode - argument is an AICA program */
nkeynes@68
    79
	    aica_program = optarg;
nkeynes@68
    80
	    break;
nkeynes@144
    81
	case 'c': /* Config file */
nkeynes@144
    82
	    config_file = optarg;
nkeynes@144
    83
	    break;
nkeynes@144
    84
	case 'd': /* Mount disc */
nkeynes@144
    85
	    disc_file = optarg;
nkeynes@144
    86
	    break;
nkeynes@392
    87
	case 'D': /* Launch w/ debugger */
nkeynes@392
    88
	    show_debugger = TRUE;
nkeynes@392
    89
	    break;
nkeynes@414
    90
	case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
nkeynes@414
    91
	    t = strtod(optarg, NULL);
nkeynes@414
    92
	    sh4_cpu_multiplier = (int)(1000.0/t);
nkeynes@414
    93
	    break;
nkeynes@77
    94
	case 's': /* AICA-only w/ S3M player */
nkeynes@77
    95
	    aica_program = S3M_PLAYER;
nkeynes@77
    96
	    s3m_file = optarg;
nkeynes@77
    97
	    break;
nkeynes@68
    98
	case 'A': /* Audio driver */
nkeynes@106
    99
	    audio_driver_name = optarg;
nkeynes@68
   100
	    break;
nkeynes@68
   101
	case 'V': /* Video driver */
nkeynes@144
   102
	    display_driver_name = optarg;
nkeynes@68
   103
	    break;
nkeynes@68
   104
	case 'p': /* Start immediately */
nkeynes@68
   105
	    start_immediately = TRUE;
nkeynes@77
   106
    	    break;
nkeynes@209
   107
	case 'u': /* Allow unsafe dcload syscalls */
nkeynes@209
   108
	    dcload_set_allow_unsafe(TRUE);
nkeynes@182
   109
	    break;
nkeynes@87
   110
    	case 'b': /* No BIOS */
nkeynes@87
   111
    	    without_bios = TRUE;
nkeynes@87
   112
    	    break;
nkeynes@77
   113
        case 'h': /* Headless */
nkeynes@77
   114
            headless = TRUE;
nkeynes@77
   115
            break;
nkeynes@372
   116
	case 't': /* Time limit */
nkeynes@372
   117
	    t = strtod(optarg, NULL);
nkeynes@372
   118
	    time_secs = (uint32_t)t;
nkeynes@372
   119
	    time_nanos = (int)((t - time_secs) * 1000000000);
nkeynes@379
   120
	    break;
nkeynes@402
   121
	case 'x': /* Disable translator */
nkeynes@402
   122
	    use_xlat = FALSE;
nkeynes@379
   123
	    break;
nkeynes@68
   124
	}
nkeynes@68
   125
    }
nkeynes@30
   126
nkeynes@144
   127
    dreamcast_load_config( config_file );
nkeynes@144
   128
nkeynes@68
   129
    if( aica_program == NULL ) {
nkeynes@435
   130
	dreamcast_init();
nkeynes@68
   131
    } else {
nkeynes@68
   132
	dreamcast_configure_aica_only();
nkeynes@68
   133
	mem_load_block( aica_program, 0x00800000, 2048*1024 );
nkeynes@77
   134
	if( s3m_file != NULL ) {
nkeynes@77
   135
	    mem_load_block( s3m_file, 0x00810000, 2048*1024 - 0x10000 );
nkeynes@77
   136
	}
nkeynes@68
   137
    }
nkeynes@1
   138
nkeynes@87
   139
    if( without_bios ) {
nkeynes@87
   140
    	bios_install();
nkeynes@105
   141
	dcload_install();
nkeynes@87
   142
    }
nkeynes@106
   143
nkeynes@106
   144
    for( i=0; audio_driver_list[i] != NULL; i++ ) {
nkeynes@106
   145
	if( strcasecmp( audio_driver_list[i]->name, audio_driver_name ) == 0 ) {
nkeynes@111
   146
	    if( audio_set_driver( audio_driver_list[i], 44100, AUDIO_FMT_16ST ) == FALSE ) {
nkeynes@111
   147
		audio_set_driver( &audio_null_driver, 44100, AUDIO_FMT_16ST );
nkeynes@111
   148
	    }
nkeynes@106
   149
	    break;
nkeynes@106
   150
	}
nkeynes@106
   151
nkeynes@106
   152
    }
nkeynes@106
   153
    if( audio_driver_list[i] == NULL ) {
nkeynes@106
   154
	ERROR( "Audio driver '%s' not found, using null driver", audio_driver_name );
nkeynes@106
   155
	audio_set_driver( &audio_null_driver, 44100, AUDIO_FMT_16ST );
nkeynes@106
   156
    }
nkeynes@106
   157
nkeynes@106
   158
    if( headless ) {
nkeynes@144
   159
	display_set_driver( &display_null_driver );
nkeynes@106
   160
    } else {
nkeynes@435
   161
	gui_init(show_debugger);
nkeynes@435
   162
nkeynes@370
   163
	gboolean initialized = FALSE;
nkeynes@144
   164
	for( i=0; display_driver_list[i] != NULL; i++ ) {
nkeynes@144
   165
	    if( strcasecmp( display_driver_list[i]->name, display_driver_name ) == 0 ) {
nkeynes@370
   166
		initialized = display_set_driver( display_driver_list[i] );
nkeynes@106
   167
		break;
nkeynes@106
   168
	    }
nkeynes@106
   169
	}
nkeynes@370
   170
	if( !initialized ) {
nkeynes@370
   171
	    if( display_driver_list[i] == NULL ) {
nkeynes@370
   172
		ERROR( "Video driver '%s' not found, using null driver", display_driver_name );
nkeynes@370
   173
	    } else {
nkeynes@370
   174
		ERROR( "Video driver '%s' failed to initialize, falling back to null driver", display_driver_name );
nkeynes@370
   175
	    }
nkeynes@144
   176
	    display_set_driver( &display_null_driver );
nkeynes@106
   177
	}
nkeynes@106
   178
    }
nkeynes@106
   179
nkeynes@144
   180
    maple_reattach_all();
nkeynes@180
   181
    INFO( "%s! ready...", APP_NAME );
nkeynes@446
   182
nkeynes@446
   183
    for( ; optind < argc; optind++ ) {
nkeynes@446
   184
	gboolean ok = gdrom_mount_image(argv[optind]);
nkeynes@446
   185
	if( !ok ) {
nkeynes@446
   186
	    ok = file_load_magic( argv[optind] );
nkeynes@446
   187
	}
nkeynes@446
   188
	if( !ok ) {
nkeynes@446
   189
	    ERROR( "Unrecognized file '%s'", argv[optind] );
nkeynes@446
   190
	}
nkeynes@446
   191
	start_immediately = ok;
nkeynes@94
   192
    }
nkeynes@94
   193
nkeynes@144
   194
    if( disc_file != NULL ) {
nkeynes@144
   195
	gdrom_mount_image( disc_file );
nkeynes@144
   196
    }
nkeynes@144
   197
nkeynes@379
   198
    sh4_set_use_xlat( use_xlat );
nkeynes@379
   199
nkeynes@372
   200
    if( start_immediately ) {
nkeynes@372
   201
	if( time_nanos != 0 || time_secs != 0 ) {
nkeynes@372
   202
	    dreamcast_run_for(time_secs, time_nanos);
nkeynes@372
   203
	    return 0;
nkeynes@372
   204
	} else {
nkeynes@372
   205
	    dreamcast_run();
nkeynes@372
   206
	}
nkeynes@372
   207
    }
nkeynes@77
   208
    if( !headless ) {
nkeynes@435
   209
	gui_main_loop();
nkeynes@77
   210
    }
nkeynes@68
   211
    return 0;
nkeynes@1
   212
}
nkeynes@1
   213
.