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


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

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