/* * Control.c * * Control for labyrinthe * * Gouverneur Th. - Cuisinier Gi. */ #include #include #include #include #include #include #include #include #include "Ecran.h" #include "self.h" extern "C" { #define GOTOXY(X,Y) printf("\033[%d;%dH",Y,X); fflush(stdout) void ControlePion(void); void handler_hup(int); int GetKey(); int main (void) { int fd; char buf[10]; struct sigaction sa; sigset_t sset; sa.sa_handler = handler_hup; sigemptyset(&(sa.sa_mask)); sigaction(SIGHUP, &sa, NULL); sigemptyset(&sset); sigdelset(&sset, SIGHUP); sigprocmask(SIG_UNBLOCK, &sset, NULL); while(1) switch (Menu()) { case 1: fd = open("Laby.pid", 0); read(fd, &buf, sizeof(buf)); EffEcran(); printf("Send SIGINT to %d\n\tPress any key to continue..\n", atoi(buf)); kill(atoi(buf), SIGINT); fflush(stdin); getchar(); break; case 2: fd = open("Laby.pid", 0); read(fd, &buf, sizeof(buf)); EffEcran(); printf("Send SIGQUIT to %d\n\tPress any key to continue..\n", atoi(buf)); kill(atoi(buf), SIGQUIT); fflush(stdin); getchar(); break; case 3: ControlePion(); break; case 4: return 0; break; default: break; } return 0; } void ControlePion(void) { int pid, fd; char buf[10]; char szChoix[80]; int sig; initscr(); keypad(stdscr,TRUE); /* write our pid to another file... */ fd = open("Control.pid", O_CREAT | O_TRUNC | O_APPEND | O_WRONLY, 0700); sprintf(buf, "%d\n", getpid()); write(fd, &buf, strlen(buf)+1); close(fd); fd = open("Manual.pid", 0); read(fd, &buf, sizeof(buf)); pid = atoi(buf); //EffEcran(); //GOTOXY(3,3); //printf("Send START signal to %d\n\tPress any key to continue..\n", pid); kill(pid, SIGSTART); while (1) { //EffEcran(); /*GOTOXY(1,1); printf("Arrow Left-Move Gauche\n"); GOTOXY(1,2); printf("Arrow Right-Move Droite\n"); GOTOXY(1,3); printf("Arrow Up-Move Haut\n"); GOTOXY(1,4); printf("Arrow Down-Move Bas\n"); GOTOXY(5,8); printf("Your move: "); fflush(stdout);*/ //fgets(&szChoix[0], sizeof(szChoix), stdin); kill(pid, sig = GetKey()); printf("\n%d", sig); refresh(); /*switch(szChoix[0]) { case 'G': kill(pid, SIGGAUCHE); GOTOXY(15, 15); printf("Sent SIGGAUCHE\n"); sleep(1); break; case 'D': kill(pid, SIGDROITE); GOTOXY(15, 15); printf("Sent SIGDROITE\n"); sleep(1); break; case 'H': kill(pid, SIGHAUT); GOTOXY(15, 15); printf("Sent SIGHAUT\n"); sleep(1); break; case 'B': kill(pid, SIGBAS); GOTOXY(15, 15); printf("Sent SIGBAS\n"); sleep(1); break; default: GOTOXY(10, 10); printf("Choix erronne...\n"); sleep(1); GOTOXY(10, 10); printf(" \n"); break; } */ } } void handler_hup(int s) { endwin(); EffEcran(); GOTOXY(1,1); printf("Fin de la partie :)\n"); exit(0); } } int GetKey() { int key; fflush(stdin); key = getch(); fflush(stdin); switch(key) { case KEY_DOWN: return SIGBAS; break; case KEY_UP : return SIGHAUT; break; case KEY_LEFT : return SIGGAUCHE; break; case KEY_RIGHT: return SIGDROITE; break; default: return -1; } }