Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / dev / misc / 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: src/sys/dev/syscons/scmouse.c,v 1.12.2.3 2001/07/28 12:51:47 yokota Exp $
27  * $DragonFly: src/sys/dev/misc/syscons/scmouse.c,v 1.14 2008/08/10 19:47:31 swildner Exp $
28  */
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/signalvar.h>
36 #include <sys/proc.h>
37 #include <sys/tty.h>
38 #include <sys/thread2.h>
39
40 #include <machine/console.h>
41 #include <sys/mouse.h>
42
43 #include "syscons.h"
44
45 #ifdef SC_TWOBUTTON_MOUSE
46 #define SC_MOUSE_PASTEBUTTON    MOUSE_BUTTON3DOWN       /* right button */
47 #define SC_MOUSE_EXTENDBUTTON   MOUSE_BUTTON2DOWN       /* not really used */
48 #else
49 #define SC_MOUSE_PASTEBUTTON    MOUSE_BUTTON2DOWN       /* middle button */
50 #define SC_MOUSE_EXTENDBUTTON   MOUSE_BUTTON3DOWN       /* right button */
51 #endif /* SC_TWOBUTTON_MOUSE */
52
53 #define SC_WAKEUP_DELTA         20
54
55 #ifndef SC_NO_SYSMOUSE
56
57 /* local variables */
58 static int              cut_buffer_size;
59 static u_char           *cut_buffer;
60
61 /* local functions */
62 static void set_mouse_pos(scr_stat *scp);
63 #ifndef SC_NO_CUTPASTE
64 static int skip_spc_right(scr_stat *scp, int p);
65 static int skip_spc_left(scr_stat *scp, int p);
66 static void mouse_cut(scr_stat *scp);
67 static void mouse_cut_start(scr_stat *scp);
68 static void mouse_cut_end(scr_stat *scp);
69 static void mouse_cut_word(scr_stat *scp);
70 static void mouse_cut_line(scr_stat *scp);
71 static void mouse_cut_extend(scr_stat *scp);
72 static void mouse_paste(scr_stat *scp);
73 #endif /* SC_NO_CUTPASTE */
74
75 #ifndef SC_NO_CUTPASTE
76 /* allocate a cut buffer */
77 void
78 sc_alloc_cut_buffer(scr_stat *scp, int wait)
79 {
80     u_char *p;
81
82     if ((cut_buffer == NULL)
83         || (cut_buffer_size < scp->xsize * scp->ysize + 1)) {
84         p = cut_buffer;
85         cut_buffer = NULL;
86         if (p != NULL)
87             kfree(p, M_SYSCONS);
88         cut_buffer_size = scp->xsize * scp->ysize + 1;
89         p = kmalloc(cut_buffer_size, M_SYSCONS, (wait) ? M_WAITOK : M_NOWAIT);
90         if (p != NULL)
91             p[0] = '\0';
92         cut_buffer = p;
93     }
94 }
95 #endif /* SC_NO_CUTPASTE */
96
97 /* move mouse */
98 void
99 sc_mouse_move(scr_stat *scp, int x, int y)
100 {
101     crit_enter();
102     scp->mouse_xpos = scp->mouse_oldxpos = x;
103     scp->mouse_ypos = scp->mouse_oldypos = y;
104     if (scp->font_size <= 0)
105         scp->mouse_pos = scp->mouse_oldpos = 0;
106     else
107         scp->mouse_pos = scp->mouse_oldpos = 
108             (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
109     scp->status |= MOUSE_MOVED;
110     crit_exit();
111 }
112
113 /* adjust mouse position */
114 static void
115 set_mouse_pos(scr_stat *scp)
116 {
117     if (scp->mouse_xpos < scp->xoff*8)
118         scp->mouse_xpos = scp->xoff*8;
119     if (scp->mouse_ypos < scp->yoff*scp->font_size)
120         scp->mouse_ypos = scp->yoff*scp->font_size;
121     if (ISGRAPHSC(scp)) {
122         if (scp->mouse_xpos > scp->xpixel-1)
123             scp->mouse_xpos = scp->xpixel-1;
124         if (scp->mouse_ypos > scp->ypixel-1)
125             scp->mouse_ypos = scp->ypixel-1;
126         return;
127     } else {
128         if (scp->mouse_xpos > (scp->xsize + scp->xoff)*8 - 1)
129             scp->mouse_xpos = (scp->xsize + scp->xoff)*8 - 1;
130         if (scp->mouse_ypos > (scp->ysize + scp->yoff)*scp->font_size - 1)
131             scp->mouse_ypos = (scp->ysize + scp->yoff)*scp->font_size - 1;
132     }
133
134     if (scp->mouse_xpos != scp->mouse_oldxpos || scp->mouse_ypos != scp->mouse_oldypos) {
135         scp->status |= MOUSE_MOVED;
136         scp->mouse_pos =
137             (scp->mouse_ypos/scp->font_size - scp->yoff)*scp->xsize 
138                 + scp->mouse_xpos/8 - scp->xoff;
139 #ifndef SC_NO_CUTPASTE
140         if ((scp->status & MOUSE_VISIBLE) && (scp->status & MOUSE_CUTTING))
141             mouse_cut(scp);
142 #endif
143     }
144 }
145
146 #ifndef SC_NO_CUTPASTE
147
148 void
149 sc_draw_mouse_image(scr_stat *scp)
150 {
151     if (ISGRAPHSC(scp))
152         return;
153
154     ++scp->sc->videoio_in_progress;
155     (*scp->rndr->draw_mouse)(scp, scp->mouse_xpos, scp->mouse_ypos, TRUE);
156     scp->mouse_oldpos = scp->mouse_pos;
157     scp->mouse_oldxpos = scp->mouse_xpos;
158     scp->mouse_oldypos = scp->mouse_ypos;
159     scp->status |= MOUSE_VISIBLE;
160     --scp->sc->videoio_in_progress;
161 }
162
163 void
164 sc_remove_mouse_image(scr_stat *scp)
165 {
166     int size;
167     int i;
168
169     if (ISGRAPHSC(scp))
170         return;
171
172     ++scp->sc->videoio_in_progress;
173     (*scp->rndr->draw_mouse)(scp,
174                              (scp->mouse_oldpos%scp->xsize + scp->xoff)*8,
175                              (scp->mouse_oldpos/scp->xsize + scp->yoff)
176                                  * scp->font_size,
177                              FALSE);
178     size = scp->xsize*scp->ysize;
179     i = scp->mouse_oldpos;
180     mark_for_update(scp, i);
181     mark_for_update(scp, i);
182     if (i + scp->xsize + 1 < size) {
183         mark_for_update(scp, i + scp->xsize + 1);
184     } else if (i + scp->xsize < size) {
185         mark_for_update(scp, i + scp->xsize);
186     } else if (i + 1 < size) {
187         mark_for_update(scp, i + 1);
188     }
189     scp->status &= ~MOUSE_VISIBLE;
190     --scp->sc->videoio_in_progress;
191 }
192
193 int
194 sc_inside_cutmark(scr_stat *scp, int pos)
195 {
196     int start;
197     int end;
198
199     if (scp->mouse_cut_end < 0)
200         return FALSE;
201     if (scp->mouse_cut_start <= scp->mouse_cut_end) {
202         start = scp->mouse_cut_start;
203         end = scp->mouse_cut_end;
204     } else {
205         start = scp->mouse_cut_end;
206         end = scp->mouse_cut_start - 1;
207     }
208     return ((start <= pos) && (pos <= end));
209 }
210
211 void
212 sc_remove_cutmarking(scr_stat *scp)
213 {
214     crit_enter();
215     if (scp->mouse_cut_end >= 0) {
216         mark_for_update(scp, scp->mouse_cut_start);
217         mark_for_update(scp, scp->mouse_cut_end);
218     }
219     scp->mouse_cut_start = scp->xsize*scp->ysize;
220     scp->mouse_cut_end = -1;
221     crit_exit();
222     scp->status &= ~MOUSE_CUTTING;
223 }
224
225 void
226 sc_remove_all_cutmarkings(sc_softc_t *sc)
227 {
228     scr_stat *scp;
229     int i;
230
231     /* delete cut markings in all vtys */
232     for (i = 0; i < sc->vtys; ++i) {
233         scp = SC_STAT(sc->dev[i]);
234         if (scp == NULL)
235             continue;
236         sc_remove_cutmarking(scp);
237     }
238 }
239
240 void
241 sc_remove_all_mouse(sc_softc_t *sc)
242 {
243     scr_stat *scp;
244     int i;
245
246     for (i = 0; i < sc->vtys; ++i) {
247         scp = SC_STAT(sc->dev[i]);
248         if (scp == NULL)
249             continue;
250         if (scp->status & MOUSE_VISIBLE) {
251             scp->status &= ~MOUSE_VISIBLE;
252             mark_all(scp);
253         }
254     }
255 }
256
257 #define IS_SPACE_CHAR(c)        (((c) & 0xff) == ' ')
258
259 /* skip spaces to right */
260 static int
261 skip_spc_right(scr_stat *scp, int p)
262 {
263     int c;
264     int i;
265
266     for (i = p % scp->xsize; i < scp->xsize; ++i) {
267         c = sc_vtb_getc(&scp->vtb, p);
268         if (!IS_SPACE_CHAR(c))
269             break;
270         ++p;
271     }
272     return i;
273 }
274
275 /* skip spaces to left */
276 static int
277 skip_spc_left(scr_stat *scp, int p)
278 {
279     int c;
280     int i;
281
282     for (i = p-- % scp->xsize - 1; i >= 0; --i) {
283         c = sc_vtb_getc(&scp->vtb, p);
284         if (!IS_SPACE_CHAR(c))
285             break;
286         --p;
287     }
288     return i;
289 }
290
291 /* copy marked region to the cut buffer */
292 static void
293 mouse_cut(scr_stat *scp)
294 {
295     int start;
296     int end;
297     int from;
298     int to;
299     int blank;
300     int c;
301     int p;
302     int i;
303
304     start = scp->mouse_cut_start;
305     if (scp->mouse_pos >= start) {
306         from = start;
307         to = end = scp->mouse_pos;
308     } else {
309         from = end = scp->mouse_pos;
310         to = start - 1;
311     }
312     for (p = from, i = blank = 0; p <= to; ++p) {
313         cut_buffer[i] = sc_vtb_getc(&scp->vtb, p);
314         /* remember the position of the last non-space char */
315         if (!IS_SPACE_CHAR(cut_buffer[i++]))
316             blank = i;          /* the first space after the last non-space */
317         /* trim trailing blank when crossing lines */
318         if ((p % scp->xsize) == (scp->xsize - 1)) {
319             cut_buffer[blank] = '\r';
320             i = blank + 1;
321         }
322     }
323     cut_buffer[i] = '\0';
324
325     /* scan towards the end of the last line */
326     --p;
327     for (i = p % scp->xsize; i < scp->xsize; ++i) {
328         c = sc_vtb_getc(&scp->vtb, p);
329         if (!IS_SPACE_CHAR(c))
330             break;
331         ++p;
332     }
333     /* if there is nothing but blank chars, trim them, but mark towards eol */
334     if (i >= scp->xsize) {
335         if (end >= start)
336             to = end = p - 1;
337         else
338             to = start = p;
339         cut_buffer[blank++] = '\r';
340         cut_buffer[blank] = '\0';
341     }
342
343     /* remove the current marking */
344     crit_enter();
345     if (scp->mouse_cut_start <= scp->mouse_cut_end) {
346         mark_for_update(scp, scp->mouse_cut_start);
347         mark_for_update(scp, scp->mouse_cut_end);
348     } else if (scp->mouse_cut_end >= 0) {
349         mark_for_update(scp, scp->mouse_cut_end);
350         mark_for_update(scp, scp->mouse_cut_start);
351     }
352
353     /* mark the new region */
354     scp->mouse_cut_start = start;
355     scp->mouse_cut_end = end;
356     mark_for_update(scp, from);
357     mark_for_update(scp, to);
358     crit_exit();
359 }
360
361 /* a mouse button is pressed, start cut operation */
362 static void
363 mouse_cut_start(scr_stat *scp) 
364 {
365     int i;
366     int j;
367
368     if (scp->status & MOUSE_VISIBLE) {
369         i = scp->mouse_cut_start;
370         j = scp->mouse_cut_end;
371         sc_remove_all_cutmarkings(scp->sc);
372         if (scp->mouse_pos == i && i == j) {
373             cut_buffer[0] = '\0';
374         } else if (skip_spc_right(scp, scp->mouse_pos) >= scp->xsize) {
375             /* if the pointer is on trailing blank chars, mark towards eol */
376             i = skip_spc_left(scp, scp->mouse_pos) + 1;
377             crit_enter();
378             scp->mouse_cut_start =
379                 (scp->mouse_pos / scp->xsize) * scp->xsize + i;
380             scp->mouse_cut_end =
381                 (scp->mouse_pos / scp->xsize + 1) * scp->xsize - 1;
382             crit_exit();
383             cut_buffer[0] = '\r';
384             cut_buffer[1] = '\0';
385             scp->status |= MOUSE_CUTTING;
386         } else {
387             crit_enter();
388             scp->mouse_cut_start = scp->mouse_pos;
389             scp->mouse_cut_end = scp->mouse_cut_start;
390             crit_exit();
391             cut_buffer[0] = sc_vtb_getc(&scp->vtb, scp->mouse_cut_start);
392             cut_buffer[1] = '\0';
393             scp->status |= MOUSE_CUTTING;
394         }
395         mark_all(scp);  /* this is probably overkill XXX */
396     }
397 }
398
399 /* end of cut operation */
400 static void
401 mouse_cut_end(scr_stat *scp) 
402 {
403     if (scp->status & MOUSE_VISIBLE)
404         scp->status &= ~MOUSE_CUTTING;
405 }
406
407 /* copy a word under the mouse pointer */
408 static void
409 mouse_cut_word(scr_stat *scp)
410 {
411     int start;
412     int end;
413     int sol;
414     int eol;
415     int c;
416     int i;
417     int j;
418
419     /*
420      * Because we don't have locale information in the kernel,
421      * we only distinguish space char and non-space chars.  Punctuation
422      * chars, symbols and other regular chars are all treated alike.
423      */
424     if (scp->status & MOUSE_VISIBLE) {
425         /* remove the current cut mark */
426         crit_enter();
427         if (scp->mouse_cut_start <= scp->mouse_cut_end) {
428             mark_for_update(scp, scp->mouse_cut_start);
429             mark_for_update(scp, scp->mouse_cut_end);
430         } else if (scp->mouse_cut_end >= 0) {
431             mark_for_update(scp, scp->mouse_cut_end);
432             mark_for_update(scp, scp->mouse_cut_start);
433         }
434         scp->mouse_cut_start = scp->xsize*scp->ysize;
435         scp->mouse_cut_end = -1;
436         crit_exit();
437
438         sol = (scp->mouse_pos / scp->xsize) * scp->xsize;
439         eol = sol + scp->xsize;
440         c = sc_vtb_getc(&scp->vtb, scp->mouse_pos);
441         if (IS_SPACE_CHAR(c)) {
442             /* blank space */
443             for (j = scp->mouse_pos; j >= sol; --j) {
444                 c = sc_vtb_getc(&scp->vtb, j);
445                 if (!IS_SPACE_CHAR(c))
446                     break;
447             }
448             start = ++j;
449             for (j = scp->mouse_pos; j < eol; ++j) {
450                 c = sc_vtb_getc(&scp->vtb, j);
451                 if (!IS_SPACE_CHAR(c))
452                     break;
453             }
454             end = j - 1;
455         } else {
456             /* non-space word */
457             for (j = scp->mouse_pos; j >= sol; --j) {
458                 c = sc_vtb_getc(&scp->vtb, j);
459                 if (IS_SPACE_CHAR(c))
460                     break;
461             }
462             start = ++j;
463             for (j = scp->mouse_pos; j < eol; ++j) {
464                 c = sc_vtb_getc(&scp->vtb, j);
465                 if (IS_SPACE_CHAR(c))
466                     break;
467             }
468             end = j - 1;
469         }
470
471         /* copy the found word */
472         for (i = 0, j = start; j <= end; ++j)
473             cut_buffer[i++] = sc_vtb_getc(&scp->vtb, j);
474         cut_buffer[i] = '\0';
475         scp->status |= MOUSE_CUTTING;
476
477         /* mark the region */
478         crit_enter();
479         scp->mouse_cut_start = start;
480         scp->mouse_cut_end = end;
481         mark_for_update(scp, start);
482         mark_for_update(scp, end);
483         crit_exit();
484     }
485 }
486
487 /* copy a line under the mouse pointer */
488 static void
489 mouse_cut_line(scr_stat *scp)
490 {
491     int i;
492     int j;
493
494     if (scp->status & MOUSE_VISIBLE) {
495         /* remove the current cut mark */
496         crit_enter();
497         if (scp->mouse_cut_start <= scp->mouse_cut_end) {
498             mark_for_update(scp, scp->mouse_cut_start);
499             mark_for_update(scp, scp->mouse_cut_end);
500         } else if (scp->mouse_cut_end >= 0) {
501             mark_for_update(scp, scp->mouse_cut_end);
502             mark_for_update(scp, scp->mouse_cut_start);
503         }
504
505         /* mark the entire line */
506         scp->mouse_cut_start =
507             (scp->mouse_pos / scp->xsize) * scp->xsize;
508         scp->mouse_cut_end = scp->mouse_cut_start + scp->xsize - 1;
509         mark_for_update(scp, scp->mouse_cut_start);
510         mark_for_update(scp, scp->mouse_cut_end);
511         crit_exit();
512
513         /* copy the line into the cut buffer */
514         for (i = 0, j = scp->mouse_cut_start; j <= scp->mouse_cut_end; ++j)
515             cut_buffer[i++] = sc_vtb_getc(&scp->vtb, j);
516         cut_buffer[i++] = '\r';
517         cut_buffer[i] = '\0';
518         scp->status |= MOUSE_CUTTING;
519     }
520 }
521
522 /* extend the marked region to the mouse pointer position */
523 static void
524 mouse_cut_extend(scr_stat *scp) 
525 {
526     int start;
527     int end;
528
529     if ((scp->status & MOUSE_VISIBLE) && !(scp->status & MOUSE_CUTTING)
530         && (scp->mouse_cut_end >= 0)) {
531         if (scp->mouse_cut_start <= scp->mouse_cut_end) {
532             start = scp->mouse_cut_start;
533             end = scp->mouse_cut_end;
534         } else {
535             start = scp->mouse_cut_end;
536             end = scp->mouse_cut_start - 1;
537         }
538         crit_enter();
539         if (scp->mouse_pos > end) {
540             scp->mouse_cut_start = start;
541             scp->mouse_cut_end = end;
542         } else if (scp->mouse_pos < start) {
543             scp->mouse_cut_start = end + 1;
544             scp->mouse_cut_end = start;
545         } else {
546             if (scp->mouse_pos - start > end + 1 - scp->mouse_pos) {
547                 scp->mouse_cut_start = start;
548                 scp->mouse_cut_end = end;
549             } else {
550                 scp->mouse_cut_start = end + 1;
551                 scp->mouse_cut_end = start;
552             }
553         }
554         crit_exit();
555         mouse_cut(scp);
556         scp->status |= MOUSE_CUTTING;
557     }
558 }
559
560 /* paste cut buffer contents into the current vty */
561 static void
562 mouse_paste(scr_stat *scp) 
563 {
564     if (scp->status & MOUSE_VISIBLE)
565         sc_paste(scp, cut_buffer, strlen(cut_buffer));
566 }
567
568 #endif /* SC_NO_CUTPASTE */
569
570 int
571 sc_mouse_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag)
572 {
573     mouse_info_t *mouse;
574     scr_stat *cur_scp;
575     scr_stat *scp;
576     struct proc *oproc;
577     int f;
578
579     scp = SC_STAT(tp->t_dev);
580
581     switch (cmd) {
582
583     case CONS_MOUSECTL:         /* control mouse arrow */
584         mouse = (mouse_info_t*)data;
585         cur_scp = scp->sc->cur_scp;
586
587         switch (mouse->operation) {
588         case MOUSE_MODE:
589             if (ISSIGVALID(mouse->u.mode.signal)) {
590                 oproc = scp->mouse_proc;
591                 scp->mouse_signal = mouse->u.mode.signal;
592                 scp->mouse_proc = curproc;
593                 scp->mouse_pid = curproc->p_pid;
594                 PHOLD(curproc);
595             } else {
596                 oproc = scp->mouse_proc;
597                 scp->mouse_signal = 0;
598                 scp->mouse_proc = NULL;
599                 scp->mouse_pid = 0;
600             }
601             if (oproc) {
602                     PRELE(oproc);
603                     oproc = NULL;
604             }
605             return 0;
606
607         case MOUSE_SHOW:
608             crit_enter();
609             if (!(scp->sc->flags & SC_MOUSE_ENABLED)) {
610                 scp->sc->flags |= SC_MOUSE_ENABLED;
611                 cur_scp->status &= ~MOUSE_HIDDEN;
612                 if (!ISGRAPHSC(cur_scp))
613                     mark_all(cur_scp);
614                 crit_exit();
615                 return 0;
616             } else {
617                 crit_exit();
618                 return EINVAL;
619             }
620             break;
621
622         case MOUSE_HIDE:
623             crit_enter();
624             if (scp->sc->flags & SC_MOUSE_ENABLED) {
625                 scp->sc->flags &= ~SC_MOUSE_ENABLED;
626                 sc_remove_all_mouse(scp->sc);
627                 crit_exit();
628                 return 0;
629             } else {
630                 crit_exit();
631                 return EINVAL;
632             }
633             break;
634
635         case MOUSE_MOVEABS:
636             crit_enter();
637             scp->mouse_xpos = mouse->u.data.x;
638             scp->mouse_ypos = mouse->u.data.y;
639             set_mouse_pos(scp);
640             crit_exit();
641             break;
642
643         case MOUSE_MOVEREL:
644             crit_enter();
645             scp->mouse_xpos += mouse->u.data.x;
646             scp->mouse_ypos += mouse->u.data.y;
647             set_mouse_pos(scp);
648             crit_exit();
649             break;
650
651         case MOUSE_GETINFO:
652             mouse->u.data.x = scp->mouse_xpos;
653             mouse->u.data.y = scp->mouse_ypos;
654             mouse->u.data.z = 0;
655             mouse->u.data.buttons = scp->mouse_buttons;
656             return 0;
657
658         case MOUSE_ACTION:
659         case MOUSE_MOTION_EVENT:
660             /* send out mouse event on /dev/sysmouse */
661 #if 0
662             /* this should maybe only be settable from /dev/consolectl SOS */
663             if (SC_VTY(tp->t_dev) != SC_CONSOLECTL)
664                 return ENOTTY;
665 #endif
666             crit_enter();
667             if (mouse->u.data.x != 0 || mouse->u.data.y != 0) {
668                 cur_scp->mouse_xpos += mouse->u.data.x;
669                 cur_scp->mouse_ypos += mouse->u.data.y;
670                 set_mouse_pos(cur_scp);
671             }
672             f = 0;
673             if (mouse->operation == MOUSE_ACTION) {
674                 f = cur_scp->mouse_buttons ^ mouse->u.data.buttons;
675                 cur_scp->mouse_buttons = mouse->u.data.buttons;
676             }
677             crit_exit();
678
679             if (sysmouse_event(mouse) == 0)
680                 return 0;
681
682             /* 
683              * If any buttons are down or the mouse has moved a lot, 
684              * stop the screen saver.
685              */
686             if (((mouse->operation == MOUSE_ACTION) && mouse->u.data.buttons)
687                 || (mouse->u.data.x*mouse->u.data.x
688                         + mouse->u.data.y*mouse->u.data.y
689                         >= SC_WAKEUP_DELTA*SC_WAKEUP_DELTA)) {
690                 sc_touch_scrn_saver();
691             }
692
693             cur_scp->status &= ~MOUSE_HIDDEN;
694
695             if (cur_scp->mouse_signal) {
696                 /* has controlling process died? */
697                 if (cur_scp->mouse_proc && 
698                     (cur_scp->mouse_proc != pfindn(cur_scp->mouse_pid))){
699                         oproc = cur_scp->mouse_proc;
700                         cur_scp->mouse_signal = 0;
701                         cur_scp->mouse_proc = NULL;
702                         cur_scp->mouse_pid = 0;
703                         if (oproc)
704                                 PRELE(oproc);
705                 } else {
706                     ksignal(cur_scp->mouse_proc, cur_scp->mouse_signal);
707                     break;
708                 }
709             }
710
711             if (ISGRAPHSC(cur_scp) || (cut_buffer == NULL))
712                 break;
713
714 #ifndef SC_NO_CUTPASTE
715             if ((mouse->operation == MOUSE_ACTION) && f) {
716                 /* process button presses */
717                 if (cur_scp->mouse_buttons & MOUSE_BUTTON1DOWN)
718                     mouse_cut_start(cur_scp);
719                 else
720                     mouse_cut_end(cur_scp);
721                 if (cur_scp->mouse_buttons & MOUSE_BUTTON2DOWN ||
722                     cur_scp->mouse_buttons & MOUSE_BUTTON3DOWN)
723                     mouse_paste(cur_scp);
724             }
725 #endif /* SC_NO_CUTPASTE */
726             break;
727
728         case MOUSE_BUTTON_EVENT:
729             if ((mouse->u.event.id & MOUSE_BUTTONS) == 0)
730                 return EINVAL;
731             if (mouse->u.event.value < 0)
732                 return EINVAL;
733 #if 0
734             /* this should maybe only be settable from /dev/consolectl SOS */
735             if (SC_VTY(tp->t_dev) != SC_CONSOLECTL)
736                 return ENOTTY;
737 #endif
738             if (mouse->u.event.value > 0)
739                 cur_scp->mouse_buttons |= mouse->u.event.id;
740             else
741                 cur_scp->mouse_buttons &= ~mouse->u.event.id;
742
743             if (sysmouse_event(mouse) == 0)
744                 return 0;
745
746             /* if a button is held down, stop the screen saver */
747             if (mouse->u.event.value > 0)
748                 sc_touch_scrn_saver();
749
750             cur_scp->status &= ~MOUSE_HIDDEN;
751
752             if (cur_scp->mouse_signal) {
753                 if (cur_scp->mouse_proc && 
754                     (cur_scp->mouse_proc != pfindn(cur_scp->mouse_pid))){
755                         oproc = cur_scp->mouse_proc;
756                         cur_scp->mouse_signal = 0;
757                         cur_scp->mouse_proc = NULL;
758                         cur_scp->mouse_pid = 0;
759                         if (oproc)
760                                 PRELE(oproc);
761                 } else {
762                     ksignal(cur_scp->mouse_proc, cur_scp->mouse_signal);
763                     break;
764                 }
765             }
766
767             if (ISGRAPHSC(cur_scp) || (cut_buffer == NULL))
768                 break;
769
770 #ifndef SC_NO_CUTPASTE
771             switch (mouse->u.event.id) {
772             case MOUSE_BUTTON1DOWN:
773                 switch (mouse->u.event.value % 4) {
774                 case 0: /* up */
775                     mouse_cut_end(cur_scp);
776                     break;
777                 case 1: /* single click: start cut operation */
778                     mouse_cut_start(cur_scp);
779                     break;
780                 case 2: /* double click: cut a word */
781                     mouse_cut_word(cur_scp);
782                     mouse_cut_end(cur_scp);
783                     break;
784                 case 3: /* triple click: cut a line */
785                     mouse_cut_line(cur_scp);
786                     mouse_cut_end(cur_scp);
787                     break;
788                 }
789                 break;
790             case SC_MOUSE_PASTEBUTTON:
791                 switch (mouse->u.event.value) {
792                 case 0: /* up */
793                     break;
794                 default:
795                     mouse_paste(cur_scp);
796                     break;
797                 }
798                 break;
799             case SC_MOUSE_EXTENDBUTTON:
800                 switch (mouse->u.event.value) {
801                 case 0: /* up */
802                     if (!(cur_scp->mouse_buttons & MOUSE_BUTTON1DOWN))
803                         mouse_cut_end(cur_scp);
804                     break;
805                 default:
806                     mouse_cut_extend(cur_scp);
807                     break;
808                 }
809                 break;
810             }
811 #endif /* SC_NO_CUTPASTE */
812             break;
813
814         case MOUSE_MOUSECHAR:
815             if (mouse->u.mouse_char < 0) {
816                 mouse->u.mouse_char = scp->sc->mouse_char;
817             } else {
818                 if (mouse->u.mouse_char >= (unsigned char)-1 - 4)
819                     return EINVAL;
820                 crit_enter();
821                 sc_remove_all_mouse(scp->sc);
822 #ifndef SC_NO_FONT_LOADING
823                 if (ISTEXTSC(cur_scp) && (cur_scp->font != NULL))
824                     sc_load_font(cur_scp, 0, cur_scp->font_size, cur_scp->font,
825                                  cur_scp->sc->mouse_char, 4);
826 #endif
827                 scp->sc->mouse_char = mouse->u.mouse_char;
828                 crit_exit();
829             }
830             break;
831
832         default:
833             return EINVAL;
834         }
835
836         return 0;
837     }
838
839     return ENOIOCTL;
840 }
841
842 #endif /* SC_NO_SYSMOUSE */