/[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.25 by sysadm, Wed Apr 30 12:54:41 2025 UTC Revision 1.33 by sysadm, Mon May 5 11:11:06 2025 UTC
# Line 17  Line 17 
17    
18  #include "bbs.h"  #include "bbs.h"
19  #include "common.h"  #include "common.h"
20    #include "str_process.h"
21  #include "log.h"  #include "log.h"
22  #include "io.h"  #include "io.h"
23  #include "screen.h"  #include "screen.h"
# Line 25  Line 26 
26  #include <sys/types.h>  #include <sys/types.h>
27  #include <sys/stat.h>  #include <sys/stat.h>
28  #include <unistd.h>  #include <unistd.h>
29    #include <stdlib.h>
30    
31  #define ACTIVE_BOARD_HEIGHT 8  #define ACTIVE_BOARD_HEIGHT 8
32    
33  int screen_lines = 24;  int screen_rows = 24;
34    int screen_cols = 80;
35    
36  void moveto(int row, int col)  void moveto(int row, int col)
37  {  {
# Line 40  void moveto(int row, int col) Line 43  void moveto(int row, int col)
43          {          {
44                  prints("\r");                  prints("\r");
45          }          }
         iflush();  
46  }  }
47    
48  void clrtoeol()  void clrtoeol()
49  {  {
50          prints("\033[K");          prints("\033[K");
         iflush();  
51  }  }
52    
53  void clrline(int line_begin, int line_end)  void clrline(int line_begin, int line_end)
# Line 58  void clrline(int line_begin, int line_en Line 59  void clrline(int line_begin, int line_en
59                  moveto(i, 0);                  moveto(i, 0);
60                  prints("\033[K");                  prints("\033[K");
61          }          }
   
         iflush();  
62  }  }
63    
64  void clrtobot(int line_begin)  void clrtobot(int line_begin)
65  {  {
         //clrline(line_begin, screen_lines);  
66          moveto(line_begin, 0);          moveto(line_begin, 0);
67          prints("\033[J");          prints("\033[J");
   
68          moveto(line_begin, 0);          moveto(line_begin, 0);
   
         iflush();  
69  }  }
70    
71  void clearscr()  void clearscr()
72  {  {
73          prints("\033[2J");          prints("\033[2J");
74          moveto(0, 0);          moveto(0, 0);
         iflush();  
75  }  }
76    
77  int press_any_key()  int press_any_key()
78  {  {
79          moveto(screen_lines, 0);          moveto(screen_rows, 0);
80          clrtoeol();          clrtoeol();
81    
82          prints("                           \033[1;33m按任意键继续...\033[0;37m");          prints("                           \033[1;33m按任意键继续...\033[0;37m");
# Line 103  void set_input_echo(int echo) Line 97  void set_input_echo(int echo)
97                  //    outc ('\x85'); // ASCII code 133                  //    outc ('\x85'); // ASCII code 133
98                  prints("\xff\xfb\x01\xff\xfb\x03");                  prints("\xff\xfb\x01\xff\xfb\x03");
99                  iflush();                  iflush();
100                  igetch_t(60);                  igetch(0);
101                  igetch_t(60);                  igetch(1);
102          }          }
103  }  }
104    
105  static int _str_input(char *buffer, int buffer_length, int echo_mode)  static int _str_input(char *buffer, int buffer_length, int echo_mode)
106  {  {
107          char buf[256], ch;          int c, offset = 0, i, hz = 0;
         int c, offset = 0, len, loop = 1, i, hz = 0;  
108    
109          for (i = 0; i < buffer_length && buffer[i] != '\0'; i++)          for (i = 0; i < buffer_length && buffer[i] != '\0'; i++)
110          {          {
111                  offset++;                  offset++;
112          }          }
113    
114          while (c = igetch_t(60))          while ((c = igetch_t(60)))
115          {          {
116                  if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR)                  if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR)
117                  {                  {
118                            igetch(1); // Cleanup remaining '\n' in the buffer
119                          break;                          break;
120                  }                  }
121                  if (c == LF)                  if (c == LF)
# Line 132  static int _str_input(char *buffer, int Line 126  static int _str_input(char *buffer, int
126                  {                  {
127                          if (offset > 0)                          if (offset > 0)
128                          {                          {
129                                  buffer[--offset] = '\0';                                  offset--;
130                                  prints("\b \b");                                  if (buffer[offset] < 0 || buffer[offset] > 127)
131                                  //            clrtoeol ();                                  {
132                                            prints("\033[D \033[D");
133                                            offset--;
134                                            if (offset < 0) // should not happen
135                                            {
136                                                    log_error("Offset of buffer is negative\n");
137                                                    offset = 0;
138                                            }
139                                    }
140                                    buffer[offset] = '\0';
141                                    prints("\033[D \033[D");
142                                  iflush();                                  iflush();
143                          }                          }
144                          continue;                          continue;
# Line 145  static int _str_input(char *buffer, int Line 149  static int _str_input(char *buffer, int
149                  }                  }
150                  if (c > 127 && c <= 255)                  if (c > 127 && c <= 255)
151                  {                  {
152                            if (!hz && offset + 2 > buffer_length) // No enough space for Chinese character
153                            {
154                                    igetch(1); // Cleanup remaining input
155                                    outc('\a');
156                                    iflush();
157                                    continue;
158                            }
159                          hz = (!hz);                          hz = (!hz);
160                  }                  }
161                  if (offset >= buffer_length)                  if (offset >= buffer_length)
162                  {                  {
163                          outc('\a');                          outc('\a');
164                            iflush();
165                          continue;                          continue;
166                  }                  }
167                  buffer[offset++] = (char)c;                  buffer[offset++] = (char)c;
# Line 169  static int _str_input(char *buffer, int Line 181  static int _str_input(char *buffer, int
181                  }                  }
182          }          }
183    
         prints("\r\n");  
         iflush();  
   
184          return offset;          return offset;
185  }  }
186    
187  int str_input(char *buffer, int buffer_length, int echo_mode)  int str_input(char *buffer, int buffer_length, int echo_mode)
188  {  {
189          int offset;          int len;
190    
191            buffer[0] = '\0';
192    
193          memset(buffer, '\0', buffer_length);          len = _str_input(buffer, buffer_length, echo_mode);
194    
195          offset = _str_input(buffer, buffer_length, echo_mode);          prints("\r\n");
196            iflush();
197    
198          return offset;          return len;
199  };  };
200    
201  int get_data(int row, int col, char *prompt, char *buffer, int buffer_length, int echo_mode)  int get_data(int row, int col, char *prompt, char *buffer, int buffer_length, int echo_mode)
# Line 191  int get_data(int row, int col, char *pro Line 203  int get_data(int row, int col, char *pro
203          int len;          int len;
204    
205          moveto(row, col);          moveto(row, col);
         iflush();  
206          prints(prompt);          prints(prompt);
207          prints(buffer);          prints(buffer);
208          iflush();          iflush();
# Line 203  int get_data(int row, int col, char *pro Line 214  int get_data(int row, int col, char *pro
214    
215  int display_file(const char *filename)  int display_file(const char *filename)
216  {  {
217          char buffer[260];          char buffer[LINE_BUFFER_LEN];
218          FILE *fin;          FILE *fin;
219          int i;          int i;
220    
# Line 212  int display_file(const char *filename) Line 223  int display_file(const char *filename)
223                  return -1;                  return -1;
224          }          }
225    
226          while (fgets(buffer, 255, fin))          while (fgets(buffer, sizeof(buffer) - 1, fin))
227          {          {
228                  i = strlen(buffer);                  i = strnlen(buffer, sizeof(buffer) - 1);
229                  if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')                  if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
230                  {                  {
231                          buffer[i - 1] = '\r';                          buffer[i - 1] = '\r';
# Line 231  int display_file(const char *filename) Line 242  int display_file(const char *filename)
242    
243  int display_file_ex(const char *filename, int begin_line, int wait)  int display_file_ex(const char *filename, int begin_line, int wait)
244  {  {
245          char buffer[260], temp[256];          char buffer[LINE_BUFFER_LEN];
246          int i, ch, input_ok, max_lines;          int ch = 0;
247          long int line, c_line_begin = 0, c_line_total = 0;          int input_ok, line, max_lines;
248          long int f_line, f_size, f_offset;          long int c_line_current = 0, c_line_total = 0;
249          FILE *fin;          FILE *fin;
250          struct stat f_stat;          long *p_line_offsets;
251            int len;
252            int percentile;
253            int loop = 1;
254    
255          max_lines = screen_lines - 1;          if ((fin = fopen(filename, "r")) == NULL)
256          clrline(begin_line, screen_lines);          {
257                    log_error("Unable to open file %s\n", filename);
258                    return -1;
259            }
260    
261            p_line_offsets = (long *)malloc(sizeof(long) * MAX_FILE_LINES);
262    
263            c_line_total = split_file_lines(fin, screen_cols, p_line_offsets, MAX_FILE_LINES);
264    
265            clrline(begin_line, screen_rows);
266          line = begin_line;          line = begin_line;
267          moveto(line, 0);          max_lines = screen_rows - 1;
268    
269          if ((fin = fopen(filename, "r")) != NULL)          while (loop)
270          {          {
271                  if (fstat(fileno(fin), &f_stat) != 0)                  if (c_line_current >= c_line_total && c_line_total <= screen_rows - 2)
272                  {                  {
273                          log_error("Get file stat failed\n");                          if (wait)
274                          return -1;                          {
275                  }                                  ch = press_any_key();
276                  f_size = f_stat.st_size;                          }
277                            else
278                            {
279                                    iflush();
280                            }
281    
282                  while (fgets(buffer, 255, fin))                          loop = 0;
283                          c_line_total++;                          break;
284                  rewind(fin);                  }
285    
286                  while (fgets(buffer, 255, fin))                  if (c_line_current >= c_line_total || line >= max_lines)
287                  {                  {
288                          if (line >= max_lines)                          if (c_line_current - (line - 1) + (screen_rows - 2) < c_line_total)
289                          {                          {
290                                  f_offset = ftell(fin);                                  percentile = (c_line_current - (line - 1) + (screen_rows - 2)) * 100 / c_line_total;
291                            }
292                            else
293                            {
294                                    percentile = 100;
295                            }
296    
297                                  moveto(screen_lines, 0);                          moveto(screen_rows, 0);
298                                  prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",                          prints("\033[1;44;32m%s (%d%%)%s\033[33m  │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
299                                             (f_offset - strlen(buffer)) * 100 / f_size);                                     (percentile < 100 ? "下面还有喔" : "没有更多了"), percentile,
300                                  iflush();                                     (percentile < 10 ? "  " : (percentile < 100 ? " " : "")));
301                            iflush();
302    
303                                  input_ok = 0;                          input_ok = 0;
304                                  while (!input_ok)                          while (!input_ok)
305                            {
306                                    ch = igetch_t(MAX_DELAY_TIME);
307                                    input_ok = 1;
308                                    switch (ch)
309                                  {                                  {
310                                          ch = igetch_t(MAX_DELAY_TIME);                                  case KEY_UP:
311                                          input_ok = 1;                                          if (c_line_current - line < 0) // Reach top
                                         switch (ch)  
312                                          {                                          {
                                         case KEY_UP:  
                                                 c_line_begin--;  
                                                 if (c_line_begin >= 0)  
                                                 {  
                                                         rewind(fin);  
                                                         for (f_line = 0; f_line < c_line_begin; f_line++)  
                                                         {  
                                                                 if (fgets(buffer, 255, fin) == NULL)  
                                                                         goto exit;  
                                                         }  
                                                 }  
                                                 else  
                                                 {  
                                                         goto exit;  
                                                 }  
                                                 break;  
                                         case KEY_DOWN:  
                                         case CR:  
                                                 c_line_begin++;  
                                                 rewind(fin);  
                                                 for (f_line = 0; f_line < c_line_begin; f_line++)  
                                                 {  
                                                         if (fgets(buffer, 255, fin) == NULL)  
                                                                 goto exit;  
                                                 }  
313                                                  break;                                                  break;
314                                          case KEY_PGUP:                                          }
315                                          case Ctrl('B'):                                          c_line_current -= line;
316                                                  if (c_line_begin > 0)                                          line = begin_line;
317                                                          c_line_begin -= (max_lines - begin_line - 1);                                          max_lines = begin_line + 1;
318                                                  else                                          prints("\033[T"); // Scroll down 1 line
319                                                          goto exit;                                          // max_lines = screen_rows - 1; // Legacy Fterm only works with this line
320                                                  if (c_line_begin < 0)                                          break;
321                                                          c_line_begin = 0;                                  case KEY_DOWN:
322                                                  rewind(fin);                                  case CR:
323                                                  for (f_line = 0; f_line < c_line_begin; f_line++)                                          if (c_line_current + ((screen_rows - 2) - (line - 1)) >= c_line_total) // Reach bottom
324                                                  {                                          {
                                                         if (fgets(buffer, 255, fin) == NULL)  
                                                                 goto exit;  
                                                 }  
                                                 break;  
                                         case KEY_RIGHT:  
                                         case KEY_PGDN:  
                                         case Ctrl('F'):  
                                         case KEY_SPACE:  
                                                 c_line_begin += (max_lines - begin_line - 1);  
                                                 if (c_line_begin + (max_lines - begin_line) >  
                                                         c_line_total)  
                                                         c_line_begin =  
                                                                 c_line_total - (max_lines - begin_line);  
                                                 rewind(fin);  
                                                 for (f_line = 0; f_line < c_line_begin; f_line++)  
                                                 {  
                                                         if (fgets(buffer, 255, fin) == NULL)  
                                                                 goto exit;  
                                                 }  
                                                 break;  
                                         case KEY_NULL:  
                                         case KEY_TIMEOUT:  
                                         case KEY_LEFT:  
                                         case 'q':  
                                         case 'Q':  
                                                 goto exit;  
325                                                  break;                                                  break;
326                                          case '?':                                          }
327                                          case 'h':                                          c_line_current += ((screen_rows - 2) - (line - 1));
328                                          case 'H':                                          line = screen_rows - 2;
329                                                  // Display help information                                          max_lines = screen_rows - 1;
330                                                  strcpy(temp, app_home_dir);                                          moveto(screen_rows, 0);
331                                                  strcat(temp, "data/read_help.txt");                                          clrtoeol();
332                                                  display_file_ex(temp, begin_line, 1);                                          prints("\033[S"); // Scroll up 1 line
333                                            break;
334                                                  // Refresh after display help information                                  case KEY_PGUP:
335                                                  rewind(fin);                                  case Ctrl('B'):
336                                                  for (f_line = 0; f_line < c_line_begin; f_line++)                                          if (c_line_current - line < 0) // Reach top
337                                                  {                                          {
                                                         if (fgets(buffer, 255, fin) == NULL)  
                                                                 goto exit;  
                                                 }  
338                                                  break;                                                  break;
339                                          default:                                          }
340                                                  input_ok = 0;                                          c_line_current -= ((screen_rows - 3) + (line - 1));
341                                            if (c_line_current < 0)
342                                            {
343                                                    c_line_current = 0;
344                                            }
345                                            line = begin_line;
346                                            max_lines = screen_rows - 1;
347                                            clrline(begin_line, screen_rows);
348                                            break;
349                                    case KEY_RIGHT:
350                                    case KEY_PGDN:
351                                    case Ctrl('F'):
352                                    case KEY_SPACE:
353                                            if (c_line_current + (screen_rows - 2) - (line - 1) >= c_line_total) // Reach bottom
354                                            {
355                                                  break;                                                  break;
356                                          }                                          }
357                                            c_line_current += (screen_rows - 3) - (line - 1);
358                                            if (c_line_current + screen_rows - 2 > c_line_total) // No enough lines to display
359                                            {
360                                                    c_line_current = c_line_total - (screen_rows - 2);
361                                            }
362                                            line = begin_line;
363                                            max_lines = screen_rows - 1;
364                                            clrline(begin_line, screen_rows);
365                                            break;
366                                    case KEY_NULL:
367                                    case KEY_TIMEOUT:
368                                    case KEY_LEFT:
369                                    case 'q':
370                                    case 'Q':
371                                            loop = 0;
372                                            break;
373                                    case '?':
374                                    case 'h':
375                                    case 'H':
376                                            // Display help information
377                                            display_file_ex(DATA_READ_HELP, begin_line, 1);
378    
379                                            // Refresh after display help information
380                                            c_line_current -= (line - 1);
381                                            line = begin_line;
382                                            max_lines = screen_rows - 1;
383                                            clrline(begin_line, screen_rows);
384                                            break;
385                                    default:
386                                            input_ok = 0;
387                                            break;
388                                  }                                  }
   
                                 clrline(begin_line, screen_lines);  
                                 line = begin_line;  
                                 moveto(line, 0);  
   
                                 continue;  
389                          }                          }
390    
391                          i = strlen(buffer);                          continue;
                         if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')  
                         {  
                                 buffer[i - 1] = '\r';  
                                 buffer[i] = '\n';  
                                 buffer[i + 1] = '\0';  
                         }  
                         prints(buffer);  
                         iflush();  
   
                         line++;  
392                  }                  }
                 if (wait)  
                         ch = press_any_key();  
                 else  
                         ch = 0;  
   
         exit:  
                 fclose(fin);  
393    
394                  return ch;                  fseek(fin, p_line_offsets[c_line_current], SEEK_SET);
395                    len = p_line_offsets[c_line_current + 1] - p_line_offsets[c_line_current];
396                    if (len >= LINE_BUFFER_LEN)
397                    {
398                            log_error("Error length exceeds buffer size: %d\n", len);
399                            len = LINE_BUFFER_LEN - 1;
400                    }
401                    if (fgets(buffer, len + 1, fin) == NULL)
402                    {
403                            log_error("Reach EOF\n");
404                            break;
405                    }
406                    moveto(line, 0);
407                    clrtoeol();
408                    prints("%s", buffer);
409                    c_line_current++;
410                    line++;
411          }          }
412    
413          return -1;          free(p_line_offsets);
414            fclose(fin);
415    
416            return ch;
417  }  }
418    
419  int show_top(char *status)  int show_top(char *status)
420  {  {
421          char buffer[256];          char buffer[LINE_BUFFER_LEN];
422    
423          str_space(buffer, 20 - strlen(BBS_current_section_name));          str_space(buffer, 20 - strnlen(BBS_current_section_name, sizeof(BBS_current_section_name)));
424    
425          moveto(1, 0);          moveto(1, 0);
426            clrtoeol();
427          prints("\033[1;44;33m%-20s \033[37m%20s"          prints("\033[1;44;33m%-20s \033[37m%20s"
428                     "         %s\033[33m讨论区 [%s]\033[m",                     "         %s\033[33m讨论区 [%s]\033[m",
429                     status, BBS_name, buffer, BBS_current_section_name);                     status, BBS_name, buffer, BBS_current_section_name);
# Line 410  int show_top(char *status) Line 434  int show_top(char *status)
434    
435  int show_bottom(char *msg)  int show_bottom(char *msg)
436  {  {
437          char str_time[256], str_time_onine[20], buffer[256];          char str_time[LINE_BUFFER_LEN];
438            char buffer[LINE_BUFFER_LEN];
439          time_t time_online;          time_t time_online;
440          struct tm *tm_online;          struct tm *tm_online;
441    
442          get_time_str(str_time, 256);          get_time_str(str_time, sizeof(str_time));
443          str_space(buffer, 33 - strlen(BBS_username));          str_space(buffer, 33 - strnlen(BBS_username, sizeof(BBS_username)));
444    
445          time_online = time(0) - BBS_login_tm;          time_online = time(0) - BBS_login_tm;
446          tm_online = gmtime(&time_online);          tm_online = gmtime(&time_online);
447    
448          moveto(screen_lines, 0);          moveto(screen_rows, 0);
449            clrtoeol();
450          prints("\033[1;44;33m[\033[36m%s\033[33m]"          prints("\033[1;44;33m[\033[36m%s\033[33m]"
451                     "%s帐号[\033[36m%s\033[33m]"                     "%s帐号[\033[36m%s\033[33m]"
452                     "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",                     "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",
# Line 433  int show_bottom(char *msg) Line 459  int show_bottom(char *msg)
459    
460  int show_active_board()  int show_active_board()
461  {  {
462          char filename[256], buffer[260];          char filename[FILE_PATH_LEN];
463            char buffer[LINE_BUFFER_LEN];
464          FILE *fin;          FILE *fin;
465          int i, j;          static int line;
466          static long int line;          int len;
467            int end_of_line;
         sprintf(filename, "%sdata/active_board.txt", app_home_dir);  
468    
469          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);
470    
471          moveto(3, 0);          if ((fin = fopen(DATA_ACTIVE_BOARD, "r")) == NULL)
472            {
473                    log_error("Unable to open file %s\n", filename);
474                    return -1;
475            }
476    
477          if ((fin = fopen(filename, "r")) != NULL)          for (int i = 0; i < line; i++)
478          {          {
479                  for (j = 0; j < line; j++)                  if (fgets(buffer, sizeof(buffer), fin) == NULL)
480                  {                  {
481                          if (fgets(buffer, 255, fin) == NULL)                          line = 0;
482                          {                          rewind(fin);
483                                  line = 0;                          break;
                                 rewind(fin);  
                                 break;  
                         }  
484                  }                  }
485            }
486    
487                  for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++)          for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
488            {
489                    if (fgets(buffer, sizeof(buffer), fin) == NULL)
490                  {                  {
491                          if (fgets(buffer, 255, fin) == NULL)                          line = 0;
492                          {                          break;
                                 line = 0;  
                                 if (j == 0)  
                                 {  
                                         rewind(fin);  
                                         if (fgets(buffer, 255, fin) == NULL)  
                                         {  
                                                 break;  
                                         }  
                                 }  
                                 else  
                                 {  
                                         break;  
                                 }  
                         }  
                         line++;  
                         i = strlen(buffer);  
                         if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')  
                         {  
                                 buffer[i - 1] = '\r';  
                                 buffer[i] = '\n';  
                                 buffer[i + 1] = '\0';  
                         }  
                         prints(buffer);  
                         iflush();  
493                  }                  }
494                  fclose(fin);                  line++;
495                    len = split_line(buffer, screen_cols, &end_of_line);
496                    buffer[len] = '\0'; // Truncate over-length line
497                    moveto(3 + i, 0);
498                    prints("%s", buffer);
499          }          }
500            iflush();
501    
502            fclose(fin);
503    
504          return 0;          return 0;
505  }  }


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

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