diff options
author | Haoran S. Diao (刁浩然) <0@hairydiode.xyz> | 2025-09-06 02:07:20 -0700 |
---|---|---|
committer | Haoran S. Diao (刁浩然) <0@hairydiode.xyz> | 2025-09-06 02:07:20 -0700 |
commit | 43e028136ef84c900f565946d5f694f9e4fc4eee (patch) | |
tree | 45b0991fc19d10ad5ba35c1d816ff92d4076d7aa /ptyim.c | |
parent | 33c523fdef955c60fb81ccf3e30b8eace12933c9 (diff) |
Fully functional so far. Just need to clean up the code and actually import the
input method table
Diffstat (limited to 'ptyim.c')
-rw-r--r-- | ptyim.c | 71 |
1 files changed, 61 insertions, 10 deletions
@@ -18,18 +18,64 @@ void childhook(int a) { running = 0; } int pty_slave = -1; +//terminal rows and cols +int trows = 0; +int tcols = 0; //hook for SIGWINCH void winchhook(int a) { struct winsize sz; ioctl(STDIN_FILENO, TIOCGWINSZ, &sz); + trows = sz.ws_row; + tcols = sz.ws_col; + //trick programs into thinking the terminal is smaller than it is + if (sz.ws_row >= 1) { + sz.ws_row--; + } ioctl(pty_slave, TIOCSWINSZ, &sz); } +void disp_im() { + //3 spaces for the im indicator +1 for automargin + int im_col = tcols - IM_TABLE_ENTRY_LEN - 4; + char pos_str[32] = ""; + int l = sprintf(pos_str, "\033[%d;%df",trows, im_col); + //printf("\0337"); + //printf("\033[%d;%df",trows,im_col); + //printf("\033[0;0f",trows,im_col); + //printf("\033[7m"); + //if (im_on) { + // printf("漢"); + //} else { + // printf("英"); + //} + //printf("%s",im_buffer); + //printf("\033[0m"); + //printf("\0338"); + write(STDOUT_FILENO,"\0337",2); + //also writes to top left the current im buffer + //write(STDOUT_FILENO,"\033[33;14f",8); + //write(STDOUT_FILENO,"\033[0;4f",6); + //write to the bottom left corner + write(STDOUT_FILENO, pos_str,l); + //invert video + write(STDOUT_FILENO,"\033[7m",4); + //overwrite what was there previously + write(STDOUT_FILENO," ", + IM_TABLE_ENTRY_LEN+4); + write(STDOUT_FILENO, pos_str,l); + //im mode indicator + if (im_on) { + write(STDOUT_FILENO,"漢:",4); + } else { + write(STDOUT_FILENO,"英:",4); + } + //actual buffer + write(STDOUT_FILENO,im_buffer,im_buffer_pos); + //restore cursor pos and attrbutes + write(STDOUT_FILENO,"\033[0m",4); + write(STDOUT_FILENO,"\0338",2); +} int main (int argc, char * argv[], char * envp[]) { - printf("%s: %d\n", "a", im_search("a",0,IM_TABLE_LEN)); - printf("%s: %d\n", "bb", im_search("bb",0,IM_TABLE_LEN)); - printf("%s: %d\n", "c", im_search("c",0,IM_TABLE_LEN)); - printf("%s: %d\n", "ba", im_search("ba",0,IM_TABLE_LEN)); //creates the pty master int pty_master = posix_openpt(O_RDWR); if (pty_master < 0) { @@ -55,7 +101,6 @@ int main (int argc, char * argv[], char * envp[]) { } //gets slave path char * pty_slave_fn = ptsname(pty_master); - printf("Slave name %s\n", pty_slave_fn); //tries to open pty slave pty_slave = open(pty_slave_fn, O_RDWR); if (pty_slave < 0) { @@ -69,7 +114,6 @@ int main (int argc, char * argv[], char * envp[]) { winchhook(0); //forks for running bash pid_t mypid = fork(); - printf("forked w/ pid %d\n", mypid); if (mypid == 0) { //set self to be session leader @@ -129,14 +173,21 @@ int main (int argc, char * argv[], char * envp[]) { //read uses file descriptors and is a system call chars_read = read(pty_master,readbuffer,1); if (chars_read >0) { - write(STDIN_FILENO,readbuffer,1); + write(STDOUT_FILENO,readbuffer,1); } - //relays input from stdin - char input_buffer[IM_TABLE_ENTRY_LEN] = {0}; chars_read = read(STDIN_FILENO, readbuffer, 1); if (chars_read > 0) { - write(pty_master,readbuffer,1); + //write(pty_master,readbuffer,1); + //relays input from stdin + char input_buffer[IM_TABLE_ENTRY_LEN] = {0}; + int im_chars = update_im_state(readbuffer[0], + input_buffer); + input_buffer[im_chars] = 0; + if (im_chars >0) { + write(pty_master,input_buffer,im_chars); + } + disp_im(); } } } |