xref: /freebsd/sys/dev/syscons/scmouse.c (revision 85732ac8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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;
662 
663     scp = SC_STAT(tp);
664 
665     switch (cmd) {
666 
667     case CONS_MOUSECTL:		/* control mouse arrow */
668     case OLD_CONS_MOUSECTL:
669 
670 	mouse = (mouse_info_t*)data;
671 
672 	random_harvest_queue(mouse, sizeof(mouse_info_t), RANDOM_MOUSE);
673 
674 	if (cmd == OLD_CONS_MOUSECTL) {
675 	    static u_char swapb[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
676 	    old_mouse_info_t *old_mouse = (old_mouse_info_t *)data;
677 
678 	    mouse = &buf;
679 	    mouse->operation = old_mouse->operation;
680 	    switch (mouse->operation) {
681 	    case MOUSE_MODE:
682 		mouse->u.mode = old_mouse->u.mode;
683 		break;
684 	    case MOUSE_SHOW:
685 	    case MOUSE_HIDE:
686 		break;
687 	    case MOUSE_MOVEABS:
688 	    case MOUSE_MOVEREL:
689 	    case MOUSE_ACTION:
690 		mouse->u.data.x = old_mouse->u.data.x;
691 		mouse->u.data.y = old_mouse->u.data.y;
692 		mouse->u.data.z = 0;
693 		mouse->u.data.buttons = swapb[old_mouse->u.data.buttons & 0x7];
694 		break;
695 	    case MOUSE_GETINFO:
696 		old_mouse->u.data.x = scp->mouse_xpos;
697 		old_mouse->u.data.y = scp->mouse_ypos;
698 		old_mouse->u.data.buttons = swapb[scp->mouse_buttons & 0x7];
699 		return 0;
700 	    default:
701 		return EINVAL;
702 	    }
703 	}
704 
705 	cur_scp = scp->sc->cur_scp;
706 
707 	switch (mouse->operation) {
708 	case MOUSE_MODE:
709 	    if (ISSIGVALID(mouse->u.mode.signal)) {
710 		scp->mouse_signal = mouse->u.mode.signal;
711 		scp->mouse_proc = td->td_proc;
712 		scp->mouse_pid = td->td_proc->p_pid;
713 	    }
714 	    else {
715 		scp->mouse_signal = 0;
716 		scp->mouse_proc = NULL;
717 		scp->mouse_pid = 0;
718 	    }
719 	    return 0;
720 
721 	case MOUSE_SHOW:
722 	    s = spltty();
723 	    if (!(scp->sc->flags & SC_MOUSE_ENABLED)) {
724 		scp->sc->flags |= SC_MOUSE_ENABLED;
725 		cur_scp->status &= ~MOUSE_HIDDEN;
726 		if (!ISGRAPHSC(cur_scp))
727 		    mark_all(cur_scp);
728 	    }
729 	    splx(s);
730 	    return 0;
731 	    /* NOTREACHED */
732 
733 	case MOUSE_HIDE:
734 	    s = spltty();
735 	    if (scp->sc->flags & SC_MOUSE_ENABLED) {
736 		scp->sc->flags &= ~SC_MOUSE_ENABLED;
737 		sc_remove_all_mouse(scp->sc);
738 	    }
739 	    splx(s);
740 	    return 0;
741 	    /* NOTREACHED */
742 
743 	case MOUSE_MOVEABS:
744 	    s = spltty();
745 	    scp->mouse_xpos = mouse->u.data.x;
746 	    scp->mouse_ypos = mouse->u.data.y;
747 	    set_mouse_pos(scp);
748 	    splx(s);
749 	    break;
750 
751 	case MOUSE_MOVEREL:
752 	    s = spltty();
753 	    scp->mouse_xpos += mouse->u.data.x;
754 	    scp->mouse_ypos += mouse->u.data.y;
755 	    set_mouse_pos(scp);
756 	    splx(s);
757 	    break;
758 
759 	case MOUSE_GETINFO:
760 	    mouse->u.data.x = scp->mouse_xpos;
761 	    mouse->u.data.y = scp->mouse_ypos;
762 	    mouse->u.data.z = 0;
763 	    mouse->u.data.buttons = scp->mouse_buttons;
764 	    return 0;
765 
766 	case MOUSE_ACTION:
767 	case MOUSE_MOTION_EVENT:
768 	    /* send out mouse event on /dev/sysmouse */
769 #if 0
770 	    /* this should maybe only be settable from /dev/consolectl SOS */
771 	    if (SC_VTY(tp->t_dev) != SC_CONSOLECTL)
772 		return ENOTTY;
773 #endif
774 	    s = spltty();
775 	    if (mouse->u.data.x != 0 || mouse->u.data.y != 0) {
776 		cur_scp->mouse_xpos += mouse->u.data.x;
777 		cur_scp->mouse_ypos += mouse->u.data.y;
778 		set_mouse_pos(cur_scp);
779 	    }
780 	    f = 0;
781 	    if (mouse->operation == MOUSE_ACTION) {
782 		f = cur_scp->mouse_buttons ^ mouse->u.data.buttons;
783 		cur_scp->mouse_buttons = mouse->u.data.buttons;
784 	    }
785 	    splx(s);
786 
787 	    if (sysmouse_event(mouse) == 0)
788 		return 0;
789 
790 	    /*
791 	     * If any buttons are down or the mouse has moved a lot,
792 	     * stop the screen saver.
793 	     */
794 	    if (((mouse->operation == MOUSE_ACTION) && mouse->u.data.buttons)
795 		|| (mouse->u.data.x*mouse->u.data.x
796 			+ mouse->u.data.y*mouse->u.data.y
797 			>= SC_WAKEUP_DELTA*SC_WAKEUP_DELTA)) {
798 		sc_touch_scrn_saver();
799 	    }
800 
801 	    cur_scp->status &= ~MOUSE_HIDDEN;
802 
803 	    if (cur_scp->mouse_level > 0) {
804 	    	sc_mouse_input(scp, mouse);
805 		break;
806 	    }
807 
808 	    if (cur_scp->mouse_signal && cur_scp->mouse_proc) {
809     		/* has controlling process died? */
810 		if (cur_scp->mouse_proc != (p1 = pfind(cur_scp->mouse_pid))) {
811 		    	cur_scp->mouse_signal = 0;
812 			cur_scp->mouse_proc = NULL;
813 			cur_scp->mouse_pid = 0;
814 			if (p1)
815 			    PROC_UNLOCK(p1);
816 		} else {
817 		    kern_psignal(cur_scp->mouse_proc, cur_scp->mouse_signal);
818 		    PROC_UNLOCK(cur_scp->mouse_proc);
819 		    break;
820 		}
821 	    }
822 
823 #ifndef SC_NO_CUTPASTE
824 	    if (ISGRAPHSC(cur_scp) || (cut_buffer == NULL))
825 		break;
826 
827 	    if ((mouse->operation == MOUSE_ACTION) && f) {
828 		/* process button presses */
829 		if (cur_scp->mouse_buttons & MOUSE_BUTTON1DOWN)
830 		    mouse_cut_start(cur_scp);
831 		else
832 		    mouse_cut_end(cur_scp);
833 		if (cur_scp->mouse_buttons & MOUSE_BUTTON2DOWN ||
834 		    cur_scp->mouse_buttons & MOUSE_BUTTON3DOWN)
835 		    sc_mouse_paste(cur_scp);
836 	    }
837 #endif /* SC_NO_CUTPASTE */
838 	    break;
839 
840 	case MOUSE_BUTTON_EVENT:
841 	    if ((mouse->u.event.id & MOUSE_BUTTONS) == 0)
842 		return EINVAL;
843 	    if (mouse->u.event.value < 0)
844 		return EINVAL;
845 #if 0
846 	    /* this should maybe only be settable from /dev/consolectl SOS */
847 	    if (SC_VTY(tp->t_dev) != SC_CONSOLECTL)
848 		return ENOTTY;
849 #endif
850 	    if (mouse->u.event.value > 0)
851 		cur_scp->mouse_buttons |= mouse->u.event.id;
852 	    else
853 		cur_scp->mouse_buttons &= ~mouse->u.event.id;
854 
855 	    if (sysmouse_event(mouse) == 0)
856 		return 0;
857 
858 	    /* if a button is held down, stop the screen saver */
859 	    if (mouse->u.event.value > 0)
860 		sc_touch_scrn_saver();
861 
862 	    cur_scp->status &= ~MOUSE_HIDDEN;
863 
864 	    if (cur_scp->mouse_level > 0) {
865 	    	sc_mouse_input(scp, mouse);
866 		break;
867 	    }
868 
869 	    if (cur_scp->mouse_signal && cur_scp->mouse_proc) {
870 		if (cur_scp->mouse_proc != (p1 = pfind(cur_scp->mouse_pid))){
871 		    	cur_scp->mouse_signal = 0;
872 			cur_scp->mouse_proc = NULL;
873 			cur_scp->mouse_pid = 0;
874 			if (p1)
875 			    PROC_UNLOCK(p1);
876 		} else {
877 		    kern_psignal(cur_scp->mouse_proc, cur_scp->mouse_signal);
878 		    PROC_UNLOCK(cur_scp->mouse_proc);
879 		    break;
880 		}
881 	    }
882 
883 #ifndef SC_NO_CUTPASTE
884 	    if (ISGRAPHSC(cur_scp) || (cut_buffer == NULL))
885 		break;
886 
887 	    switch (mouse->u.event.id) {
888 	    case MOUSE_BUTTON1DOWN:
889 	        switch (mouse->u.event.value % 4) {
890 		case 0:	/* up */
891 		    mouse_cut_end(cur_scp);
892 		    break;
893 		case 1: /* single click: start cut operation */
894 		    mouse_cut_start(cur_scp);
895 		    break;
896 		case 2:	/* double click: cut a word */
897 		    mouse_cut_word(cur_scp);
898 		    mouse_cut_end(cur_scp);
899 		    break;
900 		case 3:	/* triple click: cut a line */
901 		    mouse_cut_line(cur_scp);
902 		    mouse_cut_end(cur_scp);
903 		    break;
904 		}
905 		break;
906 	    case SC_MOUSE_PASTEBUTTON:
907 	        switch (mouse->u.event.value) {
908 		case 0:	/* up */
909 		    break;
910 		default:
911 		    sc_mouse_paste(cur_scp);
912 		    break;
913 		}
914 		break;
915 	    case SC_MOUSE_EXTENDBUTTON:
916 	        switch (mouse->u.event.value) {
917 		case 0:	/* up */
918 		    if (!(cur_scp->mouse_buttons & MOUSE_BUTTON1DOWN))
919 		        mouse_cut_end(cur_scp);
920 		    break;
921 		default:
922 		    mouse_cut_extend(cur_scp);
923 		    break;
924 		}
925 		break;
926 	    }
927 #endif /* SC_NO_CUTPASTE */
928 	    break;
929 
930 	case MOUSE_MOUSECHAR:
931 	    if (mouse->u.mouse_char < 0) {
932 		mouse->u.mouse_char = scp->sc->mouse_char;
933 	    } else {
934 		if (mouse->u.mouse_char > UCHAR_MAX - 3)
935 		    return EINVAL;
936 		s = spltty();
937 		sc_remove_all_mouse(scp->sc);
938 #ifndef SC_NO_FONT_LOADING
939 		if (ISTEXTSC(cur_scp) && (cur_scp->font != NULL))
940 		    sc_load_font(cur_scp, 0, cur_scp->font_size,
941 				 cur_scp->font_width,
942 				 cur_scp->font + cur_scp->font_size
943 				 * cur_scp->sc->mouse_char,
944 				 cur_scp->sc->mouse_char, 4);
945 #endif
946 		scp->sc->mouse_char = mouse->u.mouse_char;
947 		splx(s);
948 	    }
949 	    break;
950 
951 	default:
952 	    return EINVAL;
953 	}
954 
955 	return 0;
956     }
957 
958     return ENOIOCTL;
959 }
960 
961 #endif /* SC_NO_SYSMOUSE */
962