/[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.3 by sysadm, Fri Oct 22 19:15:49 2004 UTC Revision 1.54 by sysadm, Fri May 16 14:09:31 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/mman.h>
34    
35  void  #define ACTIVE_BOARD_HEIGHT 8
36  moveto (int row, int col)  
37    void 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      }          }
   iflush();  
47  }  }
48    
49  void  void clrtoeol()
 clrtoeol ()  
50  {  {
51    prints ("\033[K");          prints("\033[K");
   iflush();  
52  }  }
53    
54  void  void clrline(int line_begin, int line_end)
 clearscr()  
55  {  {
56    prints ("\33[2J");          int i;
57    moveto (0,0);  
58    iflush();          for (i = line_begin; i <= line_end; i++)
59            {
60                    moveto(i, 0);
61                    prints("\033[K");
62            }
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;          moveto(line_begin, 0);
68    int c, offset = 0, len, loop = 1, i, hz = 0;          prints("\033[J");
69            moveto(line_begin, 0);
70    }
71    
72    memset (buffer, '\0', buffer_length);  void clearscr()
73    {
74            prints("\033[2J");
75            moveto(0, 0);
76    }
77    
78    while (c = igetch ())  int press_any_key()
79      {  {
80        if (c == CR || c == LF)          moveto(SCREEN_ROWS, 0);
81          break;          clrtoeol();
82        if (c == BACKSPACE)  
83          {          prints("                           \033[1;33m按任意键继续...\033[0;37m");
84            if (offset > 0)          iflush();
85              {  
86                buffer[--offset] = '\0';          return igetch_t(MIN(MAX_DELAY_TIME, 60));
87                prints ("\b \b");  }
88  //            clrtoeol ();  
89                iflush ();  void set_input_echo(int echo)
90              }  {
91            continue;          if (echo)
         }  
       if (c > 255 || iscntrl (c))  
92          {          {
93            continue;                  outc('\x83'); // ASCII code 131
94                    iflush();
95          }          }
96        if (c > 127 && c <= 255)          else
         {  
           hz = (!hz);  
         }  
       if (offset >= buffer_length)  
97          {          {
98            outc ('\a');                  //    outc ('\x85'); // ASCII code 133
99            continue;                  prints("\xff\xfb\x01\xff\xfb\x03");
100                    iflush();
101                    igetch(0);
102                    igetch_reset();
103          }          }
104        buffer[offset++] = (char) c;  }
105        buffer[offset] = '\0';  
106        switch (echo_mode)  static int _str_input(char *buffer, int buf_size, int echo_mode)
107    {
108            int c;
109            int offset = 0;
110            int hz = 0;
111    
112            buffer[buf_size - 1] = '\0';
113            for (offset = 0; offset < buf_size - 1 && buffer[offset] != '\0'; offset++)
114                    ;
115    
116            igetch_reset();
117    
118            while (!SYS_server_exit)
119          {          {
120          case 0:                  c = igetch_t(MIN(MAX_DELAY_TIME, 60));
121            outc ((char) c);  
122            break;                  if (c == CR)
123          case 1:                  {
124            outc ('*');                          igetch_reset();
125            break;                          break;
126                    }
127                    else if (c == KEY_TIMEOUT || c == KEY_NULL) // timeout or broken pipe
128                    {
129                            return -1;
130                    }
131                    else if (c == LF || c == '\0')
132                    {
133                            continue;
134                    }
135                    else if (c == BACKSPACE)
136                    {
137                            if (offset > 0)
138                            {
139                                    offset--;
140                                    if (buffer[offset] < 0 || buffer[offset] > 127)
141                                    {
142                                            prints("\033[D \033[D");
143                                            offset--;
144                                            if (offset < 0) // should not happen
145                                            {
146                                                    log_error("Offset of buffer is negative\n");
147                                                    offset = 0;
148                                            }
149                                    }
150                                    buffer[offset] = '\0';
151                                    prints("\033[D \033[D");
152                                    iflush();
153                            }
154                            continue;
155                    }
156                    else if (c > 255 || iscntrl(c))
157                    {
158                            continue;
159                    }
160                    else if (c > 127 && c <= 255)
161                    {
162                            if (!hz && offset + 2 > buf_size - 1) // No enough space for Chinese character
163                            {
164                                    igetch(0); // Ignore 1 character
165                                    outc('\a');
166                                    iflush();
167                                    continue;
168                            }
169                            hz = (!hz);
170                    }
171    
172                    if (offset + 1 > buf_size - 1)
173                    {
174                            outc('\a');
175                            iflush();
176                            continue;
177                    }
178    
179                    buffer[offset++] = (char)c;
180                    buffer[offset] = '\0';
181    
182                    switch (echo_mode)
183                    {
184                    case DOECHO:
185                            outc((char)c);
186                            break;
187                    case NOECHO:
188                            outc('*');
189                            break;
190                    }
191                    if (!hz)
192                    {
193                            iflush();
194                    }
195          }          }
       if (!hz)  
         {  
           iflush ();  
         }  
     }  
196    
197    prints ("\r\n");          return offset;
198    iflush ();  }
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    return offset;  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)
 display_file(const char* filename)  
229  {  {
230    char buffer[256];          static int show_help = 1;
231    FILE *fin;          char buffer[LINE_BUFFER_LEN];
232              int ch = KEY_NULL;
233    if ((fin = fopen(filename, "r")) != NULL)          int input_ok, line, max_lines;
234    {          long int line_current = 0;
235      while (fgets(buffer, 255, fin))          const FILE_MMAP *p_file_mmap;
236      {          long int len;
237        prints (buffer);          long int percentile;
238        iflush ();          int loop;
239      }  
240      fclose (fin);          if ((p_file_mmap = get_file_mmap(filename)) == NULL)
241            {
242                    if (load_file_mmap(filename) < 0)
243                    {
244                            log_error("load_file_mmap(%s) error\n", filename);
245                            return KEY_NULL;
246                    }
247    
248                    if ((p_file_mmap = get_file_mmap(filename)) == NULL)
249                    {
250                            log_error("get_file_mmap(%s) error\n", filename);
251                            return KEY_NULL;
252                    }
253            }
254    
255            clrline(begin_line, SCREEN_ROWS);
256            line = begin_line;
257            max_lines = SCREEN_ROWS - 1;
258    
259      return 0;          loop = 1;
260    }          while (!SYS_server_exit && loop)
261              {
262    return -1;                  if (line_current >= p_file_mmap->line_total && p_file_mmap->line_total <= SCREEN_ROWS - 2)
263                    {
264                            if (wait)
265                            {
266                                    ch = press_any_key();
267                            }
268                            else
269                            {
270                                    iflush();
271                            }
272    
273                            loop = 0;
274                            break;
275                    }
276    
277                    if (line_current >= p_file_mmap->line_total || line >= max_lines)
278                    {
279                            if (line_current - (line - 1) + (SCREEN_ROWS - 2) < p_file_mmap->line_total)
280                            {
281                                    percentile = (line_current - (line - 1) + (SCREEN_ROWS - 2)) * 100 / p_file_mmap->line_total;
282                            }
283                            else
284                            {
285                                    percentile = 100;
286                            }
287    
288                            moveto(SCREEN_ROWS, 0);
289                            prints("\033[1;44;32m%s (%d%%)%s\033[33m          │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
290                                       (percentile < 100 ? "下面还有喔" : "没有更多了"), percentile,
291                                       (percentile < 10 ? "  " : (percentile < 100 ? " " : "")));
292                            iflush();
293    
294                            input_ok = 0;
295                            while (!SYS_server_exit && !input_ok)
296                            {
297                                    ch = igetch_t(MAX_DELAY_TIME);
298                                    input_ok = 1;
299                                    switch (ch)
300                                    {
301                                    case KEY_NULL:
302                                    case KEY_TIMEOUT:
303                                            goto cleanup;
304                                    case KEY_HOME:
305                                            line_current = 0;
306                                            line = begin_line;
307                                            max_lines = SCREEN_ROWS - 1;
308                                            clrline(begin_line, SCREEN_ROWS);
309                                            break;
310                                    case KEY_END:
311                                            line_current = p_file_mmap->line_total - (SCREEN_ROWS - 2);
312                                            line = begin_line;
313                                            max_lines = SCREEN_ROWS - 1;
314                                            clrline(begin_line, SCREEN_ROWS);
315                                            break;
316                                    case KEY_UP:
317                                            if (line_current - line < 0) // Reach top
318                                            {
319                                                    break;
320                                            }
321                                            line_current -= line;
322                                            line = begin_line;
323                                            // max_lines = begin_line + 1;
324                                            // prints("\033[T"); // Scroll down 1 line
325                                            max_lines = SCREEN_ROWS - 1; // Legacy Fterm only works with this line
326                                            break;
327                                    case CR:
328                                            igetch_reset();
329                                    case KEY_DOWN:
330                                            if (line_current + ((SCREEN_ROWS - 2) - (line - 1)) >= p_file_mmap->line_total) // Reach bottom
331                                            {
332                                                    break;
333                                            }
334                                            line_current += ((SCREEN_ROWS - 2) - (line - 1));
335                                            line = SCREEN_ROWS - 2;
336                                            max_lines = SCREEN_ROWS - 1;
337                                            moveto(SCREEN_ROWS, 0);
338                                            clrtoeol();
339                                            prints("\033[S"); // Scroll up 1 line
340                                            break;
341                                    case KEY_PGUP:
342                                    case Ctrl('B'):
343                                            if (line_current - line < 0) // Reach top
344                                            {
345                                                    break;
346                                            }
347                                            line_current -= ((SCREEN_ROWS - 3) + (line - 1));
348                                            if (line_current < 0)
349                                            {
350                                                    line_current = 0;
351                                            }
352                                            line = begin_line;
353                                            max_lines = SCREEN_ROWS - 1;
354                                            clrline(begin_line, SCREEN_ROWS);
355                                            break;
356                                    case KEY_RIGHT:
357                                    case KEY_PGDN:
358                                    case Ctrl('F'):
359                                    case KEY_SPACE:
360                                            if (line_current + (SCREEN_ROWS - 2) - (line - 1) >= p_file_mmap->line_total) // Reach bottom
361                                            {
362                                                    break;
363                                            }
364                                            line_current += (SCREEN_ROWS - 3) - (line - 1);
365                                            if (line_current + SCREEN_ROWS - 2 > p_file_mmap->line_total) // No enough lines to display
366                                            {
367                                                    line_current = p_file_mmap->line_total - (SCREEN_ROWS - 2);
368                                            }
369                                            line = begin_line;
370                                            max_lines = SCREEN_ROWS - 1;
371                                            clrline(begin_line, SCREEN_ROWS);
372                                            break;
373                                    case KEY_LEFT:
374                                    case 'q':
375                                    case 'Q':
376                                            loop = 0;
377                                            break;
378                                    case '?':
379                                    case 'h':
380                                    case 'H':
381                                            if (!show_help)
382                                            {
383                                                    break;
384                                            }
385    
386                                            // Display help information
387                                            show_help = 0;
388                                            display_file_ex(DATA_READ_HELP, begin_line, 1);
389                                            show_help = 1;
390    
391                                            // Refresh after display help information
392                                            line_current -= (line - 1);
393                                            line = begin_line;
394                                            max_lines = SCREEN_ROWS - 1;
395                                            clrline(begin_line, SCREEN_ROWS);
396                                            break;
397                                    default:
398                                            log_std("Input: %d\n", ch);
399                                            input_ok = 0;
400                                            break;
401                                    }
402    
403                                    BBS_last_access_tm = time(0);
404                            }
405    
406                            continue;
407                    }
408    
409                    len = p_file_mmap->line_offsets[line_current + 1] - p_file_mmap->line_offsets[line_current];
410                    if (len >= LINE_BUFFER_LEN)
411                    {
412                            log_error("Error length exceeds buffer size: %d\n", len);
413                            len = LINE_BUFFER_LEN - 1;
414                    }
415    
416                    memcpy(buffer, (const char *)p_file_mmap->p_data + p_file_mmap->line_offsets[line_current], (size_t)len);
417                    buffer[len] = '\0';
418    
419                    moveto(line, 0);
420                    clrtoeol();
421                    prints("%s", buffer);
422                    line_current++;
423                    line++;
424            }
425    
426    cleanup:
427            return ch;
428  }  }
429    
430  int  int show_top(char *status)
 show_top()  
431  {  {
432    return 0;          int end_of_line;
433            int display_len;
434            int len;
435    
436            char space1[LINE_BUFFER_LEN];
437            char space2[LINE_BUFFER_LEN];
438    
439            len = split_line(status, 20, &end_of_line, &display_len);
440            if (end_of_line)
441            {
442                    status[len] = '\0';
443            }
444            str_space(space1, 31 - display_len);
445    
446            len = split_line(BBS_current_section_name, 20, &end_of_line, &display_len);
447            if (end_of_line)
448            {
449                    status[len] = '\0';
450            }
451            str_space(space2, 30 - display_len);
452    
453            moveto(1, 0);
454            clrtoeol();
455            prints("\033[1;44;33m%s \033[37m%s%s%s\033[33m 讨论区 [%s]\033[m",
456                       status, space1, BBS_name, space2, BBS_current_section_name);
457            iflush();
458    
459            return 0;
460  }  }
461    
462  int  int show_bottom(char *msg)
 show_bottom()  
463  {  {
464    return 0;          char str_time[LINE_BUFFER_LEN];
465            char space[LINE_BUFFER_LEN];
466            time_t time_online;
467            struct tm *tm_online;
468    
469            get_time_str(str_time, sizeof(str_time));
470            str_space(space, 34 - (int)strnlen(BBS_username, sizeof(BBS_username)));
471    
472            time_online = time(0) - BBS_login_tm;
473            tm_online = gmtime(&time_online);
474    
475            moveto(SCREEN_ROWS, 0);
476            clrtoeol();
477            prints("\033[1;44;33m[\033[36m%s\033[33m]%s帐号[\033[36m%s\033[33m]"
478                       "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",
479                       str_time, space, BBS_username, tm_online->tm_mday - 1,
480                       tm_online->tm_hour, tm_online->tm_min);
481            iflush();
482    
483            return 0;
484  }  }
485    
486  int  int show_active_board()
 press_any_key()  
487  {  {
488    prints ("                       \033[1;33m按任意键盘继续...\033[0;37m");          char filename[FILE_PATH_LEN];
489    iflush();          char buffer[LINE_BUFFER_LEN];
490            FILE *fin;
491            static int line;
492            int len;
493            int end_of_line;
494            int display_len;
495    
496            clrline(3, 2 + ACTIVE_BOARD_HEIGHT);
497    
498            if ((fin = fopen(DATA_ACTIVE_BOARD, "r")) == NULL)
499            {
500                    log_error("Unable to open file %s\n", filename);
501                    return -1;
502            }
503    
504            for (int i = 0; i < line; i++)
505            {
506                    if (fgets(buffer, sizeof(buffer), fin) == NULL)
507                    {
508                            line = 0;
509                            rewind(fin);
510                            break;
511                    }
512            }
513    
514            for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
515            {
516                    if (fgets(buffer, sizeof(buffer), fin) == NULL)
517                    {
518                            line = 0;
519                            break;
520                    }
521                    line++;
522                    len = split_line(buffer, SCREEN_COLS, &end_of_line, &display_len);
523                    buffer[len] = '\0'; // Truncate over-length line
524                    moveto(3 + i, 0);
525                    prints("%s", buffer);
526            }
527            iflush();
528    
529            fclose(fin);
530    
531    return igetch ();          return 0;
532  }  }


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

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