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