xref: /freebsd-12.1/sys/dev/syscons/scmouse.c (revision 9309fbae)
1 /*-
2  * Copyright (c) 1999 Kazutaka YOKOTA <[email protected]>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer as
10  *    the first lines of this file unmodified.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include "sc.h"
30 #include "opt_syscons.h"
31 
32 #if NSC > 0
33 
34 #include <limits.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/conf.h>
38 #include <sys/signalvar.h>
39 #include <sys/proc.h>
40 #include <sys/tty.h>
41 #include <sys/malloc.h>
42 
43 #include <machine/console.h>
44 #include <machine/mouse.h>
45 
46 #include <dev/syscons/syscons.h>
47 
48 #ifdef SC_TWOBUTTON_MOUSE
49 #define SC_MOUSE_PASTEBUTTON	MOUSE_BUTTON3DOWN	/* right button */
50 #define SC_MOUSE_EXTENDBUTTON	MOUSE_BUTTON2DOWN	/* not really used */
51 #else
52 #define SC_MOUSE_PASTEBUTTON	MOUSE_BUTTON2DOWN	/* middle button */
53 #define SC_MOUSE_EXTENDBUTTON	MOUSE_BUTTON3DOWN	/* right button */
54 #endif /* SC_TWOBUTTON_MOUSE */
55 
56 #define SC_WAKEUP_DELTA		20
57 
58 /* for backward compatibility */
59 #define OLD_CONS_MOUSECTL	_IOWR('c', 10, old_mouse_info_t)
60 
61 typedef struct old_mouse_data {
62     int x;
63     int y;
64     int buttons;
65 } old_mouse_data_t;
66 
67 typedef struct old_mouse_info {
68     int operation;
69     union {
70 	struct old_mouse_data data;
71 	struct mouse_mode mode;
72     } u;
73 } old_mouse_info_t;
74 
75 #ifndef SC_NO_SYSMOUSE
76 
77 /* local variables */
78 static int		cut_buffer_size;
79 static u_char		*cut_buffer;
80 
81 /* local functions */
82 static void set_mouse_pos(scr_stat *scp);
83 #ifndef SC_NO_CUTPASTE
84 static int skip_spc_right(scr_stat *scp, int p);
85 static int skip_spc_left(scr_stat *scp, int p);
86 static void mouse_cut(scr_stat *scp);
87 static void mouse_cut_start(scr_stat *scp);
88 static void mouse_cut_end(scr_stat *scp);
89 static void mouse_cut_word(scr_stat *scp);
90 static void mouse_cut_line(scr_stat *scp);
91 static void mouse_cut_extend(scr_stat *scp);
92 static void mouse_paste(scr_stat *scp);
93 #endif /* SC_NO_CUTPASTE */
94 
95 #ifndef SC_NO_CUTPASTE
96 /* allocate a cut buffer */
97 void
98 sc_alloc_cut_buffer(scr_stat *scp, int wait)
99 {
100     u_char *p;
101 
102     if ((cut_buffer == NULL)
103 	|| (cut_buffer_size < scp->xsize * scp->ysize + 1)) {
104 	p = cut_buffer;
105 	cut_buffer = NULL;
106 	if (p != NULL)
107 	    free(p, M_DEVBUF);
108 	cut_buffer_size = scp->xsize * scp->ysize + 1;
109 	p = (u_char *)malloc(cut_buffer_size,
110 			     M_DEVBUF, (wait) ? M_WAITOK : M_NOWAIT);
111 	if (p != NULL)
112 	    p[0] = '\0';
113 	cut_buffer = p;
114     }
115 }
116 #endif /* SC_NO_CUTPASTE */
117 
118 /* move mouse */
119 void
120 sc_mouse_move(scr_stat *scp, int x, int y)
121 {
122     int s;
123 
124     s = spltty();
125     scp->mouse_xpos = x;
126     scp->mouse_ypos = y;
127     if (ISGRAPHSC(scp))
128 	scp->mouse_pos = scp->mouse_oldpos = 0;
129     else
130 	scp->mouse_pos = scp->mouse_oldpos =
131 	    (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
132     splx(s);
133 }
134 
135 /* adjust mouse position */
136 static void
137 set_mouse_pos(scr_stat *scp)
138 {
139     static int last_xpos = -1, last_ypos = -1;
140 
141     if (scp->mouse_xpos < scp->xoff*8)
142 	scp->mouse_xpos = scp->xoff*8;
143     if (scp->mouse_ypos < scp->yoff*scp->font_size)
144 	scp->mouse_ypos = scp->yoff*scp->font_size;
145     if (ISGRAPHSC(scp)) {
146         if (scp->mouse_xpos > scp->xpixel-1)
147 	    scp->mouse_xpos = scp->xpixel-1;
148         if (scp->mouse_ypos > scp->ypixel-1)
149 	    scp->mouse_ypos = scp->ypixel-1;
150 	return;
151     } else {
152 	if (scp->mouse_xpos > (scp->xsize + scp->xoff)*8 - 1)
153 	    scp->mouse_xpos = (scp->xsize + scp->xoff)*8 - 1;
154 	if (scp->mouse_ypos > (scp->ysize + scp->yoff)*scp->font_size - 1)
155 	    scp->mouse_ypos = (scp->ysize + scp->yoff)*scp->font_size - 1;
156     }
157 
158     if (scp->mouse_xpos != last_xpos || scp->mouse_ypos != last_ypos) {
159 	scp->status |= MOUSE_MOVED;
160     	scp->mouse_pos =
161 	    (scp->mouse_ypos/scp->font_size - scp->yoff)*scp->xsize
162 		+ scp->mouse_xpos/8 - scp->xoff;
163 #ifndef SC_NO_CUTPASTE
164 	if ((scp->status & MOUSE_VISIBLE) && (scp->status & MOUSE_CUTTING))
165 	    mouse_cut(scp);
166 #endif
167     }
168 }
169 
170 #ifndef SC_NO_CUTPASTE
171 
172 void
173 sc_draw_mouse_image(scr_stat *scp)
174 {
175     if (ISGRAPHSC(scp))
176 	return;
177 
178     ++scp->sc->videoio_in_progress;
179     (*scp->rndr->draw_mouse)(scp, scp->mouse_xpos, scp->mouse_ypos, TRUE);
180     scp->mouse_oldpos = scp->mouse_pos;
181     --scp->sc->videoio_in_progress;
182 }
183 
184 void
185 sc_remove_mouse_image(scr_stat *scp)
186 {
187     int size;
188     int i;
189 
190     if (ISGRAPHSC(scp))
191 	return;
192 
193     ++scp->sc->videoio_in_progress;
194     (*scp->rndr->draw_mouse)(scp,
195 			     (scp->mouse_oldpos%scp->xsize + scp->xoff)*8,
196 			     (scp->mouse_oldpos/scp->xsize + scp->yoff)
197 				 * scp->font_size,
198 			     FALSE);
199     size = scp->xsize*scp->ysize;
200     i = scp->mouse_oldpos;
201     mark_for_update(scp, i);
202     mark_for_update(scp, i);
203 #ifndef PC98
204     if (i + scp->xsize + 1 < size) {
205 	mark_for_update(scp, i + scp->xsize + 1);
206     } else if (i + scp->xsize < size) {
207 	mark_for_update(scp, i + scp->xsize);
208     } else if (i + 1 < size) {
209 	mark_for_update(scp, i + 1);
210     }
211 #endif /* PC98 */
212     --scp->sc->videoio_in_progress;
213 }
214 
215 int
216 sc_inside_cutmark(scr_stat *scp, int pos)
217 {
218     int start;
219     int end;
220 
221     if (scp->mouse_cut_end < 0)
222 	return FALSE;
223     if (scp->mouse_cut_start <= scp->mouse_cut_end) {
224 	start = scp->mouse_cut_start;
225 	end = scp->mouse_cut_end;
226     } else {
227 	start = scp->mouse_cut_end;
228 	end = scp->mouse_cut_start - 1;
229     }
230     return ((start <= pos) && (pos <= end));
231 }
232 
233 void
234 sc_remove_cutmarking(scr_stat *scp)
235 {
236     int s;
237 
238     s = spltty();
239     if (scp->mouse_cut_end >= 0) {
240 	mark_for_update(scp, scp->mouse_cut_start);
241 	mark_for_update(scp, scp->mouse_cut_end);
242     }
243     scp->mouse_cut_start = scp->xsize*scp->ysize;
244     scp->mouse_cut_end = -1;
245     splx(s);
246     scp->status &= ~MOUSE_CUTTING;
247 }
248 
249 void
250 sc_remove_all_cutmarkings(sc_softc_t *sc)
251 {
252     scr_stat *scp;
253     int i;
254 
255     /* delete cut markings in all vtys */
256     for (i = 0; i < sc->vtys; ++i) {
257 	scp = SC_STAT(sc->dev[i]);
258 	if (scp == NULL)
259 	    continue;
260 	sc_remove_cutmarking(scp);
261     }
262 }
263 
264 void
265 sc_remove_all_mouse(sc_softc_t *sc)
266 {
267     scr_stat *scp;
268     int i;
269 
270     for (i = 0; i < sc->vtys; ++i) {
271 	scp = SC_STAT(sc->dev[i]);
272 	if (scp == NULL)
273 	    continue;
274 	if (scp->status & MOUSE_VISIBLE) {
275 	    scp->status &= ~MOUSE_VISIBLE;
276 	    mark_all(scp);
277 	}
278     }
279 }
280 
281 #define IS_SPACE_CHAR(c)	(((c) & 0xff) == ' ')
282 
283 /* skip spaces to right */
284 static int
285 skip_spc_right(scr_stat *scp, int p)
286 {
287     int c;
288     int i;
289 
290     for (i = p % scp->xsize; i < scp->xsize; ++i) {
291 	c = sc_vtb_getc(&scp->vtb, p);
292 	if (!IS_SPACE_CHAR(c))
293 	    break;
294 	++p;
295     }
296     return i;
297 }
298 
299 /* skip spaces to left */
300 static int
301 skip_spc_left(scr_stat *scp, int p)
302 {
303     int c;
304     int i;
305 
306     for (i = p-- % scp->xsize - 1; i >= 0; --i) {
307 	c = sc_vtb_getc(&scp->vtb, p);
308 	if (!IS_SPACE_CHAR(c))
309 	    break;
310 	--p;
311     }
312     return i;
313 }
314 
315 /* copy marked region to the cut buffer */
316 static void
317 mouse_cut(scr_stat *scp)
318 {
319     int start;
320     int end;
321     int from;
322     int to;
323     int blank;
324     int c;
325     int p;
326     int s;
327     int i;
328 
329     start = scp->mouse_cut_start;
330     end = scp->mouse_cut_end;
331     if (scp->mouse_pos >= start) {
332 	from = start;
333 	to = end = scp->mouse_pos;
334     } else {
335 	from = end = scp->mouse_pos;
336 	to = start - 1;
337     }
338     for (p = from, i = blank = 0; p <= to; ++p) {
339 	cut_buffer[i] = sc_vtb_getc(&scp->vtb, p);
340 	/* remember the position of the last non-space char */
341 	if (!IS_SPACE_CHAR(cut_buffer[i++]))
342 	    blank = i;		/* the first space after the last non-space */
343 	/* trim trailing blank when crossing lines */
344 	if ((p % scp->xsize) == (scp->xsize - 1)) {
345 	    cut_buffer[blank] = '\r';
346 	    i = blank + 1;
347 	}
348     }
349     cut_buffer[i] = '\0';
350 
351     /* scan towards the end of the last line */
352     --p;
353     for (i = p % scp->xsize; i < scp->xsize; ++i) {
354 	c = sc_vtb_getc(&scp->vtb, p);
355 	if (!IS_SPACE_CHAR(c))
356 	    break;
357 	++p;
358     }
359     /* if there is nothing but blank chars, trim them, but mark towards eol */
360     if (i >= scp->xsize) {
361 	if (end >= start)
362 	    to = end = p - 1;
363 	else
364 	    to = start = p;
365 	cut_buffer[blank++] = '\r';
366 	cut_buffer[blank] = '\0';
367     }
368 
369     /* remove the current marking */
370     s = spltty();
371     if (scp->mouse_cut_start <= scp->mouse_cut_end) {
372 	mark_for_update(scp, scp->mouse_cut_start);
373 	mark_for_update(scp, scp->mouse_cut_end);
374     } else if (scp->mouse_cut_end >= 0) {
375 	mark_for_update(scp, scp->mouse_cut_end);
376 	mark_for_update(scp, scp->mouse_cut_start);
377     }
378 
379     /* mark the new region */
380     scp->mouse_cut_start = start;
381     scp->mouse_cut_end = end;
382     mark_for_update(scp, from);
383     mark_for_update(scp, to);
384     splx(s);
385 }
386 
387 /* a mouse button is pressed, start cut operation */
388 static void
389 mouse_cut_start(scr_stat *scp)
390 {
391     int i;
392     int j;
393     int s;
394 
395     if (scp->status & MOUSE_VISIBLE) {
396 	i = scp->mouse_cut_start;
397 	j = scp->mouse_cut_end;
398 	sc_remove_all_cutmarkings(scp->sc);
399 	if (scp->mouse_pos == i && i == j) {
400 	    cut_buffer[0] = '\0';
401 	} else if (skip_spc_right(scp, scp->mouse_pos) >= scp->xsize) {
402 	    /* if the pointer is on trailing blank chars, mark towards eol */
403 	    i = skip_spc_left(scp, scp->mouse_pos) + 1;
404 	    s = spltty();
405 	    scp->mouse_cut_start =
406 	        (scp->mouse_pos / scp->xsize) * scp->xsize + i;
407 	    scp->mouse_cut_end =
408 	        (scp->mouse_pos / scp->xsize + 1) * scp->xsize - 1;
409 	    splx(s);
410 	    cut_buffer[0] = '\r';
411 	    cut_buffer[1] = '\0';
412 	    scp->status |= MOUSE_CUTTING;
413 	} else {
414 	    s = spltty();
415 	    scp->mouse_cut_start = scp->mouse_pos;
416 	    scp->mouse_cut_end = scp->mouse_cut_start;
417 	    splx(s);
418 	    cut_buffer[0] = sc_vtb_getc(&scp->vtb, scp->mouse_cut_start);
419 	    cut_buffer[1] = '\0';
420 	    scp->status |= MOUSE_CUTTING;
421 	}
422     	mark_all(scp);	/* this is probably overkill XXX */
423     }
424 }
425 
426 /* end of cut operation */
427 static void
428 mouse_cut_end(scr_stat *scp)
429 {
430     if (scp->status & MOUSE_VISIBLE)
431 	scp->status &= ~MOUSE_CUTTING;
432 }
433 
434 /* copy a word under the mouse pointer */
435 static void
436 mouse_cut_word(scr_stat *scp)
437 {
438     int start;
439     int end;
440     int sol;
441     int eol;
442     int c;
443     int s;
444     int i;
445     int j;
446 
447     /*
448      * Because we don't have locale information in the kernel,
449      * we only distinguish space char and non-space chars.  Punctuation
450      * chars, symbols and other regular chars are all treated alike.
451      */
452     if (scp->status & MOUSE_VISIBLE) {
453 	/* remove the current cut mark */
454 	s = spltty();
455 	if (scp->mouse_cut_start <= scp->mouse_cut_end) {
456 	    mark_for_update(scp, scp->mouse_cut_start);
457 	    mark_for_update(scp, scp->mouse_cut_end);
458 	} else if (scp->mouse_cut_end >= 0) {
459 	    mark_for_update(scp, scp->mouse_cut_end);
460 	    mark_for_update(scp, scp->mouse_cut_start);
461 	}
462 	scp->mouse_cut_start = scp->xsize*scp->ysize;
463 	scp->mouse_cut_end = -1;
464 	splx(s);
465 
466 	sol = (scp->mouse_pos / scp->xsize) * scp->xsize;
467 	eol = sol + scp->xsize;
468 	c = sc_vtb_getc(&scp->vtb, scp->mouse_pos);
469 	if (IS_SPACE_CHAR(c)) {
470 	    /* blank space */
471 	    for (j = scp->mouse_pos; j >= sol; --j) {
472 		c = sc_vtb_getc(&scp->vtb, j);
473 	        if (!IS_SPACE_CHAR(c))
474 		    break;
475 	    }
476 	    start = ++j;
477 	    for (j = scp->mouse_pos; j < eol; ++j) {
478 		c = sc_vtb_getc(&scp->vtb, j);
479 	        if (!IS_SPACE_CHAR(c))
480 		    break;
481 	    }
482 	    end = j - 1;
483 	} else {
484 	    /* non-space word */
485 	    for (j = scp->mouse_pos; j >= sol; --j) {
486 		c = sc_vtb_getc(&scp->vtb, j);
487 	        if (IS_SPACE_CHAR(c))
488 		    break;
489 	    }
490 	    start = ++j;
491 	    for (j = scp->mouse_pos; j < eol; ++j) {
492 		c = sc_vtb_getc(&scp->vtb, j);
493 	        if (IS_SPACE_CHAR(c))
494 		    break;
495 	    }
496 	    end = j - 1;
497 	}
498 
499 	/* copy the found word */
500 	for (i = 0, j = start; j <= end; ++j)
501 	    cut_buffer[i++] = sc_vtb_getc(&scp->vtb, j);
502 	cut_buffer[i] = '\0';
503 	scp->status |= MOUSE_CUTTING;
504 
505 	/* mark the region */
506 	s = spltty();
507 	scp->mouse_cut_start = start;
508 	scp->mouse_cut_end = end;
509 	mark_for_update(scp, start);
510 	mark_for_update(scp, end);
511 	splx(s);
512     }
513 }
514 
515 /* copy a line under the mouse pointer */
516 static void
517 mouse_cut_line(scr_stat *scp)
518 {
519     int s;
520     int i;
521     int j;
522 
523     if (scp->status & MOUSE_VISIBLE) {
524 	/* remove the current cut mark */
525 	s = spltty();
526 	if (scp->mouse_cut_start <= scp->mouse_cut_end) {
527 	    mark_for_update(scp, scp->mouse_cut_start);
528 	    mark_for_update(scp, scp->mouse_cut_end);
529 	} else if (scp->mouse_cut_end >= 0) {
530 	    mark_for_update(scp, scp->mouse_cut_end);
531 	    mark_for_update(scp, scp->mouse_cut_start);
532 	}
533 
534 	/* mark the entire line */
535 	scp->mouse_cut_start =
536 	    (scp->mouse_pos / scp->xsize) * scp->xsize;
537 	scp->mouse_cut_end = scp->mouse_cut_start + scp->xsize - 1;
538 	mark_for_update(scp, scp->mouse_cut_start);
539 	mark_for_update(scp, scp->mouse_cut_end);
540 	splx(s);
541 
542 	/* copy the line into the cut buffer */
543 	for (i = 0, j = scp->mouse_cut_start; j <= scp->mouse_cut_end; ++j)
544 	    cut_buffer[i++] = sc_vtb_getc(&scp->vtb, j);
545 	cut_buffer[i++] = '\r';
546 	cut_buffer[i] = '\0';
547 	scp->status |= MOUSE_CUTTING;
548     }
549 }
550 
551 /* extend the marked region to the mouse pointer position */
552 static void
553 mouse_cut_extend(scr_stat *scp)
554 {
555     int start;
556     int end;
557     int s;
558 
559     if ((scp->status & MOUSE_VISIBLE) && !(scp->status & MOUSE_CUTTING)
560 	&& (scp->mouse_cut_end >= 0)) {
561 	if (scp->mouse_cut_start <= scp->mouse_cut_end) {
562 	    start = scp->mouse_cut_start;
563 	    end = scp->mouse_cut_end;
564 	} else {
565 	    start = scp->mouse_cut_end;
566 	    end = scp->mouse_cut_start - 1;
567 	}
568 	s = spltty();
569 	if (scp->mouse_pos > end) {
570 	    scp->mouse_cut_start = start;
571 	    scp->mouse_cut_end = end;
572 	} else if (scp->mouse_pos < start) {
573 	    scp->mouse_cut_start = end + 1;
574 	    scp->mouse_cut_end = start;
575 	} else {
576 	    if (scp->mouse_pos - start > end + 1 - scp->mouse_pos) {
577 		scp->mouse_cut_start = start;
578 		scp->mouse_cut_end = end;
579 	    } else {
580 		scp->mouse_cut_start = end + 1;
581 		scp->mouse_cut_end = start;
582 	    }
583 	}
584 	splx(s);
585 	mouse_cut(scp);
586 	scp->status |= MOUSE_CUTTING;
587     }
588 }
589 
590 /* paste cut buffer contents into the current vty */
591 static void
592 mouse_paste(scr_stat *scp)
593 {
594     if (scp->status & MOUSE_VISIBLE)
595 	sc_paste(scp, cut_buffer, strlen(cut_buffer));
596 }
597 
598 #endif /* SC_NO_CUTPASTE */
599 
600 int
601 sc_mouse_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag,
602 	       struct proc *p)
603 {
604     mouse_info_t *mouse;
605     mouse_info_t buf;
606     scr_stat *cur_scp;
607     scr_stat *scp;
608     int s;
609     int f;
610 
611     scp = SC_STAT(tp->t_dev);
612 
613     switch (cmd) {
614 
615     case CONS_MOUSECTL:		/* control mouse arrow */
616     case OLD_CONS_MOUSECTL:
617 
618 	mouse = (mouse_info_t*)data;
619 	if (cmd == OLD_CONS_MOUSECTL) {
620 	    static u_char swapb[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
621 	    old_mouse_info_t *old_mouse = (old_mouse_info_t *)data;
622 
623 	    mouse = &buf;
624 	    mouse->operation = old_mouse->operation;
625 	    switch (mouse->operation) {
626 	    case MOUSE_MODE:
627 		mouse->u.mode = old_mouse->u.mode;
628 		break;
629 	    case MOUSE_SHOW:
630 	    case MOUSE_HIDE:
631 		break;
632 	    case MOUSE_MOVEABS:
633 	    case MOUSE_MOVEREL:
634 	    case MOUSE_ACTION:
635 		mouse->u.data.x = old_mouse->u.data.x;
636 		mouse->u.data.y = old_mouse->u.data.y;
637 		mouse->u.data.z = 0;
638 		mouse->u.data.buttons = swapb[old_mouse->u.data.buttons & 0x7];
639 		break;
640 	    case MOUSE_GETINFO:
641 		old_mouse->u.data.x = scp->mouse_xpos;
642 		old_mouse->u.data.y = scp->mouse_ypos;
643 		old_mouse->u.data.buttons = swapb[scp->mouse_buttons & 0x7];
644 		break;
645 	    default:
646 		return EINVAL;
647 	    }
648 	}
649 
650 	cur_scp = scp->sc->cur_scp;
651 
652 	switch (mouse->operation) {
653 	case MOUSE_MODE:
654 	    if (ISSIGVALID(mouse->u.mode.signal)) {
655 		scp->mouse_signal = mouse->u.mode.signal;
656 		scp->mouse_proc = p;
657 		scp->mouse_pid = p->p_pid;
658 	    }
659 	    else {
660 		scp->mouse_signal = 0;
661 		scp->mouse_proc = NULL;
662 		scp->mouse_pid = 0;
663 	    }
664 	    return 0;
665 
666 	case MOUSE_SHOW:
667 	    if (!ISMOUSEAVAIL(scp->sc->adp->va_flags))
668 		return EINVAL;
669 	    s = spltty();
670 	    if (!(scp->sc->flags & SC_MOUSE_ENABLED)) {
671 		scp->sc->flags |= SC_MOUSE_ENABLED;
672 		if (!ISGRAPHSC(cur_scp)) {
673 		    cur_scp->status |= MOUSE_VISIBLE;
674 		    mark_all(cur_scp);
675 		}
676 		splx(s);
677 		return 0;
678 	    } else {
679 		splx(s);
680 		return EINVAL;
681 	    }
682 	    break;
683 
684 	case MOUSE_HIDE:
685 	    s = spltty();
686 	    if (scp->sc->flags & SC_MOUSE_ENABLED) {
687 		scp->sc->flags &= ~SC_MOUSE_ENABLED;
688 		sc_remove_all_mouse(scp->sc);
689 		splx(s);
690 		return 0;
691 	    } else {
692 		splx(s);
693 		return EINVAL;
694 	    }
695 	    break;
696 
697 	case MOUSE_MOVEABS:
698 	    s = spltty();
699 	    scp->mouse_xpos = mouse->u.data.x;
700 	    scp->mouse_ypos = mouse->u.data.y;
701 	    set_mouse_pos(scp);
702 	    splx(s);
703 	    break;
704 
705 	case MOUSE_MOVEREL:
706 	    s = spltty();
707 	    scp->mouse_xpos += mouse->u.data.x;
708 	    scp->mouse_ypos += mouse->u.data.y;
709 	    set_mouse_pos(scp);
710 	    splx(s);
711 	    break;
712 
713 	case MOUSE_GETINFO:
714 	    mouse->u.data.x = scp->mouse_xpos;
715 	    mouse->u.data.y = scp->mouse_ypos;
716 	    mouse->u.data.z = 0;
717 	    mouse->u.data.buttons = scp->mouse_buttons;
718 	    return 0;
719 
720 	case MOUSE_ACTION:
721 	case MOUSE_MOTION_EVENT:
722 	    /* send out mouse event on /dev/sysmouse */
723 #if 0
724 	    /* this should maybe only be settable from /dev/consolectl SOS */
725 	    if (SC_VTY(tp->t_dev) != SC_CONSOLECTL)
726 		return ENOTTY;
727 #endif
728 	    s = spltty();
729 	    if (mouse->u.data.x != 0 || mouse->u.data.y != 0) {
730 		cur_scp->mouse_xpos += mouse->u.data.x;
731 		cur_scp->mouse_ypos += mouse->u.data.y;
732 		set_mouse_pos(cur_scp);
733 	    }
734 	    f = 0;
735 	    if (mouse->operation == MOUSE_ACTION) {
736 		f = cur_scp->mouse_buttons ^ mouse->u.data.buttons;
737 		cur_scp->mouse_buttons = mouse->u.data.buttons;
738 	    }
739 	    splx(s);
740 
741 	    if (sysmouse_event(mouse) == 0)
742 		return 0;
743 
744 	    /*
745 	     * If any buttons are down or the mouse has moved a lot,
746 	     * stop the screen saver.
747 	     */
748 	    if (((mouse->operation == MOUSE_ACTION) && mouse->u.data.buttons)
749 		|| (mouse->u.data.x*mouse->u.data.x
750 			+ mouse->u.data.y*mouse->u.data.y
751 			>= SC_WAKEUP_DELTA*SC_WAKEUP_DELTA)) {
752 		sc_touch_scrn_saver();
753 	    }
754 
755 #ifndef SC_NO_CUTPASTE
756 	    if (!ISGRAPHSC(cur_scp) && (cur_scp->sc->flags & SC_MOUSE_ENABLED))
757 		cur_scp->status |= MOUSE_VISIBLE;
758 #endif /* SC_NO_CUTPASTE */
759 
760 	    if (cur_scp->mouse_signal) {
761     		/* has controlling process died? */
762 		if (cur_scp->mouse_proc &&
763 		    (cur_scp->mouse_proc != pfind(cur_scp->mouse_pid))){
764 		    	cur_scp->mouse_signal = 0;
765 			cur_scp->mouse_proc = NULL;
766 			cur_scp->mouse_pid = 0;
767 		} else {
768 		    psignal(cur_scp->mouse_proc, cur_scp->mouse_signal);
769 		    break;
770 		}
771 	    }
772 
773 	    if (ISGRAPHSC(cur_scp) || (cut_buffer == NULL))
774 		break;
775 
776 #ifndef SC_NO_CUTPASTE
777 	    if ((mouse->operation == MOUSE_ACTION) && f) {
778 		/* process button presses */
779 		if (cur_scp->mouse_buttons & MOUSE_BUTTON1DOWN)
780 		    mouse_cut_start(cur_scp);
781 		else
782 		    mouse_cut_end(cur_scp);
783 		if (cur_scp->mouse_buttons & MOUSE_BUTTON2DOWN ||
784 		    cur_scp->mouse_buttons & MOUSE_BUTTON3DOWN)
785 		    mouse_paste(cur_scp);
786 	    }
787 #endif /* SC_NO_CUTPASTE */
788 	    break;
789 
790 	case MOUSE_BUTTON_EVENT:
791 	    if ((mouse->u.event.id & MOUSE_BUTTONS) == 0)
792 		return EINVAL;
793 	    if (mouse->u.event.value < 0)
794 		return EINVAL;
795 #if 0
796 	    /* this should maybe only be settable from /dev/consolectl SOS */
797 	    if (SC_VTY(tp->t_dev) != SC_CONSOLECTL)
798 		return ENOTTY;
799 #endif
800 	    if (mouse->u.event.value > 0)
801 		cur_scp->mouse_buttons |= mouse->u.event.id;
802 	    else
803 		cur_scp->mouse_buttons &= ~mouse->u.event.id;
804 
805 	    if (sysmouse_event(mouse) == 0)
806 		return 0;
807 
808 	    /* if a button is held down, stop the screen saver */
809 	    if (mouse->u.event.value > 0)
810 		sc_touch_scrn_saver();
811 
812 #ifndef SC_NO_CUTPASTE
813 	    if (!ISGRAPHSC(cur_scp) && (cur_scp->sc->flags & SC_MOUSE_ENABLED))
814 		cur_scp->status |= MOUSE_VISIBLE;
815 #endif /* SC_NO_CUTPASTE */
816 
817 	    if (cur_scp->mouse_signal) {
818 		if (cur_scp->mouse_proc &&
819 		    (cur_scp->mouse_proc != pfind(cur_scp->mouse_pid))){
820 		    	cur_scp->mouse_signal = 0;
821 			cur_scp->mouse_proc = NULL;
822 			cur_scp->mouse_pid = 0;
823 		} else {
824 		    psignal(cur_scp->mouse_proc, cur_scp->mouse_signal);
825 		    break;
826 		}
827 	    }
828 
829 	    if (ISGRAPHSC(cur_scp) || (cut_buffer == NULL))
830 		break;
831 
832 #ifndef SC_NO_CUTPASTE
833 	    switch (mouse->u.event.id) {
834 	    case MOUSE_BUTTON1DOWN:
835 	        switch (mouse->u.event.value % 4) {
836 		case 0:	/* up */
837 		    mouse_cut_end(cur_scp);
838 		    break;
839 		case 1: /* single click: start cut operation */
840 		    mouse_cut_start(cur_scp);
841 		    break;
842 		case 2:	/* double click: cut a word */
843 		    mouse_cut_word(cur_scp);
844 		    mouse_cut_end(cur_scp);
845 		    break;
846 		case 3:	/* triple click: cut a line */
847 		    mouse_cut_line(cur_scp);
848 		    mouse_cut_end(cur_scp);
849 		    break;
850 		}
851 		break;
852 	    case SC_MOUSE_PASTEBUTTON:
853 	        switch (mouse->u.event.value) {
854 		case 0:	/* up */
855 		    break;
856 		default:
857 		    mouse_paste(cur_scp);
858 		    break;
859 		}
860 		break;
861 	    case SC_MOUSE_EXTENDBUTTON:
862 	        switch (mouse->u.event.value) {
863 		case 0:	/* up */
864 		    if (!(cur_scp->mouse_buttons & MOUSE_BUTTON1DOWN))
865 		        mouse_cut_end(cur_scp);
866 		    break;
867 		default:
868 		    mouse_cut_extend(cur_scp);
869 		    break;
870 		}
871 		break;
872 	    }
873 #endif /* SC_NO_CUTPASTE */
874 	    break;
875 
876 	case MOUSE_MOUSECHAR:
877 	    if (mouse->u.mouse_char < 0) {
878 		mouse->u.mouse_char = scp->sc->mouse_char;
879 	    } else {
880 		if (mouse->u.mouse_char >= UCHAR_MAX - 4)
881 		    return EINVAL;
882 		s = spltty();
883 		sc_remove_all_mouse(scp->sc);
884 #ifndef SC_NO_FONT_LOADING
885 		if (ISTEXTSC(cur_scp) && (cur_scp->font != NULL))
886 		    sc_load_font(cur_scp, 0, cur_scp->font_size, cur_scp->font,
887 				 cur_scp->sc->mouse_char, 4);
888 #endif
889 		scp->sc->mouse_char = mouse->u.mouse_char;
890 		splx(s);
891 	    }
892 	    break;
893 
894 	default:
895 	    return EINVAL;
896 	}
897 
898 	return 0;
899     }
900 
901     return ENOIOCTL;
902 }
903 
904 #endif /* SC_NO_SYSMOUSE */
905 
906 #endif /* NSC */
907