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


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

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