/[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.69 by sysadm, Sun May 25 06:55:23 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                            screen.c  -  description                                                    screen.c  -  description
3                               -------------------                                                           -------------------
4      begin                : Mon Oct 18 2004          Copyright            : (C) 2004-2025 by Leaflet
5      copyright            : (C) 2004 by Leaflet          Email                : leaflet@leafok.com
     email                : leaflet@leafok.com  
6   ***************************************************************************/   ***************************************************************************/
7    
8  /***************************************************************************  /***************************************************************************
9   *                                                                         *   *                                                                         *
10   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
11   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
12   *   the Free Software Foundation; either version 2 of the License, or     *   *   the Free Software Foundation; either version 3 of the License, or     *
13   *   (at your option) any later version.                                   *   *   (at your option) any later version.                                   *
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #include "screen.h"
18    #include "bbs.h"
19    #include "common.h"
20    #include "str_process.h"
21    #include "log.h"
22  #include "io.h"  #include "io.h"
23    #include "file_loader.h"
24    #include <fcntl.h>
25    #include <string.h>
26    #include <ctype.h>
27    #include <unistd.h>
28    #include <stdlib.h>
29    #include <errno.h>
30    #include <sys/types.h>
31    #include <sys/stat.h>
32    #include <sys/param.h>
33    #include <sys/shm.h>
34    
35  int screen_lines = 24;  #define ACTIVE_BOARD_HEIGHT 8
36    
37  void  void moveto(int row, int col)
 moveto (int row, int col)  
38  {  {
39    if (row >= 0)          if (row >= 0)
40      {          {
41        prints ("\033[%d;%dH", row, col);                  prints("\033[%d;%dH", row, col);
42      }          }
43    else          else
44      {          {
45        prints ("\r");                  prints("\r");
46      }          }
47    iflush();  }
48  }  
49    void clrtoeol()
50  void  {
51  clrtoeol ()          prints(CTRL_SEQ_CLR_LINE);
52  {  }
53    prints ("\033[K");  
54    iflush();  void clrline(int line_begin, int line_end)
55  }  {
56            int i;
57  void  
58  clrline (int line_begin, int line_end)          for (i = line_begin; i <= line_end; i++)
59  {          {
60    int i;                  moveto(i, 0);
61                      prints(CTRL_SEQ_CLR_LINE);
62    for (i = line_begin; i <= line_end; i++)          }
63    {  }
64      moveto (i, 0);  
65      prints ("\033[K");  void clrtobot(int line_begin)
66    }  {
67            moveto(line_begin, 0);
68    iflush();          prints("\033[J");
69  }          moveto(line_begin, 0);
70    }
71  void  
72  clearscr()  void clearscr()
73  {  {
74    prints ("\33[2J");          prints("\033[2J");
75    moveto (0,0);          moveto(0, 0);
76    iflush();  }
77  }  
78    int press_any_key()
79  int  {
80  str_input (char *buffer, int buffer_length, int echo_mode)          moveto(SCREEN_ROWS, 0);
81  {          clrtoeol();
82    char buf[256], ch;  
83    int c, offset = 0, len, loop = 1, i, hz = 0;          prints("                           \033[1;33m按任意键继续...\033[0;37m");
84            iflush();
85    memset (buffer, '\0', buffer_length);  
86            return igetch_t(MIN(MAX_DELAY_TIME, 60));
87    while (c = igetch ())  }
88      {  
89        if (c == CR || c == LF)  void set_input_echo(int echo)
90          break;  {
91        if (c == BACKSPACE)          if (echo)
92          {          {
93            if (offset > 0)                  outc('\x83'); // ASCII code 131
94              {                  iflush();
95                buffer[--offset] = '\0';          }
96                prints ("\b \b");          else
97  //            clrtoeol ();          {
98                iflush ();                  //    outc ('\x85'); // ASCII code 133
99              }                  prints("\xff\xfb\x01\xff\xfb\x03");
100            continue;                  iflush();
101          }                  igetch(0);
102        if (c > 255 || iscntrl (c))                  igetch_reset();
103          {          }
104            continue;  }
105          }  
106        if (c > 127 && c <= 255)  static int _str_input(char *buffer, int buf_size, int echo_mode)
107          {  {
108            hz = (!hz);          int c;
109          }          int offset = 0;
110        if (offset >= buffer_length)          int hz = 0;
111          {  
112            outc ('\a');          buffer[buf_size - 1] = '\0';
113            continue;          for (offset = 0; offset < buf_size - 1 && buffer[offset] != '\0'; offset++)
114          }                  ;
115        buffer[offset++] = (char) c;  
116        buffer[offset] = '\0';          igetch_reset();
117        switch (echo_mode)  
118          {          while (!SYS_server_exit)
119          case 0:          {
120            outc ((char) c);                  c = igetch_t(MIN(MAX_DELAY_TIME, 60));
121            break;  
122          case 1:                  if (c == CR)
123            outc ('*');                  {
124            break;                          igetch_reset();
125          }                          break;
126        if (!hz)                  }
127          {                  else if (c == KEY_TIMEOUT || c == KEY_NULL) // timeout or broken pipe
128            iflush ();                  {
129          }                          return -1;
130      }                  }
131                    else if (c == LF || c == '\0')
132    prints ("\r\n");                  {
133    iflush ();                          continue;
134                    }
135    return offset;                  else if (c == BACKSPACE)
136  }                  {
137                            if (offset > 0)
138  int                          {
139  display_file(const char* filename, int clear, int begin_line)                                  offset--;
140  {                                  if (buffer[offset] < 0 || buffer[offset] > 127)
141    char buffer[260];                                  {
142    FILE *fin;                                          prints("\033[D \033[D");
143    int i, line;                                          offset--;
144                                              if (offset < 0) // should not happen
145    if (clear)                                          {
146    {                                                  log_error("Offset of buffer is negative\n");
147      clrline(begin_line, screen_lines - 1);                                                  offset = 0;
148      line = begin_line;                                          }
149      moveto (line, 0);                                  }
150    }                                  buffer[offset] = '\0';
151                                      prints("\033[D \033[D");
152    if ((fin = fopen(filename, "r")) != NULL)                                  iflush();
153    {                          }
154      while (fgets(buffer, 255, fin))                          continue;
155      {                  }
156        if (line == screen_lines - 1)                  else if (c > 255 || iscntrl(c))
157        {                  {
158          moveto (screen_lines - 1, 0);                          continue;
159          press_any_key();                  }
160          clrline(begin_line, screen_lines - 1);                  else if (c > 127 && c <= 255)
161          line = begin_line;                  {
162          moveto (line, 0);                          if (!hz && offset + 2 > buf_size - 1) // No enough space for Chinese character
163        }                          {
164                                    igetch(0); // Ignore 1 character
165        i = strlen(buffer);                                  outc('\a');
166        if (buffer[i-1] == '\n' && buffer[i-2] != '\r')                                  iflush();
167        {                                  continue;
168           buffer[i-1] = '\r';                          }
169           buffer[i] = '\n';                          hz = (!hz);
170           buffer[i+1] = '\0';                  }
171        }  
172        prints (buffer);                  if (offset + 1 > buf_size - 1)
173        iflush ();                  {
174                                  outc('\a');
175        line ++;                          iflush();
176      }                          continue;
177      fclose (fin);                  }
178    
179      return 0;                  buffer[offset++] = (char)c;
180    }                  buffer[offset] = '\0';
181      
182    return -1;                  switch (echo_mode)
183  }                  {
184                    case DOECHO:
185  int                          outc((char)c);
186  show_top()                          break;
187  {                  case NOECHO:
188    return 0;                          outc('*');
189  }                          break;
190                    }
191  int                  if (!hz)
192  show_bottom()                  {
193  {                          iflush();
194    return 0;                  }
195            }
196    
197            return offset;
198    }
199    
200    int str_input(char *buffer, int buf_size, int echo_mode)
201    {
202            int len;
203    
204            buffer[0] = '\0';
205    
206            len = _str_input(buffer, buf_size, echo_mode);
207    
208            prints("\r\n");
209            iflush();
210    
211            return len;
212    };
213    
214    int get_data(int row, int col, char *prompt, char *buffer, int buf_size, int echo_mode)
215    {
216            int len;
217    
218            moveto(row, col);
219            prints(prompt);
220            prints(buffer);
221            iflush();
222    
223            len = _str_input(buffer, buf_size, echo_mode);
224    
225            return len;
226  }  }
227    
228  int  int display_file_ex(const char *filename, int begin_line, int wait)
 press_any_key()  
229  {  {
230    prints ("                          \033[1;33m按任意键盘继续...\033[0;37m");          static int show_help = 1;
231    iflush();          char buffer[LINE_BUFFER_LEN];
232            int ch = KEY_NULL;
233            int input_ok, line, max_lines;
234            long int line_current = 0;
235            const void *p_shm;
236            size_t data_len;
237            long line_total;
238            const void *p_data;
239            const long *p_line_offsets;
240            long int len;
241            long int percentile;
242            int loop;
243    
244            if ((p_shm = get_file_shm_readonly(filename, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
245            {
246                    log_error("get_file_shm(%s) error\n", filename);
247                    return KEY_NULL;
248            }
249    
250            clrline(begin_line, SCREEN_ROWS);
251            line = begin_line;
252            max_lines = SCREEN_ROWS - 1;
253    
254            loop = 1;
255            while (!SYS_server_exit && loop)
256            {
257                    if (line_current >= line_total && line_total <= SCREEN_ROWS - 2)
258                    {
259                            if (wait)
260                            {
261                                    ch = press_any_key();
262                            }
263                            else
264                            {
265                                    iflush();
266                            }
267    
268                            loop = 0;
269                            break;
270                    }
271    
272                    if (line_current >= line_total || line >= max_lines)
273                    {
274                            if (line_current - (line - 1) + (SCREEN_ROWS - 2) < line_total)
275                            {
276                                    percentile = (line_current - (line - 1) + (SCREEN_ROWS - 2)) * 100 / line_total;
277                            }
278                            else
279                            {
280                                    percentile = 100;
281                            }
282    
283                            moveto(SCREEN_ROWS, 0);
284                            prints("\033[1;44;32m%s (%d%%)%s\033[33m          │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
285                                       (percentile < 100 ? "下面还有喔" : "没有更多了"), percentile,
286                                       (percentile < 10 ? "  " : (percentile < 100 ? " " : "")));
287                            iflush();
288    
289                            input_ok = 0;
290                            while (!SYS_server_exit && !input_ok)
291                            {
292                                    ch = igetch_t(MAX_DELAY_TIME);
293                                    input_ok = 1;
294                                    switch (ch)
295                                    {
296                                    case KEY_NULL:
297                                    case KEY_TIMEOUT:
298                                            goto cleanup;
299                                    case KEY_HOME:
300                                            line_current = 0;
301                                            line = begin_line;
302                                            max_lines = SCREEN_ROWS - 1;
303                                            clrline(begin_line, SCREEN_ROWS);
304                                            break;
305                                    case KEY_END:
306                                            line_current = line_total - (SCREEN_ROWS - 2);
307                                            line = begin_line;
308                                            max_lines = SCREEN_ROWS - 1;
309                                            clrline(begin_line, SCREEN_ROWS);
310                                            break;
311                                    case KEY_UP:
312                                            if (line_current - line < 0) // Reach top
313                                            {
314                                                    break;
315                                            }
316                                            line_current -= line;
317                                            line = begin_line;
318                                            // max_lines = begin_line + 1;
319                                            // prints("\033[T"); // Scroll down 1 line
320                                            max_lines = SCREEN_ROWS - 1; // Legacy Fterm only works with this line
321                                            break;
322                                    case CR:
323                                            igetch_reset();
324                                    case KEY_DOWN:
325                                            if (line_current + ((SCREEN_ROWS - 2) - (line - 1)) >= line_total) // Reach bottom
326                                            {
327                                                    break;
328                                            }
329                                            line_current += ((SCREEN_ROWS - 2) - (line - 1));
330                                            line = SCREEN_ROWS - 2;
331                                            max_lines = SCREEN_ROWS - 1;
332                                            moveto(SCREEN_ROWS, 0);
333                                            clrtoeol();
334                                            prints("\033[S"); // Scroll up 1 line
335                                            break;
336                                    case KEY_PGUP:
337                                    case Ctrl('B'):
338                                            if (line_current - line < 0) // Reach top
339                                            {
340                                                    break;
341                                            }
342                                            line_current -= ((SCREEN_ROWS - 3) + (line - 1));
343                                            if (line_current < 0)
344                                            {
345                                                    line_current = 0;
346                                            }
347                                            line = begin_line;
348                                            max_lines = SCREEN_ROWS - 1;
349                                            clrline(begin_line, SCREEN_ROWS);
350                                            break;
351                                    case KEY_RIGHT:
352                                    case KEY_PGDN:
353                                    case Ctrl('F'):
354                                    case KEY_SPACE:
355                                            if (line_current + (SCREEN_ROWS - 2) - (line - 1) >= line_total) // Reach bottom
356                                            {
357                                                    break;
358                                            }
359                                            line_current += (SCREEN_ROWS - 3) - (line - 1);
360                                            if (line_current + SCREEN_ROWS - 2 > line_total) // No enough lines to display
361                                            {
362                                                    line_current = line_total - (SCREEN_ROWS - 2);
363                                            }
364                                            line = begin_line;
365                                            max_lines = SCREEN_ROWS - 1;
366                                            clrline(begin_line, SCREEN_ROWS);
367                                            break;
368                                    case KEY_LEFT:
369                                    case 'q':
370                                    case 'Q':
371                                            loop = 0;
372                                            break;
373                                    case '?':
374                                    case 'h':
375                                    case 'H':
376                                            if (!show_help)
377                                            {
378                                                    break;
379                                            }
380    
381                                            // Display help information
382                                            show_help = 0;
383                                            display_file_ex(DATA_READ_HELP, begin_line, 1);
384                                            show_help = 1;
385    
386                                            // Refresh after display help information
387                                            line_current -= (line - 1);
388                                            line = begin_line;
389                                            max_lines = SCREEN_ROWS - 1;
390                                            clrline(begin_line, SCREEN_ROWS);
391                                            break;
392                                    default:
393                                            input_ok = 0;
394                                            break;
395                                    }
396    
397                                    BBS_last_access_tm = time(0);
398                            }
399    
400                            continue;
401                    }
402    
403                    len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
404                    if (len >= LINE_BUFFER_LEN)
405                    {
406                            log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld \n",
407                                              len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
408                            len = LINE_BUFFER_LEN - 1;
409                    }
410    
411                    memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
412                    buffer[len] = '\0';
413    
414                    moveto(line, 0);
415                    clrtoeol();
416                    prints("%s", buffer);
417                    line_current++;
418                    line++;
419            }
420    
421    cleanup:
422            if (detach_file_shm(p_shm) < 0)
423            {
424                    log_error("detach_file_shm(%s) error\n", filename);
425            }
426    
427            return ch;
428    }
429    
430    int show_top(const char *status)
431    {
432            char status_f[21];
433            int truncate;
434            int status_len;
435            int section_name_len;
436            int len;
437    
438            strncpy(status_f, status, sizeof(status_f) - 1);
439            status_f[sizeof(status_f) - 1] = '\0';
440    
441            len = split_line(status_f, 20, &truncate, &status_len);
442            if (truncate)
443            {
444                    log_error("Status string is truncated\n");
445                    status_f[len] = '\0';
446            }
447    
448            len = split_line(BBS_current_section_name, BBS_section_name_max_len, &truncate, &section_name_len);
449            if (truncate)
450            {
451                    log_error("Section name is truncated\n");
452            }
453    
454            moveto(1, 0);
455            clrtoeol();
456            prints("\033[1;44;33m%s\033[37m%*s%*s\033[33m 讨论区 [%s]\033[m",
457                       status_f, 44 - status_len, BBS_name, 26 - section_name_len, "", BBS_current_section_name);
458    
459            return 0;
460    }
461    
462    int show_bottom(const char *msg)
463    {
464            char str_time[LINE_BUFFER_LEN];
465            time_t time_online;
466            struct tm *tm_online;
467            char msg_f[21];
468            int truncate;
469            int msg_len;
470            int len;
471            int len_username;
472    
473            get_time_str(str_time, sizeof(str_time));
474    
475            strncpy(msg_f, msg, sizeof(msg_f) - 1);
476            msg_f[sizeof(msg_f) - 1] = '\0';
477    
478            len = split_line(msg_f, 20, &truncate, &msg_len);
479            if (truncate)
480            {
481                    log_error("Status string is truncated\n");
482                    msg_f[len] = '\0';
483            }
484    
485            len_username = (int)strnlen(BBS_username, sizeof(BBS_username));
486    
487            time_online = time(0) - BBS_login_tm;
488            tm_online = gmtime(&time_online);
489    
490            moveto(SCREEN_ROWS, 0);
491            clrtoeol();
492            prints("\033[1;44;33m[\033[36m%s\033[33m]%*s%*s帐号[\033[36m%s\033[33m]"
493                       "[\033[36m%1d\033[33m天\033[36m%2d\033[33m时\033[36m%2d\033[33m分]\033[m",
494                       str_time, 21, msg_f, 9 - len_username, "", BBS_username,
495                       tm_online->tm_mday - 1, tm_online->tm_hour, tm_online->tm_min);
496    
497            return 0;
498    }
499    
500    int show_active_board()
501    {
502            static int line_current = 0;
503            static const void *p_shm = NULL;
504            static size_t data_len;
505            static long line_total;
506            static const void *p_data;
507            static const long *p_line_offsets;
508    
509            static time_t t_last_show = 0;
510            static int line_last = 0;
511    
512            char buffer[LINE_BUFFER_LEN];
513            long int len;
514    
515            if (p_shm == NULL)
516            {
517                    if ((p_shm = get_file_shm_readonly(DATA_ACTIVE_BOARD, &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
518                    {
519                            log_error("get_file_shm(%s) error\n", DATA_ACTIVE_BOARD);
520                            return KEY_NULL;
521                    }
522            }
523    
524            if (time(0) - t_last_show >= 10)
525            {
526                    line_last = line_current;
527                    t_last_show = time(0);
528            }
529            else
530            {
531                    line_current = line_last;
532            }
533    
534            clrline(2, 2 + ACTIVE_BOARD_HEIGHT);
535    
536            for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
537            {
538                    len = p_line_offsets[line_current + 1] - p_line_offsets[line_current];
539                    if (len >= LINE_BUFFER_LEN)
540                    {
541                            log_error("buffer overflow: len=%ld(%ld - %ld) line=%ld \n",
542                                              len, p_line_offsets[line_current + 1], p_line_offsets[line_current], line_current);
543                            len = LINE_BUFFER_LEN - 1;
544                    }
545    
546                    memcpy(buffer, (const char *)p_data + p_line_offsets[line_current], (size_t)len);
547                    buffer[len] = '\0';
548    
549                    moveto(3 + i, 0);
550                    prints("%s", buffer);
551    
552                    line_current++;
553                    if (line_current >= line_total)
554                    {
555                            line_current = 0;
556                            break;
557                    }
558            }
559    
560    return igetch ();          return 0;
561  }  }


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

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