简言之的选用ncurses的多进度小程序

作者: 贝博体育app网络  发布:2019-09-23

能够运转在linux平台。供给安装了ncurses,步入目录直接make就行

#SC : Screen Control

obj:
    gcc -o sc forks.c main.c -lncurses -std=c99

debug:
    gcc -o sc forks.c main.c -lncurses -std=c99 -g

clean:
    rm  sc
/*
 * @author ynwzhao 20121220
 * SC:Screen Control.
 */
#include "forks.h"

void llfork(void);
void leftfork(void);
void rightfork(void);
void screenfork(void);

// just as the name: initial. more in lib ncurses's HOWTO
void init(void)
{
    initscr();
    cbreak();
    keypad(stdscr,TRUE);
    noecho();
}

int fd[3][2];

int main(int argc,char** argv)
{
    int pid_ll;
    int pid_left,pid_right;
    int pid4;

    pipe(fd[0]);
    pipe(fd[1]);
    pipe(fd[2]);

    init();
    if((pid_ll=fork())==0)
        llfork();    
    else if((pid_left=fork())==0)
        leftfork();
    else if((pid_right=fork())==0)
        rightfork();     
    else if((pid4=fork())==0)
        screenfork();

    //close pipe descriptor,wait for children process exit
    for(int i=0;i<3;i++)
        for(int j=0;j<2;j++)
            close(fd[i][j]);
    int status;  
    waitpid(pid4, &status,0);
    waitpid(pid_ll, &status,0);
    waitpid(pid_left, &status,0);
    waitpid(pid_right,  &status,0);

    endwin();
    return 0;

}

//get the keyboard press action
void llfork(void)//底层键盘处理
{
    for(int i=0;i<3;i++)
        for(int j=0;j<2;j++){
            if(i==1 && j==1) continue;
            if(i==2 && j==1) continue;
            close(fd[i][j]);
        }

    int ch;
    while(ch=getch()){
        if(ch==KEY_F(2)){//end condition
            write(fd[1][1],&ch,4);
            write(fd[2][1],&ch,4);

            close(fd[1][1]);
            close(fd[2][1]);
            exit(0);
        }

        if(isalpha(ch))
            write(fd[1][1],&ch,4);
        else
            write(fd[2][1],&ch,4);
    }
    exit(1);
}
void leftfork(void)//左边 “WSAD”
{
    for(int i=0;i<3;i++)
        for(int j=0;j<2;j++){
            if(i==1 && j==0) continue;
            if(i==0 && j==1) continue;
            close(fd[i][j]);
        }
    while(1){
        int ch;
        read(fd[1][0],&ch,4);
        if(ch==KEY_F(2)){//end condition
            close(fd[1][0]);
            close(fd[0][1]);
            exit(0);
        }
        write(fd[0][1],&ch,4);
    }
}
void rightfork(void)//右边方向键
{
    for(int i=0;i<3;i++)
        for(int j=0;j<2;j++){
            if(i==2 && j==0) continue;
            if(i==0 && j==1) continue;
            close(fd[i][j]);
        }
    while(1){
        int ch;
        read(fd[2][0],&ch,4);
        write(fd[0][1],&ch,4);

        if(ch==KEY_F(2)){
            close(fd[2][0]);
            close(fd[0][1]);
            exit(0);
        }
    }
}

void screenfork(void)//屏幕刷新
{
    for(int i=0;i<3;i++)
        for(int j=0;j<2;j++){
            if(i==0 && j==0) continue;
            close(fd[i][j]);
        }
    screen_refresh(fd[0][0]);
    close(fd[0][0]);
    exit(0);
}
/*
 * @author ynwzhao 20121220
 * SC:Screen Control.
 */
#ifndef FORKS_H
#define FORKS_H
#include <ncurses.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>

void screen_refresh(int fd);

#endif
/*
 * @author ynwzhao 20121220
 * SC:Screen Control.
 */
#include "forks.h"

#define LEFT 0
#define RIGHT 1

struct location{
    int row;
    int col;
};

void screen_refresh(int fd)
{
    int row,col;
    int ch;
    struct location locate[2]={{5,20},{5,40}};

    getmaxyx(stdscr,row,col);
    mvaddch(locate[LEFT].row,locate[LEFT].col,'*');
    mvaddch(locate[RIGHT].row,locate[RIGHT].col,'*');
    mvprintw(row-1,1,"Press F2 to exit.");
    refresh();

    if(read(fd,&ch,4) < 0) exit(1);
    while(ch != KEY_F(2)){
        //printw("[%d:%d]",KEY_UP,ch);
        switch(ch){
            case 'w':
            case 'W':
                mvaddch(locate[LEFT].row,locate[LEFT].col,' ');
                locate[LEFT].row--;
                mvaddch(locate[LEFT].row,locate[LEFT].col,'*');
                break;
            case 's':
            case 'S':
                mvaddch(locate[LEFT].row,locate[LEFT].col,' ');
                locate[LEFT].row++;
                mvaddch(locate[LEFT].row,locate[LEFT].col,'*');
                break;
            case 'a':
            case 'A':
                mvaddch(locate[LEFT].row,locate[LEFT].col,' ');
                locate[LEFT].col--;
                mvaddch(locate[LEFT].row,locate[LEFT].col,'*');
                break;
            case 'd':
            case 'D':
                mvaddch(locate[LEFT].row,locate[LEFT].col,' ');
                locate[LEFT].col++;
                mvaddch(locate[LEFT].row,locate[LEFT].col,'*');
                break;

            case KEY_UP:
                mvaddch(locate[RIGHT].row,locate[RIGHT].col,' ');
                locate[RIGHT].row--;
                mvaddch(locate[RIGHT].row,locate[RIGHT].col,'*');
                //printw("%d",ch);
                break;
            case KEY_DOWN:
                mvaddch(locate[RIGHT].row,locate[RIGHT].col,' ');
                locate[RIGHT].row++;
                mvaddch(locate[RIGHT].row,locate[RIGHT].col,'*');
                //printw("%d",ch);
                break;
            case KEY_LEFT:
                mvaddch(locate[RIGHT].row,locate[RIGHT].col,' ');
                locate[RIGHT].col--;
                mvaddch(locate[RIGHT].row,locate[RIGHT].col,'*');
                //printw("%d",ch);
                break;
           case KEY_RIGHT:
                mvaddch(locate[RIGHT].row,locate[RIGHT].col,' ');
                locate[RIGHT].col++;
                mvaddch(locate[RIGHT].row,locate[RIGHT].col,'*');
                //printw("%d",ch);
                break;
            default:
                break;  
        }
        refresh();
        if(read(fd,&ch,4) < 0) exit(1);
    }//end while
}
//该片段来自于http://outofmemory.cn

本文由贝博体育app发布于贝博体育app网络,转载请注明出处:简言之的选用ncurses的多进度小程序

关键词: