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


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

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