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