2 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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.7 2005/02/13 03:02:25 swildner Exp $
30 #include "opt_syscons.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/signalvar.h>
38 #include <sys/malloc.h>
40 #include <machine/console.h>
41 #include <machine/mouse.h>
45 #ifdef SC_TWOBUTTON_MOUSE
46 #define SC_MOUSE_PASTEBUTTON MOUSE_BUTTON3DOWN /* right button */
47 #define SC_MOUSE_EXTENDBUTTON MOUSE_BUTTON2DOWN /* not really used */
49 #define SC_MOUSE_PASTEBUTTON MOUSE_BUTTON2DOWN /* middle button */
50 #define SC_MOUSE_EXTENDBUTTON MOUSE_BUTTON3DOWN /* right button */
51 #endif /* SC_TWOBUTTON_MOUSE */
53 #define SC_WAKEUP_DELTA 20
55 #ifndef SC_NO_SYSMOUSE
58 static int cut_buffer_size;
59 static u_char *cut_buffer;
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 */
75 #ifndef SC_NO_CUTPASTE
76 /* allocate a cut buffer */
78 sc_alloc_cut_buffer(scr_stat *scp, int wait)
82 if ((cut_buffer == NULL)
83 || (cut_buffer_size < scp->xsize * scp->ysize + 1)) {
88 cut_buffer_size = scp->xsize * scp->ysize + 1;
89 p = malloc(cut_buffer_size, M_DEVBUF, (wait) ? M_WAITOK : M_NOWAIT);
95 #endif /* SC_NO_CUTPASTE */
99 sc_mouse_move(scr_stat *scp, int x, int y)
104 scp->mouse_xpos = scp->mouse_oldxpos = x;
105 scp->mouse_ypos = scp->mouse_oldypos = y;
106 if (scp->font_size <= 0)
107 scp->mouse_pos = scp->mouse_oldpos = 0;
109 scp->mouse_pos = scp->mouse_oldpos =
110 (y/scp->font_size - scp->yoff)*scp->xsize + x/8 - scp->xoff;
111 scp->status |= MOUSE_MOVED;
115 /* adjust mouse position */
117 set_mouse_pos(scr_stat *scp)
119 if (scp->mouse_xpos < scp->xoff*8)
120 scp->mouse_xpos = scp->xoff*8;
121 if (scp->mouse_ypos < scp->yoff*scp->font_size)
122 scp->mouse_ypos = scp->yoff*scp->font_size;
123 if (ISGRAPHSC(scp)) {
124 if (scp->mouse_xpos > scp->xpixel-1)
125 scp->mouse_xpos = scp->xpixel-1;
126 if (scp->mouse_ypos > scp->ypixel-1)
127 scp->mouse_ypos = scp->ypixel-1;
130 if (scp->mouse_xpos > (scp->xsize + scp->xoff)*8 - 1)
131 scp->mouse_xpos = (scp->xsize + scp->xoff)*8 - 1;
132 if (scp->mouse_ypos > (scp->ysize + scp->yoff)*scp->font_size - 1)
133 scp->mouse_ypos = (scp->ysize + scp->yoff)*scp->font_size - 1;
136 if (scp->mouse_xpos != scp->mouse_oldxpos || scp->mouse_ypos != scp->mouse_oldypos) {
137 scp->status |= MOUSE_MOVED;
139 (scp->mouse_ypos/scp->font_size - scp->yoff)*scp->xsize
140 + scp->mouse_xpos/8 - scp->xoff;
141 #ifndef SC_NO_CUTPASTE
142 if ((scp->status & MOUSE_VISIBLE) && (scp->status & MOUSE_CUTTING))
148 #ifndef SC_NO_CUTPASTE
151 sc_draw_mouse_image(scr_stat *scp)
156 ++scp->sc->videoio_in_progress;
157 (*scp->rndr->draw_mouse)(scp, scp->mouse_xpos, scp->mouse_ypos, TRUE);
158 scp->mouse_oldpos = scp->mouse_pos;
159 scp->mouse_oldxpos = scp->mouse_xpos;
160 scp->mouse_oldypos = scp->mouse_ypos;
161 scp->status |= MOUSE_VISIBLE;
162 --scp->sc->videoio_in_progress;
166 sc_remove_mouse_image(scr_stat *scp)
174 ++scp->sc->videoio_in_progress;
175 (*scp->rndr->draw_mouse)(scp,
176 (scp->mouse_oldpos%scp->xsize + scp->xoff)*8,
177 (scp->mouse_oldpos/scp->xsize + scp->yoff)
180 size = scp->xsize*scp->ysize;
181 i = scp->mouse_oldpos;
182 mark_for_update(scp, i);
183 mark_for_update(scp, i);
184 if (i + scp->xsize + 1 < size) {
185 mark_for_update(scp, i + scp->xsize + 1);
186 } else if (i + scp->xsize < size) {
187 mark_for_update(scp, i + scp->xsize);
188 } else if (i + 1 < size) {
189 mark_for_update(scp, i + 1);
191 scp->status &= ~MOUSE_VISIBLE;
192 --scp->sc->videoio_in_progress;
196 sc_inside_cutmark(scr_stat *scp, int pos)
201 if (scp->mouse_cut_end < 0)
203 if (scp->mouse_cut_start <= scp->mouse_cut_end) {
204 start = scp->mouse_cut_start;
205 end = scp->mouse_cut_end;
207 start = scp->mouse_cut_end;
208 end = scp->mouse_cut_start - 1;
210 return ((start <= pos) && (pos <= end));
214 sc_remove_cutmarking(scr_stat *scp)
219 if (scp->mouse_cut_end >= 0) {
220 mark_for_update(scp, scp->mouse_cut_start);
221 mark_for_update(scp, scp->mouse_cut_end);
223 scp->mouse_cut_start = scp->xsize*scp->ysize;
224 scp->mouse_cut_end = -1;
226 scp->status &= ~MOUSE_CUTTING;
230 sc_remove_all_cutmarkings(sc_softc_t *sc)
235 /* delete cut markings in all vtys */
236 for (i = 0; i < sc->vtys; ++i) {
237 scp = SC_STAT(sc->dev[i]);
240 sc_remove_cutmarking(scp);
245 sc_remove_all_mouse(sc_softc_t *sc)
250 for (i = 0; i < sc->vtys; ++i) {
251 scp = SC_STAT(sc->dev[i]);
254 if (scp->status & MOUSE_VISIBLE) {
255 scp->status &= ~MOUSE_VISIBLE;
261 #define IS_SPACE_CHAR(c) (((c) & 0xff) == ' ')
263 /* skip spaces to right */
265 skip_spc_right(scr_stat *scp, int p)
270 for (i = p % scp->xsize; i < scp->xsize; ++i) {
271 c = sc_vtb_getc(&scp->vtb, p);
272 if (!IS_SPACE_CHAR(c))
279 /* skip spaces to left */
281 skip_spc_left(scr_stat *scp, int p)
286 for (i = p-- % scp->xsize - 1; i >= 0; --i) {
287 c = sc_vtb_getc(&scp->vtb, p);
288 if (!IS_SPACE_CHAR(c))
295 /* copy marked region to the cut buffer */
297 mouse_cut(scr_stat *scp)
309 start = scp->mouse_cut_start;
310 end = scp->mouse_cut_end;
311 if (scp->mouse_pos >= start) {
313 to = end = scp->mouse_pos;
315 from = end = scp->mouse_pos;
318 for (p = from, i = blank = 0; p <= to; ++p) {
319 cut_buffer[i] = sc_vtb_getc(&scp->vtb, p);
320 /* remember the position of the last non-space char */
321 if (!IS_SPACE_CHAR(cut_buffer[i++]))
322 blank = i; /* the first space after the last non-space */
323 /* trim trailing blank when crossing lines */
324 if ((p % scp->xsize) == (scp->xsize - 1)) {
325 cut_buffer[blank] = '\r';
329 cut_buffer[i] = '\0';
331 /* scan towards the end of the last line */
333 for (i = p % scp->xsize; i < scp->xsize; ++i) {
334 c = sc_vtb_getc(&scp->vtb, p);
335 if (!IS_SPACE_CHAR(c))
339 /* if there is nothing but blank chars, trim them, but mark towards eol */
340 if (i >= scp->xsize) {
345 cut_buffer[blank++] = '\r';
346 cut_buffer[blank] = '\0';
349 /* remove the current marking */
351 if (scp->mouse_cut_start <= scp->mouse_cut_end) {
352 mark_for_update(scp, scp->mouse_cut_start);
353 mark_for_update(scp, scp->mouse_cut_end);
354 } else if (scp->mouse_cut_end >= 0) {
355 mark_for_update(scp, scp->mouse_cut_end);
356 mark_for_update(scp, scp->mouse_cut_start);
359 /* mark the new region */
360 scp->mouse_cut_start = start;
361 scp->mouse_cut_end = end;
362 mark_for_update(scp, from);
363 mark_for_update(scp, to);
367 /* a mouse button is pressed, start cut operation */
369 mouse_cut_start(scr_stat *scp)
375 if (scp->status & MOUSE_VISIBLE) {
376 i = scp->mouse_cut_start;
377 j = scp->mouse_cut_end;
378 sc_remove_all_cutmarkings(scp->sc);
379 if (scp->mouse_pos == i && i == j) {
380 cut_buffer[0] = '\0';
381 } else if (skip_spc_right(scp, scp->mouse_pos) >= scp->xsize) {
382 /* if the pointer is on trailing blank chars, mark towards eol */
383 i = skip_spc_left(scp, scp->mouse_pos) + 1;
385 scp->mouse_cut_start =
386 (scp->mouse_pos / scp->xsize) * scp->xsize + i;
388 (scp->mouse_pos / scp->xsize + 1) * scp->xsize - 1;
390 cut_buffer[0] = '\r';
391 cut_buffer[1] = '\0';
392 scp->status |= MOUSE_CUTTING;
395 scp->mouse_cut_start = scp->mouse_pos;
396 scp->mouse_cut_end = scp->mouse_cut_start;
398 cut_buffer[0] = sc_vtb_getc(&scp->vtb, scp->mouse_cut_start);
399 cut_buffer[1] = '\0';
400 scp->status |= MOUSE_CUTTING;
402 mark_all(scp); /* this is probably overkill XXX */
406 /* end of cut operation */
408 mouse_cut_end(scr_stat *scp)
410 if (scp->status & MOUSE_VISIBLE)
411 scp->status &= ~MOUSE_CUTTING;
414 /* copy a word under the mouse pointer */
416 mouse_cut_word(scr_stat *scp)
428 * Because we don't have locale information in the kernel,
429 * we only distinguish space char and non-space chars. Punctuation
430 * chars, symbols and other regular chars are all treated alike.
432 if (scp->status & MOUSE_VISIBLE) {
433 /* remove the current cut mark */
435 if (scp->mouse_cut_start <= scp->mouse_cut_end) {
436 mark_for_update(scp, scp->mouse_cut_start);
437 mark_for_update(scp, scp->mouse_cut_end);
438 } else if (scp->mouse_cut_end >= 0) {
439 mark_for_update(scp, scp->mouse_cut_end);
440 mark_for_update(scp, scp->mouse_cut_start);
442 scp->mouse_cut_start = scp->xsize*scp->ysize;
443 scp->mouse_cut_end = -1;
446 sol = (scp->mouse_pos / scp->xsize) * scp->xsize;
447 eol = sol + scp->xsize;
448 c = sc_vtb_getc(&scp->vtb, scp->mouse_pos);
449 if (IS_SPACE_CHAR(c)) {
451 for (j = scp->mouse_pos; j >= sol; --j) {
452 c = sc_vtb_getc(&scp->vtb, j);
453 if (!IS_SPACE_CHAR(c))
457 for (j = scp->mouse_pos; j < eol; ++j) {
458 c = sc_vtb_getc(&scp->vtb, j);
459 if (!IS_SPACE_CHAR(c))
465 for (j = scp->mouse_pos; j >= sol; --j) {
466 c = sc_vtb_getc(&scp->vtb, j);
467 if (IS_SPACE_CHAR(c))
471 for (j = scp->mouse_pos; j < eol; ++j) {
472 c = sc_vtb_getc(&scp->vtb, j);
473 if (IS_SPACE_CHAR(c))
479 /* copy the found word */
480 for (i = 0, j = start; j <= end; ++j)
481 cut_buffer[i++] = sc_vtb_getc(&scp->vtb, j);
482 cut_buffer[i] = '\0';
483 scp->status |= MOUSE_CUTTING;
485 /* mark the region */
487 scp->mouse_cut_start = start;
488 scp->mouse_cut_end = end;
489 mark_for_update(scp, start);
490 mark_for_update(scp, end);
495 /* copy a line under the mouse pointer */
497 mouse_cut_line(scr_stat *scp)
503 if (scp->status & MOUSE_VISIBLE) {
504 /* remove the current cut mark */
506 if (scp->mouse_cut_start <= scp->mouse_cut_end) {
507 mark_for_update(scp, scp->mouse_cut_start);
508 mark_for_update(scp, scp->mouse_cut_end);
509 } else if (scp->mouse_cut_end >= 0) {
510 mark_for_update(scp, scp->mouse_cut_end);
511 mark_for_update(scp, scp->mouse_cut_start);
514 /* mark the entire line */
515 scp->mouse_cut_start =
516 (scp->mouse_pos / scp->xsize) * scp->xsize;
517 scp->mouse_cut_end = scp->mouse_cut_start + scp->xsize - 1;
518 mark_for_update(scp, scp->mouse_cut_start);
519 mark_for_update(scp, scp->mouse_cut_end);
522 /* copy the line into the cut buffer */
523 for (i = 0, j = scp->mouse_cut_start; j <= scp->mouse_cut_end; ++j)
524 cut_buffer[i++] = sc_vtb_getc(&scp->vtb, j);
525 cut_buffer[i++] = '\r';
526 cut_buffer[i] = '\0';
527 scp->status |= MOUSE_CUTTING;
531 /* extend the marked region to the mouse pointer position */
533 mouse_cut_extend(scr_stat *scp)
539 if ((scp->status & MOUSE_VISIBLE) && !(scp->status & MOUSE_CUTTING)
540 && (scp->mouse_cut_end >= 0)) {
541 if (scp->mouse_cut_start <= scp->mouse_cut_end) {
542 start = scp->mouse_cut_start;
543 end = scp->mouse_cut_end;
545 start = scp->mouse_cut_end;
546 end = scp->mouse_cut_start - 1;
549 if (scp->mouse_pos > end) {
550 scp->mouse_cut_start = start;
551 scp->mouse_cut_end = end;
552 } else if (scp->mouse_pos < start) {
553 scp->mouse_cut_start = end + 1;
554 scp->mouse_cut_end = start;
556 if (scp->mouse_pos - start > end + 1 - scp->mouse_pos) {
557 scp->mouse_cut_start = start;
558 scp->mouse_cut_end = end;
560 scp->mouse_cut_start = end + 1;
561 scp->mouse_cut_end = start;
566 scp->status |= MOUSE_CUTTING;
570 /* paste cut buffer contents into the current vty */
572 mouse_paste(scr_stat *scp)
574 if (scp->status & MOUSE_VISIBLE)
575 sc_paste(scp, cut_buffer, strlen(cut_buffer));
578 #endif /* SC_NO_CUTPASTE */
581 sc_mouse_ioctl(struct tty *tp, u_long cmd, caddr_t data, int flag,
590 scp = SC_STAT(tp->t_dev);
594 case CONS_MOUSECTL: /* control mouse arrow */
595 mouse = (mouse_info_t*)data;
596 cur_scp = scp->sc->cur_scp;
598 switch (mouse->operation) {
600 if (ISSIGVALID(mouse->u.mode.signal)) {
601 scp->mouse_signal = mouse->u.mode.signal;
602 scp->mouse_proc = td->td_proc;
603 scp->mouse_pid = td->td_proc->p_pid;
606 scp->mouse_signal = 0;
607 scp->mouse_proc = NULL;
614 if (!(scp->sc->flags & SC_MOUSE_ENABLED)) {
615 scp->sc->flags |= SC_MOUSE_ENABLED;
616 cur_scp->status &= ~MOUSE_HIDDEN;
617 if (!ISGRAPHSC(cur_scp))
629 if (scp->sc->flags & SC_MOUSE_ENABLED) {
630 scp->sc->flags &= ~SC_MOUSE_ENABLED;
631 sc_remove_all_mouse(scp->sc);
642 scp->mouse_xpos = mouse->u.data.x;
643 scp->mouse_ypos = mouse->u.data.y;
650 scp->mouse_xpos += mouse->u.data.x;
651 scp->mouse_ypos += mouse->u.data.y;
657 mouse->u.data.x = scp->mouse_xpos;
658 mouse->u.data.y = scp->mouse_ypos;
660 mouse->u.data.buttons = scp->mouse_buttons;
664 case MOUSE_MOTION_EVENT:
665 /* send out mouse event on /dev/sysmouse */
667 /* this should maybe only be settable from /dev/consolectl SOS */
668 if (SC_VTY(tp->t_dev) != SC_CONSOLECTL)
672 if (mouse->u.data.x != 0 || mouse->u.data.y != 0) {
673 cur_scp->mouse_xpos += mouse->u.data.x;
674 cur_scp->mouse_ypos += mouse->u.data.y;
675 set_mouse_pos(cur_scp);
678 if (mouse->operation == MOUSE_ACTION) {
679 f = cur_scp->mouse_buttons ^ mouse->u.data.buttons;
680 cur_scp->mouse_buttons = mouse->u.data.buttons;
684 if (sysmouse_event(mouse) == 0)
688 * If any buttons are down or the mouse has moved a lot,
689 * stop the screen saver.
691 if (((mouse->operation == MOUSE_ACTION) && mouse->u.data.buttons)
692 || (mouse->u.data.x*mouse->u.data.x
693 + mouse->u.data.y*mouse->u.data.y
694 >= SC_WAKEUP_DELTA*SC_WAKEUP_DELTA)) {
695 sc_touch_scrn_saver();
698 cur_scp->status &= ~MOUSE_HIDDEN;
700 if (cur_scp->mouse_signal) {
701 /* has controlling process died? */
702 if (cur_scp->mouse_proc &&
703 (cur_scp->mouse_proc != pfind(cur_scp->mouse_pid))){
704 cur_scp->mouse_signal = 0;
705 cur_scp->mouse_proc = NULL;
706 cur_scp->mouse_pid = 0;
708 psignal(cur_scp->mouse_proc, cur_scp->mouse_signal);
713 if (ISGRAPHSC(cur_scp) || (cut_buffer == NULL))
716 #ifndef SC_NO_CUTPASTE
717 if ((mouse->operation == MOUSE_ACTION) && f) {
718 /* process button presses */
719 if (cur_scp->mouse_buttons & MOUSE_BUTTON1DOWN)
720 mouse_cut_start(cur_scp);
722 mouse_cut_end(cur_scp);
723 if (cur_scp->mouse_buttons & MOUSE_BUTTON2DOWN ||
724 cur_scp->mouse_buttons & MOUSE_BUTTON3DOWN)
725 mouse_paste(cur_scp);
727 #endif /* SC_NO_CUTPASTE */
730 case MOUSE_BUTTON_EVENT:
731 if ((mouse->u.event.id & MOUSE_BUTTONS) == 0)
733 if (mouse->u.event.value < 0)
736 /* this should maybe only be settable from /dev/consolectl SOS */
737 if (SC_VTY(tp->t_dev) != SC_CONSOLECTL)
740 if (mouse->u.event.value > 0)
741 cur_scp->mouse_buttons |= mouse->u.event.id;
743 cur_scp->mouse_buttons &= ~mouse->u.event.id;
745 if (sysmouse_event(mouse) == 0)
748 /* if a button is held down, stop the screen saver */
749 if (mouse->u.event.value > 0)
750 sc_touch_scrn_saver();
752 cur_scp->status &= ~MOUSE_HIDDEN;
754 if (cur_scp->mouse_signal) {
755 if (cur_scp->mouse_proc &&
756 (cur_scp->mouse_proc != pfind(cur_scp->mouse_pid))){
757 cur_scp->mouse_signal = 0;
758 cur_scp->mouse_proc = NULL;
759 cur_scp->mouse_pid = 0;
761 psignal(cur_scp->mouse_proc, cur_scp->mouse_signal);
766 if (ISGRAPHSC(cur_scp) || (cut_buffer == NULL))
769 #ifndef SC_NO_CUTPASTE
770 switch (mouse->u.event.id) {
771 case MOUSE_BUTTON1DOWN:
772 switch (mouse->u.event.value % 4) {
774 mouse_cut_end(cur_scp);
776 case 1: /* single click: start cut operation */
777 mouse_cut_start(cur_scp);
779 case 2: /* double click: cut a word */
780 mouse_cut_word(cur_scp);
781 mouse_cut_end(cur_scp);
783 case 3: /* triple click: cut a line */
784 mouse_cut_line(cur_scp);
785 mouse_cut_end(cur_scp);
789 case SC_MOUSE_PASTEBUTTON:
790 switch (mouse->u.event.value) {
794 mouse_paste(cur_scp);
798 case SC_MOUSE_EXTENDBUTTON:
799 switch (mouse->u.event.value) {
801 if (!(cur_scp->mouse_buttons & MOUSE_BUTTON1DOWN))
802 mouse_cut_end(cur_scp);
805 mouse_cut_extend(cur_scp);
810 #endif /* SC_NO_CUTPASTE */
813 case MOUSE_MOUSECHAR:
814 if (mouse->u.mouse_char < 0) {
815 mouse->u.mouse_char = scp->sc->mouse_char;
817 if (mouse->u.mouse_char >= (unsigned char)-1 - 4)
820 sc_remove_all_mouse(scp->sc);
821 #ifndef SC_NO_FONT_LOADING
822 if (ISTEXTSC(cur_scp) && (cur_scp->font != NULL))
823 sc_load_font(cur_scp, 0, cur_scp->font_size, cur_scp->font,
824 cur_scp->sc->mouse_char, 4);
826 scp->sc->mouse_char = mouse->u.mouse_char;
841 #endif /* SC_NO_SYSMOUSE */