console.c revision 1.7.78.1 1 /* $NetBSD: console.c,v 1.7.78.1 2008/05/16 02:21:52 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Ignatios Souvatzis.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Bootblock support routines for Intuition console support.
34 */
35
36 #include <sys/types.h>
37
38 #include <lib/libsa/stand.h>
39 #include "samachdep.h"
40
41 #include "amigatypes.h"
42 #include "amigagraph.h"
43 #include "amigaio.h"
44 #include "libstubs.h"
45
46 const u_int32_t screentags[] = {
47 SA_Type, CUSTOMSCREEN,
48 SA_DisplayID, 0x8000,
49 SA_ShowTitle, 0,
50 SA_Quiet, 1,
51 0
52 };
53
54 u_int32_t windowtags[] = {
55 WA_CustomScreen, 0L,
56 WA_Borderless, 1L,
57 WA_Backdrop, 1L,
58 WA_Activate, 1L,
59 0
60 };
61
62 struct Console {
63 int magic;
64 struct AmigaIO *cnior;
65 struct TimerIO *tmior;
66 struct MsgPort *cnmp;
67 struct Screen *s;
68 struct Window *w;
69 } *ConsoleBase;
70 static struct Console myConsole;
71
72 u_int16_t timelimit;
73
74 int
75 consinit(void *consptr) {
76 struct Console *mc;
77
78 if (consptr != NULL) {
79 /* Check magic? */
80 ConsoleBase = consptr; /* Use existing console */
81 return (0);
82 }
83
84 mc = &myConsole;
85 IntuitionBase = OpenLibrary("intuition.library", 36L);
86 if (IntuitionBase == 0)
87 goto err;
88
89 mc->s = OpenScreenTagList(0, screentags);
90 if (!mc->s)
91 goto err;
92
93 windowtags[1] = (u_int32_t)mc->s;
94 mc->w = OpenWindowTagList(0, windowtags);
95 if (!mc->w)
96 goto err;
97
98 mc->cnmp = CreateMsgPort();
99
100 if (!mc->cnmp)
101 goto err;
102
103 mc->cnior = (struct AmigaIO *)CreateIORequest(mc->cnmp, sizeof(struct AmigaIO));
104 if (!mc->cnior)
105 goto err;
106
107 mc->cnior->buf = (void *)mc->w;
108 if (OpenDevice("console.device", 0, mc->cnior, 0))
109 goto err;
110
111 mc->tmior = (struct TimerIO *)CreateIORequest(mc->cnmp, sizeof(struct TimerIO));
112 if (!mc->tmior)
113 goto err;
114
115 if (OpenDevice("timer.device", 0, (struct AmigaIO*)mc->tmior, 0))
116 goto err;
117
118 ConsoleBase = mc;
119 return 0;
120
121 err:
122 #ifdef notyet
123 if (mc->tmior)
124 DeleteIORequest(mc->tmior);
125
126 if (mc->cnior)
127 DeleteIORequest(mc->cnior);
128
129 if (mc->cnmp)
130 DeleteMsgPort(mc->cnmp);
131
132 if (mc->w)
133 CloseWindow(mc->w);
134
135 if (mc->s)
136 CloseScreen(mc->s);
137 if (IntuitionBase)
138 CloseLibrary(IntuitionBase);
139 #endif
140
141 return 1;
142 }
143
144 #ifdef _PRIMARY_BOOT
145 int
146 consclose()
147 {
148 struct Console *mc = ConsoleBase;
149
150 if (mc == NULL)
151 return 0;
152 if (mc->tmior) {
153 CloseDevice((struct AmigaIO *)mc->tmior);
154 DeleteIORequest(mc->tmior);
155 }
156
157 if (mc->cnior) {
158 CloseDevice(mc->cnior);
159 DeleteIORequest(mc->cnior);
160 }
161
162 if (mc->cnmp)
163 DeleteMsgPort(mc->cnmp);
164
165 if (mc->w)
166 CloseWindow(mc->w);
167
168 if (mc->s)
169 CloseScreen(mc->s);
170 if (IntuitionBase)
171 CloseLibrary(IntuitionBase);
172 ConsoleBase = NULL;
173 return 0;
174 }
175 #endif
176
177 void
178 putchar(c)
179 char c;
180 {
181 struct Console *mc = ConsoleBase;
182
183 mc->cnior->length = 1;
184 mc->cnior->buf = &c;
185 mc->cnior->cmd = Cmd_Wr;
186 (void)DoIO(mc->cnior);
187 }
188
189 void
190 puts(s)
191 char *s;
192 {
193 struct Console *mc = ConsoleBase;
194
195 mc->cnior->length = -1;
196 mc->cnior->buf = s;
197 mc->cnior->cmd = Cmd_Wr;
198 (void)DoIO(mc->cnior);
199 }
200
201 int
202 getchar()
203 {
204 struct AmigaIO *ior;
205 char c = -1;
206 struct Console *mc = ConsoleBase;
207
208 mc->cnior->length = 1;
209 mc->cnior->buf = &c;
210 mc->cnior->cmd = Cmd_Rd;
211
212 SendIO(mc->cnior);
213
214 if (timelimit) {
215 mc->tmior->cmd = Cmd_Addtimereq;
216 mc->tmior->secs = timelimit;
217 mc->tmior->usec = 2; /* Paranoid */
218 SendIO((struct AmigaIO *)mc->tmior);
219
220 ior = WaitPort(mc->cnmp);
221 if (ior == mc->cnior)
222 AbortIO((struct AmigaIO *)mc->tmior);
223 else /* if (ior == mc->tmior) */ {
224 AbortIO(mc->cnior);
225 c = '\n';
226 }
227 WaitIO((struct AmigaIO *)mc->tmior);
228 timelimit = 0;
229 }
230 (void)WaitIO(mc->cnior);
231 return c;
232 }
233