/* * endgame: * Do what's necessary at the end of the game */ void endgame() { char ch; prman(); if (Errors >= MAXERRS) Errors = MAXERRS + 2; prword(); prdata(); move(MESGY, MESGX); if (Errors > MAXERRS) printw("Sorry, the word was \"%s\"\n", Word); else printw("You got it!\n"); for (;;) { mvaddstr(MESGY + 1, MESGX, "Another word? "); leaveok(stdscr, FALSE); refresh(); if ((ch = readch()) == 'n') die(0); else if (ch == 'y') break; mvaddstr(MESGY + 2, MESGX, "Please type 'y' or 'n'"); } leaveok(stdscr, TRUE); move(MESGY, MESGX); deleteln(); deleteln(); deleteln(); }
/* * endgame: * Do what is necessary at the end of the game */ void endgame() { char ch; /* Print out the hangman */ prman(); /* Print out the current word */ prword(); /* Print out the current guesses */ prdata(); /* Move curses window cursor */ move(MESGY, MESGX); /* Tell the user how they did by printing formatted output in the curses window */ if (Errors >= MAXERRS) { printw("Sorry, the word was \"%s\"\n", Word); } else { printw("You got it!\n"); } for (;;) { /* Prompt for another word */ mvaddstr(MESGY + 1, MESGX, "Another word? "); leaveok(stdscr, FALSE); refresh(); if ((ch = readch()) == 'n') { /* If user says no, exit */ exit(0); } else { if (ch == 'y') { /* If user says yes, keep going */ break; // fixes bug 6 } /* If user says neither y or n, prompt user */ mvaddstr(MESGY + 2, MESGX, "Please type 'y' or 'n'"); } } /* Clean up the window */ leaveok(stdscr, TRUE); move(MESGY, MESGX); deleteln(); deleteln(); deleteln(); }
int save_file(char* text) //save file { char path[200], ch_over, spath='a'; int sp; //path is the path to file, ch_over is just to input a character spath is a condition variablle int i=0; FILE* fps; //pointer for file move(33, 0);//move cursor to lower area printw("Enter path to save the text: "); // ask for path while(spath!='\n') // getting path { spath=getch(); //get path sp=(int) spath; //get code verion of pressed key if((sp>=65 && sp<=90) || (sp>=48 && sp<=57) || (sp>=97 && sp<=122) || sp==95 || sp==46) { addch(spath); //if it is an alphanumeric chaacter path[i]=spath;i++;//add to path variable } if(sp==27 )//escape key) // press escape to abort entering path { move(33,0); deleteln(); //delete line fromm below area return 0; } } path[i]='\0';//add null to end of path variable if(file_exist(path)) // in case file is already there do this { move(33, 0);deleteln(); //deleet line form menu printw("\nFile with this name already exist, Press 'y' to over write?"); ch_over=getch(); addch(ch_over); if(ch_over=='y')//if user pressed y { fps=fopen(path,"w+");//open file fprintf(fps,"%s",text);//write text to file fclose(fps);//close file move(33, 0);deleteln();//delete line in menu printw("\nFile saved "); return 1; } move(33, 0);deleteln(); printw("\nFile not saved "); //if did not user press y then tell him it's not saved return 0; } fps=fopen(path,"w+"); // file wasn't there open a new file fprintf(fps,"%s",text); //save text to file move(33, 0);deleteln(); //delete line from menu printw("\nFile saved ");//tell user file saved fclose(fps);//close file return 1; }
static void render_tab_bar(int active) { move(0,0); deleteln(); display_logo(); display_tabs(active); refresh(); }
void unlock(void) { addch(' '); move(22, 0); deleteln(); mvprintw(22, 0, "The rusty key creaked in the lock... The key snapped! The door opened...."); }
int main(void) { char Ham1[] = "To be, or not to be: that is the question:\n"; char Ham2[] = "Whether 'tis nobler in the mind to suffer\n"; char Ham3[] = "The slings and arrows of outrageous fortune,\n"; char Ham4[] = "Or to take arms against a sea of troubles,\n"; char Ham5[] = "And by opposing end them?\n"; initscr(); addstr(Ham1); addstr(Ham2); addstr(Ham3); addstr(Ham4); addstr(Ham5); refresh(); getchar(); /* wait for key press */ move(1,0); /* move to the line to delete */ deleteln(); /* Delete and backscroll */ refresh(); getchar(); /* wait for key press */ endwin(); return 0; }
int search_string(char *string, char* data) { int i=0, j=0, start[100], end[100], n_o_occur=0, maxchars=size; char ch='a', s_a='n', st[2]="[", en[2]="]"; move(32,0); if(string==NULL) // if user has not given any string to search then ask him to enter one { s_a='y'; string = malloc(500*sizeof(char)); printw("Enter String to search for: "); while(ch!='\n') { ch=getch(); addch(ch); string[i]=ch;i++; if(ch==127) //bakspace { getyx(stdscr,y,x); move(y,x-1); } if(ch==27 )//escape key) { move(33,0); deleteln(); return 0; } } string[i]='\0'; } int flag=0, len1, len2; len1 = strlen(string); len2 = strlen(data); if (len1 > len2){ // if seach string is greator than data then it's wrong string so exit return 0; } for (i = 0; i <= len2; ++i){ // start loop and traverse thorough data and match string if (string[0]==data[i]){ // if first character of string is found in data flag = 1; //set flag to be one for (j = 1; j<(len1-1) && (i+j)<strlen(data); ++j){ // see if remaning characters are also there then if (string[j]!=data[i+j]){ flag=0; // all characters were not matched set flag to 0 and start searching in next part of data break; } } if((flag==1 || (i+j)>strlen(data)) && s_a=='n') // if user is sarching for a string then just tel positio of first rting { break; } if((flag==1 || (i+j)>strlen(data)) && s_a=='y') // if searching for a string in all data then keep going and insert the search marks [] in text { insert_string(i,st,data); insert_string(i+j+1,en,data);i=i+1; search_marks=true; } } } if (flag==1){ return i; } return -1; }
void input(WINDOW *wnd) { int row=1,col=1,c; curs_set(1); while(1) { move (row,col); c = getch (); switch (c) { case KEY_LEFT: if (col > 1) col--; else flash (); break; case KEY_DOWN: if (row < LINES - 5) row++; else flash (); break; case KEY_UP: if (row > 1) row--; else flash (); break; case KEY_RIGHT: if (col < COLS - 2) col++; else flash (); break; case KEY_BACKSPACE: if(col>1) { mvwprintw(wnd,row,--col,"%c",' '); wrefresh (wnd); } else flash(); break; box(wnd, 0 , 0); case KEY_DL: deleteln (); box(wnd, 0 , 0); break; default: if(c==KEY_F(3)) goto DONE; if(col < COLS - 1) mvwinsch(wnd, row, ++col-1, c); else{ col=0; row++; mvwinsch(wnd, row, ++col, c); } box(wnd,0,0); wrefresh (wnd); break; } } DONE: refresh(); };
int replace_string(char* data) { int pos, i=0, j=0, start[100], end[100],n_o_occur=0, maxchars=size; char string[500], *replace_with, ch='a'; replace_with=malloc(500*sizeof(char)); move(32, 0);deleteln(); printw("Enter String to replace: "); // take the string to replace while(ch!='\n') { ch=getch(); addch(ch); if(ch!='\n'){string[i]=ch;i++;} if(ch==27 )//escape key) { move(33,0); deleteln(); return 0; } } string[i]='\0'; ch='a'; move(33, 0); i=0;printw("Enter replacement string: "); // take the string to replace with while(ch!='\n') { ch=getch(); addch(ch); if(ch!='\n') {replace_with[i]=ch;i++;} if(ch==27 )//escape key) { move(33,0); deleteln(); return 0; } } deleteln(); replace_with[i]='\0'; while((pos=search_string(string, data))!=-1) // now search the string to be replaced and get its position { delete_string(pos, strlen(string), data); // delete the searched string dfrom its position insert_string(pos, replace_with, data); // insert the replacement string at that position } free(replace_with); return 0; }
// SIMPLE TEMPORARY MESSAGE ROUTINE // When you call this by passing a string, it will be stored in the // message buffer until the next draw cycle. The message will be shown // for one turn, then disappear or be replaced with new message. void message(char* msg, bool immediate) { if (immediate) { // write message now! need this for mid-turn prompts like open/close etc. // skips the buffer & will be erased at beginning of next turn move(MESSAGE_POS_Y, MESSAGE_POS_X); deleteln(); deleteln(); deleteln(); insertln(); insertln(); insertln(); addstr(msg); napms(1); } else if (msg == nullptr) { // PRINTS TO SCREEN - ONLY CALLED FROM MAIN LOOP mvaddstr(MESSAGE_POS_Y, MESSAGE_POS_X, msgBuf); // clear message msgBuf[0] = '\0'; } else { strncpy(msgBuf, msg, 80); } }
/* * cl_vi_end -- * Shutdown the vi screen. */ static int cl_vi_end(GS *gp) { CL_PRIVATE *clp; clp = GCLP(gp); /* Restore the cursor keys to normal mode. */ (void)keypad(stdscr, FALSE); /* * If we were running vi when we quit, scroll the screen up a single * line so we don't lose any information. * * Move to the bottom of the window (some endwin implementations don't * do this for you). */ if (!F_ISSET(clp, CL_IN_EX) && !F_ISSET(gp, G_SRESTART)) { (void)move(0, 0); (void)deleteln(); (void)move(LINES - 1, 0); (void)refresh(); } cl_freecap(clp); /* End curses window. */ (void)endwin(); /* Delete curses screen. */ delscreen(clp->screen); /* * XXX * The screen TE sequence just got sent. See the comment in * cl_funcs.c:cl_attr(). */ clp->ti_te = TE_SENT; return (0); }
static void test_insdelln(void) { STATUS st; int n; init_status(stdscr, &st); do { (void) attrset(st.attr | COLOR_PAIR(st.pair)); switch (st.ch) { case 'i': for (n = 0; n < st.count; ++n) insertln(); break; case 'd': for (n = 0; n < st.count; ++n) deleteln(); break; case 'I': insdelln(st.count); break; case 'D': insdelln(-st.count); break; case 'f': fill_window(stdscr); reshow_status(stdscr, &st); break; case 'w': do_subwindow(stdscr, &st, test_winsdelln); break; case 'q': return; default: update_status(stdscr, &st); break; } } while ((st.ch = getch()) != ERR); }
static void msglist_add(const char *msg) { int x, y; getyx(stdscr, y, x); move(0, 0); int n = compute_line(msg); int i; for (i = 0; i < n; ++i) { deleteln(); } int newy = window_y - INPUT_MAXLINE - 2 - n + 1; move(newy, 0); for (i = 0; i < n; ++i) { insertln(); } printw("%s", msg); move(y, x); refresh(); }
void term_scroll_up(int x, int y, int width, int height) /* x and width are ignored. */ /* scroll_up between line y to line y+height-1 */ { #ifdef CURSES term_gotoxy(0, y); deleteln(); term_gotoxy(0, y+height-1); insertln(); #else term_gotoxy(0, y); #ifdef FMR printf("\033R"); #else del_ln(1); #endif FMR term_gotoxy(0, y+height-1); #ifdef FMR printf("\033E"); #else ins_ln(1); #endif #endif }
void edit() { int c; while(1) { move(row, col); refresh(); c = getch(); /* Editor commands */ switch (c) { /* hjkl and arrow keys: move cursor * in direction indicated */ case 'h': case KEY_LEFT: if (col > 0) col--; else flash(); break; case 'j': case KEY_DOWN: if (row < LINES - 1) row++; else flash(); break; case 'k': case KEY_UP: if (row > 0) row--; else flash(); break; case 'l': case KEY_RIGHT: if (col < COLS - 1) col++; else flash(); break; /* i: enter input mode */ case KEY_IC: case 'i': input(); break; /* x: delete current character */ case KEY_DC: case 'x': delch(); break; /* o: open up a new line and enter input mode */ case KEY_IL: case 'o': move(++row, col = 0); insertln(); input(); break; /* d: delete current line */ case KEY_DL: case 'd': deleteln(); break; /* ^L: redraw screen */ case KEY_CLEAR: case CTRL('L'): wrefresh(curscr); break; /* w: write and quit */ case 'w': return; /* q: quit without writing */ case 'q': endwin(); exit(2); default: flash(); break; } } }
/* Prints file selection menu. */ int fileselmenu(int tot, struct finfo *thingie, const char *title) { int a, cpos, k; drawscreen(); xcolor_set(4); mvprintw(2, 5, _("Choose a %s (UP/DOWN/ENTER):"), title); if (tot > 17) for (k = 0; k < 17; k++) mvaddstr(k + 4, 5, thingie[k].descr); else for (k = 0; k < tot; k++) mvaddstr(k + 4, 5, thingie[k].descr); cpos = k = 0; mvaddstr(4, 1, "->"); for (a = 1; a;) { /* * And thanks for xmunkki, he made my day and * "solved" this, what i didn't get right (or did * but i used 2 extra variables. :) */ switch (getch()) { case 27: return -1; break; case KEY_UP: case 'k': if (k <= 0) break; k--; if (cpos > 0) { cpos--; break; } move(20, 5); deleteln(); move(4, 5); insertln(); mvaddstr(4, 5, thingie[k].descr); break; case KEY_DOWN: case 'j': if (k > tot - 2) break; k++; if (cpos < 16 && cpos < tot - 1) { cpos++; break; } move(4, 5); deleteln(); move(20, 5); insertln(); mvaddstr(20, 5, thingie[k].descr); break; case ' ': case '\n': a = 0; break; default: break; } mvaddstr(cpos + 3, 1, " "); mvaddstr(cpos + 5, 1, " "); mvaddstr(cpos + 4, 1, "->"); } return k; }
int process_key_up(int y, int x, int level, int have_key) { int wall, c; move(--y, x); // Move the cursor up by one switch(wall = inch()) { case '#' : move(++y, x); break; case '+' : { if(have_key == 1) { unlock(); have_key--; move(++y, x); } else { move(22, 0); deleteln(); mvprintw(22, 0, "This door appears to be locked.... You need a key."); move(++y, x); } break; } case ' ' : { move(++y, x); addch(' '); move(--y, x); addch('@'); move(++y, x); break; } case 'o' : { level_completed(level++); switch(c = getch()) { case 'c' : draw_maze(++level); break; // Continue and render next level. case 'q' : endwin(); return 0; break; // Close ncurses window } break; } case 'E' : { have_key++; move(22, 0); deleteln(); mvprintw(22, 0, "You picked up a rusty key...."); move(++y, x); addch(' '); move(--y, x); addch('@'); move(y, x); break; } } refresh(); return(y); return(x); }
cell pp_curs_deleteln(cell x) { if (!Running) return UNSPECIFIC; deleteln(); return UNSPECIFIC; }
/* * main: Start listening to keyboard and mouse events, and copying them to the serial port */ int main(void) { // The terminal window. Note that mouse tracking only works if the terminal is running inside a GUI (e.g. LXDE) WINDOW * terminalWindow; // The key code as an integer int keyCode; // The previous mouse coordinates int lastXValue = 0; int lastYValue = 0; // The current mouse coordinates int xValue = 0; int yValue = 0; // The character used is calculated from the difference of values int xChar = 0; int yChar = 0; // Change this to make the mouse move faster or slower int trackingSpeed = 10; // This is part of the mouse tracking protocol, which currently uses characters int characterOffset = 64; // Mouse mask mmask_t old; // The character to send to the serial port char* thisCharacter; // Open the serial port int serialPort = open(SERIAL1, O_RDWR | O_NOCTTY | O_NDELAY); // The serial port file reference that we write characters to FILE *serialPortFile; // The log file for saving a local keystroke log FILE *logFile; // Open the serial port file for writing serialPortFile = fopen(SERIAL1, "w"); // Open the log file for writing logFile = fopen("log.txt", "w"); // Initialize ncurses if ( (terminalWindow = initscr()) == NULL ) { fprintf(stderr, "Error initializing ncurses.\n"); exit(EXIT_FAILURE); } // end if ncurses can't be initalised // Turn off key echoing noecho(); // Enable the keypad for non-char keys keypad(terminalWindow, TRUE); cbreak(); // Capture mouse events too mousemask (ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, &old); // Print a prompt and refresh() the screen mvaddstr(5, 10, "Press a key ('q' to quit)..."); mvprintw(7, 10, "You pressed: "); refresh(); // Capture new inputs from the keyboard or mouse until user presses 'q' while ( (keyCode = getch()) != 'q' ) { // If it was a mouse event if (keyCode == KEY_MOUSE) { // The mouse event MEVENT event; //assert (getmouse(&event) == OK); //mvprintw (0,0,"Mouse Event!\n"); // If the mouse event can be read if( getmouse( &event ) == OK ) { // Left mouse click if (event.bstate & BUTTON1_CLICKED) { // Show visual feedback about the left mouse click mvprintw(7, 10, "You clicked the mouse"); // Send 'b' (button) to the serial port fprintf(serialPortFile, "%s", "b"); fflush(serialPortFile); // Send 'l' (left click) to the serial port fprintf(serialPortFile, "%s", "l"); fflush(serialPortFile); } else { // Right mouse click if (event.bstate & BUTTON3_CLICKED) { // Show visual feedback about the right mouse click mvprintw(7, 10, "You right clicked the mouse"); // Send 'b' (button) to the serial port fprintf(serialPortFile, "%s", "b"); fflush(serialPortFile); // Send 'r' (right click) to the serial port fprintf(serialPortFile, "%s", "r"); fflush(serialPortFile); } else { // Read the mouse location and scale by the tracking speed xValue = event.x * trackingSpeed; yValue = event.y * trackingSpeed; // Calculate the difference from the last value xChar = lastXValue - xValue + characterOffset; yChar = lastYValue - yValue + characterOffset; // Show visual feedback about the mouse movement mvprintw(7, 10, "You moved the mouse %d %d", xChar, yChar); // Send 'x' (horizontal) to the serial port fprintf(serialPortFile, "%s", "x"); fflush(serialPortFile); // Send the x coordinate character to the serial port fprintf(serialPortFile, "%c", xChar); fflush(serialPortFile); // Send 'y' (vertical) to the serial port fprintf(serialPortFile, "%s", "y"); fflush(serialPortFile); // Send the y coordinate character to the serial port fprintf(serialPortFile, "%c", yChar); fflush(serialPortFile); // Send '?' (terminator) to the serial port fprintf(serialPortFile, "%s", "?"); fflush(serialPortFile); // Update the last value variable lastXValue = xValue; lastYValue = yValue; } // end if right click or movement } // end if left click } // end if mouse event } else { /* Delete the old response line, and print a new one */ deleteln(); thisCharacter = keyCodeToString(keyCode); //write(serialPort, thisCharacter, 2); fprintf(serialPortFile, "%s", "k"); fflush(serialPortFile); fprintf(serialPortFile, "%s", thisCharacter); fflush(serialPortFile); fprintf(logFile, "%s", thisCharacter); fflush(logFile); mvprintw(7, 10, "You pressed: 0x%x (%s)", keyCode, thisCharacter); refresh(); } // end if mouse or keyboard } // end while key pressed isn't 'q' // Clean up the GUI delwin(terminalWindow); endwin(); refresh(); // Close the serial port close (serialPort); // Quit return EXIT_SUCCESS; } // end function main