/[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.12 by sysadm, Mon Mar 21 17:08:21 2005 UTC Revision 1.24 by sysadm, Wed Apr 30 09:18:20 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 "log.h"
21  #include "io.h"  #include "io.h"
22    #include "screen.h"
23    #include <string.h>
24    #include <ctype.h>
25  #include <sys/types.h>  #include <sys/types.h>
26  #include <sys/stat.h>  #include <sys/stat.h>
27  #include <unistd.h>  #include <unistd.h>
28    
29  #define ACTIVE_BOARD_HEIGHT     8  #define ACTIVE_BOARD_HEIGHT 8
30    
31  int screen_lines = 24;  int screen_lines = 24;
32    
33  void  void moveto(int row, int col)
 moveto (int row, int col)  
34  {  {
35    if (row >= 0)          if (row >= 0)
36      {          {
37        prints ("\033[%d;%dH", row, col);                  prints("\033[%d;%dH", row, col);
38      }          }
39    else          else
40      {          {
41        prints ("\r");                  prints("\r");
42      }          }
43    iflush ();          iflush();
44  }  }
45    
46  void  void clrtoeol()
 clrtoeol ()  
47  {  {
48    prints ("\033[K");          prints("\033[K");
49    iflush ();          iflush();
50  }  }
51    
52  void  void clrline(int line_begin, int line_end)
 clrline (int line_begin, int line_end)  
53  {  {
54    int i;          int i;
55    
56    for (i = line_begin; i <= line_end; i++)          for (i = line_begin; i <= line_end; i++)
57      {          {
58        moveto (i, 0);                  moveto(i, 0);
59        prints ("\033[K");                  prints("\033[K");
60      }          }
61    
62    iflush ();          iflush();
63  }  }
64    
65  void  void clrtobot(int line_begin)
 clearscr ()  
66  {  {
67    prints ("\33[2J");          clrline(line_begin, screen_lines);
68    moveto (0, 0);          moveto(line_begin, 0);
   iflush ();  
69  }  }
70    
71  int  void clearscr()
 press_any_key ()  
72  {  {
73    moveto (screen_lines, 0);          prints("\033[2J");
74    clrtoeol ();          moveto(0, 0);
75            iflush();
   prints ("                           \033[1;33m按任意键继续...\033[0;37m");  
   iflush ();  
   
   return igetch ();  
76  }  }
77    
78  void  int press_any_key()
 set_input_echo (int echo)  
79  {  {
80    char temp[256];          moveto(screen_lines, 0);
81            clrtoeol();
82    
83            prints("                           \033[1;33m按任意键继续...\033[0;37m");
84            iflush();
85    
86    if (echo)          return igetch_t(60);
     {  
       outc ('\x83');            // ASCII code 131  
       iflush ();  
     }  
   else  
     {  
 //    outc ('\x85'); // ASCII code 133  
       prints ("\xff\xfb\x01\xff\xfb\x03");  
       iflush ();  
       igetch ();  
       igetch ();  
     }  
87  }  }
88    
89  int  void set_input_echo(int echo)
 str_input (char *buffer, int buffer_length, int echo_mode)  
90  {  {
91    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)  
         break;  
       if (c == LF)  
         continue;  
       if (c == BACKSPACE)  
92          {          {
93            if (offset > 0)                  outc('\x83'); // ASCII code 131
94              {                  iflush();
               buffer[--offset] = '\0';  
               prints ("\b \b");  
 //            clrtoeol ();  
               iflush ();  
             }  
           continue;  
95          }          }
96        if (c > 255 || iscntrl (c))          else
97          {          {
98            continue;                  //    outc ('\x85'); // ASCII code 133
99                    prints("\xff\xfb\x01\xff\xfb\x03");
100                    iflush();
101                    igetch_t(60);
102                    igetch_t(60);
103          }          }
104        if (c > 127 && c <= 255)  }
105          {  
106            hz = (!hz);  static int _str_input(char *buffer, int buffer_length, int echo_mode)
107          }  {
108        if (offset >= buffer_length)          char buf[256], ch;
109          {          int c, offset = 0, len, loop = 1, i, hz = 0;
110            outc ('\a');  
111            continue;          for (i = 0; i < buffer_length && buffer[i] != '\0'; i++)
         }  
       buffer[offset++] = (char) c;  
       buffer[offset] = '\0';  
       switch (echo_mode)  
112          {          {
113          case 0:                  offset++;
           outc ((char) c);  
           break;  
         case 1:  
           outc ('*');  
           break;  
114          }          }
115        if (!hz)  
116            while (c = igetch_t(60))
117          {          {
118            iflush ();                  if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR)
119                    {
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;
   FILE *fin;  
   int i;  
176    
177    if ((fin = fopen (filename, "r")) == NULL)          memset(buffer, '\0', buffer_length);
     {  
       return -1;  
     }  
178    
179    while (fgets (buffer, 255, fin))          offset = _str_input(buffer, buffer_length, echo_mode);
180      {  
181        i = strlen (buffer);          return offset;
182        if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')  };
         {  
           buffer[i - 1] = '\r';  
           buffer[i] = '\n';  
           buffer[i + 1] = '\0';  
         }  
       prints (buffer);  
       iflush ();  
     }  
   fclose (fin);  
183    
184    return 0;  int get_data(int row, int col, char *prompt, char *buffer, int buffer_length, int echo_mode)
185    {
186            int len;
187    
188            moveto(row, col);
189            iflush();
190            prints(prompt);
191            prints(buffer);
192            iflush();
193    
194            len = _str_input(buffer, buffer_length, echo_mode);
195    
196            return len;
197  }  }
198    
199  int  int display_file(const char *filename)
 display_file_ex (const char *filename, int begin_line, int wait)  
200  {  {
201    char buffer[260], temp[256];          char buffer[260];
202    int i, ch, input_ok;          FILE *fin;
203    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;  
204    
205    clrline (begin_line, screen_lines);          if ((fin = fopen(filename, "r")) == NULL)
206    line = begin_line;          {
207    moveto (line, 0);                  return -1;
208            }
209    
210    if ((fin = fopen (filename, "r")) != NULL)          while (fgets(buffer, 255, fin))
     {  
       if (fstat (fileno (fin), &f_stat) != 0)  
211          {          {
212            log_error ("Get file stat failed\n");                  i = strlen(buffer);
213            return -1;                  if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
214                    {
215                            buffer[i - 1] = '\r';
216                            buffer[i] = '\n';
217                            buffer[i + 1] = '\0';
218                    }
219                    prints(buffer);
220                    iflush();
221          }          }
222        f_size = f_stat.st_size;          fclose(fin);
223    
224            return 0;
225    }
226    
227        while (fgets (buffer, 255, fin))  int display_file_ex(const char *filename, int begin_line, int wait)
228          c_line_total++;  {
229        rewind (fin);          char buffer[260], temp[256];
230            int i, ch, input_ok, max_lines;
231            long int line, c_line_begin = 0, c_line_total = 0;
232            long int f_line, f_size, f_offset;
233            FILE *fin;
234            struct stat f_stat;
235    
236            max_lines = screen_lines - 1;
237            clrline(begin_line, screen_lines);
238            line = begin_line;
239            moveto(line, 0);
240    
241        while (fgets (buffer, 255, fin))          if ((fin = fopen(filename, "r")) != NULL)
242          {          {
243            if (line >= screen_lines)                  if (fstat(fileno(fin), &f_stat) != 0)
244              {                  {
245                f_offset = ftell (fin);                          log_error("Get file stat failed\n");
246                            return -1;
247                    }
248                    f_size = f_stat.st_size;
249    
250                moveto (screen_lines, 0);                  while (fgets(buffer, 255, fin))
251                prints                          c_line_total++;
252                  ("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",                  rewind(fin);
                  (f_offset - strlen (buffer)) * 100 / f_size);  
               iflush ();  
253    
254                input_ok = 0;                  while (fgets(buffer, 255, fin))
               while (!input_ok)  
255                  {                  {
256                    ch = igetch ();                          if (line >= max_lines)
                   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++)  
                         {  
                           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++)  
257                          {                          {
258                            if (fgets (buffer, 255, fin) == NULL)                                  f_offset = ftell(fin);
259                              goto exit;  
260                          }                                  moveto(screen_lines, 0);
261                        break;                                  prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
262                      case KEY_RIGHT:                                             (f_offset - strlen(buffer)) * 100 / f_size);
263                      case KEY_PGDN:                                  iflush();
264                      case Ctrl ('F'):  
265                      case KEY_SPACE:                                  input_ok = 0;
266                        c_line_begin += (screen_lines - begin_line - 1);                                  while (!input_ok)
267                        if (c_line_begin + (screen_lines - begin_line) >                                  {
268                            c_line_total)                                          ch = igetch_t(MAX_DELAY_TIME);
269                          c_line_begin =                                          input_ok = 1;
270                            c_line_total - (screen_lines - begin_line);                                          switch (ch)
271                        rewind (fin);                                          {
272                        for (f_line = 0; f_line < c_line_begin; f_line++)                                          case KEY_UP:
273                          {                                                  c_line_begin--;
274                            if (fgets (buffer, 255, fin) == NULL)                                                  if (c_line_begin >= 0)
275                              goto exit;                                                  {
276                                                            rewind(fin);
277                                                            for (f_line = 0; f_line < c_line_begin; f_line++)
278                                                            {
279                                                                    if (fgets(buffer, 255, fin) == NULL)
280                                                                            goto exit;
281                                                            }
282                                                    }
283                                                    else
284                                                    {
285                                                            goto exit;
286                                                    }
287                                                    break;
288                                            case KEY_DOWN:
289                                            case CR:
290                                                    c_line_begin++;
291                                                    rewind(fin);
292                                                    for (f_line = 0; f_line < c_line_begin; f_line++)
293                                                    {
294                                                            if (fgets(buffer, 255, fin) == NULL)
295                                                                    goto exit;
296                                                    }
297                                                    break;
298                                            case KEY_PGUP:
299                                            case Ctrl('B'):
300                                                    if (c_line_begin > 0)
301                                                            c_line_begin -= (max_lines - begin_line - 1);
302                                                    else
303                                                            goto exit;
304                                                    if (c_line_begin < 0)
305                                                            c_line_begin = 0;
306                                                    rewind(fin);
307                                                    for (f_line = 0; f_line < c_line_begin; f_line++)
308                                                    {
309                                                            if (fgets(buffer, 255, fin) == NULL)
310                                                                    goto exit;
311                                                    }
312                                                    break;
313                                            case KEY_RIGHT:
314                                            case KEY_PGDN:
315                                            case Ctrl('F'):
316                                            case KEY_SPACE:
317                                                    c_line_begin += (max_lines - begin_line - 1);
318                                                    if (c_line_begin + (max_lines - begin_line) >
319                                                            c_line_total)
320                                                            c_line_begin =
321                                                                    c_line_total - (max_lines - begin_line);
322                                                    rewind(fin);
323                                                    for (f_line = 0; f_line < c_line_begin; f_line++)
324                                                    {
325                                                            if (fgets(buffer, 255, fin) == NULL)
326                                                                    goto exit;
327                                                    }
328                                                    break;
329                                            case KEY_NULL:
330                                            case KEY_TIMEOUT:
331                                            case KEY_LEFT:
332                                            case 'q':
333                                            case 'Q':
334                                                    goto exit;
335                                                    break;
336                                            case '?':
337                                            case 'h':
338                                            case 'H':
339                                                    // Display help information
340                                                    strcpy(temp, app_home_dir);
341                                                    strcat(temp, "data/read_help.txt");
342                                                    display_file_ex(temp, begin_line, 1);
343    
344                                                    // Refresh after display help information
345                                                    rewind(fin);
346                                                    for (f_line = 0; f_line < c_line_begin; f_line++)
347                                                    {
348                                                            if (fgets(buffer, 255, fin) == NULL)
349                                                                    goto exit;
350                                                    }
351                                                    break;
352                                            default:
353                                                    input_ok = 0;
354                                                    break;
355                                            }
356                                    }
357    
358                                    clrline(begin_line, screen_lines);
359                                    line = begin_line;
360                                    moveto(line, 0);
361    
362                                    continue;
363                          }                          }
364                        break;  
365                      case KEY_LEFT:                          i = strlen(buffer);
366                      case 'q':                          if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
                     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++)  
367                          {                          {
368                            if (fgets (buffer, 255, fin) == NULL)                                  buffer[i - 1] = '\r';
369                              goto exit;                                  buffer[i] = '\n';
370                                    buffer[i + 1] = '\0';
371                          }                          }
372                        break;                          prints(buffer);
373                      default:                          iflush();
                       input_ok = 0;  
                       break;  
                     }  
                 }  
   
               clrline (begin_line, screen_lines);  
               line = begin_line;  
               moveto (line, 0);  
374    
375                continue;                          line++;
376              }                  }
377                    if (wait)
378                            ch = press_any_key();
379                    else
380                            ch = 0;
381    
382            i = strlen (buffer);          exit:
383            if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')                  fclose(fin);
             {  
               buffer[i - 1] = '\r';  
               buffer[i] = '\n';  
               buffer[i + 1] = '\0';  
             }  
           prints (buffer);  
           iflush ();  
384    
385            line++;                  return ch;
386          }          }
       if (wait)  
         ch = press_any_key ();  
       else  
         ch = 0;  
   
     exit:  
       fclose (fin);  
   
       return ch;  
     }  
387    
388    return -1;          return -1;
389  }  }
390    
391  int  int show_top(char *status)
 show_top (char *status)  
392  {  {
393    char buffer[256];          char buffer[256];
394    
395    str_space (buffer, 20 - strlen (BBS_current_section_name));          str_space(buffer, 20 - strlen(BBS_current_section_name));
396    
397    moveto (1, 0);          moveto(1, 0);
398    prints ("\033[1;44;33m%-20s \033[37m%20s"          prints("\033[1;44;33m%-20s \033[37m%20s"
399            "         %s\033[33m讨论区 [%s]\033[m",                     "         %s\033[33m讨论区 [%s]\033[m",
400            status, BBS_name, buffer, BBS_current_section_name);                     status, BBS_name, buffer, BBS_current_section_name);
401    iflush ();          iflush();
402    
403    return 0;          return 0;
404  }  }
405    
406  int  int show_bottom(char *msg)
 show_bottom (char *msg)  
407  {  {
408    char str_time[256], str_time_onine[20], buffer[256];          char str_time[256], str_time_onine[20], buffer[256];
409    time_t time_online;          time_t time_online;
410    struct tm *tm_online;          struct tm *tm_online;
411    
412    get_time_str (str_time, 256);          get_time_str(str_time, 256);
413    str_space (buffer, 33 - strlen (BBS_username));          str_space(buffer, 33 - strlen(BBS_username));
414    
415    time_online = time (0) - BBS_login_tm;          time_online = time(0) - BBS_login_tm;
416    tm_online = gmtime (&time_online);          tm_online = gmtime(&time_online);
417    
418    moveto (screen_lines, 0);          moveto(screen_lines, 0);
419    prints ("\033[1;44;33m[\033[36m%s\033[33m]"          prints("\033[1;44;33m[\033[36m%s\033[33m]"
420            "%s帐号[\033[36m%s\033[33m]"                     "%s帐号[\033[36m%s\033[33m]"
421            "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",                     "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",
422            str_time, buffer, BBS_username, tm_online->tm_mday - 1,                     str_time, buffer, BBS_username, tm_online->tm_mday - 1,
423            tm_online->tm_hour, tm_online->tm_min);                     tm_online->tm_hour, tm_online->tm_min);
424    iflush ();          iflush();
425    
426    return 0;          return 0;
427  }  }
428    
429  int  int show_active_board()
 show_active_board ()  
430  {  {
431    char filename[256], buffer[260];          char filename[256], buffer[260];
432    FILE *fin;          FILE *fin;
433    int i, j;          int i, j;
434    static long int line;          static long int line;
435    
436    sprintf (filename, "%sdata/active_board.txt", app_home_dir);          sprintf(filename, "%sdata/active_board.txt", app_home_dir);
437    
438    clrline (3, 2 + ACTIVE_BOARD_HEIGHT);          clrline(3, 2 + ACTIVE_BOARD_HEIGHT);
439    
440    moveto (3, 0);          moveto(3, 0);
441    
442    if ((fin = fopen (filename, "r")) != NULL)          if ((fin = fopen(filename, "r")) != NULL)
     {  
       for (j = 0; j < line; j++)  
443          {          {
444            if (fgets (buffer, 255, fin) == NULL)                  for (j = 0; j < line; j++)
             {  
               line = 0;  
               rewind (fin);  
               break;  
             }  
         }  
   
       for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++)  
         {  
           if (fgets (buffer, 255, fin) == NULL)  
             {  
               line = 0;  
               if (j == 0)  
445                  {                  {
446                    rewind (fin);                          if (fgets(buffer, 255, fin) == NULL)
447                    if (fgets (buffer, 255, fin) == NULL)                          {
448                      {                                  line = 0;
449                        break;                                  rewind(fin);
450                      }                                  break;
451                            }
452                  }                  }
453                else  
454                    for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++)
455                  {                  {
456                    break;                          if (fgets(buffer, 255, fin) == NULL)
457                            {
458                                    line = 0;
459                                    if (j == 0)
460                                    {
461                                            rewind(fin);
462                                            if (fgets(buffer, 255, fin) == NULL)
463                                            {
464                                                    break;
465                                            }
466                                    }
467                                    else
468                                    {
469                                            break;
470                                    }
471                            }
472                            line++;
473                            i = strlen(buffer);
474                            if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
475                            {
476                                    buffer[i - 1] = '\r';
477                                    buffer[i] = '\n';
478                                    buffer[i + 1] = '\0';
479                            }
480                            prints(buffer);
481                            iflush();
482                  }                  }
483              }                  fclose(fin);
           line++;  
           i = strlen (buffer);  
           if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')  
             {  
               buffer[i - 1] = '\r';  
               buffer[i] = '\n';  
               buffer[i + 1] = '\0';  
             }  
           prints (buffer);  
           iflush ();  
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