/[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.2 by sysadm, Fri Oct 22 18:47:51 2004 UTC Revision 1.22 by sysadm, Tue Apr 29 03:39:04 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 "io.h"  #include "io.h"
21    #include <sys/types.h>
22    #include <sys/stat.h>
23    #include <unistd.h>
24    
25  void  #define ACTIVE_BOARD_HEIGHT 8
26  moveto (int row, int col)  
27    int screen_lines = 24;
28    
29    void 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 clrtoeol()
43    {
44            prints("\033[K");
45            iflush();
46    }
47    
48    void clrline(int line_begin, int line_end)
49    {
50            int i;
51    
52            for (i = line_begin; i <= line_end; i++)
53            {
54                    moveto(i, 0);
55                    prints("\033[K");
56            }
57    
58            iflush();
59  }  }
60    
61  void  void clrtobot(int line_begin)
 clrtoeol ()  
62  {  {
63    prints ("\033[K");          clrline(line_begin, screen_lines);
64    iflush();          moveto(line_begin, 0);
65  }  }
66    
67  void  void clearscr()
 clearscr()  
68  {  {
69    prints ("\33[2J");          prints("\33[2J");
70    moveto (0,0);          moveto(0, 0);
71    iflush();          iflush();
72  }  }
73    
74  int  int press_any_key()
 str_input (char *buffer, int buffer_length, int echo_mode)  
75  {  {
76    char buf[256], ch;          moveto(screen_lines, 0);
77    int c, offset = 0, len, loop = 1, i, hz = 0;          clrtoeol();
78    
79            prints("                           \033[1;33m按任意键继续...\033[0;37m");
80            iflush();
81    
82    memset (buffer, '\0', buffer_length);          return igetch_t(60);
83    }
84    
85    while (c = igetch ())  void set_input_echo(int echo)
86      {  {
87        if (c == CR || c == LF)          if (echo)
         break;  
       if (c == BACKSPACE)  
88          {          {
89            if (offset > 0)                  outc('\x83'); // ASCII code 131
90              {                  iflush();
               buffer[--offset] = '\0';  
               prints ("\b \b");  
 //            clrtoeol ();  
               iflush ();  
             }  
           continue;  
91          }          }
92        if (c > 255 || iscntrl (c))          else
93          {          {
94            continue;                  //    outc ('\x85'); // ASCII code 133
95                    prints("\xff\xfb\x01\xff\xfb\x03");
96                    iflush();
97                    igetch_t(60);
98                    igetch_t(60);
99          }          }
100        if (c > 127 && c <= 255)  }
101          {  
102            hz = (!hz);  int _str_input(char *buffer, int buffer_length, int echo_mode)
103          }  {
104        if (offset >= buffer_length)          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            outc ('\a');                  offset++;
           continue;  
110          }          }
111        buffer[offset++] = (char) c;  
112        buffer[offset] = '\0';          while (c = igetch_t(60))
113        switch (echo_mode)          {
114                    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");
164            iflush();
165    
166            return offset;
167    }
168    
169    int str_input(char *buffer, int buffer_length, int echo_mode)
170    {
171            int offset;
172    
173            memset(buffer, '\0', buffer_length);
174    
175            offset = _str_input(buffer, buffer_length, echo_mode);
176    
177            return offset;
178    };
179    
180    int get_data(int row, int col, char *prompt, char *buffer, int buffer_length, int echo_mode)
181    {
182            int len;
183    
184            moveto(row, col);
185            iflush();
186            prints(prompt);
187            prints(buffer);
188            iflush();
189    
190            len = _str_input(buffer, buffer_length, echo_mode);
191    
192            return len;
193    }
194    
195    int display_file(const char *filename)
196    {
197            char buffer[260];
198            FILE *fin;
199            int i;
200    
201            if ((fin = fopen(filename, "r")) == NULL)
202          {          {
203          case 0:                  return -1;
           outc ((char) c);  
           break;  
         case 1:  
           outc ('*');  
           break;  
204          }          }
       if (!hz)  
         {  
           iflush ();  
         }  
     }  
205    
206    prints ("\r\n");          while (fgets(buffer, 255, fin))
207    iflush ();          {
208                    i = strlen(buffer);
209                    if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
210                    {
211                            buffer[i - 1] = '\r';
212                            buffer[i] = '\n';
213                            buffer[i + 1] = '\0';
214                    }
215                    prints(buffer);
216                    iflush();
217            }
218            fclose(fin);
219    
220    return offset;          return 0;
221  }  }
222    
223  int  int display_file_ex(const char *filename, int begin_line, int wait)
 display_file(const char* filename)  
224  {  {
225    char buffer[256];          char buffer[260], temp[256];
226    FILE *fin;          int i, ch, input_ok, max_lines;
227              long int line, c_line_begin = 0, c_line_total = 0;
228    if ((fin = fopen(filename, "r")) != NULL)          long int f_line, f_size, f_offset;
229    {          FILE *fin;
230      while (fgets(buffer, 255, fin))          struct stat f_stat;
231      {  
232        prints (buffer);          max_lines = screen_lines - 1;
233        iflush ();          clrline(begin_line, screen_lines);
234      }          line = begin_line;
235      fclose (fin);          moveto(line, 0);
236    
237      return 0;          if ((fin = fopen(filename, "r")) != NULL)
238    }          {
239                      if (fstat(fileno(fin), &f_stat) != 0)
240    return -1;                  {
241                            log_error("Get file stat failed\n");
242                            return -1;
243                    }
244                    f_size = f_stat.st_size;
245    
246                    while (fgets(buffer, 255, fin))
247                            c_line_total++;
248                    rewind(fin);
249    
250                    while (fgets(buffer, 255, fin))
251                    {
252                            if (line >= max_lines)
253                            {
254                                    f_offset = ftell(fin);
255    
256                                    moveto(screen_lines, 0);
257                                    prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
258                                               (f_offset - strlen(buffer)) * 100 / f_size);
259                                    iflush();
260    
261                                    input_ok = 0;
262                                    while (!input_ok)
263                                    {
264                                            ch = igetch_t(MAX_DELAY_TIME);
265                                            input_ok = 1;
266                                            switch (ch)
267                                            {
268                                            case KEY_UP:
269                                                    c_line_begin--;
270                                                    if (c_line_begin >= 0)
271                                                    {
272                                                            rewind(fin);
273                                                            for (f_line = 0; f_line < c_line_begin; f_line++)
274                                                            {
275                                                                    if (fgets(buffer, 255, fin) == NULL)
276                                                                            goto exit;
277                                                            }
278                                                    }
279                                                    else
280                                                    {
281                                                            goto exit;
282                                                    }
283                                                    break;
284                                            case KEY_DOWN:
285                                            case CR:
286                                                    c_line_begin++;
287                                                    rewind(fin);
288                                                    for (f_line = 0; f_line < c_line_begin; f_line++)
289                                                    {
290                                                            if (fgets(buffer, 255, fin) == NULL)
291                                                                    goto exit;
292                                                    }
293                                                    break;
294                                            case KEY_PGUP:
295                                            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    
361                            i = strlen(buffer);
362                            if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
363                            {
364                                    buffer[i - 1] = '\r';
365                                    buffer[i] = '\n';
366                                    buffer[i + 1] = '\0';
367                            }
368                            prints(buffer);
369                            iflush();
370    
371                            line++;
372                    }
373                    if (wait)
374                            ch = press_any_key();
375                    else
376                            ch = 0;
377    
378            exit:
379                    fclose(fin);
380    
381                    return ch;
382            }
383    
384            return -1;
385  }  }
386    
387  int  int show_top(char *status)
 show_top()  
388  {  {
389    return 0;          char buffer[256];
390    
391            str_space(buffer, 20 - strlen(BBS_current_section_name));
392    
393            moveto(1, 0);
394            prints("\033[1;44;33m%-20s \033[37m%20s"
395                       "         %s\033[33m讨论区 [%s]\033[m",
396                       status, BBS_name, buffer, BBS_current_section_name);
397            iflush();
398    
399            return 0;
400  }  }
401    
402  int  int show_bottom(char *msg)
 show_bottom()  
403  {  {
404    return 0;          char str_time[256], str_time_onine[20], buffer[256];
405            time_t time_online;
406            struct tm *tm_online;
407    
408            get_time_str(str_time, 256);
409            str_space(buffer, 33 - strlen(BBS_username));
410    
411            time_online = time(0) - BBS_login_tm;
412            tm_online = gmtime(&time_online);
413    
414            moveto(screen_lines, 0);
415            prints("\033[1;44;33m[\033[36m%s\033[33m]"
416                       "%s帐号[\033[36m%s\033[33m]"
417                       "[\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,
419                       tm_online->tm_hour, tm_online->tm_min);
420            iflush();
421    
422            return 0;
423  }  }
424    
425  int  int show_active_board()
 press_any_key()  
426  {  {
427    prints ("                       \033[1;33m按任意键盘继续...\033[0;37m                       \r\n");          char filename[256], buffer[260];
428    iflush();          FILE *fin;
429            int i, j;
430            static long int line;
431    
432            sprintf(filename, "%sdata/active_board.txt", app_home_dir);
433    
434            clrline(3, 2 + ACTIVE_BOARD_HEIGHT);
435    
436            moveto(3, 0);
437    
438            if ((fin = fopen(filename, "r")) != NULL)
439            {
440                    for (j = 0; j < line; j++)
441                    {
442                            if (fgets(buffer, 255, fin) == NULL)
443                            {
444                                    line = 0;
445                                    rewind(fin);
446                                    break;
447                            }
448                    }
449    
450                    for (j = 0; j < ACTIVE_BOARD_HEIGHT; j++)
451                    {
452                            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);
480            }
481    
482    return igetch ();          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