/[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.1 by sysadm, Fri Oct 22 15:21:28 2004 UTC Revision 1.20 by sysadm, Tue Jan 3 12:34:54 2006 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 ();
41  }  }
42    
43  void  void
44  clrtoeol ()  clrtoeol ()
45  {  {
46    prints ("\033[K");    prints ("\033[K");
47      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
65    clrtobot (int line_begin)
66    {
67      clrline (line_begin, screen_lines);
68      moveto (line_begin, 0);
69    }
70    
71    void
72    clearscr ()
73    {
74      prints ("\33[2J");
75      moveto (0, 0);
76      iflush ();
77  }  }
78    
79  int  int
80  str_input (char *buffer, int buffer_length, int echo_mode)  press_any_key ()
81    {
82      moveto (screen_lines, 0);
83      clrtoeol ();
84    
85      prints ("                           \033[1;33m按任意键继续...\033[0;37m");
86      iflush ();
87    
88      return igetch_t (60);
89    }
90    
91    void
92    set_input_echo (int echo)
93    {
94      if (echo)
95        {
96          outc ('\x83');            // ASCII code 131
97          iflush ();
98        }
99      else
100        {
101    //    outc ('\x85'); // ASCII code 133
102          prints ("\xff\xfb\x01\xff\xfb\x03");
103          iflush ();
104          igetch_t (60);
105          igetch_t (60);
106        }
107    }
108    
109    int
110    _str_input (char *buffer, int buffer_length, int echo_mode)
111  {  {
112    char buf[256], ch;    char buf[256], ch;
113    int c, offset = 0, len, loop = 1, i, hz = 0;    int c, offset = 0, len, loop = 1, i, hz = 0;
114    
115    memset (buffer, '\0', buffer_length);    for (i=0; i<buffer_length && buffer[i] != '\0'; i++)
116        {
117              offset++;
118        }
119    
120    while (c = igetch ())    while (c = igetch_t (60))
121      {      {
122        if (c == CR || c == LF)        if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR)
123          break;          break;
124          if (c == LF)
125            continue;
126        if (c == BACKSPACE)        if (c == BACKSPACE)
127          {          {
128            if (offset > 0)            if (offset > 0)
# Line 64  str_input (char *buffer, int buffer_leng Line 139  str_input (char *buffer, int buffer_leng
139            continue;            continue;
140          }          }
141        if (c > 127 && c <= 255)        if (c > 127 && c <= 255)
142          {          {
143            hz = (!hz);            hz = (!hz);
144          }          }
145        if (offset >= buffer_length)        if (offset >= buffer_length)
146          {          {
147            outc ('\a');            outc ('\a');
# Line 76  str_input (char *buffer, int buffer_leng Line 151  str_input (char *buffer, int buffer_leng
151        buffer[offset] = '\0';        buffer[offset] = '\0';
152        switch (echo_mode)        switch (echo_mode)
153          {          {
154          case 0:          case DOECHO:
155            outc ((char) c);            outc ((char) c);
156            break;            break;
157          case 1:          case NOECHO:
158            outc ('*');            outc ('*');
159            break;            break;
160          }          }
161        if (!hz)        if (!hz)
162          {          {
163            iflush ();            iflush ();
164          }          }
165      }      }
166    
167    prints ("\r\n");    prints ("\r\n");
# Line 96  str_input (char *buffer, int buffer_leng Line 171  str_input (char *buffer, int buffer_leng
171  }  }
172    
173  int  int
174  display_file(const char* filename)  str_input (char *buffer, int buffer_length, int echo_mode)
175  {  {
176    char buffer[256];    int offset;
   FILE *fin;  
177        
178    if ((fin = fopen(filename, "r")) != NULL)    memset (buffer, '\0', buffer_length);
179    {    
180      while (fgets(buffer, 255, fin))    offset = _str_input (buffer, buffer_length, echo_mode);
181      
182      return offset;
183    };
184    
185    int
186    get_data (int row, int col, char *prompt, char *buffer, int buffer_length, int echo_mode)
187    {
188      int len;
189    
190      moveto (row, col);
191      iflush ();
192      prints (prompt);
193      prints (buffer);
194      iflush ();
195    
196      len = _str_input (buffer, buffer_length, echo_mode);
197      
198      return len;
199    }
200    
201    int
202    display_file (const char *filename)
203    {
204      char buffer[260];
205      FILE *fin;
206      int i;
207    
208      if ((fin = fopen (filename, "r")) == NULL)
209      {      {
210          return -1;
211        }
212    
213      while (fgets (buffer, 255, fin))
214        {
215          i = strlen (buffer);
216          if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
217            {
218              buffer[i - 1] = '\r';
219              buffer[i] = '\n';
220              buffer[i + 1] = '\0';
221            }
222        prints (buffer);        prints (buffer);
223        iflush ();        iflush ();
224      }      }
225      fclose (fin);    fclose (fin);
226    
227      return 0;
228    }
229    
230    int
231    display_file_ex (const char *filename, int begin_line, int wait)
232    {
233      char buffer[260], temp[256];
234      int i, ch, input_ok, max_lines;
235      long int line, c_line_begin = 0, c_line_total = 0;
236      long int f_line, f_size, f_offset;
237      FILE *fin;
238      struct stat f_stat;
239    
240      max_lines = screen_lines - 1;
241      clrline (begin_line, screen_lines);
242      line = begin_line;
243      moveto (line, 0);
244    
245      if ((fin = fopen (filename, "r")) != NULL)
246        {
247          if (fstat (fileno (fin), &f_stat) != 0)
248            {
249              log_error ("Get file stat failed\n");
250              return -1;
251            }
252          f_size = f_stat.st_size;
253    
254          while (fgets (buffer, 255, fin))
255            c_line_total++;
256          rewind (fin);
257    
258          while (fgets (buffer, 255, fin))
259            {
260              if (line >= max_lines)
261                {
262                  f_offset = ftell (fin);
263    
264                  moveto (screen_lines, 0);
265                  prints
266                    ("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
267                     (f_offset - strlen (buffer)) * 100 / f_size);
268                  iflush ();
269    
270                  input_ok = 0;
271                  while (!input_ok)
272                    {
273                      ch = igetch_t (MAX_DELAY_TIME);
274                      input_ok = 1;
275                      switch (ch)
276                        {
277                        case KEY_UP:
278                          c_line_begin--;
279                          if (c_line_begin >= 0)
280                            {
281                              rewind (fin);
282                              for (f_line = 0; f_line < c_line_begin; f_line++)
283                                {
284                                  if (fgets (buffer, 255, fin) == NULL)
285                                    goto exit;
286                                }
287                            }
288                          else
289                            {
290                              goto exit;
291                            }
292                          break;
293                        case KEY_DOWN:
294                        case CR:
295                          c_line_begin++;
296                          rewind (fin);
297                          for (f_line = 0; f_line < c_line_begin; f_line++)
298                            {
299                              if (fgets (buffer, 255, fin) == NULL)
300                                goto exit;
301                            }
302                          break;
303                        case KEY_PGUP:
304                        case Ctrl ('B'):
305                          if (c_line_begin > 0)
306                            c_line_begin -= (max_lines - begin_line - 1);
307                          else
308                            goto exit;
309                          if (c_line_begin < 0)
310                            c_line_begin = 0;
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                        case KEY_RIGHT:
319                        case KEY_PGDN:
320                        case Ctrl ('F'):
321                        case KEY_SPACE:
322                          c_line_begin += (max_lines - begin_line - 1);
323                          if (c_line_begin + (max_lines - begin_line) >
324                              c_line_total)
325                            c_line_begin =
326                              c_line_total - (max_lines - begin_line);
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                        case KEY_NULL:
335                        case KEY_TIMEOUT:
336                        case KEY_LEFT:
337                        case 'q':
338                        case 'Q':
339                          goto exit;
340                          break;
341                        case '?':
342                        case 'h':
343                        case 'H':
344                          //Display help information
345                          strcpy (temp, app_home_dir);
346                          strcat (temp, "data/read_help.txt");
347                          display_file_ex (temp, begin_line, 1);
348    
349                          //Refresh after display help information
350                          rewind (fin);
351                          for (f_line = 0; f_line < c_line_begin; f_line++)
352                            {
353                              if (fgets (buffer, 255, fin) == NULL)
354                                goto exit;
355                            }
356                          break;
357                        default:
358                          input_ok = 0;
359                          break;
360                        }
361                    }
362    
363                  clrline (begin_line, screen_lines);
364                  line = begin_line;
365                  moveto (line, 0);
366    
367                  continue;
368                }
369    
370              i = strlen (buffer);
371              if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
372                {
373                  buffer[i - 1] = '\r';
374                  buffer[i] = '\n';
375                  buffer[i + 1] = '\0';
376                }
377              prints (buffer);
378              iflush ();
379    
380              line++;
381            }
382          if (wait)
383            ch = press_any_key ();
384          else
385            ch = 0;
386    
387        exit:
388          fclose (fin);
389    
390          return ch;
391        }
392    
     return 0;  
   }  
     
393    return -1;    return -1;
394  }  }
395    
396  int  int
397  show_top()  show_top (char *status)
398  {  {
399      char buffer[256];
400    
401      str_space (buffer, 20 - strlen (BBS_current_section_name));
402    
403      moveto (1, 0);
404      prints ("\033[1;44;33m%-20s \033[37m%20s"
405              "         %s\033[33m讨论区 [%s]\033[m",
406              status, BBS_name, buffer, BBS_current_section_name);
407      iflush ();
408    
409    return 0;    return 0;
410  }  }
411    
412  int  int
413  show_bottom()  show_bottom (char *msg)
414  {  {
415      char str_time[256], str_time_onine[20], buffer[256];
416      time_t time_online;
417      struct tm *tm_online;
418    
419      get_time_str (str_time, 256);
420      str_space (buffer, 33 - strlen (BBS_username));
421    
422      time_online = time (0) - BBS_login_tm;
423      tm_online = gmtime (&time_online);
424    
425      moveto (screen_lines, 0);
426      prints ("\033[1;44;33m[\033[36m%s\033[33m]"
427              "%s帐号[\033[36m%s\033[33m]"
428              "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",
429              str_time, buffer, BBS_username, tm_online->tm_mday - 1,
430              tm_online->tm_hour, tm_online->tm_min);
431      iflush ();
432    
433      return 0;
434    }
435    
436    int
437    show_active_board ()
438    {
439      char filename[256], buffer[260];
440      FILE *fin;
441      int i, j;
442      static long int line;
443    
444      sprintf (filename, "%sdata/active_board.txt", app_home_dir);
445    
446      clrline (3, 2 + ACTIVE_BOARD_HEIGHT);
447    
448      moveto (3, 0);
449    
450      if ((fin = fopen (filename, "r")) != NULL)
451        {
452          for (j = 0; j < line; j++)
453            {
454              if (fgets (buffer, 255, fin) == NULL)
455                {
456                  line = 0;
457                  rewind (fin);
458                  break;
459                }
460            }
461    
462          for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++)
463            {
464              if (fgets (buffer, 255, fin) == NULL)
465                {
466                  line = 0;
467                  if (j == 0)
468                    {
469                      rewind (fin);
470                      if (fgets (buffer, 255, fin) == NULL)
471                        {
472                          break;
473                        }
474                    }
475                  else
476                    {
477                      break;
478                    }
479                }
480              line++;
481              i = strlen (buffer);
482              if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
483                {
484                  buffer[i - 1] = '\r';
485                  buffer[i] = '\n';
486                  buffer[i + 1] = '\0';
487                }
488              prints (buffer);
489              iflush ();
490            }
491          fclose (fin);
492        }
493    
494    return 0;    return 0;
495  }  }


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

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