/[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.4 by sysadm, Fri Oct 22 19:51:01 2004 UTC Revision 1.28 by sysadm, Sat May 3 13:41:21 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)          char buf[256], ch;
108            int c, offset = 0, len, loop = 1, i, hz = 0;
109    
110            for (i = 0; i < buffer_length && buffer[i] != '\0'; i++)
111          {          {
112            outc ('\a');                  offset++;
           continue;  
113          }          }
114        buffer[offset++] = (char) c;  
115        buffer[offset] = '\0';          while (c = igetch_t(60))
116        switch (echo_mode)          {
117                    if (c == KEY_NULL || c == KEY_TIMEOUT || c == CR)
118                    {
119                            igetch(1); // Cleanup remaining '\n' in the buffer
120                            break;
121                    }
122                    if (c == LF)
123                    {
124                            continue;
125                    }
126                    if (c == BACKSPACE)
127                    {
128                            if (offset > 0)
129                            {
130                                    buffer[--offset] = '\0';
131                                    prints("\b \b");
132                                    //            clrtoeol ();
133                                    iflush();
134                            }
135                            continue;
136                    }
137                    if (c > 255 || iscntrl(c))
138                    {
139                            continue;
140                    }
141                    if (c > 127 && c <= 255)
142                    {
143                            hz = (!hz);
144                    }
145                    if (offset >= buffer_length)
146                    {
147                            outc('\a');
148                            continue;
149                    }
150                    buffer[offset++] = (char)c;
151                    buffer[offset] = '\0';
152                    switch (echo_mode)
153                    {
154                    case DOECHO:
155                            outc((char)c);
156                            break;
157                    case NOECHO:
158                            outc('*');
159                            break;
160                    }
161                    if (!hz)
162                    {
163                            iflush();
164                    }
165            }
166    
167            prints("\r\n");
168            iflush();
169    
170            return offset;
171    }
172    
173    int str_input(char *buffer, int buffer_length, int echo_mode)
174    {
175            int offset;
176    
177            memset(buffer, '\0', buffer_length);
178    
179            offset = _str_input(buffer, buffer_length, echo_mode);
180    
181            return offset;
182    };
183    
184    int get_data(int row, int col, char *prompt, char *buffer, int buffer_length, int echo_mode)
185    {
186            int len;
187    
188            moveto(row, col);
189            prints(prompt);
190            prints(buffer);
191            iflush();
192    
193            len = _str_input(buffer, buffer_length, echo_mode);
194    
195            return len;
196    }
197    
198    int display_file(const char *filename)
199    {
200            char buffer[LINE_BUFFER_LEN];
201            FILE *fin;
202            int i;
203    
204            if ((fin = fopen(filename, "r")) == NULL)
205          {          {
206          case 0:                  return -1;
           outc ((char) c);  
           break;  
         case 1:  
           outc ('*');  
           break;  
207          }          }
       if (!hz)  
         {  
           iflush ();  
         }  
     }  
208    
209    prints ("\r\n");          while (fgets(buffer, sizeof(buffer) - 1, fin))
210    iflush ();          {
211                    i = strnlen(buffer, sizeof(buffer) - 1);
212                    if (buffer[i - 1] == '\n' && buffer[i - 2] != '\r')
213                    {
214                            buffer[i - 1] = '\r';
215                            buffer[i] = '\n';
216                            buffer[i + 1] = '\0';
217                    }
218                    prints(buffer);
219                    iflush();
220            }
221            fclose(fin);
222    
223    return offset;          return 0;
224  }  }
225    
226  int  int display_file_ex(const char *filename, int begin_line, int wait)
 display_file(const char* filename)  
227  {  {
228    char buffer[260];          char buffer[LINE_BUFFER_LEN];
229    FILE *fin;          char temp[LINE_BUFFER_LEN];
230    int i;          int ch = 0;
231              int input_ok, line, max_lines;
232    if ((fin = fopen(filename, "r")) != NULL)          long int c_line_current = 0, c_line_total = 0;
233    {          FILE *fin;
234      while (fgets(buffer, 255, fin))          struct stat f_stat;
235      {          long *p_line_offsets;
236        i = strlen(buffer);          int len;
237        if (buffer[i-1] == '\n' && buffer[i-2] != '\r')          int percentile;
238        {          int loop = 1;
239           buffer[i-1] = '\r';  
240           buffer[i] = '\n';          if ((fin = fopen(filename, "r")) == NULL)
241           buffer[i+1] = '\0';          {
242        }                  log_error("Unable to open file %s\n", filename);
243        prints (buffer);                  return -1;
244        iflush ();          }
245      }  
246      fclose (fin);          p_line_offsets = (long *)malloc(sizeof(long) * MAX_FILE_LINES);
247    
248            c_line_total = split_file_lines(fin, screen_cols, p_line_offsets, MAX_FILE_LINES);
249    
250            clrline(begin_line, screen_rows);
251            line = begin_line;
252            max_lines = screen_rows - 1;
253    
254            while (loop)
255            {
256                    if (c_line_current >= c_line_total)
257                    {
258                            if (wait)
259                            {
260                                    ch = press_any_key();
261                            }
262                            else
263                            {
264                                    iflush();
265                            }
266    
267                            loop = 0;
268                            break;
269                    }
270    
271                    if (line >= max_lines)
272                    {
273                            if (c_line_current - (line - 1) + (screen_rows - 2) < c_line_total)
274                            {
275                                    percentile = (c_line_current - (line - 1) + (screen_rows - 2)) * 100 / c_line_total;
276                            }
277                            else
278                            {
279                                    log_error("P100 reached\n");
280                                    percentile = 100;
281                            }
282    
283                            moveto(screen_rows, 0);
284                            prints("\033[1;44;32m下面还有喔 (%d%%)\033[33m   │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
285                                       percentile);
286                            iflush();
287    
288                            input_ok = 0;
289                            while (!input_ok)
290                            {
291                                    ch = igetch_t(MAX_DELAY_TIME);
292                                    input_ok = 1;
293                                    switch (ch)
294                                    {
295                                    case KEY_UP:
296                                            if (c_line_current - line < 0) // Reach top
297                                            {
298                                                    break;
299                                            }
300                                            c_line_current -= line;
301                                            line = begin_line;
302                                            max_lines = begin_line + 1;
303                                            prints("\033[T"); // Scroll down 1 line
304                                            //max_lines = screen_rows - 1; // Legacy Fterm only works with this line
305                                            break;
306                                    case KEY_DOWN:
307                                    case CR:
308                                            if (c_line_current + ((screen_rows - 2) - (line - 1)) >= c_line_total) // Reach bottom
309                                            {
310                                                    break;
311                                            }
312                                            c_line_current += ((screen_rows - 2) - (line - 1));
313                                            line = screen_rows - 2;
314                                            max_lines = screen_rows - 1;
315                                            moveto(screen_rows, 0);
316                                            clrtoeol();
317                                            prints("\033[S"); // Scroll up 1 line
318                                            break;
319                                    case KEY_PGUP:
320                                    case Ctrl('B'):
321                                            if (c_line_current - line < 0) // Reach top
322                                            {
323                                                    break;
324                                            }
325                                            c_line_current -= ((screen_rows - 3) + (line - 1));
326                                            if (c_line_current < 0)
327                                            {
328                                                    c_line_current = 0;
329                                            }
330                                            line = begin_line;
331                                            max_lines = screen_rows - 1;
332                                            clrline(begin_line, screen_rows);
333                                            break;
334                                    case KEY_RIGHT:
335                                    case KEY_PGDN:
336                                    case Ctrl('F'):
337                                    case KEY_SPACE:
338                                            if (c_line_current + (screen_rows - 2) - (line - 1) >= c_line_total) // Reach bottom
339                                            {
340                                                    break;
341                                            }
342                                            c_line_current += (screen_rows - 3) - (line - 1);
343                                            if (c_line_current + screen_rows - 2 > c_line_total) // No enough lines to display
344                                            {
345                                                    c_line_current = c_line_total - (screen_rows - 2);
346                                            }
347                                            line = begin_line;
348                                            max_lines = screen_rows - 1;
349                                            clrline(begin_line, screen_rows);
350                                            break;
351                                    case KEY_NULL:
352                                    case KEY_TIMEOUT:
353                                    case KEY_LEFT:
354                                    case 'q':
355                                    case 'Q':
356                                            loop = 0;
357                                            break;
358                                    case '?':
359                                    case 'h':
360                                    case 'H':
361                                            // Display help information
362                                            strcpy(temp, app_home_dir);
363                                            strcat(temp, "data/read_help.txt");
364                                            display_file_ex(temp, begin_line, 1);
365    
366                                            // Refresh after display help information
367                                            c_line_current -= (line - 1);
368                                            line = begin_line;
369                                            max_lines = screen_rows - 1;
370                                            clrline(begin_line, screen_rows);
371                                            break;
372                                    default:
373                                            input_ok = 0;
374                                            break;
375                                    }
376                            }
377    
378                            continue;
379                    }
380    
381                    fseek(fin, p_line_offsets[c_line_current], SEEK_SET);
382                    len = p_line_offsets[c_line_current + 1] - p_line_offsets[c_line_current];
383                    if (len >= LINE_BUFFER_LEN)
384                    {
385                            log_error("Error length exceeds buffer size: %d\n", len);
386                            len = LINE_BUFFER_LEN - 1;
387                    }
388                    if (fgets(buffer, len + 1, fin) == NULL)
389                    {
390                            log_error("Reach EOF\n");
391                            break;
392                    }
393                    moveto(line, 0);
394                    clrtoeol();
395                    prints("%s", buffer);
396                    c_line_current++;
397                    line++;
398            }
399    
400      return 0;          free(p_line_offsets);
401    }          fclose(fin);
402      
403    return -1;          return ch;
404  }  }
405    
406  int  int show_top(char *status)
 show_top()  
407  {  {
408    return 0;          char buffer[LINE_BUFFER_LEN];
409    
410            str_space(buffer, 20 - strlen(BBS_current_section_name));
411    
412            moveto(1, 0);
413            clrtoeol();
414            prints("\033[1;44;33m%-20s \033[37m%20s"
415                       "         %s\033[33m讨论区 [%s]\033[m",
416                       status, BBS_name, buffer, BBS_current_section_name);
417            iflush();
418    
419            return 0;
420  }  }
421    
422  int  int show_bottom(char *msg)
 show_bottom()  
423  {  {
424    return 0;          char str_time[LINE_BUFFER_LEN];
425            char str_time_onine[20];
426            char buffer[LINE_BUFFER_LEN];
427            time_t time_online;
428            struct tm *tm_online;
429    
430            get_time_str(str_time, sizeof(str_time));
431            str_space(buffer, 33 - strlen(BBS_username));
432    
433            time_online = time(0) - BBS_login_tm;
434            tm_online = gmtime(&time_online);
435    
436            moveto(screen_rows, 0);
437            clrtoeol();
438            prints("\033[1;44;33m[\033[36m%s\033[33m]"
439                       "%s帐号[\033[36m%s\033[33m]"
440                       "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",
441                       str_time, buffer, BBS_username, tm_online->tm_mday - 1,
442                       tm_online->tm_hour, tm_online->tm_min);
443            iflush();
444    
445            return 0;
446  }  }
447    
448  int  int show_active_board()
 press_any_key()  
449  {  {
450    prints ("                       \033[1;33m按任意键盘继续...\033[0;37m");          char filename[FILE_PATH_LEN];
451    iflush();          char buffer[LINE_BUFFER_LEN];
452            FILE *fin;
453            static int line;
454            int len;
455            int end_of_line;
456    
457            sprintf(filename, "%sdata/active_board.txt", app_home_dir);
458    
459            clrline(3, 2 + ACTIVE_BOARD_HEIGHT);
460    
461            if ((fin = fopen(filename, "r")) == NULL)
462            {
463                    log_error("Unable to open file %s\n", filename);
464                    return -1;
465            }
466    
467            for (int i = 0; i < line; i++)
468            {
469                    if (fgets(buffer, sizeof(buffer), fin) == NULL)
470                    {
471                            line = 0;
472                            rewind(fin);
473                            break;
474                    }
475            }
476    
477            for (int i = 0; i < ACTIVE_BOARD_HEIGHT; i++)
478            {
479                    if (fgets(buffer, sizeof(buffer), fin) == NULL)
480                    {
481                            line = 0;
482                            break;
483                    }
484                    line++;
485                    len = split_line(buffer, screen_cols, &end_of_line);
486                    buffer[len] = '\0'; // Truncate over-length line
487                    moveto(3 + i, 0);
488                    prints("%s", buffer);
489            }
490            iflush();
491    
492            fclose(fin);
493    
494    return igetch ();          return 0;
495  }  }


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

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