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


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

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