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


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

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