diff --git a/palm-client/src/convo_form.c b/palm-client/src/convo_form.c index 6db3468..c970ad2 100644 --- a/palm-client/src/convo_form.c +++ b/palm-client/src/convo_form.c @@ -16,6 +16,23 @@ #include "ui/rowlist.h" #include "convo_form.h" +/* In the second code segment: see sections.h for the rules. */ +static UInt16 TextWidth(void) VM_SECT2; +static void SenderOf(const VmMsgHeader *h, Char *out, UInt16 size) VM_SECT2; +static void SuffixOf(const VmMsgHeader *h, Char *out) VM_SECT2; +static UInt32 LocalDay(UInt32 utc) VM_SECT2; +static void AddLine(UInt8 kind, UInt16 msg, UInt16 off, UInt16 len, Boolean first, Boolean last, UInt32 day) VM_SECT2; +static void Build(void) VM_SECT2; +static void DrawTextWithMentions(const Char *text, UInt16 len, Coord x, Coord y) VM_SECT2; +static void DrawLine(UInt16 row, const RectangleType *b, void *ctx) VM_SECT2; +static void DrawCounter(void) VM_SECT2; +static UInt16 FirstUnread(void) VM_SECT2; +static void ScrollToNew(void) VM_SECT2; +static void Refresh(Boolean toEnd) VM_SECT2; +static void Send(FormType *form) VM_SECT2; +static void InsertMention(const Char *sender) VM_SECT2; +static void Tapped(UInt16 row) VM_SECT2; + #define MAX_MSGS 40 /* newest messages shown */ #define MAX_LINES 200 #define LINE_H VM_CHIP_H /* chips need the extra 2 px */ @@ -25,6 +42,8 @@ #define L_DAY 0 #define L_CHIP 1 #define L_TEXT 2 +#define L_NEW 3 /* "── new ──" before the first message unread when the screen opened */ +#define NO_NEW 0xFFFF typedef struct { UInt8 kind; @@ -39,6 +58,7 @@ static Line *gLines = NULL; static UInt16 gCount = 0; static RowList gList; static Char gTitle[VM_NAME_SIZE + 2]; +static UInt16 gNewFrom = NO_NEW; /* message record of the first unread one at open */ static const Char *kDays[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; static const Char *kMonths[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", @@ -137,6 +157,10 @@ static void Build(void) lastDay = day; lastSender[0] = 0; } + if (m == gNewFrom) { + AddLine(L_NEW, m, 0, 0, false, false, 0); + lastSender[0] = 0; /* start the new part with a sender chip */ + } SenderOf(h, sender, sizeof(sender)); if (StrCompare(sender, lastSender) != 0) { AddLine(L_CHIP, m, 0, 0, false, false, 0); @@ -210,7 +234,9 @@ static void DrawLine(UInt16 row, const RectangleType *b, void *ctx) FntSetFont(old); return; } - if (l->kind == L_DAY) { + if (l->kind == L_NEW) { + DrawSection("new", x, y, b->extent.x); + } else if (l->kind == L_DAY) { TimSecondsToDateTime(l->day * 86400UL, &dt); StrPrintF(buf, "%s %s %u", kDays[DayOfWeek(dt.month, dt.day, dt.year)], kMonths[dt.month - 1], dt.day); DrawSection(buf, x, y, b->extent.x); @@ -258,6 +284,37 @@ static void DrawCounter(void) FntSetFont(old); } +/* The oldest unread message of this conversation, or NO_NEW. */ +static UInt16 FirstUnread(void) +{ + UInt16 i, n = MessagesCount(), found = NO_NEW; + + for (i = n; i > 0; i--) { + const VmMsgHeader *h = MessagesLock(i - 1); + + if (h) { + if (h->unread && ConvEqual(&h->conv, &gAppConv)) + found = i - 1; + MessagesRelease(i - 1); + } + } + return found; +} + +/* Opens at the "new" divider (one line of context above it), or at the end. */ +static void ScrollToNew(void) +{ + UInt16 i; + + for (i = 0; i < gCount; i++) { + if (gLines[i].kind == L_NEW) { + RowListScrollTo(&gList, i > 0 ? i - 1 : 0); + return; + } + } + RowListScrollToEnd(&gList); +} + static void Refresh(Boolean toEnd) { MessagesMarkRead(&gAppConv); @@ -348,10 +405,11 @@ Boolean ConvoFormHandleEvent(EventType *event) ConvName(&gAppConv, gTitle, VM_NAME_SIZE); FrmCopyTitle(form, gTitle); RowListInit(&gList, ConvoGadget, ConvoScroll, LINE_H, DrawLine, NULL, NULL); - MessagesMarkRead(&gAppConv); + gNewFrom = FirstUnread(); Build(); + MessagesMarkRead(&gAppConv); RowListSetCount(&gList, gCount); - RowListScrollToEnd(&gList); + ScrollToNew(); FrmDrawForm(form); RowListDraw(&gList); DrawCounter(); diff --git a/palm-client/src/convos.c b/palm-client/src/convos.c index 2d346e6..a9e94ea 100644 --- a/palm-client/src/convos.c +++ b/palm-client/src/convos.c @@ -122,6 +122,9 @@ static void SetPreview(VmConvRow *r, const VmMsgHeader *h) } StrNCopy(r->preview + len, text, VM_PREVIEW_LEN - 1 - len); r->preview[VM_PREVIEW_LEN - 1] = 0; + for (len = 0; r->preview[len]; len++) /* one line: breaks become spaces */ + if (r->preview[len] == '\n' || r->preview[len] == '\r') + r->preview[len] = ' '; } UInt16 ConvosBuild(VmConvRow *rows, UInt16 max) diff --git a/palm-client/src/main.c b/palm-client/src/main.c index d8de0a8..c7c2181 100644 --- a/palm-client/src/main.c +++ b/palm-client/src/main.c @@ -109,8 +109,12 @@ static void CheckWake(void) static void Poll(void) { UInt16 formId = FrmGetActiveFormID(); + UInt16 changed = SessionPoll(); - if (SessionPoll()) + /* a short beep for a new DM or mention, unless that conversation is on screen */ + if ((changed & VM_CHANGED_ALERT) && !(formId == ConvoForm && ConvEqual(SessionAlertConv(), &gAppConv))) + SndPlaySystemSound(sndConfirmation); + if (changed) FrmUpdateForm(formId, VM_UPDATE_DATA); if (formId == ConvoForm) ConvoFormIdle(); diff --git a/palm-client/src/session.c b/palm-client/src/session.c index 6f617de..2c7e583 100644 --- a/palm-client/src/session.c +++ b/palm-client/src/session.c @@ -319,6 +319,48 @@ Err SessionGetChannel(UInt8 slot, VmChannel *out) return err; } +/* The newest message that should alert (a DM, or a mention of us in a channel). */ +static VmConv gAlertConv; + +static void NoteAlert(const VmMessage *m) +{ + MemSet(&gAlertConv, sizeof(gAlertConv), 0); + if (m->isChannel) { + gAlertConv.type = VM_CONV_CHANNEL; + gAlertConv.channel = m->channelIdx; + } else { + gAlertConv.type = VM_CONV_DIRECT; + MemMove(gAlertConv.key, m->senderPrefix, 6); + } + gChanged |= VM_CHANGED_ALERT; +} + +/* True if the (UTF-8) text contains "@[our name]". */ +static Boolean MentionsUs(const VmMessage *m) +{ + UInt8 tag[VM_NAME_SIZE + 4]; + UInt16 n, i; + + if (!gSelf.name[0]) + return false; + tag[0] = '@'; + tag[1] = '['; + n = TxtPalmToUtf8(gSelf.name, StrLen(gSelf.name), tag + 2, VM_NAME_SIZE); + if (n == 0) + return false; + tag[2 + n] = ']'; + n += 3; + for (i = 0; i + n <= m->textLen; i++) + if (MemCmp(m->text + i, tag, n) == 0) + return true; + return false; +} + +const VmConv *SessionAlertConv(void) +{ + return &gAlertConv; +} + Err SessionSyncMessages(void) { static const UInt8 codes[] = { VM_RESP_CONTACT_MSG_V3, VM_RESP_CHANNEL_MSG_V3, VM_RESP_NO_MORE_MESSAGES }; @@ -338,8 +380,11 @@ Err SessionSyncMessages(void) return err; if (gDecoder.payload[0] == VM_RESP_NO_MORE_MESSAGES) return errNone; - if (VmParseMessage(gDecoder.payload, gDecoder.len, &m) && MessagesAddIncoming(&m, SessionNow()) == errNone) + if (VmParseMessage(gDecoder.payload, gDecoder.len, &m) && MessagesAddIncoming(&m, SessionNow()) == errNone) { gChanged |= VM_CHANGED_MESSAGES; + if (!m.isChannel || MentionsUs(&m)) + NoteAlert(&m); + } } gMsgWaiting = true; /* more left: carry on at the next poll */ return errNone; diff --git a/palm-client/src/session.h b/palm-client/src/session.h index d5d60a9..12a0abf 100644 --- a/palm-client/src/session.h +++ b/palm-client/src/session.h @@ -18,6 +18,7 @@ /* What SessionPoll found changed, so the screen on show can redraw. */ #define VM_CHANGED_MESSAGES 0x0001 #define VM_CHANGED_CONTACTS 0x0002 +#define VM_CHANGED_ALERT 0x0004 /* a new DM or mention of us arrived: see SessionAlertConv */ /* Opens the link and runs DEVICE_QUERY + APP_START. On error the link is closed. */ @@ -69,6 +70,8 @@ UInt32 SessionRadioTime(void); * messages and times out unacked DMs. Returns VM_CHANGED_* flags. */ UInt16 SessionPoll(void); +/* The conversation of the newest message that should alert (a DM or a mention of us). */ +const VmConv *SessionAlertConv(void); /* Pulls queued messages into the message store (SYNC_NEXT_MESSAGE until none are left). */ Err SessionSyncMessages(void); Err SessionGetChannel(UInt8 slot, VmChannel *out); diff --git a/palm-client/src/ui/text.c b/palm-client/src/ui/text.c index d01fc11..94a18c4 100644 --- a/palm-client/src/ui/text.c +++ b/palm-client/src/ui/text.c @@ -35,8 +35,18 @@ UInt16 TxtFit(const Char *s, UInt16 len, Int16 maxW, TxtWidthFn w) UInt16 TxtWrap(const Char *s, UInt16 len, Int16 maxW, TxtWidthFn w, UInt16 *consumed) { - UInt16 fit = TxtFit(s, len, maxW, w); - UInt16 brk, end; + UInt16 fit, brk, end, nl; + + /* a line break in the text ends the line there, if it comes before the wrap point */ + for (nl = 0; nl < len && s[nl] != '\n' && s[nl] != '\r'; nl++) + ; + fit = TxtFit(s, nl, maxW, w); + if (fit >= nl && nl < len) { + *consumed = nl + 1; + if (s[nl] == '\r' && nl + 1 < len && s[nl + 1] == '\n') + (*consumed)++; /* \r\n counts as one break */ + return nl; + } if (fit >= len) { *consumed = len; diff --git a/palm-client/src/ui/text.h b/palm-client/src/ui/text.h index ad8cbcf..a221675 100644 --- a/palm-client/src/ui/text.h +++ b/palm-client/src/ui/text.h @@ -22,7 +22,8 @@ UInt16 TxtFit(const Char *s, UInt16 len, Int16 maxW, TxtWidthFn w) VM_SECT2; * One line of word wrap: returns how many characters to draw on this line * (breaking after the last space that fits, or mid-word if a word is longer * than the line) and sets *consumed to that plus any spaces to skip before - * the next line. Returns 0 with *consumed 0 only for empty input. + * the next line. A \n (or \r, \r\n) in the text ends the line there and is + * consumed. Returns 0 with *consumed 0 only for empty input. */ UInt16 TxtWrap(const Char *s, UInt16 len, Int16 maxW, TxtWidthFn w, UInt16 *consumed) VM_SECT2; diff --git a/palm-client/tests/test_text.c b/palm-client/tests/test_text.c index 0d3dea2..6a01d80 100644 --- a/palm-client/tests/test_text.c +++ b/palm-client/tests/test_text.c @@ -45,6 +45,16 @@ static void test_fit_and_wrap(void) n = TxtWrap("https://example.com/long", 24, 10 * 4, Four, &used); CHECK(n == 10 && used == 10); + /* line breaks end the line and are consumed */ + n = TxtWrap("ab\ncd", 5, 100, Four, &used); + CHECK(n == 2 && used == 3); + n = TxtWrap("ab\r\ncd", 6, 100, Four, &used); + CHECK(n == 2 && used == 4); + n = TxtWrap("\n\nx", 3, 100, Four, &used); /* an empty line */ + CHECK(n == 0 && used == 1); + n = TxtWrap("abcdef ghij\nk", 13, 5 * 4, Four, &used); /* the width wraps before the break */ + CHECK(n == 5 && used == 5); + /* the rest fits */ n = TxtWrap("short", 5, 100, Four, &used); CHECK(n == 5 && used == 5);