From db302f0b97baeea3c571ea28060c9de452ad647b Mon Sep 17 00:00:00 2001 From: camh Date: Sun, 9 Jan 2022 22:57:49 -0800 Subject: [PATCH] get new message notifications working --- JS/index.js | 7 ++- mac_main.c | 9 ++-- nuklear_app.c | 124 +++++++++++++++++++++++++++++++++++--------------- 3 files changed, 98 insertions(+), 42 deletions(-) diff --git a/JS/index.js b/JS/index.js index f707714..1b892dd 100644 --- a/JS/index.js +++ b/JS/index.js @@ -457,6 +457,8 @@ class iMessageClient { async getChatCounts () { + console.log(`getChatCounts`) + if (TEST_MODE) { return parseChatsToFriendlyNameString(TEST_CHATS) @@ -487,6 +489,8 @@ class iMessageClient { let chats = result.data.getChatCounts + console.log(`got chat counts`) + if (!chats) { return `` @@ -506,7 +510,8 @@ class iMessageClient { // remove trailing comma friendlyNameStrings = friendlyNameStrings.substring(1, friendlyNameStrings.length) - + + console.log(friendlyNameStrings) return friendlyNameStrings } diff --git a/mac_main.c b/mac_main.c index 3520e9e..f2bd051 100644 --- a/mac_main.c +++ b/mac_main.c @@ -193,7 +193,7 @@ void EventLoop(struct nk_context *ctx) // PROFILE_START("eventloop"); // #endif - if (gotKeyboardEvent && TickCount() > gotKeyboardEventTime + 80) { + if (gotKeyboardEvent && TickCount() > gotKeyboardEventTime + 20) { gotKeyboardEvent = false; ShowCursor(); @@ -224,7 +224,7 @@ void EventLoop(struct nk_context *ctx) if (chatFriendlyNamesCounter > 0) { // writeSerialPortDebug(boutRefNum, "check chat counts"); - getChatCounts(activeChat); + getChatCounts(); } } @@ -314,7 +314,7 @@ void EventLoop(struct nk_context *ctx) SystemTask(); // only re-render if there is an event, prevents screen flickering, speeds up app - if (beganInput || firstOrMouseMove) { + if (beganInput || firstOrMouseMove || forceRedraw) { // forceRedraw is from nuklear_app #ifdef PROFILING PROFILE_START("nk_input_end"); @@ -454,9 +454,10 @@ void DoEvent(EventRecord *event, struct nk_context *ctx) { HideCursor(); gotKeyboardEvent = true; - gotKeyboardEventTime = TickCount(); } + gotKeyboardEventTime = TickCount(); + #ifdef MAC_APP_DEBUGGING writeSerialPortDebug(boutRefNum, "key"); #endif diff --git a/nuklear_app.c b/nuklear_app.c index 5bf1600..2eac10c 100644 --- a/nuklear_app.c +++ b/nuklear_app.c @@ -1,6 +1,5 @@ // TODO: -// - IN PROGRESS new message window -- needs to blank out messages, then needs fixes on new mac end -- this might work -// - IN PROGRESS get new messages in other chats and display some sort of alert +// - IN PROGRESS new message window -- need to figure this out // - IN PROGRESS need timeout on serial messages in case the computer at the other end dies (prevent hard reset) -- probably possible in coprocessorjs library. made an attempt, needs tested #define WINDOW_WIDTH 510 @@ -17,6 +16,8 @@ #define NK_MEMSET memset #define NK_MEMCPY memcpy +// #define MESSAGES_FOR_MACINTOSH_DEBUGGING + void aFailed(char *file, int line) { MoveTo(10, 10); @@ -53,7 +54,6 @@ char new_message_input_buffer[255]; int activeMessageCounter = 0; int chatFriendlyNamesCounter = 0; int coprocessorLoaded = 0; -int drawChatsOneMoreTime = 2; // this is how many 'iterations' of the UI that we need to see every element for int forceRedraw = 2; // this is how many 'iterations' of the UI that we need to see every element for int haveRun = 0; int ipAddressSet = 0; @@ -81,18 +81,12 @@ void getMessagesFromjsFunctionResponse() { activeMessageCounter = 0; - // writeSerialPortDebug(boutRefNum, "BEGIN"); - - // writeSerialPortDebug(boutRefNum, jsFunctionResponse); char *token = (char *)strtokm(jsFunctionResponse, "ENDLASTMESSAGE"); + // loop through the string to extract all other tokens while (token != NULL) { - // writeSerialPortDebug(boutRefNum, "LOAD VALUE TO TOKEN"); - // writeSerialPortDebug(boutRefNum, token); sprintf(activeChatMessages[activeMessageCounter], "%s", token); - // writeSerialPortDebug(boutRefNum, activeChatMessages[activeMessageCounter]); - // writeSerialPortDebug(boutRefNum, "DONE! LOAD VALUE TO TOKEN"); token = (char *)strtokm(NULL, "ENDLASTMESSAGE"); activeMessageCounter++; } @@ -150,6 +144,11 @@ void getMessages(char *thread, int page) { return; } +// from https://stackoverflow.com/a/4770992 +Boolean prefix(const char *pre, const char *str) { + return strncmp(pre, str, strlen(pre)) == 0; +} + void getChatCounts() { char output[62]; @@ -157,45 +156,80 @@ void getChatCounts() { // writeSerialPortDebug(boutRefNum, output); callFunctionOnCoprocessor("getChatCounts", output, chatCountFunctionResponse); - // writeSerialPortDebug(boutRefNum, jsFunctionResponse); - if (!strcmp(chatCountFunctionResponse, previousChatCountFunctionResponse)) { - - writeSerialPortDebug(boutRefNum, "update current chat count"); + #ifdef MESSAGES_FOR_MACINTOSH_DEBUGGING + writeSerialPortDebug(boutRefNum, "getChatCounts"); writeSerialPortDebug(boutRefNum, chatCountFunctionResponse); + #endif + + if (strcmp(chatCountFunctionResponse, previousChatCountFunctionResponse)) { + + #ifdef MESSAGES_FOR_MACINTOSH_DEBUGGING + writeSerialPortDebug(boutRefNum, "update current chat count"); + writeSerialPortDebug(boutRefNum, chatCountFunctionResponse); + #endif + SysBeep(1); - char *token = (char *)strtok(chatCountFunctionResponse, ","); + char **saveptr; + char *token = strtok_r(chatCountFunctionResponse, ",", saveptr); // loop through the string to extract all other tokens while (token != NULL) { - writeSerialPortDebug(boutRefNum, "update current chat count loop"); - writeSerialPortDebug(boutRefNum, token); + + #ifdef MESSAGES_FOR_MACINTOSH_DEBUGGING + writeSerialPortDebug(boutRefNum, "update current chat count loop"); + writeSerialPortDebug(boutRefNum, token); + #endif // should be in format NAME:::COUNT - char *name = strtok(token, ":::"); - short count = atoi(strtok(NULL, " ")); + char **saveptr2; + char *name = strtok_r(token, ":::", saveptr2); + char *countString = strtok_r(NULL, ":::", saveptr2); + short count = atoi(countString); + + #ifdef MESSAGES_FOR_MACINTOSH_DEBUGGING + char x[255]; + sprintf(x, "name: %s, countString: %s, count: %d", name, countString, count); + writeSerialPortDebug(boutRefNum, x); + #endif if (count == 0) { + token = strtok_r(NULL, ",", saveptr); + continue; } for (int i = 0; i < chatFriendlyNamesCounter; i++) { - if (strstr(chatFriendlyNames[i], " new) ")) { + if (strstr(chatFriendlyNames[i], " new) ") != NULL) { - char *tempChatFriendlyName; - strtok(chatFriendlyNames[i], " new) "); - tempChatFriendlyName = strtok(NULL, " "); + char chatName[64]; + sprintf(chatName, "%s", chatFriendlyNames[i]); - if (strcmp(tempChatFriendlyName, name)) { + // we are throwing out the first token + strtok_r(chatName, " new) ", saveptr2); + + char *tempChatFriendlyName = strtok_r(NULL, " new) ", saveptr2); + + if (prefix(tempChatFriendlyName, name)) { + + #ifdef MESSAGES_FOR_MACINTOSH_DEBUGGING + writeSerialPortDebug(boutRefNum, "match1"); + writeSerialPortDebug(boutRefNum, name); + #endif sprintf(chatFriendlyNames[i], "(%d new) %s", count, name); break; } } else { - if (strcmp(chatFriendlyNames[i], name)) { + if (prefix(chatFriendlyNames[i], name)) { + + #ifdef MESSAGES_FOR_MACINTOSH_DEBUGGING + writeSerialPortDebug(boutRefNum, "match2"); + writeSerialPortDebug(boutRefNum, name); + #endif sprintf(chatFriendlyNames[i], "(%d new) %s", count, name); break; @@ -203,10 +237,14 @@ void getChatCounts() { } } - token = (char *)strtokm(NULL, ","); + token = strtok_r(NULL, ",", saveptr); } strcpy(previousChatCountFunctionResponse, chatCountFunctionResponse); + forceRedraw = 3; + } else { + + writeSerialPortDebug(boutRefNum, "no need to update current chat count"); } return; @@ -380,11 +418,11 @@ static void nuklearApp(struct nk_context *ctx) { chatWindowCollision = checkCollision(chats_window_size); - if ((chatWindowCollision || forceRedraw || drawChatsOneMoreTime) && nk_begin(ctx, "Chats", chats_window_size, NK_WINDOW_BORDER|NK_WINDOW_NO_SCROLLBAR)) { + if ((chatWindowCollision || forceRedraw) && nk_begin(ctx, "Chats", chats_window_size, NK_WINDOW_BORDER|NK_WINDOW_NO_SCROLLBAR)) { if (chatWindowCollision) { - drawChatsOneMoreTime = 2; + forceRedraw = 2; } getChats(); @@ -403,7 +441,23 @@ static void nuklearApp(struct nk_context *ctx) { if (nk_button_label(ctx, chatFriendlyNames[i])) { - sprintf(activeChat, "%s", chatFriendlyNames[i]); + if (strstr(chatFriendlyNames[i], " new) ") != NULL) { + + char chatName[96]; + sprintf(chatName, "%s", chatFriendlyNames[i]); + writeSerialPortDebug(boutRefNum, chatName); + + // we are throwing out the first token + strtokm(chatName, " new) "); + char *name = strtokm(NULL, " new) "); + + writeSerialPortDebug(boutRefNum, name); + sprintf(activeChat, "%s", name); + } else { + + sprintf(activeChat, "%s", chatFriendlyNames[i]); + } + getMessages(activeChat, 0); } } @@ -413,7 +467,6 @@ static void nuklearApp(struct nk_context *ctx) { nk_end(ctx); } - if (nk_begin(ctx, "Message Input", message_input_window_size, NK_WINDOW_BORDER|NK_WINDOW_NO_SCROLLBAR)) { // bottom text input @@ -434,7 +487,7 @@ static void nuklearApp(struct nk_context *ctx) { nk_end(ctx); } - if ((forceRedraw || drawChatsOneMoreTime) && nk_begin_titled(ctx, "Message", activeChat, messages_window_size, NK_WINDOW_BORDER|NK_WINDOW_TITLE|NK_WINDOW_NO_SCROLLBAR)) { + if ((forceRedraw) && nk_begin_titled(ctx, "Message", activeChat, messages_window_size, NK_WINDOW_BORDER|NK_WINDOW_TITLE|NK_WINDOW_NO_SCROLLBAR)) { nk_layout_row_begin(ctx, NK_STATIC, 12, 1); { @@ -453,12 +506,7 @@ static void nuklearApp(struct nk_context *ctx) { if (forceRedraw) { - forceRedraw = 0; - } - - if (drawChatsOneMoreTime) { - - drawChatsOneMoreTime = 0; + forceRedraw--; } } @@ -481,6 +529,8 @@ void refreshNuklearApp(Boolean blankInput) { struct nk_context* initializeNuklearApp() { sprintf(activeChat, "no active chat"); + memset(&chatCountFunctionResponse, '\0', 102400); + memset(&previousChatCountFunctionResponse, '\0', 102400); graphql_input_window_size = nk_rect(WINDOW_WIDTH / 2 - 118, 80, 234, 100); chats_window_size = nk_rect(0, 0, 180, WINDOW_HEIGHT);