/[LeafOK_CVS]/lbbs/src/screen.c
ViewVC logotype

Diff of /lbbs/src/screen.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.6 by sysadm, Sat Oct 23 18:41:41 2004 UTC Revision 1.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 15  Line 15 
15   *                                                                         *   *                                                                         *
16   ***************************************************************************/   ***************************************************************************/
17    
18    #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
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  int  int press_any_key()
 str_input (char *buffer, int buffer_length, int echo_mode)  
79  {  {
80    char buf[256], ch;          moveto(screen_lines, 0);
81    int c, offset = 0, len, loop = 1, i, hz = 0;          clrtoeol();
82    
83    memset (buffer, '\0', buffer_length);          prints("                           \033[1;33m按任意键继续...\033[0;37m");
84            iflush();
85    
86    while (c = igetch ())          return igetch_t(60);
87      {  }
88        if (c == CR || c == LF)  
89          break;  void set_input_echo(int echo)
90        if (c == BACKSPACE)  {
91          {          if (echo)
           if (offset > 0)  
             {  
               buffer[--offset] = '\0';  
               prints ("\b \b");  
 //            clrtoeol ();  
               iflush ();  
             }  
           continue;  
         }  
       if (c > 255 || iscntrl (c))  
         {  
           continue;  
         }  
       if (c > 127 && c <= 255)  
92          {          {
93            hz = (!hz);                  outc('\x83'); // ASCII code 131
94                    iflush();
95          }          }
96        if (offset >= buffer_length)          else
97          {          {
98            outc ('\a');                  //    outc ('\x85'); // ASCII code 133
99            continue;                  prints("\xff\xfb\x01\xff\xfb\x03");
100                    iflush();
101                    igetch_t(60);
102                    igetch_t(60);
103          }          }
104        buffer[offset++] = (char) c;  }
105        buffer[offset] = '\0';  
106        switch (echo_mode)  static int _str_input(char *buffer, int buffer_length, int echo_mode)
107    {
108            char buf[256], ch;
109            int c, offset = 0, len, loop = 1, i, hz = 0;
110    
111            for (i = 0; i < buffer_length && buffer[i] != '\0'; i++)
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;
176    FILE *fin;  
177    int i;          memset(buffer, '\0', buffer_length);
178    
179            offset = _str_input(buffer, buffer_length, echo_mode);
180    
181            return offset;
182    };
183    
184    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    if ((fin = fopen (filename, "r")) != NULL)          len = _str_input(buffer, buffer_length, echo_mode);
195    {  
196      while (fgets (buffer, 255, fin))          return len;
       {  
         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 ();  
       }  
     fclose(fin);  
     return 0;  
   }  
     
   return -1;  
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  
257                          {                          {
258                            goto exit;                                  f_offset = ftell(fin);
259                          }  
260                        break;                                  moveto(screen_lines, 0);
261                      case KEY_DOWN:                                  prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
262                      case CR:                                             (f_offset - strlen(buffer)) * 100 / f_size);
263                        c_line_begin++;                                  iflush();
264                        rewind (fin);  
265                        for (f_line = 0; f_line < c_line_begin; f_line++)                                  input_ok = 0;
266                          {                                  while (!input_ok)
267                            if (fgets (buffer, 255, fin) == NULL)                                  {
268                              goto exit;                                          ch = igetch_t(MAX_DELAY_TIME);
269                          }                                          input_ok = 1;
270                        break;                                          switch (ch)
271                      case KEY_PGUP:                                          {
272                      case Ctrl ('B'):                                          case KEY_UP:
273                        if (c_line_begin > 0)                                                  c_line_begin--;
274                          c_line_begin -= (screen_lines - begin_line - 1);                                                  if (c_line_begin >= 0)
275                        else                                                  {
276                          goto exit;                                                          rewind(fin);
277                        if (c_line_begin < 0)                                                          for (f_line = 0; f_line < c_line_begin; f_line++)
278                          c_line_begin = 0;                                                          {
279                        rewind (fin);                                                                  if (fgets(buffer, 255, fin) == NULL)
280                        for (f_line = 0; f_line < c_line_begin; f_line++)                                                                          goto exit;
281                          {                                                          }
282                            if (fgets (buffer, 255, fin) == NULL)                                                  }
283                              goto exit;                                                  else
284                          }                                                  {
285                        break;                                                          goto exit;
286                      case KEY_RIGHT:                                                  }
287                      case KEY_PGDOWN:                                                  break;
288                      case Ctrl ('F'):                                          case KEY_DOWN:
289                      case KEY_SPACE:                                          case CR:
290                        c_line_begin += (screen_lines - begin_line - 1);                                                  c_line_begin++;
291                        if (c_line_begin + (screen_lines - begin_line) > c_line_total)                                                  rewind(fin);
292                          c_line_begin = c_line_total - (screen_lines - begin_line);                                                  for (f_line = 0; f_line < c_line_begin; f_line++)
293                        rewind (fin);                                                  {
294                        for (f_line = 0; f_line < c_line_begin; f_line++)                                                          if (fgets(buffer, 255, fin) == NULL)
295                          {                                                                  goto exit;
296                            if (fgets (buffer, 255, fin) == NULL)                                                  }
297                              goto exit;                                                  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();
374                        input_ok = 0;  
375                        break;                          line++;
                     }  
376                  }                  }
377                    if (wait)
378                            ch = press_any_key();
379                    else
380                            ch = 0;
381    
382                clrline (begin_line, screen_lines);          exit:
383                line = begin_line;                  fclose(fin);
               moveto (line, 0);  
384    
385                continue;                  return ch;
386              }          }
387    
388            i = strlen (buffer);          return -1;
389            if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')  }
             {  
               buffer[i - 1] = '\r';  
               buffer[i] = '\n';  
               buffer[i + 1] = '\0';  
             }  
           prints (buffer);  
           iflush ();  
390    
391            line++;  int show_top(char *status)
392          }  {
393        if (wait)          char buffer[256];
         ch = press_any_key();  
       else  
         ch = 0;  
394    
395      exit:          str_space(buffer, 20 - strlen(BBS_current_section_name));
       fclose (fin);  
396    
397        return ch;          moveto(1, 0);
398      }          prints("\033[1;44;33m%-20s \033[37m%20s"
399                       "         %s\033[33m讨论区 [%s]\033[m",
400                       status, BBS_name, buffer, BBS_current_section_name);
401            iflush();
402    
403    return -1;          return 0;
404  }  }
405    
406  int  int show_bottom(char *msg)
 show_top ()  
407  {  {
408    return 0;          char str_time[256], str_time_onine[20], buffer[256];
409            time_t time_online;
410            struct tm *tm_online;
411    
412            get_time_str(str_time, 256);
413            str_space(buffer, 33 - strlen(BBS_username));
414    
415            time_online = time(0) - BBS_login_tm;
416            tm_online = gmtime(&time_online);
417    
418            moveto(screen_lines, 0);
419            prints("\033[1;44;33m[\033[36m%s\033[33m]"
420                       "%s帐号[\033[36m%s\033[33m]"
421                       "[\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,
423                       tm_online->tm_hour, tm_online->tm_min);
424            iflush();
425    
426            return 0;
427  }  }
428    
429  int  int show_active_board()
 show_bottom ()  
430  {  {
431    return 0;          char filename[256], buffer[260];
432            FILE *fin;
433            int i, j;
434            static long int line;
435    
436            sprintf(filename, "%sdata/active_board.txt", app_home_dir);
437    
438            clrline(3, 2 + ACTIVE_BOARD_HEIGHT);
439    
440            moveto(3, 0);
441    
442            if ((fin = fopen(filename, "r")) != NULL)
443            {
444                    for (j = 0; j < line; j++)
445                    {
446                            if (fgets(buffer, 255, fin) == NULL)
447                            {
448                                    line = 0;
449                                    rewind(fin);
450                                    break;
451                            }
452                    }
453    
454                    for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++)
455                    {
456                            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);
484            }
485    
486            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