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


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

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