/[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.104 by sysadm, Wed Jul 2 04:17:33 2025 UTC Revision 1.115 by sysadm, Sat Oct 18 12:06:10 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 80
42    
43    static const char *get_time_str(char *s, size_t len)
44    {
45            static const char *weekday[] = {
46                    "天", "一", "二", "三", "四", "五", "六"};
47            time_t curtime;
48            struct tm local_tm;
49    
50            time(&curtime);
51            localtime_r(&curtime, &local_tm);
52            size_t j = strftime(s, len, "%b %d %H:%M 星期", &local_tm);
53    
54            if (j == 0 || j + strlen(weekday[local_tm.tm_wday]) + 1 > len)
55            {
56                    return NULL;
57            }
58    
59            strncat(s, weekday[local_tm.tm_wday], len - 1 - j);
60    
61            return s;
62    }
63    
64  void moveto(int row, int col)  void moveto(int row, int col)
65  {  {
# Line 81  void clearscr() Line 102  void clearscr()
102          moveto(0, 0);          moveto(0, 0);
103  }  }
104    
105  int press_any_key()  inline int press_any_key()
106    {
107            return press_any_key_ex("                           \033[1;33m按任意键继续...\033[m", 60);
108    }
109    
110    int press_any_key_ex(const char *msg, int sec)
111  {  {
112            int ch = 0;
113            int duration = 0;
114            time_t t_begin = time(NULL);
115    
116          moveto(SCREEN_ROWS, 0);          moveto(SCREEN_ROWS, 0);
117          clrtoeol();          clrtoeol();
118    
119          prints("                           \033[1;33m按任意键继续...\033[0;37m");          prints(msg);
120          iflush();          iflush();
121    
122          return igetch_t(MIN(MAX_DELAY_TIME, 60));          igetch_reset();
123    
124            do
125            {
126                    ch = igetch_t(sec - duration);
127                    duration = (int)(time(NULL) - t_begin);
128            } while (!SYS_server_exit && ch == 0 && duration < 60);
129    
130            return ch;
131  }  }
132    
133  void set_input_echo(int echo)  void set_input_echo(int echo)
# Line 97  void set_input_echo(int echo) Line 135  void set_input_echo(int echo)
135          if (echo)          if (echo)
136          {          {
137                  outc('\x83'); // ASCII code 131                  outc('\x83'); // ASCII code 131
                 iflush();  
138          }          }
139          else          else
140          {          {
141                  //    outc ('\x85'); // ASCII code 133                  // outc ('\x85'); // ASCII code 133
142                  prints("\xff\xfb\x01\xff\xfb\x03");                  prints("\xff\xfb\x01\xff\xfb\x03");
                 iflush();  
                 igetch(0);  
                 igetch_reset();  
143          }          }
144            iflush();
145  }  }
146    
147  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, int echo_mode)
# Line 130  static int _str_input(char *buffer, int Line 165  static int _str_input(char *buffer, int
165    
166                  if (ch == CR)                  if (ch == CR)
167                  {                  {
                         igetch_reset();  
168                          break;                          break;
169                  }                  }
170                  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 148  static int _str_input(char *buffer, int Line 182  static int _str_input(char *buffer, int
182                                  offset--;                                  offset--;
183                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
184                                  {                                  {
185                                          while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000)                                          while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0)
186                                          {                                          {
187                                                  offset--;                                                  offset--;
188                                          }                                          }
# Line 169  static int _str_input(char *buffer, int Line 203  static int _str_input(char *buffer, int
203                  else if ((ch & 0xff80) == 0x80) // head of multi-byte character                  else if ((ch & 0xff80) == 0x80) // head of multi-byte character
204                  {                  {
205                          str_len = 0;                          str_len = 0;
206                          c = (char)(ch & 0b11110000);                          c = (char)(ch & 0xf0);
207                          while (c & 0b10000000)                          while (c & 0x80)
208                          {                          {
209                                  input_str[str_len] = (char)(ch - 256);                                  input_str[str_len] = (char)(ch - 256);
210                                  str_len++;                                  str_len++;
211                                  c = (c & 0b01111111) << 1;                                  c = (c & 0x7f) << 1;
212    
213                                  if ((c & 0b10000000) == 0) // Input completed                                  if ((c & 0x80) == 0) // Input completed
214                                  {                                  {
215                                          break;                                          break;
216                                  }                                  }
# Line 185  static int _str_input(char *buffer, int Line 219  static int _str_input(char *buffer, int
219                                  ch = igetch(100);                                                // 0.1 second                                  ch = igetch(100);                                                // 0.1 second
220                                  if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input                                  if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
221                                  {                                  {
222    #ifdef _DEBUG
223                                          log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);                                          log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
224    #endif
225                                          str_len = 0;                                          str_len = 0;
226                                          break;                                          break;
227                                  }                                  }
# Line 264  int str_input(char *buffer, int buf_size Line 300  int str_input(char *buffer, int buf_size
300          iflush();          iflush();
301    
302          return len;          return len;
303  };  }
304    
305  int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int max_display_len, int echo_mode)  int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int max_display_len)
306  {  {
307          int len;          int len = 0;
308            int col_cur = 0;
309            int ch;
310            int offset = 0;
311            int eol;
312            int display_len;
313            char input_str[4];
314            int str_len = 0;
315            char c;
316    
317            buffer[buf_size - 1] = '\0';
318            offset = split_line(buffer, max_display_len, &eol, &display_len, 0);
319            buffer[offset] = '\0';
320            len = offset;
321            col_cur = col + str_length(prompt, 1) + display_len;
322    
323          moveto(row, col);          moveto(row, col);
324          prints("%s", prompt);          prints("%s", prompt);
325          prints("%s", buffer);          prints("%s", buffer);
326            prints("%*s", max_display_len - display_len, "");
327            moveto(row, col_cur);
328          iflush();          iflush();
329    
330          len = _str_input(buffer, buf_size, max_display_len, echo_mode);          igetch_reset();
331    
332            while (!SYS_server_exit)
333            {
334                    ch = igetch_t(MIN(MAX_DELAY_TIME, 60));
335    
336                    if (ch == CR)
337                    {
338                            break;
339                    }
340                    else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe
341                    {
342                            return -1;
343                    }
344                    else if (ch == LF || ch == '\0')
345                    {
346                            continue;
347                    }
348                    else if (ch == BACKSPACE)
349                    {
350                            if (offset > 0)
351                            {
352                                    str_len = 1;
353                                    offset--;
354                                    if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
355                                    {
356                                            while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0)
357                                            {
358                                                    str_len++;
359                                                    offset--;
360                                            }
361                                            display_len--;
362                                            col_cur--;
363                                    }
364    
365                                    memmove(buffer + offset, buffer + offset + str_len, (size_t)(len - offset - str_len));
366                                    len -= str_len;
367                                    buffer[len] = '\0';
368                                    display_len--;
369                                    col_cur--;
370    
371                                    moveto(row, col_cur);
372                                    prints("%s", buffer + offset);
373                                    prints("%*s", max_display_len - display_len, "");
374                                    moveto(row, col_cur);
375                                    iflush();
376                            }
377                            continue;
378                    }
379                    else if (ch == KEY_DEL)
380                    {
381                            if (offset < len)
382                            {
383                                    if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character
384                                    {
385                                            str_len = 0;
386                                            c = (char)(buffer[offset] & 0xf0);
387                                            while (c & 0x80)
388                                            {
389                                                    str_len++;
390                                                    c = (c & 0x7f) << 1;
391                                            }
392                                            display_len--;
393                                    }
394                                    else
395                                    {
396                                            str_len = 1;
397                                    }
398    
399                                    memmove(buffer + offset, buffer + offset + str_len, (size_t)(len - offset - str_len));
400                                    len -= str_len;
401                                    buffer[len] = '\0';
402                                    display_len--;
403    
404                                    moveto(row, col_cur);
405                                    prints("%s", buffer + offset);
406                                    prints("%*s", max_display_len - display_len, "");
407                                    moveto(row, col_cur);
408                                    iflush();
409                            }
410                            continue;
411                    }
412                    else if (ch == KEY_LEFT)
413                    {
414                            if (offset > 0)
415                            {
416                                    str_len = 1;
417                                    offset--;
418                                    if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
419                                    {
420                                            while (offset > 0 && (buffer[offset] & 0xc0) != 0xc0)
421                                            {
422                                                    str_len++;
423                                                    offset--;
424                                            }
425                                            col_cur--;
426                                    }
427                                    col_cur--;
428    
429                                    moveto(row, col_cur);
430                                    iflush();
431                            }
432                            continue;
433                    }
434                    else if (ch == KEY_RIGHT)
435                    {
436                            if (offset < len)
437                            {
438                                    str_len = 0;
439                                    if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character
440                                    {
441                                            c = (char)(buffer[offset] & 0xf0);
442                                            while (c & 0x80)
443                                            {
444                                                    str_len++;
445                                                    c = (c & 0x7f) << 1;
446                                            }
447                                            col_cur++;
448                                    }
449                                    else
450                                    {
451                                            str_len = 1;
452                                    }
453    
454                                    col_cur++;
455                                    offset += str_len;
456    
457                                    moveto(row, col_cur);
458                                    iflush();
459                            }
460                            continue;
461                    }
462                    else if (ch == KEY_HOME || ch == KEY_UP)
463                    {
464                            if (offset > 0)
465                            {
466                                    offset = 0;
467                                    col_cur = col + str_length(prompt, 1);
468    
469                                    moveto(row, col_cur);
470                                    iflush();
471                            }
472                            continue;
473                    }
474                    else if (ch == KEY_END || ch == KEY_DOWN)
475                    {
476                            if (offset < len)
477                            {
478                                    offset = len;
479                                    col_cur = col + str_length(prompt, 1) + display_len;
480    
481                                    moveto(row, col_cur);
482                                    iflush();
483                            }
484                            continue;
485                    }
486                    else if (ch > 255 || iscntrl(ch))
487                    {
488                            continue;
489                    }
490                    else if ((ch & 0xff80) == 0x80) // head of multi-byte character
491                    {
492                            str_len = 0;
493                            c = (char)(ch & 0xf0);
494                            while (c & 0x80)
495                            {
496                                    input_str[str_len] = (char)(ch - 256);
497                                    str_len++;
498                                    c = (c & 0x7f) << 1;
499    
500                                    if ((c & 0x80) == 0) // Input completed
501                                    {
502                                            break;
503                                    }
504    
505                                    // Expect additional bytes of input
506                                    ch = igetch(100);                                                // 0.1 second
507                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
508                                    {
509    #ifdef _DEBUG
510                                            log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
511    #endif
512                                            str_len = 0;
513                                            break;
514                                    }
515                            }
516    
517                            if (str_len == 0) // Incomplete input
518                            {
519                                    continue;
520                            }
521    
522                            if (len + str_len > buf_size - 1 || display_len + 2 > max_display_len) // No enough space for Chinese character
523                            {
524                                    outc('\a');
525                                    iflush();
526                                    continue;
527                            }
528    
529                            memmove(buffer + offset + str_len, buffer + offset, (size_t)(len - offset));
530                            memcpy(buffer + offset, input_str, (size_t)str_len);
531                            len += str_len;
532                            buffer[len] = '\0';
533                            display_len += 2;
534    
535                            moveto(row, col_cur);
536                            prints("%s", buffer + offset);
537                            prints("%*s", max_display_len - display_len, "");
538    
539                            col_cur += 2;
540    
541                            moveto(row, col_cur);
542                            iflush();
543    
544                            offset += str_len;
545                    }
546                    else if (ch >= 32 && ch < 127) // Printable character
547                    {
548                            if (len + 1 > buf_size - 1 || display_len + 1 > max_display_len)
549                            {
550                                    outc('\a');
551                                    iflush();
552                                    continue;
553                            }
554    
555                            memmove(buffer + offset + 1, buffer + offset, (size_t)(len - offset));
556                            buffer[offset] = (char)ch;
557                            len++;
558                            buffer[len] = '\0';
559                            display_len++;
560    
561                            moveto(row, col_cur);
562                            prints("%s", buffer + offset);
563                            prints("%*s", max_display_len - display_len, "");
564    
565                            col_cur++;
566    
567                            moveto(row, col_cur);
568                            iflush();
569    
570                            offset++;
571                    }
572                    else // Invalid character
573                    {
574                            continue;
575                    }
576            }
577    
578          return len;          return len;
579  }  }
# Line 367  int display_data(const void *p_data, lon Line 665  int display_data(const void *p_data, lon
665                                  ch = igetch_t(MAX_DELAY_TIME);                                  ch = igetch_t(MAX_DELAY_TIME);
666                                  input_ok = 1;                                  input_ok = 1;
667    
668                                    if (ch != KEY_NULL && ch != KEY_TIMEOUT)
669                                    {
670                                            BBS_last_access_tm = time(NULL);
671                                    }
672    
673                                  // extended key handler                                  // extended key handler
674                                  if (key_handler(&ch, &ctx) != 0)                                  if (key_handler(&ch, &ctx) != 0)
675                                  {                                  {
# Line 376  int display_data(const void *p_data, lon Line 679  int display_data(const void *p_data, lon
679                                  switch (ch)                                  switch (ch)
680                                  {                                  {
681                                  case KEY_NULL:                                  case KEY_NULL:
682                                            log_error("KEY_NULL\n");
683                                            goto cleanup;
684                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
685                                            log_error("User input timeout\n");
686                                          goto cleanup;                                          goto cleanup;
687                                  case KEY_HOME:                                  case KEY_HOME:
688                                          if (line_current - output_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
# Line 410  int display_data(const void *p_data, lon Line 716  int display_data(const void *p_data, lon
716                                          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
717                                          break;                                          break;
718                                  case CR:                                  case CR:
                                         igetch_reset();  
719                                  case KEY_SPACE:                                  case KEY_SPACE:
720                                  case KEY_DOWN:                                  case KEY_DOWN:
721                                          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
# Line 479  int display_data(const void *p_data, lon Line 784  int display_data(const void *p_data, lon
784                                          input_ok = 0;                                          input_ok = 0;
785                                          break;                                          break;
786                                  }                                  }
   
                                 BBS_last_access_tm = time(NULL);  
787                          }                          }
788    
789                          continue;                          continue;
# Line 572  int show_top(const char *str_left, const Line 875  int show_top(const char *str_left, const
875    
876          strncpy(str_left_f, str_left, sizeof(str_left_f) - 1);          strncpy(str_left_f, str_left, sizeof(str_left_f) - 1);
877          str_left_f[sizeof(str_left_f) - 1] = '\0';          str_left_f[sizeof(str_left_f) - 1] = '\0';
878          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);
879          str_left_f[len] = '\0';          str_left_f[len] = '\0';
880    
881          strncpy(str_middle_f, str_middle, sizeof(str_middle_f) - 1);          strncpy(str_middle_f, str_middle, sizeof(str_middle_f) - 1);
882          str_middle_f[sizeof(str_middle_f) - 1] = '\0';          str_middle_f[sizeof(str_middle_f) - 1] = '\0';
883          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);
884          str_middle_f[len] = '\0';          str_middle_f[len] = '\0';
885    
886          strncpy(str_right_f, str_right, sizeof(str_right_f) - 1);          strncpy(str_right_f, str_right, sizeof(str_right_f) - 1);
887          str_right_f[sizeof(str_right_f) - 1] = '\0';          str_right_f[sizeof(str_right_f) - 1] = '\0';
888          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);
889          str_right_f[len] = '\0';          str_right_f[len] = '\0';
890    
891          moveto(1, 0);          moveto(1, 0);
892          clrtoeol();          clrtoeol();
893          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",
894                     str_left_f, 44 - str_left_len, str_middle_f, 36, str_right_f);                     str_left_f, 44 - str_left_len - str_middle_len, "",
895                       str_middle_f, 36 - str_right_len, "", str_right_f);
896    
897          return 0;          return 0;
898  }  }


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

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