xref: /freebsd/sys/dev/syscons/scmouse.c (revision cbd30a72ca196976c1c700400ecd424baa1b9c16)
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 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
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
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
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
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
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
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
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
233 sc_remove_mouse_image(scr_stat *scp)
234 {
235     int size;
236     int i;
237 
238     if (ISGRAPHSC(scp))
239 	return;
240 
241     SC_VIDEO_LOCK(scp->sc);
242     (*scp->rndr->draw_mouse)(scp,
243 			     (scp->mouse_oldpos%scp->xsize + scp->xoff)
244 			         * scp->font_width,
245 			     (scp->mouse_oldpos/scp->xsize + scp->yoff)
246 				 * scp->font_size,
247 			     FALSE);
248     size = scp->xsize*scp->ysize;
249     i = scp->mouse_oldpos;
250     mark_for_update(scp, i);
251     mark_for_update(scp, i);
252     if (i + scp->xsize + 1 < size) {
253 	mark_for_update(scp, i + scp->xsize + 1);
254     } else if (i + scp->xsize < size) {
255 	mark_for_update(scp, i + scp->xsize);
256     } else if (i + 1 < size) {
257 	mark_for_update(scp, i + 1);
258     }
259     scp->status &= ~MOUSE_VISIBLE;
260     SC_VIDEO_UNLOCK(scp->sc);
261 }
262 
263 int
264 sc_inside_cutmark(scr_stat *scp, int pos)
265 {
266     int start;
267     int end;
268 
269     if (scp->mouse_cut_end < 0)
270 	return FALSE;
271     if (scp->mouse_cut_start <= scp->mouse_cut_end) {
272 	start = scp->mouse_cut_start;
273 	end = scp->mouse_cut_end;
274     } else {
275 	start = scp->mouse_cut_end;
276 	end = scp->mouse_cut_start - 1;
277     }
278     return ((start <= pos) && (pos <= end));
279 }
280 
281 void
282 sc_remove_cutmarking(scr_stat *scp)
283 {
284     int s;
285 
286     s = spltty();
287     if (scp->mouse_cut_end >= 0) {
288 	mark_for_update(scp, scp->mouse_cut_start);
289 	mark_for_update(scp, scp->mouse_cut_end);
290     }
291     scp->mouse_cut_start = scp->xsize*scp->ysize;
292     scp->mouse_cut_end = -1;
293     splx(s);
294     scp->status &= ~MOUSE_CUTTING;
295 }
296 
297 void
298 sc_remove_all_cutmarkings(sc_softc_t *sc)
299 {
300     scr_stat *scp;
301     int i;
302 
303     /* delete cut markings in all vtys */
304     for (i = 0; i < sc->vtys; ++i) {
305 	scp = SC_STAT(sc->dev[i]);
306 	if (scp == NULL)
307 	    continue;
308 	sc_remove_cutmarking(scp);
309     }
310 }
311 
312 void
313 sc_remove_all_mouse(sc_softc_t *sc)
314 {
315     scr_stat *scp;
316     int i;
317 
318     for (i = 0; i < sc->vtys; ++i) {
319 	scp = SC_STAT(sc->dev[i]);
320 	if (scp == NULL)
321 	    continue;
322 	if (scp->status & MOUSE_VISIBLE) {
323 	    scp->status &= ~MOUSE_VISIBLE;
324 	    mark_all(scp);
325 	}
326     }
327 }
328 
329 #define IS_SPACE_CHAR(c)	(((c) & 0xff) == ' ')
330 
331 #ifdef SC_CUT_SPACES2TABS
332 #define IS_BLANK_CHAR(c)	(((c) & 0xff) == ' ' || ((c) & 0xff) == '\t')
333 #else
334 #define IS_BLANK_CHAR(c)	IS_SPACE_CHAR(c)
335 #endif /* SC_CUT_SPACES2TABS */
336 
337 #ifdef SC_CUT_SEPCHARS
338 #define IS_SEP_CHAR(c)		(index(SC_CUT_SEPCHARS, (c) & 0xff) != NULL)
339 #else
340 #define IS_SEP_CHAR(c)		IS_SPACE_CHAR(c)
341 #endif /* SC_CUT_SEPCHARS */
342 
343 /* skip spaces to right */
344 static int
345 skip_spc_right(scr_stat *scp, int p)
346 {
347     int c;
348     int i;
349 
350     for (i = p % scp->xsize; i < scp->xsize; ++i) {
351 	c = sc_vtb_getc(&scp->vtb, p);
352 	if (!IS_SPACE_CHAR(c))
353 	    break;
354 	++p;
355     }
356     return i;
357 }
358 
359 /* skip spaces to left */
360 static int
361 skip_spc_left(scr_stat *scp, int p)
362 {
363     int c;
364     int i;
365 
366     for (i = p-- % scp->xsize - 1; i >= 0; --i) {
367 	c = sc_vtb_getc(&scp->vtb, p);
368 	if (!IS_SPACE_CHAR(c))
369 	    break;
370 	--p;
371     }
372     return i;
373 }
374 
375 static void
376 mouse_do_cut(scr_stat *scp, int from, int to)
377 {
378     int blank;
379     int i;
380     int leadspaces;
381     int p;
382     int s;
383 
384     for (p = from, i = blank = leadspaces = 0; p <= to; ++p) {
385 	cut_buffer[i] = sc_vtb_getc(&scp->vtb, p);
386 	/* Be prepared that sc_vtb_getc() can return '\0' */
387 	if (cut_buffer[i] == '\0')
388 	    cut_buffer[i] = ' ';
389 #ifdef SC_CUT_SPACES2TABS
390 	if (leadspaces != -1) {
391 	    if (IS_SPACE_CHAR(cut_buffer[i])) {
392 		leadspaces++;
393 		/* Check that we are at tabstop position */
394 		if ((p % scp->xsize) % 8 == 7) {
395 		    i -= leadspaces - 1;
396 		    cut_buffer[i] = '\t';
397 		    leadspaces = 0;
398 		}
399 	    } else {
400 		leadspaces = -1;
401 	    }
402 	}
403 #endif /* SC_CUT_SPACES2TABS */
404 	/* remember the position of the last non-space char */
405 	if (!IS_BLANK_CHAR(cut_buffer[i]))
406 	    blank = i + 1;	/* the first space after the last non-space */
407 	++i;
408 	/* trim trailing blank when crossing lines */
409 	if ((p % scp->xsize) == (scp->xsize - 1)) {
410 	    cut_buffer[blank++] = '\r';
411 	    i = blank;
412 	    leadspaces = 0;
413 	}
414     }
415     cut_buffer[i] = '\0';
416 
417     /* remove the current marking */
418     s = spltty();
419     if (scp->mouse_cut_start <= scp->mouse_cut_end) {
420 	mark_for_update(scp, scp->mouse_cut_start);
421 	mark_for_update(scp, scp->mouse_cut_end);
422     } else if (scp->mouse_cut_end >= 0) {
423 	mark_for_update(scp, scp->mouse_cut_end);
424 	mark_for_update(scp, scp->mouse_cut_start);
425     }
426 
427     /* mark the new region */
428     scp->mouse_cut_start = from;
429     scp->mouse_cut_end = to;
430     mark_for_update(scp, from);
431     mark_for_update(scp, to);
432     splx(s);
433 }
434 
435 /* copy marked region to the cut buffer */
436 static void
437 mouse_cut(scr_stat *scp)
438 {
439     int start;
440     int end;
441     int from;
442     int to;
443     int c;
444     int p;
445     int s;
446     int i;
447 
448     start = scp->mouse_cut_start;
449     end = scp->mouse_cut_end;
450     if (scp->mouse_pos >= start) {
451 	from = start;
452 	to = end = scp->mouse_pos;
453     } else {
454 	from = end = scp->mouse_pos;
455 	to = start - 1;
456     }
457     p = to;
458     for (i = p % scp->xsize; i < scp->xsize; ++i) {
459 	c = sc_vtb_getc(&scp->vtb, p);
460 	if (!IS_SPACE_CHAR(c))
461 	    break;
462 	++p;
463     }
464     /* if there is nothing but blank chars, trim them, but mark towards eol */
465     if (i == scp->xsize) {
466 	if (end >= start)
467 	    to = end = p - 1;
468 	else
469 	    to = start = p;
470     }
471     mouse_do_cut(scp, from, to);
472     s = spltty();
473     scp->mouse_cut_start = start;
474     scp->mouse_cut_end = end;
475     splx(s);
476 }
477 
478 /* a mouse button is pressed, start cut operation */
479 static void
480 mouse_cut_start(scr_stat *scp)
481 {
482     int i;
483     int s;
484 
485     if (scp->status & MOUSE_VISIBLE) {
486 	sc_remove_all_cutmarkings(scp->sc);
487 	if ((scp->mouse_pos == scp->mouse_cut_start) &&
488 	    (scp->mouse_pos == scp->mouse_cut_end)) {
489 	    cut_buffer[0] = '\0';
490 	    return;
491 	} else if (skip_spc_right(scp, scp->mouse_pos) >= scp->xsize) {
492 	    /* if the pointer is on trailing blank chars, mark towards eol */
493 	    i = skip_spc_left(scp, scp->mouse_pos) + 1;
494 	    s = spltty();
495 	    scp->mouse_cut_start =
496 	        rounddown(scp->mouse_pos, scp->xsize) + i;
497 	    scp->mouse_cut_end =
498 	        (scp->mouse_pos / scp->xsize + 1) * scp->xsize - 1;
499 	    splx(s);
500 	    cut_buffer[0] = '\r';
501 	} else {
502 	    s = spltty();
503 	    scp->mouse_cut_start = scp->mouse_pos;
504 	    scp->mouse_cut_end = scp->mouse_cut_start;
505 	    splx(s);
506 	    cut_buffer[0] = sc_vtb_getc(&scp->vtb, scp->mouse_cut_start);
507 	}
508 	cut_buffer[1] = '\0';
509 	scp->status |= MOUSE_CUTTING;
510     	mark_all(scp);	/* this is probably overkill XXX */
511     }
512 }
513 
514 /* end of cut operation */
515 static void
516 mouse_cut_end(scr_stat *scp)
517 {
518     if (scp->status & MOUSE_VISIBLE)
519 	scp->status &= ~MOUSE_CUTTING;
520 }
521 
522 /* copy a word under the mouse pointer */
523 static void
524 mouse_cut_word(scr_stat *scp)
525 {
526     int start;
527     int end;
528     int sol;
529     int eol;
530     int c;
531     int j;
532     int len;
533 
534     /*
535      * Because we don't have locale information in the kernel,
536      * we only distinguish space char and non-space chars.  Punctuation
537      * chars, symbols and other regular chars are all treated alike
538      * unless user specified SC_CUT_SEPCHARS in his kernel config file.
539      */
540     if (scp->status & MOUSE_VISIBLE) {
541 	sol = rounddown(scp->mouse_pos, scp->xsize);
542 	eol = sol + scp->xsize;
543 	c = sc_vtb_getc(&scp->vtb, scp->mouse_pos);
544 	if (IS_SEP_CHAR(c)) {
545 	    /* blank space */
546 	    for (j = scp->mouse_pos; j >= sol; --j) {
547 		c = sc_vtb_getc(&scp->vtb, j);
548 	        if (!IS_SEP_CHAR(c))
549 		    break;
550 	    }
551 	    start = ++j;
552 	    for (j = scp->mouse_pos; j < eol; ++j) {
553 		c = sc_vtb_getc(&scp->vtb, j);
554 	        if (!IS_SEP_CHAR(c))
555 		    break;
556 	    }
557 	    end = j - 1;
558 	} else {
559 	    /* non-space word */
560 	    for (j = scp->mouse_pos; j >= sol; --j) {
561 		c = sc_vtb_getc(&scp->vtb, j);
562 	        if (IS_SEP_CHAR(c))
563 		    break;
564 	    }
565 	    start = ++j;
566 	    for (j = scp->mouse_pos; j < eol; ++j) {
567 		c = sc_vtb_getc(&scp->vtb, j);
568 	        if (IS_SEP_CHAR(c))
569 		    break;
570 	    }
571 	    end = j - 1;
572 	}
573 
574 	/* copy the found word */
575 	mouse_do_cut(scp, start, end);
576 	len = strlen(cut_buffer);
577 	if (cut_buffer[len - 1] == '\r')
578 	    cut_buffer[len - 1] = '\0';
579     }
580 }
581 
582 /* copy a line under the mouse pointer */
583 static void
584 mouse_cut_line(scr_stat *scp)
585 {
586     int len;
587     int from;
588 
589     if (scp->status & MOUSE_VISIBLE) {
590 	from = rounddown(scp->mouse_pos, scp->xsize);
591 	mouse_do_cut(scp, from, from + scp->xsize - 1);
592 	len = strlen(cut_buffer);
593 	if (cut_buffer[len - 1] == '\r')
594 	    cut_buffer[len - 1] = '\0';
595 	scp->status |= MOUSE_CUTTING;
596     }
597 }
598 
599 /* extend the marked region to the mouse pointer position */
600 static void
601 mouse_cut_extend(scr_stat *scp)
602 {
603     int start;
604     int end;
605     int s;
606 
607     if ((scp->status & MOUSE_VISIBLE) && !(scp->status & MOUSE_CUTTING)
608 	&& (scp->mouse_cut_end >= 0)) {
609 	if (scp->mouse_cut_start <= scp->mouse_cut_end) {
610 	    start = scp->mouse_cut_start;
611 	    end = scp->mouse_cut_end;
612 	} else {
613 	    start = scp->mouse_cut_end;
614 	    end = scp->mouse_cut_start - 1;
615 	}
616 	s = spltty();
617 	if (scp->mouse_pos > end) {
618 	    scp->mouse_cut_start = start;
619 	    scp->mouse_cut_end = end;
620 	} else if (scp->mouse_pos < start) {
621 	    scp->mouse_cut_start = end + 1;
622 	    scp->mouse_cut_end = start;
623 	} else {
624 	    if (scp->mouse_pos - start > end + 1 - scp->mouse_pos) {
625 		scp->mouse_cut_start = start;
626 		scp->mouse_cut_end = end;
627 	    } else {
628 		scp->mouse_cut_start = end + 1;
629 		scp->mouse_cut_end = start;
630 	    }
631 	}
632 	splx(s);
633 	mouse_cut(scp);
634 	scp->status |= MOUSE_CUTTING;
635     }
636 }
637 
638 /* paste cut buffer contents into the current vty */
639 void
640 sc_mouse_paste(scr_stat *scp)
641 {
642     sc_paste(scp, cut_buffer, strlen(cut_buffer));
643 }
644 
645 #endif /* SC_NO_CUTPASTE */
646 
647 int
648 sc_mouse_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
649 {
650     mouse_info_t *mouse;
651     mouse_info_t buf;
652     scr_stat *cur_scp;
653     scr_stat *scp;
654     struct proc *p1;
655     int s;
656     int f;
657 
658     scp = SC_STAT(tp);
659 
660     switch (cmd) {
661 
662     case CONS_MOUSECTL:		/* control mouse arrow */
663     case OLD_CONS_MOUSECTL:
664 
665 	mouse = (mouse_info_t*)data;
666 
667 	random_harvest_queue(mouse, sizeof(mouse_info_t), 2, RANDOM_MOUSE);
668 
669 	if (cmd == OLD_CONS_MOUSECTL) {
670 	    static u_char swapb[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
671 	    old_mouse_info_t *old_mouse = (old_mouse_info_t *)data;
672 
673 	    mouse = &buf;
674 	    mouse->operation = old_mouse->operation;
675 	    switch (mouse->operation) {
676 	    case MOUSE_MODE:
677 		mouse->u.mode = old_mouse->u.mode;
678 		break;
679 	    case MOUSE_SHOW:
680 	    case MOUSE_HIDE:
681 		break;
682 	    case MOUSE_MOVEABS:
683 	    case MOUSE_MOVEREL:
684 	    case MOUSE_ACTION:
685 		mouse->u.data.x = old_mouse->u.data.x;
686 		mouse->u.data.y = old_mouse->u.data.y;
687 		mouse->u.data.z = 0;
688 		mouse->u.data.buttons = swapb[old_mouse->u.data.buttons & 0x7];
689 		break;
690 	    case MOUSE_GETINFO:
691 		old_mouse->u.data.x = scp->mouse_xpos;
692 		old_mouse->u.data.y = scp->mouse_ypos;
693 		old_mouse->u.data.buttons = swapb[scp->mouse_buttons & 0x7];
694 		return 0;
695 	    default:
696 		return EINVAL;
697 	    }
698 	}
699 
700 	cur_scp = scp->sc->cur_scp;
701 
702 	switch (mouse->operation) {
703 	case MOUSE_MODE:
704 	    if (ISSIGVALID(mouse->u.mode.signal)) {
705 		scp->mouse_signal = mouse->u.mode.signal;
706 		scp->mouse_proc = td->td_proc;
707 		scp->mouse_pid = td->td_proc->p_pid;
708 	    }
709 	    else {
710 		scp->mouse_signal = 0;
711 		scp->mouse_proc = NULL;
712 		scp->mouse_pid = 0;
713 	    }
714 	    return 0;
715 
716 	case MOUSE_SHOW:
717 	    s = spltty();
718 	    if (!(scp->sc->flags & SC_MOUSE_ENABLED)) {
719 		scp->sc->flags |= SC_MOUSE_ENABLED;
720 		cur_scp->status &= ~MOUSE_HIDDEN;
721 		if (!ISGRAPHSC(cur_scp))
722 		    mark_all(cur_scp);
723 	    }
724 	    splx(s);
725 	    return 0;
726 	    /* NOTREACHED */
727 
728 	case MOUSE_HIDE:
729 	    s = spltty();
730 	    if (scp->sc->flags & SC_MOUSE_ENABLED) {
731 		scp->sc->flags &= ~SC_MOUSE_ENABLED;
732 		sc_remove_all_mouse(scp->sc);
733 	    }
734 	    splx(s);
735 	    return 0;
736 	    /* NOTREACHED */
737 
738 	case MOUSE_MOVEABS:
739 	    s = spltty();
740 	    scp->mouse_xpos = mouse->u.data.x;
741 	    scp->mouse_ypos = mouse->u.data.y;
742 	    set_mouse_pos(scp);
743 	    splx(s);
744 	    break;
745 
746 	case MOUSE_MOVEREL:
747 	    s = spltty();
748 	    scp->mouse_xpos += mouse->u.data.x;
749 	    scp->mouse_ypos += mouse->u.data.y;
750 	    set_mouse_pos(scp);
751 	    splx(s);
752 	    break;
753 
754 	case MOUSE_GETINFO:
755 	    mouse->u.data.x = scp->mouse_xpos;
756 	    mouse->u.data.y = scp->mouse_ypos;
757 	    mouse->u.data.z = 0;
758 	    mouse->u.data.buttons = scp->mouse_buttons;
759 	    return 0;
760 
761 	case MOUSE_ACTION:
762 	case MOUSE_MOTION_EVENT:
763 	    /* send out mouse event on /dev/sysmouse */
764 #if 0
765 	    /* this should maybe only be settable from /dev/consolectl SOS */
766 	    if (SC_VTY(tp->t_dev) != SC_CONSOLECTL)
767 		return ENOTTY;
768 #endif
769 	    s = spltty();
770 	    if (mouse->u.data.x != 0 || mouse->u.data.y != 0) {
771 		cur_scp->mouse_xpos += mouse->u.data.x;
772 		cur_scp->mouse_ypos += mouse->u.data.y;
773 		set_mouse_pos(cur_scp);
774 	    }
775 	    f = 0;
776 	    if (mouse->operation == MOUSE_ACTION) {
777 		f = cur_scp->mouse_buttons ^ mouse->u.data.buttons;
778 		cur_scp->mouse_buttons = mouse->u.data.buttons;
779 	    }
780 	    splx(s);
781 
782 	    if (sysmouse_event(mouse) == 0)
783 		return 0;
784 
785 	    /*
786 	     * If any buttons are down or the mouse has moved a lot,
787 	     * stop the screen saver.
788 	     */
789 	    if (((mouse->operation == MOUSE_ACTION) && mouse->u.data.buttons)
790 		|| (mouse->u.data.x*mouse->u.data.x
791 			+ mouse->u.data.y*mouse->u.data.y
792 			>= SC_WAKEUP_DELTA*SC_WAKEUP_DELTA)) {
793 		sc_touch_scrn_saver();
794 	    }
795 
796 	    cur_scp->status &= ~MOUSE_HIDDEN;
797 
798 	    if (cur_scp->mouse_level > 0) {
799 	    	sc_mouse_input(scp, mouse);
800 		break;
801 	    }
802 
803 	    if (cur_scp->mouse_signal && cur_scp->mouse_proc) {
804     		/* has controlling process died? */
805 		if (cur_scp->mouse_proc != (p1 = pfind(cur_scp->mouse_pid))) {
806 		    	cur_scp->mouse_signal = 0;
807 			cur_scp->mouse_proc = NULL;
808 			cur_scp->mouse_pid = 0;
809 			if (p1)
810 			    PROC_UNLOCK(p1);
811 		} else {
812 		    kern_psignal(cur_scp->mouse_proc, cur_scp->mouse_signal);
813 		    PROC_UNLOCK(cur_scp->mouse_proc);
814 		    break;
815 		}
816 	    }
817 
818 #ifndef SC_NO_CUTPASTE
819 	    if (ISGRAPHSC(cur_scp) || (cut_buffer == NULL))
820 		break;
821 
822 	    if ((mouse->operation == MOUSE_ACTION) && f) {
823 		/* process button presses */
824 		if (cur_scp->mouse_buttons & MOUSE_BUTTON1DOWN)
825 		    mouse_cut_start(cur_scp);
826 		else
827 		    mouse_cut_end(cur_scp);
828 		if (cur_scp->mouse_buttons & MOUSE_BUTTON2DOWN ||
829 		    cur_scp->mouse_buttons & MOUSE_BUTTON3DOWN)
830 		    sc_mouse_paste(cur_scp);
831 	    }
832 #endif /* SC_NO_CUTPASTE */
833 	    break;
834 
835 	case MOUSE_BUTTON_EVENT:
836 	    if ((mouse->u.event.id & MOUSE_BUTTONS) == 0)
837 		return EINVAL;
838 	    if (mouse->u.event.value < 0)
839 		return EINVAL;
840 #if 0
841 	    /* this should maybe only be settable from /dev/consolectl SOS */
842 	    if (SC_VTY(tp->t_dev) != SC_CONSOLECTL)
843 		return ENOTTY;
844 #endif
845 	    if (mouse->u.event.value > 0)
846 		cur_scp->mouse_buttons |= mouse->u.event.id;
847 	    else
848 		cur_scp->mouse_buttons &= ~mouse->u.event.id;
849 
850 	    if (sysmouse_event(mouse) == 0)
851 		return 0;
852 
853 	    /* if a button is held down, stop the screen saver */
854 	    if (mouse->u.event.value > 0)
855 		sc_touch_scrn_saver();
856 
857 	    cur_scp->status &= ~MOUSE_HIDDEN;
858 
859 	    if (cur_scp->mouse_level > 0) {
860 	    	sc_mouse_input(scp, mouse);
861 		break;
862 	    }
863 
864 	    if (cur_scp->mouse_signal && cur_scp->mouse_proc) {
865 		if (cur_scp->mouse_proc != (p1 = pfind(cur_scp->mouse_pid))){
866 		    	cur_scp->mouse_signal = 0;
867 			cur_scp->mouse_proc = NULL;
868 			cur_scp->mouse_pid = 0;
869 			if (p1)
870 			    PROC_UNLOCK(p1);
871 		} else {
872 		    kern_psignal(cur_scp->mouse_proc, cur_scp->mouse_signal);
873 		    PROC_UNLOCK(cur_scp->mouse_proc);
874 		    break;
875 		}
876 	    }
877 
878 #ifndef SC_NO_CUTPASTE
879 	    if (ISGRAPHSC(cur_scp) || (cut_buffer == NULL))
880 		break;
881 
882 	    switch (mouse->u.event.id) {
883 	    case MOUSE_BUTTON1DOWN:
884 	        switch (mouse->u.event.value % 4) {
885 		case 0:	/* up */
886 		    mouse_cut_end(cur_scp);
887 		    break;
888 		case 1: /* single click: start cut operation */
889 		    mouse_cut_start(cur_scp);
890 		    break;
891 		case 2:	/* double click: cut a word */
892 		    mouse_cut_word(cur_scp);
893 		    mouse_cut_end(cur_scp);
894 		    break;
895 		case 3:	/* triple click: cut a line */
896 		    mouse_cut_line(cur_scp);
897 		    mouse_cut_end(cur_scp);
898 		    break;
899 		}
900 		break;
901 	    case SC_MOUSE_PASTEBUTTON:
902 	        switch (mouse->u.event.value) {
903 		case 0:	/* up */
904 		    break;
905 		default:
906 		    sc_mouse_paste(cur_scp);
907 		    break;
908 		}
909 		break;
910 	    case SC_MOUSE_EXTENDBUTTON:
911 	        switch (mouse->u.event.value) {
912 		case 0:	/* up */
913 		    if (!(cur_scp->mouse_buttons & MOUSE_BUTTON1DOWN))
914 		        mouse_cut_end(cur_scp);
915 		    break;
916 		default:
917 		    mouse_cut_extend(cur_scp);
918 		    break;
919 		}
920 		break;
921 	    }
922 #endif /* SC_NO_CUTPASTE */
923 	    break;
924 
925 	case MOUSE_MOUSECHAR:
926 	    if (mouse->u.mouse_char < 0) {
927 		mouse->u.mouse_char = scp->sc->mouse_char;
928 	    } else {
929 		if (mouse->u.mouse_char > UCHAR_MAX - 3)
930 		    return EINVAL;
931 		s = spltty();
932 		sc_remove_all_mouse(scp->sc);
933 #ifndef SC_NO_FONT_LOADING
934 		if (ISTEXTSC(cur_scp) && (cur_scp->font != NULL))
935 		    sc_load_font(cur_scp, 0, cur_scp->font_size,
936 				 cur_scp->font_width,
937 				 cur_scp->font + cur_scp->font_size
938 				 * cur_scp->sc->mouse_char,
939 				 cur_scp->sc->mouse_char, 4);
940 #endif
941 		scp->sc->mouse_char = mouse->u.mouse_char;
942 		splx(s);
943 	    }
944 	    break;
945 
946 	default:
947 	    return EINVAL;
948 	}
949 
950 	return 0;
951     }
952 
953     return ENOIOCTL;
954 }
955 
956 #endif /* SC_NO_SYSMOUSE */
957