/[LeafOK_CVS]/lbbs/src/screen.c
ViewVC logotype

Diff of /lbbs/src/screen.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.110 by sysadm, Thu Oct 16 11:26:16 2025 UTC Revision 1.127 by sysadm, Sat Nov 8 08:21:31 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                    screen.c  -  description  /*
3                                                           -------------------   * screen
4          Copyright            : (C) 2004-2025 by Leaflet   *   - advanced telnet-based user interactive input / output features
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9  #include "bbs.h"  #include "bbs.h"
10  #include "common.h"  #include "common.h"
# Line 29  Line 21 
21  #include <string.h>  #include <string.h>
22  #include <stdlib.h>  #include <stdlib.h>
23  #include <unistd.h>  #include <unistd.h>
24    #include <wchar.h>
25  #include <sys/param.h>  #include <sys/param.h>
26  #include <sys/stat.h>  #include <sys/stat.h>
27  #include <sys/shm.h>  #include <sys/shm.h>
28  #include <sys/types.h>  #include <sys/types.h>
29    
30  #define ACTIVE_BOARD_HEIGHT 8  const char CTRL_SEQ_CLR_LINE[] = "\033[K";
31    
32  #define STR_TOP_LEFT_MAX_LEN 80  static const int ACTIVE_BOARD_HEIGHT = 8;
 #define STR_TOP_MIDDLE_MAX_LEN 40  
 #define STR_TOP_RIGHT_MAX_LEN 80  
33    
34  static const char *get_time_str(char *s, size_t len)  static const int STR_TOP_LEFT_MAX_LEN = 80;
35    static const int STR_TOP_MIDDLE_MAX_LEN = 40;
36    static const int STR_TOP_RIGHT_MAX_LEN = 80;
37    
38    static size_t get_time_str(char *s, size_t len)
39  {  {
         static const char *weekday[] = {  
                 "天", "一", "二", "三", "四", "五", "六"};  
40          time_t curtime;          time_t curtime;
41          struct tm local_tm;          struct tm local_tm;
42    
43          time(&curtime);          time(&curtime);
44          localtime_r(&curtime, &local_tm);          localtime_r(&curtime, &local_tm);
45          size_t j = strftime(s, len, "%b %d %H:%M 星期", &local_tm);          size_t j = strftime(s, len, "%m/%d %H:%M %Z", &local_tm);
   
         if (j == 0 || j + strlen(weekday[local_tm.tm_wday]) + 1 > len)  
         {  
                 return NULL;  
         }  
46    
47          strncat(s, weekday[local_tm.tm_wday], len - 1 - j);          return j;
   
         return s;  
48  }  }
49    
50  void moveto(int row, int col)  void moveto(int row, int col)
# Line 102  void clearscr() Line 88  void clearscr()
88          moveto(0, 0);          moveto(0, 0);
89  }  }
90    
91  int press_any_key()  inline int press_any_key()
92  {  {
93            return press_any_key_ex("                           \033[1;33m按任意键继续...\033[m", 60);
94    }
95    
96    int press_any_key_ex(const char *msg, int sec)
97    {
98            int ch = 0;
99            int duration = 0;
100            time_t t_begin = time(NULL);
101    
102          moveto(SCREEN_ROWS, 0);          moveto(SCREEN_ROWS, 0);
103          clrtoeol();          clrtoeol();
104    
105          prints("                           \033[1;33m按任意键继续...\033[0;37m");          prints(msg);
106          iflush();          iflush();
107    
108          return igetch_t(MIN(MAX_DELAY_TIME, 60));          igetch_reset();
109    
110            do
111            {
112                    ch = igetch_t(sec - duration);
113                    duration = (int)(time(NULL) - t_begin);
114            } while (!SYS_server_exit && ch == 0 && duration < 60);
115    
116            return ch;
117  }  }
118    
119  void set_input_echo(int echo)  void set_input_echo(int echo)
# Line 118  void set_input_echo(int echo) Line 121  void set_input_echo(int echo)
121          if (echo)          if (echo)
122          {          {
123                  outc('\x83'); // ASCII code 131                  outc('\x83'); // ASCII code 131
                 iflush();  
124          }          }
125          else          else
126          {          {
127                  //    outc ('\x85'); // ASCII code 133                  // outc ('\x85'); // ASCII code 133
128                  prints("\xff\xfb\x01\xff\xfb\x03");                  prints("\xff\xfb\x01\xff\xfb\x03");
                 iflush();  
                 igetch(0);  
                 igetch_reset();  
129          }          }
130            iflush();
131  }  }
132    
133  static int _str_input(char *buffer, int buf_size, int max_display_len, int echo_mode)  static int _str_input(char *buffer, int buf_size, int max_display_len, enum io_echo_t echo_mode)
134  {  {
135          int ch;          int ch;
136          int offset = 0;          int offset = 0;
137          int eol;          int eol;
138          int display_len;          int display_len;
139          char input_str[4];          char input_str[5];
140          int str_len = 0;          int str_len = 0;
141            wchar_t wcs[2];
142          char c;          char c;
143    
144          buffer[buf_size - 1] = '\0';          buffer[buf_size - 1] = '\0';
# Line 147  static int _str_input(char *buffer, int Line 148  static int _str_input(char *buffer, int
148    
149          while (!SYS_server_exit)          while (!SYS_server_exit)
150          {          {
151                  ch = igetch_t(MIN(MAX_DELAY_TIME, 60));                  ch = igetch_t(MIN(BBS_max_user_idle_time, 60));
152    
153                  if (ch == CR)                  if (ch == CR)
154                  {                  {
                         igetch_reset();  
155                          break;                          break;
156                  }                  }
157                  else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe                  else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe
# Line 169  static int _str_input(char *buffer, int Line 169  static int _str_input(char *buffer, int
169                                  offset--;                                  offset--;
170                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
171                                  {                                  {
172                                          while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000)                                          while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0)
173                                          {                                          {
174                                                  offset--;                                                  offset--;
175                                          }                                          }
# Line 190  static int _str_input(char *buffer, int Line 190  static int _str_input(char *buffer, int
190                  else if ((ch & 0xff80) == 0x80) // head of multi-byte character                  else if ((ch & 0xff80) == 0x80) // head of multi-byte character
191                  {                  {
192                          str_len = 0;                          str_len = 0;
193                          c = (char)(ch & 0b11110000);                          c = (char)(ch & 0xf0);
194                          while (c & 0b10000000)                          while (c & 0x80)
195                          {                          {
196                                  input_str[str_len] = (char)(ch - 256);                                  input_str[str_len] = (char)(ch - 256);
197                                  str_len++;                                  str_len++;
198                                  c = (c & 0b01111111) << 1;                                  c = (c & 0x7f) << 1;
199    
200                                  if ((c & 0b10000000) == 0) // Input completed                                  if ((c & 0x80) == 0) // Input completed
201                                  {                                  {
202                                          break;                                          break;
203                                  }                                  }
# Line 213  static int _str_input(char *buffer, int Line 213  static int _str_input(char *buffer, int
213                                          break;                                          break;
214                                  }                                  }
215                          }                          }
216                            input_str[str_len] = '\0';
217    
218                          if (str_len == 0) // Incomplete input                          if (str_len == 0) // Incomplete input
219                          {                          {
220                                  continue;                                  continue;
221                          }                          }
222    
223                          if (offset + str_len > buf_size - 1 || display_len + 2 > max_display_len) // No enough space for Chinese character                          if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
224                            {
225                                    log_error("mbstowcs() error\n");
226                            }
227                            if (offset + str_len > buf_size - 1 || display_len + (UTF8_fixed_width ? 2 : wcwidth(wcs[0])) > max_display_len) // No enough space for Chinese character
228                          {                          {
229                                  outc('\a');                                  outc('\a');
230                                  iflush();                                  iflush();
# Line 275  static int _str_input(char *buffer, int Line 280  static int _str_input(char *buffer, int
280          return offset;          return offset;
281  }  }
282    
283  int str_input(char *buffer, int buf_size, int echo_mode)  int str_input(char *buffer, int buf_size, enum io_echo_t echo_mode)
284  {  {
285          int len;          int len;
286    
# Line 287  int str_input(char *buffer, int buf_size Line 292  int str_input(char *buffer, int buf_size
292          iflush();          iflush();
293    
294          return len;          return len;
295  };  }
296    
297  int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int max_display_len)  int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int max_display_len)
298  {  {
# Line 297  int get_data(int row, int col, char *pro Line 302  int get_data(int row, int col, char *pro
302          int offset = 0;          int offset = 0;
303          int eol;          int eol;
304          int display_len;          int display_len;
305          char input_str[4];          char input_str[5];
306            wchar_t wcs[2];
307          int str_len = 0;          int str_len = 0;
308          char c;          char c;
309    
# Line 318  int get_data(int row, int col, char *pro Line 324  int get_data(int row, int col, char *pro
324    
325          while (!SYS_server_exit)          while (!SYS_server_exit)
326          {          {
327                  ch = igetch_t(MIN(MAX_DELAY_TIME, 60));                  ch = igetch_t(MIN(BBS_max_user_idle_time, 60));
328    
329                  if (ch == CR)                  if (ch == CR)
330                  {                  {
                         igetch_reset();  
331                          break;                          break;
332                  }                  }
333                  else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe                  else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe
# Line 341  int get_data(int row, int col, char *pro Line 346  int get_data(int row, int col, char *pro
346                                  offset--;                                  offset--;
347                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
348                                  {                                  {
349                                          while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000)                                          while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0)
350                                          {                                          {
351                                                  str_len++;                                                  str_len++;
352                                                  offset--;                                                  offset--;
# Line 371  int get_data(int row, int col, char *pro Line 376  int get_data(int row, int col, char *pro
376                                  if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character                                  if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character
377                                  {                                  {
378                                          str_len = 0;                                          str_len = 0;
379                                          c = (char)(buffer[offset] & 0b11110000);                                          c = (char)(buffer[offset] & 0xf0);
380                                          while (c & 0b10000000)                                          while (c & 0x80)
381                                          {                                          {
382                                                  str_len++;                                                  str_len++;
383                                                  c = (c & 0b01111111) << 1;                                                  c = (c & 0x7f) << 1;
384                                          }                                          }
385                                          display_len--;                                          display_len--;
386                                  }                                  }
# Line 405  int get_data(int row, int col, char *pro Line 410  int get_data(int row, int col, char *pro
410                                  offset--;                                  offset--;
411                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
412                                  {                                  {
413                                          while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000)                                          while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0)
414                                          {                                          {
415                                                  str_len++;                                                  str_len++;
416                                                  offset--;                                                  offset--;
# Line 426  int get_data(int row, int col, char *pro Line 431  int get_data(int row, int col, char *pro
431                                  str_len = 0;                                  str_len = 0;
432                                  if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character                                  if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character
433                                  {                                  {
434                                          c = (char)(buffer[offset] & 0b11110000);                                          c = (char)(buffer[offset] & 0xf0);
435                                          while (c & 0b10000000)                                          while (c & 0x80)
436                                          {                                          {
437                                                  str_len++;                                                  str_len++;
438                                                  c = (c & 0b01111111) << 1;                                                  c = (c & 0x7f) << 1;
439                                          }                                          }
440                                          col_cur++;                                          col_cur++;
441                                  }                                  }
# Line 478  int get_data(int row, int col, char *pro Line 483  int get_data(int row, int col, char *pro
483                  else if ((ch & 0xff80) == 0x80) // head of multi-byte character                  else if ((ch & 0xff80) == 0x80) // head of multi-byte character
484                  {                  {
485                          str_len = 0;                          str_len = 0;
486                          c = (char)(ch & 0b11110000);                          c = (char)(ch & 0xf0);
487                          while (c & 0b10000000)                          while (c & 0x80)
488                          {                          {
489                                  input_str[str_len] = (char)(ch - 256);                                  input_str[str_len] = (char)(ch - 256);
490                                  str_len++;                                  str_len++;
491                                  c = (c & 0b01111111) << 1;                                  c = (c & 0x7f) << 1;
492    
493                                  if ((c & 0b10000000) == 0) // Input completed                                  if ((c & 0x80) == 0) // Input completed
494                                  {                                  {
495                                          break;                                          break;
496                                  }                                  }
# Line 501  int get_data(int row, int col, char *pro Line 506  int get_data(int row, int col, char *pro
506                                          break;                                          break;
507                                  }                                  }
508                          }                          }
509                            input_str[str_len] = '\0';
510    
511                          if (str_len == 0) // Incomplete input                          if (str_len == 0) // Incomplete input
512                          {                          {
513                                  continue;                                  continue;
514                          }                          }
515    
516                          if (len + str_len > buf_size - 1 || display_len + 2 > max_display_len) // No enough space for Chinese character                          if (mbstowcs(wcs, input_str, 1) == (size_t)-1)
517                            {
518                                    log_error("mbstowcs() error\n");
519                            }
520                            if (len + str_len > buf_size - 1 || display_len + (UTF8_fixed_width ? 2 : wcwidth(wcs[0])) > max_display_len) // No enough space for Chinese character
521                          {                          {
522                                  outc('\a');                                  outc('\a');
523                                  iflush();                                  iflush();
# Line 595  int display_data(const void *p_data, lon Line 605  int display_data(const void *p_data, lon
605          loop = 1;          loop = 1;
606          while (!SYS_server_exit && loop)          while (!SYS_server_exit && loop)
607          {          {
608                  if (eof_exit > 0 && line_current >= display_line_total && display_line_total <= screen_row_total)                  if (eof_exit > 0 && line_current >= display_line_total)
609                  {                  {
610                          if (eof_exit == 1)                          if (eof_exit == 1)
611                          {                          {
# Line 650  int display_data(const void *p_data, lon Line 660  int display_data(const void *p_data, lon
660                          input_ok = 0;                          input_ok = 0;
661                          while (!SYS_server_exit && !input_ok)                          while (!SYS_server_exit && !input_ok)
662                          {                          {
663                                  ch = igetch_t(MAX_DELAY_TIME);                                  ch = igetch_t(BBS_max_user_idle_time);
664                                  input_ok = 1;                                  input_ok = 1;
665    
666                                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)                                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
# Line 704  int display_data(const void *p_data, lon Line 714  int display_data(const void *p_data, lon
714                                          output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line                                          output_end_row = SCREEN_ROWS - 1; // Legacy Fterm only works with this line
715                                          break;                                          break;
716                                  case CR:                                  case CR:
                                         igetch_reset();  
                                 case KEY_SPACE:  
717                                  case KEY_DOWN:                                  case KEY_DOWN:
718                                          if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= display_line_total) // Reach end                                          if (line_current + (screen_row_total - (output_current_row - screen_begin_row)) >= display_line_total) // Reach end
719                                          {                                          {
# Line 733  int display_data(const void *p_data, lon Line 741  int display_data(const void *p_data, lon
741                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
742                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
743                                          break;                                          break;
744                                    case KEY_SPACE:
745                                  case KEY_PGDN:                                  case KEY_PGDN:
746                                          if (line_current + screen_row_total - (output_current_row - screen_begin_row) >= display_line_total) // Reach end                                          if (line_current + screen_row_total - (output_current_row - screen_begin_row) >= display_line_total) // Reach end
747                                          {                                          {
# Line 806  cleanup: Line 815  cleanup:
815          return ch;          return ch;
816  }  }
817    
818  static int display_file_key_handler(int *p_key, DISPLAY_CTX *p_ctx)  int display_file_key_handler(int *p_key, DISPLAY_CTX *p_ctx)
819  {  {
820          switch (*p_key)          switch (*p_key)
821          {          {
# Line 889  int show_top(const char *str_left, const Line 898  int show_top(const char *str_left, const
898  int show_bottom(const char *msg)  int show_bottom(const char *msg)
899  {  {
900          char str_time[LINE_BUFFER_LEN];          char str_time[LINE_BUFFER_LEN];
901            int len_str_time;
902          time_t time_online;          time_t time_online;
903          struct tm *tm_online;          struct tm *tm_online;
904          char msg_f[LINE_BUFFER_LEN];          char msg_f[LINE_BUFFER_LEN];
905          int eol;          int eol;
906          int msg_len;          int len_msg;
907          int len;          int len;
908          int len_username;          int len_username;
909          char str_tm_online[LINE_BUFFER_LEN];          char str_tm_online[LINE_BUFFER_LEN];
910            int len_str_tm_online;
911    
912          get_time_str(str_time, sizeof(str_time));          len_str_time = (int)get_time_str(str_time, sizeof(str_time));
913    
914          msg_f[0] = '\0';          msg_f[0] = '\0';
915          msg_len = 0;          len_msg = 0;
916          if (msg != NULL)          if (msg != NULL)
917          {          {
918                  strncpy(msg_f, msg, sizeof(msg_f) - 1);                  strncpy(msg_f, msg, sizeof(msg_f) - 1);
919                  msg_f[sizeof(msg_f) - 1] = '\0';                  msg_f[sizeof(msg_f) - 1] = '\0';
920                  len = split_line(msg_f, 23, &eol, &msg_len, 1);                  len = split_line(msg_f, 23, &eol, &len_msg, 1);
921                  msg_f[len] = '\0';                  msg_f[len] = '\0';
922          }          }
923    
# Line 917  int show_bottom(const char *msg) Line 928  int show_bottom(const char *msg)
928          if (tm_online->tm_mday > 1)          if (tm_online->tm_mday > 1)
929          {          {
930                  snprintf(str_tm_online, sizeof(str_tm_online),                  snprintf(str_tm_online, sizeof(str_tm_online),
931                                   "\033[36m%2d\033[33m天\033[36m%2d\033[33m时",                                   "\033[36m%d\033[33md \033[36m%d\033[33m:\033[36m%.2d",
932                                   tm_online->tm_mday - 1, tm_online->tm_hour);                                   tm_online->tm_mday - 1, tm_online->tm_hour, tm_online->tm_min);
933          }          }
934          else          else
935          {          {
936                  snprintf(str_tm_online, sizeof(str_tm_online),                  snprintf(str_tm_online, sizeof(str_tm_online),
937                                   "\033[36m%2d\033[33m时\033[36m%2d\033[33m分",                                   "\033[36m%d\033[33m:\033[36m%.2d",
938                                   tm_online->tm_hour, tm_online->tm_min);                                   tm_online->tm_hour, tm_online->tm_min);
939          }          }
940            len_str_tm_online = str_length(str_tm_online, 1);
941    
942          moveto(SCREEN_ROWS, 0);          moveto(SCREEN_ROWS, 0);
943          clrtoeol();          clrtoeol();
944          prints("\033[1;44;33m时间[\033[36m%s\033[33m]%s%*s \033[33m帐号[\033[36m%s\033[33m][%s\033[33m]\033[m",          prints("\033[1;44;33m时间[\033[36m%s\033[33m]%s%*s \033[33m用户[\033[36m%s\033[33m][%s\033[33m]\033[m",
945                     str_time, msg_f, 38 - msg_len - len_username, "", BBS_username, str_tm_online);                     str_time, msg_f, 65 - len_str_time - len_msg - len_username - len_str_tm_online,
946                       "", BBS_username, str_tm_online);
947    
948          return 0;          return 0;
949  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1