// svgalib_menu_live_V1.c
// Written by Dave Barter 21st May 2002
// this program provides a crude front end to XMAME SVGALIB
// it is based upon the ncurses menu example
// it is not robust and needs more work !


#include <dirent.h>
#include <ncurses.h>
#include <menu.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD     4
#define ENTER_KEY 10
#define PLAYER_ONE_KEY 49
#define XMAME_ROM_PATH "/root/xmame/roms"
#define XMAME_COMMAND_STRING "/root/xmame/xmame.svgalib -out /dev/null -err /dev/null -rompath /root/xmame/roms %s &> /dev/null"

char *choices[500];

void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);

int main()
{


	ITEM **my_items;
	ITEM * cur;				// Currently selected menu item
	char command_string[200];	// This is where I will build my XMAME command
	TEXT d_text;			// Name of currently selected menu item
	int c;

    MENU *my_menu;
    WINDOW *my_menu_win;
    int n_choices, i;

    /* Initialize curses */
    initscr();
    start_color();
    cbreak();
    noecho();
    keypad(stdscr, TRUE);
    init_pair(1, COLOR_RED, COLOR_BLACK);
    init_pair(2, COLOR_CYAN, COLOR_BLACK);

    fill_choices();   // Load a directory into the choices array

    /* Create items */

    n_choices = ARRAY_SIZE(choices);
    my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
    for(i = 0; i < n_choices; ++i)
        my_items[i] = new_item(choices[i], "");


    /* Crate menu */
    my_menu = new_menu((ITEM **)my_items);

    /* Create the window to be associated with the menu */
    my_menu_win = newwin(10, 40, 4, 4);
    keypad(my_menu_win, TRUE);

    /* Set main window and sub window */
    set_menu_win(my_menu, my_menu_win);
    set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1));
    set_menu_format(my_menu, 5, 1);

    /* Set menu mark to the string " * " */
    set_menu_mark(my_menu, " * ");

    /* Print a border around the main window and print a title */
    box(my_menu_win, 0, 0);
    print_in_middle(my_menu_win, 1, 0, 40, " XMAME - Choose Game", COLOR_PAIR(1));
    mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
    mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38);
    mvwaddch(my_menu_win, 2, 39, ACS_RTEE);

    /* Post the menu */
    post_menu(my_menu);
    wrefresh(my_menu_win);

    attron(COLOR_PAIR(2));
    mvprintw(LINES - 2, 0, "Select your game and then press PLAYER ONE Key");

    attroff(COLOR_PAIR(2));
    refresh();

    while ((c = wgetch(my_menu_win)) != ENTER_KEY)
    {   switch(c)
        {   case KEY_DOWN:
                menu_driver(my_menu, REQ_DOWN_ITEM);
                break;
            case KEY_UP:
                menu_driver(my_menu, REQ_UP_ITEM);

                break;
            case KEY_NPAGE:    /* Page Down */
                menu_driver(my_menu, REQ_SCR_DPAGE);
                break;
            case KEY_PPAGE:    /* Page Up  */
                menu_driver(my_menu, REQ_SCR_UPAGE);
                break;

		}


		cur=current_item(my_menu); // Load up the currently selected menu item
		d_text = cur->name;		   // Get the name object out of the structure
		wrefresh(my_menu_win);

		if(c == PLAYER_ONE_KEY) break;	// This is messy, but for Chris and his keys

    }


	sprintf(command_string,XMAME_COMMAND_STRING, d_text.str);

    /* Unpost and free all the memory taken up */

    unpost_menu(my_menu);
    free_menu(my_menu);
    for(i = 0; i < n_choices; ++i)
        free_item(my_items[i]);
    endwin();


    if(!strcmp(d_text.str, "EXIT"))		// HACK ! to get out of menu system when booting from CD
	{
	exit(1);
	}

    // system(display_string);

	clear();
    refresh();

    printf("Loading XMAME .... please wait\n");


    execlp("/bin/bash", "sh", "-c", command_string, 0);

}

void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{   int length, x, y;
    float temp;

    if(win == NULL)
        win = stdscr;
    getyx(win, y, x);
    if(startx != 0)
        x = startx;
    if(starty != 0)
        y = starty;
    if(width == 0)
        width = 80;

    length = strlen(string);
    temp = (width - length)/ 2;
    x = startx + (int)temp;
    wattron(win, color);
    mvwprintw(win, y, x, "%s", string);
    wattroff(win, color);
    refresh();
}

int fill_choices(void)
{

	int count = 0;
	int n;
	struct dirent **namelist;

	//ARRRG ! Hard coded path names

	n=scandir(XMAME_ROM_PATH, &namelist, 0, alphasort);

	while(n--)
	 {
	choices[count++]=strtok(namelist[n]->d_name,".");

	 }

}
