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 673

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

powered by: WebSVN 2.1.0

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