From a30c97080502c3d7c0b7f5aeae390e596bde181a Mon Sep 17 00:00:00 2001 From: tlatorre Date: Sat, 12 Nov 2022 16:42:33 -0800 Subject: code now works correctly when you release the mouse button --- moji.c | 53 ++++++++++------------------------------------------- 1 file changed, 10 insertions(+), 43 deletions(-) diff --git a/moji.c b/moji.c index e1042c7..c32ae34 100644 --- a/moji.c +++ b/moji.c @@ -159,6 +159,12 @@ long released_time_sec = 0; /* Time the mouse was released (milliseconds) */ long released_time_msec = 0; +/* Is the left mouse button currently pressed? */ +int left_click_pressed = FALSE; + +/* Has the current character been processed? */ +int processed = FALSE; + void backspace(void) { GtkTextBuffer *text_buffer; @@ -205,12 +211,11 @@ static int right_click_released(GtkWidget *widget, gpointer data) return TRUE; } -int left_click_pressed = FALSE; - static int pressed(GtkWidget *widget, gpointer data) { aeGetTime(&pressed_time_sec,&pressed_time_msec); left_click_pressed = TRUE; + processed = FALSE; return TRUE; } @@ -226,6 +231,7 @@ gboolean timer(gpointer data) if (diff > 10000) { current_position = 0; current_letter = 0; + processed = TRUE; aeGetTime(&pressed_time_sec,&pressed_time_msec); } else if (diff > CLICK_TIME) { current_char = current_chars->characters[current_letter % current_chars->len]; @@ -265,6 +271,7 @@ gboolean timer(gpointer data) write_buf(); aeGetTime(&pressed_time_sec,&pressed_time_msec); + processed = TRUE; } return TRUE; @@ -272,52 +279,12 @@ gboolean timer(gpointer data) static int released(GtkWidget *widget, gpointer data) { - const char *current_char; - left_click_pressed = FALSE; aeGetTime(&released_time_sec,&released_time_msec); long long diff = (released_time_sec - pressed_time_sec)*1000 + (released_time_msec - pressed_time_msec); - if (diff > 10000) { - current_position = 0; - current_letter = 0; - } else if (diff > CLICK_TIME) { - current_char = current_chars->characters[current_letter % current_chars->len]; - if (!strcmp(current_char,LEFT_ARROW)) { - backspace(); - } else if (!strcmp(current_char,CLEAR_SCREEN)) { - current_position = 0; - current_letter = 0; - } else if (!strcmp(current_char,SOUND)) { - char cmd[1000000]; - buf[current_position] = '\0'; - sprintf(cmd, "gtts-cli \"%s\" --output moji.mp3", buf); - if (!system(cmd)) { - system("mpg123 moji.mp3"); - system("rm moji.mp3"); - } - } else if (!strcmp(current_char,"_")) { - sprintf(buf+current_position," "); - current_position += strlen(current_char); - } else if (!strcmp(current_char,NEWLINE)) { - sprintf(buf+current_position,"\n"); - current_position += 1; - } else { - sprintf(buf+current_position,"%s",current_char); - current_position += strlen(current_char); - } - if (current_position > LEN(buf) - 1) { - /* FIXME: better way to do this? */ - current_position = 0; - } - /* Uncomment the next line to start over at the beginning of the - * alphabet after a new letter is entered. But, I think it's better - * to keep the current letter. The reason is that you might want to - * delete a whole word and so when you navigate to the backspace you - * just have to keep entering the same letter. */ - current_letter = 0; - } else { + if ((processed == FALSE) && (diff < CLICK_TIME)) { current_letter += 1; } -- cgit