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


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

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