/*----------------------------------------------------------------------*\ * Function: unsigned int _pSLsys_getkey (void); * * wait for and get the next available keystroke. * Also re-maps some useful keystrokes. * * Backspace (^H) => Del (127) * Ctrl-Space => ^@ (^@^3 - a pc NUL char) * extended keys are prefixed by a null character \*----------------------------------------------------------------------*/ unsigned int _pSLsys_getkey (void) { /* Check the input buffer because _pSLsys_input_pending may have been * called prior to this to stuff the input buffer. */ if (SLang_Input_Buffer_Len) return SLang_getkey (); if (SLw32_Hstdin == INVALID_HANDLE_VALUE) return SLANG_GETKEY_ERROR; while (1) { int status; if (SLKeyBoard_Quit) return SLang_Abort_Char; status = _pSLsys_input_pending (600); if (status == -1) return SLANG_GETKEY_ERROR; if (status > 0) return SLang_getkey (); } }
int main(void) { struct winsize ws; /* get the size of the terminal connected to stdout */ if (ioctl(1, TIOCGWINSZ, &ws)) { perror("failed to get window size"); return 1; } SLtt_get_terminfo(); SLtt_Screen_Rows = ws.ws_row; SLtt_Screen_Cols = ws.ws_col; SLsmg_init_smg(); SLang_init_tty(-1, 0, 1); drawCharSet(0, 0, "Normal Character Set"); drawCharSet(40, 1, "Alternate Character Set"); SLsmg_refresh(); SLang_getkey(); SLsmg_gotorc(SLtt_Screen_Rows - 1, 0); SLsmg_refresh(); SLsmg_reset_smg(); SLang_reset_tty(); return 0; }
/* non blocking getkey */ static int getkey_handler() { unsigned int ch = 0; if(SLang_input_pending(1)) // return SLang_getkey(); ch = SLang_getkey(); if(ch) func("getkey_handler detected char %u",ch); return ch; }
static int getkey() { int c; while ((c = SLang_getkey()) == '\xC') { /* if Ctrl-L redraw whole screen */ SLsmg_touch_lines(0, SLtt_Screen_Rows); SLsmg_refresh(); } return c; }
/* * Process an event, wait if requested */ static errr Term_xtra_sla_event(int v) { /* Do not wait unless requested */ if (!v && (SLang_input_pending(0) == 0)) return (1); /* Get and enqueue the key */ Term_keypress(SLang_getkey ()); /* Success */ return 0; }
static int getkey_function (void) { int ch; ch = SLang_getkey (); if (ch != SLANG_GETKEY_ERROR) { *Keyboard_Buffer_Stop++ = (unsigned char) ch; if (Keyboard_Buffer_Stop == Keyboard_Buffer + sizeof (Keyboard_Buffer)) Keyboard_Buffer_Stop = Keyboard_Buffer; } return ch; }
/* pending getkey request */ sl_ready_input(int port, int fd) { unsigned int key; driver_select(port, 0, DO_READ, 0); switch (wait_for) { case GETKEY: { key = SLang_getkey (); return ret_int(port, key); } case KP_GETKEY: { key = SLkp_getkey (); return ret_int(port, key); } return 0; } }
int SLcurses_wgetch (SLcurses_Window_Type *w) { if (w == NULL) return ERR; SLcurses_wrefresh (w); if ((Keyboard_Buffer_Start != Keyboard_Buffer_Stop) || (w->delay_off == -1) || SLang_input_pending (w->delay_off)) { if (w->use_keypad) return get_keypad_key (); return SLang_getkey (); } return ERR; }
int tty_lowlevel_getch (void) { int c; if (no_slang_delay && (SLang_input_pending (0) == 0)) return -1; c = SLang_getkey (); if (c == SLANG_GETKEY_ERROR) { fprintf (stderr, "SLang_getkey returned SLANG_GETKEY_ERROR\n" "Assuming EOF on stdin and exiting\n"); exit (EXIT_FAILURE); } return c; }
int ui__getch(int delay_secs) { struct timeval timeout, *ptimeout = delay_secs ? &timeout : NULL; fd_set read_set; int err, key; ui__setup_sigwinch(); FD_ZERO(&read_set); FD_SET(0, &read_set); if (delay_secs) { timeout.tv_sec = delay_secs; timeout.tv_usec = 0; } err = select(1, &read_set, NULL, NULL, ptimeout); if (err == 0) return K_TIMER; if (err == -1) { if (errno == EINTR) return K_RESIZE; return K_ERROR; } key = SLang_getkey(); if (key != K_ESC) return key; FD_ZERO(&read_set); FD_SET(0, &read_set); timeout.tv_sec = 0; timeout.tv_usec = 20; err = select(1, &read_set, NULL, NULL, &timeout); if (err == 0) return K_ESC; SLang_ungetkey(key); return SLkp_getkey(); }
static int ctl_read(int32 *valp) { int c; if (!SLang_input_pending(0)) return RC_NONE; c=SLang_getkey(); switch(c) { case 'h': case '?': ctl_help_mode(); return RC_NONE; case 'V': *valp=10; return RC_CHANGE_VOLUME; case 'v': *valp=-10; return RC_CHANGE_VOLUME; case 'q': return RC_QUIT; case 'n': return RC_NEXT; case 'p': return RC_REALLY_PREVIOUS; case 'r': return RC_RESTART; case 'f': *valp=play_mode->rate; return RC_FORWARD; case 'b': *valp=play_mode->rate; return RC_BACK; case 's': return RC_TOGGLE_PAUSE; } return RC_NONE; }
static int get_keypad_key (void) { int ch; if (Keyboard_Buffer_Stop != Keyboard_Buffer_Start) return get_buffered_key (); ch = SLang_getkey (); if (ch == '\033') { if (0 == SLang_input_pending (ESCDELAY / 100)) return ch; } else if (ch == SLANG_GETKEY_ERROR) return ERR; SLang_ungetkey (ch); ch = SLkp_getkey (); if (ch == SL_KEY_ERR) ch = get_buffered_key (); else Keyboard_Buffer_Stop = Keyboard_Buffer_Start; return ch; }
int main(void) { int i; char * buf; SLtt_get_terminfo(); SLang_init_tty(4, 0, 0); buf = SLtt_tgetstr("ku"); if (!buf) { printf("termcap entry not found for kl\n\r"); } else { printf("termcap entry found for kl: %s", buf); while (*buf) { printf("0x%02x ", *buf++); } printf("\n\r"); } printf("\n\r"); printf("Press a key: "); fflush(stdout); SLang_input_pending(50); printf("\n\r"); printf("You pressed: "); while (SLang_input_pending(1)) { i = SLang_getkey(); printf("0x%02x ", i); } printf("\n\r"); SLang_reset_tty(); return 0; }
int main(void) { SLtt_get_terminfo(); SLtt_Use_Ansi_Colors = 1; SLsmg_init_smg(); SLang_init_tty(4, 0, 0); SLsmg_cls(); printall(0); SLsmg_set_char_set(1); printall(40); SLsmg_refresh(); SLang_getkey(); SLsmg_gotorc(SLtt_Screen_Rows - 1, 0); SLsmg_refresh(); SLsmg_reset_smg(); SLang_reset_tty(); return 0; }
void newtFlushInput(void) { while (SLang_input_pending(0)) { SLang_getkey(); } }
int main() { int i, j; int keepRunning; int input; unsigned int ch; int ch2; char inputBuffer[40]; int terminalIndex; struct tesiObject *to; for(i = 0; i < 10; i++) tesiObjects[i] = NULL; SLtt_get_terminfo(); SLang_init_tty(-1, 0, 0); SLsmg_init_smg(); SLsmg_refresh(); inputBuffer[0] = 0; terminalIndex = -1; keepRunning = 1; while(keepRunning) { j = 0; for(i = 0; i < 10; i++) { if(tesiObjects[i] != NULL) j += tesi_handleInput(tesiObjects[i]); } if(j) SLsmg_refresh(); if(!SLang_input_pending(1)) // wait 1/10 of a second continue; ch = SLang_getkey(); switch(ch) { case '`': // tilde pressed, cycle through terms? terminalIndex++; if(terminalIndex == 10 || vtGet(terminalIndex) == NULL) terminalIndex = -1; // highlight selected terminal break; case 'Q': keepRunning = 0; break; default: if(terminalIndex > -1) { // send input to terminal to = vtGet(terminalIndex); if(to) // this should never be null, but check anyway write(to->fd_input, &ch, 1); } else { // build input buffer #ifdef DEBUG fprintf(stderr, "Keypress: %d\n", ch); #endif if(ch == 13) { // parse buffer when Enter is pressed // new terminal processInput(inputBuffer); inputBuffer[0] = 0; } else { i = strlen(inputBuffer); inputBuffer[ i ] = ch; inputBuffer[ i + 1 ] = 0; SLsmg_gotorc(SLtt_Screen_Rows - 1, 0); SLsmg_write_string(inputBuffer); SLsmg_refresh(); } } break; } } for(i = 0; i < 10; i++) { if(tesiObjects[i] != NULL) { vtDestroy(i); } } SLsmg_reset_smg(); SLang_reset_tty(); return 0; }
void newtWaitForKey(void) { newtRefresh(); SLang_getkey(); newtClearKeyBuffer(); }
void newtClearKeyBuffer(void) { while (SLang_input_pending(1)) { SLang_getkey(); } }
int main() { TerminalIO *tio; VTCore *vt; VTScreenView *view; char *argv[] = {"/bin/bash", 0}; //char input[65]; unsigned int input; char ch[2]; int fd; fd_set fileDescriptors; struct timeval timeout; // Initialize Terminal SLtt_get_terminfo (); SLang_init_tty (-1, 0, 0); SLsmg_init_smg (); SLsmg_refresh(); // iTerm Setup // new iTerm TerminalIO object tio = TtyTerminalIO_new(SLtt_Screen_Cols, SLtt_Screen_Rows, "/bin/bash", argv); // width, height, program, argv if(tio == NULL) { printf("Error initializing TerminalIO\n"); return 1; } // new Virtual Terminal Screen View object view = malloc (sizeof (VTScreenView)); if (view == NULL) { perror ("VTScreenView_new"); return 1; } VTScreenView_init (view); view->draw_text = vtDrawText; view->update_cursor_position = vtMoveCursor; view->clear_rect = vtClearRect; view->scroll_view = vtScroll; // Start the iTerm core vt = VTCore_new(tio, SLtt_Screen_Cols, SLtt_Screen_Rows, 10); // terminalIO, cols, rows, history if(vt == NULL) { printf("Error initializing VTCore\n"); return 1; } // Tell the core with view to use ... can we have multiple? VTCore_set_screen_view(vt, view); timeout.tv_sec = 0; timeout.tv_usec = 100000; fd = TtyTerminalIO_get_associated_fd(tio); while(1) { FD_ZERO(&fileDescriptors); FD_SET(0, &fileDescriptors); FD_SET( TtyTerminalIO_get_associated_fd(tio), &fileDescriptors); timeout.tv_sec = 0; timeout.tv_usec = 100000; select(fd + 1, &fileDescriptors, NULL, NULL, &timeout); if(FD_ISSET(fd, &fileDescriptors)) { VTCore_dispatch(vt); SLsmg_refresh(); } if(FD_ISSET(0, &fileDescriptors)) { input = SLang_getkey(); //printf("input: %c\n", (char)input); ch[0] = (char) input; ch[1] = 0; write(fd, &ch, 1); if(ch[0] == 'Q') break; } } free(view); VTCore_destroy(vt); TtyTerminalIO_destroy(tio); // Shut down s-lang IO SLsmg_reset_smg (); SLang_reset_tty (); return 0; }
/* * Run a command from the menus. */ void RunCommand (Menu_Item_Type *i) { char *command; int must_redraw=0; Conditional_String *cs=NULL; Window_List_Type *this_window=CurrentWindow; if (i->command[0] != '\0') { /* don't try to run a null command */ if (i->edit_flag) { /* edit command on fly */ cs=EditTags(i->command); if (cs->ignore) { /* user hit escape */ free(cs->value); free(cs); return; } else { /* user hit enter */ command=malloc(strlen(cs->value)+1); strcpy(command,cs->value); free(cs->value); free(cs); } } else { /* don't edit command on fly */ command=malloc(strlen(i->command)+1); strcpy(command,i->command); } #ifdef SETENV_FLAG_OK if (i->setenv_flag) { /* a setenv command */ RunSetenv(command); } else #endif if (i->makemenu_flag) { /* process command output as rc file */ ReadRc(command,RC_PREPROC); SanityCheckMenus(); /* * make sure that all modified menus currently on screen * get recalced. */ while (this_window) { if (this_window->menu->recalc) { CalcMenu(this_window->menu); must_redraw=1; } this_window=this_window->last; } if (must_redraw) DrawAll(); } else if (i->truncate_flag) { /* display in a window and truncate */ RunShow(i->text,command,1); } else if (i->display_flag) { /* display in a window and wrap. */ RunShow(i->text,command,0); } else { /* normal display */ if (! i->noclear_flag) { /* clear screen */ SLsmg_cls(); SLsmg_normal_video(); Screen_Reset(); #ifdef GPM_SUPPORT EndMouse(); /* return to normal GPM/selection mode */ #endif } /* * This is what the whole pdmenu program comes down to. * The rest is fluff. ;-) */ /* * start: Steve Blott ([email protected]) * * add capability to exec() (rather than system()) a command, thereby * replacing the current process; if the first word of command is * "exec", then exec() it, otherwise system() it */ char *cp = command; while (isspace(cp[0])) cp++; if ( strncmp(cp, "exec", 4) == 0 && isspace(cp[4]) ) { char *cv[4]; /* command vector */ cv[0] = "sh"; cv[1] = "-c"; cv[2] = cp; cv[3] = 0; execvp(cv[0],cv); /* should not reach here; if the execvp fails, then pdmenu will * continue to run, silently ignoring the failure; if the execvp * succeeds but the subsequent exec fails, then pdmenu will silently * disappear (its process no longer exists), and no feedback will be * received */ } else system(command); if (! i->noclear_flag) { /* redraw screen */ Screen_Init(); if (i->pause_flag) { /* pause 1st */ printf("\n%s",_("Press Enter to return to Pdmenu.")); fflush(stdout); /* make sure above is displayed. */ /* Now wait for the keypress. */ while (1) { int k; k = SLang_getkey(); if (k == '\n' || k == '\r') { break; } } SLang_flush_input(); /* kill any buffered input */ printf("\n"); } #ifdef GPM_SUPPORT gpm_ok=InitMouse(); /* grab mouse pointer again. */ #endif /* * we need to account for the screen size changing behind our backs * while the program was running. */ SetScreensize(); Resize_Screen(); DrawAll(); } } free(command); } }
/* this is the main key processing loop for interactive mode */ void display_results_interactively() { bool_t done = 0; int c, c1; /* set up the terminal etc. */ SLang_init_tty(-1, 0, 0); SLtt_get_terminfo(); if(-1 == SLsmg_init_smg() ) { fprintf (stderr, "Unable to initialize terminal."); return; } SLkp_init(); /* for cursor keys */ SLsignal(SIGWINCH, sigwinch_handler); init_disp_state(); while(!done) { redraw_current_state(); c = SLang_getkey(); /* if it's a meta combination, translate */ if( c == '' ) { c1 = SLang_getkey(); switch(c1) { case '<': c = SL_KEY_HOME; break; case '>': c = SL_KEY_END; break; case 'v': case 'V': c = SL_KEY_PPAGE; break; case 'r': case 'R': c = '?'; break; case 's': case 'S': c = '/'; break; default: /* could be special cursor keys */ SLang_ungetkey(c1); SLang_ungetkey(c); c = SLkp_getkey(); } } /* handle key press */ switch(c) { case 'q': case 'Q': done = 1; break; case 'c': interactive_categorize(); disp.highlighted = disp.first_visible = 0; break; case '\r': case 'v': if( emails.num_limited > 0 ) { view_current_email(); } break; case SL_KEY_F(1): if( emails.num_limited > 0 && disp.fkey_cmd[0] ) { interactive_pipe_all_tagged(disp.fkey_cmd[0]); } break; case SL_KEY_F(2): if( emails.num_limited > 0 && disp.fkey_cmd[1] ) { interactive_pipe_all_tagged(disp.fkey_cmd[1]); } break; case SL_KEY_F(3): if( emails.num_limited > 0 && disp.fkey_cmd[2] ) { interactive_pipe_all_tagged(disp.fkey_cmd[2]); } break; case SL_KEY_F(4): if( emails.num_limited > 0 && disp.fkey_cmd[3] ) { interactive_pipe_all_tagged(disp.fkey_cmd[3]); } break; case SL_KEY_F(5): if( emails.num_limited > 0 && disp.fkey_cmd[4] ) { interactive_pipe_all_tagged(disp.fkey_cmd[4]); } break; case SL_KEY_F(6): if( emails.num_limited > 0 && disp.fkey_cmd[5] ) { interactive_pipe_all_tagged(disp.fkey_cmd[5]); } break; case SL_KEY_F(7): if( emails.num_limited > 0 && disp.fkey_cmd[6] ) { interactive_pipe_all_tagged(disp.fkey_cmd[6]); } break; case SL_KEY_F(8): if( emails.num_limited > 0 && disp.fkey_cmd[7] ) { interactive_pipe_all_tagged(disp.fkey_cmd[7]); } break; case SL_KEY_F(9): if( emails.num_limited > 0 && disp.fkey_cmd[8] ) { interactive_pipe_all_tagged(disp.fkey_cmd[8]); } break; case SL_KEY_F(10): if( emails.num_limited > 0 && disp.fkey_cmd[9] ) { interactive_pipe_all_tagged(disp.fkey_cmd[9]); } break; case 's': if( emails.num_limited > 0 ) { interactive_pipe_current(""); } break; case 'S': if( emails.num_limited > 0 ) { interactive_pipe_all_tagged(""); } break; case 'o': if( ++emails.score_type >= MAX_SCORES ) { emails.score_type = 0; } qsort(emails.list, emails.num_emails, sizeof(mbox_item), compare_scores); recalculate_limited(); break; case 'p': if( ++emails.index_format >= MAX_FORMATS ) { emails.index_format = 0; } break; case 't': if( emails.num_limited > 0 ) { tag_current(1); } break; case 'T': if( emails.num_limited > 0 ) { tag_all_limited(1); } break; case 'u': if( emails.num_limited > 0 ) { tag_current(0); } break; case 'U': if( emails.num_limited > 0 ) { tag_all_limited(0); } break; case 'z': reverse_sort(); recalculate_limited(); break; case 'G': case SL_KEY_END: if( emails.num_limited > 0 ) { disp.first_visible = emails.num_limited - 1; } else { disp.highlighted = disp.first_visible = 0; } break; case '1': case SL_KEY_HOME: disp.first_visible = 0; break; case '': case SL_KEY_PPAGE: if( disp.first_visible > disp.num_rows ) { disp.first_visible -= disp.num_rows; } else { disp.first_visible = 0; disp.highlighted = 0; } /* assert emails.num_limited >= disp.first_visible */ if( disp.highlighted > (emails.num_limited - disp.first_visible) ) { disp.highlighted = (emails.num_limited - disp.first_visible); } break; case 'k': case '': case SL_KEY_UP: if( disp.highlighted > 0 ) { disp.highlighted--; } else { if( disp.first_visible > 1 ) { disp.first_visible -= 1; } else { disp.first_visible = 0; } } break; case 'j': case '': case SL_KEY_DOWN: if( emails.num_limited > 0 ) { if( disp.highlighted < (emails.num_limited - disp.first_visible - 1) ) { if( disp.highlighted < (disp.num_rows - 3) ) { disp.highlighted++; } else { if( (disp.first_visible += 1) >= emails.num_limited ) { disp.first_visible = emails.num_limited - 1; } } } } else { disp.highlighted = disp.first_visible = 0; } break; case '': case '': case ' ': case SL_KEY_NPAGE: if( emails.num_limited > 0 ) { if( (disp.first_visible += disp.num_rows) >= emails.num_limited ) { disp.first_visible = emails.num_limited - 1; } if( disp.highlighted > (emails.num_limited - disp.first_visible) ) { disp.highlighted = (emails.num_limited - disp.first_visible) - 1; } } else { disp.highlighted = disp.first_visible = 0; } break; case '?': interactive_search(0, ""); disp.highlighted = disp.first_visible = 0; break; case '/': interactive_search(1, ""); disp.highlighted = disp.first_visible = 0; break; default: break; } } /* we're done */ SLsmg_reset_smg(); SLang_reset_tty(); }
static int sl_output(int port, char *buf, int len) { int x,y,z,v,w; char *str, *t1, *t2, *t3; int ret; char ch; /* check for signals */ if (signal_cought != 0) { /* async out signal */ char xxx[5]; xxx[0] = 0; put_int32(signal_cought, xxx+1); driver_output(port, xxx, 5); signal_cought = 0; } switch (*buf++) { case INIT_TTY: { int abort_char, flow_ctl, opost; abort_char = get_int32(buf); buf+=4; flow_ctl = get_int32(buf); buf+= 4; opost = get_int32(buf); buf+= 4; ret = SLang_init_tty (abort_char,flow_ctl, opost); return ret_int(port, ret); } case SET_ABORT_FUNCTION: { SLang_set_abort_signal (NULL); return ret_int(port, 0); } case GETKEY: { unsigned int key; if (SLang_input_pending (0) == 0) { wait_for = GETKEY; driver_select(port, 0, DO_READ, 1); return 0; } x = SLang_getkey (); return ret_int(port, x); } /* read a symbol */ case KP_GETKEY: { if (SLang_input_pending (0) == 0) { wait_for = KP_GETKEY; driver_select(port, 0, DO_READ, 1); return 0; } x = SLkp_getkey (); return ret_int(port, x); } case UNGETKEY: { unsigned char key = (unsigned char) *buf; SLang_ungetkey (key); return 0; } case RESET_TTY: { SLang_reset_tty(); return 0; } case KP_INIT: { return ret_int(port, SLkp_init ()); } case SETVAR: { x = get_int32(buf);buf+= 4; y = get_int32(buf); switch (x) { case esl_baud_rate: SLang_TT_Baud_Rate = y; return 0; case esl_read_fd: return 0; case esl_abort_char: SLang_Abort_Char = y; return 0; case esl_ignore_user_abort: SLang_Ignore_User_Abort=y; return 0; case esl_input_buffer_len : SLang_Input_Buffer_Len=y; return 0; case esl_keyboard_quit: SLKeyBoard_Quit=y; return 0; case esl_last_key_char: SLang_Last_Key_Char=y; return 0; case esl_rl_eof_char: SLang_RL_EOF_Char=y; return 0; case esl_rline_quit: SLang_Rline_Quit=y; return 0; case esl_screen_rows: case esl_screen_cols : return 0; case esl_tab_width: SLsmg_Tab_Width=y; return 0; case esl_newline_behaviour: SLsmg_Newline_Behavior=y; return 0; case esl_error: SLang_Error=y; return 0; case esl_version: return 0; case esl_backspace_moves : SLsmg_Backspace_Moves=y; return 0; case esl_display_eight_bit: SLsmg_Display_Eight_Bit=y; return 0; default: return 0; } } case GETVAR: { x = get_int32(buf); switch (x) { case esl_baud_rate: return ret_int(port, SLang_TT_Baud_Rate); case esl_read_fd: return ret_int(port, SLang_TT_Read_FD); case esl_abort_char: return (ret_int(port, SLang_Abort_Char)); case esl_ignore_user_abort: return ret_int(port, SLang_Ignore_User_Abort); case esl_input_buffer_len : return ret_int(port, SLang_Input_Buffer_Len); case esl_keyboard_quit: return ret_int(port, SLKeyBoard_Quit); case esl_last_key_char: return ret_int(port, SLang_Last_Key_Char); case esl_rl_eof_char: return ret_int(port, SLang_RL_EOF_Char); case esl_rline_quit: return ret_int(port, SLang_Rline_Quit); case esl_screen_rows: return ret_int(port, SLtt_Screen_Rows); case esl_screen_cols : return ret_int(port, SLtt_Screen_Cols); case esl_tab_width: return ret_int(port, SLsmg_Tab_Width); case esl_newline_behaviour: return ret_int(port, SLsmg_Newline_Behavior); case esl_error: return ret_int(port, SLang_Error); case esl_version: return ret_int(port, SLang_Version); case esl_backspace_moves : return ret_int(port, SLsmg_Backspace_Moves); case esl_display_eight_bit: return ret_int(port, SLsmg_Display_Eight_Bit); default: return ret_int(port, -1); } } /*{{{ SLsmg Screen Management Functions */ case SMG_FILL_REGION: { x = get_int32(buf); buf+= 4; y = get_int32(buf); buf+= 4; z = get_int32(buf); buf+= 4; v = get_int32(buf); buf+= 4; ch = *buf; SLsmg_fill_region(x, y,z,v,ch); return 0; } case SMG_SET_CHAR_SET: { x = get_int32(buf); buf+= 4; SLsmg_set_char_set(x); return 0; } case SMG_SUSPEND_SMG: { return ret_int(port, SLsmg_suspend_smg()); } case SMG_RESUME_SMG: { ret_int(port, SLsmg_resume_smg()); } case SMG_ERASE_EOL: { SLsmg_erase_eol(); return 0; } case SMG_GOTORC: { x = get_int32(buf); buf+= 4; y = get_int32(buf); buf+= 4; SLsmg_gotorc(x, y); return 0; } case SMG_ERASE_EOS: { SLsmg_erase_eos(); return 0; } case SMG_REVERSE_VIDEO: { SLsmg_reverse_video(); return 0; } case SMG_SET_COLOR: { x = get_int32(buf); buf+= 4; SLsmg_set_color(x); return 0; } case SMG_NORMAL_VIDEO: { SLsmg_normal_video(); return 0; } case SMG_WRITE_STRING: { SLsmg_write_string(buf); return 0; } case SMG_WRITE_CHAR: { ch = *buf; SLsmg_write_char(ch); return 0; } case SMG_WRITE_WRAPPED_STRING: { t1 = buf; buf += strlen(t1) + 1; x = get_int32(buf); buf+= 4; y = get_int32(buf); buf+= 4; z = get_int32(buf); buf+= 4; v = get_int32(buf); buf+= 4; w = get_int32(buf); buf+= 4; SLsmg_write_wrapped_string(t1, x,y,z,v,w); return 0; } case SMG_CLS: { SLsmg_cls(); return 0; } case SMG_REFRESH: { SLsmg_refresh(); return 0; } case SMG_TOUCH_LINES: { x = get_int32(buf); buf+= 4; y = get_int32(buf); buf+= 4; SLsmg_touch_lines(x, y); return 0; } case SMG_TOUCH_SCREEN: { #if (SLANG_VERSION < 10400 ) return ret_int(port, -1); #else SLsmg_touch_screen(); #endif return 0; } case SMG_INIT_SMG: { return ret_int(port, SLsmg_init_smg()); } case SMG_REINIT_SMG: { #if (SLANG_VERSION < 10400 ) return ret_int(port, -1); #else return ret_int(port, SLsmg_reinit_smg()); #endif } case SMG_RESET_SMG: { SLsmg_reset_smg(); return 0; } case SMG_CHAR_AT: { return ret_int(port, SLsmg_char_at()); } case SMG_SET_SCREEN_START: { int *ip1, *ip2; *ip1 = get_int32(buf); buf+= 4; *ip2 = get_int32(buf); buf+= 4; SLsmg_set_screen_start(ip1, ip2); return ret_int_int(port, *ip1, *ip2); } case SMG_DRAW_HLINE: { x = get_int32(buf); buf+= 4; SLsmg_draw_hline(x); return 0; } case SMG_DRAW_VLINE: { x = get_int32(buf); buf+= 4; SLsmg_draw_vline(x); return 0; } case SMG_DRAW_OBJECT: { x = get_int32(buf); buf+= 4; y = get_int32(buf); buf+= 4; x = get_int32(buf); buf+= 4; SLsmg_draw_object(x, y,z); return 0; } case SMG_DRAW_BOX: { x = get_int32(buf); buf+= 4; y = get_int32(buf); buf+= 4; z = get_int32(buf); buf+= 4; v = get_int32(buf); buf+= 4; SLsmg_draw_box(x, y,z,v); return 0; } case SMG_GET_COLUMN: { return ret_int(port, SLsmg_get_column()); } case SMG_GET_ROW: { return ret_int(port, SLsmg_get_row()); } case SMG_FORWARD: { x = get_int32(buf); buf+= 4; SLsmg_forward(x); return 0; } case SMG_WRITE_COLOR_CHARS: { SLsmg_Char_Type * sl; sl = decode_smg_char_type(&buf); x = get_int32(buf); buf+= 4; SLsmg_write_color_chars(sl, x); return 0; } case SMG_READ_RAW: { x = get_int32(buf); buf+= 4; t1 = malloc((2*x) + 2 + 1); y = SLsmg_read_raw((unsigned short*)t1 +1, x); t1[1] = 1; driver_output(port, t1, y+1); free(t1); return 0; } case SMG_WRITE_RAW: { SLsmg_Char_Type * sl; sl = decode_smg_char_type(&buf); x = get_int32(buf); y = SLsmg_write_raw(sl, x); return ret_int(port, y); } case SMG_SET_COLOR_IN_REGION: { x = get_int32(buf); buf+= 4; y = get_int32(buf); buf+= 4; z = get_int32(buf); buf+= 4; v = get_int32(buf); buf+= 4; w = get_int32(buf); buf+= 4; SLsmg_set_color_in_region(x, y,z,v,w); return 0; } /* all the tt_functions */ case TT_FLUSH_OUTPUT: { ret = SLtt_flush_output(); return ret_int(port, ret); } case TT_SET_SCROLL_REGION: { x = get_int32(buf); buf+=4; y = get_int32(buf); buf+=4; SLtt_set_scroll_region(x, y); return 0; } case TT_RESET_SCROLL_REGION: { SLtt_reset_scroll_region(); return 0; } case TT_REVERSE_VIDEO: { SLtt_reverse_video (get_int32(buf)); return 0; } case TT_BOLD_VIDEO: { SLtt_begin_insert(); return 0; } case TT_BEGIN_INSERT: { SLtt_begin_insert(); return 0; } case TT_END_INSERT: { SLtt_end_insert(); return 0; } case TT_DEL_EOL: { SLtt_del_eol(); return 0; } case TT_GOTO_RC: { x = get_int32(buf); buf+=4; y = get_int32(buf); buf+=4; SLtt_goto_rc (x, y); return 0; } case TT_DELETE_NLINES: { SLtt_delete_nlines(get_int32(buf)); return 0; } case TT_DELETE_CHAR: { SLtt_delete_char(); return 0; } case TT_ERASE_LINE: { SLtt_erase_line(); return 0; } case TT_NORMAL_VIDEO: { SLtt_normal_video(); return 0; } case TT_CLS: { SLtt_cls(); return 0; } case TT_BEEP: { SLtt_beep(); return 0; } case TT_REVERSE_INDEX: { SLtt_reverse_index(get_int32(buf)); return 0; } case TT_SMART_PUTS: { SLsmg_Char_Type *t1 ; SLsmg_Char_Type *t2; t1 = decode_smg_char_type(&buf); t2 = decode_smg_char_type(&buf); x = get_int32(buf); buf+=4; y = get_int32(buf); buf+=4; SLtt_smart_puts(t1, t2,x,y); return 0; } case TT_WRITE_STRING: { SLtt_write_string (buf); return 0; } case TT_PUTCHAR: { SLtt_putchar((char) get_int32(buf)); return 0; } case TT_INIT_VIDEO: { ret = SLtt_init_video (); return ret_int(port, ret); } case TT_RESET_VIDEO: { SLtt_reset_video (); return 0; } case TT_GET_TERMINFO: { SLtt_get_terminfo(); return 0; } case TT_GET_SCREEN_SIZE: { SLtt_get_screen_size (); return 0; } case TT_SET_CURSOR_VISIBILITY: { ret = SLtt_set_cursor_visibility (get_int32(buf)); return ret_int(port, ret); } case TT_SET_MOUSE_MODE: { x = get_int32(buf); buf+=4; y = get_int32(buf); buf+=4; ret = SLtt_set_mouse_mode (x,y); return ret_int(port, ret); } case TT_INITIALIZE: { ret =SLtt_initialize (buf); return ret_int(port, ret); } case TT_ENABLE_CURSOR_KEYS: { SLtt_enable_cursor_keys(); return 0; } case TT_SET_TERM_VTXXX: { return 0; } case TT_SET_COLOR_ESC: { x = get_int32(buf); buf+=4; SLtt_set_color_esc (x, buf); return 0; } case TT_WIDE_WIDTH: { SLtt_narrow_width(); return 0; } case TT_NARROW_WIDTH: { SLtt_narrow_width(); return 0; } case TT_SET_ALT_CHAR_SET: { SLtt_set_alt_char_set (get_int32(buf)); return 0; } case TT_WRITE_TO_STATUS_LINE: { x = get_int32(buf); buf+=4; SLtt_write_to_status_line (buf, x); return 0; } case TT_DISABLE_STATUS_LINE: { SLtt_disable_status_line (); return 0; } case TT_TGETSTR: { str = SLtt_tgetstr (buf); return ret_string(port, str); } case TT_TGETNUM: { x = SLtt_tgetnum (buf); return ret_int(port, x); } case TT_TGETFLAG: { x = SLtt_tgetflag (buf); return ret_int(port, x); } case TT_TIGETENT: { str = SLtt_tigetent (buf); return ret_string(port, str); } case TT_TIGETSTR: { return 0; } case TT_TIGETNUM: { return 0; } case SLTT_GET_COLOR_OBJECT: { x = get_int32(buf); buf+=4; y = SLtt_get_color_object (x); return ret_int(port, y); return 0; } case TT_SET_COLOR_OBJECT: { x = get_int32(buf); buf+=4; y = get_int32(buf); buf+=4; SLtt_set_color_object (x, y); return 0; } case TT_SET_COLOR: { x = get_int32(buf); buf+=4; t1 = buf; t2 = buf + (strlen(t1) + 1); t3 = buf + (strlen(t1) + strlen(t2) + 2); SLtt_set_color (x, t1, t2, t3); return 0; } case TT_SET_MONO: { x = get_int32(buf); buf+=4; t1 = buf; buf += strlen(t1) + 1; y = get_int32(buf); SLtt_set_mono (x, t1, y); return 0; } case TT_ADD_COLOR_ATTRIBUTE: { x = get_int32(buf); buf+=4; y = get_int32(buf); buf+=4; SLtt_add_color_attribute (x, y); return 0; } case TT_SET_COLOR_FGBG: { x = get_int32(buf); buf+=4; y = get_int32(buf); buf+=4; z = get_int32(buf); buf+=4; SLtt_set_color_fgbg (x, y, z); return 0; } case ISATTY: { x = get_int32(buf); buf+=4; return ret_int(port, isatty(x)); } case EFORMAT: { fprintf(stderr, "%s", buf); fflush(stderr); return 0; } case SIGNAL: { x = get_int32(buf); buf+=4; SLsignal(x_to_sig(x), sig_handler); return 0; } case SIGNAL_CHECK: { /* polled */ if (signal_cought != 0) signal_cought = 0; return ret_int(port, signal_cought); } default: return 0; } }
int display_getkey(void) { return SLang_getkey(); }
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; }
static int slang_get_event(caca_display_t *dp, caca_privevent_t *ev) { int intkey; /* If SIGINT was caught, we pass it to the application as Ctrl-C. */ if(dp->drv.p->sigint_event > 0) { ev->type = CACA_EVENT_KEY_PRESS; ev->data.key.ch = CACA_KEY_CTRL_C; ev->data.key.utf32 = 0x03; ev->data.key.utf8[0] = 0x03; ev->data.key.utf8[1] = 0; dp->drv.p->sigint_event--; return 1; } if(!SLang_input_pending(0)) { ev->type = CACA_EVENT_NONE; return 0; } /* We first use SLang_getkey() to see whether Esc was pressed * alone, then (if it wasn't) we unget the key and use SLkp_getkey() * instead, so that escape sequences are interpreted. */ intkey = SLang_getkey(); if(intkey != 0x1b /* Esc */ || SLang_input_pending(0)) { SLang_ungetkey(intkey); intkey = SLkp_getkey(); } /* If the key was ASCII, return it immediately */ if(intkey < 0x7f) { ev->type = CACA_EVENT_KEY_PRESS; ev->data.key.ch = intkey; ev->data.key.utf32 = intkey; ev->data.key.utf8[0] = intkey; ev->data.key.utf8[1] = '\0'; return 1; } /* If the key was UTF-8, parse the whole sequence */ if(intkey >= 0x80 && intkey < 0x100) { int keys[7]; /* Necessary for ungetkey(); */ char utf8[7]; uint32_t utf32; size_t i, bytes = 0; keys[0] = intkey; utf8[0] = intkey; for(i = 1; i < 6; i++) { if(!SLang_input_pending(0)) break; keys[i] = SLang_getkey(); utf8[i] = (unsigned char)keys[i]; } utf8[i] = '\0'; utf32 = caca_utf8_to_utf32(utf8, &bytes); while(i > bytes) SLang_ungetkey(keys[--i]); if(bytes) { ev->type = CACA_EVENT_KEY_PRESS; ev->data.key.ch = 0; ev->data.key.utf32 = utf32; strcpy(ev->data.key.utf8, utf8); return 1; } } if(intkey == 0x3e9) { int button = (SLang_getkey() - ' ' + 1) & 0xf; int x = SLang_getkey() - '!'; int y = SLang_getkey() - '!'; ev->data.mouse.button = button; ev->type = CACA_EVENT_MOUSE_PRESS; _push_event(dp, ev); ev->type = CACA_EVENT_MOUSE_RELEASE; _push_event(dp, ev); if(dp->mouse.x == x && dp->mouse.y == y) return _pop_event(dp, ev); dp->mouse.x = x; dp->mouse.y = y; ev->type = CACA_EVENT_MOUSE_MOTION; ev->data.mouse.x = dp->mouse.x; ev->data.mouse.y = dp->mouse.y; return 1; } switch(intkey) { case SL_KEY_UP: ev->data.key.ch = CACA_KEY_UP; break; case SL_KEY_DOWN: ev->data.key.ch = CACA_KEY_DOWN; break; case SL_KEY_LEFT: ev->data.key.ch = CACA_KEY_LEFT; break; case SL_KEY_RIGHT: ev->data.key.ch = CACA_KEY_RIGHT; break; case SL_KEY_IC: ev->data.key.ch = CACA_KEY_INSERT; break; case SL_KEY_DELETE: ev->data.key.ch = CACA_KEY_DELETE; break; case 0x7f: case SL_KEY_BACKSPACE: ev->data.key.ch = CACA_KEY_BACKSPACE; break; case SL_KEY_HOME: ev->data.key.ch = CACA_KEY_HOME; break; case SL_KEY_END: ev->data.key.ch = CACA_KEY_END; break; case SL_KEY_PPAGE: ev->data.key.ch = CACA_KEY_PAGEUP; break; case SL_KEY_NPAGE: ev->data.key.ch = CACA_KEY_PAGEDOWN; break; case SL_KEY_F(1): ev->data.key.ch = CACA_KEY_F1; break; case SL_KEY_F(2): ev->data.key.ch = CACA_KEY_F2; break; case SL_KEY_F(3): ev->data.key.ch = CACA_KEY_F3; break; case SL_KEY_F(4): ev->data.key.ch = CACA_KEY_F4; break; case SL_KEY_F(5): ev->data.key.ch = CACA_KEY_F5; break; case SL_KEY_F(6): ev->data.key.ch = CACA_KEY_F6; break; case SL_KEY_F(7): ev->data.key.ch = CACA_KEY_F7; break; case SL_KEY_F(8): ev->data.key.ch = CACA_KEY_F8; break; case SL_KEY_F(9): ev->data.key.ch = CACA_KEY_F9; break; case SL_KEY_F(10): ev->data.key.ch = CACA_KEY_F10; break; case SL_KEY_F(11): ev->data.key.ch = CACA_KEY_F11; break; case SL_KEY_F(12): ev->data.key.ch = CACA_KEY_F12; break; default: /* Unknown key */ ev->type = CACA_EVENT_NONE; return 0; } ev->type = CACA_EVENT_KEY_PRESS; ev->data.key.utf32 = 0; ev->data.key.utf8[0] = '\0'; return 1; }
int newtGetKey(void) { int key; char buf[10], * chptr = buf; const struct keymap * curr; do { key = SLang_getkey(); if (key == 0xFFFF) { if (needResize) return NEWT_KEY_RESIZE; /* ignore other signals */ continue; } if (key == NEWT_KEY_SUSPEND && suspendCallback) suspendCallback(suspendCallbackData); } while (key == NEWT_KEY_SUSPEND); switch (key) { case 'v' | 0x80: case 'V' | 0x80: return NEWT_KEY_PGUP; case 22: return NEWT_KEY_PGDN; return NEWT_KEY_BKSPC; case 0x7f: return NEWT_KEY_BKSPC; case 0x08: return NEWT_KEY_BKSPC; default: if (key != keyPrefix) return key; } memset(buf, 0, sizeof(buf)); *chptr++ = key; while (SLang_input_pending(5)) { key = SLang_getkey(); if (key == keyPrefix) { /* he hit unknown keys too many times -- start over */ memset(buf, 0, sizeof(buf)); chptr = buf; } *chptr++ = key; /* this search should use bsearch(), but when we only look through a list of 20 (or so) keymappings, it's probably faster just to do a inline linear search */ for (curr = keymap; curr->code; curr++) { if (curr->str) { if (!strcmp(curr->str, buf)) return curr->code; } } } for (curr = keymap; curr->code; curr++) { if (curr->str) { if (!strcmp(curr->str, buf)) return curr->code; } } /* Looks like we were a bit overzealous in reading characters. Return just the first character, and put everything else back in the buffer for later */ chptr--; while (chptr > buf) SLang_ungetkey(*chptr--); return *chptr; }