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


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

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