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