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