MASSIVE reorganization of the device operations vector. Change cdevsw
[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.11 2006/07/28 02:17:36 dillon 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 <machine/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             free(p, M_SYSCONS);
88         cut_buffer_size = scp->xsize * scp->ysize + 1;
89         p = malloc(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     end = scp->mouse_cut_end;
306     if (scp->mouse_pos >= start) {
307         from = start;
308         to = end = scp->mouse_pos;
309     } else {
310         from = end = scp->mouse_pos;
311         to = start - 1;
312     }
313     for (p = from, i = blank = 0; p <= to; ++p) {
314         cut_buffer[i] = sc_vtb_getc(&scp->vtb, p);
315         /* remember the position of the last non-space char */
316         if (!IS_SPACE_CHAR(cut_buffer[i++]))
317             blank = i;          /* the first space after the last non-space */
318         /* trim trailing blank when crossing lines */
319         if ((p % scp->xsize) == (scp->xsize - 1)) {
320             cut_buffer[blank] = '\r';
321             i = blank + 1;
322         }
323     }
324     cut_buffer[i] = '\0';
325
326     /* scan towards the end of the last line */
327     --p;
328     for (i = p % scp->xsize; i < scp->xsize; ++i) {
329         c = sc_vtb_getc(&scp->vtb, p);
330         if (!IS_SPACE_CHAR(c))
331             break;
332         ++p;
333     }
334     /* if there is nothing but blank chars, trim them, but mark towards eol */
335     if (i >= scp->xsize) {
336         if (end >= start)
337             to = end = p - 1;
338         else
339             to = start = p;
340         cut_buffer[blank++] = '\r';
341         cut_buffer[blank] = '\0';
342     }
343
344     /* remove the current marking */
345     crit_enter();
346     if (scp->mouse_cut_start <= scp->mouse_cut_end) {
347         mark_for_update(scp, scp->mouse_cut_start);
348         mark_for_update(scp, scp->mouse_cut_end);
349     } else if (scp->mouse_cut_end >= 0) {
350         mark_for_update(scp, scp->mouse_cut_end);
351         mark_for_update(scp, scp->mouse_cut_start);
352     }
353
354     /* mark the new region */
355     scp->mouse_cut_start = start;
356     scp->mouse_cut_end = end;
357     mark_for_update(scp, from);
358     mark_for_update(scp, to);
359     crit_exit();
360 }
361
362 /* a mouse button is pressed, start cut operation */
363 static void
364 mouse_cut_start(scr_stat *scp) 
365 {
366     int i;
367     int j;
368
369     if (scp->status & MOUSE_VISIBLE) {
370         i = scp->mouse_cut_start;
371         j = scp->mouse_cut_end;
372         sc_remove_all_cutmarkings(scp->sc);
373         if (scp->mouse_pos == i && i == j) {
374             cut_buffer[0] = '\0';
375         } else if (skip_spc_right(scp, scp->mouse_pos) >= scp->xsize) {
376             /* if the pointer is on trailing blank chars, mark towards eol */
377             i = skip_spc_left(scp, scp->mouse_pos) + 1;
378             crit_enter();
379             scp->mouse_cut_start =
380                 (scp->mouse_pos / scp->xsize) * scp->xsize + i;
381             scp->mouse_cut_end =
382                 (scp->mouse_pos / scp->xsize + 1) * scp->xsize - 1;
383             crit_exit();
384             cut_buffer[0] = '\r';
385             cut_buffer[1] = '\0';
386             scp->status |= MOUSE_CUTTING;
387         } else {
388             crit_enter();
389             scp->mouse_cut_start = scp->mouse_pos;
390             scp->mouse_cut_end = scp->mouse_cut_start;
391             crit_exit();
392             cut_buffer[0] = sc_vtb_getc(&scp->vtb, scp->mouse_cut_start);
393             cut_buffer[1] = '\0';
394             scp->status |= MOUSE_CUTTING;
395         }
396         mark_all(scp);  /* this is probably overkill XXX */
397     }
398 }
399
400 /* end of cut operation */
401 static void
402 mouse_cut_end(scr_stat *scp) 
403 {
404     if (scp->status & MOUSE_VISIBLE)
405         scp->status &= ~MOUSE_CUTTING;
406 }
407
408 /* copy a word under the mouse pointer */
409 static void
410 mouse_cut_word(scr_stat *scp)
411 {
412     int start;
413     int end;
414     int sol;
415     int eol;
416     int c;
417     int i;
418     int j;
419
420     /*
421      * Because we don't have locale information in the kernel,
422      * we only distinguish space char and non-space chars.  Punctuation
423      * chars, symbols and other regular chars are all treated alike.
424      */
425     if (scp->status & MOUSE_VISIBLE) {
426         /* remove the current cut mark */
427         crit_enter();
428         if (scp->mouse_cut_start <= scp->mouse_cut_end) {
429             mark_for_update(scp, scp->mouse_cut_start);
430             mark_for_update(scp, scp->mouse_cut_end);
431         } else if (scp->mouse_cut_end >= 0) {
432             mark_for_update(scp, scp->mouse_cut_end);
433             mark_for_update(scp, scp->mouse_cut_start);
434         }
435         scp->mouse_cut_start = scp->xsize*scp->ysize;
436         scp->mouse_cut_end = -1;
437         crit_exit();
438
439         sol = (scp->mouse_pos / scp->xsize) * scp->xsize;
440         eol = sol + scp->xsize;
441         c = sc_vtb_getc(&scp->vtb, scp->mouse_pos);
442         if (IS_SPACE_CHAR(c)) {
443             /* blank space */
444             for (j = scp->mouse_pos; j >= sol; --j) {
445                 c = sc_vtb_getc(&scp->vtb, j);
446                 if (!IS_SPACE_CHAR(c))
447                     break;
448             }
449             start = ++j;
450             for (j = scp->mouse_pos; j < eol; ++j) {
451                 c = sc_vtb_getc(&scp->vtb, j);
452                 if (!IS_SPACE_CHAR(c))
453                     break;
454             }
455             end = j - 1;
456         } else {
457             /* non-space word */
458             for (j = scp->mouse_pos; j >= sol; --j) {
459                 c = sc_vtb_getc(&scp->vtb, j);
460                 if (IS_SPACE_CHAR(c))
461                     break;
462             }
463             start = ++j;
464             for (j = scp->mouse_pos; j < eol; ++j) {
465                 c = sc_vtb_getc(&scp->vtb, j);
466                 if (IS_SPACE_CHAR(c))
467                     break;
468             }
469             end = j - 1;
470         }
471
472         /* copy the found word */
473         for (i = 0, j = start; j <= end; ++j)
474             cut_buffer[i++] = sc_vtb_getc(&scp->vtb, j);
475         cut_buffer[i] = '\0';
476         scp->status |= MOUSE_CUTTING;
477
478         /* mark the region */
479         crit_enter();
480         scp->mouse_cut_start = start;
481         scp->mouse_cut_end = end;
482         mark_for_update(scp, start);
483         mark_for_update(scp, end);
484         crit_exit();
485     }
486 }
487
488 /* copy a line under the mouse pointer */
489 static void
490 mouse_cut_line(scr_stat *scp)
491 {
492     int i;
493     int j;
494
495     if (scp->status & MOUSE_VISIBLE) {
496         /* remove the current cut mark */
497         crit_enter();
498         if (scp->mouse_cut_start <= scp->mouse_cut_end) {
499             mark_for_update(scp, scp->mouse_cut_start);
500             mark_for_update(scp, scp->mouse_cut_end);
501         } else if (scp->mouse_cut_end >= 0) {
502             mark_for_update(scp, scp->mouse_cut_end);
503             mark_for_update(scp, scp->mouse_cut_start);
504         }
505
506         /* mark the entire line */
507         scp->mouse_cut_start =
508             (scp->mouse_pos / scp->xsize) * scp->xsize;
509         scp->mouse_cut_end = scp->mouse_cut_start + scp->xsize - 1;
510         mark_for_update(scp, scp->mouse_cut_start);
511         mark_for_update(scp, scp->mouse_cut_end);
512         crit_exit();
513
514         /* copy the line into the cut buffer */
515         for (i = 0, j = scp->mouse_cut_start; j <= scp->mouse_cut_end; ++j)
516             cut_buffer[i++] = sc_vtb_getc(&scp->vtb, j);
517         cut_buffer[i++] = '\r';
518         cut_buffer[i] = '\0';
519         scp->status |= MOUSE_CUTTING;
520     }
521 }
522
523 /* extend the marked region to the mouse pointer position */
524 static void
525 mouse_cut_extend(scr_stat *scp) 
526 {
527     int start;
528     int end;
529
530     if ((scp->status & MOUSE_VISIBLE) && !(scp->status & MOUSE_CUTTING)
531         && (scp->mouse_cut_end >= 0)) {
532         if (scp->mouse_cut_start <= scp->mouse_cut_end) {
533             start = scp->mouse_cut_start;
534             end = scp->mouse_cut_end;
535         } else {
536             start = scp->mouse_cut_end;
537             end = scp->mouse_cut_start - 1;
538         }
539         crit_enter();
540         if (scp->mouse_pos > end) {
541             scp->mouse_cut_start = start;
542             scp->mouse_cut_end = end;
543         } else if (scp->mouse_pos < start) {
544             scp->mouse_cut_start = end + 1;
545             scp->mouse_cut_end = start;
546         } else {
547             if (scp->mouse_pos - start > end + 1 - scp->mouse_pos) {
548                 scp->mouse_cut_start = start;
549                 scp->mouse_cut_end = end;
550             } else {
551                 scp->mouse_cut_start = end + 1;
552                 scp->mouse_cut_end = start;
553             }
554         }
555         crit_exit();
556         mouse_cut(scp);
557         scp->status |= MOUSE_CUTTING;
558     }
559 }
560
561 /* paste cut buffer contents into the current vty */
562 static void
563 mouse_paste(scr_stat *scp) 
564 {
565     if (scp->status & MOUSE_VISIBLE)
566         sc_paste(scp, cut_buffer, strlen(cut_buffer));
567 }
568
569 #endif /* SC_NO_CUTPASTE */
570
571 int
572 sc_mouse_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag)
573 {
574     mouse_info_t *mouse;
575     scr_stat *cur_scp;
576     scr_stat *scp;
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                 scp->mouse_signal = mouse->u.mode.signal;
591                 scp->mouse_proc = curproc;
592                 scp->mouse_pid = curproc->p_pid;
593             }
594             else {
595                 scp->mouse_signal = 0;
596                 scp->mouse_proc = NULL;
597                 scp->mouse_pid = 0;
598             }
599             return 0;
600
601         case MOUSE_SHOW:
602             crit_enter();
603             if (!(scp->sc->flags & SC_MOUSE_ENABLED)) {
604                 scp->sc->flags |= SC_MOUSE_ENABLED;
605                 cur_scp->status &= ~MOUSE_HIDDEN;
606                 if (!ISGRAPHSC(cur_scp))
607                     mark_all(cur_scp);
608                 crit_exit();
609                 return 0;
610             } else {
611                 crit_exit();
612                 return EINVAL;
613             }
614             break;
615
616         case MOUSE_HIDE:
617             crit_enter();
618             if (scp->sc->flags & SC_MOUSE_ENABLED) {
619                 scp->sc->flags &= ~SC_MOUSE_ENABLED;
620                 sc_remove_all_mouse(scp->sc);
621                 crit_exit();
622                 return 0;
623             } else {
624                 crit_exit();
625                 return EINVAL;
626             }
627             break;
628
629         case MOUSE_MOVEABS:
630             crit_enter();
631             scp->mouse_xpos = mouse->u.data.x;
632             scp->mouse_ypos = mouse->u.data.y;
633             set_mouse_pos(scp);
634             crit_exit();
635             break;
636
637         case MOUSE_MOVEREL:
638             crit_enter();
639             scp->mouse_xpos += mouse->u.data.x;
640             scp->mouse_ypos += mouse->u.data.y;
641             set_mouse_pos(scp);
642             crit_exit();
643             break;
644
645         case MOUSE_GETINFO:
646             mouse->u.data.x = scp->mouse_xpos;
647             mouse->u.data.y = scp->mouse_ypos;
648             mouse->u.data.z = 0;
649             mouse->u.data.buttons = scp->mouse_buttons;
650             return 0;
651
652         case MOUSE_ACTION:
653         case MOUSE_MOTION_EVENT:
654             /* send out mouse event on /dev/sysmouse */
655 #if 0
656             /* this should maybe only be settable from /dev/consolectl SOS */
657             if (SC_VTY(tp->t_dev) != SC_CONSOLECTL)
658                 return ENOTTY;
659 #endif
660             crit_enter();
661             if (mouse->u.data.x != 0 || mouse->u.data.y != 0) {
662                 cur_scp->mouse_xpos += mouse->u.data.x;
663                 cur_scp->mouse_ypos += mouse->u.data.y;
664                 set_mouse_pos(cur_scp);
665             }
666             f = 0;
667             if (mouse->operation == MOUSE_ACTION) {
668                 f = cur_scp->mouse_buttons ^ mouse->u.data.buttons;
669                 cur_scp->mouse_buttons = mouse->u.data.buttons;
670             }
671             crit_exit();
672
673             if (sysmouse_event(mouse) == 0)
674                 return 0;
675
676             /* 
677              * If any buttons are down or the mouse has moved a lot, 
678              * stop the screen saver.
679              */
680             if (((mouse->operation == MOUSE_ACTION) && mouse->u.data.buttons)
681                 || (mouse->u.data.x*mouse->u.data.x
682                         + mouse->u.data.y*mouse->u.data.y
683                         >= SC_WAKEUP_DELTA*SC_WAKEUP_DELTA)) {
684                 sc_touch_scrn_saver();
685             }
686
687             cur_scp->status &= ~MOUSE_HIDDEN;
688
689             if (cur_scp->mouse_signal) {
690                 /* has controlling process died? */
691                 if (cur_scp->mouse_proc && 
692                     (cur_scp->mouse_proc != pfind(cur_scp->mouse_pid))){
693                         cur_scp->mouse_signal = 0;
694                         cur_scp->mouse_proc = NULL;
695                         cur_scp->mouse_pid = 0;
696                 } else {
697                     psignal(cur_scp->mouse_proc, cur_scp->mouse_signal);
698                     break;
699                 }
700             }
701
702             if (ISGRAPHSC(cur_scp) || (cut_buffer == NULL))
703                 break;
704
705 #ifndef SC_NO_CUTPASTE
706             if ((mouse->operation == MOUSE_ACTION) && f) {
707                 /* process button presses */
708                 if (cur_scp->mouse_buttons & MOUSE_BUTTON1DOWN)
709                     mouse_cut_start(cur_scp);
710                 else
711                     mouse_cut_end(cur_scp);
712                 if (cur_scp->mouse_buttons & MOUSE_BUTTON2DOWN ||
713                     cur_scp->mouse_buttons & MOUSE_BUTTON3DOWN)
714                     mouse_paste(cur_scp);
715             }
716 #endif /* SC_NO_CUTPASTE */
717             break;
718
719         case MOUSE_BUTTON_EVENT:
720             if ((mouse->u.event.id & MOUSE_BUTTONS) == 0)
721                 return EINVAL;
722             if (mouse->u.event.value < 0)
723                 return EINVAL;
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             if (mouse->u.event.value > 0)
730                 cur_scp->mouse_buttons |= mouse->u.event.id;
731             else
732                 cur_scp->mouse_buttons &= ~mouse->u.event.id;
733
734             if (sysmouse_event(mouse) == 0)
735                 return 0;
736
737             /* if a button is held down, stop the screen saver */
738             if (mouse->u.event.value > 0)
739                 sc_touch_scrn_saver();
740
741             cur_scp->status &= ~MOUSE_HIDDEN;
742
743             if (cur_scp->mouse_signal) {
744                 if (cur_scp->mouse_proc && 
745                     (cur_scp->mouse_proc != pfind(cur_scp->mouse_pid))){
746                         cur_scp->mouse_signal = 0;
747                         cur_scp->mouse_proc = NULL;
748                         cur_scp->mouse_pid = 0;
749                 } else {
750                     psignal(cur_scp->mouse_proc, cur_scp->mouse_signal);
751                     break;
752                 }
753             }
754
755             if (ISGRAPHSC(cur_scp) || (cut_buffer == NULL))
756                 break;
757
758 #ifndef SC_NO_CUTPASTE
759             switch (mouse->u.event.id) {
760             case MOUSE_BUTTON1DOWN:
761                 switch (mouse->u.event.value % 4) {
762                 case 0: /* up */
763                     mouse_cut_end(cur_scp);
764                     break;
765                 case 1: /* single click: start cut operation */
766                     mouse_cut_start(cur_scp);
767                     break;
768                 case 2: /* double click: cut a word */
769                     mouse_cut_word(cur_scp);
770                     mouse_cut_end(cur_scp);
771                     break;
772                 case 3: /* triple click: cut a line */
773                     mouse_cut_line(cur_scp);
774                     mouse_cut_end(cur_scp);
775                     break;
776                 }
777                 break;
778             case SC_MOUSE_PASTEBUTTON:
779                 switch (mouse->u.event.value) {
780                 case 0: /* up */
781                     break;
782                 default:
783                     mouse_paste(cur_scp);
784                     break;
785                 }
786                 break;
787             case SC_MOUSE_EXTENDBUTTON:
788                 switch (mouse->u.event.value) {
789                 case 0: /* up */
790                     if (!(cur_scp->mouse_buttons & MOUSE_BUTTON1DOWN))
791                         mouse_cut_end(cur_scp);
792                     break;
793                 default:
794                     mouse_cut_extend(cur_scp);
795                     break;
796                 }
797                 break;
798             }
799 #endif /* SC_NO_CUTPASTE */
800             break;
801
802         case MOUSE_MOUSECHAR:
803             if (mouse->u.mouse_char < 0) {
804                 mouse->u.mouse_char = scp->sc->mouse_char;
805             } else {
806                 if (mouse->u.mouse_char >= (unsigned char)-1 - 4)
807                     return EINVAL;
808                 crit_enter();
809                 sc_remove_all_mouse(scp->sc);
810 #ifndef SC_NO_FONT_LOADING
811                 if (ISTEXTSC(cur_scp) && (cur_scp->font != NULL))
812                     sc_load_font(cur_scp, 0, cur_scp->font_size, cur_scp->font,
813                                  cur_scp->sc->mouse_char, 4);
814 #endif
815                 scp->sc->mouse_char = mouse->u.mouse_char;
816                 crit_exit();
817             }
818             break;
819
820         default:
821             return EINVAL;
822         }
823
824         return 0;
825     }
826
827     return ENOIOCTL;
828 }
829
830 #endif /* SC_NO_SYSMOUSE */