forked from apache/nuttx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnxterm_driver.c
451 lines (357 loc) · 12.3 KB
/
nxterm_driver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/****************************************************************************
* graphics/nxterm/nxterm_driver.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdbool.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/fs/fs.h>
#include "nxterm.h"
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int nxterm_open(FAR struct file *filep);
static int nxterm_close(FAR struct file *filep);
static ssize_t nxterm_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen);
static int nxterm_ioctl(FAR struct file *filep, int cmd,
unsigned long arg);
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
static int nxterm_unlink(FAR struct inode *inode);
#endif
/****************************************************************************
* Public Data
****************************************************************************/
/* This is the common NX driver file operations */
#ifdef CONFIG_NXTERM_NXKBDIN
const struct file_operations g_nxterm_drvrops =
{
nxterm_open, /* open */
nxterm_close, /* close */
nxterm_read, /* read */
nxterm_write, /* write */
NULL, /* seek */
nxterm_ioctl, /* ioctl */
NULL, /* mmap */
NULL, /* truncate */
nxterm_poll /* poll */
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
, nxterm_unlink /* unlink */
#endif
};
#else /* CONFIG_NXTERM_NXKBDIN */
const struct file_operations g_nxterm_drvrops =
{
nxterm_open, /* open */
nxterm_close, /* close */
NULL, /* read */
nxterm_write, /* write */
NULL, /* seek */
nxterm_ioctl, /* ioctl */
NULL, /* mmap */
NULL, /* truncate */
NULL /* poll */
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
, nxterm_unlink /* unlink */
#endif
};
#endif /* CONFIG_NXTERM_NXKBDIN */
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxterm_open
****************************************************************************/
static int nxterm_open(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct nxterm_state_s *priv = inode->i_private;
/* Get the driver structure from the inode */
inode = filep->f_inode;
priv = inode->i_private;
DEBUGASSERT(priv);
/* Verify that the driver is opened for write-only access */
#ifndef CONFIG_NXTERM_NXKBDIN
if ((filep->f_oflags & O_RDOK) != 0)
{
gerr("ERROR: Attempted open with read access\n");
return -EACCES;
}
#endif
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
/* Increment the count of open file reference */
DEBUGASSERT(priv->orefs != UINT8_MAX);
priv->orefs++;
#endif
/* Assign the driver structure to the file */
filep->f_priv = priv;
return OK;
}
/****************************************************************************
* Name: nxterm_close
****************************************************************************/
static int nxterm_close(FAR struct file *filep)
{
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
FAR struct nxterm_state_s *priv;
int ret;
/* Recover our private state structure */
DEBUGASSERT(filep->f_priv != NULL);
priv = (FAR struct nxterm_state_s *)filep->f_priv;
/* Get exclusive access */
ret = nxmutex_lock(&priv->lock);
if (ret < 0)
{
return ret;
}
/* Has the driver been unlinked? Was this the last open references to the
* terminal driver?
*/
DEBUGASSERT(priv->orefs > 0);
if (priv->unlinked && priv->orefs <= 1)
{
/* Yes.. Unregister the terminal device */
nxmutex_unlock(&priv->lock);
nxterm_unregister(priv);
return OK;
}
else
{
/* No.. Just decrement the count of open file references */
priv->orefs--;
}
nxmutex_unlock(&priv->lock);
#endif
return OK;
}
/****************************************************************************
* Name: nxterm_write
****************************************************************************/
static ssize_t nxterm_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen)
{
FAR struct nxterm_state_s *priv;
enum nxterm_vt100state_e state;
ssize_t remaining;
char ch;
int ret;
/* Recover our private state structure */
DEBUGASSERT(filep->f_priv != NULL);
priv = (FAR struct nxterm_state_s *)filep->f_priv;
/* Get exclusive access */
ret = nxmutex_lock(&priv->lock);
if (ret < 0)
{
return ret;
}
/* Hide the cursor while we update the display */
nxterm_hidecursor(priv);
/* Loop writing each character to the display */
for (remaining = (ssize_t)buflen; remaining > 0; remaining--)
{
/* Get the next character from the user buffer */
ch = *buffer++;
/* Check if this character is part of a VT100 escape sequence */
do
{
/* Is the character part of a VT100 escape sequnce? */
state = nxterm_vt100(priv, ch);
switch (state)
{
/* Character is not part of a VT100 escape sequence (and no
* characters are buffer.
*/
default:
case VT100_NOT_CONSUMED:
{
/* We can output the character to the window */
nxterm_putc(priv, (uint8_t)ch);
}
break;
/* The full VT100 escape sequence was processed (and the new
* character was consumed)
*/
case VT100_PROCESSED:
/* Character was consumed as part of the VT100 escape processing
* (but the escape sequence is still incomplete.
*/
case VT100_CONSUMED:
{
/* Do nothing... the VT100 logic owns the character */
}
break;
/* Invalid/unsupported character in escape sequence */
case VT100_ABORT:
{
int i;
/* Add the first unhandled character to the window */
nxterm_putc(priv, (uint8_t)priv->seq[0]);
/* Move all buffer characters down one */
for (i = 1; i < priv->nseq; i++)
{
priv->seq[i - 1] = priv->seq[i];
}
priv->nseq--;
/* Then loop again and check if what remains is part of a
* VT100 escape sequence. We could speed this up by
* checking if priv->seq[0] == ASCII_ESC.
*/
}
break;
}
}
while (state == VT100_ABORT);
}
/* Show the cursor at its new position */
nxterm_showcursor(priv);
nxmutex_unlock(&priv->lock);
return (ssize_t)buflen;
}
/****************************************************************************
* Name: nxterm_ioctl
****************************************************************************/
static int nxterm_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
/* NOTE: We don't need driver context here because the NXTERM handle
* provided within each of the NXTERM IOCTL command data. Mutual
* exclusion is similar managed by the IOCTL cmmand handler.
*
* This permits the IOCTL to be called in abnormal context (such as
* from boardctl())
*/
return nxterm_ioctl_tap(cmd, arg);
}
/****************************************************************************
* Name: nxterm_unlink
****************************************************************************/
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
static int nxterm_unlink(FAR struct inode *inode)
{
FAR struct nxterm_state_s *priv;
int ret;
DEBUGASSERT(inode->i_private != NULL);
priv = inode->i_private;
/* Get exclusive access */
ret = nxmutex_lock(&priv->lock);
if (ret < 0)
{
return ret;
}
/* Are there open references to the terminal driver? */
if (priv->orefs > 0)
{
/* Yes.. Just mark the driver unlinked. Resources will be cleaned up
* when the final reference is close.
*/
priv->unlinked = true;
}
else
{
/* No.. Unregister the terminal device now */
nxmutex_unlock(&priv->lock);
nxterm_unregister(priv);
return OK;
}
nxmutex_unlock(&priv->lock);
return OK;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nxterm_ioctl_tap
*
* Description:
* Execute an NXTERM IOCTL command from an external caller.
*
* NOTE: We don't need driver context here because the NXTERM handle
* provided within each of the NXTERM IOCTL command data. Mutual
* exclusion is similar managed by the IOCTL cmmand handler.
*
* This permits the IOCTL to be called in abnormal context (such as
* from boardctl())
*
****************************************************************************/
int nxterm_ioctl_tap(int cmd, uintptr_t arg)
{
int ret;
switch (cmd)
{
/* CMD: NXTERMIOC_NXTERM_REDRAW
* DESCRIPTION: Re-draw a portion of the NX console. This function
* should be called from the appropriate window callback
* logic.
* ARG: A reference readable instance of struct
* nxtermioc_redraw_s
* CONFIGURATION: CONFIG_NXTERM
*/
case NXTERMIOC_NXTERM_REDRAW:
{
FAR struct nxtermioc_redraw_s *redraw =
(FAR struct nxtermioc_redraw_s *)((uintptr_t)arg);
nxterm_redraw(redraw->handle, &redraw->rect, redraw->more);
ret = OK;
}
break;
/* CMD: NXTERMIOC_NXTERM_KBDIN
* DESCRIPTION: Provide NxTerm keyboard input to NX.
* ARG: A reference readable instance of struct
* nxtermioc_kbdin_s
* CONFIGURATION: CONFIG_NXTERM_NXKBDIN
*/
case NXTERMIOC_NXTERM_KBDIN:
{
#ifdef CONFIG_NXTERM_NXKBDIN
FAR struct nxtermioc_kbdin_s *kbdin =
(FAR struct nxtermioc_kbdin_s *)((uintptr_t)arg);
nxterm_kbdin(kbdin->handle, kbdin->buffer, kbdin->buflen);
ret = OK;
#else
ret = -ENOSYS;
#endif
}
break;
/* CMD: NXTERMIOC_NXTERM_RESIZE
* DESCRIPTION: Inform NxTerm keyboard the the size of the window has
* changed
* ARG: A reference readable instance of struct
* nxtermioc_resize_s
* CONFIGURATION: CONFIG_NXTERM
*/
case NXTERMIOC_NXTERM_RESIZE:
{
FAR struct nxtermioc_resize_s *resize =
(FAR struct nxtermioc_resize_s *)((uintptr_t)arg);
ret = nxterm_resize(resize->handle, &resize->size);
}
break;
default:
ret = -ENOTTY;
break;
}
return ret;
}