/[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.23 by sysadm, Tue Apr 29 14:04:10 2025 UTC Revision 1.26 by sysadm, Sat May 3 06:24:54 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"
22  #include "io.h"  #include "io.h"
23    #include "screen.h"
24    #include <string.h>
25    #include <ctype.h>
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 36  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 54  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 68  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 94  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    
107  int _str_input(char *buffer, int buffer_length, int echo_mode)  static int _str_input(char *buffer, int buffer_length, int echo_mode)
108  {  {
109          char buf[256], ch;          char buf[256], ch;
110          int c, offset = 0, len, loop = 1, i, hz = 0;          int c, offset = 0, len, loop = 1, i, hz = 0;
# Line 109  int _str_input(char *buffer, int buffer_ Line 114  int _str_input(char *buffer, int buffer_
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 182  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 194  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 203  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 222  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    
242            if ((fin = fopen(filename, "r")) == NULL)
243            {
244                    log_error("Unable to open file %s\n", filename);
245                    return -1;
246            }
247    
248            p_line_offsets = (long *)malloc(sizeof(long) * MAX_FILE_LINES);
249    
250            c_line_total = split_file_lines(fin, screen_cols, p_line_offsets, MAX_FILE_LINES);
251    
252          max_lines = screen_lines - 1;          clrline(begin_line, screen_rows);
         clrline(begin_line, screen_lines);  
253          line = begin_line;          line = begin_line;
254          moveto(line, 0);          max_lines = screen_rows - 1;
255    
256          if ((fin = fopen(filename, "r")) != NULL)          while (c_line_current < c_line_total)
257          {          {
258                  if (fstat(fileno(fin), &f_stat) != 0)                  if (line >= max_lines)
259                  {                  {
260                          log_error("Get file stat failed\n");                          percentile = (c_line_current - (line - 1) + (screen_rows - 2)) * 100 / c_line_total;
                         return -1;  
                 }  
                 f_size = f_stat.st_size;  
261    
262                  while (fgets(buffer, 255, fin))                          moveto(screen_rows, 0);
263                          c_line_total++;                          prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
264                  rewind(fin);                                     percentile);
265                            iflush();
266    
267                  while (fgets(buffer, 255, fin))                          input_ok = 0;
268                  {                          while (!input_ok)
                         if (line >= max_lines)  
269                          {                          {
270                                  f_offset = ftell(fin);                                  ch = igetch_t(MAX_DELAY_TIME);
271                                    input_ok = 1;
272                                  moveto(screen_lines, 0);                                  switch (ch)
                                 prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",  
                                            (f_offset - strlen(buffer)) * 100 / f_size);  
                                 iflush();  
   
                                 input_ok = 0;  
                                 while (!input_ok)  
273                                  {                                  {
274                                          ch = igetch_t(MAX_DELAY_TIME);                                  case KEY_UP:
275                                          input_ok = 1;                                          if (c_line_current - line < 0) // Reach top
                                         switch (ch)  
276                                          {                                          {
                                         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;  
                                                 }  
277                                                  break;                                                  break;
278                                          case KEY_RIGHT:                                          }
279                                          case KEY_PGDN:                                          c_line_current -= line;
280                                          case Ctrl('F'):                                          line = begin_line;
281                                          case KEY_SPACE:                                          max_lines = begin_line + 1;
282                                                  c_line_begin += (max_lines - begin_line - 1);                                          prints("\033[1T"); // Scroll down 1 line
283                                                  if (c_line_begin + (max_lines - begin_line) >                                          break;
284                                                          c_line_total)                                  case KEY_DOWN:
285                                                          c_line_begin =                                  case CR:
286                                                                  c_line_total - (max_lines - begin_line);                                          if (c_line_current + ((screen_rows - 2) - (line - 1)) >= c_line_total) // Reach bottom
287                                                  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;  
288                                                  break;                                                  break;
289                                          case '?':                                          }
290                                          case 'h':                                          c_line_current += ((screen_rows - 2) - (line - 1));
291                                          case 'H':                                          line = screen_rows - 2;
292                                                  // Display help information                                          max_lines = screen_rows - 1;
293                                                  strcpy(temp, app_home_dir);                                          moveto(screen_rows, 0);
294                                                  strcat(temp, "data/read_help.txt");                                          clrtoeol();
295                                                  display_file_ex(temp, begin_line, 1);                                          prints("\033[1S"); // Scroll up 1 line
296                                            break;
297                                                  // Refresh after display help information                                  case KEY_PGUP:
298                                                  rewind(fin);                                  case Ctrl('B'):
299                                                  for (f_line = 0; f_line < c_line_begin; f_line++)                                          if (c_line_current - line < 0) // Reach top
300                                                  {                                          {
                                                         if (fgets(buffer, 255, fin) == NULL)  
                                                                 goto exit;  
                                                 }  
301                                                  break;                                                  break;
302                                          default:                                          }
303                                                  input_ok = 0;                                          c_line_current -= ((screen_rows - 3) + (line - 1));
304                                            if (c_line_current < 0)
305                                            {
306                                                    c_line_current = 0;
307                                            }
308                                            line = begin_line;
309                                            max_lines = screen_rows - 1;
310                                            clrline(begin_line, screen_rows);
311                                            break;
312                                    case KEY_RIGHT:
313                                    case KEY_PGDN:
314                                    case Ctrl('F'):
315                                    case KEY_SPACE:
316                                            if (c_line_current + (screen_rows - 2) - (line - 1) >= c_line_total) // Reach bottom
317                                            {
318                                                  break;                                                  break;
319                                          }                                          }
320                                            c_line_current += (screen_rows - 3) - (line - 1);
321                                            if (c_line_current + screen_rows - 2 > c_line_total) // No enough lines to display
322                                            {
323                                                    c_line_current = c_line_total - (screen_rows - 2);
324                                            }
325                                            line = begin_line;
326                                            max_lines = screen_rows - 1;
327                                            clrline(begin_line, screen_rows);
328                                            break;
329                                    case KEY_NULL:
330                                    case KEY_TIMEOUT:
331                                    case KEY_LEFT:
332                                    case 'q':
333                                    case 'Q':
334                                            c_line_current = c_line_total;
335                                            wait = 0;
336                                            break;
337                                    case '?':
338                                    case 'h':
339                                    case 'H':
340                                            // Display help information
341                                            strcpy(temp, app_home_dir);
342                                            strcat(temp, "data/read_help.txt");
343                                            display_file_ex(temp, begin_line, 1);
344    
345                                            // Refresh after display help information
346                                            c_line_current -= (line - 1);
347                                            line = begin_line;
348                                            max_lines = screen_rows - 1;
349                                            clrline(begin_line, screen_rows);
350                                            break;
351                                    default:
352                                            input_ok = 0;
353                                            break;
354                                  }                                  }
   
                                 clrline(begin_line, screen_lines);  
                                 line = begin_line;  
                                 moveto(line, 0);  
   
                                 continue;  
355                          }                          }
356    
357                          i = strlen(buffer);                          continue;
358                          if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')                  }
                         {  
                                 buffer[i - 1] = '\r';  
                                 buffer[i] = '\n';  
                                 buffer[i + 1] = '\0';  
                         }  
                         prints(buffer);  
                         iflush();  
359    
360                          line++;                  fseek(fin, p_line_offsets[c_line_current], SEEK_SET);
361                    len = p_line_offsets[c_line_current + 1] - p_line_offsets[c_line_current];
362                    if (len >= LINE_BUFFER_LEN)
363                    {
364                            log_error("Error length exceeds buffer size: %d\n", len);
365                            len = LINE_BUFFER_LEN - 1;
366                  }                  }
367                  if (wait)                  if (fgets(buffer, len + 1, fin) == NULL)
368                          ch = press_any_key();                  {
369                  else                          log_error("Reach EOF\n");
370                          ch = 0;                          break;
371                    }
372                    moveto(line, 0);
373                    clrtoeol();
374                    prints("%s", buffer);
375                    c_line_current++;
376                    line++;
377            }
378    
379          exit:          iflush();
                 fclose(fin);  
380    
381                  return ch;          if (wait)
382            {
383                    ch = press_any_key();
384          }          }
385    
386          return -1;          free(p_line_offsets);
387            fclose(fin);
388    
389            return ch;
390  }  }
391    
392  int show_top(char *status)  int show_top(char *status)
393  {  {
394          char buffer[256];          char buffer[LINE_BUFFER_LEN];
395    
396          str_space(buffer, 20 - strlen(BBS_current_section_name));          str_space(buffer, 20 - strlen(BBS_current_section_name));
397    
398          moveto(1, 0);          moveto(1, 0);
399            clrtoeol();
400          prints("\033[1;44;33m%-20s \033[37m%20s"          prints("\033[1;44;33m%-20s \033[37m%20s"
401                     "         %s\033[33m讨论区 [%s]\033[m",                     "         %s\033[33m讨论区 [%s]\033[m",
402                     status, BBS_name, buffer, BBS_current_section_name);                     status, BBS_name, buffer, BBS_current_section_name);
# Line 401  int show_top(char *status) Line 407  int show_top(char *status)
407    
408  int show_bottom(char *msg)  int show_bottom(char *msg)
409  {  {
410          char str_time[256], str_time_onine[20], buffer[256];          char str_time[LINE_BUFFER_LEN];
411            char str_time_onine[20];
412            char buffer[LINE_BUFFER_LEN];
413          time_t time_online;          time_t time_online;
414          struct tm *tm_online;          struct tm *tm_online;
415    
416          get_time_str(str_time, 256);          get_time_str(str_time, sizeof(str_time));
417          str_space(buffer, 33 - strlen(BBS_username));          str_space(buffer, 33 - strlen(BBS_username));
418    
419          time_online = time(0) - BBS_login_tm;          time_online = time(0) - BBS_login_tm;
420          tm_online = gmtime(&time_online);          tm_online = gmtime(&time_online);
421    
422          moveto(screen_lines, 0);          moveto(screen_rows, 0);
423            clrtoeol();
424          prints("\033[1;44;33m[\033[36m%s\033[33m]"          prints("\033[1;44;33m[\033[36m%s\033[33m]"
425                     "%s帐号[\033[36m%s\033[33m]"                     "%s帐号[\033[36m%s\033[33m]"
426                     "[\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 424  int show_bottom(char *msg) Line 433  int show_bottom(char *msg)
433    
434  int show_active_board()  int show_active_board()
435  {  {
436          char filename[256], buffer[260];          char filename[FILE_PATH_LEN];
437            char buffer[LINE_BUFFER_LEN];
438          FILE *fin;          FILE *fin;
439          int i, j;          static int line;
440          static long int line;          int len;
441            int end_of_line;
442    
443          sprintf(filename, "%sdata/active_board.txt", app_home_dir);          sprintf(filename, "%sdata/active_board.txt", app_home_dir);
444    
445          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);
446    
447          moveto(3, 0);          if ((fin = fopen(filename, "r")) == NULL)
448            {
449                    log_error("Unable to open file %s\n", filename);
450                    return -1;
451            }
452    
453          if ((fin = fopen(filename, "r")) != NULL)          for (int i = 0; i < line; i++)
454          {          {
455                  for (j = 0; j < line; j++)                  if (fgets(buffer, sizeof(buffer), fin) == NULL)
456                  {                  {
457                          if (fgets(buffer, 255, fin) == NULL)                          line = 0;
458                          {                          rewind(fin);
459                                  line = 0;                          break;
                                 rewind(fin);  
                                 break;  
                         }  
460                  }                  }
461            }
462    
463                  for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++)          for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
464            {
465                    if (fgets(buffer, sizeof(buffer), fin) == NULL)
466                  {                  {
467                          if (fgets(buffer, 255, fin) == NULL)                          line = 0;
468                          {                          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();  
469                  }                  }
470                  fclose(fin);                  line++;
471                    len = split_line(buffer, screen_cols, &end_of_line);
472                    buffer[len] = '\0'; // Truncate over-length line
473                    moveto(3 + i, 0);
474                    prints("%s", buffer);
475          }          }
476            iflush();
477    
478            fclose(fin);
479    
480          return 0;          return 0;
481  }  }


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

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