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


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

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