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