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