Search
lxdream.org :: lxdream/src/main.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/main.c
changeset 537:d924be49e192
prev531:f0fee3ba71d1
next543:361ec0a70cf2
author nkeynes
date Tue Nov 20 10:27:58 2007 +0000 (13 years ago)
permissions -rw-r--r--
last change Move gtk UI into gtkui subdir (prep for non-gtk builds), and protect with
an automake conditional
file annotate diff log raw
nkeynes@30
     1
/**
nkeynes@495
     2
 * $Id: main.c,v 1.35 2007-11-07 11:45:53 nkeynes Exp $
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@68
    20
#include <unistd.h>
nkeynes@94
    21
#include <getopt.h>
nkeynes@27
    22
#include "dream.h"
nkeynes@450
    23
#include "config.h"
nkeynes@105
    24
#include "syscall.h"
nkeynes@422
    25
#include "mem.h"
nkeynes@27
    26
#include "dreamcast.h"
nkeynes@422
    27
#include "display.h"
nkeynes@422
    28
#include "loader.h"
nkeynes@450
    29
#include "gui.h"
nkeynes@106
    30
#include "aica/audio.h"
nkeynes@422
    31
#include "gdrom/gdrom.h"
nkeynes@144
    32
#include "maple/maple.h"
nkeynes@422
    33
#include "sh4/sh4core.h"
nkeynes@11
    34
nkeynes@77
    35
#define S3M_PLAYER "s3mplay.bin"
nkeynes@77
    36
nkeynes@414
    37
char *option_list = "a:m:s:A:V:puhbd:c:t:xD";
nkeynes@94
    38
struct option longopts[1] = { { NULL, 0, 0, 0 } };
nkeynes@68
    39
char *aica_program = NULL;
nkeynes@77
    40
char *s3m_file = NULL;
nkeynes@480
    41
const char *disc_file = NULL;
nkeynes@531
    42
char *display_driver_name = NULL;
nkeynes@531
    43
char *audio_driver_name = NULL;
nkeynes@68
    44
gboolean start_immediately = FALSE;
nkeynes@77
    45
gboolean headless = FALSE;
nkeynes@87
    46
gboolean without_bios = FALSE;
nkeynes@402
    47
gboolean use_xlat = TRUE;
nkeynes@392
    48
gboolean show_debugger = FALSE;
nkeynes@372
    49
uint32_t time_secs = 0;
nkeynes@372
    50
uint32_t time_nanos = 0;
nkeynes@414
    51
extern uint32_t sh4_cpu_multiplier;
nkeynes@68
    52
nkeynes@30
    53
int main (int argc, char *argv[])
nkeynes@1
    54
{
nkeynes@106
    55
    int opt, i;
nkeynes@372
    56
    double t;
nkeynes@531
    57
    gboolean display_ok;
nkeynes@495
    58
nkeynes@495
    59
    install_crash_handler();
nkeynes@1
    60
#ifdef ENABLE_NLS
nkeynes@68
    61
    bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
nkeynes@68
    62
    textdomain (PACKAGE);
nkeynes@1
    63
#endif
nkeynes@531
    64
    display_ok = gui_parse_cmdline(&argc, &argv);
nkeynes@464
    65
nkeynes@94
    66
    while( (opt = getopt_long( argc, argv, option_list, longopts, NULL )) != -1 ) {
nkeynes@68
    67
	switch( opt ) {
nkeynes@68
    68
	case 'a': /* AICA only mode - argument is an AICA program */
nkeynes@68
    69
	    aica_program = optarg;
nkeynes@68
    70
	    break;
nkeynes@144
    71
	case 'c': /* Config file */
nkeynes@450
    72
	    lxdream_set_config_filename(optarg);
nkeynes@144
    73
	    break;
nkeynes@144
    74
	case 'd': /* Mount disc */
nkeynes@144
    75
	    disc_file = optarg;
nkeynes@144
    76
	    break;
nkeynes@392
    77
	case 'D': /* Launch w/ debugger */
nkeynes@392
    78
	    show_debugger = TRUE;
nkeynes@392
    79
	    break;
nkeynes@414
    80
	case 'm': /* Set SH4 CPU clock multiplier (default 0.5) */
nkeynes@414
    81
	    t = strtod(optarg, NULL);
nkeynes@414
    82
	    sh4_cpu_multiplier = (int)(1000.0/t);
nkeynes@414
    83
	    break;
nkeynes@77
    84
	case 's': /* AICA-only w/ S3M player */
nkeynes@77
    85
	    aica_program = S3M_PLAYER;
nkeynes@77
    86
	    s3m_file = optarg;
nkeynes@77
    87
	    break;
nkeynes@68
    88
	case 'A': /* Audio driver */
nkeynes@106
    89
	    audio_driver_name = optarg;
nkeynes@68
    90
	    break;
nkeynes@68
    91
	case 'V': /* Video driver */
nkeynes@144
    92
	    display_driver_name = optarg;
nkeynes@68
    93
	    break;
nkeynes@68
    94
	case 'p': /* Start immediately */
nkeynes@68
    95
	    start_immediately = TRUE;
nkeynes@77
    96
    	    break;
nkeynes@209
    97
	case 'u': /* Allow unsafe dcload syscalls */
nkeynes@209
    98
	    dcload_set_allow_unsafe(TRUE);
nkeynes@182
    99
	    break;
nkeynes@87
   100
    	case 'b': /* No BIOS */
nkeynes@87
   101
    	    without_bios = TRUE;
nkeynes@87
   102
    	    break;
nkeynes@77
   103
        case 'h': /* Headless */
nkeynes@77
   104
            headless = TRUE;
nkeynes@77
   105
            break;
nkeynes@372
   106
	case 't': /* Time limit */
nkeynes@372
   107
	    t = strtod(optarg, NULL);
nkeynes@372
   108
	    time_secs = (uint32_t)t;
nkeynes@372
   109
	    time_nanos = (int)((t - time_secs) * 1000000000);
nkeynes@379
   110
	    break;
nkeynes@402
   111
	case 'x': /* Disable translator */
nkeynes@402
   112
	    use_xlat = FALSE;
nkeynes@379
   113
	    break;
nkeynes@68
   114
	}
nkeynes@68
   115
    }
nkeynes@30
   116
nkeynes@450
   117
    lxdream_load_config( );
nkeynes@144
   118
nkeynes@68
   119
    if( aica_program == NULL ) {
nkeynes@435
   120
	dreamcast_init();
nkeynes@68
   121
    } else {
nkeynes@68
   122
	dreamcast_configure_aica_only();
nkeynes@68
   123
	mem_load_block( aica_program, 0x00800000, 2048*1024 );
nkeynes@77
   124
	if( s3m_file != NULL ) {
nkeynes@77
   125
	    mem_load_block( s3m_file, 0x00810000, 2048*1024 - 0x10000 );
nkeynes@77
   126
	}
nkeynes@68
   127
    }
nkeynes@1
   128
nkeynes@87
   129
    if( without_bios ) {
nkeynes@87
   130
    	bios_install();
nkeynes@105
   131
	dcload_install();
nkeynes@87
   132
    }
nkeynes@106
   133
nkeynes@531
   134
    audio_driver_t audio_driver = get_audio_driver_by_name(audio_driver_name);
nkeynes@531
   135
    if( audio_driver == NULL ) {
nkeynes@531
   136
	ERROR( "Audio driver '%s' not found, aborting.", audio_driver_name );
nkeynes@531
   137
	exit(2);
nkeynes@531
   138
    } else if( audio_set_driver( audio_driver, 44100, AUDIO_FMT_16ST ) == FALSE ) {
nkeynes@531
   139
	ERROR( "Failed to initialize audio driver '%s', using null driver", 
nkeynes@531
   140
	       audio_driver->name );
nkeynes@106
   141
	audio_set_driver( &audio_null_driver, 44100, AUDIO_FMT_16ST );
nkeynes@106
   142
    }
nkeynes@106
   143
nkeynes@106
   144
    if( headless ) {
nkeynes@144
   145
	display_set_driver( &display_null_driver );
nkeynes@106
   146
    } else {
nkeynes@435
   147
	gui_init(show_debugger);
nkeynes@435
   148
nkeynes@531
   149
	display_driver_t display_driver = get_display_driver_by_name(display_driver_name);
nkeynes@531
   150
	if( display_driver == NULL ) {
nkeynes@531
   151
	    ERROR( "Video driver '%s' not found, aborting.", display_driver_name );
nkeynes@531
   152
	    exit(2);
nkeynes@531
   153
	} else if( display_set_driver( display_driver ) == FALSE ) {
nkeynes@531
   154
	    ERROR( "Video driver '%s' failed to initialize (could not connect to display?)", 
nkeynes@531
   155
		   display_driver->name );
nkeynes@531
   156
	    exit(2);
nkeynes@106
   157
	}
nkeynes@106
   158
    }
nkeynes@106
   159
nkeynes@144
   160
    maple_reattach_all();
nkeynes@180
   161
    INFO( "%s! ready...", APP_NAME );
nkeynes@446
   162
nkeynes@446
   163
    for( ; optind < argc; optind++ ) {
nkeynes@469
   164
	gboolean ok = gdrom_menu_open_file(argv[optind]);
nkeynes@446
   165
	if( !ok ) {
nkeynes@446
   166
	    ok = file_load_magic( argv[optind] );
nkeynes@446
   167
	}
nkeynes@446
   168
	if( !ok ) {
nkeynes@446
   169
	    ERROR( "Unrecognized file '%s'", argv[optind] );
nkeynes@446
   170
	}
nkeynes@446
   171
	start_immediately = ok;
nkeynes@94
   172
    }
nkeynes@94
   173
nkeynes@144
   174
    if( disc_file != NULL ) {
nkeynes@470
   175
	gdrom_menu_open_file( disc_file );
nkeynes@144
   176
    }
nkeynes@144
   177
nkeynes@464
   178
    if( gdrom_get_current_disc() == NULL ) {
nkeynes@464
   179
	disc_file = lxdream_get_config_value( CONFIG_GDROM );
nkeynes@464
   180
	if( disc_file != NULL ) {
nkeynes@469
   181
	    gdrom_menu_open_file( disc_file );
nkeynes@464
   182
	}
nkeynes@464
   183
    }
nkeynes@464
   184
nkeynes@379
   185
    sh4_set_use_xlat( use_xlat );
nkeynes@379
   186
nkeynes@372
   187
    if( start_immediately ) {
nkeynes@372
   188
	if( time_nanos != 0 || time_secs != 0 ) {
nkeynes@372
   189
	    dreamcast_run_for(time_secs, time_nanos);
nkeynes@372
   190
	    return 0;
nkeynes@372
   191
	} else {
nkeynes@372
   192
	    dreamcast_run();
nkeynes@372
   193
	}
nkeynes@372
   194
    }
nkeynes@77
   195
    if( !headless ) {
nkeynes@435
   196
	gui_main_loop();
nkeynes@77
   197
    }
nkeynes@68
   198
    return 0;
nkeynes@1
   199
}
nkeynes@1
   200
.