/[LeafOK_CVS]/lbbs/src/screen.c
ViewVC logotype

Diff of /lbbs/src/screen.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.5 by sysadm, Sat Oct 23 08:22:25 2004 UTC Revision 1.29 by sysadm, Sun May 4 01:44:00 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  int screen_lines = 24;  #define ACTIVE_BOARD_HEIGHT 8
32    
33  void  int screen_rows = 24;
34  moveto (int row, int col)  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            moveto(screen_rows, 0);
80            clrtoeol();
81    
82            prints("                           \033[1;33m按任意键继续...\033[0;37m");
83            iflush();
84    
85            return igetch_t(60);
86    }
87    
88    void set_input_echo(int echo)
89    {
90            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            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                    offset++;
113            }
114    
115            while (c = igetch_t(60))
116            {
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                    return -1;
207            }
208    
209            while (fgets(buffer, sizeof(buffer) - 1, fin))
210            {
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 0;
224    }
225    
226    int display_file_ex(const char *filename, int begin_line, int wait)
227    {
228            char buffer[LINE_BUFFER_LEN];
229            char temp[LINE_BUFFER_LEN];
230            int ch = 0;
231            int input_ok, line, max_lines;
232            long int c_line_current = 0, c_line_total = 0;
233            FILE *fin;
234            struct stat f_stat;
235            long *p_line_offsets;
236            int len;
237            int percentile;
238            int loop = 1;
239    
240            if ((fin = fopen(filename, "r")) == NULL)
241            {
242                    log_error("Unable to open file %s\n", filename);
243                    return -1;
244            }
245    
246            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 && c_line_total <= screen_rows - 2)
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 (c_line_current >= c_line_total || 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                                    percentile = 100;
280                            }
281    
282                            moveto(screen_rows, 0);
283                            prints("\033[1;44;32m%s (%d%%)%s\033[33m  │ 结束 ← <q> │ ↑/↓/PgUp/PgDn 移动 │ ? 辅助说明 │     \033[m",
284                                       (percentile < 100 ? "下面还有喔" : "没有更多了"), percentile,
285                                       (percentile < 10 ? "  " : (percentile < 100 ? " " : "")));
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            free(p_line_offsets);
401            fclose(fin);
402    
403            return ch;
404    }
405    
406    int show_top(char *status)
407  {  {
408    if (row >= 0)          char buffer[LINE_BUFFER_LEN];
409      {  
410        prints ("\033[%d;%dH", row, col);          str_space(buffer, 20 - strlen(BBS_current_section_name));
411      }  
412    else          moveto(1, 0);
413      {          clrtoeol();
414        prints ("\r");          prints("\033[1;44;33m%-20s \033[37m%20s"
415      }                     "         %s\033[33m讨论区 [%s]\033[m",
416    iflush();                     status, BBS_name, buffer, BBS_current_section_name);
417  }          iflush();
418    
419  void          return 0;
420  clrtoeol ()  }
421  {  
422    prints ("\033[K");  int show_bottom(char *msg)
423    iflush();  {
424  }          char str_time[LINE_BUFFER_LEN];
425            char str_time_onine[20];
426  void          char buffer[LINE_BUFFER_LEN];
427  clrline (int line_begin, int line_end)          time_t time_online;
428  {          struct tm *tm_online;
429    int i;  
430              get_time_str(str_time, sizeof(str_time));
431    for (i = line_begin; i <= line_end; i++)          str_space(buffer, 33 - strlen(BBS_username));
432    {  
433      moveto (i, 0);          time_online = time(0) - BBS_login_tm;
434      prints ("\033[K");          tm_online = gmtime(&time_online);
435    }  
436            moveto(screen_rows, 0);
437    iflush();          clrtoeol();
438  }          prints("\033[1;44;33m[\033[36m%s\033[33m]"
439                       "%s帐号[\033[36m%s\033[33m]"
440  void                     "[\033[36m%1d\033[33m:\033[36m%2d\033[33m:\033[36m%2d\033[33m]\033[m",
441  clearscr()                     str_time, buffer, BBS_username, tm_online->tm_mday - 1,
442  {                     tm_online->tm_hour, tm_online->tm_min);
443    prints ("\33[2J");          iflush();
444    moveto (0,0);  
445    iflush();          return 0;
 }  
   
 int  
 str_input (char *buffer, int buffer_length, int echo_mode)  
 {  
   char buf[256], ch;  
   int c, offset = 0, len, loop = 1, i, hz = 0;  
   
   memset (buffer, '\0', buffer_length);  
   
   while (c = igetch ())  
     {  
       if (c == CR || c == LF)  
         break;  
       if (c == BACKSPACE)  
         {  
           if (offset > 0)  
             {  
               buffer[--offset] = '\0';  
               prints ("\b \b");  
 //            clrtoeol ();  
               iflush ();  
             }  
           continue;  
         }  
       if (c > 255 || iscntrl (c))  
         {  
           continue;  
         }  
       if (c > 127 && c <= 255)  
         {  
           hz = (!hz);  
         }  
       if (offset >= buffer_length)  
         {  
           outc ('\a');  
           continue;  
         }  
       buffer[offset++] = (char) c;  
       buffer[offset] = '\0';  
       switch (echo_mode)  
         {  
         case 0:  
           outc ((char) c);  
           break;  
         case 1:  
           outc ('*');  
           break;  
         }  
       if (!hz)  
         {  
           iflush ();  
         }  
     }  
   
   prints ("\r\n");  
   iflush ();  
   
   return offset;  
 }  
   
 int  
 display_file(const char* filename, int clear, int begin_line)  
 {  
   char buffer[260];  
   FILE *fin;  
   int i, line;  
     
   if (clear)  
   {  
     clrline(begin_line, screen_lines - 1);  
     line = begin_line;  
     moveto (line, 0);  
   }  
     
   if ((fin = fopen(filename, "r")) != NULL)  
   {  
     while (fgets(buffer, 255, fin))  
     {  
       if (line == screen_lines - 1)  
       {  
         moveto (screen_lines - 1, 0);  
         press_any_key();  
         clrline(begin_line, screen_lines - 1);  
         line = begin_line;  
         moveto (line, 0);  
       }  
   
       i = strlen(buffer);  
       if (buffer[i-1] == '\n' && buffer[i-2] != '\r')  
       {  
          buffer[i-1] = '\r';  
          buffer[i] = '\n';  
          buffer[i+1] = '\0';  
       }  
       prints (buffer);  
       iflush ();  
         
       line ++;  
     }  
     fclose (fin);  
   
     return 0;  
   }  
     
   return -1;  
 }  
   
 int  
 show_top()  
 {  
   return 0;  
 }  
   
 int  
 show_bottom()  
 {  
   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