Debian Codegraph @

terps/alan3/term.c

raw symbols SCIP
1 /*----------------------------------------------------------------------*\
2
3 term.c
4
5 System dependent terminal oriented functions for ALAN interpreter ARUN
6
7 \*----------------------------------------------------------------------*/
8
9 #include "sysdep.h"
10
11
12 #ifdef HAVE_TERMIOS
13
14 #ifdef __linux__
15 #include <sys/ioctl.h>
16 #include <asm/ioctls.h>
17 #endif
18
19 #ifdef __FreeBSD__
20 #include <sys/ioctl.h>
21 #endif
22
23 #endif /* HAVE_TERMIOS */
24
25 #ifdef HAVE_GLK
26 #include "glkio.h"
27 #endif
28
29 /* IMPORTS */
30 #include "memory.h"
31 #include "output.h"
32 #include "options.h"
33 #include "instance.h"
34 #include "current.h"
35
36
37 /* PUBLIC DATA */
38 bool onStatusLine = false; /* To know if where printing the status line or not */
39
40
41
42 #ifdef __windows__
43
44 #include <windows.h>
45
46 HWND getConsoleHandle(void) {
47 char oldTitle[1000];
48 char newTitle[1000];
49 HWND handle;
50
51 GetConsoleTitle(oldTitle, 1000);
52
53 wsprintf(newTitle, "Arun console%d-%d", GetTickCount(), GetCurrentProcessId());
54 SetConsoleTitle(newTitle);
55 Sleep(50);
56 handle = FindWindow(NULL, newTitle);
57 SetConsoleTitle(oldTitle);
58 return handle;
59 }
60 #endif
61
62
63 /*======================================================================
64
65 getPageSize()
66
67 Try to get the current page size from the system, else use the ones
68 from the header.
69
70 */
71 void getPageSize(void)
72 {
73 #ifdef HAVE_GLK
74 pageLength = 0;
75 pageWidth = 0;
76
77 #else
78 #ifdef HAVE_TERMIOS
79
80 #include <sys/termios.h>
81
82 #ifdef __linux__
83 extern int ioctl (int __fd, unsigned long int __request, ...) __THROW;
84 #else
85 extern int ioctl();
86 #endif
87 struct winsize win;
88 int ecode;
89
90 ecode = ioctl(1, TIOCGWINSZ, &win);
91
92 if (ecode != 0 || win.ws_row == 0)
93 pageLength = header->pageLength;
94 else
95 pageLength = win.ws_row;
96
97 if (ecode != 0 || win.ws_col == 0)
98 pageWidth = header->pageWidth;
99 else
100 pageWidth = win.ws_col;
101
102 #else
103
104 pageLength = header->pageLength;
105 pageWidth = header->pageWidth;
106
107 #endif
108 #endif
109 }
110
111 /*======================================================================*/
112 void statusline(void)
113 {
114 #ifdef HAVE_GLK
115 glui32 glkWidth;
116 char line[100];
117 int pcol = col;
118
119 if (!statusLineOption) return;
120 if (glkStatusWin == NULL)
121 return;
122
123 glk_set_window(glkStatusWin);
124 glk_window_clear(glkStatusWin);
125 glk_window_get_size(glkStatusWin, &glkWidth, NULL);
126
127 #ifdef HAVE_GARGLK
128 int i;
129 glk_set_style(style_User1);
130 for (i = 0; i < glkWidth; i++)
131 glk_put_char(' ');
132 #endif
133
134 onStatusLine = true;
135 col = 1;
136 glk_window_move_cursor(glkStatusWin, 1, 0);
137 sayInstance(where(HERO, true));
138
139 // TODO Add status message1 & 2 as author customizable messages
140 if (header->maximumScore > 0)
141 sprintf(line, "Score %d(%d)/%d moves", current.score, (int)header->maximumScore, current.tick);
142 else
143 sprintf(line, "%d moves", current.tick);
144 glk_window_move_cursor(glkStatusWin, glkWidth-strlen(line)-1, 0);
145 glk_put_string(line);
146 needSpace = false;
147
148 col = pcol;
149 onStatusLine = false;
150
151 glk_set_window(glkMainWin);
152 #else
153 #ifdef HAVE_ANSI_CONTROL
154 char line[100];
155 int i;
156 int pcol = col;
157
158 if (!statusLineOption) return;
159 /* ansi_position(1,1); ansi_bold_on(); */
160 printf("\x1b[1;1H");
161 printf("\x1b[7m");
162
163 onStatusLine = true;
164 col = 1;
165 sayInstance(where(HERO, false));
166
167 if (header->maximumScore > 0)
168 sprintf(line, "Score %d(%d)/%d moves", current.score, header->maximumScore, current.tick);
169 else
170 sprintf(line, "%ld moves", (long)current.tick);
171 for (i=0; i < pageWidth - col - strlen(line); i++) putchar(' ');
172 printf(line);
173 printf("\x1b[m");
174 printf("\x1b[%d;1H", pageLength);
175
176 needSpace = false;
177 capitalize = true;
178
179 onStatusLine = false;
180 col = pcol;
181 #endif
182 #endif
183 }