/[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.24 by sysadm, Wed Apr 30 09:18:20 2025 UTC Revision 1.27 by sysadm, Sat May 3 10:11:37 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  {  {
66          clrline(line_begin, screen_lines);          moveto(line_begin, 0);
67            prints("\033[J");
68          moveto(line_begin, 0);          moveto(line_begin, 0);
69  }  }
70    
# Line 72  void clearscr() Line 72  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);          igetch(1);
80    
81            moveto(screen_rows, 0);
82          clrtoeol();          clrtoeol();
83    
84          prints("                           \033[1;33m按任意键继续...\033[0;37m");          prints("                           \033[1;33m按任意键继续...\033[0;37m");
# Line 98  void set_input_echo(int echo) Line 99  void set_input_echo(int echo)
99                  //    outc ('\x85'); // ASCII code 133                  //    outc ('\x85'); // ASCII code 133
100                  prints("\xff\xfb\x01\xff\xfb\x03");                  prints("\xff\xfb\x01\xff\xfb\x03");
101                  iflush();                  iflush();
102                  igetch_t(60);                  igetch(0);
103                  igetch_t(60);                  igetch(1);
104          }          }
105  }  }
106    
# Line 113  static int _str_input(char *buffer, int Line 114  static int _str_input(char *buffer, int
114                  offset++;                  offset++;
115          }          }
116    
117            igetch(1);
118    
119          while (c = igetch_t(60))          while (c = igetch_t(60))
120          {          {
121                  if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR)                  if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR)
# Line 186  int get_data(int row, int col, char *pro Line 189  int get_data(int row, int col, char *pro
189          int len;          int len;
190    
191          moveto(row, col);          moveto(row, col);
         iflush();  
192          prints(prompt);          prints(prompt);
193          prints(buffer);          prints(buffer);
194          iflush();          iflush();
# Line 198  int get_data(int row, int col, char *pro Line 200  int get_data(int row, int col, char *pro
200    
201  int display_file(const char *filename)  int display_file(const char *filename)
202  {  {
203          char buffer[260];          char buffer[LINE_BUFFER_LEN];
204          FILE *fin;          FILE *fin;
205          int i;          int i;
206    
# Line 207  int display_file(const char *filename) Line 209  int display_file(const char *filename)
209                  return -1;                  return -1;
210          }          }
211    
212          while (fgets(buffer, 255, fin))          while (fgets(buffer, sizeof(buffer) - 1, fin))
213          {          {
214                  i = strlen(buffer);                  i = strnlen(buffer, sizeof(buffer) - 1);
215                  if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')                  if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
216                  {                  {
217                          buffer[i - 1] = '\r';                          buffer[i - 1] = '\r';
# Line 226  int display_file(const char *filename) Line 228  int display_file(const char *filename)
228    
229  int display_file_ex(const char *filename, int begin_line, int wait)  int display_file_ex(const char *filename, int begin_line, int wait)
230  {  {
231          char buffer[260], temp[256];          char buffer[LINE_BUFFER_LEN];
232          int i, ch, input_ok, max_lines;          char temp[LINE_BUFFER_LEN];
233          long int line, c_line_begin = 0, c_line_total = 0;          int ch = 0;
234          long int f_line, f_size, f_offset;          int input_ok, line, max_lines;
235            long int c_line_current = 0, c_line_total = 0;
236          FILE *fin;          FILE *fin;
237          struct stat f_stat;          struct stat f_stat;
238            long *p_line_offsets;
239            int len;
240            int percentile;
241            int loop = 1;
242    
243            if ((fin = fopen(filename, "r")) == NULL)
244            {
245                    log_error("Unable to open file %s\n", filename);
246                    return -1;
247            }
248    
249          max_lines = screen_lines - 1;          p_line_offsets = (long *)malloc(sizeof(long) * MAX_FILE_LINES);
250          clrline(begin_line, screen_lines);  
251            c_line_total = split_file_lines(fin, screen_cols, p_line_offsets, MAX_FILE_LINES);
252    
253            clrline(begin_line, screen_rows);
254          line = begin_line;          line = begin_line;
255          moveto(line, 0);          max_lines = screen_rows - 1;
256    
257          if ((fin = fopen(filename, "r")) != NULL)          while (loop)
258          {          {
259                  if (fstat(fileno(fin), &f_stat) != 0)                  if (c_line_current >= c_line_total)
260                  {                  {
261                          log_error("Get file stat failed\n");                          if (wait)
262                          return -1;                          {
263                  }                                  ch = press_any_key();
264                  f_size = f_stat.st_size;                          }
265                            else
266                            {
267                                    iflush();
268                            }
269    
270                  while (fgets(buffer, 255, fin))                          loop = 0;
271                          c_line_total++;                          break;
272                  rewind(fin);                  }
273    
274                  while (fgets(buffer, 255, fin))                  if (line >= max_lines)
275                  {                  {
276                          if (line >= max_lines)                          if (c_line_current - (line - 1) + (screen_rows - 2) < c_line_total)
277                            {
278                                    percentile = (c_line_current - (line - 1) + (screen_rows - 2)) * 100 / c_line_total;
279                            }
280                            else
281                          {                          {
282                                  f_offset = ftell(fin);                                  log_error("P100 reached\n");
283                                    percentile = 100;
284                            }
285    
286                                  moveto(screen_lines, 0);                          moveto(screen_rows, 0);
287                                  prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",                          prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
288                                             (f_offset - strlen(buffer)) * 100 / f_size);                                     percentile);
289                                  iflush();                          iflush();
290    
291                                  input_ok = 0;                          input_ok = 0;
292                                  while (!input_ok)                          while (!input_ok)
293                            {
294                                    ch = igetch_t(MAX_DELAY_TIME);
295                                    input_ok = 1;
296                                    switch (ch)
297                                  {                                  {
298                                          ch = igetch_t(MAX_DELAY_TIME);                                  case KEY_UP:
299                                          input_ok = 1;                                          if (c_line_current - line < 0) // Reach top
                                         switch (ch)  
300                                          {                                          {
                                         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;  
                                                 }  
                                                 break;  
                                         case KEY_PGUP:  
                                         case Ctrl('B'):  
                                                 if (c_line_begin > 0)  
                                                         c_line_begin -= (max_lines - begin_line - 1);  
                                                 else  
                                                         goto exit;  
                                                 if (c_line_begin < 0)  
                                                         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;  
                                                 }  
301                                                  break;                                                  break;
302                                          case KEY_RIGHT:                                          }
303                                          case KEY_PGDN:                                          c_line_current -= line;
304                                          case Ctrl('F'):                                          line = begin_line;
305                                          case KEY_SPACE:                                          max_lines = begin_line + 1;
306                                                  c_line_begin += (max_lines - begin_line - 1);                                          prints("\033[1T"); // Scroll down 1 line
307                                                  if (c_line_begin + (max_lines - begin_line) >                                          break;
308                                                          c_line_total)                                  case KEY_DOWN:
309                                                          c_line_begin =                                  case CR:
310                                                                  c_line_total - (max_lines - begin_line);                                          if (c_line_current + ((screen_rows - 2) - (line - 1)) >= c_line_total) // Reach bottom
311                                                  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;  
312                                                  break;                                                  break;
313                                          case '?':                                          }
314                                          case 'h':                                          c_line_current += ((screen_rows - 2) - (line - 1));
315                                          case 'H':                                          line = screen_rows - 2;
316                                                  // Display help information                                          max_lines = screen_rows - 1;
317                                                  strcpy(temp, app_home_dir);                                          moveto(screen_rows, 0);
318                                                  strcat(temp, "data/read_help.txt");                                          clrtoeol();
319                                                  display_file_ex(temp, begin_line, 1);                                          prints("\033[1S"); // Scroll up 1 line
320                                            break;
321                                                  // Refresh after display help information                                  case KEY_PGUP:
322                                                  rewind(fin);                                  case Ctrl('B'):
323                                                  for (f_line = 0; f_line < c_line_begin; f_line++)                                          if (c_line_current - line < 0) // Reach top
324                                                  {                                          {
                                                         if (fgets(buffer, 255, fin) == NULL)  
                                                                 goto exit;  
                                                 }  
325                                                  break;                                                  break;
326                                          default:                                          }
327                                                  input_ok = 0;                                          c_line_current -= ((screen_rows - 3) + (line - 1));
328                                            if (c_line_current < 0)
329                                            {
330                                                    c_line_current = 0;
331                                            }
332                                            line = begin_line;
333                                            max_lines = screen_rows - 1;
334                                            clrline(begin_line, screen_rows);
335                                            break;
336                                    case KEY_RIGHT:
337                                    case KEY_PGDN:
338                                    case Ctrl('F'):
339                                    case KEY_SPACE:
340                                            if (c_line_current + (screen_rows - 2) - (line - 1) >= c_line_total) // Reach bottom
341                                            {
342                                                  break;                                                  break;
343                                          }                                          }
344                                            c_line_current += (screen_rows - 3) - (line - 1);
345                                            if (c_line_current + screen_rows - 2 > c_line_total) // No enough lines to display
346                                            {
347                                                    c_line_current = c_line_total - (screen_rows - 2);
348                                            }
349                                            line = begin_line;
350                                            max_lines = screen_rows - 1;
351                                            clrline(begin_line, screen_rows);
352                                            break;
353                                    case KEY_NULL:
354                                    case KEY_TIMEOUT:
355                                    case KEY_LEFT:
356                                    case 'q':
357                                    case 'Q':
358                                            loop = 0;
359                                            break;
360                                    case '?':
361                                    case 'h':
362                                    case 'H':
363                                            // Display help information
364                                            strcpy(temp, app_home_dir);
365                                            strcat(temp, "data/read_help.txt");
366                                            display_file_ex(temp, begin_line, 1);
367    
368                                            // Refresh after display help information
369                                            c_line_current -= (line - 1);
370                                            line = begin_line;
371                                            max_lines = screen_rows - 1;
372                                            clrline(begin_line, screen_rows);
373                                            break;
374                                    default:
375                                            input_ok = 0;
376                                            break;
377                                  }                                  }
   
                                 clrline(begin_line, screen_lines);  
                                 line = begin_line;  
                                 moveto(line, 0);  
   
                                 continue;  
                         }  
   
                         i = strlen(buffer);  
                         if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')  
                         {  
                                 buffer[i - 1] = '\r';  
                                 buffer[i] = '\n';  
                                 buffer[i + 1] = '\0';  
378                          }                          }
                         prints(buffer);  
                         iflush();  
379    
380                          line++;                          continue;
381                  }                  }
                 if (wait)  
                         ch = press_any_key();  
                 else  
                         ch = 0;  
   
         exit:  
                 fclose(fin);  
382    
383                  return ch;                  fseek(fin, p_line_offsets[c_line_current], SEEK_SET);
384                    len = p_line_offsets[c_line_current + 1] - p_line_offsets[c_line_current];
385                    if (len >= LINE_BUFFER_LEN)
386                    {
387                            log_error("Error length exceeds buffer size: %d\n", len);
388                            len = LINE_BUFFER_LEN - 1;
389                    }
390                    if (fgets(buffer, len + 1, fin) == NULL)
391                    {
392                            log_error("Reach EOF\n");
393                            break;
394                    }
395                    moveto(line, 0);
396                    clrtoeol();
397                    prints("%s", buffer);
398                    c_line_current++;
399                    line++;
400          }          }
401    
402          return -1;          free(p_line_offsets);
403            fclose(fin);
404    
405            return ch;
406  }  }
407    
408  int show_top(char *status)  int show_top(char *status)
409  {  {
410          char buffer[256];          char buffer[LINE_BUFFER_LEN];
411    
412          str_space(buffer, 20 - strlen(BBS_current_section_name));          str_space(buffer, 20 - strlen(BBS_current_section_name));
413    
414          moveto(1, 0);          moveto(1, 0);
415            clrtoeol();
416          prints("\033[1;44;33m%-20s \033[37m%20s"          prints("\033[1;44;33m%-20s \033[37m%20s"
417                     "         %s\033[33m讨论区 [%s]\033[m",                     "         %s\033[33m讨论区 [%s]\033[m",
418                     status, BBS_name, buffer, BBS_current_section_name);                     status, BBS_name, buffer, BBS_current_section_name);
# Line 405  int show_top(char *status) Line 423  int show_top(char *status)
423    
424  int show_bottom(char *msg)  int show_bottom(char *msg)
425  {  {
426          char str_time[256], str_time_onine[20], buffer[256];          char str_time[LINE_BUFFER_LEN];
427            char str_time_onine[20];
428            char buffer[LINE_BUFFER_LEN];
429          time_t time_online;          time_t time_online;
430          struct tm *tm_online;          struct tm *tm_online;
431    
432          get_time_str(str_time, 256);          get_time_str(str_time, sizeof(str_time));
433          str_space(buffer, 33 - strlen(BBS_username));          str_space(buffer, 33 - strlen(BBS_username));
434    
435          time_online = time(0) - BBS_login_tm;          time_online = time(0) - BBS_login_tm;
436          tm_online = gmtime(&time_online);          tm_online = gmtime(&time_online);
437    
438          moveto(screen_lines, 0);          moveto(screen_rows, 0);
439            clrtoeol();
440          prints("\033[1;44;33m[\033[36m%s\033[33m]"          prints("\033[1;44;33m[\033[36m%s\033[33m]"
441                     "%s帐号[\033[36m%s\033[33m]"                     "%s帐号[\033[36m%s\033[33m]"
442                     "[\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 428  int show_bottom(char *msg) Line 449  int show_bottom(char *msg)
449    
450  int show_active_board()  int show_active_board()
451  {  {
452          char filename[256], buffer[260];          char filename[FILE_PATH_LEN];
453            char buffer[LINE_BUFFER_LEN];
454          FILE *fin;          FILE *fin;
455          int i, j;          static int line;
456          static long int line;          int len;
457            int end_of_line;
458    
459          sprintf(filename, "%sdata/active_board.txt", app_home_dir);          sprintf(filename, "%sdata/active_board.txt", app_home_dir);
460    
461          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);
462    
463          moveto(3, 0);          if ((fin = fopen(filename, "r")) == NULL)
464            {
465                    log_error("Unable to open file %s\n", filename);
466                    return -1;
467            }
468    
469          if ((fin = fopen(filename, "r")) != NULL)          for (int i = 0; i < line; i++)
470          {          {
471                  for (j = 0; j < line; j++)                  if (fgets(buffer, sizeof(buffer), fin) == NULL)
472                  {                  {
473                          if (fgets(buffer, 255, fin) == NULL)                          line = 0;
474                          {                          rewind(fin);
475                                  line = 0;                          break;
                                 rewind(fin);  
                                 break;  
                         }  
476                  }                  }
477            }
478    
479                  for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++)          for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
480            {
481                    if (fgets(buffer, sizeof(buffer), fin) == NULL)
482                  {                  {
483                          if (fgets(buffer, 255, fin) == NULL)                          line = 0;
484                          {                          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();  
485                  }                  }
486                  fclose(fin);                  line++;
487                    len = split_line(buffer, screen_cols, &end_of_line);
488                    buffer[len] = '\0'; // Truncate over-length line
489                    moveto(3 + i, 0);
490                    prints("%s", buffer);
491          }          }
492            iflush();
493    
494            fclose(fin);
495    
496          return 0;          return 0;
497  }  }


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

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