static void really_define_key(WINDOW *win, const char *new_string, int code) { int rc; const char *code_name = keyname(code); char *old_string; char *vis_string = 0; char temp[80]; if (code_name == 0) { sprintf(temp, "Keycode %d", code); code_name = temp; } if ((old_string = keybound(code, 0)) != 0) { wprintw(win, "%s is %s\n", code_name, vis_string = visible(old_string)); } else { wprintw(win, "%s is not bound\n", code_name); } log_last_line(win); if (vis_string != 0) { free(vis_string); vis_string = 0; } vis_string = visible(new_string); if ((rc = key_defined(new_string)) > 0) { wprintw(win, "%s was bound to %s\n", vis_string, keyname(rc)); log_last_line(win); } else if (new_string != 0 && rc < 0) { wprintw(win, "%s conflicts with longer strings\n", vis_string); log_last_line(win); } rc = define_key(new_string, code); if (rc == ERR) { wprintw(win, "%s unchanged\n", code_name); log_last_line(win); } else if (new_string != 0) { wprintw(win, "%s is now bound to %s\n", vis_string, code_name); log_last_line(win); } else if (old_string != 0) { wprintw(win, "%s deleted\n", code_name); log_last_line(win); } if (vis_string != 0) free(vis_string); if (old_string != 0) free(old_string); }
NCURSES_EXPORT(NCURSES_CONST char *) _nc_keyname (SCREEN *sp, int c) { int i; char name[20]; char *p; NCURSES_CONST char *result = 0; if (c == -1) { result = "-1"; } else { for (i = 0; _nc_key_names[i].offset != -1; i++) { if (_nc_key_names[i].code == c) { result = (NCURSES_CONST char *)key_names + _nc_key_names[i].offset; break; } } if (result == 0 && (c >= 0 && c < SIZEOF_TABLE)) { if (MyTable == 0) MyTable = typeCalloc(char *, SIZEOF_TABLE); if (MyTable != 0) { if (MyTable[c] == 0) { int cc = c; p = name; if (cc >= 128 && (sp == 0 || sp->_use_meta)) { strcpy(p, "M-"); p += 2; cc -= 128; } if (cc < 32) sprintf(p, "^%c", cc + '@'); else if (cc == 127) strcpy(p, "^?"); else sprintf(p, "%c", cc); MyTable[c] = strdup(name); } result = MyTable[c]; } #if NCURSES_EXT_FUNCS && NCURSES_XNAMES } else if (result == 0 && cur_term != 0) { int j, k; char * bound; TERMTYPE *tp = &(cur_term->type); int save_trace = _nc_tracing; _nc_tracing = 0; /* prevent recursion via keybound() */ for (j = 0; (bound = keybound(c, j)) != 0; ++j) { for(k = STRCOUNT; k < (int) NUM_STRINGS(tp); k++) { if (tp->Strings[k] != 0 && !strcmp(bound, tp->Strings[k])) { result = ExtStrname(tp, k, strnames); break; } } free(bound); if (result != 0) break; } _nc_tracing = save_trace; #endif } }
int main() { int i, j, k, screenHeight, screenWidth; int input; unsigned int ch; char *boundString; int ch2; char inputBuffer[40]; int terminalIndex, fdMax; fd_set fileDescriptors; struct timespec timeout; //struct tesiObject *to; struct virtualTerminal *vt; for(i = 0; i < 10; i++) virtualTerminals[i] = NULL; fdMax = 0; timeout.tv_sec = 0; timeout.tv_nsec = 50000000; // 10E-9 #ifdef USE_NCURSES ncursesScreen = initscr(); if(ncursesScreen == NULL) { perror("Error initializing nCurses\n"); exit(1); } if(has_colors()) { start_color(); #ifdef DEBUG fprintf(stderr, "max colors: %d\n", COLORS); fprintf(stderr, "max color pairs: %d\n", COLOR_PAIRS); #endif k = 1; for(i = 0; i < COLORS; i++) { for(j = 0; j < COLORS; j++) { vt_colors[i][j] = k; init_pair(k, i, j); k++; } } //init all color pairs /* * black red green yellow blue magenta cyan white * attributes 30-37 * in iterm, black starts at 1 * */ } keypad(ncursesScreen, true); // cause nCurses to package key combos into special, single values nodelay(ncursesScreen, TRUE); // return immediately if no input is waiting raw(); noecho(); // don't echo input refresh(); // clear the main window // Get main window dimensions. This will be used when creating additional virtual terminals getmaxyx(ncursesScreen, screenHeight, screenWidth); #endif inputBuffer[0] = 0; mvwaddstr(ncursesScreen, screenHeight - 1, 0, ":"); mvwaddstr(ncursesScreen, 1, 0, "USING KNOX\n\nCommands\n\t\"create\" - creates a new Virtual Terminal\n\t\"create #\" - create # number of new Virtual Terminals\n\t\"NUMBER\" - sets focus to so numbered terminal\n\t\"NUMBER COMMAND\" - sends COMMAND to numbered terminal followed by newline"); wmove(ncursesScreen, screenHeight - 1, 1); terminalIndex = -1; keepRunning = 1; k = 0; while(keepRunning) { FD_ZERO(&fileDescriptors); for(i = 0; i < 10; i++) { //if(vtGet(i) != NULL && tesi_handleInput(vtGet(i))) { vt = vtGet(i); if(vt && vt->fd != -1) { if(vt->fd > fdMax) fdMax = vt->fd; FD_SET(vt->fd, &fileDescriptors); } } pselect(fdMax + 1, &fileDescriptors, NULL, NULL, &timeout, NULL); j = 0; for(i = 0; i < 10; i++) { //if(vtGet(i) != NULL && tesi_handleInput(vtGet(i))) { vt = vtGet(i); if(vt != NULL && vt->fd != -1 && FD_ISSET(vt->fd, &fileDescriptors)) { VTCore_dispatch(vt->core); #ifdef USE_NCURSES //vt = (struct virtualTerminal*) vtGet(i)->pointer; wnoutrefresh(vt->window); #endif j++; // keep track of the terminals that need updating } } if(j || k) { // if a VT or command window needs updating #ifdef USE_NCURSES // re-move cursor to correct location after updating screens if(terminalIndex > -1) { //to = (struct tesiObject*) vtGet(terminalIndex); //vt = (struct virtualTerminal*) to->pointer; vt = vtGet(terminalIndex); //wmove(vt->window, to->y, to->x); } else { wmove(ncursesScreen, screenHeight - 1, 1 + strlen(inputBuffer)); } doupdate(); } k = 0; ch = wgetch(ncursesScreen); // ? #endif #ifdef USE_SLANG SLsmg_refresh(); if(!SLang_input_pending(1)) // wait 1/10 of a second continue; ch = SLang_getkey(); #endif #ifdef USE_NCURSES #endif switch(ch) { case '`': // tilde pressed, cycle through terms? //case KEY_RIGHT: // tilde pressed, cycle through terms? terminalIndex++; if(terminalIndex == 10 || vtGet(terminalIndex) == NULL) terminalIndex = -1; vtHighlight(terminalIndex); k = 1; // update cursor position break; case ERR: // no input break; default: if(terminalIndex > -1) { // send input to terminal //to = vtGet(terminalIndex); vt = vtGet(terminalIndex); if(vt) { // this should never be null, but check anyway boundString = keybound(ch, 0); if(boundString) { #ifdef DEBUG fprintf(stderr, "key string: %s\n", boundString); #endif write(vt->fd, boundString, strlen(boundString)); free(boundString); } else write(vt->fd, &ch, 1); } } else { // build input buffer #ifdef DEBUG fprintf(stderr, "Keypress: %d\n", ch); #endif if(ch == 10) { // parse buffer when Enter is pressed, returns active terminal index //wclear(ncursesScreen); terminalIndex = processInput(inputBuffer, terminalIndex, screenHeight, screenWidth); vtHighlight(terminalIndex); FD_ZERO(&fileDescriptors); FD_SET(0, &fileDescriptors); #ifdef USE_NCURSES // clear command window mvwaddch(ncursesScreen, screenHeight - 1, 0, ':'); wmove(ncursesScreen, screenHeight - 1, 1); wclrtoeol(ncursesScreen); wmove(ncursesScreen, screenHeight - 1, 1); wnoutrefresh(ncursesScreen); k = 1; #endif inputBuffer[0] = 0; } else { i = strlen(inputBuffer); inputBuffer[ i ] = ch; inputBuffer[ i + 1 ] = 0; #ifdef USE_NCURSES mvwaddstr(ncursesScreen, screenHeight - 1, 1, inputBuffer); #endif #ifdef USE_SLANG SLsmg_gotorc(SLtt_Screen_Rows - 1, 0); SLsmg_write_string(inputBuffer); SLsmg_refresh(); #endif } } break; } } for(i = 0; i < 10; i++) { if(virtualTerminals[i] != NULL) { vtDestroy(i); } } #ifdef USE_NCURSES endwin(); #endif #ifdef USE_SLANG SLsmg_reset_smg(); SLang_reset_tty(); #endif return 0; }