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