/[LeafOK_CVS]/lbbs/src/screen.c
ViewVC logotype

Diff of /lbbs/src/screen.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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


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

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