/[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.102 by sysadm, Sat Jun 21 02:15:18 2025 UTC Revision 1.106 by sysadm, Sun Jul 20 02:04:21 2025 UTC
# Line 36  Line 36 
36    
37  #define ACTIVE_BOARD_HEIGHT 8  #define ACTIVE_BOARD_HEIGHT 8
38    
39  #define STR_TOP_LEFT_MAX_LEN 40  #define STR_TOP_LEFT_MAX_LEN 80
40  #define STR_TOP_MIDDLE_MAX_LEN 20  #define STR_TOP_MIDDLE_MAX_LEN 40
41  #define STR_TOP_RIGHT_MAX_LEN 20  #define STR_TOP_RIGHT_MAX_LEN 40
42    
43  void moveto(int row, int col)  void moveto(int row, int col)
44  {  {
# Line 86  int press_any_key() Line 86  int press_any_key()
86          moveto(SCREEN_ROWS, 0);          moveto(SCREEN_ROWS, 0);
87          clrtoeol();          clrtoeol();
88    
89          prints("                           \033[1;33m按任意键继续...\033[0;37m");          prints("                           \033[1;33m鎸変换鎰忛敭缁х画...\033[0;37m");
90          iflush();          iflush();
91    
92          return igetch_t(MIN(MAX_DELAY_TIME, 60));          return igetch_t(MIN(MAX_DELAY_TIME, 60));
# Line 109  void set_input_echo(int echo) Line 109  void set_input_echo(int echo)
109          }          }
110  }  }
111    
112  static int _str_input(char *buffer, int buf_size, int echo_mode)  static int _str_input(char *buffer, int buf_size, int max_display_len, int echo_mode)
113  {  {
114          int c;          int ch;
115          int offset = 0;          int offset = 0;
116          int hz = 0;          int eol;
117            int display_len;
118            char input_str[4];
119            int str_len = 0;
120            char c;
121    
122          buffer[buf_size - 1] = '\0';          buffer[buf_size - 1] = '\0';
123          for (offset = 0; offset < buf_size - 1 && buffer[offset] != '\0'; offset++)          offset = split_line(buffer, max_display_len, &eol, &display_len, 0);
                 ;  
124    
125          igetch_reset();          igetch_reset();
126    
127          while (!SYS_server_exit)          while (!SYS_server_exit)
128          {          {
129                  c = igetch_t(MIN(MAX_DELAY_TIME, 60));                  ch = igetch_t(MIN(MAX_DELAY_TIME, 60));
130    
131                  if (c == CR)                  if (ch == CR)
132                  {                  {
133                          igetch_reset();                          igetch_reset();
134                          break;                          break;
135                  }                  }
136                  else if (c == KEY_TIMEOUT || c == KEY_NULL) // timeout or broken pipe                  else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe
137                  {                  {
138                          return -1;                          return -1;
139                  }                  }
140                  else if (c == LF || c == '\0')                  else if (ch == LF || ch == '\0')
141                  {                  {
142                          continue;                          continue;
143                  }                  }
144                  else if (c == BACKSPACE)                  else if (ch == BACKSPACE)
145                  {                  {
146                          if (offset > 0)                          if (offset > 0)
147                          {                          {
148                                  offset--;                                  offset--;
149                                  if (buffer[offset] < 0 || buffer[offset] > 127)                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
150                                  {                                  {
151                                          prints("\033[D \033[D");                                          while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000)
                                         offset--;  
                                         if (offset < 0) // should not happen  
152                                          {                                          {
153                                                  log_error("Offset of buffer is negative\n");                                                  offset--;
                                                 offset = 0;  
154                                          }                                          }
155                                            display_len--;
156                                            prints("\033[D \033[D");
157                                  }                                  }
158                                  buffer[offset] = '\0';                                  buffer[offset] = '\0';
159                                    display_len--;
160                                  prints("\033[D \033[D");                                  prints("\033[D \033[D");
161                                  iflush();                                  iflush();
162                          }                          }
163                          continue;                          continue;
164                  }                  }
165                  else if (c > 255 || iscntrl(c))                  else if (ch > 255 || iscntrl(ch))
166                  {                  {
167                          continue;                          continue;
168                  }                  }
169                  else if (c > 127 && c <= 255)                  else if ((ch & 0xff80) == 0x80) // head of multi-byte character
170                  {                  {
171                          if (!hz && offset + 2 > buf_size - 1) // No enough space for Chinese character                          str_len = 0;
172                            c = (char)(ch & 0b11110000);
173                            while (c & 0b10000000)
174                            {
175                                    input_str[str_len] = (char)(ch - 256);
176                                    str_len++;
177                                    c = (c & 0b01111111) << 1;
178    
179                                    if ((c & 0b10000000) == 0) // Input completed
180                                    {
181                                            break;
182                                    }
183    
184                                    // Expect additional bytes of input
185                                    ch = igetch(100);                                                // 0.1 second
186                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
187                                    {
188    #ifdef _DEBUG
189                                            log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
190    #endif
191                                            str_len = 0;
192                                            break;
193                                    }
194                            }
195    
196                            if (str_len == 0) // Incomplete input
197                            {
198                                    continue;
199                            }
200    
201                            if (offset + str_len > buf_size - 1 || display_len + 2 > max_display_len) // No enough space for Chinese character
202                          {                          {
                                 igetch(0); // Ignore 1 character  
203                                  outc('\a');                                  outc('\a');
204                                  iflush();                                  iflush();
205                                  continue;                                  continue;
206                          }                          }
                         hz = (!hz);  
                 }  
207    
208                  if (offset + 1 > buf_size - 1)                          memcpy(buffer + offset, input_str, (size_t)str_len);
209                  {                          offset += str_len;
210                          outc('\a');                          buffer[offset] = '\0';
211                          iflush();                          display_len += 2;
212                          continue;  
213                            switch (echo_mode)
214                            {
215                            case DOECHO:
216                                    prints(input_str);
217                                    break;
218                            case NOECHO:
219                                    prints("**");
220                                    break;
221                            }
222                  }                  }
223                    else if (ch >= 32 && ch < 127) // Printable character
224                    {
225                            if (offset + 1 > buf_size - 1 || display_len + 1 > max_display_len)
226                            {
227                                    outc('\a');
228                                    iflush();
229                                    continue;
230                            }
231    
232                  buffer[offset++] = (char)c;                          buffer[offset++] = (char)ch;
233                  buffer[offset] = '\0';                          buffer[offset] = '\0';
234                            display_len++;
235    
236                  switch (echo_mode)                          switch (echo_mode)
237                  {                          {
238                  case DOECHO:                          case DOECHO:
239                          outc((char)c);                                  outc((char)ch);
240                          break;                                  break;
241                  case NOECHO:                          case NOECHO:
242                          outc('*');                                  outc('*');
243                          break;                                  break;
244                            }
245                  }                  }
246                  if (!hz)                  else // Invalid character
247                  {                  {
248                          iflush();                          continue;
249                  }                  }
250    
251                    iflush();
252          }          }
253    
254          return offset;          return offset;
# Line 209  int str_input(char *buffer, int buf_size Line 260  int str_input(char *buffer, int buf_size
260    
261          buffer[0] = '\0';          buffer[0] = '\0';
262    
263          len = _str_input(buffer, buf_size, echo_mode);          len = _str_input(buffer, buf_size, buf_size, echo_mode);
264    
265          prints("\r\n");          prints("\r\n");
266          iflush();          iflush();
# Line 217  int str_input(char *buffer, int buf_size Line 268  int str_input(char *buffer, int buf_size
268          return len;          return len;
269  };  };
270    
271  int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int echo_mode)  int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int max_display_len, int echo_mode)
272  {  {
273          int len;          int len;
274    
# Line 226  int get_data(int row, int col, char *pro Line 277  int get_data(int row, int col, char *pro
277          prints("%s", buffer);          prints("%s", buffer);
278          iflush();          iflush();
279    
280          len = _str_input(buffer, buf_size, echo_mode);          len = _str_input(buffer, buf_size, max_display_len, echo_mode);
281    
282          return len;          return len;
283  }  }
# Line 294  int display_data(const void *p_data, lon Line 345  int display_data(const void *p_data, lon
345                          ctx.line_bottom = MIN(line_current - (output_current_row - screen_begin_row) + screen_row_total, display_line_total);                          ctx.line_bottom = MIN(line_current - (output_current_row - screen_begin_row) + screen_row_total, display_line_total);
346    
347                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
348                                           "\033[1;44;33m第\033[32m%ld\033[33m-\033[32m%ld\033[33m行 (\033[32m%ld%%\033[33m) %s",                                           "\033[1;44;33m绗琝033[32m%ld\033[33m-\033[32m%ld\033[33m琛 (\033[32m%ld%%\033[33m) %s",
349                                           ctx.line_top,                                           ctx.line_top,
350                                           ctx.line_bottom,                                           ctx.line_bottom,
351                                           percentile,                                           percentile,
# Line 373  int display_data(const void *p_data, lon Line 424  int display_data(const void *p_data, lon
424                                          output_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
425                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
426                                          clrtoeol();                                          clrtoeol();
427                                          prints("\033[S"); // Scroll up 1 line                                          // prints("\033[S"); // Scroll up 1 line
428                                            prints("\n"); // Legacy Cterm only works with this line
429                                          break;                                          break;
430                                  case KEY_PGUP:                                  case KEY_PGUP:
431                                          if (line_current - output_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
# Line 470  static int display_file_key_handler(int Line 522  static int display_file_key_handler(int
522          {          {
523          case 0: // Set msg          case 0: // Set msg
524                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
525                                   "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "                                   "| 杩斿洖[\033[32m鈫怽033[33m,\033[32mESC\033[33m] | "
526                                   "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | "                                   "绉诲姩[\033[32m鈫慭033[33m/\033[32m鈫揬033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | "
527                                   "帮助[\033[32mh\033[33m] |");                                   "甯姪[\033[32mh\033[33m] |");
528                  break;                  break;
529          }          }
530    
# Line 522  int show_top(const char *str_left, const Line 574  int show_top(const char *str_left, const
574    
575          strncpy(str_left_f, str_left, sizeof(str_left_f) - 1);          strncpy(str_left_f, str_left, sizeof(str_left_f) - 1);
576          str_left_f[sizeof(str_left_f) - 1] = '\0';          str_left_f[sizeof(str_left_f) - 1] = '\0';
577          len = split_line(str_left_f, STR_TOP_LEFT_MAX_LEN, &eol, &str_left_len, 1);          len = split_line(str_left_f, STR_TOP_LEFT_MAX_LEN / 2, &eol, &str_left_len, 1);
578          str_left_f[len] = '\0';          str_left_f[len] = '\0';
579    
580          strncpy(str_middle_f, str_middle, sizeof(str_middle_f) - 1);          strncpy(str_middle_f, str_middle, sizeof(str_middle_f) - 1);
581          str_middle_f[sizeof(str_middle_f) - 1] = '\0';          str_middle_f[sizeof(str_middle_f) - 1] = '\0';
582          len = split_line(str_middle, STR_TOP_MIDDLE_MAX_LEN, &eol, &str_middle_len, 1);          len = split_line(str_middle, STR_TOP_MIDDLE_MAX_LEN / 2, &eol, &str_middle_len, 1);
583          str_middle_f[len] = '\0';          str_middle_f[len] = '\0';
584    
585          strncpy(str_right_f, str_right, sizeof(str_right_f) - 1);          strncpy(str_right_f, str_right, sizeof(str_right_f) - 1);
586          str_right_f[sizeof(str_right_f) - 1] = '\0';          str_right_f[sizeof(str_right_f) - 1] = '\0';
587          len = split_line(str_right, STR_TOP_RIGHT_MAX_LEN, &eol, &str_right_len, 1);          len = split_line(str_right, STR_TOP_RIGHT_MAX_LEN / 2, &eol, &str_right_len, 1);
588          str_right_f[len] = '\0';          str_right_f[len] = '\0';
589    
590          moveto(1, 0);          moveto(1, 0);
591          clrtoeol();          clrtoeol();
592          prints("\033[1;44;33m%s\033[37m%*s\033[33m%*s\033[m",          prints("\033[1;44;33m%s\033[37m%*s%s\033[33m%*s%s\033[m",
593                     str_left_f, 44 - str_left_len, str_middle_f, 36, str_right_f);                     str_left_f, 44 - str_left_len - str_middle_len, "",
594                       str_middle_f, 36 - str_right_len, "", str_right_f);
595    
596          return 0;          return 0;
597  }  }
# Line 574  int show_bottom(const char *msg) Line 627  int show_bottom(const char *msg)
627          if (tm_online->tm_mday > 1)          if (tm_online->tm_mday > 1)
628          {          {
629                  snprintf(str_tm_online, sizeof(str_tm_online),                  snprintf(str_tm_online, sizeof(str_tm_online),
630                                   "\033[36m%2d\033[33m天\033[36m%2d\033[33m时",                                   "\033[36m%2d\033[33m澶‐033[36m%2d\033[33m鏃",
631                                   tm_online->tm_mday - 1, tm_online->tm_hour);                                   tm_online->tm_mday - 1, tm_online->tm_hour);
632          }          }
633          else          else
634          {          {
635                  snprintf(str_tm_online, sizeof(str_tm_online),                  snprintf(str_tm_online, sizeof(str_tm_online),
636                                   "\033[36m%2d\033[33m时\033[36m%2d\033[33m分",                                   "\033[36m%2d\033[33m鏃禱033[36m%2d\033[33m鍒",
637                                   tm_online->tm_hour, tm_online->tm_min);                                   tm_online->tm_hour, tm_online->tm_min);
638          }          }
639    
640          moveto(SCREEN_ROWS, 0);          moveto(SCREEN_ROWS, 0);
641          clrtoeol();          clrtoeol();
642          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",
643                     str_time, msg_f, 38 - msg_len - len_username, "", BBS_username, str_tm_online);                     str_time, msg_f, 38 - msg_len - len_username, "", BBS_username, str_tm_online);
644    
645          return 0;          return 0;


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

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