void curses_display_init(DisplayState *ds, int full_screen)
{
    DisplayChangeListener *dcl;
#ifndef _WIN32
    if (!isatty(1)) {
        fprintf(stderr, "We need a terminal output\n");
        exit(1);
    }
#endif

    curses_setup();
    curses_keyboard_setup();
    atexit(curses_atexit);

#ifndef _WIN32
#if defined(SIGWINCH) && defined(KEY_RESIZE)
    /* some curses implementations provide a handler, but we
     * want to be sure this is handled regardless of the library */
    signal(SIGWINCH, curses_winch_handler);
#endif
#endif

    dcl = (DisplayChangeListener *) g_malloc0(sizeof(DisplayChangeListener));
    dcl->dpy_update = curses_update;
    dcl->dpy_resize = curses_resize;
    dcl->dpy_refresh = curses_refresh;
    dcl->dpy_text_cursor = curses_cursor_position;
    register_displaychangelistener(ds, dcl);
    qemu_free_displaysurface(ds);
    ds->surface = qemu_create_displaysurface_from(640, 400, 0, 0, (uint8_t*) screen);

    invalidate = 1;
}
Example #2
0
void curses_display_init(DisplayState *ds, int full_screen)
{
#ifndef _WIN32
    if (!isatty(1)) {
        fprintf(stderr, "We need a terminal output\n");
        exit(1);
    }
#endif

    curses_setup();
    curses_keyboard_setup();
    atexit(curses_atexit);

#ifndef _WIN32
#if defined(SIGWINCH) && defined(KEY_RESIZE)
    /* some curses implementations provide a handler, but we
     * want to be sure this is handled regardless of the library */
    signal(SIGWINCH, curses_winch_handler);
#endif
#endif

    dcl = (DisplayChangeListener *) g_malloc0(sizeof(DisplayChangeListener));
    dcl->ops = &dcl_ops;
    register_displaychangelistener(ds, dcl);

    invalidate = 1;
}
Example #3
0
int main(int ac, char **av)
{
    parse_args(ac, av);
    setup_sighandler(SIGINT, 0, on_sigint);
    setup_sighandler(SIGALRM, SA_RESTART, update_display);
    alarm(1);
    gl_env.last_update_time = time(NULL);
    gl_env.logtop = new_logtop(gl_env.history_size);
    if (!gl_env.quiet && !gl_env.line_by_line)
        curses_setup();
    run(gl_env.logtop);
    at_exit();
    return EXIT_SUCCESS;
}
int main(int argc, char *argv[]) {
  if(argc!=2) {
    usage();
  }
	char game_grid[16][16];
	if(argc >= 2) {
		player_name = argv[1];
		if(strlen(player_name) > 32) {
			player_name[32] = 0;
		}
	}
	
	curses_setup();
	play_game(game_grid);
	curses_cleanup();
	return 0;
}
Example #5
0
/*
 * set the terminal as non blocking 
 */
static void curses_init(void)
{
   DEBUG_MSG("curses_init");

   /* init the widgets library */
   wdg_init();

   /* 
    * we have to set it because we ask user interaction
    * during this function.
    * we cant wait to return to set the flag...
    */
   GBL_UI->initialized = 1;

   DEBUG_MSG("curses_init: screen %dx%d colors: %d", (int)current_screen.cols, (int)current_screen.lines,
                                                     (int)(current_screen.flags & WDG_SCR_HAS_COLORS));

   /* initialize the colors */
   wdg_init_color(EC_COLOR, GBL_CONF->colors.fg, GBL_CONF->colors.bg);
   wdg_init_color(EC_COLOR_JOIN1, GBL_CONF->colors.join1, GBL_CONF->colors.bg);
   wdg_init_color(EC_COLOR_JOIN2, GBL_CONF->colors.join2, GBL_CONF->colors.bg);
   wdg_init_color(EC_COLOR_BORDER, GBL_CONF->colors.border, GBL_CONF->colors.bg);
   wdg_init_color(EC_COLOR_TITLE, GBL_CONF->colors.title, GBL_CONF->colors.bg);
   wdg_init_color(EC_COLOR_FOCUS, GBL_CONF->colors.focus, GBL_CONF->colors.bg);
   wdg_init_color(EC_COLOR_MENU, GBL_CONF->colors.menu_fg, GBL_CONF->colors.menu_bg);
   wdg_init_color(EC_COLOR_WINDOW, GBL_CONF->colors.window_fg, GBL_CONF->colors.window_bg);
   wdg_init_color(EC_COLOR_SELECTION, GBL_CONF->colors.selection_fg, GBL_CONF->colors.selection_bg);
   wdg_init_color(EC_COLOR_ERROR, GBL_CONF->colors.error_fg, GBL_CONF->colors.error_bg);
   wdg_init_color(EC_COLOR_ERROR_BORDER, GBL_CONF->colors.error_border, GBL_CONF->colors.error_bg);

   /* set the screen color */
   wdg_screen_color(EC_COLOR);
   
   /* call the setup interface */
   curses_setup();

   /* reached only after the setup interface has quit */
}
void curses_display_init(DisplayState *ds, int full_screen)
{
#ifndef _WIN32
    if (!isatty(1)) {
        fprintf(stderr, "We need a terminal output\n");
        exit(1);
    }
#endif

    curses_setup();
    curses_keyboard_setup();
    atexit(curses_atexit);

#ifndef _WIN32
#if defined(SIGWINCH) && defined(KEY_RESIZE)
    /* some curses implementations provide a handler, but we
     * want to be sure this is handled regardless of the library */
    signal(SIGWINCH, curses_winch_handler);
#endif
#endif

    ds->data = (void *) screen;
    ds->linesize = 0;
    ds->depth = 0;
    ds->width = 640;
    ds->height = 400;
    ds->dpy_update = curses_update;
    ds->dpy_resize = curses_resize;
    ds->dpy_refresh = curses_refresh;
    ds->dpy_text_cursor = curses_cursor_position;

    invalidate = 1;

    /* Standard VGA initial text mode dimensions */
    curses_resize(ds, 80, 25);
}
Example #7
0
void
do_command(int fanout, char *username)
{
    struct sigaction signaler;
    FILE *fd, *fda, *in;
    char buf[MAXBUF], cbuf[MAXBUF], pipebuf[2048];
    char *command = TOP_COMMAND;
    int status, i, j, n, g, pollret, nrofargs, arg, slen, fdf;
    int tnodes, k, l;
    char *p, **q, **rsh, *cd, **cmd, *rshstring;
    char *scriptbase, *scriptdir;
    node_t *nodeptr, *nodehold;
    struct pollfd fds[2];
    
    maxnodelen = 0;
    j = i = 0;
    in = NULL;
    cd = pipebuf;
    exitflag = 0;
    sortedprocs = NULL;

    if (debug) {
	if (username != NULL)
	    (void)printf("As User: %s\n", username);
	(void)printf("On nodes:\n");
    }
    for (nodeptr = nodelink; nodeptr; nodeptr = nodeptr->next) {
	if (strlen(nodeptr->name) > maxnodelen)
	    maxnodelen = strlen(nodeptr->name);
	if (debug) {
	    if (!(j % 4) && j > 0)
		(void)printf("\n");
	    (void)printf("%s\t", nodeptr->name);
	}
	j++;
    }
    if (maxnodelen < 8)
	    maxnodelen = 8;
    
    i = j; /* side effect of above */
    tnodes = i;
    j = i / fanout;
    if (i % fanout)
	j++; /* compute the # of rungroups */

    nodedata = calloc(tnodes, sizeof(nodedata_t));
    sortnode = calloc(tnodes, sizeof(nodedata_t));
    if (nodedata == NULL || sortnode == NULL)
	    bailout();

    /* the per-node array of procs */
    procdata = calloc(tnodes, sizeof(procdata_t *));
    if (procdata == NULL)
	    bailout();
    
    if (debug) {
	(void)printf("\nDo Command: %s\n", command);
	(void)printf("Fanout: %d Groups:%d\n", fanout, j);
    }

#if 0
    /* XXX this is going to be a problem */
    close(STDIN_FILENO); /* DAMN this bug took awhile to find */
    if (open("/dev/null", O_RDONLY, NULL) != 0)
	    bailout();
#endif
    signaler.sa_handler = sig_handler;
    signaler.sa_flags = 0;
    sigaction(SIGTERM, &signaler, NULL);
    sigaction(SIGINT, &signaler, NULL);
    
    rsh = parse_rcmd("RCMD_CMD", "RCMD_CMD_ARGS", &nrofargs);
    rshstring = build_rshstring(rsh, nrofargs);

    if (!batchflag)
	    curses_setup();
    
    /* begin the processing loop */
    while (!exitflag) {
	g = 0;
        nodeptr = nodelink;
        for (n=0; n <= j; n++) { /* fanout group */
	    if (exitflag)
		    goto out;
	    nodehold = nodeptr;
	    for (i=0; (i < fanout && nodeptr != NULL); i++) {
		g++;
		if (exitflag)
			goto out;
		if (debug)
		    (void)printf("Working node: %d, fangroup %d,"
			" fanout part: %d\n", g, n, i);
/*
 * we set up pipes for each node, to prepare for the oncoming barrage of data.
 * Close on exec must be set here, otherwise children spawned after other
 * children, inherit the open file descriptors, and cause the pipes to remain
 * open forever.
 */
		if (pipe(nodeptr->out.fds) != 0)
		    bailout();
		if (pipe(nodeptr->err.fds) != 0)
		    bailout();
		if (fcntl(nodeptr->out.fds[0], F_SETFD, 1) == -1)
		    bailout();
		if (fcntl(nodeptr->out.fds[1], F_SETFD, 1) == -1)
		    bailout();
		if (fcntl(nodeptr->err.fds[0], F_SETFD, 1) == -1)
		    bailout();
		if (fcntl(nodeptr->err.fds[1], F_SETFD, 1) == -1)
		    bailout();
		nodeptr->childpid = fork();
		switch (nodeptr->childpid) {
		    /* its the ol fork and switch routine eh? */
		case -1:
		    bailout();
		    break;
		case 0:
		    /* remove from parent group to avoid kernel
		     * passing signals to children.
		     */
		    (void)setsid();
		    if (dup2(nodeptr->out.fds[1], STDOUT_FILENO) 
			!= STDOUT_FILENO) 
			bailout();
		    if (dup2(nodeptr->err.fds[1], STDERR_FILENO)
			!= STDERR_FILENO)
			bailout();
		    if (close(nodeptr->out.fds[0]) != 0)
			bailout();
		    if (close(nodeptr->err.fds[0]) != 0)
			bailout();
		    /* stdin & stderr non-blocking */
		    fdf = fcntl(nodeptr->out.fds[0], F_GETFL);
		    fcntl(nodeptr->out.fds[0], F_SETFL, fdf|O_NONBLOCK);
		    fdf = fcntl(nodeptr->err.fds[0], F_GETFL);
		    fcntl(nodeptr->err.fds[0], F_SETFL, fdf|O_NONBLOCK);
		    
		    if (username != NULL)
			(void)snprintf(buf, MAXBUF, "%s@%s", username,
				       nodeptr->name);
		    else
			(void)snprintf(buf, MAXBUF, "%s", nodeptr->name);
		    if (debug)
			    (void)printf("%s %s %s\n", rshstring,
				buf, command);
		    if (testflag && rshport > 0 && porttimeout > 0)
			if (!test_node_connection(rshport, porttimeout,
						  nodeptr))
			    _exit(EXIT_SUCCESS);
		    cmd = calloc(nrofargs+1, sizeof(char *));
		    arg = 0;
		    while (rsh[arg] != NULL) {
			    cmd[arg] = rsh[arg];
			    arg++;
		    }
		    cmd[arg++] = buf;
		    cmd[arg++] = command;
		    cmd[arg] = (char *)0;
		    execvp(rsh[0], cmd);
		    bailout();
		    break;
		default:
		    break;
		} /* switch */
		nodeptr = nodeptr->next;
	    } /* for i */
	    nodeptr = nodehold;
	    for (i=0; (i < fanout && nodeptr != NULL); i++) {
	        int pid=0;
		    
		if (exitflag)
			goto out;
		if (debug)
		    (void)printf("Printing node: %d, fangroup %d,"
				 " fanout part: %d\n", ((n) ? g-fanout+i : i),
				 n, i);
		currentchild = nodeptr->childpid;
		/* now close off the useless stuff, and read the goodies */
		if (close(nodeptr->out.fds[1]) != 0)
		    bailout();
		if (close(nodeptr->err.fds[1]) != 0)
		    bailout();
		fda = fdopen(nodeptr->out.fds[0], "r"); /* stdout */
		if (fda == NULL)
		    bailout();
		fd = fdopen(nodeptr->err.fds[0], "r"); /* stderr */
		if (fd == NULL)
		    bailout();
		fds[0].fd = nodeptr->out.fds[0];
		fds[1].fd = nodeptr->err.fds[0];
		fds[0].events = POLLIN|POLLPRI;
		fds[1].events = POLLIN|POLLPRI;
		pollret = 1;

//		DPRINTF("HELLO: n=%d %d %d - %d %d\n", n, g-fanout+i, i, g, fanout);
		DPRINTF("HELLO: %d\n", n*fanout + i);
		while (pollret >= 0) {
		    int gotdata;

		    pollret = poll(fds, 2, 5);
		    gotdata = 0;
		    if ((fds[0].revents&POLLIN) == POLLIN ||
			(fds[0].revents&POLLHUP) == POLLHUP ||
		        (fds[0].revents&POLLPRI) == POLLPRI) {
#ifdef __linux__
			cd = fgets(pipebuf, sizeof(pipebuf), fda);
			if (cd != NULL) {
#else
			while ((cd = fgets(pipebuf, sizeof(pipebuf), fda))) {
#endif
				pid += parse_top(cd, nodeptr,
				    n*fanout + i, pid);
			    gotdata++;
			}
		    }
		    if ((fds[1].revents&POLLIN) == POLLIN ||
			(fds[1].revents&POLLHUP) == POLLHUP ||
			(fds[1].revents&POLLPRI) == POLLPRI) {
#ifdef __linux__
			cd = fgets(pipebuf, sizeof(pipebuf), fd);
			if (cd != NULL) {
#else
			while ((cd = fgets(pipebuf, sizeof(pipebuf), fd))) {
#endif
			    gotdata++;
			}
		    }
		    if (!gotdata)
			if (((fds[0].revents&POLLHUP) == POLLHUP ||
			     (fds[0].revents&POLLERR) == POLLERR ||
			     (fds[0].revents&POLLNVAL) == POLLNVAL) &&
			    ((fds[1].revents&POLLHUP) == POLLHUP ||
			     (fds[1].revents&POLLERR) == POLLERR ||
			     (fds[1].revents&POLLNVAL) == POLLNVAL))
			    break;
		}
		fclose(fda);
		fclose(fd);
		(void)wait(&status);
		nodeptr = nodeptr->next;
	    } /* for pipe read */
	    totalpids = 0;
	    for (k=0; k < tnodes; k++) {
		    totalpids += nodedata[k].procs;
		    DPRINTF("k=%d procs=%d\n", k, nodedata[k].procs);
	    }
	    DPRINTF(" total of %d pids\n", totalpids);
	    if (sortedprocs != NULL)
		    free(sortedprocs);
	    sortedprocs = calloc(totalpids, sizeof(procdata_t *));
	    i = 0;
	    DPRINTF("tnodes=%d\n", tnodes);
	    for (k=0; k < tnodes; k++) {
		    DPRINTF("node %d procs==%d\n", k, nodedata[k].procs);
		    for (l=0; l < nodedata[k].procs; l++) {
			    if (procdata[k][l].node != NULL &&
				strcmp(procdata[k][l].command, "")) {
				    sortedprocs[i] = &procdata[k][l];
				    DPRINTF("pdata=%p\n", &procdata[k][l]);
				    i++;
			    }
		    }
	    }
	    qsort(sortedprocs, i, sizeof(procdata_t *), compare_proc);
#if 0
	    for (k=0; k < i; k++)
		    DPRINTF("node=%s, pid=%d, cpu=%f\n",
			sortedprocs[k]->node->name, sortedprocs[k]->pid,
			sortedprocs[k]->cpu);
#endif
	    if (!batchflag && !debug) {
		    curses_print(i, tnodes);
		    curses_getkey();
	    }
	} /* for n */
	DPRINTF("fanout done\n");
	if (batchflag == 1 || debug) {
		batch_print(i, tnodes);
		exitflag = 1;
	}
    } /* while loop */
out:
    if (!batchflag && !debug) {
	    move(LINES, 0);
	    endwin();
    }
    
    free(rshstring);
    for (i=0; rsh[i] != NULL; i++)
	    free(rsh[i]);
    free(rsh);
}

void
sig_handler(int i)
{
	switch (i) {
	case SIGINT:
	case SIGTERM:
		exitflag = 1;
		break;
	default:
		bailout();
		break;
	}
}