Exemple #1
0
/* Setup the IO for curses or non-curses mode.
   - In non-curses mode, readline and gdb use the standard input and
   standard output/error directly.
   - In curses mode, the standard output/error is controlled by TUI
   with the tui_stdout and tui_stderr.  The output is redirected in
   the curses command window.  Several readline callbacks are installed
   so that readline asks for its input to the curses command window
   with wgetch().  */
void
tui_setup_io(int mode)
{
  if (mode)
    {
      /* Redirect readline to TUI: */
      tui_old_rl_redisplay_function = rl_redisplay_function;
      tui_old_rl_deprep_terminal = rl_deprep_term_function;
      tui_old_rl_prep_terminal = (VFunction *)rl_prep_term_function;
      tui_old_rl_getc_function = (Function *)rl_getc_function;
      tui_old_rl_outstream = rl_outstream;
      tui_old_readline_echoing_p = readline_echoing_p;
      rl_redisplay_function = tui_redisplay_readline;
      rl_deprep_term_function = tui_deprep_terminal;
      rl_prep_term_function = tui_prep_terminal;
      rl_getc_function = tui_getc;
      readline_echoing_p = 0;
      rl_outstream = tui_rl_outstream;
      rl_prompt = 0;
      rl_completion_display_matches_hook = tui_rl_display_match_list;
      rl_already_prompted = 0;

      /* Keep track of previous gdb output: */
      tui_old_stdout = gdb_stdout;
      tui_old_stderr = gdb_stderr;
      tui_old_uiout = uiout;

      /* Reconfigure gdb output: */
      gdb_stdout = tui_stdout;
      gdb_stderr = tui_stderr;
      gdb_stdlog = gdb_stdout;/* for moment */
      gdb_stdtarg = gdb_stderr;	/* for moment */
      uiout = tui_out;

      /* Save tty for SIGCONT: */
      savetty();
    }
  else
    {
      /* Restore gdb output: */
      gdb_stdout = tui_old_stdout;
      gdb_stderr = tui_old_stderr;
      gdb_stdlog = gdb_stdout; /* for moment */
      gdb_stdtarg = gdb_stderr;	/* for moment */
      uiout = tui_old_uiout;

      /* Restore readline: */
      rl_redisplay_function = tui_old_rl_redisplay_function;
      rl_deprep_term_function = tui_old_rl_deprep_terminal;
      rl_prep_term_function = (rl_vintfunc_t *)tui_old_rl_prep_terminal;
      rl_getc_function = (rl_getc_func_t *)tui_old_rl_getc_function;
      rl_outstream = tui_old_rl_outstream;
      rl_completion_display_matches_hook = 0;
      readline_echoing_p = tui_old_readline_echoing_p;
      rl_already_prompted = 0;

      /* Save tty for SIGCONT: */
      savetty();
    }
}
Exemple #2
0
void
term_init()
{
    savetty();				/* remember current tty state */

#ifdef I_TERMIO
    outspeed = _tty.c_cflag & CBAUD;	/* for tputs() */
    ERASECH = _tty.c_cc[VERASE];	/* for finish_command() */
    KILLCH = _tty.c_cc[VKILL];		/* for finish_command() */
    if (GT = ((_tty.c_oflag & TABDLY) != TAB3))
	/* we have tabs, so that's OK */;
    else
	_tty.c_oflag &= ~TAB3;	/* turn off kernel tabbing -- done in rn */
#else /* !I_TERMIO */
# ifdef I_TERMIOS
    outspeed = cfgetospeed(&_tty);	/* for tputs() (output) */
    ERASECH = _tty.c_cc[VERASE];	/* for finish_command() */
    KILLCH = _tty.c_cc[VKILL];		/* for finish_command() */
/*    _tty.c_oflag &= ~OXTABS;		/* turn off kernel tabbing-done in rn */
# else /* !I_TERMIOS */
    outspeed = _tty.sg_ospeed;		/* for tputs() */
    ERASECH = _tty.sg_erase;		/* for finish_command() */
    KILLCH = _tty.sg_kill;		/* for finish_command() */
    if (GT = ((_tty.sg_flags & XTABS) != XTABS))
	/* we have tabs, so that's OK */;
    else
	_tty.sg_flags &= ~XTABS;
# endif /* I_TERMIOS */
#endif /* I_TERMIO */

    /* The following could be a table but I can't be sure that there isn't */
    /* some degree of sparsity out there in the world. */

    switch (outspeed) {			/* 1 second of padding */
#ifdef BEXTA
        case BEXTA:  just_a_sec = 1920; break;
#else
#ifdef B19200
        case B19200: just_a_sec = 1920; break;
#endif
#endif
        case B9600:  just_a_sec =  960; break;
        case B4800:  just_a_sec =  480; break;
        case B2400:  just_a_sec =  240; break;
        case B1800:  just_a_sec =  180; break;
        case B1200:  just_a_sec =  120; break;
        case B600:   just_a_sec =   60; break;
	case B300:   just_a_sec =   30; break;
	/* do I really have to type the rest of this??? */
        case B200:   just_a_sec =   20; break;
        case B150:   just_a_sec =   15; break;
        case B134:   just_a_sec =   13; break;
        case B110:   just_a_sec =   11; break;
        case B75:    just_a_sec =    8; break;
        case B50:    just_a_sec =    5; break;
        default:     just_a_sec =  960; break;
					/* if we are running detached I */
    }					/*  don't want to know about it! */
}
Exemple #3
0
void
cont_catcher()
{
    sigset(SIGCONT,cont_catcher);
    savetty();
    crmode();
    raw();
    noecho();
    nonl();
}
void use_console()
{
	 clear();
	 refresh();
	 savetty();
	 reset_shell_mode();
	 curs_set(2);
	 
	
}
Exemple #5
0
void do_shell(void)
{
    int ret;
    addch('\n');
    savetty();
#ifdef DJGPP
    ret = system("");
#else
    ret = system(shell);
#endif
    resetty();
}
Exemple #6
0
void StatFormBase::StartWin()
{
	if(m_bCursesOn)
		return;
	//忽略中止信号
	signal(SIGINT,SIG_IGN);
	initscr();
	noecho();
	intrflush(stdscr,true); 
	keypad(stdscr,TRUE);
	savetty();
	
	m_bCursesOn = true;
}
Exemple #7
0
void resizeTest(WINDOW *dummy)
{
    WINDOW *win1;
    int nwidth = 135, nheight = 52;
    int owidth = COLS, oheight = LINES;

    savetty();

    resize_term(nheight, nwidth);

    clear();
    refresh();

    win1 = newwin(10, 50, 14, 25);

    if (win1 == NULL)
    {
        endwin();
        return;
    }

#ifdef A_COLOR
    if (has_colors())
    {
        init_pair(3, COLOR_BLUE, COLOR_WHITE);
        wattrset(win1, COLOR_PAIR(3));
    }

    wclear(win1);
#endif
    mvwaddstr(win1, 0, 0, "The screen may now be resized");
    mvwprintw(win1, 1, 4, "Given size: %d by %d", nwidth, nheight);
    mvwprintw(win1, 2, 4, "Actual size: %d by %d", COLS, LINES);
    Continue(win1);

    wclear(win1);
    resetty();

    mvwaddstr(win1, 0, 0, "The screen should now be reset");
    mvwprintw(win1, 1, 6, "Old size: %d by %d", owidth, oheight);
    mvwprintw(win1, 2, 6, "Size now: %d by %d", COLS, LINES);
    Continue(win1);

    delwin(win1);

    clear();
    refresh();
}
Exemple #8
0
/*  Initializes PDCurses and game startup */
int initGame(int argc, char *argv[]) {
    int c;

    srand (time(NULL));
	initscr();
    raw();
    nodelay(stdscr,1);
    noecho();
    curs_set(0);
    nonl();

	if (has_colors()) start_color();

    for (c = 0; c < COLORS; c++) {
        if (c == COLOR_WHITE) init_pair(c,COLOR_BLACK,c);
        else init_pair(c,COLOR_WHITE,c);
    }

    /* Special color pair used for drawing player */
    init_pair(10,COLOR_YELLOW,COLOR_BLACK);
    init_pair(11,COLOR_GREEN,COLOR_BLACK);

	width  = 80;
	height = 20;

    clear();
    refresh();

    /* Resize the terminal window */
    resize_term(height,width);

	win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2);
	helpwindow = newwin(0,0,0,0);
	stats = newwin(20,20,0,60); /* create the character stats window */
	encounterwin = newwin(3,19,10,20);

	if (win == NULL || stats == NULL || helpwindow == NULL) {
		endwin();
		return 1;
	}

    savetty();
    resize_term(0,0);
    game_state = GAME_MENU;
	return 0;
}
Exemple #9
0
static TACommandVerdict savetty_cmd(TAThread thread, TAInputStream stream)
{
    // Prepare
    int res;
    
    START_TARGET_OPERATION(thread);
    
    // Execute
    res = savetty();
    
    END_TARGET_OPERATION(thread);
    
    // Response
    writeInt(thread, res);
    sendResponse(thread);
    
    return taDefaultVerdict;
}
Exemple #10
0
void
gettmode()
{
	if (ioctl(_tty_ch, TIOCGETP, &_tty) < 0)
		return;
	savetty();
	if (ioctl(_tty_ch, TIOCSETP, &_tty) < 0)
		_tty.sg_flags = _res_flg;
	ospeed = _tty.sg_ospeed;
	_res_flg = _tty.sg_flags;
	GT = ((_tty.sg_flags & XTABS) == 0);
	NONL = ((_tty.sg_flags & CRMOD) == 0);
	_tty.sg_flags &= ~XTABS;
	ioctl(_tty_ch, TIOCSETP, &_tty);
# ifdef DEBUG
	fprintf(outf, "GETTMODE: GT = %s\n", GT ? "TRUE" : "FALSE");
	fprintf(outf, "GETTMODE: NONL = %s\n", NONL ? "TRUE" : "FALSE");
	fprintf(outf, "GETTMODE: ospeed = %d\n", ospeed);
# endif
}
Exemple #11
0
gettmode() {

	if (gtty(_tty_ch, &_tty) < 0)
		return;
	savetty();
	if (stty(_tty_ch, &_tty) < 0)
		_tty.sg_flags = _res_flg;
	ospeed = _tty.sg_ospeed;
	_res_flg = _tty.sg_flags;
	UPPERCASE = (_tty.sg_flags & LCASE) != 0;
	GT = ((_tty.sg_flags & XTABS) == 0);
	NONL = ((_tty.sg_flags & CRMOD) == 0);
	_tty.sg_flags &= ~XTABS;
	stty(_tty_ch, &_tty);
# ifdef DEBUG
	fprintf(outf, "GETTMODE: UPPERCASE = %s\n", UPPERCASE ? "TRUE":"FALSE");
	fprintf(outf, "GETTMODE: GT = %s\n", GT ? "TRUE" : "FALSE");
	fprintf(outf, "GETTMODE: NONL = %s\n", NONL ? "TRUE" : "FALSE");
	fprintf(outf, "GETTMODE: ospeed = %d\n", ospeed);
# endif
}
Exemple #12
0
void
intro()
{
char *tmpname;

    srand(time(0L));			/* Kick the random number generator */

    signal(SIGINT,uninitgame);
    if(signal(SIGQUIT,SIG_IGN) != SIG_IGN) signal(SIGQUIT,uninitgame);
#if 1
	/* for some bizzare reason, getlogin or cuserid cause havoc with the terminal */
	if ((tmpname = getlogin()) != NULL) {
		strcpy(name, tmpname);
	} else
#endif
	strcpy(name,dftname);
    name[0] = toupper(name[0]);

    initscr();
    savetty();
    nonl();
    cbreak();
    noecho();
    clear();
    mvaddstr(4,29,"Welcome to Battleship!");
    move(8,0);
PR("                                                  \\\n");
PR("                           \\                     \\ \\\n");
PR("                          \\ \\                   \\ \\ \\_____________\n");
PR("                         \\ \\ \\_____________      \\ \\/            |\n");
PR("                          \\ \\/             \\      \\/             |\n");
PR("                           \\/               \\_____/              |__\n");
PR("           ________________/                                       |\n");
PR("           \\  S.S. Penguin                                         |\n");
PR("            \\                                                     /\n");
PR("             \\___________________________________________________/\n");
    mvaddstr(20,27,"Hit any key to continue..."); refresh();
    getch();
}
Exemple #13
0
visual()
{
	int moveenemy();

	destroyed = 0;
	savetty();
	if(initscr() == ERR){
		puts("Whoops!  No more memory...");
		return(0);
	}
	oldsig = signal(SIGINT, succumb);
	crmode();
	noecho();
	screen();
	row = rnd(LINES-3) + 1;
	column = rnd(COLS-2) + 1;
	moveenemy();
	for (;;) {
		switch(getchar()){

			case 'h':
			case 'r':
				dc = -1;
				fuel--;
				break;

			case 'H':
			case 'R':
				dc = -5;
				fuel -= 10;
				break;

			case 'l':
				dc = 1;
				fuel--;
				break;

			case 'L':
				dc = 5;
				fuel -= 10;
				break;

			case 'j':
			case 'u':
				dr = 1;
				fuel--;
				break;

			case 'J':
			case 'U':
				dr = 5;
				fuel -= 10;
				break;

			case 'k':
			case 'd':
				dr = -1;
				fuel--;
				break;

			case 'K':
			case 'D':
				dr = -5;
				fuel -= 10;
				break;

			case '+':
				if (cross){
					cross = 0;
					notarget();
				}
				else
					cross = 1;
				break;

			case ' ':
			case 'f':
				if (torps){
					torps -= 2;
					blast();
					if (row == MIDR && column - MIDC < 2 && MIDC - column < 2){
						destroyed = 1;
						alarm(0);
					}
				}
				else
					mvaddstr(0,0,"*** Out of torpedoes. ***");
				break;

			case 'q':
				endfly();
				return(0);

			default:
				mvaddstr(0,26,"Commands = r,R,l,L,u,U,d,D,f,+,q");
				continue;

			case EOF:
				break;
		}
		if (destroyed){
			endfly();
			return(1);
		}
		if (clock <= 0){
			endfly();
			die();
		}
	}
}
Exemple #14
0
int main(int argc, char *argv[]) {
    int i, y, z, optchr, keypress;
    int j = 0;
    int count = 0;
    int screensaver = 0;
    int asynch = 0;
    int bold = 0;
    int force = 0;
    int firstcoldone = 0;
    int oldstyle = 0;
    int random = 0;
    int update = 4;
    int highnum = 0;
    int mcolor = COLOR_GREEN;
    int rainbow = 0;
    int lambda = 0;
    int randnum = 0;
    int randmin = 0;
    int pause = 0;
    int classic = 0;

    srand((unsigned) time(NULL));
    setlocale(LC_ALL, "");

    /* Many thanks to morph- ([email protected]) for this getopt patch */
    opterr = 0;
    while ((optchr = getopt(argc, argv, "abBcfhlLnrosmxVu:C:")) != EOF) {
        switch (optchr) {
        case 's':
            screensaver = 1;
            break;
        case 'a':
            asynch = 1;
            break;
        case 'b':
            if (bold != 2) {
                bold = 1;
            }
            break;
        case 'B':
            bold = 2;
            break;
        case 'C':
            if (!strcasecmp(optarg, "green")) {
                mcolor = COLOR_GREEN;
            } else if (!strcasecmp(optarg, "red")) {
                mcolor = COLOR_RED;
            } else if (!strcasecmp(optarg, "blue")) {
                mcolor = COLOR_BLUE;
            } else if (!strcasecmp(optarg, "white")) {
                mcolor = COLOR_WHITE;
            } else if (!strcasecmp(optarg, "yellow")) {
                mcolor = COLOR_YELLOW;
            } else if (!strcasecmp(optarg, "cyan")) {
                mcolor = COLOR_CYAN;
            } else if (!strcasecmp(optarg, "magenta")) {
                mcolor = COLOR_MAGENTA;
            } else if (!strcasecmp(optarg, "black")) {
                mcolor = COLOR_BLACK;
            } else {
                c_die(" Invalid color selection\n Valid "
                       "colors are green, red, blue, "
                       "white, yellow, cyan, magenta " "and black.\n");
            }
            break;
        case 'c':
            classic = 1;
            break;
        case 'f':
            force = 1;
            break;
        case 'l':
            console = 1;
            break;
        case 'L':
            lock = 1;
            break;
        case 'n':
            bold = -1;
            break;
        case 'h':
        case '?':
            usage();
            exit(0);
        case 'o':
            oldstyle = 1;
            break;
        case 'u':
            update = atoi(optarg);
            break;
        case 'x':
            xwindow = 1;
            break;
        case 'V':
            version();
            exit(0);
        case 'r':
             rainbow = 1;
             break;
        case 'm':
             lambda = 1;
             break;
        }
    }

    if (force && strcmp("linux", getenv("TERM"))) {
        /* setenv is much more safe to use than putenv */
        setenv("TERM", "linux", 1);
    }
    initscr();
    savetty();
    nonl();
    cbreak();
    noecho();
    timeout(0);
    leaveok(stdscr, TRUE);
    curs_set(0);
    signal(SIGINT, sighandler);
    signal(SIGQUIT, sighandler);
    signal(SIGWINCH, sighandler);
    signal(SIGTSTP, sighandler);

if (console) {
#ifdef HAVE_CONSOLECHARS
        if (va_system("consolechars -f matrix") != 0) {
            c_die
                (" There was an error running consolechars. Please make sure the\n"
                 " consolechars program is in your $PATH.  Try running \"consolechars -f matrix\" by hand.\n");
        }
#elif defined(HAVE_SETFONT)
        if (va_system("setfont matrix") != 0) {
            c_die
                (" There was an error running setfont. Please make sure the\n"
                 " setfont program is in your $PATH.  Try running \"setfont matrix\" by hand.\n");
        }
#else
        c_die(" Unable to use both \"setfont\" and \"consolechars\".\n");
#endif
}
    if (has_colors()) {
        start_color();
        /* Add in colors, if available */
#ifdef HAVE_USE_DEFAULT_COLORS
        if (use_default_colors() != ERR) {
            init_pair(COLOR_BLACK, -1, -1);
            init_pair(COLOR_GREEN, COLOR_GREEN, -1);
            init_pair(COLOR_WHITE, COLOR_WHITE, -1);
            init_pair(COLOR_RED, COLOR_RED, -1);
            init_pair(COLOR_CYAN, COLOR_CYAN, -1);
            init_pair(COLOR_MAGENTA, COLOR_MAGENTA, -1);
            init_pair(COLOR_BLUE, COLOR_BLUE, -1);
            init_pair(COLOR_YELLOW, COLOR_YELLOW, -1);
        } else {
#else
        { /* Hack to deal the after effects of else in HAVE_USE_DEFAULT_COLOURS*/
#endif
            init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
            init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
            init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
            init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
            init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
            init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
            init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
            init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
        }
    }

    /* Set up values for random number generation */
    if(classic) {
        /* Japanese character unicode range [they are seen in the original cmatrix] */
        randmin = 12288;
        highnum = 12351;
    } else if (console || xwindow) {
        randmin = 166;
        highnum = 217;
    } else {
        randmin = 33;
        highnum = 123;
    }
    randnum = highnum - randmin;

    var_init();

    while (1) {
        /* Check for signals */
        if (signal_status == SIGINT || signal_status == SIGQUIT) {
            if(lock != 1)
                finish();
            /* exits */
        }
        if (signal_status == SIGWINCH) {
            resize_screen();
            signal_status = 0;
        }

        if(signal_status == SIGTSTP){
            if(lock != 1)
                    finish();
        }

        count++;
        if (count > 4) {
            count = 1;
        }

        if ((keypress = wgetch(stdscr)) != ERR) {
            if (screensaver == 1) {
#ifdef USE_TIOCSTI
                char *str = malloc(0);
                size_t str_len = 0;
                do {
                    str = realloc(str, str_len + 1);
                    str[str_len++] = keypress;
                } while ((keypress = wgetch(stdscr)) != ERR);
                size_t i;
                for (i = 0; i < str_len; i++)
                    ioctl(STDIN_FILENO, TIOCSTI, (char*)(str + i));
                free(str);
#endif
                finish();
            } else {
                switch (keypress) {
                case 'q':
                    if(lock != 1)
                        finish();
                    break;
                case 'a':
                    asynch = 1 - asynch;
                    break;
                case 'b':
                    bold = 1;
                    break;
                case 'B':
                    bold = 2;
                    break;
                case 'L':
                    lock = 1;
                    break;
                case 'n':
                    bold = 0;
                    break;
                case '0': /* Fall through */
                case '1': /* Fall through */
                case '2': /* Fall through */
                case '3': /* Fall through */
                case '4': /* Fall through */
                case '5': /* Fall through */
                case '6': /* Fall through */
                case '7': /* Fall through */
                case '8': /* Fall through */
                case '9':
                    update = keypress - 48;
                    break;
                case '!':
                    mcolor = COLOR_RED;
                    rainbow = 0;
                    break;
                case '@':
                    mcolor = COLOR_GREEN;
                    rainbow = 0;
                    break;
                case '#':
                    mcolor = COLOR_YELLOW;
                    rainbow = 0;
                    break;
                case '$':
                    mcolor = COLOR_BLUE;
                    rainbow = 0;
                    break;
                case '%':
                    mcolor = COLOR_MAGENTA;
                    rainbow = 0;
                    break;
                case 'r':
                     rainbow = 1;
                     break;
                case 'm':
                     lambda = !lambda;
                     break;
                case '^':
                    mcolor = COLOR_CYAN;
                    rainbow = 0;
                    break;
                case '&':
                    mcolor = COLOR_WHITE;
                    rainbow = 0;
                    break;
                case 'p':
                case 'P':
                    pause = (pause == 0)?1:0;
                    break;

                }
            }
        }
        for (j = 0; j <= COLS - 1; j += 2) {
            if ((count > updates[j] || asynch == 0) && pause == 0) {

                /* I dont like old-style scrolling, yuck */
                if (oldstyle) {
                    for (i = LINES - 1; i >= 1; i--) {
                        matrix[i][j].val = matrix[i - 1][j].val;
                    }
                    random = (int) rand() % (randnum + 8) + randmin;

                    if (matrix[1][j].val == 0) {
                        matrix[0][j].val = 1;
                    } else if (matrix[1][j].val == ' '
                             || matrix[1][j].val == -1) {
                        if (spaces[j] > 0) {
                            matrix[0][j].val = ' ';
                            spaces[j]--;
                        } else {

                            /* Random number to determine whether head of next collumn
                               of chars has a white 'head' on it. */

                            if (((int) rand() % 3) == 1) {
                                matrix[0][j].val = 0;
                            } else {
                                matrix[0][j].val = (int) rand() % randnum + randmin;
                            }
                            spaces[j] = (int) rand() % LINES + 1;
                        }
                    } else if (random > highnum && matrix[1][j].val != 1) {
                        matrix[0][j].val = ' ';
                    } else {
                        matrix[0][j].val = (int) rand() % randnum + randmin;
                    }

                } else { /* New style scrolling (default) */
                    if (matrix[0][j].val == -1 && matrix[1][j].val == ' '
                        && spaces[j] > 0) {
                        matrix[0][j].val = -1;
                        spaces[j]--;
                    } else if (matrix[0][j].val == -1
                        && matrix[1][j].val == ' ') {
                        length[j] = (int) rand() % (LINES - 3) + 3;
                        matrix[0][j].val = (int) rand() % randnum + randmin;

                        spaces[j] = (int) rand() % LINES + 1;
                    }
                    i = 0;
                    y = 0;
                    firstcoldone = 0;
                    while (i <= LINES) {

                        /* Skip over spaces */
                        while (i <= LINES && (matrix[i][j].val == ' ' ||
                               matrix[i][j].val == -1)) {
                            i++;
                        }

                        if (i > LINES) {
                            break;
                        }

                        /* Go to the head of this collumn */
                        z = i;
                        y = 0;
                        while (i <= LINES && (matrix[i][j].val != ' ' &&
                               matrix[i][j].val != -1)) {
                            matrix[i][j].is_head = false;
                            i++;
                            y++;
                        }

                        if (i > LINES) {
                            matrix[z][j].val = ' ';
                            continue;
                        }

                        matrix[i][j].val = (int) rand() % randnum + randmin;
                        matrix[i][j].is_head = true;

                        /* If we're at the top of the collumn and it's reached its
                           full length (about to start moving down), we do this
                           to get it moving.  This is also how we keep segments not
                           already growing from growing accidentally =>
                         */
                        if (y > length[j] || firstcoldone) {
                            matrix[z][j].val = ' ';
                            matrix[0][j].val = -1;
                        }
                        firstcoldone = 1;
                        i++;
                    }
                }
            }
            /* A simple hack */
            if (!oldstyle) {
                y = 1;
                z = LINES;
            } else {
                y = 0;
                z = LINES - 1;
            }
            for (i = y; i <= z; i++) {
                move(i - y, j);

                if (matrix[i][j].val == 0 || (matrix[i][j].is_head && !rainbow)) {
                    if (console || xwindow) {
                        attron(A_ALTCHARSET);
                    }
                    attron(COLOR_PAIR(COLOR_WHITE));
                    if (bold) {
                        attron(A_BOLD);
                    }
                    if (matrix[i][j].val == 0) {
                        if (console || xwindow) {
                            addch(183);
                        } else {
                            addch('&');
                        }
                    } else {
                        addch(matrix[i][j].val);
                    }

                    attroff(COLOR_PAIR(COLOR_WHITE));
                    if (bold) {
                        attroff(A_BOLD);
                    }
                    if (console || xwindow) {
                        attroff(A_ALTCHARSET);
                    }
                } else {
                    if(rainbow) {
                        int randomColor = rand() % 6;

                        switch(randomColor){
                            case 0:
                                mcolor = COLOR_GREEN;
                                break;
                            case 1:
                                mcolor = COLOR_BLUE;
                                break;
                            case 2:
                                mcolor = COLOR_BLACK;
                                break;
                            case 3:
                                mcolor = COLOR_YELLOW;
                                break;
                            case 4:
                                mcolor = COLOR_CYAN;
                                break;
                            case 5:
                                mcolor = COLOR_MAGENTA;
                                break;
                       }
                    }
                    attron(COLOR_PAIR(mcolor));
                    if (matrix[i][j].val == 1) {
                        if (bold) {
                            attron(A_BOLD);
                        }
                        addch('|');
                        if (bold) {
                            attroff(A_BOLD);
                        }
                    } else {
                        if (console || xwindow) {
                            attron(A_ALTCHARSET);
                        }
                        if (bold == 2 ||
                            (bold == 1 && matrix[i][j].val % 2 == 0)) {
                            attron(A_BOLD);
                        }
                        if (matrix[i][j].val == -1) {
                            addch(' ');
                        } else if (lambda && matrix[i][j].val != ' ') {
                            addstr("λ");
                        } else {
                            addch(matrix[i][j].val);
                        }
                        if (bold == 2 ||
                            (bold == 1 && matrix[i][j].val % 2 == 0)) {
                            attroff(A_BOLD);
                        }
                        if (console || xwindow) {
                            attroff(A_ALTCHARSET);
                        }
                    }
                    attroff(COLOR_PAIR(mcolor));
                }
            }
        }

        //Check if computer is locked
        if(lock == 1){

            //Add our message to the screen
            char *msg = "Computer locked.";
            int msg_x = LINES/2;
            int msg_y = COLS/2 - strlen(msg)/2;
            int i = 0;

            //Add space before message
            move(msg_x-1, msg_y-2);
            for(i = 0; i < strlen(msg)+4; i++)
                addch(' ');

            //Write message
            move(msg_x, msg_y-2);
            addch(' ');
            addch(' ');
            addstr(msg);
            addch(' ');
            addch(' ');

            //Add space after message
            move(msg_x+1, msg_y-2);
            for(i = 0; i < strlen(msg)+4; i++)
                addch(' ');
        }

        napms(update * 10);
    }
    finish();
}
Exemple #15
0
int main(int argc, char *argv[])
{
    WINDOW *p;
    FILE *f;
    int ch;
    initscr();
    savetty();
    noecho();//disable auto-echoing
    cbreak();//making getch() work without a buffer I.E. raw characters
    keypad(stdscr,TRUE);//allows use of special keys, namely the arrow keys
    clear();    // empty the screen
    timeout(0);
    
    /* create a new pad */
    p = newpad(WIDE,COLS);
    keypad(p, TRUE);
    if( p == NULL )
        printf("Unable to create new pad\n");
    
    load_file(p, f, argv[1]); // open and load the file on the screen
    
    
    /* display the pad’s contents on the screen */
    prefresh(p,0, 0, 0,0, LINES-1,COLS-1);
    int c, cursorY=0, cursorX=0, linseCount = 0;
    while((c=wgetch(p))!='q'){
        switch(c){
            case KEY_LEFT:
                if(cursorX != 0)
                    cursorX--;
                break;
            case KEY_RIGHT:
                if (cursorX != COLS-1)
                    cursorX++;
                break;
            case KEY_UP:
                if(cursorY==0  && linseCount==0)
                    break;
                else if(cursorY == linseCount){
                    cursorY--;
                    linseCount--;
                    prefresh(p,linseCount, 0, 0,0, LINES-1,COLS-1);
                }
                else
                    cursorY--;
                break;
            case KEY_DOWN:
                if(cursorY==LINES-1  && linseCount==WIDE-1)
                    break;
                else if(cursorY == linseCount+LINES-1){
                    cursorY++;
                    linseCount++;
                    prefresh(p,linseCount, 0, 0,0, LINES-1,COLS-1);
                }
                else
                    cursorY++;
                break;
        }
        wmove(p,cursorY,cursorX);
        prefresh(p,linseCount, 0, 0,0, LINES-1,COLS-1);
    }
    endwin();
    return 0;
}
Exemple #16
0
int main(int argc, char *argv[])
{
    int i, j = 0, count = 0, screensaver = 0, asynch = 0, bold = -1,
	force = 0, y, z, firstcoldone = 0, oldstyle = 0, random =
	0, update = 4, highnum = 0, mcolor = COLOR_GREEN, randnum =
	0, randmin = 0, indep = 0;

    char *oldtermname, *syscmd = NULL;
    int optchr, keypress;

    /* Many thanks to morph- ([email protected]) for this getopt patch */
    opterr = 0;
    while ((optchr = getopt(argc, argv, "abBfhilnosxVu:C:")) != EOF) {
	switch (optchr) {
	case 's':
	    screensaver = 1;
	    break;
	case 'a':
	    asynch = 1;
	    break;
	case 'i':
	    indep = 1;
	    break;
	case 'b':
	    if (bold != 2 && bold != 0)
		bold = 1;
	    break;
	case 'B':
	    if (bold != 0)
		bold = 2;
	    break;
	case 'C':
	    if (!strcasecmp(optarg, "green"))
		mcolor = COLOR_GREEN;
	    else if (!strcasecmp(optarg, "red"))
		mcolor = COLOR_RED;
	    else if (!strcasecmp(optarg, "blue"))
		mcolor = COLOR_BLUE;
	    else if (!strcasecmp(optarg, "white"))
		mcolor = COLOR_WHITE;
	    else if (!strcasecmp(optarg, "yellow"))
		mcolor = COLOR_YELLOW;
	    else if (!strcasecmp(optarg, "cyan"))
		mcolor = COLOR_CYAN;
	    else if (!strcasecmp(optarg, "magenta"))
		mcolor = COLOR_MAGENTA;
	    else if (!strcasecmp(optarg, "black"))
		mcolor = COLOR_BLACK;
	    else {
		printf(" Invalid color selection\n Valid "
		       "colors are green, red, blue, "
		       "white, yellow, cyan, magenta " "and black.\n");
		exit(1);
	    }
	    break;
	case 'f':
	    force = 1;
	    break;
	case 'l':
	    console = 1;
	    break;
	case 'n':
	    bold = 0;
	    break;
	case 'h':
	case '?':
	    usage();
	    exit(0);
	case 'o':
	    oldstyle = 1;
	    break;
	case 'u':
	    update = atoi(optarg);
	    break;
	case 'x':
	    xwindow = 1;
	    break;
	case 'V':
	    version();
	    exit(0);
	}
    }

    /* If bold hasn't been turned on or off yet, assume off */
    if (bold == -1)
	bold = 0;

    oldtermname = getenv("TERM");
    if (force && strcmp("linux", getenv("TERM"))) {
	/* Portability wins out here, apparently putenv is much more
	   common on non-Linux than setenv */
	putenv("TERM=linux");
    }
    initscr();
    savetty();
    nonl();
    cbreak();
    noecho();
    timeout(0);
    leaveok(stdscr, TRUE);
    curs_set(0);
    signal(SIGINT, finish);
    signal(SIGWINCH, handle_sigwinch);

#ifdef HAVE_CONSOLECHARS
    if (console)
	if (va_system("consolechars -f matrix") != 0) {
	    c_die
		(" There was an error running consolechars. Please make sure the\n"
		 " consolechars program is in your $PATH.  Try running \"setfont matrix\" by hand.\n");
	}
#elif defined(HAVE_SETFONT)
    if (console)
	if (va_system("setfont matrix") != 0) {
	    c_die
		(" There was an error running setfont. Please make sure the\n"
		 " setfont program is in your $PATH.  Try running \"setfont matrix\" by hand.\n");
	}
#endif
    if (has_colors()) {
	start_color();
	/* Add in colors, if available */
#ifdef HAVE_USE_DEFAULT_COLORS
	if (use_default_colors() != ERR) {
	    init_pair(COLOR_BLACK, -1, -1);
	    init_pair(COLOR_GREEN, COLOR_GREEN, -1);
	    init_pair(COLOR_WHITE, COLOR_WHITE, -1);
	    init_pair(COLOR_RED, COLOR_RED, -1);
	    init_pair(COLOR_CYAN, COLOR_CYAN, -1);
	    init_pair(COLOR_MAGENTA, COLOR_MAGENTA, -1);
	    init_pair(COLOR_BLUE, COLOR_BLUE, -1);
	    init_pair(COLOR_YELLOW, COLOR_YELLOW, -1);
	} else {
#else
	{
#endif
	    init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
	    init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
	    init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
	    init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
	    init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
	    init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
	    init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
	    init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
	}
    }

    srand(time(NULL));

    /* Set up values for random number generation */
    if (console || xwindow) {
	randnum = 51;
	randmin = 166;
	highnum = 217;
    } else {
	randnum = 93;
	randmin = 33;
	highnum = 123;
    }

    var_init();

    while (1) {
	count++;
	if (count > 4)
	    count = 1;

	if ((keypress = wgetch(stdscr)) != ERR) {
	    if (screensaver == 1)
		finish(0);
	    else
		switch (keypress) {
		case 'q':
		    finish(0);
		    break;
		case 'a':
		    asynch = 1 - asynch;
		    break;
		case 'b':
		    bold = 1;
		    break;
		case 'B':
		    bold = 2;
		    break;
		case 'i':
		    indep = 1 - indep;
		    break;
		case 'n':
		    bold = 0;
		    break;
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
		    update = keypress - 48;
		    break;
		case '!':
		    mcolor = COLOR_RED;
		    break;
		case '@':
		    mcolor = COLOR_GREEN;
		    break;
		case '#':
		    mcolor = COLOR_YELLOW;
		    break;
		case '$':
		    mcolor = COLOR_BLUE;
		    break;
		case '%':
		    mcolor = COLOR_MAGENTA;
		    break;
		case '^':
		    mcolor = COLOR_CYAN;
		    break;
		case '&':
		    mcolor = COLOR_WHITE;
		    break;
		}
	}
	for (j = 0; j <= COLS - 1; j += 2) {
		/* I dont like old-style scrolling, yuck */
		if (oldstyle) {
		    for (i = LINES - 1; i >= 1; i--)
			matrix[i][j].val = matrix[i - 1][j].val;

		    random = (int) rand() % (randnum + 8) + randmin;

		    if (matrix[1][j].val == 0)
			matrix[0][j].val = 1;
		    else if (matrix[1][j].val == ' '
			     || matrix[1][j].val == -1) {
			if (spaces[j] > 0) {
			    matrix[0][j].val = ' ';
			    spaces[j]--;
			} else {

			    /* Random number to determine whether head of next collumn
			       of chars has a white 'head' on it. */

			    if (((int) rand() % 3) == 1)
				matrix[0][j].val = 0;
			    else
				matrix[0][j].val = (int) rand() % randnum
				    + randmin;

			    spaces[j] = (int) rand() % LINES + 1;
			}
		    } else if (random > highnum && matrix[1][j].val != 1)
			matrix[0][j].val = ' ';
		    else
			matrix[0][j].val =
			    (int) rand() % randnum + randmin;

		} else {	/* New style scrolling (default) */
		    /* generate gap */
		    if (matrix[0][j].val == -1 && matrix[1][j].val == ' '
			&& spaces[j] > 0) {
			matrix[0][j].val = -1;
			spaces[j]--;
		    /* spawn a new segment */
		    } else if (matrix[0][j].val == -1
			       && matrix[1][j].val == ' ') {
			length[j] = (int) rand() % (LINES - 3) + 3;
			matrix[0][j].val =
			    (int) rand() % randnum + randmin;

			if ((int) rand() % 2 == 1)
			    matrix[0][j].bold = 2;

			/* reuse .speed iff asynch && !indep */
			if (asynch == 0 && indep == 0)
			    matrix[0][j].speed = 1;
			else if (matrix[0][j].speed == -1 || indep == 1)
			    matrix[0][j].speed = (int) rand() % 3 + 1;

			spaces[j] = (int) rand() % LINES + 1;
		    }
		    i = 0;
		    y = 0;
		    firstcoldone = 0;
		    while (i <= LINES) {

			/* Skip over spaces */
			while (i <= LINES && (matrix[i][j].val == ' ' ||
			       matrix[i][j].val == -1))
			    i++;

			if (i > LINES)
			    break;

			/* Go to the head of this collumn */
			z = i;
			y = 0;
			while (i <= LINES && (matrix[i][j].val != ' ' &&
			       matrix[i][j].val != -1)) {
			    if (i < LINES && matrix[i][j].speed != -1 &&
			        matrix[i+1][j].speed != -1 &&
			        matrix[i][j].speed !=
			        matrix[i+1][j].speed) {
				i++;
				y++;
				break;
			    }
			    i++;
			    y++;
			}

			if (count <= matrix[i-1][j].speed) {
			    i++;
			    continue;
			}

			if (i > LINES) {
			    matrix[z][j].val = ' ';
			    matrix[LINES][j].bold = 1;
			    continue;
			}

			matrix[i][j].val =
			    (int) rand() % randnum + randmin;
			matrix[i][j].speed = matrix[i-1][j].speed;

			/* propagate white heads */
			if (matrix[i - 1][j].bold == 2) {
			    matrix[i - 1][j].bold = 1;
			    matrix[i][j].bold = 2;
			}

			/* If we're at the top of the collumn and it's reached its
			 * full length (about to start moving down), we do this
			 * to get it moving.  This is also how we keep segments not
			 * already growing from growing accidentally =>
			 */
			if (y > length[j] || firstcoldone) {
			    matrix[z][j].val = ' ';
			    matrix[0][j].val = -1;
			}
			firstcoldone = 1;
			i++;
		    }
		}
	    /* Hack =P */
	    if (!oldstyle) {
		y = 1;
		z = LINES;
	    } else {
		y = 0;
		z = LINES - 1;
	    }
	    for (i = y; i <= z; i++) {
		move(i - y, j);

		if (matrix[i][j].val == 0 || matrix[i][j].bold == 2) {
		    if (console || xwindow)
			attron(A_ALTCHARSET);
		    attron(COLOR_PAIR(COLOR_WHITE));
		    if (bold)
			attron(A_BOLD);
		    if (matrix[i][j].val == 0) {
			if (console || xwindow)
			    addch(183);
			else
			    addch('&');
		    } else
			addch(matrix[i][j].val);

		    attroff(COLOR_PAIR(COLOR_WHITE));
		    if (bold)
			attroff(A_BOLD);
		    if (console || xwindow)
			attroff(A_ALTCHARSET);
		} else {
		    attron(COLOR_PAIR(mcolor));
		    if (matrix[i][j].val == 1) {
			if (bold)
			    attron(A_BOLD);
			addch('|');
			if (bold)
			    attroff(A_BOLD);
		    } else {
			if (console || xwindow)
			    attron(A_ALTCHARSET);
			if (bold == 2 ||
			    (bold == 1 && matrix[i][j].val % 2 == 0))
			    attron(A_BOLD);
			if (matrix[i][j].val == -1)
			    addch(' ');
			else
			    addch(matrix[i][j].val);
			if (bold == 2 ||
			    (bold == 1 && matrix[i][j].val % 2 == 0))
			    attroff(A_BOLD);
			if (console || xwindow)
			    attroff(A_ALTCHARSET);
		    }
		    attroff(COLOR_PAIR(mcolor));
		}
	    }
	}
	refresh();
	napms(update * 10);

    }

    syscmd = nmalloc(sizeof (char *) * (strlen(oldtermname) + 15));
    sprintf(syscmd, "putenv TERM=%s", oldtermname);    
    system(syscmd);
    finish(0);

}
Exemple #17
0
cell pp_curs_savetty(cell x) {
	if (!Running) return UNSPECIFIC;
	savetty();
	return UNSPECIFIC;
}
Exemple #18
0
int main(){
    WINDOW *consola;
    /*
    La función initscr(); determina el tipo de terminal e inicializa todas las
    estructuras de datos privadas de la biblioteca ncurses.h
    También hace una primera llamada a la función refresh, que actualiza el
    aspecto de la pantalla. Si se produce algún error, imprime un mensaje
    indicándolo y acaba el programa. Si finaliza correctamente, devuelve un
    puntero a una estructura que hace referencia a la pantalla. Ese puntero se
    usará como parámetro en algunas funciones.
    */
     consola = initscr();
     /*
    La función savetty(); guarda el estado en que se puso en marcha el programa
    por una llamada a reset_prog_mode().
    */
	savetty();
     /*
	La función keypad activa y desactiva la rutina de procesado de caracteres
    especiales. Si está activada (el segundo parámetro es TRUE) cuando se pulse
    una tecla especial, las funciones de lectura, como getch(), devuelven un
    único valor, por ejemplo KEY_LEFT, para el cursor hacia arriba. 
    Si está desactivada, la lectura de una tecla especial requiere "dos"
    caracteres, y por tanto dos lecturas, y es el usuario el que se tiene que
    encargar de interpretar los caracteres de estas teclas especiales.
	*/
	keypad(consola, TRUE);
	/*
	Las rutinas echo() y noecho() controlan si el carácter tecleado es escrito
    en pantalla (eco) al leerse con la rutina getch.
	*/
	noecho();
	/*
	La rutinas nl() y nonl() controlan el funcionamiento de la tecla
    "retorno de carro" tanto en la lectura como en la escritura. Si se llama a 
    nonl(), la lectura del retorno de carro, no añade una nueva línea, sino que
    su funcionamiento es controlado directamente por la aplicación.
    */
    nonl();
    /*
    La rutina curs_set establece el aspecto del cursor:
        0 -> invisible, 1 -> visible, 2 -> muy visible.
    */
    curs_set(0);
    /*
    Limpia la pantalla (consola)
    */
    erase();
     
     Persona alumno = {"Martin","Alem","M",19};
     typedef unsigned int nota;
     float promedio = 0;
     nota parcial1 = 0;
     nota parcial2 = 0;
     
     printw("\nNota del primer parcial: ");
     scanw("%d", &parcial1);
     printw("\nNota del segundo parcial: ");
     scanw("%d", &parcial2);
     promedio = (float) (parcial1 + parcial2) / 2;
     printw("\nEl alumno %7s %7s tiene como promedio: %.2f",
     alumno.nombre,alumno.apellido,promedio);
     printw("\n\nPresione una tecla para salir...");
     getch();
     echo();
     /*
    La función resetty() restaura el modo de programa a partir de la última
    llamada a savetty(), y se usa previo a la función endwin();
    */
     resetty();
     /*
    Al terminar de trabajar en el modo terminal, el programa debe llamar a la
    función endwin() para restaurar los valores apropiados de las variables de
    entorno y seguir trabajando en el modo texto normal
    */
     endwin();
     return 0;
}
Exemple #19
0
/* Setup the IO for curses or non-curses mode.
   - In non-curses mode, readline and gdb use the standard input and
   standard output/error directly.
   - In curses mode, the standard output/error is controlled by TUI
   with the tui_stdout and tui_stderr.  The output is redirected in
   the curses command window.  Several readline callbacks are installed
   so that readline asks for its input to the curses command window
   with wgetch().  */
void
tui_setup_io (int mode)
{
  extern int _rl_echoing_p;

  if (mode)
    {
      /* Redirect readline to TUI.  */
      tui_old_rl_redisplay_function = rl_redisplay_function;
      tui_old_rl_deprep_terminal = rl_deprep_term_function;
      tui_old_rl_prep_terminal = rl_prep_term_function;
      tui_old_rl_getc_function = rl_getc_function;
      tui_old_rl_display_matches_hook = rl_completion_display_matches_hook;
      tui_old_rl_outstream = rl_outstream;
      tui_old_rl_echoing_p = _rl_echoing_p;
      rl_redisplay_function = tui_redisplay_readline;
      rl_deprep_term_function = tui_deprep_terminal;
      rl_prep_term_function = tui_prep_terminal;
      rl_getc_function = tui_getc;
      _rl_echoing_p = 0;
      rl_outstream = tui_rl_outstream;
      rl_prompt = 0;
      rl_completion_display_matches_hook = tui_rl_display_match_list;
      rl_already_prompted = 0;

      /* Keep track of previous gdb output.  */
      tui_old_stdout = gdb_stdout;
      tui_old_stderr = gdb_stderr;
      tui_old_uiout = dynamic_cast<cli_ui_out *> (current_uiout);
      gdb_assert (tui_old_uiout != nullptr);

      /* Reconfigure gdb output.  */
      gdb_stdout = tui_stdout;
      gdb_stderr = tui_stderr;
      gdb_stdlog = gdb_stdout;	/* for moment */
      gdb_stdtarg = gdb_stderr;	/* for moment */
      gdb_stdtargerr = gdb_stderr;	/* for moment */
      current_uiout = tui_out;

      /* Save tty for SIGCONT.  */
      savetty ();
    }
  else
    {
      /* Restore gdb output.  */
      gdb_stdout = tui_old_stdout;
      gdb_stderr = tui_old_stderr;
      gdb_stdlog = gdb_stdout;	/* for moment */
      gdb_stdtarg = gdb_stderr;	/* for moment */
      gdb_stdtargerr = gdb_stderr;	/* for moment */
      current_uiout = tui_old_uiout;

      /* Restore readline.  */
      rl_redisplay_function = tui_old_rl_redisplay_function;
      rl_deprep_term_function = tui_old_rl_deprep_terminal;
      rl_prep_term_function = tui_old_rl_prep_terminal;
      rl_getc_function = tui_old_rl_getc_function;
      rl_completion_display_matches_hook = tui_old_rl_display_matches_hook;
      rl_outstream = tui_old_rl_outstream;
      _rl_echoing_p = tui_old_rl_echoing_p;
      rl_already_prompted = 0;

      /* Save tty for SIGCONT.  */
      savetty ();
    }
}