/[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.7 by sysadm, Tue Nov 30 07:10:32 2004 UTC Revision 1.26 by sysadm, Sat May 3 06:24:54 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;
   
   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 ();  
75  }  }
76    
77  void  int press_any_key()
 set_input_echo(int echo)  
78  {  {
79    char temp[256];          igetch(1);
80    
81    if (echo)          moveto(screen_rows, 0);
82    {          clrtoeol();
     outc ('\x83'); // ASCII code 131  
     iflush ();  
   }  
   else  
   {  
 //    outc ('\x85'); // ASCII code 133  
     prints ("\xff\xfb\x01\xff\xfb\x03");  
     iflush ();  
     igetch ();  
     igetch ();  
   }  
 }  
83    
84  int          prints("                           \033[1;33m按任意键继续...\033[0;37m");
85  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;  
86    
87    memset (buffer, '\0', buffer_length);          return igetch_t(60);
88    }
89    
90    while (c = igetch ())  void set_input_echo(int echo)
91      {  {
92        if (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)  
93          {          {
94            hz = (!hz);                  outc('\x83'); // ASCII code 131
95                    iflush();
96          }          }
97        if (offset >= buffer_length)          else
98          {          {
99            outc ('\a');                  //    outc ('\x85'); // ASCII code 133
100            continue;                  prints("\xff\xfb\x01\xff\xfb\x03");
101                    iflush();
102                    igetch(0);
103                    igetch(1);
104          }          }
105        buffer[offset++] = (char) c;  }
106        buffer[offset] = '\0';  
107        switch (echo_mode)  static int _str_input(char *buffer, int buffer_length, int echo_mode)
108    {
109            char buf[256], ch;
110            int c, offset = 0, len, loop = 1, i, hz = 0;
111    
112            for (i = 0; i < buffer_length && buffer[i] != '\0'; i++)
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  display_file (const char *filename)  {
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    char buffer[260];          int len;
   FILE *fin;  
   int i;  
190    
191    if ((fin = fopen (filename, "r")) != NULL)          moveto(row, col);
192    {          prints(prompt);
193      while (fgets (buffer, 255, fin))          prints(buffer);
194        {          iflush();
195          i = strlen (buffer);  
196          if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')          len = _str_input(buffer, buffer_length, echo_mode);
197            {  
198              buffer[i - 1] = '\r';          return len;
             buffer[i] = '\n';  
             buffer[i + 1] = '\0';  
           }  
         prints (buffer);  
         iflush ();  
       }  
     fclose(fin);  
     return 0;  
   }  
     
   return -1;  
199  }  }
200    
201  int  int display_file(const char *filename)
 display_file_ex (const char *filename, int begin_line, int wait)  
202  {  {
203    char buffer[260], temp[256];          char buffer[LINE_BUFFER_LEN];
204    int i, ch, input_ok;          FILE *fin;
205    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;  
206    
207    clrline (begin_line, screen_lines);          if ((fin = fopen(filename, "r")) == NULL)
208    line = begin_line;          {
209    moveto (line, 0);                  return -1;
210            }
211    
212    if ((fin = fopen (filename, "r")) != NULL)          while (fgets(buffer, sizeof(buffer) - 1, fin))
     {  
       if (fstat (fileno (fin), &f_stat) != 0)  
213          {          {
214            log_error ("Get file stat failed\n");                  i = strnlen(buffer, sizeof(buffer) - 1);
215            return -1;                  if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
216                    {
217                            buffer[i - 1] = '\r';
218                            buffer[i] = '\n';
219                            buffer[i + 1] = '\0';
220                    }
221                    prints(buffer);
222                    iflush();
223          }          }
224        f_size = f_stat.st_size;          fclose(fin);
225    
226        while (fgets (buffer, 255, fin))          return 0;
227          c_line_total ++;  }
228        rewind (fin);  
229    int display_file_ex(const char *filename, int begin_line, int wait)
230    {
231            char buffer[LINE_BUFFER_LEN];
232            char temp[LINE_BUFFER_LEN];
233            int ch = 0;
234            int input_ok, line, max_lines;
235            long int c_line_current = 0, c_line_total = 0;
236            FILE *fin;
237            struct stat f_stat;
238            long *p_line_offsets;
239            int len;
240            int percentile;
241    
242        while (fgets (buffer, 255, fin))          if ((fin = fopen(filename, "r")) == NULL)
243          {          {
244            if (line >= screen_lines)                  log_error("Unable to open file %s\n", filename);
245              {                  return -1;
246                f_offset = ftell (fin);          }
247    
248                moveto (screen_lines, 0);          p_line_offsets = (long *)malloc(sizeof(long) * MAX_FILE_LINES);
               prints  
                 ("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",  
                  (f_offset-strlen(buffer)) * 100 / f_size);  
               iflush ();  
249    
250                input_ok = 0;          c_line_total = split_file_lines(fin, screen_cols, p_line_offsets, MAX_FILE_LINES);
251                while (!input_ok)  
252            clrline(begin_line, screen_rows);
253            line = begin_line;
254            max_lines = screen_rows - 1;
255    
256            while (c_line_current < c_line_total)
257            {
258                    if (line >= max_lines)
259                  {                  {
260                    ch = igetch ();                          percentile = (c_line_current - (line - 1) + (screen_rows - 2)) * 100 / c_line_total;
261                    input_ok = 1;  
262                    switch (ch)                          moveto(screen_rows, 0);
263                      {                          prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
264                      case KEY_UP:                                     percentile);
265                        c_line_begin--;                          iflush();
266                        if (c_line_begin >= 0)  
267                          {                          input_ok = 0;
268                            rewind (fin);                          while (!input_ok)
                           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 -= (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++)  
                         {  
                           if (fgets (buffer, 255, fin) == NULL)  
                             goto exit;  
                         }  
                       break;  
                     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++)  
                         {  
                           if (fgets (buffer, 255, fin) == NULL)  
                             goto exit;  
                         }  
                       break;  
                     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++)  
269                          {                          {
270                            if (fgets (buffer, 255, fin) == NULL)                                  ch = igetch_t(MAX_DELAY_TIME);
271                              goto exit;                                  input_ok = 1;
272                                    switch (ch)
273                                    {
274                                    case KEY_UP:
275                                            if (c_line_current - line < 0) // Reach top
276                                            {
277                                                    break;
278                                            }
279                                            c_line_current -= line;
280                                            line = begin_line;
281                                            max_lines = begin_line + 1;
282                                            prints("\033[1T"); // Scroll down 1 line
283                                            break;
284                                    case KEY_DOWN:
285                                    case CR:
286                                            if (c_line_current + ((screen_rows - 2) - (line - 1)) >= c_line_total) // Reach bottom
287                                            {
288                                                    break;
289                                            }
290                                            c_line_current += ((screen_rows - 2) - (line - 1));
291                                            line = screen_rows - 2;
292                                            max_lines = screen_rows - 1;
293                                            moveto(screen_rows, 0);
294                                            clrtoeol();
295                                            prints("\033[1S"); // Scroll up 1 line
296                                            break;
297                                    case KEY_PGUP:
298                                    case Ctrl('B'):
299                                            if (c_line_current - line < 0) // Reach top
300                                            {
301                                                    break;
302                                            }
303                                            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;
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                                    }
355                          }                          }
                       break;  
                     default:  
                       input_ok = 0;  
                       break;  
                     }  
                 }  
356    
357                clrline (begin_line, screen_lines);                          continue;
358                line = begin_line;                  }
               moveto (line, 0);  
359    
360                continue;                  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 (fgets(buffer, len + 1, fin) == NULL)
368                    {
369                            log_error("Reach EOF\n");
370                            break;
371                    }
372                    moveto(line, 0);
373                    clrtoeol();
374                    prints("%s", buffer);
375                    c_line_current++;
376                    line++;
377            }
378    
379            i = strlen (buffer);          iflush();
           if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')  
             {  
               buffer[i - 1] = '\r';  
               buffer[i] = '\n';  
               buffer[i + 1] = '\0';  
             }  
           prints (buffer);  
           iflush ();  
380    
381            line++;          if (wait)
382            {
383                    ch = press_any_key();
384          }          }
       if (wait)  
         ch = press_any_key();  
       else  
         ch = 0;  
385    
386      exit:          free(p_line_offsets);
387        fclose (fin);          fclose(fin);
388    
389        return ch;          return ch;
390      }  }
391    
392    int show_top(char *status)
393    {
394            char buffer[LINE_BUFFER_LEN];
395    
396            str_space(buffer, 20 - strlen(BBS_current_section_name));
397    
398    return -1;          moveto(1, 0);
399            clrtoeol();
400            prints("\033[1;44;33m%-20s \033[37m%20s"
401                       "         %s\033[33m讨论区 [%s]\033[m",
402                       status, BBS_name, buffer, BBS_current_section_name);
403            iflush();
404    
405            return 0;
406  }  }
407    
408  int  int show_bottom(char *msg)
 show_top ()  
409  {  {
410    return 0;          char str_time[LINE_BUFFER_LEN];
411            char str_time_onine[20];
412            char buffer[LINE_BUFFER_LEN];
413            time_t time_online;
414            struct tm *tm_online;
415    
416            get_time_str(str_time, sizeof(str_time));
417            str_space(buffer, 33 - strlen(BBS_username));
418    
419            time_online = time(0) - BBS_login_tm;
420            tm_online = gmtime(&time_online);
421    
422            moveto(screen_rows, 0);
423            clrtoeol();
424            prints("\033[1;44;33m[\033[36m%s\033[33m]"
425                       "%s帐号[\033[36m%s\033[33m]"
426                       "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",
427                       str_time, buffer, BBS_username, tm_online->tm_mday - 1,
428                       tm_online->tm_hour, tm_online->tm_min);
429            iflush();
430    
431            return 0;
432  }  }
433    
434  int  int show_active_board()
 show_bottom ()  
435  {  {
436    return 0;          char filename[FILE_PATH_LEN];
437            char buffer[LINE_BUFFER_LEN];
438            FILE *fin;
439            static int line;
440            int len;
441            int end_of_line;
442    
443            sprintf(filename, "%sdata/active_board.txt", app_home_dir);
444    
445            clrline(3, 2 + ACTIVE_BOARD_HEIGHT);
446    
447            if ((fin = fopen(filename, "r")) == NULL)
448            {
449                    log_error("Unable to open file %s\n", filename);
450                    return -1;
451            }
452    
453            for (int i = 0; i < line; i++)
454            {
455                    if (fgets(buffer, sizeof(buffer), fin) == NULL)
456                    {
457                            line = 0;
458                            rewind(fin);
459                            break;
460                    }
461            }
462    
463            for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
464            {
465                    if (fgets(buffer, sizeof(buffer), fin) == NULL)
466                    {
467                            line = 0;
468                            break;
469                    }
470                    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;
481  }  }


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

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