/[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.111 by sysadm, Fri Oct 17 01:25:08 2025 UTC Revision 1.129 by sysadm, Tue Nov 11 00:28:05 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     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "bbs.h"  #include "bbs.h"
14  #include "common.h"  #include "common.h"
# Line 29  Line 25 
25  #include <string.h>  #include <string.h>
26  #include <stdlib.h>  #include <stdlib.h>
27  #include <unistd.h>  #include <unistd.h>
28    #include <wchar.h>
29  #include <sys/param.h>  #include <sys/param.h>
30  #include <sys/stat.h>  #include <sys/stat.h>
31  #include <sys/shm.h>  #include <sys/shm.h>
32  #include <sys/types.h>  #include <sys/types.h>
33    
34  #define ACTIVE_BOARD_HEIGHT 8  const char CTRL_SEQ_CLR_LINE[] = "\033[K";
35    
36    static const int ACTIVE_BOARD_HEIGHT = 8;
37    
38  #define STR_TOP_LEFT_MAX_LEN 80  static const int STR_TOP_LEFT_MAX_LEN = 80;
39  #define STR_TOP_MIDDLE_MAX_LEN 40  static const int STR_TOP_MIDDLE_MAX_LEN = 40;
40  #define STR_TOP_RIGHT_MAX_LEN 80  static const int STR_TOP_RIGHT_MAX_LEN = 80;
41    
42  static const char *get_time_str(char *s, size_t len)  static size_t get_time_str(char *s, size_t len)
43  {  {
         static const char *weekday[] = {  
                 "天", "一", "二", "三", "四", "五", "六"};  
44          time_t curtime;          time_t curtime;
45          struct tm local_tm;          struct tm local_tm;
46    
47          time(&curtime);          time(&curtime);
48          localtime_r(&curtime, &local_tm);          localtime_r(&curtime, &local_tm);
49          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;  
         }  
50    
51          strncat(s, weekday[local_tm.tm_wday], len - 1 - j);          return j;
   
         return s;  
52  }  }
53    
54  void moveto(int row, int col)  void moveto(int row, int col)
# Line 102  void clearscr() Line 92  void clearscr()
92          moveto(0, 0);          moveto(0, 0);
93  }  }
94    
95  int press_any_key()  inline int press_any_key()
96    {
97            return press_any_key_ex("                           \033[1;33m按任意键继续...\033[m", 60);
98    }
99    
100    int press_any_key_ex(const char *msg, int sec)
101  {  {
102            int ch = 0;
103            int duration = 0;
104            time_t t_begin = time(NULL);
105    
106          moveto(SCREEN_ROWS, 0);          moveto(SCREEN_ROWS, 0);
107          clrtoeol();          clrtoeol();
108    
109          prints("                           \033[1;33m按任意键继续...\033[0;37m");          prints(msg);
110          iflush();          iflush();
111    
112          return igetch_t(MIN(MAX_DELAY_TIME, 60));          igetch_reset();
113    
114            do
115            {
116                    ch = igetch_t(sec - duration);
117                    duration = (int)(time(NULL) - t_begin);
118            } while (!SYS_server_exit && ch == 0 && duration < 60);
119    
120            return ch;
121  }  }
122    
123  void set_input_echo(int echo)  void set_input_echo(int echo)
# Line 127  void set_input_echo(int echo) Line 134  void set_input_echo(int echo)
134          iflush();          iflush();
135  }  }
136    
137  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)
138  {  {
139          int ch;          int ch;
140          int offset = 0;          int offset = 0;
141          int eol;          int eol;
142          int display_len;          int display_len;
143          char input_str[4];          char input_str[5];
144          int str_len = 0;          int str_len = 0;
145            wchar_t wcs[2];
146          char c;          char c;
147    
148          buffer[buf_size - 1] = '\0';          buffer[buf_size - 1] = '\0';
# Line 144  static int _str_input(char *buffer, int Line 152  static int _str_input(char *buffer, int
152    
153          while (!SYS_server_exit)          while (!SYS_server_exit)
154          {          {
155                  ch = igetch_t(MIN(MAX_DELAY_TIME, 60));                  ch = igetch_t(MIN(BBS_max_user_idle_time, 60));
156    
157                  if (ch == CR)                  if (ch == CR)
158                  {                  {
# Line 165  static int _str_input(char *buffer, int Line 173  static int _str_input(char *buffer, int
173                                  offset--;                                  offset--;
174                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
175                                  {                                  {
176                                          while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000)                                          while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0)
177                                          {                                          {
178                                                  offset--;                                                  offset--;
179                                          }                                          }
# Line 186  static int _str_input(char *buffer, int Line 194  static int _str_input(char *buffer, int
194                  else if ((ch & 0xff80) == 0x80) // head of multi-byte character                  else if ((ch & 0xff80) == 0x80) // head of multi-byte character
195                  {                  {
196                          str_len = 0;                          str_len = 0;
197                          c = (char)(ch & 0b11110000);                          c = (char)(ch & 0xf0);
198                          while (c & 0b10000000)                          while (c & 0x80)
199                          {                          {
200                                  input_str[str_len] = (char)(ch - 256);                                  input_str[str_len] = (char)(ch - 256);
201                                  str_len++;                                  str_len++;
202                                  c = (c & 0b01111111) << 1;                                  c = (c & 0x7f) << 1;
203    
204                                  if ((c & 0b10000000) == 0) // Input completed                                  if ((c & 0x80) == 0) // Input completed
205                                  {                                  {
206                                          break;                                          break;
207                                  }                                  }
# Line 209  static int _str_input(char *buffer, int Line 217  static int _str_input(char *buffer, int
217                                          break;                                          break;
218                                  }                                  }
219                          }                          }
220                            input_str[str_len] = '\0';
221    
222                          if (str_len == 0) // Incomplete input                          if (str_len == 0) // Incomplete input
223                          {                          {
224                                  continue;                                  continue;
225                          }                          }
226    
227                          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)
228                            {
229                                    log_error("mbstowcs() error\n");
230                            }
231                            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
232                          {                          {
233                                  outc('\a');                                  outc('\a');
234                                  iflush();                                  iflush();
# Line 271  static int _str_input(char *buffer, int Line 284  static int _str_input(char *buffer, int
284          return offset;          return offset;
285  }  }
286    
287  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)
288  {  {
289          int len;          int len;
290    
# Line 283  int str_input(char *buffer, int buf_size Line 296  int str_input(char *buffer, int buf_size
296          iflush();          iflush();
297    
298          return len;          return len;
299  };  }
300    
301  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)
302  {  {
# Line 293  int get_data(int row, int col, char *pro Line 306  int get_data(int row, int col, char *pro
306          int offset = 0;          int offset = 0;
307          int eol;          int eol;
308          int display_len;          int display_len;
309          char input_str[4];          char input_str[5];
310          int str_len = 0;          int str_len = 0;
311            wchar_t wcs[2];
312            int wc_len;
313          char c;          char c;
314    
315          buffer[buf_size - 1] = '\0';          buffer[buf_size - 1] = '\0';
# Line 314  int get_data(int row, int col, char *pro Line 329  int get_data(int row, int col, char *pro
329    
330          while (!SYS_server_exit)          while (!SYS_server_exit)
331          {          {
332                  ch = igetch_t(MIN(MAX_DELAY_TIME, 60));                  ch = igetch_t(MIN(BBS_max_user_idle_time, 60));
333    
334                  if (ch == CR)                  if (ch == CR)
335                  {                  {
# Line 336  int get_data(int row, int col, char *pro Line 351  int get_data(int row, int col, char *pro
351                                  offset--;                                  offset--;
352                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
353                                  {                                  {
354                                          while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000)                                          while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0)
355                                          {                                          {
356                                                  str_len++;                                                  str_len++;
357                                                  offset--;                                                  offset--;
358                                          }                                          }
359                                          display_len--;  
360                                          col_cur--;                                          if (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1)
361                                            {
362                                                    log_error("mbstowcs() error\n");
363                                            }
364                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
365    
366                                            if (wc_len == 2)
367                                            {
368                                                    display_len--;
369                                                    col_cur--;
370                                            }
371                                  }                                  }
372    
373                                  memmove(buffer + offset, buffer + offset + str_len, (size_t)(len - offset - str_len));                                  memmove(buffer + offset, buffer + offset + str_len, (size_t)(len - offset - str_len));
# Line 366  int get_data(int row, int col, char *pro Line 391  int get_data(int row, int col, char *pro
391                                  if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character                                  if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character
392                                  {                                  {
393                                          str_len = 0;                                          str_len = 0;
394                                          c = (char)(buffer[offset] & 0b11110000);                                          c = (char)(buffer[offset] & 0xf0);
395                                          while (c & 0b10000000)                                          while (c & 0x80)
396                                          {                                          {
397                                                  str_len++;                                                  str_len++;
398                                                  c = (c & 0b01111111) << 1;                                                  c = (c & 0x7f) << 1;
399                                          }                                          }
400                                          display_len--;                                          display_len--;
401                                  }                                  }
# Line 400  int get_data(int row, int col, char *pro Line 425  int get_data(int row, int col, char *pro
425                                  offset--;                                  offset--;
426                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
427                                  {                                  {
428                                          while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000)                                          while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0)
429                                          {                                          {
430                                                  str_len++;                                                  str_len++;
431                                                  offset--;                                                  offset--;
432                                          }                                          }
433                                          col_cur--;  
434                                            if (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1)
435                                            {
436                                                    log_error("mbstowcs() error\n");
437                                            }
438                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
439    
440                                            if (wc_len == 2)
441                                            {
442                                                    col_cur--;
443                                            }
444                                  }                                  }
445                                  col_cur--;                                  col_cur--;
446    
# Line 421  int get_data(int row, int col, char *pro Line 456  int get_data(int row, int col, char *pro
456                                  str_len = 0;                                  str_len = 0;
457                                  if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character                                  if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character
458                                  {                                  {
459                                          c = (char)(buffer[offset] & 0b11110000);                                          c = (char)(buffer[offset] & 0xf0);
460                                          while (c & 0b10000000)                                          while (c & 0x80)
461                                          {                                          {
462                                                  str_len++;                                                  str_len++;
463                                                  c = (c & 0b01111111) << 1;                                                  c = (c & 0x7f) << 1;
464                                            }
465    
466                                            if (mbstowcs(wcs, buffer + offset, 1) == (size_t)-1)
467                                            {
468                                                    log_error("mbstowcs() error\n");
469                                            }
470                                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
471    
472                                            if (wc_len == 2)
473                                            {
474                                                    col_cur++;
475                                          }                                          }
                                         col_cur++;  
476                                  }                                  }
477                                  else                                  else
478                                  {                                  {
# Line 473  int get_data(int row, int col, char *pro Line 518  int get_data(int row, int col, char *pro
518                  else if ((ch & 0xff80) == 0x80) // head of multi-byte character                  else if ((ch & 0xff80) == 0x80) // head of multi-byte character
519                  {                  {
520                          str_len = 0;                          str_len = 0;
521                          c = (char)(ch & 0b11110000);                          c = (char)(ch & 0xf0);
522                          while (c & 0b10000000)                          while (c & 0x80)
523                          {                          {
524                                  input_str[str_len] = (char)(ch - 256);                                  input_str[str_len] = (char)(ch - 256);
525                                  str_len++;                                  str_len++;
526                                  c = (c & 0b01111111) << 1;                                  c = (c & 0x7f) << 1;
527    
528                                  if ((c & 0b10000000) == 0) // Input completed                                  if ((c & 0x80) == 0) // Input completed
529                                  {                                  {
530                                          break;                                          break;
531                                  }                                  }
# Line 496  int get_data(int row, int col, char *pro Line 541  int get_data(int row, int col, char *pro
541                                          break;                                          break;
542                                  }                                  }
543                          }                          }
544                            input_str[str_len] = '\0';
545    
546                          if (str_len == 0) // Incomplete input                          if (str_len == 0) // Incomplete input
547                          {                          {
548                                  continue;                                  continue;
549                          }                          }
550    
551                          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)
552                            {
553                                    log_error("mbstowcs() error\n");
554                            }
555                            wc_len = (UTF8_fixed_width ? 2 : wcwidth(wcs[0]));
556    
557                            if (len + str_len > buf_size - 1 ||
558                                    display_len + wc_len > max_display_len) // No enough space for Chinese character
559                          {                          {
560                                  outc('\a');                                  outc('\a');
561                                  iflush();                                  iflush();
# Line 513  int get_data(int row, int col, char *pro Line 566  int get_data(int row, int col, char *pro
566                          memcpy(buffer + offset, input_str, (size_t)str_len);                          memcpy(buffer + offset, input_str, (size_t)str_len);
567                          len += str_len;                          len += str_len;
568                          buffer[len] = '\0';                          buffer[len] = '\0';
569                          display_len += 2;                          display_len += wc_len;
570    
571                          moveto(row, col_cur);                          moveto(row, col_cur);
572                          prints("%s", buffer + offset);                          prints("%s", buffer + offset);
573                          prints("%*s", max_display_len - display_len, "");                          prints("%*s", max_display_len - display_len, "");
574    
575                          col_cur += 2;                          col_cur += wc_len;
576    
577                          moveto(row, col_cur);                          moveto(row, col_cur);
578                          iflush();                          iflush();
# Line 590  int display_data(const void *p_data, lon Line 643  int display_data(const void *p_data, lon
643          loop = 1;          loop = 1;
644          while (!SYS_server_exit && loop)          while (!SYS_server_exit && loop)
645          {          {
646                  if (eof_exit > 0 && line_current >= display_line_total && display_line_total <= screen_row_total)                  if (eof_exit > 0 && line_current >= display_line_total)
647                  {                  {
648                          if (eof_exit == 1)                          if (eof_exit == 1)
649                          {                          {
# Line 645  int display_data(const void *p_data, lon Line 698  int display_data(const void *p_data, lon
698                          input_ok = 0;                          input_ok = 0;
699                          while (!SYS_server_exit && !input_ok)                          while (!SYS_server_exit && !input_ok)
700                          {                          {
701                                  ch = igetch_t(MAX_DELAY_TIME);                                  ch = igetch_t(BBS_max_user_idle_time);
702                                  input_ok = 1;                                  input_ok = 1;
703    
704                                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)                                  if (ch != KEY_NULL && ch != KEY_TIMEOUT)
# Line 699  int display_data(const void *p_data, lon Line 752  int display_data(const void *p_data, lon
752                                          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
753                                          break;                                          break;
754                                  case CR:                                  case CR:
                                 case KEY_SPACE:  
755                                  case KEY_DOWN:                                  case KEY_DOWN:
756                                          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
757                                          {                                          {
# Line 727  int display_data(const void *p_data, lon Line 779  int display_data(const void *p_data, lon
779                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
780                                          clrline(output_current_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
781                                          break;                                          break;
782                                    case KEY_SPACE:
783                                  case KEY_PGDN:                                  case KEY_PGDN:
784                                          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
785                                          {                                          {
# Line 800  cleanup: Line 853  cleanup:
853          return ch;          return ch;
854  }  }
855    
856  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)
857  {  {
858          switch (*p_key)          switch (*p_key)
859          {          {
# Line 883  int show_top(const char *str_left, const Line 936  int show_top(const char *str_left, const
936  int show_bottom(const char *msg)  int show_bottom(const char *msg)
937  {  {
938          char str_time[LINE_BUFFER_LEN];          char str_time[LINE_BUFFER_LEN];
939            int len_str_time;
940          time_t time_online;          time_t time_online;
941          struct tm *tm_online;          struct tm *tm_online;
942          char msg_f[LINE_BUFFER_LEN];          char msg_f[LINE_BUFFER_LEN];
943          int eol;          int eol;
944          int msg_len;          int len_msg;
945          int len;          int len;
946          int len_username;          int len_username;
947          char str_tm_online[LINE_BUFFER_LEN];          char str_tm_online[LINE_BUFFER_LEN];
948            int len_str_tm_online;
949    
950          get_time_str(str_time, sizeof(str_time));          len_str_time = (int)get_time_str(str_time, sizeof(str_time));
951    
952          msg_f[0] = '\0';          msg_f[0] = '\0';
953          msg_len = 0;          len_msg = 0;
954          if (msg != NULL)          if (msg != NULL)
955          {          {
956                  strncpy(msg_f, msg, sizeof(msg_f) - 1);                  strncpy(msg_f, msg, sizeof(msg_f) - 1);
957                  msg_f[sizeof(msg_f) - 1] = '\0';                  msg_f[sizeof(msg_f) - 1] = '\0';
958                  len = split_line(msg_f, 23, &eol, &msg_len, 1);                  len = split_line(msg_f, 23, &eol, &len_msg, 1);
959                  msg_f[len] = '\0';                  msg_f[len] = '\0';
960          }          }
961    
# Line 911  int show_bottom(const char *msg) Line 966  int show_bottom(const char *msg)
966          if (tm_online->tm_mday > 1)          if (tm_online->tm_mday > 1)
967          {          {
968                  snprintf(str_tm_online, sizeof(str_tm_online),                  snprintf(str_tm_online, sizeof(str_tm_online),
969                                   "\033[36m%2d\033[33m天\033[36m%2d\033[33m时",                                   "\033[36m%d\033[33md \033[36m%d\033[33m:\033[36m%.2d",
970                                   tm_online->tm_mday - 1, tm_online->tm_hour);                                   tm_online->tm_mday - 1, tm_online->tm_hour, tm_online->tm_min);
971          }          }
972          else          else
973          {          {
974                  snprintf(str_tm_online, sizeof(str_tm_online),                  snprintf(str_tm_online, sizeof(str_tm_online),
975                                   "\033[36m%2d\033[33m时\033[36m%2d\033[33m分",                                   "\033[36m%d\033[33m:\033[36m%.2d",
976                                   tm_online->tm_hour, tm_online->tm_min);                                   tm_online->tm_hour, tm_online->tm_min);
977          }          }
978            len_str_tm_online = str_length(str_tm_online, 1);
979    
980          moveto(SCREEN_ROWS, 0);          moveto(SCREEN_ROWS, 0);
981          clrtoeol();          clrtoeol();
982          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",
983                     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,
984                       "", BBS_username, str_tm_online);
985    
986          return 0;          return 0;
987  }  }


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

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