/[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.93 by sysadm, Wed Jun 11 11:55:50 2025 UTC Revision 1.113 by sysadm, Fri Oct 17 11:23:30 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
 #include "screen.h"  
17  #include "bbs.h"  #include "bbs.h"
18  #include "common.h"  #include "common.h"
 #include "str_process.h"  
 #include "log.h"  
 #include "io.h"  
19  #include "editor.h"  #include "editor.h"
20  #include "file_loader.h"  #include "file_loader.h"
21  #include <fcntl.h>  #include "io.h"
22    #include "log.h"
23    #include "login.h"
24    #include "screen.h"
25    #include "str_process.h"
26  #include <ctype.h>  #include <ctype.h>
 #include <unistd.h>  
 #include <stdlib.h>  
27  #include <errno.h>  #include <errno.h>
28  #include <sys/types.h>  #include <fcntl.h>
29  #include <sys/stat.h>  #include <string.h>
30    #include <stdlib.h>
31    #include <unistd.h>
32  #include <sys/param.h>  #include <sys/param.h>
33    #include <sys/stat.h>
34  #include <sys/shm.h>  #include <sys/shm.h>
35    #include <sys/types.h>
 #define _POSIX_C_SOURCE 200809L  
 #include <string.h>  
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 82  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");
108    }
109    
110    int press_any_key_ex(const char *msg)
111    {
112            int ch = 0;
113            int wait_seconds = 60;
114            int duration = 0;
115            time_t t_begin = time(NULL);
116    
117          moveto(SCREEN_ROWS, 0);          moveto(SCREEN_ROWS, 0);
118          clrtoeol();          clrtoeol();
119    
120          prints("                           \033[1;33m...\033[0;37m");          prints(msg);
121          iflush();          iflush();
122    
123          return igetch_t(MIN(MAX_DELAY_TIME, 60));          do
124            {
125                    ch = igetch_t(wait_seconds - duration);
126                    duration = (int)(time(NULL) - t_begin);
127            } while (!SYS_server_exit && ch == 0 && duration < 60);
128    
129            return ch;
130  }  }
131    
132  void set_input_echo(int echo)  void set_input_echo(int echo)
# Line 98  void set_input_echo(int echo) Line 134  void set_input_echo(int echo)
134          if (echo)          if (echo)
135          {          {
136                  outc('\x83'); // ASCII code 131                  outc('\x83'); // ASCII code 131
                 iflush();  
137          }          }
138          else          else
139          {          {
140                  //    outc ('\x85'); // ASCII code 133                  // outc ('\x85'); // ASCII code 133
141                  prints("\xff\xfb\x01\xff\xfb\x03");                  prints("\xff\xfb\x01\xff\xfb\x03");
                 iflush();  
                 igetch(0);  
                 igetch_reset();  
142          }          }
143            iflush();
144  }  }
145    
146  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)
147  {  {
148          int c;          int ch;
149          int offset = 0;          int offset = 0;
150          int hz = 0;          int eol;
151            int display_len;
152            char input_str[4];
153            int str_len = 0;
154            char c;
155    
156          buffer[buf_size - 1] = '\0';          buffer[buf_size - 1] = '\0';
157          for (offset = 0; offset < buf_size - 1 && buffer[offset] != '\0'; offset++)          offset = split_line(buffer, max_display_len, &eol, &display_len, 0);
                 ;  
158    
159          igetch_reset();          igetch_reset();
160    
161          while (!SYS_server_exit)          while (!SYS_server_exit)
162          {          {
163                  c = igetch_t(MIN(MAX_DELAY_TIME, 60));                  ch = igetch_t(MIN(MAX_DELAY_TIME, 60));
164    
165                  if (c == CR)                  if (ch == CR)
166                  {                  {
                         igetch_reset();  
167                          break;                          break;
168                  }                  }
169                  else if (c == KEY_TIMEOUT || c == KEY_NULL) // timeout or broken pipe                  else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe
170                  {                  {
171                          return -1;                          return -1;
172                  }                  }
173                  else if (c == LF || c == '\0')                  else if (ch == LF || ch == '\0')
174                  {                  {
175                          continue;                          continue;
176                  }                  }
177                  else if (c == BACKSPACE)                  else if (ch == BACKSPACE)
178                  {                  {
179                          if (offset > 0)                          if (offset > 0)
180                          {                          {
181                                  offset--;                                  offset--;
182                                  if (buffer[offset] < 0 || buffer[offset] > 127)                                  if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
183                                  {                                  {
184                                          prints("\033[D \033[D");                                          while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000)
                                         offset--;  
                                         if (offset < 0) // should not happen  
185                                          {                                          {
186                                                  log_error("Offset of buffer is negative\n");                                                  offset--;
                                                 offset = 0;  
187                                          }                                          }
188                                            display_len--;
189                                            prints("\033[D \033[D");
190                                  }                                  }
191                                  buffer[offset] = '\0';                                  buffer[offset] = '\0';
192                                    display_len--;
193                                  prints("\033[D \033[D");                                  prints("\033[D \033[D");
194                                  iflush();                                  iflush();
195                          }                          }
196                          continue;                          continue;
197                  }                  }
198                  else if (c > 255 || iscntrl(c))                  else if (ch > 255 || iscntrl(ch))
199                  {                  {
200                          continue;                          continue;
201                  }                  }
202                  else if (c > 127 && c <= 255)                  else if ((ch & 0xff80) == 0x80) // head of multi-byte character
203                  {                  {
204                          if (!hz && offset + 2 > buf_size - 1) // No enough space for Chinese character                          str_len = 0;
205                            c = (char)(ch & 0b11110000);
206                            while (c & 0b10000000)
207                            {
208                                    input_str[str_len] = (char)(ch - 256);
209                                    str_len++;
210                                    c = (c & 0b01111111) << 1;
211    
212                                    if ((c & 0b10000000) == 0) // Input completed
213                                    {
214                                            break;
215                                    }
216    
217                                    // Expect additional bytes of input
218                                    ch = igetch(100);                                                // 0.1 second
219                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
220                                    {
221    #ifdef _DEBUG
222                                            log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
223    #endif
224                                            str_len = 0;
225                                            break;
226                                    }
227                            }
228    
229                            if (str_len == 0) // Incomplete input
230                            {
231                                    continue;
232                            }
233    
234                            if (offset + str_len > buf_size - 1 || display_len + 2 > max_display_len) // No enough space for Chinese character
235                          {                          {
                                 igetch(0); // Ignore 1 character  
236                                  outc('\a');                                  outc('\a');
237                                  iflush();                                  iflush();
238                                  continue;                                  continue;
239                          }                          }
                         hz = (!hz);  
                 }  
240    
241                  if (offset + 1 > buf_size - 1)                          memcpy(buffer + offset, input_str, (size_t)str_len);
242                  {                          offset += str_len;
243                          outc('\a');                          buffer[offset] = '\0';
244                          iflush();                          display_len += 2;
245                          continue;  
246                            switch (echo_mode)
247                            {
248                            case DOECHO:
249                                    prints(input_str);
250                                    break;
251                            case NOECHO:
252                                    prints("**");
253                                    break;
254                            }
255                  }                  }
256                    else if (ch >= 32 && ch < 127) // Printable character
257                    {
258                            if (offset + 1 > buf_size - 1 || display_len + 1 > max_display_len)
259                            {
260                                    outc('\a');
261                                    iflush();
262                                    continue;
263                            }
264    
265                  buffer[offset++] = (char)c;                          buffer[offset++] = (char)ch;
266                  buffer[offset] = '\0';                          buffer[offset] = '\0';
267                            display_len++;
268    
269                  switch (echo_mode)                          switch (echo_mode)
270                  {                          {
271                  case DOECHO:                          case DOECHO:
272                          outc((char)c);                                  outc((char)ch);
273                          break;                                  break;
274                  case NOECHO:                          case NOECHO:
275                          outc('*');                                  outc('*');
276                          break;                                  break;
277                            }
278                  }                  }
279                  if (!hz)                  else // Invalid character
280                  {                  {
281                          iflush();                          continue;
282                  }                  }
283    
284                    iflush();
285          }          }
286    
287          return offset;          return offset;
# Line 210  int str_input(char *buffer, int buf_size Line 293  int str_input(char *buffer, int buf_size
293    
294          buffer[0] = '\0';          buffer[0] = '\0';
295    
296          len = _str_input(buffer, buf_size, echo_mode);          len = _str_input(buffer, buf_size, buf_size, echo_mode);
297    
298          prints("\r\n");          prints("\r\n");
299          iflush();          iflush();
# Line 218  int str_input(char *buffer, int buf_size Line 301  int str_input(char *buffer, int buf_size
301          return len;          return len;
302  };  };
303    
304  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)
305  {  {
306          int len;          int len = 0;
307            int col_cur = 0;
308            int ch;
309            int offset = 0;
310            int eol;
311            int display_len;
312            char input_str[4];
313            int str_len = 0;
314            char c;
315    
316            buffer[buf_size - 1] = '\0';
317            offset = split_line(buffer, max_display_len, &eol, &display_len, 0);
318            buffer[offset] = '\0';
319            len = offset;
320            col_cur = col + str_length(prompt, 1) + display_len;
321    
322          moveto(row, col);          moveto(row, col);
323          prints("%s", prompt);          prints("%s", prompt);
324          prints("%s", buffer);          prints("%s", buffer);
325            prints("%*s", max_display_len - display_len, "");
326            moveto(row, col_cur);
327          iflush();          iflush();
328    
329          len = _str_input(buffer, buf_size, echo_mode);          igetch_reset();
330    
331            while (!SYS_server_exit)
332            {
333                    ch = igetch_t(MIN(MAX_DELAY_TIME, 60));
334    
335                    if (ch == CR)
336                    {
337                            break;
338                    }
339                    else if (ch == KEY_TIMEOUT || ch == KEY_NULL) // timeout or broken pipe
340                    {
341                            return -1;
342                    }
343                    else if (ch == LF || ch == '\0')
344                    {
345                            continue;
346                    }
347                    else if (ch == BACKSPACE)
348                    {
349                            if (offset > 0)
350                            {
351                                    str_len = 1;
352                                    offset--;
353                                    if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
354                                    {
355                                            while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000)
356                                            {
357                                                    str_len++;
358                                                    offset--;
359                                            }
360                                            display_len--;
361                                            col_cur--;
362                                    }
363    
364                                    memmove(buffer + offset, buffer + offset + str_len, (size_t)(len - offset - str_len));
365                                    len -= str_len;
366                                    buffer[len] = '\0';
367                                    display_len--;
368                                    col_cur--;
369    
370                                    moveto(row, col_cur);
371                                    prints("%s", buffer + offset);
372                                    prints("%*s", max_display_len - display_len, "");
373                                    moveto(row, col_cur);
374                                    iflush();
375                            }
376                            continue;
377                    }
378                    else if (ch == KEY_DEL)
379                    {
380                            if (offset < len)
381                            {
382                                    if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character
383                                    {
384                                            str_len = 0;
385                                            c = (char)(buffer[offset] & 0b11110000);
386                                            while (c & 0b10000000)
387                                            {
388                                                    str_len++;
389                                                    c = (c & 0b01111111) << 1;
390                                            }
391                                            display_len--;
392                                    }
393                                    else
394                                    {
395                                            str_len = 1;
396                                    }
397    
398                                    memmove(buffer + offset, buffer + offset + str_len, (size_t)(len - offset - str_len));
399                                    len -= str_len;
400                                    buffer[len] = '\0';
401                                    display_len--;
402    
403                                    moveto(row, col_cur);
404                                    prints("%s", buffer + offset);
405                                    prints("%*s", max_display_len - display_len, "");
406                                    moveto(row, col_cur);
407                                    iflush();
408                            }
409                            continue;
410                    }
411                    else if (ch == KEY_LEFT)
412                    {
413                            if (offset > 0)
414                            {
415                                    str_len = 1;
416                                    offset--;
417                                    if (buffer[offset] < 0 || buffer[offset] > 127) // UTF8
418                                    {
419                                            while (offset > 0 && (buffer[offset] & 0b11000000) != 0b11000000)
420                                            {
421                                                    str_len++;
422                                                    offset--;
423                                            }
424                                            col_cur--;
425                                    }
426                                    col_cur--;
427    
428                                    moveto(row, col_cur);
429                                    iflush();
430                            }
431                            continue;
432                    }
433                    else if (ch == KEY_RIGHT)
434                    {
435                            if (offset < len)
436                            {
437                                    str_len = 0;
438                                    if ((buffer[offset] & 0x80) == 0x80) // head of multi-byte character
439                                    {
440                                            c = (char)(buffer[offset] & 0b11110000);
441                                            while (c & 0b10000000)
442                                            {
443                                                    str_len++;
444                                                    c = (c & 0b01111111) << 1;
445                                            }
446                                            col_cur++;
447                                    }
448                                    else
449                                    {
450                                            str_len = 1;
451                                    }
452    
453                                    col_cur++;
454                                    offset += str_len;
455    
456                                    moveto(row, col_cur);
457                                    iflush();
458                            }
459                            continue;
460                    }
461                    else if (ch == KEY_HOME || ch == KEY_UP)
462                    {
463                            if (offset > 0)
464                            {
465                                    offset = 0;
466                                    col_cur = col + str_length(prompt, 1);
467    
468                                    moveto(row, col_cur);
469                                    iflush();
470                            }
471                            continue;
472                    }
473                    else if (ch == KEY_END || ch == KEY_DOWN)
474                    {
475                            if (offset < len)
476                            {
477                                    offset = len;
478                                    col_cur = col + str_length(prompt, 1) + display_len;
479    
480                                    moveto(row, col_cur);
481                                    iflush();
482                            }
483                            continue;
484                    }
485                    else if (ch > 255 || iscntrl(ch))
486                    {
487                            continue;
488                    }
489                    else if ((ch & 0xff80) == 0x80) // head of multi-byte character
490                    {
491                            str_len = 0;
492                            c = (char)(ch & 0b11110000);
493                            while (c & 0b10000000)
494                            {
495                                    input_str[str_len] = (char)(ch - 256);
496                                    str_len++;
497                                    c = (c & 0b01111111) << 1;
498    
499                                    if ((c & 0b10000000) == 0) // Input completed
500                                    {
501                                            break;
502                                    }
503    
504                                    // Expect additional bytes of input
505                                    ch = igetch(100);                                                // 0.1 second
506                                    if (ch == KEY_NULL || ch == KEY_TIMEOUT) // Ignore received bytes if no futher input
507                                    {
508    #ifdef _DEBUG
509                                            log_error("Ignore %d bytes of incomplete UTF8 character\n", str_len);
510    #endif
511                                            str_len = 0;
512                                            break;
513                                    }
514                            }
515    
516                            if (str_len == 0) // Incomplete input
517                            {
518                                    continue;
519                            }
520    
521                            if (len + str_len > buf_size - 1 || display_len + 2 > max_display_len) // No enough space for Chinese character
522                            {
523                                    outc('\a');
524                                    iflush();
525                                    continue;
526                            }
527    
528                            memmove(buffer + offset + str_len, buffer + offset, (size_t)(len - offset));
529                            memcpy(buffer + offset, input_str, (size_t)str_len);
530                            len += str_len;
531                            buffer[len] = '\0';
532                            display_len += 2;
533    
534                            moveto(row, col_cur);
535                            prints("%s", buffer + offset);
536                            prints("%*s", max_display_len - display_len, "");
537    
538                            col_cur += 2;
539    
540                            moveto(row, col_cur);
541                            iflush();
542    
543                            offset += str_len;
544                    }
545                    else if (ch >= 32 && ch < 127) // Printable character
546                    {
547                            if (len + 1 > buf_size - 1 || display_len + 1 > max_display_len)
548                            {
549                                    outc('\a');
550                                    iflush();
551                                    continue;
552                            }
553    
554                            memmove(buffer + offset + 1, buffer + offset, (size_t)(len - offset));
555                            buffer[offset] = (char)ch;
556                            len++;
557                            buffer[len] = '\0';
558                            display_len++;
559    
560                            moveto(row, col_cur);
561                            prints("%s", buffer + offset);
562                            prints("%*s", max_display_len - display_len, "");
563    
564                            col_cur++;
565    
566                            moveto(row, col_cur);
567                            iflush();
568    
569                            offset++;
570                    }
571                    else // Invalid character
572                    {
573                            continue;
574                    }
575            }
576    
577          return len;          return len;
578  }  }
# Line 240  int display_data(const void *p_data, lon Line 585  int display_data(const void *p_data, lon
585          DISPLAY_CTX ctx;          DISPLAY_CTX ctx;
586          int ch = 0;          int ch = 0;
587          int input_ok;          int input_ok;
         int screen_current_row;  
588          const int screen_begin_row = 1;          const int screen_begin_row = 1;
589          int screen_end_row = SCREEN_ROWS - 1;          const int screen_row_total = SCREEN_ROWS - screen_begin_row;
590          const int screen_row_total = screen_end_row - screen_begin_row + 1;          int output_current_row = screen_begin_row;
591            int output_end_row = SCREEN_ROWS - 1;
592          long int line_current = 0;          long int line_current = 0;
593          long int len;          long int len;
594          long int percentile;          long int percentile;
595          int loop;          int loop;
596          int eol, display_len;          int eol, display_len;
597    
598          clrline(screen_begin_row, SCREEN_ROWS);          clrline(output_current_row, SCREEN_ROWS);
         screen_current_row = screen_begin_row;  
599    
600          // update msg_ext with extended key handler          // update msg_ext with extended key handler
601          if (key_handler(&ch, &ctx) != 0)          if (key_handler(&ch, &ctx) != 0)
# Line 277  int display_data(const void *p_data, lon Line 621  int display_data(const void *p_data, lon
621                          break;                          break;
622                  }                  }
623    
624                  if (line_current >= display_line_total || screen_current_row > screen_end_row)                  if (line_current >= display_line_total || output_current_row > output_end_row)
625                  {                  {
626                          ctx.reach_begin = (line_current < screen_current_row ? 1 : 0);                          ctx.reach_begin = (line_current < output_current_row ? 1 : 0);
627    
628                          if (line_current - (screen_current_row - screen_begin_row) + screen_row_total < display_line_total)                          if (line_current - (output_current_row - screen_begin_row) + screen_row_total < display_line_total)
629                          {                          {
630                                  percentile = (line_current - (screen_current_row - screen_begin_row) + screen_row_total) * 100 / display_line_total;                                  percentile = (line_current - (output_current_row - screen_begin_row) + screen_row_total) * 100 / display_line_total;
631                                  ctx.reach_end = 0;                                  ctx.reach_end = 0;
632                          }                          }
633                          else                          else
# Line 292  int display_data(const void *p_data, lon Line 636  int display_data(const void *p_data, lon
636                                  ctx.reach_end = 1;                                  ctx.reach_end = 1;
637                          }                          }
638    
639                          ctx.line_top = line_current - (screen_current_row - screen_begin_row) + 1;                          ctx.line_top = line_current - (output_current_row - screen_begin_row) + 1;
640                          ctx.line_bottom = MIN(line_current - (screen_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);
641    
642                          snprintf(buffer, sizeof(buffer),                          snprintf(buffer, sizeof(buffer),
643                                           "\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",
644                                           ctx.line_top,                                           ctx.line_top,
645                                           ctx.line_bottom,                                           ctx.line_bottom,
646                                           percentile,                                           percentile,
647                                           ctx.msg);                                           ctx.msg);
648    
649                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len);                          len = split_line(buffer, SCREEN_COLS, &eol, &display_len, 1);
650                          for (; display_len < SCREEN_COLS; display_len++)                          for (; display_len < SCREEN_COLS; display_len++)
651                          {                          {
652                                  buffer[len++] = ' ';                                  buffer[len++] = ' ';
# Line 315  int display_data(const void *p_data, lon Line 659  int display_data(const void *p_data, lon
659                          iflush();                          iflush();
660    
661                          input_ok = 0;                          input_ok = 0;
                         ch = igetch_t(MAX_DELAY_TIME);  
662                          while (!SYS_server_exit && !input_ok)                          while (!SYS_server_exit && !input_ok)
663                          {                          {
664                                    ch = igetch_t(MAX_DELAY_TIME);
665                                    input_ok = 1;
666    
667                                    if (ch != KEY_NULL && ch != KEY_TIMEOUT)
668                                    {
669                                            BBS_last_access_tm = time(NULL);
670                                    }
671    
672                                  // extended key handler                                  // extended key handler
673                                  if (key_handler(&ch, &ctx) != 0)                                  if (key_handler(&ch, &ctx) != 0)
674                                  {                                  {
675                                          goto cleanup;                                          goto cleanup;
676                                  }                                  }
677    
                                 input_ok = 1;  
678                                  switch (ch)                                  switch (ch)
679                                  {                                  {
680                                  case KEY_NULL:                                  case KEY_NULL:
681                                            log_error("KEY_NULL\n");
682                                            goto cleanup;
683                                  case KEY_TIMEOUT:                                  case KEY_TIMEOUT:
684                                            log_error("User input timeout\n");
685                                          goto cleanup;                                          goto cleanup;
686                                  case KEY_HOME:                                  case KEY_HOME:
687                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
688                                          {                                          {
689                                                  break;                                                  break;
690                                          }                                          }
691                                          line_current = 0;                                          line_current = 0;
692                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
693                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
694                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
695                                          break;                                          break;
696                                  case KEY_END:                                  case KEY_END:
697                                          if (display_line_total < screen_row_total)                                          if (display_line_total < screen_row_total)
# Line 346  int display_data(const void *p_data, lon Line 699  int display_data(const void *p_data, lon
699                                                  break;                                                  break;
700                                          }                                          }
701                                          line_current = display_line_total - screen_row_total;                                          line_current = display_line_total - screen_row_total;
702                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
703                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
704                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
705                                          break;                                          break;
706                                  case KEY_UP:                                  case KEY_UP:
707                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
708                                          {                                          {
709                                                  break;                                                  break;
710                                          }                                          }
711                                          line_current -= screen_current_row;                                          line_current -= output_current_row;
712                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
713                                          // screen_end_line = screen_begin_line;                                          // screen_end_line = screen_begin_line;
714                                          // prints("\033[T"); // Scroll down 1 line                                          // prints("\033[T"); // Scroll down 1 line
715                                          screen_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
716                                          break;                                          break;
717                                  case CR:                                  case CR:
                                         igetch_reset();  
718                                  case KEY_SPACE:                                  case KEY_SPACE:
719                                  case KEY_DOWN:                                  case KEY_DOWN:
720                                          if (line_current + (screen_row_total - (screen_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
721                                          {                                          {
722                                                  break;                                                  break;
723                                          }                                          }
724                                          line_current += (screen_row_total - (screen_current_row - screen_begin_row));                                          line_current += (screen_row_total - (output_current_row - screen_begin_row));
725                                          screen_current_row = screen_row_total;                                          output_current_row = screen_row_total;
726                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
727                                          moveto(SCREEN_ROWS, 0);                                          moveto(SCREEN_ROWS, 0);
728                                          clrtoeol();                                          clrtoeol();
729                                          prints("\033[S"); // Scroll up 1 line                                          // prints("\033[S"); // Scroll up 1 line
730                                            prints("\n"); // Legacy Cterm only works with this line
731                                          break;                                          break;
732                                  case KEY_PGUP:                                  case KEY_PGUP:
733                                          if (line_current - screen_current_row < 0) // Reach begin                                          if (line_current - output_current_row < 0) // Reach begin
734                                          {                                          {
735                                                  break;                                                  break;
736                                          }                                          }
737                                          line_current -= ((screen_row_total - 1) + (screen_current_row - screen_begin_row));                                          line_current -= ((screen_row_total - 1) + (output_current_row - screen_begin_row));
738                                          if (line_current < 0)                                          if (line_current < 0)
739                                          {                                          {
740                                                  line_current = 0;                                                  line_current = 0;
741                                          }                                          }
742                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
743                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
744                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
745                                          break;                                          break;
746                                  case KEY_PGDN:                                  case KEY_PGDN:
747                                          if (line_current + screen_row_total - (screen_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
748                                          {                                          {
749                                                  break;                                                  break;
750                                          }                                          }
751                                          line_current += (screen_row_total - 1) - (screen_current_row - screen_begin_row);                                          line_current += (screen_row_total - 1) - (output_current_row - screen_begin_row);
752                                          if (line_current + screen_row_total > display_line_total) // No enough lines to display                                          if (line_current + screen_row_total > display_line_total) // No enough lines to display
753                                          {                                          {
754                                                  line_current = display_line_total - screen_row_total;                                                  line_current = display_line_total - screen_row_total;
755                                          }                                          }
756                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
757                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
758                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
759                                          break;                                          break;
760                                  case KEY_ESC:                                  case KEY_ESC:
761                                  case KEY_LEFT:                                  case KEY_LEFT:
# Line 419  int display_data(const void *p_data, lon Line 772  int display_data(const void *p_data, lon
772                                          show_help = 1;                                          show_help = 1;
773                                  case KEY_F5:                                  case KEY_F5:
774                                          // Refresh after display help information                                          // Refresh after display help information
775                                          line_current -= (screen_current_row - screen_begin_row);                                          line_current -= (output_current_row - screen_begin_row);
776                                          screen_current_row = screen_begin_row;                                          output_current_row = screen_begin_row;
777                                          screen_end_row = SCREEN_ROWS - 1;                                          output_end_row = SCREEN_ROWS - 1;
778                                          clrline(screen_begin_row, SCREEN_ROWS);                                          clrline(output_current_row, SCREEN_ROWS);
                                         break;  
                                 case KEY_F2: // For test only  
                                         EDITOR_DATA *p_editor_data;  
                                         size_t data_new_len = strlen(p_data) + LINE_BUFFER_LEN;  
   
                                         char *p_data_new = malloc(data_new_len);  
                                         if (p_data_new == NULL)  
                                         {  
                                                 break;  
                                         }  
                                         p_editor_data = editor_data_load(p_data);  
                                         if (p_editor_data == NULL)  
                                         {  
                                                 free(p_data_new);  
                                                 break;  
                                         }  
   
                                         editor_display(p_editor_data);  
                                         editor_data_save(p_editor_data, p_data_new, data_new_len);  
                                         editor_data_cleanup(p_editor_data);  
                                         p_editor_data = NULL;  
                                         free(p_data_new);  
                                         p_data_new = NULL;  
   
                                         // Refresh after display editor  
                                         line_current -= (screen_current_row - screen_begin_row);  
                                         screen_current_row = screen_begin_row;  
                                         screen_end_row = SCREEN_ROWS - 1;  
                                         clrline(screen_begin_row, SCREEN_ROWS);  
779                                          break;                                          break;
780                                  case 0: // Refresh bottom line                                  case 0: // Refresh bottom line
781                                          break;                                          break;
# Line 459  int display_data(const void *p_data, lon Line 783  int display_data(const void *p_data, lon
783                                          input_ok = 0;                                          input_ok = 0;
784                                          break;                                          break;
785                                  }                                  }
   
                                 BBS_last_access_tm = time(0);  
                                 if (!input_ok)  
                                 {  
                                         ch = igetch_t(MAX_DELAY_TIME);  
                                 }  
786                          }                          }
787    
788                          continue;                          continue;
# Line 487  int display_data(const void *p_data, lon Line 805  int display_data(const void *p_data, lon
805                  memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);                  memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
806                  buffer[len] = '\0';                  buffer[len] = '\0';
807    
808                  moveto(screen_current_row, 0);                  moveto(output_current_row, 0);
809                  clrtoeol();                  clrtoeol();
810                  prints("%s", buffer);                  prints("%s", buffer);
811                  line_current++;                  line_current++;
812                  screen_current_row++;                  output_current_row++;
813          }          }
814    
815  cleanup:  cleanup:
# Line 504  static int display_file_key_handler(int Line 822  static int display_file_key_handler(int
822          {          {
823          case 0: // Set msg          case 0: // Set msg
824                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),                  snprintf(p_ctx->msg, sizeof(p_ctx->msg),
825                                   "| [\033[32m\033[33m,\033[32mESC\033[33m] | "                                   "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "
826                                   "ƶ[\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] | "
827                                   "[\033[32mh\033[33m] |");                                   "帮助[\033[32mh\033[33m] |");
828                  break;                  break;
829          }          }
830    
# Line 528  int display_file(const char *filename, i Line 846  int display_file(const char *filename, i
846                  return KEY_NULL;                  return KEY_NULL;
847          }          }
848    
849            if (user_online_update("VIEW_FILE") < 0)
850            {
851                    log_error("user_online_update(VIEW_FILE) error\n");
852            }
853    
854          ret = display_data(p_data, line_total, p_line_offsets, eof_exit, display_file_key_handler, DATA_READ_HELP);          ret = display_data(p_data, line_total, p_line_offsets, eof_exit, display_file_key_handler, DATA_READ_HELP);
855    
856          if (detach_file_shm(p_shm) < 0)          if (detach_file_shm(p_shm) < 0)
# Line 551  int show_top(const char *str_left, const Line 874  int show_top(const char *str_left, const
874    
875          strncpy(str_left_f, str_left, sizeof(str_left_f) - 1);          strncpy(str_left_f, str_left, sizeof(str_left_f) - 1);
876          str_left_f[sizeof(str_left_f) - 1] = '\0';          str_left_f[sizeof(str_left_f) - 1] = '\0';
877          len = split_line(str_left_f, STR_TOP_LEFT_MAX_LEN, &eol, &str_left_len);          len = split_line(str_left_f, STR_TOP_LEFT_MAX_LEN / 2, &eol, &str_left_len, 1);
878          str_left_f[len] = '\0';          str_left_f[len] = '\0';
879    
880          strncpy(str_middle_f, str_middle, sizeof(str_middle_f) - 1);          strncpy(str_middle_f, str_middle, sizeof(str_middle_f) - 1);
881          str_middle_f[sizeof(str_middle_f) - 1] = '\0';          str_middle_f[sizeof(str_middle_f) - 1] = '\0';
882          len = split_line(str_middle, STR_TOP_MIDDLE_MAX_LEN, &eol, &str_middle_len);          len = split_line(str_middle, STR_TOP_MIDDLE_MAX_LEN / 2, &eol, &str_middle_len, 1);
883          str_middle_f[len] = '\0';          str_middle_f[len] = '\0';
884    
885          strncpy(str_right_f, str_right, sizeof(str_right_f) - 1);          strncpy(str_right_f, str_right, sizeof(str_right_f) - 1);
886          str_right_f[sizeof(str_right_f) - 1] = '\0';          str_right_f[sizeof(str_right_f) - 1] = '\0';
887          len = split_line(str_right, STR_TOP_RIGHT_MAX_LEN, &eol, &str_right_len);          len = split_line(str_right, STR_TOP_RIGHT_MAX_LEN / 2, &eol, &str_right_len, 1);
888          str_right_f[len] = '\0';          str_right_f[len] = '\0';
889    
890          moveto(1, 0);          moveto(1, 0);
891          clrtoeol();          clrtoeol();
892          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",
893                     str_left_f, 44 - str_left_len, str_middle_f, 36, str_right_f);                     str_left_f, 44 - str_left_len - str_middle_len, "",
894                       str_middle_f, 36 - str_right_len, "", str_right_f);
895    
896          return 0;          return 0;
897  }  }
# Line 592  int show_bottom(const char *msg) Line 916  int show_bottom(const char *msg)
916          {          {
917                  strncpy(msg_f, msg, sizeof(msg_f) - 1);                  strncpy(msg_f, msg, sizeof(msg_f) - 1);
918                  msg_f[sizeof(msg_f) - 1] = '\0';                  msg_f[sizeof(msg_f) - 1] = '\0';
919                  len = split_line(msg_f, 23, &eol, &msg_len);                  len = split_line(msg_f, 23, &eol, &msg_len, 1);
920                  msg_f[len] = '\0';                  msg_f[len] = '\0';
921          }          }
922    
923          len_username = (int)strnlen(BBS_username, sizeof(BBS_username));          len_username = (int)strnlen(BBS_username, sizeof(BBS_username));
924    
925          time_online = time(0) - BBS_login_tm;          time_online = time(NULL) - BBS_login_tm;
926          tm_online = gmtime(&time_online);          tm_online = gmtime(&time_online);
927          if (tm_online->tm_mday > 1)          if (tm_online->tm_mday > 1)
928          {          {
929                  snprintf(str_tm_online, sizeof(str_tm_online),                  snprintf(str_tm_online, sizeof(str_tm_online),
930                                   "\033[36m%2d\033[33m\033[36m%2d\033[33mʱ",                                   "\033[36m%2d\033[33m天\033[36m%2d\033[33m时",
931                                   tm_online->tm_mday - 1, tm_online->tm_hour);                                   tm_online->tm_mday - 1, tm_online->tm_hour);
932          }          }
933          else          else
934          {          {
935                  snprintf(str_tm_online, sizeof(str_tm_online),                  snprintf(str_tm_online, sizeof(str_tm_online),
936                                   "\033[36m%2d\033[33mʱ\033[36m%2d\033[33m",                                   "\033[36m%2d\033[33m时\033[36m%2d\033[33m分",
937                                   tm_online->tm_hour, tm_online->tm_min);                                   tm_online->tm_hour, tm_online->tm_min);
938          }          }
939    
940          moveto(SCREEN_ROWS, 0);          moveto(SCREEN_ROWS, 0);
941          clrtoeol();          clrtoeol();
942          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",
943                     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);
944    
945          return 0;          return 0;
# Line 645  int show_active_board() Line 969  int show_active_board()
969                  }                  }
970          }          }
971    
972          if (time(0) - t_last_show >= 10)          if (time(NULL) - t_last_show >= 10)
973          {          {
974                  line_last = line_current;                  line_last = line_current;
975                  t_last_show = time(0);                  t_last_show = time(NULL);
976          }          }
977          else          else
978          {          {


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

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