OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [mw/] [src/] [demos/] [nanox/] [nterm.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 673 markom
/*
2
 * Nano-X terminal emulator
3
 *
4
 * Al Riddoch
5
 * Greg Haerr
6
 */
7
 
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <fcntl.h>
11
#include <unistd.h>
12
#include <errno.h>
13
#include <signal.h>
14 716 simons
/* SIMO: we use strlen */
15
#include <string.h>
16 673 markom
 
17
#define MWINCLUDECOLORS
18
#include "nano-X.h"
19
 
20
#define HAVEBLIT 0              /* set if have bitblit (experimental)*/
21
 
22
#define _       ((unsigned) 0)          /* off bits */
23
#define X       ((unsigned) 1)          /* on bits */
24
#define MASK(a,b,c,d,e,f,g) \
25
        (((((((((((((a * 2) + b) * 2) + c) * 2) + d) * 2) \
26
        + e) * 2) + f) * 2) + g) << 9)
27
 
28
#if DOS_DJGPP
29
#define SIGCHLD         17 /* from Linux */
30
#endif
31
 
32
static GR_WINDOW_ID     w1;     /* id for window */
33
static GR_GC_ID         gc1;    /* graphics context */
34
static GR_GC_ID         gc3;    /* graphics context */
35
static GR_COORD         xpos;   /* x coord for text */
36
static GR_COORD         ypos;   /* y coord for text */
37
static GR_SCREEN_INFO   si;     /* screen info */
38
static int              tfd;
39
 
40
void do_buttondown();
41
void do_buttonup();
42
void do_motion();
43
void text_init();
44
int term_init();
45
void do_keystroke();
46
void do_focusin();
47
void do_focusout();
48
void do_enter();
49
void do_exit();
50
void do_fdinput();
51
void printg();
52
void HandleEvent(GR_EVENT *ep);
53
 
54
int main(int argc, char ** argv)
55
{
56
        GR_BITMAP       bitmap1fg[7];   /* mouse cursor */
57
        GR_BITMAP       bitmap1bg[7];
58
 
59
        if (GrOpen() < 0) {
60
                fprintf(stderr, "cannot open graphics\n");
61
                exit(1);
62
        }
63
 
64
        GrGetScreenInfo(&si);
65
 
66
        w1 = GrNewWindow(GR_ROOT_WINDOW_ID, 50, 30, si.cols - 120,
67
                si.rows - 60, 1, WHITE, LTBLUE);
68
 
69
        GrSelectEvents(w1, GR_EVENT_MASK_BUTTON_DOWN |
70
                GR_EVENT_MASK_KEY_DOWN | GR_EVENT_MASK_EXPOSURE |
71
                GR_EVENT_MASK_FOCUS_IN | GR_EVENT_MASK_FOCUS_OUT |
72
                GR_EVENT_MASK_CLOSE_REQ);
73
 
74
        GrMapWindow(w1);
75
 
76
        gc1 = GrNewGC();
77
        gc3 = GrNewGC();
78
 
79
        GrSetGCForeground(gc1, GRAY);
80
        GrSetGCBackground(gc1, LTBLUE);
81
        GrSetGCFont(gc1, GrCreateFont(GR_FONT_SYSTEM_FIXED, 0, NULL));
82
        /*GrSetGCFont(gc1, GrCreateFont(GR_FONT_OEM_FIXED, 0, NULL));*/
83
        GrSetGCForeground(gc3, WHITE);
84
        GrSetGCBackground(gc3, BLACK);
85
 
86
        bitmap1fg[0] = MASK(_,_,X,_,X,_,_);
87
        bitmap1fg[1] = MASK(_,_,_,X,_,_,_);
88
        bitmap1fg[2] = MASK(_,_,_,X,_,_,_);
89
        bitmap1fg[3] = MASK(_,_,_,X,_,_,_);
90
        bitmap1fg[4] = MASK(_,_,_,X,_,_,_);
91
        bitmap1fg[5] = MASK(_,_,_,X,_,_,_);
92
        bitmap1fg[6] = MASK(_,_,X,_,X,_,_);
93
 
94
        bitmap1bg[0] = MASK(_,X,X,X,X,X,_);
95
        bitmap1bg[1] = MASK(_,_,X,X,X,_,_);
96
        bitmap1bg[2] = MASK(_,_,X,X,X,_,_);
97
        bitmap1bg[3] = MASK(_,_,X,X,X,_,_);
98
        bitmap1bg[4] = MASK(_,_,X,X,X,_,_);
99
        bitmap1bg[5] = MASK(_,_,X,X,X,_,_);
100
        bitmap1bg[6] = MASK(_,X,X,X,X,X,_);
101
 
102
        GrSetCursor(w1, 7, 7, 3, 3, WHITE, BLACK, bitmap1fg, bitmap1bg);
103
 
104
        /*GrFillRect(GR_ROOT_WINDOW_ID, gc1, 0, 0, si.cols, si.rows);*/
105
 
106
        GrSetGCForeground(gc1, BLACK);
107
        GrSetGCBackground(gc1, WHITE);
108
        text_init();
109
        if (term_init() < 0) {
110
                GrClose();
111
                exit(1);
112
        }
113
 
114
        /* we want tfd events also*/
115
        GrRegisterInput(tfd);
116
 
117
#if 1
118
        GrMainLoop(HandleEvent);
119
#else
120
        while(1) {
121
                GR_EVENT ev;
122
 
123
                GrGetNextEvent(&ev);
124
                HandleEvent(&ev);
125
        }
126
#endif
127
        /* notreached*/
128
        return 0;
129
}
130
 
131
void
132
HandleEvent(GR_EVENT *ep)
133
{
134
        switch (ep->type) {
135
                case GR_EVENT_TYPE_KEY_DOWN:
136
                        do_keystroke(&ep->keystroke);
137
                        break;
138
 
139
                case GR_EVENT_TYPE_FOCUS_IN:
140
                        do_focusin(&ep->general);
141
                        break;
142
 
143
                case GR_EVENT_TYPE_FOCUS_OUT:
144
                        do_focusout(&ep->general);
145
                        break;
146
 
147
                case GR_EVENT_TYPE_CLOSE_REQ:
148
                        GrClose();
149
                        exit(0);
150
 
151
                case GR_EVENT_TYPE_FDINPUT:
152
                        do_fdinput();
153
                        break;
154
        }
155
}
156
 
157
#if ELKS
158
char * nargv[2] = {"/bin/sash", NULL};
159
#else
160
#if DOS_DJGPP
161
char * nargv[2] = {"bash", NULL};
162
#else
163
char * nargv[2] = {"/bin/sh", NULL};
164
#endif
165
#endif
166
 
167
void sigchild(int signo)
168
{
169
        printg("We have a signal right now!\n");
170
        GrClose();
171
        exit(0);
172
}
173
 
174
int term_init()
175
{
176
        char pty_name[12];
177
        int n = 0;
178
        pid_t pid;
179
 
180
again:
181
        sprintf(pty_name, "/dev/ptyp%d", n);
182
        if ((tfd = open(pty_name, O_RDWR | O_NONBLOCK)) < 0) {
183
                if ((errno == EBUSY || errno == EIO) && n < 10) {
184
                        n++;
185
                        goto again;
186
                }
187
                fprintf(stderr, "Can't create pty %s\n", pty_name);
188
                return -1;
189
        }
190
        signal(SIGCHLD, sigchild);
191
        signal(SIGINT, sigchild);
192
        if ((pid = fork()) == -1) {
193
                fprintf(stderr, "No processes\n");
194
                return -1;
195
        }
196
        if (!pid) {
197
                close(STDIN_FILENO);
198
                close(STDOUT_FILENO);
199
                close(STDERR_FILENO);
200
                close(tfd);
201
 
202
                setsid();
203
                pty_name[5] = 't';
204
                if ((tfd = open(pty_name, O_RDWR)) < 0) {
205
                        fprintf(stderr, "Child: Can't open pty %s\n", pty_name);
206
                        exit(1);
207
                }
208
                dup2(tfd, STDIN_FILENO);
209
                dup2(tfd, STDOUT_FILENO);
210
                dup2(tfd, STDERR_FILENO);
211
                execv(nargv[0], nargv);
212
                exit(1);
213
        }
214
        return 0;
215
}
216
 
217
 
218
GR_SIZE         width;          /* width of character */
219
GR_SIZE         height;         /* height of character */
220
GR_SIZE         base;           /* height of baseline */
221
 
222
void text_init()
223
{
224
        GrGetGCTextSize(gc1, "A", 1, GR_TFASCII, &width, &height, &base);
225
}
226
 
227
void char_del(GR_COORD x, GR_COORD y)
228
{
229
        xpos -= width;
230
        GrFillRect(w1, gc3, x, y /*- height*/ /*+ base*/ + 1, width, height);
231
}
232
 
233
void char_out(GR_CHAR ch)
234
{
235
        switch(ch) {
236
        case '\r':
237
                xpos = 0;
238
                return;
239
        case '\n':
240
                ypos += height;
241
                if(ypos > si.rows - 60 - height) {
242
                        ypos -= height;
243
#if HAVEBLIT
244
                        bogl_cfb8_blit(50, 30, si.cols-120,
245
                                si.rows-60-height, 50, 30+height);
246
                        GrFillRect(w1, gc3, 50, ypos, si.cols-120, height);
247
#else
248
                        /* FIXME: changing FALSE to TRUE crashes nano-X*/
249
                        /* clear screen, no scroll*/
250
                        ypos = 0;
251
                        GrClearWindow(w1, GR_FALSE);
252
#endif
253
                }
254
                return;
255
        case '\007':                    /* bel*/
256
                return;
257
        case '\t':
258
                xpos += width;
259
                while((xpos/width) & 7)
260
                        char_out(' ');
261
                return;
262
        case '\b':                      /* assumes fixed width font!!*/
263
                if (xpos <= 0)
264
                        return;
265
                char_del(xpos, ypos);
266
                return;
267
        }
268
        GrText(w1, gc1, xpos+1, ypos, &ch, 1, GR_TFTOP);
269
        xpos += width;
270
}
271
 
272
void printg(char * text)
273
{
274
        int i;
275
 
276
        for(i = 0; i < strlen(text); i++) {
277
                char_out(text[i]);
278
        }
279
}
280
 
281
 
282
/*
283
 * Here when a keyboard press occurs.
284
 */
285
void
286
do_keystroke(kp)
287
        GR_EVENT_KEYSTROKE      *kp;
288
{
289
        char foo;
290
 
291
        foo = kp->ch;
292
        write(tfd, &foo, 1);
293
}
294
 
295
 
296
/*
297
 * Here when a focus in event occurs.
298
 */
299
void
300
do_focusin(gp)
301
        GR_EVENT_GENERAL        *gp;
302
{
303
        if (gp->wid != w1)
304
                return;
305
        GrSetBorderColor(w1, LTBLUE);
306
}
307
 
308
/*
309
 * Here when a focus out event occurs.
310
 */
311
void
312
do_focusout(gp)
313
        GR_EVENT_GENERAL        *gp;
314
{
315
        if (gp->wid != w1)
316
                return;
317
        GrSetBorderColor(w1, GRAY);
318
}
319
 
320
/*
321
 * Here to read the shell input file descriptor.
322
 */
323
void
324
do_fdinput()
325
{
326
        char    c;
327
 
328
        if (read(tfd, &c, 1) == 1)
329
                char_out(c);
330
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.