* Update configure script according to previous change to configure.ac
[alpine.git] / alpine / status.c
blob2737ded73755d02ba78c6ea39b212794ea228e29
1 /*
2 * ========================================================================
3 * Copyright 2006-2007 University of Washington
4 * Copyright 2013-2022 Eduardo Chappa
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * ========================================================================
15 /*======================================================================
16 status.c
17 Functions that manage the status line (third from the bottom)
18 - put messages on the queue to be displayed
19 - display messages on the queue with timers
20 - check queue to figure out next timeout
21 - prompt for yes/no type of questions
22 ====*/
24 #include "headers.h"
25 #include "status.h"
26 #include "keymenu.h"
27 #include "mailview.h"
28 #include "mailcmd.h"
29 #include "busy.h"
30 #include "after.h"
32 #include "../pith/charconv/utf8.h"
34 #include "../pith/conf.h"
35 #include "../pith/bitmap.h"
39 * Internal queue of messages. The circular, double-linked list's
40 * allocated on demand, and cleared as each message is displayed.
42 typedef struct message {
43 char *text;
44 unsigned flags:8;
45 unsigned shown:1;
46 unsigned saw_it:1;
47 unsigned pending_removal:1;
48 int min_display_time, max_display_time;
49 struct message *next, *prev;
50 } SMQ_T;
54 * Internal prototypes
56 void pause_for_and_dq_cur_msg(void);
57 SMQ_T *top_of_queue(void);
58 int new_info_msg_need_not_be_queued(void);
59 int is_last_message(SMQ_T *);
60 void d_q_status_message(void);
61 int output_message(SMQ_T *, int);
62 int output_message_modal(SMQ_T *, int);
63 void delay_cmd_cue(int);
64 int modal_bogus_input(UCS);
65 int messages_in_queue(void);
66 int status_message_remaining_nolock(void);
67 void set_saw_it_to_zero();
68 void mark_modals_done();
69 SMQ_T *copy_status_queue(SMQ_T *);
73 /*----------------------------------------------------------------------
74 Manage the second line from the bottom where status and error messages
75 are displayed. A small queue is set up and messages are put on the queue
76 by calling one of the q_status_message routines. Even though this is a queue
77 most of the time message will go right on through. The messages are
78 displayed just before the read for the next command, or when a read times
79 out. Read timeouts occur every minute or so for new mail checking and every
80 few seconds when there are still messages on the queue. Hopefully, this scheme
81 will not let messages fly past that the user can't see.
82 ----------------------------------------------------------------------*/
85 static SMQ_T *message_queue = NULL;
86 static short needs_clearing = 0, /* Flag set by want_to()
87 and optionally_enter() */
88 prevstartcol, prevendcol;
89 static char prevstatusbuf[6*MAX_SCREEN_COLS+1];
90 static time_t displayed_time;
93 /*----------------------------------------------------------------------
94 Put a message for the status line on the queue
96 Args: time -- the min time in seconds to display the message
97 message -- message string
99 Result: queues message on queue represented by static variables
101 This puts a single message on the queue to be shown.
102 ----------*/
103 void
104 q_status_message(int flags, int min_time, int max_time, char *message)
106 SMQ_T *new, *q;
107 char *clean_msg;
108 size_t mlen;
110 status_message_lock();
113 * If there is already a message queued and
114 * new message is just informational, discard it.
116 if(flags & SM_INFO && new_info_msg_need_not_be_queued()){
117 status_message_unlock();
118 return;
122 * By convention, we have min_time equal to zero in messages which we
123 * think are not as important, so-called comfort messages. We have
124 * min_time >= 3 for messages which we think the user should see for
125 * sure. Some users don't like to wait so we've provided a way for them
126 * to live on the edge.
127 * status_msg_delay == -1 => min time == MIN(0, min_time)
128 * status_msg_delay == -2 => min time == MIN(1, min_time)
129 * status_msg_delay == -3 => min time == MIN(2, min_time)
130 * ...
132 if(ps_global->status_msg_delay < 0)
133 min_time = MIN(-1 - ps_global->status_msg_delay, min_time);
135 /* The 40 is room for 40 escaped control characters */
136 mlen = strlen(message) + 40;
137 clean_msg = (char *)fs_get(mlen + 1);
138 iutf8ncpy(clean_msg, message, mlen); /* does the cleaning */
140 clean_msg[mlen] = '\0';
142 if((q = message_queue) != NULL){ /* the queue exists */
145 * Scan through all of the messages currently in the queue.
146 * If the new message is already queued, don't add it again.
148 do {
149 if(!q->pending_removal && q->text && !strcmp(q->text, clean_msg)){
150 q->shown = 0;
151 if(q->min_display_time < min_time)
152 q->min_display_time = min_time;
154 if(q->max_display_time < max_time)
155 q->max_display_time = max_time;
157 dprint((9, "q_status_message(%s): skipping duplicate msg\n",
158 clean_msg ? clean_msg : "?"));
160 if(clean_msg)
161 fs_give((void **)&clean_msg);
163 status_message_unlock();
164 return;
167 q = q->next;
169 } while(q != message_queue);
172 new = (SMQ_T *)fs_get(sizeof(SMQ_T));
173 memset(new, 0, sizeof(SMQ_T));
174 new->text = clean_msg;
175 new->min_display_time = min_time;
176 new->max_display_time = max_time;
177 new->flags = flags;
178 if(message_queue){
179 new->next = message_queue;
180 new->prev = message_queue->prev;
181 new->prev->next = message_queue->prev = new;
183 else
184 message_queue = new->next = new->prev = new;
186 status_message_unlock();
188 dprint((9, "q_status_message(%s)\n",
189 clean_msg ? clean_msg : "?"));
193 /*----------------------------------------------------------------------
194 Mark the status line as dirty so it gets cleared next chance
195 ----*/
196 void
197 mark_status_dirty(void)
199 mark_status_unknown();
200 needs_clearing++;
204 /*----------------------------------------------------------------------
205 Cause status line drawing optimization to be turned off, because we
206 don't know what the status line looks like.
207 ----*/
208 void
209 mark_status_unknown(void)
211 prevstartcol = -1;
212 prevendcol = -1;
213 prevstatusbuf[0] = '\0';
217 /*----------------------------------------------------------------------
218 Wait a suitable amount of time for the currently displayed message
219 ----*/
220 void
221 pause_for_and_dq_cur_msg(void)
223 if(top_of_queue()){
224 int w;
226 if((w = status_message_remaining_nolock()) != 0){
227 delay_cmd_cue(1);
228 /* protect user from changes in the clock. If the clock
229 * changes during this process (for example, going from
230 * Standard time to Delayed time) this may result in big
231 * values for w. In those cases, reset w.
233 w = (w > 0 ? (w > 5 ? 5 : w) : 0);
234 if(ps_global && !ps_global->initial_cmds) sleep(w);
235 delay_cmd_cue(0);
238 d_q_status_message();
244 * This relies on the global displayed_time being properly set
245 * for this message.
247 void
248 pause_for_and_mark_specific_msg(SMQ_T *msg)
250 if(msg){
251 int w;
253 w = (int) (displayed_time - time(0)) + msg->min_display_time;
254 w = (w > 0) ? (w > 5 ? 5 : w): 0;
255 if(w){
256 delay_cmd_cue(1);
257 if (ps_global && !ps_global->initial_cmds) sleep(w);
258 delay_cmd_cue(0);
261 msg->pending_removal = 1;
266 /*----------------------------------------------------------------------
267 Time remaining for current message's minimum display
268 ----*/
270 status_message_remaining(void)
272 int ret;
274 status_message_lock();
275 ret = status_message_remaining_nolock();
276 status_message_unlock();
278 return(ret);
283 status_message_remaining_nolock(void)
285 SMQ_T *q;
286 int d = 0;
288 if((q = top_of_queue()) != NULL)
289 d = (int) (displayed_time - time(0)) + q->min_display_time;
291 return((d > 0) ? d : 0);
296 * Return first message in queue that isn't pending_removal.
298 SMQ_T *
299 top_of_queue(void)
301 SMQ_T *p;
303 if((p = message_queue) != NULL){
305 if(!p->pending_removal)
306 return(p);
307 while((p = p->next) != message_queue);
310 return(NULL);
315 new_info_msg_need_not_be_queued(void)
317 SMQ_T *q;
319 if(status_message_remaining_nolock() > 0)
320 return 1;
322 if((q = top_of_queue()) != NULL && (q = q->next) != message_queue){
324 if(!q->pending_removal && !(q->flags & SM_INFO))
325 return 1;
326 while((q = q->next) != message_queue);
329 return 0;
333 /*----------------------------------------------------------------------
334 Find out how many messages are queued for display
336 Args: dtime -- will get set to minimum display time for current message
338 Result: number of messages in the queue.
340 ---------*/
342 messages_queued(long int *dtime)
344 SMQ_T *q;
345 int ret;
347 status_message_lock();
348 if(dtime && (q = top_of_queue()) != NULL)
349 *dtime = (long) MAX(q->min_display_time, 1L);
351 ret = ps_global->in_init_seq ? 0 : messages_in_queue();
353 status_message_unlock();
355 return(ret);
359 /*----------------------------------------------------------------------
360 Return number of messages in queue
361 ---------*/
363 messages_in_queue(void)
365 int n = 0;
366 SMQ_T *p;
368 if((p = message_queue) != NULL){
370 if(!p->pending_removal)
371 n++;
372 while((p = p->next) != message_queue);
375 return(n);
379 /*----------------------------------------------------------------------
380 Return last message queued
381 ---------*/
382 char *
383 last_message_queued(void)
385 SMQ_T *p, *r = NULL;
386 char *ret = NULL;
388 status_message_lock();
389 if((p = message_queue) != NULL){
391 if(p->flags & SM_ORDER && !p->pending_removal)
392 r = p;
393 while((p = p->next) != message_queue);
396 ret = (r && r->text) ? cpystr(r->text) : NULL;
398 status_message_unlock();
400 return(ret);
405 is_last_message(SMQ_T *msg)
407 SMQ_T *p, *r = NULL;
409 if(msg && !msg->pending_removal){
410 if((p = message_queue) != NULL){
412 if(!p->pending_removal)
413 r = p;
414 while((p = p->next) != message_queue);
418 return(r && msg && (r == msg));
422 /*----------------------------------------------------------------------
423 Update status line, clearing or displaying a message
425 Arg: command -- The command that is about to be executed
427 Result: status line cleared or
428 next message queued is displayed or
429 current message is redisplayed.
430 if next message displayed, it's min display time
431 is returned else if message already displayed, it's
432 time remaining on the display is returned, else 0.
434 This is called when ready to display the next message, usually just
435 before reading the next command from the user. We pass in the nature
436 of the command because it affects what we do here. If the command just
437 executed by the user is a redraw screen, we don't want to reset or go to
438 next message because it might not have been seen. Also if the command
439 is just a noop, which are usually executed when checking for new mail
440 and happen every few minutes, we don't clear the message.
442 If it was really a command and there's nothing more to show, then we
443 clear, because we know the user has seen the message. In this case the
444 user might be typing commands very quickly and miss a message, so
445 there is a time stamp and time check that each message has been on the
446 screen for a few seconds. If it hasn't we just return and let it be
447 taken care of next time.
448 ----------------------------------------------------------------------*/
450 display_message(UCS command)
452 SMQ_T *q, *copy_of_q;
453 int need_to_unlock;
454 int ding;
456 if(ps_global == NULL || ps_global->ttyo == NULL
457 || ps_global->ttyo->screen_rows < 1 || ps_global->in_init_seq)
458 return(0);
460 status_message_lock();
462 /*---- Deal with any previously displayed message ----*/
463 if((q = top_of_queue()) != NULL && q->shown){
464 int rv = -1;
466 if(command == ctrl('L')){ /* just repaint it, and go on */
467 mark_status_unknown();
468 mark_keymenu_dirty();
469 mark_titlebar_dirty();
470 rv = 0;
472 else{ /* ensure sufficient time's passed */
473 time_t now;
474 int diff;
476 now = time(0);
477 diff = (int)(displayed_time - now)
478 + ((command == NO_OP_COMMAND || command == NO_OP_IDLE)
479 ? q->max_display_time
480 : q->min_display_time);
481 dprint((9,
482 "STATUS: diff:%d, displayed: %ld, now: %ld\n",
483 diff, (long) displayed_time, (long) now));
484 if(diff > 0)
485 rv = diff; /* check again next time */
486 else if(is_last_message(q)
487 && (command == NO_OP_COMMAND || command == NO_OP_IDLE)
488 && q->max_display_time)
489 rv = 0; /* last msg, no cmd, has max */
492 if(rv >= 0){ /* leave message displayed? */
493 if(prevstartcol < 0){ /* need to redisplay it? */
494 ding = q->flags & SM_DING;
495 q->flags &= ~SM_DING;
496 if(q->flags & SM_MODAL && !q->shown){
497 copy_of_q = copy_status_queue(q);
498 mark_modals_done();
499 status_message_unlock();
500 output_message_modal(copy_of_q, ding);
502 else{
503 output_message(q, ding);
504 status_message_unlock();
507 else
508 status_message_unlock();
510 return(rv);
513 d_q_status_message(); /* remove it from queue and */
514 needs_clearing++; /* clear the line if needed */
517 if(!top_of_queue() && (command == ctrl('L') || needs_clearing)){
518 int inverse;
519 struct variable *vars = ps_global->vars;
520 char *last_bg = NULL;
522 dprint((9, "Clearing status line\n"));
523 inverse = InverseState(); /* already in inverse? */
524 if(inverse && pico_usingcolor() && VAR_STATUS_FORE_COLOR &&
525 VAR_STATUS_BACK_COLOR){
526 last_bg = pico_get_last_bg_color();
527 pico_set_nbg_color(); /* so ClearLine will clear in bg color */
530 ClearLine(ps_global->ttyo->screen_rows - FOOTER_ROWS(ps_global));
531 if(last_bg){
532 (void)pico_set_bg_color(last_bg);
533 if(last_bg)
534 fs_give((void **)&last_bg);
537 mark_status_unknown();
538 if(command == ctrl('L')){
539 mark_keymenu_dirty();
540 mark_titlebar_dirty();
544 need_to_unlock = 1;
546 /* display next message, weeding out 0 display times */
547 for(q = top_of_queue(); q && !q->shown; q = top_of_queue()){
548 if(q->min_display_time || is_last_message(q)){
549 displayed_time = time(0);
550 ding = q->flags & SM_DING;
551 q->flags &= ~SM_DING;
552 if(q->flags & SM_MODAL && !q->shown){
553 copy_of_q = copy_status_queue(q);
554 mark_modals_done();
555 status_message_unlock();
556 output_message_modal(copy_of_q, ding);
557 need_to_unlock = 0;
559 else
560 output_message(q, ding);
562 break;
564 /* zero display time message, log it, delete it */
565 else{
566 if(q->text){
567 char buf[1000];
568 char *append = " [not actually shown]";
569 char *ptr;
570 size_t len;
572 len = strlen(q->text) + strlen(append);
573 if(len < sizeof(buf))
574 ptr = buf;
575 else
576 ptr = (char *) fs_get((len+1) * sizeof(char));
578 strncpy(ptr, q->text, len);
579 ptr[len] = '\0';
580 strncat(ptr, append, len+1-1-strlen(ptr));
581 ptr[len] = '\0';
582 add_review_message(ptr, -1);
583 if(ptr != buf)
584 fs_give((void **) &ptr);
587 d_q_status_message();
591 needs_clearing = 0; /* always cleared or written */
592 fflush(stdout);
594 if(need_to_unlock)
595 status_message_unlock();
597 return(0);
601 /*----------------------------------------------------------------------
602 Display all the messages on the queue as quickly as possible
603 ----*/
604 void
605 flush_status_messages(int skip_last_pause)
607 SMQ_T *q, *copy_of_q;
608 int ding;
610 start_over:
611 status_message_lock();
613 for(q = top_of_queue(); q; q = top_of_queue()){
614 /* don't have to wait for this one */
615 if(is_last_message(q) && skip_last_pause && q->shown)
616 break;
618 if(q->shown)
619 pause_for_and_dq_cur_msg();
621 /* find next one we need to show and show it */
622 for(q = top_of_queue(); q && !q->shown; q = top_of_queue()){
623 if((q->min_display_time || is_last_message(q))){
624 displayed_time = time(0);
625 ding = q->flags & SM_DING;
626 q->flags &= ~SM_DING;
627 if(q->flags & SM_MODAL){
628 copy_of_q = copy_status_queue(q);
629 mark_modals_done();
630 status_message_unlock();
631 output_message_modal(copy_of_q, ding);
634 * Because we unlock the message queue in order
635 * to display modal messages we have to worry
636 * about the queue being changed while we have
637 * it unlocked. So start the whole process over.
639 goto start_over;
641 else
642 output_message(q, ding);
644 else{
645 d_q_status_message();
650 status_message_unlock();
654 /*----------------------------------------------------------------------
655 Make sure any and all SM_ORDER messages get displayed.
657 Note: This flags the message line as having nothing displayed.
658 The idea is that it's a function called by routines that want
659 the message line for a prompt or something, and that they're
660 going to obliterate the message anyway.
661 ----*/
662 void
663 flush_ordered_messages(void)
665 SMQ_T *q, *copy_of_q;
666 int firsttime = 1;
667 int ding;
669 status_message_lock();
671 set_saw_it_to_zero();
673 start_over2:
674 if(!firsttime)
675 status_message_lock();
676 else
677 firsttime = 0;
679 if((q = message_queue) != NULL){
681 if(q->pending_removal || q->saw_it || q->shown){
682 if(!q->pending_removal && q->shown)
683 pause_for_and_mark_specific_msg(q);
685 q->saw_it = 1;
686 q = (q->next != message_queue) ? q->next : NULL;
688 else{
689 /* find next one we need to show and show it */
691 if(q->pending_removal || q->saw_it || q->shown){
692 q->saw_it = 1;
693 q = (q->next != message_queue) ? q->next : NULL;
695 else{
696 if((q->flags & (SM_ORDER | SM_MODAL))
697 && q->min_display_time){
698 displayed_time = time(0);
699 ding = q->flags & SM_DING;
700 q->flags &= ~SM_DING;
701 if(q->flags & SM_MODAL){
702 copy_of_q = copy_status_queue(q);
703 mark_modals_done();
704 q->saw_it = 1;
705 status_message_unlock();
706 output_message_modal(copy_of_q, ding);
707 goto start_over2;
709 else{
710 output_message(q, ding);
713 else{
714 q->saw_it = 1;
715 if(!(q->flags & SM_ASYNC))
716 q->pending_removal = 1;
718 q = (q->next != message_queue) ? q->next : NULL;
721 }while(q && !q->shown);
723 }while(q);
726 status_message_unlock();
730 void
731 set_saw_it_to_zero()
733 SMQ_T *q;
735 /* set saw_it to zero */
736 if((q = message_queue) != NULL){
738 q->saw_it = 0;
739 q = (q->next != message_queue) ? q->next : NULL;
740 }while(q);
745 void
746 mark_modals_done()
748 SMQ_T *q;
750 /* set shown to one */
751 if((q = message_queue) != NULL){
753 if(q->flags & SM_MODAL){
754 q->shown = 1;
755 q->pending_removal = 1;
758 q = (q->next != message_queue) ? q->next : NULL;
759 }while(q);
765 * Caller needs to free the memory.
767 SMQ_T *
768 copy_status_queue(SMQ_T *start)
770 SMQ_T *q, *new, *head = NULL;
772 if((q = start) != NULL){
774 new = (SMQ_T *) fs_get(sizeof(SMQ_T));
775 *new = *q;
777 if(q->text)
778 new->text = cpystr(q->text);
779 else
780 new->text = NULL;
782 if(head){
783 new->next = head;
784 new->prev = head->prev;
785 new->prev->next = head->prev = new;
787 else
788 head = new->next = new->prev = new;
790 q = (q->next != start) ? q->next : NULL;
791 }while(q);
794 return(head);
798 /*----------------------------------------------------------------------
799 Removes the first message from the message queue.
800 Returns 0 if all was well, -1 if had to skip the removal and
801 message_queue is unchanged.
802 ----*/
803 void
804 d_q_status_message(void)
806 int the_last_one = 0;
807 SMQ_T *q, *p, *last_one;
809 /* mark the top one for removal */
810 if((p = top_of_queue()) != NULL)
811 p->pending_removal = 1;
813 if(message_queue){
815 /* flush out pending removals */
816 the_last_one = 0; /* loop control */
817 q = message_queue;
818 last_one = message_queue->prev;
819 while(!the_last_one){
820 if(q == last_one)
821 the_last_one++;
823 if(q->pending_removal){
824 p = q;
825 if(q == q->next){ /* last item in queue */
826 q = message_queue = NULL;
828 else{
829 p->next->prev = p->prev;
830 q = p->prev->next = p->next; /* here's the increment */
831 if(message_queue == p)
832 message_queue = q;
835 if(p){
836 if(p->text)
837 fs_give((void **) &p->text);
839 fs_give((void **) &p);
842 else
843 q = q->next;
849 /*----------------------------------------------------------------------
850 Actually output the message to the screen
852 Args: message -- The message to output
853 from_alarm_handler -- Called from alarm signal handler.
854 We don't want to add this message to the review
855 message list since we may mess with the malloc
856 arena here, and the interrupt may be from
857 the middle of something malloc'ing.
858 ----*/
860 status_message_write(char *message, int from_alarm_handler)
862 int col, row, max_width, invert;
863 int bytes;
864 char newstatusbuf[6*MAX_SCREEN_COLS + 1];
865 struct variable *vars = ps_global->vars;
866 COLOR_PAIR *lastc = NULL, *newc;
868 if(!from_alarm_handler)
869 add_review_message(message, -1);
871 invert = !InverseState(); /* already in inverse? */
872 row = MAX(0, ps_global->ttyo->screen_rows - FOOTER_ROWS(ps_global));
874 /* Put [] around message and truncate to screen width */
875 max_width = ps_global->ttyo != NULL ? ps_global->ttyo->screen_cols : 80;
876 max_width = MIN(max_width, MAX_SCREEN_COLS);
877 newstatusbuf[0] = '[';
878 newstatusbuf[1] = '\0';
880 bytes = utf8_to_width(newstatusbuf+1, message, sizeof(newstatusbuf)-1, max_width-2, NULL);
881 newstatusbuf[1+bytes] = ']';
882 newstatusbuf[1+bytes+1] = '\0';
884 if(prevstartcol == -1 || strcmp(newstatusbuf, prevstatusbuf)){
885 UCS *prevbuf = NULL, *newbuf = NULL;
886 size_t plen;
888 if(prevstartcol != -1){
889 prevbuf = utf8_to_ucs4_cpystr(prevstatusbuf);
890 newbuf = utf8_to_ucs4_cpystr(newstatusbuf);
894 * Simple optimization. If the strings are the same length
895 * and width just skip leading and trailing strings of common
896 * characters and only write whatever's in between. Otherwise,
897 * write out the whole thing.
898 * We could do something more complicated but the odds of
899 * getting any optimization goes way down if they aren't the
900 * same length and width and the complexity goes way up.
902 if(prevbuf && newbuf
903 && (plen=ucs4_strlen(prevbuf)) == ucs4_strlen(newbuf)
904 && ucs4_str_width(prevbuf) == ucs4_str_width(newbuf)){
905 UCS *start_of_unequal, *end_of_unequal;
906 UCS *pprev, *endnewbuf;
907 char *to_screen = NULL;
908 int column;
910 pprev = prevbuf;
911 start_of_unequal = newbuf;
912 endnewbuf = newbuf + plen;
913 col = column = prevstartcol;
915 while(start_of_unequal < endnewbuf && (*start_of_unequal) == (*pprev)){
916 int w;
918 w = wcellwidth(*start_of_unequal);
919 if(w >= 0)
920 column += w;
922 pprev++;
923 start_of_unequal++;
926 end_of_unequal = endnewbuf-1;
927 pprev = prevbuf + plen - 1;
929 while(end_of_unequal > start_of_unequal && (*end_of_unequal) == (*pprev)){
930 *end_of_unequal = '\0';
931 pprev--;
932 end_of_unequal--;
935 if(pico_usingcolor() && VAR_STATUS_FORE_COLOR &&
936 VAR_STATUS_BACK_COLOR &&
937 pico_is_good_color(VAR_STATUS_FORE_COLOR) &&
938 pico_is_good_color(VAR_STATUS_BACK_COLOR)){
939 lastc = pico_get_cur_color();
940 if(lastc){
941 newc = new_color_pair(VAR_STATUS_FORE_COLOR,
942 VAR_STATUS_BACK_COLOR);
943 (void)pico_set_colorp(newc, PSC_NONE);
944 free_color_pair(&newc);
947 else if(invert)
948 StartInverse();
950 /* PutLine wants UTF-8 string */
951 if(start_of_unequal && (*start_of_unequal))
952 to_screen = ucs4_to_utf8_cpystr(start_of_unequal);
954 if(to_screen){
955 PutLine0(row, column, to_screen);
956 fs_give((void **) &to_screen);
958 strncpy(prevstatusbuf, newstatusbuf, sizeof(prevstatusbuf));
959 prevstatusbuf[sizeof(prevstatusbuf)-1] = '\0';
962 if(lastc){
963 (void)pico_set_colorp(lastc, PSC_NONE);
964 free_color_pair(&lastc);
966 else if(invert)
967 EndInverse();
969 else{
970 int width = (int) utf8_width(newstatusbuf);
972 if(pico_usingcolor())
973 lastc = pico_get_cur_color();
975 if(!invert && pico_usingcolor() && VAR_STATUS_FORE_COLOR &&
976 VAR_STATUS_BACK_COLOR &&
977 pico_is_good_color(VAR_STATUS_FORE_COLOR) &&
978 pico_is_good_color(VAR_STATUS_BACK_COLOR))
979 pico_set_nbg_color(); /* so ClearLine uses bg color */
981 ClearLine(row);
983 if (width > ps_global->ttyo->screen_cols)
984 col = 0;
985 else
986 col = (ps_global->ttyo->screen_cols - width) / 2;
988 MoveCursor(row, col); /* so Inverse will be set correctly */
990 if(pico_usingcolor() && VAR_STATUS_FORE_COLOR &&
991 VAR_STATUS_BACK_COLOR &&
992 pico_is_good_color(VAR_STATUS_FORE_COLOR) &&
993 pico_is_good_color(VAR_STATUS_BACK_COLOR)){
994 if(lastc){
995 newc = new_color_pair(VAR_STATUS_FORE_COLOR,
996 VAR_STATUS_BACK_COLOR);
997 (void)pico_set_colorp(newc, PSC_NONE);
998 free_color_pair(&newc);
1001 else if(invert){
1002 if(lastc)
1003 free_color_pair(&lastc);
1005 StartInverse();
1008 PutLine0(row, col, newstatusbuf);
1010 if(lastc){
1011 (void)pico_set_colorp(lastc, PSC_NONE);
1012 free_color_pair(&lastc);
1014 else if(invert)
1015 EndInverse();
1017 strncpy(prevstatusbuf, newstatusbuf, sizeof(prevstatusbuf));
1018 prevstatusbuf[sizeof(prevstatusbuf)-1] = '\0';
1019 prevstartcol = col;
1020 prevendcol = col + utf8_width(prevstatusbuf) - 1;
1023 /* move cursor to a consistent position */
1024 if(F_ON(F_SHOW_CURSOR, ps_global))
1025 MoveCursor(row, MIN(MAX(0,col+1),ps_global->ttyo->screen_cols-1));
1026 else
1027 MoveCursor(row, 0);
1029 fflush(stdout);
1031 if(prevbuf)
1032 fs_give((void **) &prevbuf);
1034 if(newbuf)
1035 fs_give((void **) &newbuf);
1037 else
1038 col = prevstartcol;
1040 return(col);
1044 /*----------------------------------------------------------------------
1045 Write the given status message to the display.
1047 Args: mq_entry -- pointer to message queue entry to write.
1049 ----*/
1050 int
1051 output_message(SMQ_T *mq_entry, int ding)
1053 int col = 0;
1055 dprint((9, "output_message(%s)\n",
1056 (mq_entry && mq_entry->text) ? mq_entry->text : "?"));
1058 if(ding && F_OFF(F_QUELL_BEEPS, ps_global)){
1059 Writechar(BELL, 0); /* ring bell */
1060 fflush(stdout);
1063 if(!(mq_entry->flags & SM_MODAL)){
1064 col = status_message_write(mq_entry->text, 0);
1065 if(ps_global->status_msg_delay > 0){
1066 if(F_ON(F_SHOW_CURSOR, ps_global))
1067 /* col+1 because col is "[" character */
1068 MoveCursor(ps_global->ttyo->screen_rows-FOOTER_ROWS(ps_global),
1069 MIN(MAX(0,col+1),ps_global->ttyo->screen_cols-1));
1070 else
1071 MoveCursor(ps_global->ttyo->screen_rows-FOOTER_ROWS(ps_global), 0);
1073 fflush(stdout);
1074 if (ps_global && !ps_global->initial_cmds)
1075 sleep(ps_global->status_msg_delay);
1078 mq_entry->shown = 1;
1081 return(col);
1086 * This is split off from output_message due to the locking
1087 * on the status message queue data. This calls scrolltool
1088 * which can call back into q_status and so on. So instead of
1089 * keeping the queue locked for a long time we copy the
1090 * data, unlock the queue, and display the data.
1092 int
1093 output_message_modal(SMQ_T *mq_entry, int ding)
1095 int rv = 0;
1096 SMQ_T *m, *mnext;
1098 if(!mq_entry)
1099 return(rv);
1101 dprint((9, "output_message_modal(%s)\n",
1102 (mq_entry && mq_entry->text) ? mq_entry->text : "?"));
1104 if(ding && F_OFF(F_QUELL_BEEPS, ps_global)){
1105 Writechar(BELL, 0); /* ring bell */
1106 fflush(stdout);
1109 if(!mq_entry->shown){
1110 int i = 0,
1111 pad = MAX(0, (ps_global->ttyo->screen_cols - 59) / 2);
1112 char *p, *q, *s, *t;
1113 SCROLL_S sargs;
1115 /* Count the number of modal messages and add up their lengths. */
1116 for(m = mq_entry->next; m != mq_entry; m = m->next)
1117 if((m->flags & SM_MODAL) && !m->shown){
1118 i++;
1121 snprintf(tmp_20k_buf, SIZEOF_20KBUF, "%*s%s\n%*s%s\n%*s%s\n%*s%s\n%*s%s\n%*s%s\n%*s%s\n\n",
1122 /* 1 2 3 4 5 6*/
1123 /*23456789012345678901234567890123456789012345678901234567890*/
1124 pad, "",
1125 "***********************************************************",
1126 pad, "", i ?
1127 "* What follows are advisory messages. After reading them *" :
1128 "* What follows is an advisory message. After reading it *",
1130 pad, "",
1131 "* simply hit \"Return\" to continue your Alpine session. *",
1132 pad, "",
1133 "* *",
1134 pad, "", i ?
1135 "* To review these messages later, press 'J' from the *" :
1136 "* To review this message later, press 'J' from the *",
1137 pad, "",
1138 "* MAIN MENU. *",
1139 pad, "",
1140 "***********************************************************");
1141 tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
1142 t = tmp_20k_buf + strlen(tmp_20k_buf);
1144 m = mq_entry;
1146 if((m->flags & SM_MODAL) && !m->shown){
1147 int indent;
1149 indent = ps_global->ttyo->screen_cols > 80
1150 ? (ps_global->ttyo->screen_cols - 80) / 3 : 0;
1152 if(t - tmp_20k_buf > 19000){
1153 snprintf(t, SIZEOF_20KBUF-(t-tmp_20k_buf), "\n%*s* * * Running out of buffer space * * *", indent, "");
1154 tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
1155 t += strlen(t);
1156 snprintf(t, SIZEOF_20KBUF-(t-tmp_20k_buf), "\n%*s* * * Press RETURN for more messages * * *", indent, "");
1157 tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
1158 break;
1161 add_review_message(m->text, -1);
1163 if((p = strstr(m->text, "[ALERT]")) != NULL){
1164 snprintf(t, SIZEOF_20KBUF-(t-tmp_20k_buf), "%*.*s\n", (int)(indent + p - m->text), (int) (p - m->text), m->text);
1165 tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
1166 t += strlen(t);
1168 for(p += 7; *p && isspace((unsigned char)*p); p++)
1170 indent += 8;
1172 else{
1173 p = m->text;
1176 while(strlen(p) > ps_global->ttyo->screen_cols - 2 * indent){
1177 for(q = p + ps_global->ttyo->screen_cols - 2 * indent;
1178 q > p && !isspace((unsigned char)*q); q--)
1181 snprintf(t, SIZEOF_20KBUF-(t-tmp_20k_buf), "\n%*.*s", (int) (indent + q - p), (int) (q - p), p);
1182 tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
1183 t += strlen(t);
1184 p = q + 1;
1187 snprintf(t, SIZEOF_20KBUF-(t-tmp_20k_buf), "\n%*s%s", indent, "", p);
1188 tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
1189 t += strlen(t);
1191 if(i--){
1192 snprintf(t, SIZEOF_20KBUF-(t-tmp_20k_buf), "\n\n%*s\n", pad + 30, "- - -");
1193 tmp_20k_buf[SIZEOF_20KBUF-1] = '\0';
1194 t += strlen(t);
1197 m = m->next;
1198 } while(m != mq_entry);
1200 s = cpystr(tmp_20k_buf);
1201 ClearLine(ps_global->ttyo->screen_rows - FOOTER_ROWS(ps_global));
1203 memset(&sargs, 0, sizeof(SCROLL_S));
1204 sargs.text.text = s;
1205 sargs.text.src = CharStar;
1206 sargs.bar.title = "Status Message";
1207 sargs.bogus_input = modal_bogus_input;
1208 sargs.no_stat_msg = 1;
1209 sargs.keys.menu = &modal_message_keymenu;
1210 setbitmap(sargs.keys.bitmap);
1212 scrolltool(&sargs);
1214 fs_give((void **)&s);
1215 ps_global->mangled_screen = 1;
1218 /* free the passed in queue */
1219 m = mq_entry;
1221 if(m->text)
1222 fs_give((void **) &m->text);
1224 mnext = m->next;
1225 fs_give((void **) &m);
1226 m = mnext;
1227 } while(m != mq_entry);
1229 return(rv);
1234 /*----------------------------------------------------------------------
1235 Write or clear delay cue
1237 Args: on -- whether to turn it on or not
1239 ----*/
1240 void
1241 delay_cmd_cue(int on)
1243 COLOR_PAIR *lastc;
1244 struct variable *vars = ps_global->vars;
1246 if(prevstartcol >= 0 && prevendcol < ps_global->ttyo->screen_cols){
1247 MoveCursor(ps_global->ttyo->screen_rows - FOOTER_ROWS(ps_global),
1248 MAX(prevstartcol - 1, 0));
1249 lastc = pico_set_colors(VAR_STATUS_FORE_COLOR, VAR_STATUS_BACK_COLOR,
1250 PSC_REV|PSC_RET);
1251 Write_to_screen(on ? (prevstartcol ? "[>" : ">")
1252 : (prevstartcol ? " [" : "["));
1254 MoveCursor(ps_global->ttyo->screen_rows - FOOTER_ROWS(ps_global),
1255 prevendcol);
1257 Write_to_screen(on ? (prevendcol < ps_global->ttyo->screen_cols-1 ? "<]" : "<")
1258 : (prevendcol < ps_global->ttyo->screen_cols-1 ? "] " : "]"));
1260 if(lastc){
1261 (void)pico_set_colorp(lastc, PSC_NONE);
1262 free_color_pair(&lastc);
1265 if(F_ON(F_SHOW_CURSOR, ps_global))
1266 MoveCursor(ps_global->ttyo->screen_rows-FOOTER_ROWS(ps_global),
1267 MIN(MAX(0,(prevstartcol + on ? 2 : 1)),ps_global->ttyo->screen_cols-1));
1268 else
1269 MoveCursor(ps_global->ttyo->screen_rows-FOOTER_ROWS(ps_global), 0);
1272 fflush(stdout);
1273 #ifdef _WINDOWS
1274 mswin_setcursor ((on) ? MSWIN_CURSOR_BUSY : MSWIN_CURSOR_ARROW);
1275 #endif
1280 * modal_bogus_input - used by scrolltool to complain about
1281 * invalid user input.
1284 modal_bogus_input(UCS ch)
1286 char s[MAX_SCREEN_COLS+1];
1288 snprintf(s, sizeof(s), _("Command \"%s\" not allowed. Press RETURN to continue Alpine."),
1289 pretty_command(ch));
1290 s[sizeof(s)-1] = '\0';
1291 status_message_write(s, 0);
1292 Writechar(BELL, 0);
1293 return(0);
1296 void
1297 free_message_queue(void)
1299 SMQ_T *m, *mnext;
1301 if(message_queue == NULL)
1302 return;
1304 m = message_queue;
1306 if(m->text)
1307 fs_give((void **) &m->text);
1309 mnext = m->next;
1310 fs_give((void **) &m);
1311 m = mnext;
1312 } while(m != message_queue);
1313 message_queue = NULL;