/[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.15 by sysadm, Fri Apr 8 11:11:11 2005 UTC Revision 1.27 by sysadm, Sat May 3 10:11:37 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;
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);          igetch(1);
   clrtoeol ();  
80    
81    prints ("                           \033[1;33m按任意键继续...\033[0;37m");          moveto(screen_rows, 0);
82    iflush ();          clrtoeol();
83    
84    return igetch_t (60);          prints("                           \033[1;33m按任意键继续...\033[0;37m");
85  }          iflush();
86    
87  void          return igetch_t(60);
 set_input_echo (int echo)  
 {  
   if (echo)  
     {  
       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);  
     }  
88  }  }
89    
90  int  void set_input_echo(int echo)
 str_input (char *buffer, int buffer_length, int echo_mode)  
91  {  {
92    char buf[256], ch;          if (echo)
   int c, offset = 0, len, loop = 1, i, hz = 0;  
   
   memset (buffer, '\0', buffer_length);  
   
   while (c = igetch_t (60))  
     {  
       if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR)  
         break;  
       if (c == LF)  
         continue;  
       if (c == BACKSPACE)  
93          {          {
94            if (offset > 0)                  outc('\x83'); // ASCII code 131
95              {                  iflush();
               buffer[--offset] = '\0';  
               prints ("\b \b");  
 //            clrtoeol ();  
               iflush ();  
             }  
           continue;  
96          }          }
97        if (c > 255 || iscntrl (c))          else
98          {          {
99            continue;                  //    outc ('\x85'); // ASCII code 133
100                    prints("\xff\xfb\x01\xff\xfb\x03");
101                    iflush();
102                    igetch(0);
103                    igetch(1);
104          }          }
105        if (c > 127 && c <= 255)  }
106          {  
107            hz = (!hz);  static int _str_input(char *buffer, int buffer_length, int echo_mode)
108          }  {
109        if (offset >= buffer_length)          char buf[256], ch;
110          {          int c, offset = 0, len, loop = 1, i, hz = 0;
111            outc ('\a');  
112            continue;          for (i = 0; i < buffer_length && buffer[i] != '\0'; i++)
         }  
       buffer[offset++] = (char) c;  
       buffer[offset] = '\0';  
       switch (echo_mode)  
113          {          {
114          case 0:                  offset++;
           outc ((char) c);  
           break;  
         case 1:  
           outc ('*');  
           break;  
115          }          }
116        if (!hz)  
117            igetch(1);
118    
119            while (c = igetch_t(60))
120          {          {
121            iflush ();                  if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR)
122                    {
123                            break;
124                    }
125                    if (c == LF)
126                    {
127                            continue;
128                    }
129                    if (c == BACKSPACE)
130                    {
131                            if (offset > 0)
132                            {
133                                    buffer[--offset] = '\0';
134                                    prints("\b \b");
135                                    //            clrtoeol ();
136                                    iflush();
137                            }
138                            continue;
139                    }
140                    if (c > 255 || iscntrl(c))
141                    {
142                            continue;
143                    }
144                    if (c > 127 && c <= 255)
145                    {
146                            hz = (!hz);
147                    }
148                    if (offset >= buffer_length)
149                    {
150                            outc('\a');
151                            continue;
152                    }
153                    buffer[offset++] = (char)c;
154                    buffer[offset] = '\0';
155                    switch (echo_mode)
156                    {
157                    case DOECHO:
158                            outc((char)c);
159                            break;
160                    case NOECHO:
161                            outc('*');
162                            break;
163                    }
164                    if (!hz)
165                    {
166                            iflush();
167                    }
168          }          }
     }  
169    
170    prints ("\r\n");          prints("\r\n");
171    iflush ();          iflush();
172    
173    return offset;          return offset;
174  }  }
175    
176  int  int str_input(char *buffer, int buffer_length, int echo_mode)
177  get_data (int row, int col, char *prompt, char *buffer, int buffer_length, int echo_mode)  {
178            int offset;
179    
180            memset(buffer, '\0', buffer_length);
181    
182            offset = _str_input(buffer, buffer_length, echo_mode);
183    
184            return offset;
185    };
186    
187    int get_data(int row, int col, char *prompt, char *buffer, int buffer_length, int echo_mode)
188  {  {
189    int len;          int len;
190    
191            moveto(row, col);
192            prints(prompt);
193            prints(buffer);
194            iflush();
195    
196    moveto (row, col);          len = _str_input(buffer, buffer_length, echo_mode);
   iflush ();  
   prints (prompt);  
   prints (buffer);  
   iflush ();  
197    
198    len = str_input (buffer, buffer_length, echo_mode);          return len;
     
   return len;  
199  }  }
200    
201  int  int display_file(const char *filename)
 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    
207    if ((fin = fopen (filename, "r")) == NULL)          if ((fin = fopen(filename, "r")) == NULL)
208      {          {
209        return -1;                  return -1;
210      }          }
211    
212    while (fgets (buffer, 255, fin))          while (fgets(buffer, sizeof(buffer) - 1, fin))
     {  
       i = strlen (buffer);  
       if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')  
213          {          {
214            buffer[i - 1] = '\r';                  i = strnlen(buffer, sizeof(buffer) - 1);
215            buffer[i] = '\n';                  if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
216            buffer[i + 1] = '\0';                  {
217                            buffer[i - 1] = '\r';
218                            buffer[i] = '\n';
219                            buffer[i + 1] = '\0';
220                    }
221                    prints(buffer);
222                    iflush();
223          }          }
224        prints (buffer);          fclose(fin);
       iflush ();  
     }  
   fclose (fin);  
225    
226    return 0;          return 0;
227  }  }
228    
229  int  int display_file_ex(const char *filename, int begin_line, int wait)
 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;          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    FILE *fin;          long int c_line_current = 0, c_line_total = 0;
236    struct stat f_stat;          FILE *fin;
237            struct stat f_stat;
238    clrline (begin_line, screen_lines);          long *p_line_offsets;
239    line = begin_line;          int len;
240    moveto (line, 0);          int percentile;
241            int loop = 1;
242    
243    if ((fin = fopen (filename, "r")) != NULL)          if ((fin = fopen(filename, "r")) == NULL)
     {  
       if (fstat (fileno (fin), &f_stat) != 0)  
244          {          {
245            log_error ("Get file stat failed\n");                  log_error("Unable to open file %s\n", filename);
246            return -1;                  return -1;
247          }          }
       f_size = f_stat.st_size;  
248    
249        while (fgets (buffer, 255, fin))          p_line_offsets = (long *)malloc(sizeof(long) * MAX_FILE_LINES);
         c_line_total++;  
       rewind (fin);  
250    
251        while (fgets (buffer, 255, fin))          c_line_total = split_file_lines(fin, screen_cols, p_line_offsets, MAX_FILE_LINES);
         {  
           if (line >= screen_lines)  
             {  
               f_offset = ftell (fin);  
252    
253                moveto (screen_lines, 0);          clrline(begin_line, screen_rows);
254                prints          line = begin_line;
255                  ("\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 ();  
256    
257                input_ok = 0;          while (loop)
258                while (!input_ok)          {
259                    if (c_line_current >= c_line_total)
260                  {                  {
261                    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++)  
262                          {                          {
263                            if (fgets (buffer, 255, fin) == NULL)                                  ch = press_any_key();
                             goto exit;  
264                          }                          }
265                        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++)  
266                          {                          {
267                            if (fgets (buffer, 255, fin) == NULL)                                  iflush();
                             goto exit;  
268                          }                          }
269                        break;  
270                      case KEY_RIGHT:                          loop = 0;
271                      case KEY_PGDN:                          break;
272                      case Ctrl ('F'):                  }
273                      case KEY_SPACE:  
274                        c_line_begin += (screen_lines - begin_line - 1);                  if (line >= max_lines)
275                        if (c_line_begin + (screen_lines - begin_line) >                  {
276                            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++)  
277                          {                          {
278                            if (fgets (buffer, 255, fin) == NULL)                                  percentile = (c_line_current - (line - 1) + (screen_rows - 2)) * 100 / c_line_total;
                             goto exit;  
279                          }                          }
280                        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++)  
281                          {                          {
282                            if (fgets (buffer, 255, fin) == NULL)                                  log_error("P100 reached\n");
283                              goto exit;                                  percentile = 100;
284                          }                          }
                       break;  
                     default:  
                       input_ok = 0;  
                       break;  
                     }  
                 }  
285    
286                clrline (begin_line, screen_lines);                          moveto(screen_rows, 0);
287                line = begin_line;                          prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
288                moveto (line, 0);                                     percentile);
289                            iflush();
290    
291                continue;                          input_ok = 0;
292              }                          while (!input_ok)
293                            {
294                                    ch = igetch_t(MAX_DELAY_TIME);
295                                    input_ok = 1;
296                                    switch (ch)
297                                    {
298                                    case KEY_UP:
299                                            if (c_line_current - line < 0) // Reach top
300                                            {
301                                                    break;
302                                            }
303                                            c_line_current -= line;
304                                            line = begin_line;
305                                            max_lines = begin_line + 1;
306                                            prints("\033[1T"); // Scroll down 1 line
307                                            break;
308                                    case KEY_DOWN:
309                                    case CR:
310                                            if (c_line_current + ((screen_rows - 2) - (line - 1)) >= c_line_total) // Reach bottom
311                                            {
312                                                    break;
313                                            }
314                                            c_line_current += ((screen_rows - 2) - (line - 1));
315                                            line = screen_rows - 2;
316                                            max_lines = screen_rows - 1;
317                                            moveto(screen_rows, 0);
318                                            clrtoeol();
319                                            prints("\033[1S"); // Scroll up 1 line
320                                            break;
321                                    case KEY_PGUP:
322                                    case Ctrl('B'):
323                                            if (c_line_current - line < 0) // Reach top
324                                            {
325                                                    break;
326                                            }
327                                            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;
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                                    }
378                            }
379    
380            i = strlen (buffer);                          continue;
381            if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')                  }
             {  
               buffer[i - 1] = '\r';  
               buffer[i] = '\n';  
               buffer[i + 1] = '\0';  
             }  
           prints (buffer);  
           iflush ();  
382    
383            line++;                  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          }          }
       if (wait)  
         ch = press_any_key ();  
       else  
         ch = 0;  
401    
402      exit:          free(p_line_offsets);
403        fclose (fin);          fclose(fin);
404    
405        return ch;          return ch;
     }  
   
   return -1;  
406  }  }
407    
408  int  int show_top(char *status)
 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    prints ("\033[1;44;33m%-20s \033[37m%20s"          clrtoeol();
416            "         %s\033[33m讨论区 [%s]\033[m",          prints("\033[1;44;33m%-20s \033[37m%20s"
417            status, BBS_name, buffer, BBS_current_section_name);                     "         %s\033[33m讨论区 [%s]\033[m",
418    iflush ();                     status, BBS_name, buffer, BBS_current_section_name);
419            iflush();
420    
421    return 0;          return 0;
422  }  }
423    
424  int  int show_bottom(char *msg)
 show_bottom (char *msg)  
425  {  {
426    char str_time[256], str_time_onine[20], buffer[256];          char str_time[LINE_BUFFER_LEN];
427    time_t time_online;          char str_time_onine[20];
428    struct tm *tm_online;          char buffer[LINE_BUFFER_LEN];
429            time_t time_online;
430            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    prints ("\033[1;44;33m[\033[36m%s\033[33m]"          clrtoeol();
440            "%s帐号[\033[36m%s\033[33m]"          prints("\033[1;44;33m[\033[36m%s\033[33m]"
441            "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",                     "%s帐号[\033[36m%s\033[33m]"
442            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",
443            tm_online->tm_hour, tm_online->tm_min);                     str_time, buffer, BBS_username, tm_online->tm_mday - 1,
444    iflush ();                     tm_online->tm_hour, tm_online->tm_min);
445            iflush();
446    
447    return 0;          return 0;
448  }  }
449    
450  int  int show_active_board()
 show_active_board ()  
451  {  {
452    char filename[256], buffer[260];          char filename[FILE_PATH_LEN];
453    FILE *fin;          char buffer[LINE_BUFFER_LEN];
454    int i, j;          FILE *fin;
455    static long int line;          static int line;
456            int len;
457    sprintf (filename, "%sdata/active_board.txt", app_home_dir);          int end_of_line;
458    
459    clrline (3, 2 + ACTIVE_BOARD_HEIGHT);          sprintf(filename, "%sdata/active_board.txt", app_home_dir);
460    
461    moveto (3, 0);          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);
462    
463    if ((fin = fopen (filename, "r")) != NULL)          if ((fin = fopen(filename, "r")) == NULL)
     {  
       for (j = 0; j < line; j++)  
464          {          {
465            if (fgets (buffer, 255, fin) == NULL)                  log_error("Unable to open file %s\n", filename);
466              {                  return -1;
               line = 0;  
               rewind (fin);  
               break;  
             }  
467          }          }
468    
469        for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++)          for (int i = 0; i < line; i++)
470          {          {
471            if (fgets (buffer, 255, fin) == NULL)                  if (fgets(buffer, sizeof(buffer), fin) == NULL)
             {  
               line = 0;  
               if (j == 0)  
472                  {                  {
473                    rewind (fin);                          line = 0;
474                    if (fgets (buffer, 255, fin) == NULL)                          rewind(fin);
475                      {                          break;
                       break;  
                     }  
476                  }                  }
477                else          }
478    
479            for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
480            {
481                    if (fgets(buffer, sizeof(buffer), fin) == NULL)
482                  {                  {
483                    break;                          line = 0;
484                            break;
485                  }                  }
486              }                  line++;
487            line++;                  len = split_line(buffer, screen_cols, &end_of_line);
488            i = strlen (buffer);                  buffer[len] = '\0'; // Truncate over-length line
489            if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')                  moveto(3 + i, 0);
490              {                  prints("%s", buffer);
               buffer[i - 1] = '\r';  
               buffer[i] = '\n';  
               buffer[i + 1] = '\0';  
             }  
           prints (buffer);  
           iflush ();  
491          }          }
492        fclose (fin);          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