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


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

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