/[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.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    #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            prints("\033[J");
70    
71            moveto(line_begin, 0);
72    
73            iflush();
74    }
75    
76    void clearscr()
77    {
78            prints("\033[2J");
79            moveto(0, 0);
80            iflush();
81    }
82    
83    int press_any_key()
84    {
85            moveto(screen_lines, 0);
86            clrtoeol();
87    
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                    outc('\x83'); // ASCII code 131
99                    iflush();
100            }
101            else
102            {
103                    //    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    }
110    
111    static int _str_input(char *buffer, int buffer_length, int echo_mode)
112    {
113            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                    if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR)
124                    {
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    
172            prints("\r\n");
173            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                    return -1;
213            }
214    
215            while (fgets(buffer, 255, fin))
216            {
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 0;
230    }
231    
232    int display_file_ex(const char *filename, int begin_line, int wait)
233  {  {
234    if (row >= 0)          char buffer[260], temp[256];
235      {          int i, ch, input_ok, max_lines;
236        prints ("\033[%d;%dH", row, col);          long int line, c_line_begin = 0, c_line_total = 0;
237      }          long int f_line, f_size, f_offset;
238    else          FILE *fin;
239      {          struct stat f_stat;
240        prints ("\r");  
241      }          max_lines = screen_lines - 1;
242    iflush();          clrline(begin_line, screen_lines);
243  }          line = begin_line;
244            moveto(line, 0);
245  void  
246  clrtoeol ()          if ((fin = fopen(filename, "r")) != NULL)
247  {          {
248    prints ("\033[K");                  if (fstat(fileno(fin), &f_stat) != 0)
249    iflush();                  {
250  }                          log_error("Get file stat failed\n");
251                            return -1;
252  void                  }
253  clrline (int line_begin, int line_end)                  f_size = f_stat.st_size;
254  {  
255    int i;                  while (fgets(buffer, 255, fin))
256                              c_line_total++;
257    for (i = line_begin; i <= line_end; i++)                  rewind(fin);
258    {  
259      moveto (i, 0);                  while (fgets(buffer, 255, fin))
260      prints ("\033[K");                  {
261    }                          if (line >= max_lines)
262                            {
263    iflush();                                  f_offset = ftell(fin);
264  }  
265                                    moveto(screen_lines, 0);
266  void                                  prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
267  clearscr()                                             (f_offset - strlen(buffer)) * 100 / f_size);
268  {                                  iflush();
269    prints ("\33[2J");  
270    moveto (0,0);                                  input_ok = 0;
271    iflush();                                  while (!input_ok)
272  }                                  {
273                                            ch = igetch_t(MAX_DELAY_TIME);
274  int                                          input_ok = 1;
275  str_input (char *buffer, int buffer_length, int echo_mode)                                          switch (ch)
276  {                                          {
277    char buf[256], ch;                                          case KEY_UP:
278    int c, offset = 0, len, loop = 1, i, hz = 0;                                                  c_line_begin--;
279                                                    if (c_line_begin >= 0)
280    memset (buffer, '\0', buffer_length);                                                  {
281                                                            rewind(fin);
282    while (c = igetch ())                                                          for (f_line = 0; f_line < c_line_begin; f_line++)
283      {                                                          {
284        if (c == CR || c == LF)                                                                  if (fgets(buffer, 255, fin) == NULL)
285          break;                                                                          goto exit;
286        if (c == BACKSPACE)                                                          }
287          {                                                  }
288            if (offset > 0)                                                  else
289              {                                                  {
290                buffer[--offset] = '\0';                                                          goto exit;
291                prints ("\b \b");                                                  }
292  //            clrtoeol ();                                                  break;
293                iflush ();                                          case KEY_DOWN:
294              }                                          case CR:
295            continue;                                                  c_line_begin++;
296          }                                                  rewind(fin);
297        if (c > 255 || iscntrl (c))                                                  for (f_line = 0; f_line < c_line_begin; f_line++)
298          {                                                  {
299            continue;                                                          if (fgets(buffer, 255, fin) == NULL)
300          }                                                                  goto exit;
301        if (c > 127 && c <= 255)                                                  }
302          {                                                  break;
303            hz = (!hz);                                          case KEY_PGUP:
304          }                                          case Ctrl('B'):
305        if (offset >= buffer_length)                                                  if (c_line_begin > 0)
306          {                                                          c_line_begin -= (max_lines - begin_line - 1);
307            outc ('\a');                                                  else
308            continue;                                                          goto exit;
309          }                                                  if (c_line_begin < 0)
310        buffer[offset++] = (char) c;                                                          c_line_begin = 0;
311        buffer[offset] = '\0';                                                  rewind(fin);
312        switch (echo_mode)                                                  for (f_line = 0; f_line < c_line_begin; f_line++)
313          {                                                  {
314          case 0:                                                          if (fgets(buffer, 255, fin) == NULL)
315            outc ((char) c);                                                                  goto exit;
316            break;                                                  }
317          case 1:                                                  break;
318            outc ('*');                                          case KEY_RIGHT:
319            break;                                          case KEY_PGDN:
320          }                                          case Ctrl('F'):
321        if (!hz)                                          case KEY_SPACE:
322          {                                                  c_line_begin += (max_lines - begin_line - 1);
323            iflush ();                                                  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    prints ("\r\n");                                                  rewind(fin);
328    iflush ();                                                  for (f_line = 0; f_line < c_line_begin; f_line++)
329                                                    {
330    return offset;                                                          if (fgets(buffer, 255, fin) == NULL)
331  }                                                                  goto exit;
332                                                    }
333  int                                                  break;
334  display_file(const char* filename, int clear, int begin_line)                                          case KEY_NULL:
335  {                                          case KEY_TIMEOUT:
336    char buffer[260];                                          case KEY_LEFT:
337    FILE *fin;                                          case 'q':
338    int i, line;                                          case 'Q':
339                                                      goto exit;
340    if (clear)                                                  break;
341    {                                          case '?':
342      clrline(begin_line, screen_lines - 1);                                          case 'h':
343      line = begin_line;                                          case 'H':
344      moveto (line, 0);                                                  // Display help information
345    }                                                  strcpy(temp, app_home_dir);
346                                                      strcat(temp, "data/read_help.txt");
347    if ((fin = fopen(filename, "r")) != NULL)                                                  display_file_ex(temp, begin_line, 1);
348    {  
349      while (fgets(buffer, 255, fin))                                                  // Refresh after display help information
350      {                                                  rewind(fin);
351        if (line == screen_lines - 1)                                                  for (f_line = 0; f_line < c_line_begin; f_line++)
352        {                                                  {
353          moveto (screen_lines - 1, 0);                                                          if (fgets(buffer, 255, fin) == NULL)
354          press_any_key();                                                                  goto exit;
355          clrline(begin_line, screen_lines - 1);                                                  }
356          line = begin_line;                                                  break;
357          moveto (line, 0);                                          default:
358        }                                                  input_ok = 0;
359                                                    break;
360        i = strlen(buffer);                                          }
361        if (buffer[i-1] == '\n' && buffer[i-2] != '\r')                                  }
362        {  
363           buffer[i-1] = '\r';                                  clrline(begin_line, screen_lines);
364           buffer[i] = '\n';                                  line = begin_line;
365           buffer[i+1] = '\0';                                  moveto(line, 0);
366        }  
367        prints (buffer);                                  continue;
368        iflush ();                          }
369          
370        line ++;                          i = strlen(buffer);
371      }                          if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
372      fclose (fin);                          {
373                                    buffer[i - 1] = '\r';
374      return 0;                                  buffer[i] = '\n';
375    }                                  buffer[i + 1] = '\0';
376                              }
377    return -1;                          prints(buffer);
378  }                          iflush();
379    
380  int                          line++;
381  show_top()                  }
382  {                  if (wait)
383    return 0;                          ch = press_any_key();
384  }                  else
385                            ch = 0;
386  int  
387  show_bottom()          exit:
388  {                  fclose(fin);
389    return 0;  
390                    return ch;
391            }
392    
393            return -1;
394  }  }
395    
396  int  int show_top(char *status)
 press_any_key()  
397  {  {
398    prints ("                          \033[1;33m按任意键盘继续...\033[0;37m");          char buffer[256];
399    iflush();  
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 show_bottom(char *msg)
412    {
413            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 show_active_board()
435    {
436            char filename[256], buffer[260];
437            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