/[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.35 by sysadm, Tue May 6 05:12:29 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    if (row >= 0)          prints("\033[K");
     {  
       prints ("\033[%d;%dH", row, col);  
     }  
   else  
     {  
       prints ("\r");  
     }  
   iflush();  
51  }  }
52    
53  void  void clrline(int line_begin, int line_end)
 clrtoeol ()  
54  {  {
55    prints ("\033[K");          int i;
56    iflush();  
57            for (i = line_begin; i <= line_end; i++)
58            {
59                    moveto(i, 0);
60                    prints("\033[K");
61            }
62  }  }
63    
64  void  void clrtobot(int line_begin)
 clearscr()  
65  {  {
66    prints ("\33[2J");          moveto(line_begin, 0);
67    moveto (0,0);          prints("\033[J");
68    iflush();          moveto(line_begin, 0);
69  }  }
70    
71  int  void clearscr()
 str_input (char *buffer, int buffer_length, int echo_mode)  
72  {  {
73    char buf[256], ch;          prints("\033[2J");
74    int c, offset = 0, len, loop = 1, i, hz = 0;          moveto(0, 0);
75    }
76    
77    memset (buffer, '\0', buffer_length);  int press_any_key()
78    {
79            moveto(screen_rows, 0);
80            clrtoeol();
81    
82    while (c = igetch ())          prints("                           \033[1;33m按任意键继续...\033[0;37m");
83      {          iflush();
84        if (c == CR || c == LF)  
85          break;          return igetch_t(60);
86        if (c == BACKSPACE)  }
87    
88    void set_input_echo(int echo)
89    {
90            if (echo)
91          {          {
92            if (offset > 0)                  outc('\x83'); // ASCII code 131
93              {                  iflush();
               buffer[--offset] = '\0';  
               prints ("\b \b");  
 //            clrtoeol ();  
               iflush ();  
             }  
           continue;  
94          }          }
95        if (c > 255 || iscntrl (c))          else
96          {          {
97            continue;                  //    outc ('\x85'); // ASCII code 133
98                    prints("\xff\xfb\x01\xff\xfb\x03");
99                    iflush();
100                    igetch(0);
101                    igetch(1);
102          }          }
103        if (c > 127 && c <= 255)  }
104          {  
105            hz = (!hz);  static int _str_input(char *buffer, int buffer_length, int echo_mode)
106          }  {
107        if (offset >= buffer_length)          int c, offset = 0, i, hz = 0;
108    
109            for (i = 0; i < buffer_length && buffer[i] != '\0'; i++)
110          {          {
111            outc ('\a');                  offset++;
           continue;  
112          }          }
113        buffer[offset++] = (char) c;  
114        buffer[offset] = '\0';          while ((c = igetch_t(60)))
115        switch (echo_mode)          {
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 str_input(char *buffer, int buffer_length, int echo_mode)
188    {
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            int len;
204    
205            igetch(1); // Cleanup input buffer
206    
207            moveto(row, col);
208            prints(prompt);
209            prints(buffer);
210            iflush();
211    
212            len = _str_input(buffer, buffer_length, echo_mode);
213    
214            return len;
215    }
216    
217    int display_file(const char *filename)
218    {
219            char buffer[LINE_BUFFER_LEN];
220            FILE *fin;
221            size_t i;
222    
223            if ((fin = fopen(filename, "r")) == NULL)
224          {          {
225          case 0:                  return -1;
           outc ((char) c);  
           break;  
         case 1:  
           outc ('*');  
           break;  
226          }          }
       if (!hz)  
         {  
           iflush ();  
         }  
     }  
227    
228    prints ("\r\n");          while (fgets(buffer, sizeof(buffer) - 1, fin))
229    iflush ();          {
230                    i = strnlen(buffer, sizeof(buffer) - 1);
231                    if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
232                    {
233                            buffer[i - 1] = '\r';
234                            buffer[i] = '\n';
235                            buffer[i + 1] = '\0';
236                    }
237                    prints(buffer);
238                    iflush();
239            }
240            fclose(fin);
241    
242    return offset;          return 0;
243  }  }
244    
245  int  int display_file_ex(const char *filename, int begin_line, int wait)
 display_file(const char* filename)  
246  {  {
247    char buffer[256];          char buffer[LINE_BUFFER_LEN];
248    FILE *fin;          int ch = 0;
249              int input_ok, line, max_lines;
250    if ((fin = fopen(filename, "r")) != NULL)          long int c_line_current = 0;
251    {          long int c_line_total = 0;
252      while (fgets(buffer, 255, fin))          FILE *fin;
253      {          long *p_line_offsets;
254        prints (buffer);          long int len;
255        iflush ();          long int percentile;
256      }          int loop = 1;
257      fclose (fin);  
258            if ((fin = fopen(filename, "r")) == NULL)
259            {
260                    log_error("Unable to open file %s\n", filename);
261                    return -1;
262            }
263    
264            p_line_offsets = (long *)malloc(sizeof(long) * MAX_FILE_LINES);
265    
266      return 0;          c_line_total = split_file_lines(fin, screen_cols, p_line_offsets, MAX_FILE_LINES);
267    }  
268              clrline(begin_line, screen_rows);
269    return -1;          line = begin_line;
270            max_lines = screen_rows - 1;
271    
272            while (loop)
273            {
274                    if (c_line_current >= c_line_total && c_line_total <= screen_rows - 2)
275                    {
276                            if (wait)
277                            {
278                                    ch = press_any_key();
279                            }
280                            else
281                            {
282                                    iflush();
283                            }
284    
285                            loop = 0;
286                            break;
287                    }
288    
289                    if (c_line_current >= c_line_total || line >= max_lines)
290                    {
291                            if (c_line_current - (line - 1) + (screen_rows - 2) < c_line_total)
292                            {
293                                    percentile = (c_line_current - (line - 1) + (screen_rows - 2)) * 100 / c_line_total;
294                            }
295                            else
296                            {
297                                    percentile = 100;
298                            }
299    
300                            moveto(screen_rows, 0);
301                            prints("\033[1;44;32m%s (%d%%)%s\033[33m  │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
302                                       (percentile < 100 ? "下面还有喔" : "没有更多了"), percentile,
303                                       (percentile < 10 ? "  " : (percentile < 100 ? " " : "")));
304                            iflush();
305    
306                            input_ok = 0;
307                            while (!input_ok)
308                            {
309                                    ch = igetch_t(MAX_DELAY_TIME);
310                                    input_ok = 1;
311                                    switch (ch)
312                                    {
313                                    case KEY_UP:
314                                            if (c_line_current - line < 0) // Reach top
315                                            {
316                                                    break;
317                                            }
318                                            c_line_current -= line;
319                                            line = begin_line;
320                                            max_lines = begin_line + 1;
321                                            prints("\033[T"); // Scroll down 1 line
322                                            // max_lines = screen_rows - 1; // Legacy Fterm only works with this line
323                                            break;
324                                    case KEY_DOWN:
325                                    case CR:
326                                            if (c_line_current + ((screen_rows - 2) - (line - 1)) >= c_line_total) // Reach bottom
327                                            {
328                                                    break;
329                                            }
330                                            c_line_current += ((screen_rows - 2) - (line - 1));
331                                            line = screen_rows - 2;
332                                            max_lines = screen_rows - 1;
333                                            moveto(screen_rows, 0);
334                                            clrtoeol();
335                                            prints("\033[S"); // Scroll up 1 line
336                                            break;
337                                    case KEY_PGUP:
338                                    case Ctrl('B'):
339                                            if (c_line_current - line < 0) // Reach top
340                                            {
341                                                    break;
342                                            }
343                                            c_line_current -= ((screen_rows - 3) + (line - 1));
344                                            if (c_line_current < 0)
345                                            {
346                                                    c_line_current = 0;
347                                            }
348                                            line = begin_line;
349                                            max_lines = screen_rows - 1;
350                                            clrline(begin_line, screen_rows);
351                                            break;
352                                    case KEY_RIGHT:
353                                    case KEY_PGDN:
354                                    case Ctrl('F'):
355                                    case KEY_SPACE:
356                                            if (c_line_current + (screen_rows - 2) - (line - 1) >= c_line_total) // Reach bottom
357                                            {
358                                                    break;
359                                            }
360                                            c_line_current += (screen_rows - 3) - (line - 1);
361                                            if (c_line_current + screen_rows - 2 > c_line_total) // No enough lines to display
362                                            {
363                                                    c_line_current = c_line_total - (screen_rows - 2);
364                                            }
365                                            line = begin_line;
366                                            max_lines = screen_rows - 1;
367                                            clrline(begin_line, screen_rows);
368                                            break;
369                                    case KEY_NULL:
370                                    case KEY_TIMEOUT:
371                                    case KEY_LEFT:
372                                    case 'q':
373                                    case 'Q':
374                                            loop = 0;
375                                            break;
376                                    case '?':
377                                    case 'h':
378                                    case 'H':
379                                            // Display help information
380                                            display_file_ex(DATA_READ_HELP, begin_line, 1);
381    
382                                            // Refresh after display help information
383                                            c_line_current -= (line - 1);
384                                            line = begin_line;
385                                            max_lines = screen_rows - 1;
386                                            clrline(begin_line, screen_rows);
387                                            break;
388                                    default:
389                                            input_ok = 0;
390                                            break;
391                                    }
392                            }
393    
394                            continue;
395                    }
396    
397                    fseek(fin, p_line_offsets[c_line_current], SEEK_SET);
398                    len = p_line_offsets[c_line_current + 1] - p_line_offsets[c_line_current];
399                    if (len >= LINE_BUFFER_LEN)
400                    {
401                            log_error("Error length exceeds buffer size: %d\n", len);
402                            len = LINE_BUFFER_LEN - 1;
403                    }
404                    if (fgets(buffer, (int)len + 1, fin) == NULL)
405                    {
406                            log_error("Reach EOF\n");
407                            break;
408                    }
409                    moveto(line, 0);
410                    clrtoeol();
411                    prints("%s", buffer);
412                    c_line_current++;
413                    line++;
414            }
415    
416            free(p_line_offsets);
417            fclose(fin);
418    
419            return ch;
420  }  }
421    
422  int  int show_top(char *status)
 show_top()  
423  {  {
424    return 0;          char buffer[LINE_BUFFER_LEN];
425    
426            str_space(buffer, 20 - (int)strnlen(BBS_current_section_name, sizeof(BBS_current_section_name)));
427    
428            moveto(1, 0);
429            clrtoeol();
430            prints("\033[1;44;33m%-20s \033[37m%20s"
431                       "         %s\033[33m讨论区 [%s]\033[m",
432                       status, BBS_name, buffer, BBS_current_section_name);
433            iflush();
434    
435            return 0;
436  }  }
437    
438  int  int show_bottom(char *msg)
 show_bottom()  
439  {  {
440    return 0;          char str_time[LINE_BUFFER_LEN];
441            char buffer[LINE_BUFFER_LEN];
442            time_t time_online;
443            struct tm *tm_online;
444    
445            get_time_str(str_time, sizeof(str_time));
446            str_space(buffer, 33 - (int)strnlen(BBS_username, sizeof(BBS_username)));
447    
448            time_online = time(0) - BBS_login_tm;
449            tm_online = gmtime(&time_online);
450    
451            moveto(screen_rows, 0);
452            clrtoeol();
453            prints("\033[1;44;33m[\033[36m%s\033[33m]"
454                       "%s帐号[\033[36m%s\033[33m]"
455                       "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",
456                       str_time, buffer, BBS_username, tm_online->tm_mday - 1,
457                       tm_online->tm_hour, tm_online->tm_min);
458            iflush();
459    
460            return 0;
461  }  }
462    
463  int  int show_active_board()
 press_any_key()  
464  {  {
465    prints ("                       \033[1;33m按任意键盘继续...\033[0;37m");          char filename[FILE_PATH_LEN];
466    iflush();          char buffer[LINE_BUFFER_LEN];
467            FILE *fin;
468            static int line;
469            unsigned int len;
470            int end_of_line;
471    
472            clrline(3, 2 + ACTIVE_BOARD_HEIGHT);
473    
474            if ((fin = fopen(DATA_ACTIVE_BOARD, "r")) == NULL)
475            {
476                    log_error("Unable to open file %s\n", filename);
477                    return -1;
478            }
479    
480            for (int i = 0; i < line; i++)
481            {
482                    if (fgets(buffer, sizeof(buffer), fin) == NULL)
483                    {
484                            line = 0;
485                            rewind(fin);
486                            break;
487                    }
488            }
489    
490            for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
491            {
492                    if (fgets(buffer, sizeof(buffer), fin) == NULL)
493                    {
494                            line = 0;
495                            break;
496                    }
497                    line++;
498                    len = split_line(buffer, screen_cols, &end_of_line);
499                    buffer[len] = '\0'; // Truncate over-length line
500                    moveto(3 + i, 0);
501                    prints("%s", buffer);
502            }
503            iflush();
504    
505            fclose(fin);
506    
507    return igetch ();          return 0;
508  }  }


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

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