Ejemplo n.º 1
0
void
term_resize_check()
{
    if ((COLS == dgl_local_COLS) && (LINES == dgl_local_LINES) && !curses_resize) return;

    endwin();
    initcurses();
    dgl_local_COLS = COLS;
    dgl_local_LINES = LINES;
    curses_resize = 0;
}
Ejemplo n.º 2
0
void
term_resize_check()
{
    if ((COLS == dgl_local_COLS) && (LINES == dgl_local_LINES) && !curses_resize) return;

    signal(SIGWINCH, SIG_IGN);
    endwin();
    initcurses();
    dgl_local_COLS = COLS;
    dgl_local_LINES = LINES;
    curses_resize = 0;
    signal(SIGWINCH, sigwinch_func);
}
Ejemplo n.º 3
0
int
main(int argc, char *argv[])
{
	char cwd[PATH_MAX], *ipath;
	char *ifilter;

	if (argc > 2)
		usage(argv[0]);

	/* Confirm we are in a terminal */
	if (!isatty(0) || !isatty(1)) {
		fprintf(stderr, "stdin or stdout is not a tty\n");
		exit(1);
	}

	if (getuid() == 0)
		ifilter = ".";
	else
		ifilter = "^[^.]"; /* Hide dotfiles */

	if (argv[1] != NULL) {
		ipath = argv[1];
	} else {
		ipath = getcwd(cwd, sizeof(cwd));
		if (ipath == NULL)
			ipath = "/";
	}

	signal(SIGINT, SIG_IGN);

	/* Test initial path */
	if (canopendir(ipath) == 0) {
		fprintf(stderr, "%s: %s\n", ipath, strerror(errno));
		exit(1);
	}

	/* Set locale before curses setup */
	setlocale(LC_ALL, "");

	initcurses();

	browse(ipath, ifilter);

	exitcurses();

	exit(0);
}
Ejemplo n.º 4
0
int main(int argc, char ** argv)
{
    MainProg* mainprog;
    kLogOpen("boinctui.log");
    initcurses();
#ifdef DEBUG
    struct mallinfo minf1 = mallinfo();
#endif
    mainprog = new MainProg();
    mainprog->refresh();
    mainprog->mainloop(); //запускаем осн. цикл событий
    delete mainprog;
#ifdef DEBUG
    struct mallinfo minf2 = mallinfo();
    kLogPrintf("mallinfo.uordblks= %d-%d = %d (bytes leak)\n",minf1.uordblks,minf2.uordblks, minf2.uordblks-minf1.uordblks);
    //malloc_stats();
#endif
    donecurses();
    kLogClose();
    exit(EXIT_SUCCESS);
}
Ejemplo n.º 5
0
void init()
{
    /* Set the sleep time */
    sleeptime.tv_nsec = SLEEP;
    /* Initialize the curses. Curses initialises few variables that we need
     * further on */
    initcurses();
    /* Locale */
    setlocale(LC_ALL, "");
    /* Initialize arrays. SIZE is dependent on the life-window */
    cells = malloc(SIZE);
    if(cells == NULL)
        exit(1);
    memset(cells, 0, SIZE); /* Fill it with 0 */

    /* The buffer is identical to cells */
    buffer = malloc(SIZE);
    if(buffer == NULL)
        exit(1);
    memset(buffer, 0, SIZE);

    /* Write the first status information */
    status();
}
Ejemplo n.º 6
0
int
dgl_exec_cmdqueue(struct dg_cmdpart *queue, int game, struct dg_user *me)
{
    int i;
    struct dg_cmdpart *tmp = queue;
    char *p1;
    char *p2;
    int played = 0;

    if (!queue) return 1;

    p1 = (char *)malloc(1024);
    p2 = (char *)malloc(1024);

    if (!p1 || !p2) return 1;

    return_from_submenu = 0;

    while (tmp && !return_from_submenu) {
        if (tmp->param1) strcpy(p1, dgl_format_str(game, me, tmp->param1, NULL));
        if (tmp->param2) strcpy(p2, dgl_format_str(game, me, tmp->param2, NULL));

        switch (tmp->cmd) {
        default:
            break;
        case DGLCMD_RAWPRINT:
            if (p1) fprintf(stdout, "%s", p1);
            break;
        case DGLCMD_MKDIR:
            if (p1 && (access(p1, F_OK) != 0)) mkdir(p1, 0755);
            break;
        case DGLCMD_UNLINK:
            if (p1 && (access(p1, F_OK) == 0)) unlink(p1);
            break;
        case DGLCMD_CHDIR:
            if (p1) {
                if (chdir(p1) == -1) {
                    debug_write("chdir-command failed");
                    graceful_exit(123);
                }
            }
            break;
        case DGLCMD_IF_NX_CP:
            if (p1 && p2) {
                FILE *tmpfile;
                tmpfile = fopen(p2, "r");
                if (tmpfile) {
                    fclose(tmpfile);
                    break;
                }
            }
        /* else fall through to cp */
        case DGLCMD_CP:
            if (p1 && p2) {
                FILE *cannedf, *newfile;
                char buf[1024];
                size_t bytes;
                /* FIXME: use nethack-themed error messages here, as per write_canned_rcfile() */
                if (!(cannedf = fopen (p1, "r"))) break;
                if (!(newfile = fopen (p2, "w"))) break;
                while ((bytes = fread (buf, 1, 1024, cannedf)) > 0) {
                    if (fwrite (buf, 1, bytes, newfile) != bytes) {
                        if (ferror (newfile)) {
                            fclose (cannedf);
                            fclose (newfile);
                            break;
                        }
                    }
                }
                fclose (cannedf);
                fclose (newfile);
                chmod (p2, default_fmode);
            }
            break;
        case DGLCMD_EXEC:
            if (p1 && p2) {
                pid_t child;
                char *myargv[3];

                myargv[0] = p1;
                myargv[1] = p2;
                myargv[2] = 0;

                clear();
                refresh();
                endwin();
                idle_alarm_set_enabled(0);
                child = fork();
                if (child == -1) {
                    perror("fork");
                    debug_write("exec-command fork failed");
                    graceful_exit(114);
                } else if (child == 0) {
                    execvp(p1, myargv);
                    exit(0);
                } else
                    waitpid(child, NULL, 0);
                idle_alarm_set_enabled(1);
                initcurses();
                check_retard(1);
            }
            break;
        case DGLCMD_SETENV:
            if (p1 && p2) mysetenv(p1, p2, 1);
            break;
        case DGLCMD_CHPASSWD:
            if (loggedin) changepw(1);
            break;
        case DGLCMD_CHMAIL:
            if (loggedin) change_email();
            break;
        case DGLCMD_WATCH_MENU:
            inprogressmenu(-1);
            break;
        case DGLCMD_LOGIN:
            if (!loggedin) loginprompt(0);
            if (loggedin) runmenuloop(dgl_find_menu(get_mainmenu_name()));
            break;
        case DGLCMD_REGISTER:
            if (!loggedin && globalconfig.allow_registration) newuser();
            break;
        case DGLCMD_QUIT:
            debug_write("command: quit");
            graceful_exit(0);
        /* break; */
        case DGLCMD_SUBMENU:
            if (p1)
                runmenuloop(dgl_find_menu(p1));
            break;
        case DGLCMD_RETURN:
            return_from_submenu = 1;
            break;
        case DGLCMD_PLAY_IF_EXIST:
            if (!(loggedin && me && p1 && p2)) break;
            {
                FILE *tmpfile;
                tmpfile = fopen(p2, "r");
                if (tmpfile) {
                    fclose(tmpfile);
                } else break;
            }
        /* else fall through to playgame */
        case DGLCMD_PLAYGAME:
            if (loggedin && me && p1 && !played) {
                int userchoice, i;
                char *tmpstr;
                for (userchoice = 0; userchoice < num_games; userchoice++) {
                    if (!strcmp(myconfig[userchoice]->game_id, p1) || !strcmp(myconfig[userchoice]->game_name, p1) || !strcmp(myconfig[userchoice]->shortname, p1)) {
                        if (purge_stale_locks(userchoice)) {
                            if (myconfig[userchoice]->rcfile) {
                                if (access (dgl_format_str(userchoice, me, myconfig[userchoice]->rc_fmt, NULL), R_OK) == -1)
                                    write_canned_rcfile (userchoice, dgl_format_str(userchoice, me, myconfig[userchoice]->rc_fmt, NULL));
                            }

                            setproctitle("%s [playing %s]", me->username, myconfig[userchoice]->shortname);

                            clear();
                            refresh();
                            endwin ();

                            /* first run the generic "do these when a game is started" commands */
                            dgl_exec_cmdqueue(globalconfig.cmdqueue[DGLTIME_GAMESTART], userchoice, me);
                            /* then run the game-specific commands */
                            dgl_exec_cmdqueue(myconfig[userchoice]->cmdqueue, userchoice, me);

                            /* fix the variables in the arguments */
                            for (i = 0; i < myconfig[userchoice]->num_args; i++) {
                                tmpstr = strdup(dgl_format_str(userchoice, me, myconfig[userchoice]->bin_args[i], NULL));
                                free(myconfig[userchoice]->bin_args[i]);
                                myconfig[userchoice]->bin_args[i] = tmpstr;
                            }

                            signal(SIGWINCH, SIG_DFL);
                            signal(SIGINT, SIG_DFL);
                            signal(SIGQUIT, SIG_DFL);
                            signal(SIGTERM, SIG_DFL);
                            idle_alarm_set_enabled(0);
                            /* launch program */
                            ttyrec_main (userchoice, me->username,
                                         dgl_format_str(userchoice, me, myconfig[userchoice]->ttyrecdir, NULL),
                                         gen_ttyrec_filename());
                            idle_alarm_set_enabled(1);
                            played = 1;
                            /* lastly, run the generic "do these when a game is left" commands */
                            signal (SIGHUP, catch_sighup);
                            signal (SIGINT, catch_sighup);
                            signal (SIGQUIT, catch_sighup);
                            signal (SIGTERM, catch_sighup);
                            signal(SIGWINCH, sigwinch_func);

                            dgl_exec_cmdqueue(myconfig[userchoice]->postcmdqueue, userchoice, me);

                            dgl_exec_cmdqueue(globalconfig.cmdqueue[DGLTIME_GAMEEND], userchoice, me);

                            setproctitle ("%s", me->username);
                            initcurses ();
                            check_retard(1); /* reset retard counter */
                        }
                        break;
                    }
                }
            }
            break;
        }
        tmp = tmp->next;
    }

    free(p1);
    free(p2);

    return 0;
}
Ejemplo n.º 7
0
int
main (int argc, char **argv)
{
	int     i,l;		/* non-descript indices */
	char    c;		/* non-descript character storage */

	if (pledge("stdio rpath wpath cpath tty exec", NULL) == -1)
		err(1, "pledge");

	signal(SIGINT, getout);	/* trap interrupts */

	/* use whole screen for text */
	begscr = 0;

	getarg(argc, argv);

	initcurses();

	/* check if restored game and save flag for later */
	if ((rfl = rflag)) {
		if (pledge("stdio rpath wpath cpath tty", NULL) == -1)
			err(1, "pledge");

		wrboard();	/* print board */
		/* if new game, pretend to be a non-restored game */
		if (cturn == 0)
			rflag = 0;
	} else {
		rscore = wscore = 0;	/* zero score */

		if (aflag) {	/* print rules */
			addstr(rules);
			if (yorn(0)) {
				endwin();
				execl(TEACH, "teachgammon", (char *)NULL);

				err(1, "%s", noteach);
			} else {/* if not rules, then instructions */
				addstr(need);
				if (yorn(0)) {	/* print instructions */
					clear();
					text(instruct);
				}
			}
		}

		if (pledge("stdio rpath wpath cpath tty", NULL) == -1)
			err(1, "pledge");

		init();		/* initialize board */

		if (pnum == 2) {/* ask for color(s) */
			printw("\n%s", askcol);
			while (pnum == 2) {
				c = readc();
				switch (c) {

				case 'R':	/* red */
					pnum = -1;
					break;

				case 'W':	/* white */
					pnum = 1;
					break;

				case 'B':	/* both */
					pnum = 0;
					break;

				case 'P':	/* Control the dice */
					iroll = 1;
					addstr("\nDice controlled!\n");
					addstr(askcol);
					break;

				default:	/* error */
					beep();
				}
			}
		}

		wrboard();		/* print board */

		move(18, 0);
	}
	/* limit text to bottom of screen */
	begscr = 17;

	for (;;)  {			/* begin game! */
		/* initial roll if needed */
		if ((!rflag) || raflag)
			roll();

		/* perform ritual of first roll */
		if (!rflag) {
			move(17, 0);
			while (D0 == D1)	/* no doubles */
				roll();

			/* print rolls */
			printw("%s%d%s%d", rollr, D0, rollw, D1);

			/* winner goes first */
			if (D0 > D1) {
				addstr(rstart);
				cturn = 1;
			} else {
				addstr(wstart);
				cturn = -1;
			}
		}
		/* initialize variables according to whose turn it is */

		if (cturn == 1) {	/* red */
			home = 25;
			bar = 0;
			inptr = &in[1];
			inopp = &in[0];
			offptr = &off[1];
			offopp = &off[0];
			Colorptr = &color[1];
			colorptr = &color[3];
			colen = 3;
		} else {		/* white */
			home = 0;
			bar = 25;
			inptr = &in[0];
			inopp = &in[1];
			offptr = &off[0];
			offopp = &off[1];
			Colorptr = &color[0];
			colorptr = &color[2];
			colen = 5;
		}

		/* do first move (special case) */
		if (!(rflag && raflag)) {
			if (cturn == pnum)	/* computer's move */
				domove(0);
			else {	/* player's move */
				mvlim = movallow();
				/* reprint roll */
				move(cturn == -1 ? 18 : 19, 0);
				proll();
				getmove();	/* get player's move */
			}
		}
		move(17, 0);
		clrtoeol();
		begscr = 18;
		/* no longer any difference between normal and recovered game. */
		rflag = 0;

		/* move as long as it's someone's turn */
		while (cturn == 1 || cturn == -1) {

			/* board maintainence */
			moveplayers();	/* fix board */

			/* do computer's move */
			if (cturn == pnum) {
				domove(1);

				/* see if double refused */
				if (cturn == -2 || cturn == 2)
					break;

				/* check for winning move */
				if (*offopp == 15) {
					cturn *= -2;
					break;
				}
				continue;

			}
			/* (player's move) */

			/* clean screen if safe */
			if (hflag) {
				move(20, 0);
				clrtobot();
				hflag = 1;
			}
			/* if allowed, give him a chance to double */
			if (dflag && dlast != cturn && gvalue < 64) {
				move(cturn == -1 ? 18: 19, 0);
				addstr(*Colorptr);
				c = readc();

				/* character cases */
				switch (c) {

				case 'R':		/* reprint board */
					wrboard();
					break;

				case 'S':		/* save game */
					raflag = 1;
					save(1);
					break;

				case 'Q':		/* quit */
					quit();
					break;

				case 'D':		/* double */
					dble();
					break;

				case ' ':		/* roll */
				case '\n':
					roll();
					printw(" rolls %d %d.  ", D0, D1);

					/* see if he can move */
					if ((mvlim = movallow()) == 0) {

						/* can't move */
						printw("%s%s%s", toobad1, *colorptr, unable);
						if (pnum) {
							moveplayers();
							sleep(MVPAUSE);
						}
						nexturn();
						break;
					}

					getmove();

					/* okay to clean screen */
					hflag = 1;
					break;

				default:		/* invalid character */

					/* print help message */
					move(20, 0);
					text(helpm);
					move(cturn == -1 ? 18 : 19, 0);

					/* don't erase */
					hflag = 0;
				}
			} else {/* couldn't double */

				/* print roll */
				roll();
				move(cturn == -1 ? 18: 19, 0);
				proll();

				/* can he move? */
				if ((mvlim = movallow()) == 0) {

					/* he can't */
					printw("%s%s%s", toobad2, *colorptr, cantmv);
					moveplayers();
					sleep(MVPAUSE);
					nexturn();
					continue;
				}

				getmove();
			}
		}

		/* don't worry about who won if quit */
		if (cturn == 0)
			break;

		/* fix cturn = winner */
		cturn /= -2;

		/* final board pos. */
		moveplayers();

		/* backgammon? */
		mflag = 0;
		l = bar + 7 * cturn;
		for (i = bar; i != l; i += cturn)
			if (board[i] * cturn)
				mflag++;

		/* compute game value */
		move(20, 0);
		if (*offopp == 15) {
			if (mflag) {
				addstr(bgammon);
				gvalue *= 3;
			}
			else if (*offptr <= 0) {
				addstr(gammon);
				gvalue *= 2;
			}
		}
		/* report situation */
		if (cturn == -1) {
			addstr("Red wins ");
			rscore += gvalue;
		} else {
			addstr("White wins ");
			wscore += gvalue;
		}
		printw("%d point%s.\n", gvalue, (gvalue > 1) ? "s":"");

		/* write score */
		wrscore();

		/* see if he wants another game */
		addstr(again);
		if ((i = yorn('S')) == 0)
			break;

		init();
		if (i == 2) {
			addstr("  Save.\n");
			cturn = 0;
			save(0);
		}
		/* yes, reset game */
		wrboard();
	}

	/* give him a chance to save if game was recovered */
	if (rfl && cturn) {
		addstr(svpromt);
		if (yorn(0)) {
			/* re-initialize for recovery */
			init();
			cturn = 0;
			save(0);
		}
	}
	/* leave peacefully */
	getout(0);
	/* NOT REACHED */
}
Ejemplo n.º 8
0
int main(int argc, char *argv[]) {
	int exit_code = 0;
	slimproto_t slimproto;
	slimaudio_t slimaudio;

	PaDeviceIndex output_device_id = PA_DEFAULT_DEVICE;
	char *output_device_name = NULL;
	char *hostapi_name = NULL;

	unsigned int output_predelay = 0;
	unsigned int output_predelay_amplitude = 0;
#ifdef EMPEG
	bool power_bypass = false, power_last = false;
	bool geteq = false;
	long key, ir;
#endif
#ifdef EMPEG
	slimaudio_volume_t volume_control = VOLUME_DRIVER;
#else
	slimaudio_volume_t volume_control = VOLUME_SOFTWARE;
#endif
	unsigned int retry_interval = RETRY_DEFAULT;

	char macaddress[6] = { 0, 0, 0, 0, 0, 1 };

	int keepalive_interval = -1;

	bool listdevs = false;
	bool listservers = false;
	bool discover_server = false;
	unsigned int json_port;

#ifdef SLIMPROTO_ZONES
	bool default_macaddress = true;
	unsigned int zone = 0;
	unsigned int num_zones = 1;
#endif
#ifdef DAEMONIZE
	bool should_daemonize = false;
	char *logfile = NULL;
#endif
#ifdef PORTAUDIO_DEV
	/* User suggested latency */
	bool modify_latency = false;
	unsigned int user_latency = 0L;
#endif
	char slimserver_address[INET_FQDNSTRLEN] = "127.0.0.1";

	char getopt_options[OPTLEN] = "a:FId:Y:e:f:hk:Lm:n:o:P:p:Rr:TO:Vv:";

	static struct option long_options[] = {
		{"predelay_amplitude", required_argument, 0, 'a'},
		{"discover",           no_argument,       0, 'F'},
		{"debug",              required_argument, 0, 'd'},
		{"debuglog",           required_argument, 0, 'Y'},
		{"help",               no_argument,       0, 'h'},
		{"keepalive",          required_argument, 0, 'k'},
		{"list",               no_argument,       0, 'L'},
		{"findservers",        no_argument,       0, 'I'},
		{"mac",	               required_argument, 0, 'm'},
		{"name",               required_argument, 0, 'n'},
		{"output",             required_argument, 0, 'o'},
		{"playerid",           required_argument, 0, 'e'},
		{"firmware",           required_argument, 0, 'f'},
		{"port",               required_argument, 0, 'P'},
		{"predelay",           required_argument, 0, 'p'},
		{"threshold_override", no_argument,       0, 'T'},
		{"output_threshold",   required_argument, 0, 'O'},
#ifdef EMPEG
		{"puteq",              no_argument,       0, 'Q'},
		{"geteq",              no_argument,       0, 'q'},
#endif
		{"retry",              no_argument,       0, 'R'},
		{"intretry",           required_argument, 0, 'r'},
		{"version",            no_argument,       0, 'V'},
		{"volume",             required_argument, 0, 'v'},
		{"zone",               required_argument, 0, 'z'},
#ifdef PORTAUDIO_DEV
		{"latency",            required_argument, 0, 'y'},
		{"audiotype",          required_argument, 0, 't'},
#else
		{"paframes",           required_argument, 0, 'g'},
		{"pabuffers",          required_argument, 0, 'j'},
#endif
#ifdef DAEMONIZE
		{"daemonize",          required_argument, 0, 'M'},
#endif
#ifdef __WIN32__
		{"highpriority",       no_argument,       0, 'H'},
#ifdef PADEV_WASAPI
		{"shared",             no_argument,       0, 'S'},
#endif
#endif
#ifdef INTERACTIVE
		{"lircrc",             required_argument, 0, 'c'},
		{"lirc",               no_argument,       0, 'i'},
		{"lcd",                no_argument,       0, 'l'},
		{"lcdc",               no_argument,       0, 'C'},
		{"display",            no_argument,       0, 'D'},
		{"width",              required_argument, 0, 'w'},
#endif
#ifdef SLIMPROTO_RENICE
		{"renice",             no_argument,       0, 'N'},
#endif
#ifdef SLIMPROTO_ZONES
		{"zone",               required_argument, 0, 'z'},
#endif
		{0, 0, 0, 0}
	};

#ifdef INTERACTIVE
        fd_set read_fds;
        fd_set write_fds;
        int key = 0;
        unsigned long ir = 0;
	int maxfd = 0;
	char *home;
	struct timeval timeout;
	timeout.tv_usec = 0;

#ifdef __WIN32__
	int WSAerrno;
	int ptw32_processInitialize (void);
	ptw32_processInitialize();
#endif /* __WIN32__ */
        /* default lircrc file ($HOME/.lircrc) */
	home = getenv("HOME");
	if (home == NULL) home = "";
	lircrc = (char *)malloc((strlen(home) + strlen("/.lircrc") + 1) * sizeof(char));
	strcpy(lircrc,home);
	strcat(lircrc,"/.lircrc");
#endif /* INTERACTIVE */

#ifdef EMPEG
	strcat (getopt_options, "Qq");
#endif
#ifdef PORTAUDIO_DEV
	strcat (getopt_options, "y:t:");
#else
	strcat (getopt_options, "g:j:");
#endif	
#ifdef DAEMONIZE	
	strcat (getopt_options, "M:");
#endif
#ifdef INTERACTIVE
	strcat (getopt_options, "c:CDilw:");
#endif
#ifdef __WIN32__
	strcat (getopt_options, "H");
#ifdef PADEV_WASAPI
	strcat (getopt_options, "S");
#endif
#endif
#ifdef SLIMPROTO_RENICE
	strcat (getopt_options, "N");
#endif
#ifdef SLIMPROTO_ZONES
	strcat (getopt_options, "z:");
#endif
#ifdef EMPEG
	empeg_getmac(macaddress);
#endif

	while (true) {
		const char shortopt =
			getopt_long_only(argc, argv, getopt_options, long_options, NULL);

		if (shortopt == (char) -1) {
			break;
		}

		switch (shortopt) {
		case 'a':
			output_predelay_amplitude = strtoul(optarg, NULL, 0);
			break;
		case 'F':
			discover_server = true;
			break;
		case 'd':
#ifdef SLIMPROTO_DEBUG
			if (strcmp(optarg, "all") == 0)
			{
				slimproto_debug = true;
				slimaudio_debug = true;
				slimaudio_buffer_debug = true;
				slimaudio_buffer_debug_v = true;
				slimaudio_decoder_debug = true;
				slimaudio_decoder_debug_r = true;
				slimaudio_decoder_debug_v = true;
				slimaudio_http_debug = true;
				slimaudio_http_debug_v = true;
				slimaudio_output_debug = true;
				slimaudio_output_debug_v = true;
			}
			else if (strcmp(optarg, "slimproto") == 0)
				slimproto_debug = true;
			else if (strcmp(optarg, "slimaudio") == 0)
				slimaudio_debug = true;
			else if (strcmp(optarg, "slimaudio_buffer") == 0)
				slimaudio_buffer_debug = true;
			else if (strcmp(optarg, "slimaudio_buffer_v") == 0)
				slimaudio_buffer_debug_v = true;
			else if (strcmp(optarg, "slimaudio_decoder") == 0)
				slimaudio_decoder_debug = true;
			else if (strcmp(optarg, "slimaudio_decoder_r") == 0)
				slimaudio_decoder_debug_r = true;
			else if (strcmp(optarg, "slimaudio_decoder_v") == 0)
				slimaudio_decoder_debug_v = true;
			else if (strcmp(optarg, "slimaudio_http") == 0)
				slimaudio_http_debug = true;
			else if (strcmp(optarg, "slimaudio_http_v") == 0)
				slimaudio_http_debug_v = true;
			else if (strcmp(optarg, "slimaudio_output") == 0)
				slimaudio_output_debug = true;
			else if (strcmp(optarg, "slimaudio_output_v") == 0)
				slimaudio_output_debug_v = true;
			else
				fprintf(stderr, "%s: Unknown debug option %s\n", argv[0], optarg);
#else
				fprintf(stderr, "%s: Recompile with -DSLIMPROTO_DEBUG to enable debugging.\n", argv[0]);
#endif
			break;
		case 'Y':
#ifdef SLIMPROTO_DEBUG
                        if ( optarg == NULL )
                        {
                                fprintf(stderr, "%s: Cannot parse debug log filename %s\n", argv[0], optarg);
                                exit(-1);
                        } else
                        {
				debuglog = freopen( optarg, "a", stderr);
				if ( debuglog )
					debug_logfile = true;
				else
					fprintf(stderr, "%s: Redirection of stderr to %s failed.\n", argv[0], optarg);
                        }
#endif
			break;

/* From server/Slim/Networking/Slimproto.pm from 7.5r28596
** squeezebox(2)
** softsqueeze(3)
** squeezebox2(4)
** transporter(5)
** softsqueeze3(6)
** receiver(7)
** squeezeslave(8)
** controller(9)
** boom(10)
** softboom(11)
** squeezeplay(12)
** radio(13)
** touch(14)
*/
		case 'e':
			player_type = strtoul(optarg, NULL, 0);
			if ( (player_type < 2) || (player_type > 14) )
			{
				player_type = PLAYER_TYPE;
				fprintf(stderr, "%s: Unknown player type, using (%d)\n", argv[0], player_type);
			}
			break;
		case 'f':
			firmware = strtoul(optarg, NULL, 0);
			if ( (firmware < 0) || (firmware > 254) )
			{
				firmware = FIRMWARE_VERSION;
				fprintf(stderr, "%s: Invalid firmware value, using (%d)\n", argv[0], firmware);
			}
			break;
		case 'h':
			print_help();
			exit(0);	
		case 'k':
			keepalive_interval = strtoul(optarg, NULL, 0);
			break;
		case 'T':
			threshold_override = true;
			break;
		case 'O':
			output_threshold = strtoul(optarg, NULL, 0);
			if ( (output_threshold < 0) || (output_threshold > 1000000) )
			{
				output_threshold = OUTPUT_THRESHOLD;
				fprintf(stderr, "%s: Invalid output threshold, using (%d)\n",
					argv[0], output_threshold);
			}
			break;
		case 'm':
			if (parse_macaddress(macaddress, optarg) != 0) {
				fprintf(stderr, "%s: Cannot parse mac address %s\n", argv[0], optarg);
				exit(-1);	
			}
#ifdef SLIMPROTO_ZONES
			default_macaddress = false;
#endif
			break;
#ifdef DAEMONIZE
		case 'M':
			if ( optarg == NULL )
			{
				fprintf(stderr, "%s: Cannot parse log filename %s\n", argv[0], optarg);
				exit(-1);	
			} else
			{
				logfile = optarg;
			}
			should_daemonize = true;
			break;
#endif

#ifdef __WIN32__
		case 'H':
			/* Change Window process priority class to HIGH */
			if ( !SetPriorityClass ( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
			{
				int dwError = GetLastError();
				fprintf(stderr, "%s: Failed to set priority (%d), using default.\n", argv[0],
					dwError);
			} 
			break;
#ifdef PADEV_WASAPI
		case 'S':
			wasapi_exclusive = false;
			break;
#endif
#endif
#ifdef SLIMPROTO_RENICE
		case 'N':
			renice = true;
			break;
#endif
		case 'n':
			output_device_name = optarg;
			output_change = true;
			break;
		case 'o':
			output_device_id = strtoul(optarg, NULL, 0);
			output_change = true;
			break;
		case 'p':
			output_predelay = strtoul(optarg, NULL, 0);
			break;
		case 'P':
			port = strtoul(optarg, NULL, 0);
			if ( (port < 0) || (port > 65535) )
			{
				port = SLIMPROTOCOL_PORT;
				fprintf(stderr, "%s: Invalid port number, using %d.\n", argv[0], port);
			}
			break;
			break;
#ifdef EMPEG
		case 'Q':
			empeg_puteq_tofile();
			exit(0);
			break;
		case 'q':
			geteq = true;
			break;
#endif
		case 'R':
			retry_connection = true;
			break;
		case 'r':
			retry_connection = true;
			retry_interval = strtoul(optarg, NULL, 0);
			if ( ( retry_interval < 1 ) || ( retry_interval > 120 ) )
			{
				retry_interval = RETRY_DEFAULT;
				fprintf (stderr, "Invalid retry interval, using %d seconds.\n", retry_interval );
			}
			break;
#ifdef INTERACTIVE
		case 'c':
		        free(lircrc);
			lircrc = optarg;
			break;
#ifndef __WIN32__
		case 'i':
			using_lirc = true;
			break;
		case 'l':
			use_lcdd_menu = true;
			break;
		case 'C':
			use_lcdd_menu = true;
			lcdd_compat = true;
			break;
#endif
		case 'D':
			using_curses = 1;
			break;
		case 'w':
			linelen = strtoul(optarg, NULL, 0);
			break;
#endif
		case 'L':
			listdevs = true;
			break;
		case 'I':
			listservers = true;
			break;
		case 'V':
			print_version();
			exit(0);
			break;
		case 'v':
		 	if (strcmp(optarg, "sw") == 0) {
				volume_control = VOLUME_SOFTWARE;
			}
#ifndef PORTAUDIO_DEV
			else if (strcmp(optarg, "on") == 0 ) {
				volume_control = VOLUME_DRIVER;
			}
#endif
			else if (strcmp(optarg, "off") == 0 ) {
				volume_control = VOLUME_NONE;
			}
			break;
#ifdef PORTAUDIO_DEV
		case 'y':
			modify_latency = true;
			user_latency = strtoul(optarg, NULL, 0);

			if ( user_latency > 1000 )
			{
				fprintf (stderr, "Suggested latency invalid, using device default.\n");
				modify_latency = false;
			}
			break;
		case 't':
			hostapi_name = optarg;
			break;
#else
		case 'g':
			pa_framesPerBuffer = strtoul(optarg, NULL, 0);

			if ( (pa_framesPerBuffer > 65536) || (pa_framesPerBuffer < 64) )
			{
				fprintf (stderr,
					"Portaudio frames per buffer invalid, using default (%d).\n",
					PA_FRAMES_PER_BUFFER);
				pa_framesPerBuffer = PA_FRAMES_PER_BUFFER ;
			}
			break;
		case 'j':
			pa_numberOfBuffers = strtoul(optarg, NULL, 0);

			if ( (pa_numberOfBuffers > 64) || (pa_numberOfBuffers < 0) )
			{
				fprintf (stderr,
					"Number of Portaudio buffers invalid, using default (%d).\n",
					PA_NUM_BUFFERS);
				pa_numberOfBuffers = PA_NUM_BUFFERS;
			}
			break;
#endif
#ifdef SLIMPROTO_ZONES
		case 'z':
			if (sscanf(optarg, "%u/%u", &zone, &num_zones) != 2)
			{
				fprintf (stderr, "Invalid zone specification, using default.\n");
			}
			if (num_zones > MAX_ZONES)
			{
				fprintf(stderr, "Number of zones > %d not supported\n", MAX_ZONES);
				zone=0;
				num_zones=1;
			}
			if (num_zones <= zone)
			{
				fprintf (stderr, "Invalid zone specification, using default.\n");
				zone = 0;
				num_zones = 1;
			}
			break;
#endif
		default:
			break;
		}
	}

	if (listdevs) {
		GetAudioDevices(output_device_id, output_device_name, hostapi_name, output_change, true);
		exit(0);
	}

	if (listservers) {
		slimproto_discover(slimserver_address, sizeof(slimserver_address), port, &json_port, true);
		exit(0);
	}

	if (optind < argc)
		strncpy(slimserver_address, argv[optind], sizeof(slimserver_address));

#ifdef DAEMONIZE
	if ( should_daemonize ) {
#ifdef INTERACTIVE
		if ( using_curses || use_lcdd_menu )
		{
			fprintf(stderr, "Daemonize not supported with display modes.\n");
			exit(-1);
		}
		else
#endif
			init_daemonize();
	}
#endif
	signal(SIGTERM, &exit_handler);
	signal(SIGINT, &exit_handler);
	install_restart_handler();

#ifdef INTERACTIVE
	install_toggle_handler();  /*SIGUSR2 to toggle IR/LCD on and off */
#endif
	if (slimproto_init(&slimproto) < 0) {
		fprintf(stderr, "Failed to initialize slimproto\n");
		exit(-1);	
	}

#ifdef SLIMPROTO_ZONES
	if (slimaudio_init(&slimaudio, &slimproto, output_device_id, output_device_name,
		hostapi_name, output_change, zone, num_zones) < 0)
#else
	if (slimaudio_init(&slimaudio, &slimproto, output_device_id, output_device_name,
		hostapi_name, output_change) < 0)
#endif
	{
		fprintf(stderr, "Failed to initialize slimaudio\n");
		exit(-1);
	}
#ifdef SLIMPROTO_ZONES
	if (default_macaddress)
		macaddress[5] += zone;
#endif
#ifdef PORTAUDIO_DEV
	if( modify_latency ) {
		slimaudio_set_latency( &slimaudio, user_latency );
	}
#endif	
	slimaudio_set_renice( &slimaudio, renice );
	slimproto_add_connect_callback(&slimproto, connect_callback, macaddress);

#ifdef INTERACTIVE
	/* Process VFD (display) commands */
	if ( using_curses || using_lirc || use_lcdd_menu )
		slimproto_add_command_callback(&slimproto, "vfdc", vfd_callback, macaddress);
#endif
#ifdef EMPEG
	slimproto_add_command_callback(&slimproto, "grfe", empeg_vfd_callback, macaddress);
	slimproto_add_command_callback(&slimproto, "grfb", empeg_vfdbrt_callback, macaddress);
	slimproto_add_command_callback(&slimproto, "aude", empeg_aude_callback, macaddress);
#endif

	slimaudio_set_volume_control(&slimaudio, volume_control);
	slimaudio_set_output_predelay(&slimaudio, output_predelay, output_predelay_amplitude);

	if (keepalive_interval >= 0) {
		slimaudio_set_keepalive_interval(&slimaudio, keepalive_interval);
	}

#ifdef INTERACTIVE
	init_lcd();
#endif
#ifdef EMPEG
	empeg_init();
	if (geteq)
	   empeg_geteq_fromfile();
	power_last = empeg_state.power_on;
	empeg_state.power_on = false;
#endif

	if (slimaudio_open(&slimaudio) < 0) {
		fprintf(stderr, "Failed to open slimaudio\n");
		exit_code = -1;
		goto exit;
	}

#ifdef SLIMPROTO_DEBUG
	if (slimaudio_debug)
		fprintf ( stderr, "Using audio device index: %d\n", slimaudio.output_device_id );
#endif

#ifdef INTERACTIVE
        init_lirc();

	setlocale(LC_ALL, "");
	initcurses();
#endif
#ifdef DAEMONIZE
	if ( should_daemonize ) {
		daemonize(logfile);
	}
#endif
	/* When retry_connection is true, retry connecting to Squeezebox Server 
	** until we succeed, unless the signal handler tells us to give up.
	*/
	do {
		if (signal_restart_flag) { 
#ifdef INTERACTIVE
			exitcurses();
#endif
			fprintf(stderr,"Retry in %d seconds.\n", retry_interval);
			Pa_Sleep(1000 * retry_interval);
#ifdef INTERACTIVE
	   	        initcurses();
#endif
		}
#ifdef EMPEG
		if (discover_server && empeg_state.last_server[0] != '\0')
		{
			strcpy(slimserver_address, (char *)empeg_state.last_server);
			empeg_state.last_server[0] = '\0';
		}
		else
#endif
		if (discover_server && slimproto_discover(slimserver_address,
			sizeof(slimserver_address), port, &json_port, false) < 0)
		{
			fprintf(stderr,"Discover failed.\n");
			if (!retry_connection) {
				exit_code = -1;
				goto exit;
			}
			signal_restart_flag = true;
			continue;
		}

		if (slimproto_connect(
			&slimproto, slimserver_address, port) < 0) {
			fprintf(stderr, "Connection to Squeezebox Server %s failed.\n", slimserver_address);
			if (!retry_connection) {
				exit_code = -1;
				goto exit;
			}
			signal_restart_flag = true;
			continue;
		}
		signal_restart_flag = false;
		discover_server = false;
#ifdef EMPEG
		strcpy((char *)empeg_state.last_server, slimserver_address);
		if (power_last)
			while (!empeg_state.power_on)
			{
				Pa_Sleep(100);
				slimproto_ir(&slimproto, 1, 1, 0x76898F70);
			}
#endif
                while (!signal_exit_flag && !signal_restart_flag) {
#ifdef EMPEG
		   int rc = empeg_idle();
		   if (power_bypass)
		   {
		      if (rc == 0 || !empeg_state.power_on)
		      {
		         power_last = false;
		         power_bypass = false;
		      }
		   }
		   else if (rc == -1)
		   {
		      fprintf(stderr, "Power loss detected.\n");
		      power_last = empeg_state.power_on;
                      slimproto_ir(&slimproto, 1, 1, 0x76898778);
		      while (empeg_state.power_on)
		         Pa_Sleep(250);
		   }
		   else if (rc == -2 && empeg_state.power_on)
		   {
		      fprintf(stderr, "Manual override, aborting power down.\n");
		      power_bypass = true;
		   }
		   else if (rc == -3)
		   {
		      fprintf(stderr, "Power restored.\n");
		      if (power_last)
                         slimproto_ir(&slimproto, 1, 1, 0x76898F70);
		   }
		   else if (rc == -4)
		   {
		      fprintf(stderr, "Powering down.\n");
		      slimproto_goodbye(&slimproto, 0x00);
		      Pa_Sleep(400);
		      slimproto_close(&slimproto);
		      empeg_state.power_on = power_last;
		      empeg_poweroff();
		      signal_restart_flag = true;
		   }
#endif
#ifdef INTERACTIVE
                   if (using_curses == 1 || use_lcdd_menu || using_lirc) {
                      FD_ZERO(&read_fds);
                      FD_ZERO(&write_fds);
		      if (using_curses == 1)
     	                 FD_SET(0, &read_fds); /* watch stdin */
   	              if  (use_lcdd_menu) {
		         FD_SET(lcd_fd, &read_fds); 
		         maxfd = lcd_fd;
		      }
   	              if (using_lirc) {
		         FD_SET(lirc_fd, &read_fds); 
                         if (lirc_fd > maxfd) 
		            maxfd = lirc_fd;
                      }
		      timeout.tv_sec = 5;
                      if(select(maxfd + 1, &read_fds, NULL, NULL, &timeout) == -1) {
#ifndef __WIN32__
    	                 if (errno != EINTR)
			 {
		           fprintf(stderr,"Select Error:%d\n", errno);
#else
			 WSAerrno = WSAGetLastError();
			 if ( (WSAerrno != WSAEINTR) && (WSAerrno != WSAENOTSOCK) )
			 {	
		           fprintf(stderr,"Select Error:%d\n", WSAerrno);
#endif
   	                   abort();
	                 }
#ifdef __WIN32__
			 else
				 WaitForSingleObject( GetStdHandle(STD_INPUT_HANDLE), 5000 );
#endif
                      }
		      if (FD_ISSET(0, &read_fds)) {
                         while ((key = getch()) != ERR) {
                            ir = getircode(key);
	                    if (ir == (unsigned long) 0x01) {
  		               signal_exit_flag = 1;
                            }else{
			       if (ir != 0) slimproto_ir(&slimproto, 1, 1, ir);
			    }
		         }
		      } 
		      if (using_lirc && FD_ISSET(lirc_fd, &read_fds)) {
                         while((key = read_lirc()) != 0 ) { 
                            ir = getircode(key);
	                    if (ir == 0x01) { 
  		               signal_exit_flag = 1;
                            } else {
			       if (ir != 0) slimproto_ir(&slimproto, 1, 1, ir);
			    }
		         }
		      } 
		      if (use_lcdd_menu && FD_ISSET(lcd_fd, &read_fds)) {
                         while(read_lcd()); 
		      }
		   } else {
                      wait_for_restart_signal();
		   }
#else
#ifdef EMPEG
                   while ((key = empeg_getkey()) != -1)
                   {
                      ir = empeg_getircode(key);
                      if (ir != 0)
                         slimproto_ir(&slimproto, 1, 1, ir);
                   }
#else
                   wait_for_restart_signal();
#endif
#endif
		}
#ifdef INTERACTIVE
                FD_ZERO(&read_fds);
                FD_ZERO(&write_fds);
#endif
        } while (signal_restart_flag && !signal_exit_flag);

exit:
	slimaudio_close(&slimaudio);

	slimproto_goodbye(&slimproto, 0x00);

	/* Wait 200ms for BYE! message send to complete */
	Pa_Sleep(200);

	slimproto_close(&slimproto);

#ifdef INTERACTIVE
	exitcurses();
	close_lirc();
#endif

#if defined(EMPEG) || defined(INTERACTIVE)
	close_lcd();
#endif

#ifdef SLIMPROTO_DEBUG
	if (debug_logfile)
	{
		fclose (debuglog);
	}
#endif

	slimproto_destroy(&slimproto);
	slimaudio_destroy(&slimaudio);

	return exit_code;
} 
Ejemplo n.º 9
0
int
main(int argc, char *argv[])
{
	int     i;

	if (pledge("stdio rpath tty exec", NULL) == -1)
		err(1, "pledge");

	signal(SIGINT, getout);
	initcurses();
	text(hello);
	text(list);
	i = text(contin);
	if (i == 0)
		i = 2;
	init();
	while (i)
		switch (i) {
		case 1:
			leave();	/* Does not return */
			break;

		case 2:
			if ((i = text(intro1)))
				break;
			wrboard();
			if ((i = text(intro2)))
				break;
		
		case 3:
			if ((i = text(moves)))
				break;
		
		case 4:
			if ((i = text(removepiece)))
				break;
		
		case 5:
			if ((i = text(hits)))
				break;
		
		case 6:
			if ((i = text(endgame)))
				break;
		
		case 7:
			if ((i = text(doubl)))
				break;
		
		case 8:
			if ((i = text(stragy)))
				break;
		
		case 9:
			if ((i = text(prog)))
				break;
		
		case 10:
			if ((i = text(lastch)))
				break;
		}
	tutor();
	/* NOT REACHED */
}
Ejemplo n.º 10
0
int main(int argc, char *argv[]) {
	slimproto_t slimproto;
	slimaudio_t slimaudio;

	PaDeviceIndex output_device_id = PA_DEFAULT_DEVICE;

	unsigned int output_predelay = 0;
	unsigned int output_predelay_amplitude = 0;

	slimaudio_volume_t volume_control = VOLUME_SOFTWARE;

	unsigned int retry_interval = RETRY_DEFAULT;

	char macaddress[6] = { 0, 0, 0, 0, 0, 1 };

	int keepalive_interval = -1;

	bool listdevs = false;

#ifdef DAEMONIZE
	bool should_daemonize = false;
	char *logfile = NULL;
#endif


#ifdef INTERACTIVE
        fd_set read_fds;
        fd_set write_fds;
        int key = 0;
        unsigned long ir = 0;
	int maxfd = 0;
	char *home;
	struct timeval timeout;
	timeout.tv_usec = 0;

#ifdef __WIN32__
	int WSAerrno;
	int ptw32_processInitialize (void);
	ptw32_processInitialize();
#endif
        // default lircrc file ($HOME/.lircrc)
	home = getenv("HOME");
	if (home == NULL) home = "";
	lircrc = (char *)malloc((strlen(home) + strlen("/.lircrc") + 1) * sizeof(char));
	strcpy(lircrc,home);
	strcat(lircrc,"/.lircrc");
#endif

	char getopt_options[OPTLEN] = "a:d:Y:e:f:hk:Lm:o:P:p:Rr:Vv:";
	static struct option long_options[] = {
		{"predelay_amplitude", required_argument, 0, 'a'},
		{"debug",              required_argument, 0, 'd'},
		{"debuglog",           required_argument, 0, 'Y'},
		{"help",               no_argument,       0, 'h'},
		{"keepalive",          required_argument, 0, 'k'},
		{"list",               no_argument,       0, 'L'},
		{"mac",	               required_argument, 0, 'm'},
		{"output",             required_argument, 0, 'o'},
		{"playerid",           required_argument, 0, 'e'},
		{"firmware",           required_argument, 0, 'f'},
		{"port",               required_argument, 0, 'P'},
		{"predelay",           required_argument, 0, 'p'},
		{"retry",              no_argument,       0, 'R'},
		{"intretry",           required_argument, 0, 'r'},
		{"version",            no_argument,       0, 'V'},
		{"volume",             required_argument, 0, 'v'},
#ifdef PORTAUDIO_DEV
		{"latency",            required_argument, 0, 'y'},
#endif
#ifdef DAEMONIZE
		{"daemonize",          required_argument, 0, 'M'},
#endif
#ifdef __WIN32__
		{"highpriority",       no_argument,       0, 'H'},
#ifdef PA_WASAPI
		{"shared",             no_argument,       0, 'S'},
#endif
#endif
#ifdef INTERACTIVE
		{"lircrc",             required_argument, 0, 'c'},
		{"lirc",               no_argument,       0, 'i'},
		{"lcd",                no_argument,       0, 'l'},
		{"display",            no_argument,       0, 'D'},
		{"width",              required_argument, 0, 'w'},
#endif
		{0, 0, 0, 0}
	};
#ifdef PORTAUDIO_DEV
	strcat (getopt_options, "y:");
#endif	
#ifdef DAEMONIZE	
	strcat (getopt_options, "M:");
#endif
#ifdef INTERACTIVE
	strcat (getopt_options, "c:Dilw:");
#endif
#ifdef __WIN32__
	strcat (getopt_options, "H");
#ifdef PA_WASAPI
	strcat (getopt_options, "S");
#endif
#endif
	while (true) {
		const char shortopt =
			getopt_long_only(argc, argv, getopt_options, long_options, NULL);

		if (shortopt == (char) -1) {
			break;
		}

		switch (shortopt) {
		case 'a':
			output_predelay_amplitude = strtoul(optarg, NULL, 0);
			break;
		case 'd':
#ifdef SLIMPROTO_DEBUG
			if (strcmp(optarg, "all") == 0)
			{
				slimproto_debug = true;
				slimaudio_debug = true;
				slimaudio_buffer_debug = true;
				slimaudio_buffer_debug_v = true;
				slimaudio_decoder_debug = true;
				slimaudio_decoder_debug_r = true;
				slimaudio_decoder_debug_v = true;
				slimaudio_http_debug = true;
				slimaudio_http_debug_v = true;
				slimaudio_output_debug = true;
				slimaudio_output_debug_v = true;
			}
			else if (strcmp(optarg, "slimproto") == 0)
				slimproto_debug = true;
			else if (strcmp(optarg, "slimaudio") == 0)
				slimaudio_debug = true;
			else if (strcmp(optarg, "slimaudio_buffer") == 0)
				slimaudio_buffer_debug = true;
			else if (strcmp(optarg, "slimaudio_buffer_v") == 0)
				slimaudio_buffer_debug_v = true;
			else if (strcmp(optarg, "slimaudio_decoder") == 0)
				slimaudio_decoder_debug = true;
			else if (strcmp(optarg, "slimaudio_decoder_r") == 0)
				slimaudio_decoder_debug_r = true;
			else if (strcmp(optarg, "slimaudio_decoder_v") == 0)
				slimaudio_decoder_debug_v = true;
			else if (strcmp(optarg, "slimaudio_http") == 0)
				slimaudio_http_debug = true;
			else if (strcmp(optarg, "slimaudio_http_v") == 0)
				slimaudio_http_debug_v = true;
			else if (strcmp(optarg, "slimaudio_output") == 0)
				slimaudio_output_debug = true;
			else if (strcmp(optarg, "slimaudio_output_v") == 0)
				slimaudio_output_debug_v = true;
			else
				fprintf(stderr, "%s: Unknown debug option %s\n", argv[0], optarg);
#else
				fprintf(stderr, "%s: Recompile with -DSLIMPROTO_DEBUG to enable debugging.\n", argv[0]);
#endif
			break;
		case 'Y':
#ifdef SLIMPROTO_DEBUG
                        if ( optarg == NULL )
                        {
                                fprintf(stderr, "%s: Cannot parse debug log filename %s\n", argv[0], optarg);
                                exit(-1);
                        } else
                        {
				debuglog = freopen( optarg, "a", stderr);
				if ( debuglog )
					debug_logfile = true;
				else
					fprintf(stderr, "%s: Redirection of stderr to %s failed.\n", argv[0], optarg);
                        }
#endif
			break;

// From server/Slim/Networking/Slimproto.pm from 7.5r28596
// squeezebox(2)
// softsqueeze(3)
// squeezebox2(4)
// transporter(5)
// softsqueeze3(6)
// receiver(7)
// squeezeslave(8)
// controller(9)
// boom(10)
// softboom(11)
// squeezeplay(12)
// radio(13)
// touch(14)

		case 'e':
			player_type = strtoul(optarg, NULL, 0);
			if ( (player_type < 2) || (player_type > 14) )
			{
				player_type = PLAYER_TYPE;
				fprintf(stderr, "%s: Unknown player type, using (%d)\n", argv[0], player_type);
			}
			break;
		case 'f':
			firmware = strtoul(optarg, NULL, 0);
			if ( (firmware < 0) || (firmware > 254) )
			{
				firmware = FIRMWARE_VERSION;
				fprintf(stderr, "%s: Invalid firmware value, using (%d)\n", argv[0], firmware);
			}
			break;
		case 'h':
			print_help();
			exit(0);	
		case 'k':
			keepalive_interval = strtoul(optarg, NULL, 0);
			break;
		case 'm':
			if (parse_macaddress(macaddress, optarg) != 0) {
				fprintf(stderr, "%s: Cannot parse mac address %s\n", argv[0], optarg);
				exit(-1);	
			}
			break;
#ifdef DAEMONIZE
		case 'M':
			if ( optarg == NULL )
			{
				fprintf(stderr, "%s: Cannot parse log filename %s\n", argv[0], optarg);
				exit(-1);	
			} else
			{
				logfile = optarg;
			}
			should_daemonize = true;
			break;
#endif

#ifdef __WIN32__
		case 'H':
			/* Change Window process priority class to HIGH */
			if ( !SetPriorityClass ( GetCurrentProcess(), HIGH_PRIORITY_CLASS ) )
			{
				int dwError = GetLastError();
				fprintf(stderr, "%s: Failed to set priority (%d), using default.\n", argv[0],
					dwError);
			} 
			break;
#ifdef PA_WASAPI
		case 'S':
			wasapi_exclusive = false;
			break;
#endif
#endif
		case 'o':
			output_device_id = strtoul(optarg, NULL, 0);
			output_change = true;
			break;
		case 'p':
			output_predelay = strtoul(optarg, NULL, 0);
			break;
		case 'P':
			port = strtoul(optarg, NULL, 0);
			if ( (port < 0) || (port > 65535) )
			{
				port = SLIMPROTOCOL_PORT;
				fprintf(stderr, "%s: Invalid port value, using (%d)\n", argv[0], port);
			}
			break;
			break;
		case 'R':
			retry_connection = true;
			break;
		case 'r':
			retry_connection = true;
			retry_interval = strtoul(optarg, NULL, 0);
			if ( retry_interval < 1 )
			{
				fprintf (stderr, "Retry option requires value in seconds.\n");
				exit(-1);
			}
			break;
#ifdef INTERACTIVE
		case 'c':
		        free(lircrc);
			lircrc = optarg;
			break;
#ifndef __WIN32__
		case 'i':
			using_lirc = true;
			break;
		case 'l':
			use_lcdd_menu = true;
			break;
#endif
		case 'D':
			using_curses = 1;
			break;
		case 'w':
			linelen = strtoul(optarg, NULL, 0);
			break;
#endif
		case 'L':
			listdevs = true;
			break;
		case 'V':
			print_version();
			exit(0);
			break;
		case 'v':
		 	if (strcmp(optarg, "sw") == 0) {
				volume_control = VOLUME_SOFTWARE;
			}
#ifndef PORTAUDIO_DEV
			else if (strcmp(optarg, "on") == 0 ) {
				volume_control = VOLUME_DRIVER;
			}
#endif
			else if (strcmp(optarg, "off") == 0 ) {
				volume_control = VOLUME_NONE;
			}
			break;
#ifdef PORTAUDIO_DEV
		case 'y':
			modify_latency = true;
			user_latency = strtoul(optarg, NULL, 0);

			if ( user_latency > 1000 )
			{
				fprintf (stderr, "Suggested latency invalid, using device default.\n");
				modify_latency = false;
			}
			break;
#endif
		default:
			break;
		}
	}

	if (listdevs) {
		GetAudioDevices(output_device_id, output_change, true);
		exit(0);
	}

#ifdef DAEMONIZE
	if ( should_daemonize ) {
#ifdef INTERACTIVE
		if ( using_curses || using_lirc || use_lcdd_menu )
		{
			fprintf(stderr, "Daemonize not supported with lirc or display modes.\n");
			exit(-1);
		}
		else
#endif
			init_daemonize();
	}
#endif

	char *slimserver_address = "127.0.0.1";
	if (optind < argc)
		slimserver_address = argv[optind];

	signal(SIGTERM, &exit_handler);
	install_restart_handler();

#ifdef INTERACTIVE
	install_toggle_handler();  //SIGUSR2 to toggle IR/LCD on and off
#endif
	if (slimproto_init(&slimproto) < 0) {
		fprintf(stderr, "Failed to initialize slimproto\n");
		exit(-1);	
	}

	if (slimaudio_init(&slimaudio, &slimproto, output_device_id, output_change) < 0) {
		fprintf(stderr, "Failed to initialize slimaudio\n");
		exit(-1);
	}

	slimproto_add_connect_callback(&slimproto, connect_callback, macaddress);

#ifdef INTERACTIVE
	// Process VFD (display) commands
	if ( using_curses || using_lirc || use_lcdd_menu )
		slimproto_add_command_callback(&slimproto, "vfdc", vfd_callback, macaddress);
#endif

	slimaudio_set_volume_control(&slimaudio, volume_control);
	slimaudio_set_output_predelay(&slimaudio, output_predelay, output_predelay_amplitude);

	if (keepalive_interval >= 0) {
		slimaudio_set_keepalive_interval(&slimaudio, keepalive_interval);
	}

#ifdef INTERACTIVE
	init_lcd();
#endif

	if (slimaudio_open(&slimaudio) < 0) {
		fprintf(stderr, "Failed to open slimaudio\n");
#ifdef INTERACTIVE
		close (lcd_fd);
#endif
		exit(-1);
	}

#ifdef SLIMPROTO_DEBUG
	if (slimaudio_debug)
		fprintf ( stderr, "Using audio device index: %d\n", slimaudio.output_device_id );
#endif

#ifdef INTERACTIVE
        init_lirc();

	setlocale(LC_ALL, "");
	initcurses();
#endif
#ifdef DAEMONIZE
	if ( should_daemonize ) {
		daemonize(logfile);
	}
#endif
	// When retry_connection is true, retry connecting to Squeezebox Server 
	// until we succeed, unless the signal handler tells us to give up.
	do {
		while (slimproto_connect(
			&slimproto, slimserver_address, port) < 0) {
			if (!retry_connection || signal_exit_flag) {
				if (signal_exit_flag) {
					// No message when the exit is triggered
					// by the user.
#ifdef INTERACTIVE
					exitcurses();
					close_lirc();
					close_lcd();
#endif
					exit(0);
				}
#ifdef INTERACTIVE
				exitcurses();
				close_lirc();
				close_lcd();
#endif
				fprintf(stderr, "Connection to Squeezebox Server %s failed.\n", slimserver_address);
				exit(-1);
			}
#ifdef INTERACTIVE
			exitcurses();
#endif
			fprintf(stderr,"Retry in %d seconds.\n", retry_interval);
			Pa_Sleep(1000 * retry_interval);
#ifdef INTERACTIVE
	   	        initcurses();
#endif
		}
                signal_restart_flag = false;
                while (!signal_exit_flag && !signal_restart_flag) {
#ifdef INTERACTIVE
                   if (using_curses == 1 || use_lcdd_menu) {
                      FD_ZERO(&read_fds);
                      FD_ZERO(&write_fds);
		      if (using_curses == 1)
     	                 FD_SET(0, &read_fds); /* watch stdin */
   	              if (use_lcdd_menu) {
		         FD_SET(lcd_fd, &read_fds); 
		         maxfd = lcd_fd;
		      }
   	              if (using_lirc) {
		         FD_SET(lirc_fd, &read_fds); 
                         if (lirc_fd > maxfd) 
		            maxfd = lirc_fd;
                      }
		      timeout.tv_sec = 5;
                      if(select(maxfd + 1, &read_fds, NULL, NULL, &timeout) == -1) {
#ifndef __WIN32__
    	                 if (errno != EINTR)
			 {
		           fprintf(stderr,"Select Error:%d\n", errno);
#else
			 WSAerrno = WSAGetLastError();
			 if ( (WSAerrno != WSAEINTR) && (WSAerrno != WSAENOTSOCK) )
			 {	
		           fprintf(stderr,"Select Error:%d\n", WSAerrno);
#endif
   	                   abort();
	                 }
#ifdef __WIN32__
			 else
				 WaitForSingleObject( GetStdHandle(STD_INPUT_HANDLE), 5000 );
#endif
                      }
		      if (FD_ISSET(0, &read_fds)) {
                         while ((key = getch()) != ERR) {
                            ir = getircode(key);
	                    if (ir == (unsigned long) 0x01) {
  		               signal_exit_flag = 1;
                            }else{
			       if (ir != 0) slimproto_ir(&slimproto, 1, 1, ir);
			    }
		         }
		      } 
		      if (using_lirc && FD_ISSET(lirc_fd, &read_fds)) {
                         while((key = read_lirc()) != 0 ) { 
                            ir = getircode(key);
	                    if (ir == 0x01) { 
  		               signal_exit_flag = 1;
                            } else {
			       if (ir != 0) slimproto_ir(&slimproto, 1, 1, ir);
			    }
		         }
		      } 
		      if (use_lcdd_menu && FD_ISSET(lcd_fd, &read_fds)) {
                         while(read_lcd()); 
		      }
		   } else {
                      wait_for_restart_signal();
		   }
#else
                   wait_for_restart_signal();
#endif
		}
#ifdef INTERACTIVE
                FD_ZERO(&read_fds);
                FD_ZERO(&write_fds);
#endif
		if (signal_restart_flag) { 
#ifdef INTERACTIVE
			exitcurses();
#endif
			fprintf(stderr,"Retry in %d seconds.\n", retry_interval);
			Pa_Sleep(1000 * retry_interval);
#ifdef INTERACTIVE
	   	        initcurses();
#endif
		}
        } while (signal_restart_flag && !signal_exit_flag);

#ifdef INTERACTIVE
	close_lirc();
#endif
	slimaudio_close(&slimaudio);
	slimproto_close(&slimproto);

#ifdef INTERACTIVE
        exitcurses();
        close_lcd();
#endif
#ifdef SLIMPROTO_DEBUG
	if (debug_logfile)
	{
		fclose (debuglog);
	}
#endif
	slimaudio_destroy(&slimaudio);
	slimproto_destroy(&slimproto);
	return 0;
} 
Ejemplo n.º 11
0
void
browse(const char *ipath, const char *ifilter)
{
	int r, fd;
	regex_t re;
	char *newpath;
	struct stat sb;
	char *name, *bin, *dir, *tmp, *run, *env;
	int nowtyping = 0;
    FILE *fp;

	oldpath = NULL;
	path = xstrdup(ipath);
	fltr = xstrdup(ifilter);
begin:
	/* Path and filter should be malloc(3)-ed strings at all times */
	r = populate();
	if (r == -1) {
		if (!nowtyping) {
			printwarn();
			goto nochange;
		}
	}

	for (;;) {
		redraw();

		/* Handle filter-as-you-type mode */
		if (nowtyping)
			goto moretyping;
nochange:
		switch (nextsel(&run, &env)) {
		case SEL_QUIT:
			free(path);
			free(fltr);
			dentfree(dents, n);
			return;
		case SEL_BACK:
			/* There is no going back */
			if (strcmp(path, "/") == 0 ||
			    strcmp(path, ".") == 0 ||
			    strchr(path, '/') == NULL)
				goto nochange;
			dir = xdirname(path);
			if (canopendir(dir) == 0) {
				free(dir);
				printwarn();
				goto nochange;
			}
			/* Save history */
			oldpath = path;
			path = dir;
			/* Reset filter */
			free(fltr);
			fltr = xstrdup(ifilter);
			goto begin;
		case SEL_GOIN:
			/* Cannot descend in empty directories */
			if (n == 0)
				goto nochange;

			name = dents[cur].name;
			newpath = mkpath(path, name);
			DPRINTF_S(newpath);

			/* Get path info */
			fd = open(newpath, O_RDONLY | O_NONBLOCK);
			if (fd == -1) {
				printwarn();
				free(newpath);
				goto nochange;
			}
			r = fstat(fd, &sb);
			if (r == -1) {
				printwarn();
				close(fd);
				free(newpath);
				goto nochange;
			}
			close(fd);
			DPRINTF_U(sb.st_mode);

			switch (sb.st_mode & S_IFMT) {
			case S_IFDIR:
				if (canopendir(newpath) == 0) {
					printwarn();
					free(newpath);
					goto nochange;
				}
				free(path);
				path = newpath;
				/* Reset filter */
				free(fltr);
				fltr = xstrdup(ifilter);
				goto begin;
			case S_IFREG:
				bin = openwith(newpath);
				if (bin == NULL) {
					printmsg("No association");
					free(newpath);
					goto nochange;
				}
				exitcurses();
				spawn(bin, newpath, NULL);
				initcurses();
				free(newpath);
				continue;
			default:
				printmsg("Unsupported file");
				goto nochange;
			}
		case SEL_FLTR:
			/* Read filter */
			printprompt("filter: ");
			tmp = readln();
			if (tmp == NULL)
				tmp = xstrdup(ifilter);
			/* Check and report regex errors */
			r = setfilter(&re, tmp);
			if (r != 0) {
				free(tmp);
				goto nochange;
			}
			free(fltr);
			fltr = tmp;
			DPRINTF_S(fltr);
			/* Save current */
			if (n > 0)
				oldpath = mkpath(path, dents[cur].name);
			goto begin;
		case SEL_TYPE:
			nowtyping = 1;
			tmp = NULL;
moretyping:
			printprompt("type: ");
			if (tmp != NULL)
				printw("%s", tmp);
			r = readmore(&tmp);
			DPRINTF_D(r);
			DPRINTF_S(tmp);
			if (r == 1)
				nowtyping = 0;
			/* Check regex errors */
			if (tmp != NULL) {
				r = setfilter(&re, tmp);
				if (r != 0)
					if (nowtyping) {
						goto moretyping;
					} else {
						free(tmp);
						goto nochange;
					}
			}
			/* Copy or reset filter */
			free(fltr);
			if (tmp != NULL)
				fltr = xstrdup(tmp);
			else
				fltr = xstrdup(ifilter);
			/* Save current */
			if (n > 0)
				oldpath = mkpath(path, dents[cur].name);
			if (!nowtyping)
				free(tmp);
			goto begin;
		case SEL_NEXT:
			if (cur < n - 1)
				cur++;
			break;
		case SEL_PREV:
			if (cur > 0)
				cur--;
			break;
		case SEL_PGDN:
			if (cur < n - 1)
				cur += MIN((LINES - 4) / 2, n - 1 - cur);
			break;
		case SEL_PGUP:
			if (cur > 0)
				cur -= MIN((LINES - 4) / 2, cur);
			break;
		case SEL_HOME:
			cur = 0;
			break;
		case SEL_END:
			cur = n - 1;
			break;
		case SEL_CD:
			/* Read target dir */
			printprompt("chdir: ");
			tmp = readln();
			if (tmp == NULL) {
				clearprompt();
				goto nochange;
			}
			newpath = mkpath(path, tmp);
			free(tmp);
			if (canopendir(newpath) == 0) {
				free(newpath);
				printwarn();
				goto nochange;
			}
			free(path);
			path = newpath;
			free(fltr);
			fltr = xstrdup(ifilter); /* Reset filter */
			DPRINTF_S(path);
			goto begin;
		case SEL_MTIME:
			mtimeorder = !mtimeorder;
			/* Save current */
			if (n > 0)
				oldpath = mkpath(path, dents[cur].name);
			goto begin;
		case SEL_REDRAW:
			/* Save current */
			if (n > 0)
				oldpath = mkpath(path, dents[cur].name);
			goto begin;
		case SEL_RUN:
			run = xgetenv(env, run);
			exitcurses();
			spawn(run, NULL, path);
			initcurses();
			break;
		case SEL_RUNARG:
			name = dents[cur].name;
			run = xgetenv(env, run);
			exitcurses();
			spawn(run, name, path);
			initcurses();
			break;
        case SEL_PRINT:
			name = dents[cur].name;
            fp = fopen("savelist.txt", "a");
            fprintf(fp, "%s/%s\n", path, name);
            fclose(fp);
            break;
		}
		/* Screensaver */
		if (idletimeout != 0 && idle == idletimeout) {
			idle = 0;
			exitcurses();
			spawn(idlecmd, NULL, NULL);
			initcurses();
		}
	}
}
Ejemplo n.º 12
0
t_sshort GrphDisp_Init(t_uchar Col, /* total column size of display                */
                   t_uchar Row, /* total row size of display                   */
                   t_uchar MaxWindow/* approx. windows that can be open        */
                             /* (assuming window is 1/4 of the total        */
                             /* display. Nevertheless windows can be opened */
                             /* as long as there is room on the stack.      */
)
{

  ColMajor = Col;
  RowMajor = Row;
  BaseBit = 0;               /* used in pixel macro                         */
  TmpBit = 0;                /* used in pixel macro                         */
  StackIndex = 0;            /* stack is empty 1/4 of the maximum window *  */
                             /* the amount of windows this is the total     */
                             /* amount of space how you use it is up to you */

  #if defined(FEDORA_VERSION)
    /* Init Curses */
    initcurses();
  #endif /* MAEL Version */
  
  StackSize = (ColMajor*RowMajor*2)*MaxWindow;

  Vdisplay = (char *)Mem_Alloc(ColMajor*RowMajor*2+256);
  if ((t_uchar *)Vdisplay == 0)
  {
    /* Memory error */
    printf ("Error Grphdisp: memoria insuficiente\n");
    printf ("Press any key to halt:");
    #if defined(FEDORA_VERSION)
    mael_getch();
    #else
    getch ();
    #endif
    exit (1);
    return (NOSDOS_FAILURE);
  }                          /* StackData = (t_uchar                           */
                             /* *)Memory_Alloc(StackSize,MA_ALLOC);         */
  StackData = (t_uchar *)Mem_Alloc(StackSize);
  if (StackData == 0)
  {
    /* Memory error */
    printf ("Error Grphdisp: memoria insuficiente\n");
    printf ("Press any key to halt:");
    #if defined(FEDORA_VERSION)
    mael_getch();
    #else
    getch ();
    #endif
    exit (1);
    return (NOSDOS_FAILURE);
  }
  memset(StackData, 0, StackSize);
  memset(Vdisplay, 0, ColMajor*RowMajor*2);

  /* Clears entire screen */
  clrscr ();

#if !defined(FEDORA_VERSION)
{

  t_uchar IdxLine, IdxCol;

  /* Makes frame */
  gotoxy (DISP_INICOL-1, DISP_INILINE-1);
  
  /*cprintf ("%c", FRAME_LEFTH); */
  PrintChar ((char) FRAME_LEFTH);
  
  for (IdxCol = 0; IdxCol < ColMajor; IdxCol++)
    /*cprintf ("%c", FRAME_HOR); */
    PrintChar ((char) FRAME_HOR);
  /*cprintf ("%c", FRAME_RIGHTH); */
  PrintChar ((char) FRAME_RIGHTH);
  for (IdxLine = 0; IdxLine < RowMajor; IdxLine++)
  {
    gotoxy (DISP_INICOL-1, DISP_INILINE+IdxLine);
    /*cprintf ("%c", FRAME_VER); */
    PrintChar ((char) FRAME_VER);
    gotoxy (DISP_INICOL+ColMajor, DISP_INILINE+IdxLine);
    /*cprintf ("%c", FRAME_VER); */
    PrintChar ((char) FRAME_VER);
  } /* endfor */
  gotoxy (DISP_INICOL-1, DISP_INILINE+IdxLine);
  /*cprintf ("%c", FRAME_LEFTL); */
  PrintChar ((char) FRAME_LEFTL);
  for (IdxCol = 0; IdxCol < ColMajor; IdxCol++)
    /*cprintf ("%c", FRAME_HOR); */
    PrintChar ((char) FRAME_HOR);
  /*cprintf ("%c", FRAME_RIGHTL); */
  PrintChar ((char) FRAME_RIGHTL);
}
#endif /* MAEL Version */

  GrphDisp_DispBuffer((t_uchar *) Vdisplay, 0, 0, (t_uchar) (ColMajor-1), (t_uchar) (RowMajor-1));
  return (NOSDOS_OK);
}