selection.c revision 1.6
1/* $NetBSD: selection.c,v 1.6 2004/01/05 11:17:14 jmmv Exp $ */ 2 3/* 4 * Copyright (c) 2002, 2003, 2004 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Julio M. Merino Vidal. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. The name authors may not be used to endorse or promote products 16 * derived from this software without specific prior written 17 * permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS 20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33 34#ifndef lint 35__RCSID("$NetBSD: selection.c,v 1.6 2004/01/05 11:17:14 jmmv Exp $"); 36#endif /* not lint */ 37 38#include <sys/ioctl.h> 39#include <sys/time.h> 40#include <sys/types.h> 41#include <sys/tty.h> 42#include <dev/wscons/wsconsio.h> 43 44#include <ctype.h> 45#include <err.h> 46#include <fcntl.h> 47#include <stdio.h> 48#include <stdlib.h> 49#include <string.h> 50#include <unistd.h> 51 52#include "pathnames.h" 53#include "wsmoused.h" 54 55/* ---------------------------------------------------------------------- */ 56 57/* 58 * Public interface exported by the `selection' mode. 59 */ 60 61int selection_startup(struct mouse *m); 62int selection_cleanup(void); 63void selection_wsmouse_event(struct wscons_event); 64void selection_wscons_event(struct wscons_event); 65void selection_poll_timeout(void); 66 67struct mode_bootstrap Selection_Mode = { 68 "selection", 69 selection_startup, 70 selection_cleanup, 71 selection_wsmouse_event, 72 selection_wscons_event, 73 selection_poll_timeout 74}; 75 76/* ---------------------------------------------------------------------- */ 77 78/* 79 * Structures used in this module only. 80 */ 81 82/* The `selarea' structure is used to describe a selection in the screen. 83 It also holds a copy of the selected text. */ 84struct selarea { 85 size_t sa_x1; /* Start column */ 86 size_t sa_y1; /* Start row */ 87 size_t sa_x2; /* End column */ 88 size_t sa_y2; /* End row */ 89 size_t sa_startoff; /* Absolute offset of start position */ 90 size_t sa_endoff; /* Absolute offset of end position */ 91 size_t sa_buflen; /* Length of selected text */ 92 char *sa_buf; /* A copy of the selected text */ 93}; 94 95/* The `selmouse' structure extends the `mouse' structure adding all fields 96 required for this module to work. */ 97struct selmouse { 98 struct mouse *sm_mouse; /* Pointer to parent structure */ 99 100 int sm_ttyfd; /* Active TTY file descriptor */ 101 102 size_t sm_x; /* Mouse pointer column */ 103 size_t sm_y; /* Mouse pointer row */ 104 size_t sm_max_x; /* Maximun column allowed */ 105 size_t sm_max_y; /* Maximun row allowed */ 106 107 size_t sm_slowdown_x; /* X axis slowdown */ 108 size_t sm_slowdown_y; /* Y axis slowdown */ 109 size_t sm_count_x; /* Number of X movements skipped */ 110 size_t sm_count_y; /* Number of Y movements skipped */ 111 112 int sm_visible; /* Whether pointer is visible or not */ 113 int sm_selecting; /* Whether we are selecting or not */ 114 115 int sm_but_select; /* Button number to select an area */ 116 int sm_but_paste; /* Button number to paste buffer */ 117}; 118 119/* ---------------------------------------------------------------------- */ 120 121/* 122 * Global variables. 123 */ 124 125static struct selmouse Selmouse; 126static struct selarea Selarea; 127static int Initialized = 0; 128 129/* ---------------------------------------------------------------------- */ 130 131/* 132 * Prototypes for functions private to this module. 133 */ 134 135static void cursor_hide(void); 136static void cursor_show(void); 137static void open_tty(int); 138static void char_invert(size_t, size_t); 139static void *alloc_sel(size_t); 140static char *fill_buf(char *, size_t, size_t, size_t); 141static size_t row_length(size_t); 142static void selarea_copy_text(void); 143static void selarea_start(void); 144static void selarea_end(void); 145static void selarea_calculate(void); 146static void selarea_hide(void); 147static void selarea_show(void); 148static void selarea_paste(void); 149 150/* ---------------------------------------------------------------------- */ 151 152/* Mode initialization. Reads configuration, checks if the kernel has 153 * support for mouse pointer and opens required files. */ 154int 155selection_startup(struct mouse *m) 156{ 157 int i; 158 struct winsize ws; 159 struct wsdisplay_char ch; 160 struct block *conf; 161 162 if (Initialized) { 163 log_warnx("selection mode already initialized"); 164 return 1; 165 } 166 167 (void)memset(&Selmouse, 0, sizeof(struct selmouse)); 168 Selmouse.sm_mouse = m; 169 170 conf = config_get_mode("selection"); 171 Selmouse.sm_slowdown_x = block_get_propval_int(conf, "slowdown_x", 0); 172 Selmouse.sm_slowdown_y = block_get_propval_int(conf, "slowdown_y", 3); 173 if (block_get_propval_int(conf, "lefthanded", 0)) { 174 Selmouse.sm_but_select = 2; 175 Selmouse.sm_but_paste = 0; 176 } else { 177 Selmouse.sm_but_select = 0; 178 Selmouse.sm_but_paste = 2; 179 } 180 181 /* Get terminal size */ 182 if (ioctl(0, TIOCGWINSZ, &ws) < 0) { 183 log_warn("cannot get terminal size"); 184 return 0; 185 } 186 187 /* Open current tty */ 188 (void)ioctl(Selmouse.sm_mouse->m_statfd, WSDISPLAYIO_GETACTIVESCREEN, 189 &i); 190 Selmouse.sm_ttyfd = -1; 191 open_tty(i); 192 193 /* Check if the kernel has character functions */ 194 ch.row = ch.col = 0; 195 if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_GETWSCHAR, &ch) < 0) { 196 (void)close(Selmouse.sm_ttyfd); 197 log_warn("ioctl(WSDISPLAYIO_GETWSCHAR) failed"); 198 return 0; 199 } 200 201 Selmouse.sm_max_y = ws.ws_row - 1; 202 Selmouse.sm_max_x = ws.ws_col - 1; 203 Selmouse.sm_y = Selmouse.sm_max_y / 2; 204 Selmouse.sm_x = Selmouse.sm_max_x / 2; 205 Selmouse.sm_count_y = 0; 206 Selmouse.sm_count_x = 0; 207 Selmouse.sm_visible = 0; 208 Selmouse.sm_selecting = 0; 209 Initialized = 1; 210 211 return 1; 212} 213 214/* ---------------------------------------------------------------------- */ 215 216/* Mode cleanup. */ 217int 218selection_cleanup(void) 219{ 220 221 cursor_hide(); 222 if (Selmouse.sm_ttyfd >= 0) 223 (void)close(Selmouse.sm_ttyfd); 224 return 1; 225} 226 227/* ---------------------------------------------------------------------- */ 228 229/* Parse wsmouse events. Both motion and button events are handled. The 230 * former move the mouse across the screen and the later create a new 231 * selection or paste the buffer. */ 232void 233selection_wsmouse_event(struct wscons_event evt) 234{ 235 236 if (IS_MOTION_EVENT(evt.type)) { 237 if (Selmouse.sm_selecting) 238 selarea_hide(); 239 cursor_hide(); 240 241 switch (evt.type) { 242 case WSCONS_EVENT_MOUSE_DELTA_X: 243 if (Selmouse.sm_count_x >= Selmouse.sm_slowdown_x) { 244 Selmouse.sm_count_x = 0; 245 if (evt.value > 0) 246 Selmouse.sm_x++; 247 else if (Selmouse.sm_x != 0) 248 Selmouse.sm_x--; 249 if (Selmouse.sm_x > Selmouse.sm_max_x) 250 Selmouse.sm_x = Selmouse.sm_max_x; 251 } else 252 Selmouse.sm_count_x++; 253 break; 254 255 case WSCONS_EVENT_MOUSE_DELTA_Y: 256 if (Selmouse.sm_count_y >= Selmouse.sm_slowdown_y) { 257 Selmouse.sm_count_y = 0; 258 if (evt.value < 0) 259 Selmouse.sm_y++; 260 else if (Selmouse.sm_y != 0) 261 Selmouse.sm_y--; 262 if (Selmouse.sm_y > Selmouse.sm_max_y) 263 Selmouse.sm_y = Selmouse.sm_max_y; 264 } else 265 Selmouse.sm_count_y++; 266 break; 267 268 case WSCONS_EVENT_MOUSE_DELTA_Z: 269 break; 270 271 default: 272 log_warnx("unknown event"); 273 } 274 275 if (Selmouse.sm_selecting) 276 selarea_show(); 277 cursor_show(); 278 279 } else if (IS_BUTTON_EVENT(evt.type)) { 280 switch (evt.type) { 281 case WSCONS_EVENT_MOUSE_UP: 282 if (evt.value == Selmouse.sm_but_select) { 283 /* End selection */ 284 selarea_end(); 285 selarea_hide(); 286 } 287 break; 288 289 case WSCONS_EVENT_MOUSE_DOWN: 290 if (evt.value == Selmouse.sm_but_select) { 291 /* Start selection */ 292 selarea_start(); 293 cursor_hide(); 294 selarea_show(); 295 } else if (evt.value == Selmouse.sm_but_paste) { 296 /* Paste selection */ 297 selarea_paste(); 298 break; 299 } 300 break; 301 302 default: 303 log_warnx("unknown button event"); 304 } 305 } 306} 307 308/* ---------------------------------------------------------------------- */ 309 310/* Parse wscons status events. */ 311void 312selection_wscons_event(struct wscons_event evt) 313{ 314 315 switch (evt.type) { 316 case WSCONS_EVENT_SCREEN_SWITCH: 317 if (Selmouse.sm_selecting) 318 selarea_hide(); 319 cursor_hide(); 320 321 if (!Selmouse.sm_mouse->m_disabled) 322 open_tty(evt.value); 323 324 cursor_show(); 325 if (Selmouse.sm_selecting) 326 selarea_show(); 327 328 break; 329 } 330} 331 332/* ---------------------------------------------------------------------- */ 333 334/* Device polling has timed out, so we hide the mouse to avoid further 335 * console pollution. */ 336void 337selection_poll_timeout(void) 338{ 339 340 if (!Selmouse.sm_selecting) 341 cursor_hide(); 342} 343 344/* ---------------------------------------------------------------------- */ 345 346/* Hides the mouse pointer, if visible. */ 347static void 348cursor_hide(void) 349{ 350 351 if (Selmouse.sm_visible) { 352 char_invert(Selmouse.sm_y, Selmouse.sm_x); 353 Selmouse.sm_visible = 0; 354 } 355} 356 357/* ---------------------------------------------------------------------- */ 358 359/* Shows the mouse pointer, if not visible. */ 360static void 361cursor_show(void) 362{ 363 364 if (!Selmouse.sm_visible) { 365 char_invert(Selmouse.sm_y, Selmouse.sm_x); 366 Selmouse.sm_visible = 1; 367 } 368} 369 370/* ---------------------------------------------------------------------- */ 371 372/* Opens the specified TTY device, used when switching consoles. */ 373static void 374open_tty(int ttyno) 375{ 376 char buf[20]; 377 378 if (Selmouse.sm_ttyfd >= 0) 379 (void)close(Selmouse.sm_ttyfd); 380 381 (void)snprintf(buf, sizeof(buf), _PATH_TTYPREFIX "%d", ttyno); 382 Selmouse.sm_ttyfd = open(buf, O_RDONLY | O_NONBLOCK); 383 if (Selmouse.sm_ttyfd < 0) 384 log_warnx("cannot open %s", buf); 385} 386 387/* ---------------------------------------------------------------------- */ 388 389/* Flips the background and foreground colors of the specified screen 390 * position. */ 391static void 392char_invert(size_t row, size_t col) 393{ 394 int t; 395 struct wsdisplay_char ch; 396 397 ch.row = row; 398 ch.col = col; 399 400 if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_GETWSCHAR, &ch) == -1) { 401 log_warn("ioctl(WSDISPLAYIO_GETWSCHAR) failed"); 402 return; 403 } 404 405 t = ch.foreground; 406 ch.foreground = ch.background; 407 ch.background = t; 408 409 if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_PUTWSCHAR, &ch) == -1) 410 log_warn("ioctl(WSDISPLAYIO_PUTWSCHAR) failed"); 411} 412 413/* ---------------------------------------------------------------------- */ 414 415/* Allocates memory for a selection. This function is very simple but is 416 * used to get a consistent warning message. */ 417static void * 418alloc_sel(size_t len) 419{ 420 void *ptr; 421 422 ptr = malloc(len); 423 if (ptr == NULL) 424 log_warn("cannot allocate memory for selection (%lu bytes)", 425 (unsigned long)len); 426 return ptr; 427} 428 429/* ---------------------------------------------------------------------- */ 430 431/* Copies a region of a line inside the buffer pointed by `ptr'. */ 432static char * 433fill_buf(char *ptr, size_t row, size_t col, size_t end) 434{ 435 struct wsdisplay_char ch; 436 437 ch.row = row; 438 for (ch.col = col; ch.col < end; ch.col++) { 439 if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_GETWSCHAR, 440 &ch) == -1) { 441 log_warn("ioctl(WSDISPLAYIO_GETWSCHAR) failed"); 442 *ptr++ = ' '; 443 } else { 444 *ptr++ = ch.letter; 445 } 446 } 447 return ptr; 448} 449 450/* ---------------------------------------------------------------------- */ 451 452/* Scans the specified line and checks its length. Characters at the end 453 * of it which match isspace() are discarded. */ 454static size_t 455row_length(size_t row) 456{ 457 struct wsdisplay_char ch; 458 459 ch.col = Selmouse.sm_max_x; 460 ch.row = row; 461 do { 462 if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_GETWSCHAR, &ch) == -1) 463 log_warn("ioctl(WSDISPLAYIO_GETWSCHAR) failed"); 464 ch.col--; 465 } while (isspace((unsigned char)ch.letter) && ch.col >= 0); 466 return ch.col + 2; 467} 468 469/* ---------------------------------------------------------------------- */ 470 471/* Copies all the text selected to the selection buffer. Whitespace at 472 * end of lines is trimmed. */ 473static void 474selarea_copy_text(void) 475{ 476 char *ptr, *str; 477 size_t l, r; 478 479 if (Selarea.sa_y1 == Selarea.sa_y2) { 480 /* Selection is one row */ 481 l = row_length(Selarea.sa_y1); 482 if (Selarea.sa_x1 > l) 483 /* Selection is after last character, 484 * therefore it is empty */ 485 str = NULL; 486 else { 487 if (Selarea.sa_x2 > l) 488 Selarea.sa_x2 = l; 489 ptr = str = 490 alloc_sel(Selarea.sa_x2 - Selarea.sa_x1 + 1); 491 if (ptr == NULL) 492 return; 493 494 ptr = fill_buf(ptr, Selarea.sa_y1, Selarea.sa_x1, 495 Selarea.sa_x2); 496 *ptr = '\0'; 497 } 498 } else { 499 /* Selection is multiple rows */ 500 ptr = str = 501 alloc_sel(Selarea.sa_endoff - Selarea.sa_startoff + 1); 502 if (ptr == NULL) 503 return; 504 505 /* Calculate and copy first line */ 506 l = row_length(Selarea.sa_y1); 507 if (Selarea.sa_x1 < l) { 508 ptr = fill_buf(ptr, Selarea.sa_y1, Selarea.sa_x1, l); 509 *ptr++ = '\r'; 510 } 511 512 /* Copy mid lines if there are any */ 513 if ((Selarea.sa_y2 - Selarea.sa_y1) > 1) { 514 for (r = Selarea.sa_y1 + 1; r <= Selarea.sa_y2 - 1; 515 r++) { 516 ptr = fill_buf(ptr, r, 0, row_length(r)); 517 *ptr++ = '\r'; 518 } 519 } 520 521 /* Calculate and copy end line */ 522 l = row_length(Selarea.sa_y2); 523 if (Selarea.sa_x2 < l) 524 l = Selarea.sa_x2; 525 ptr = fill_buf(ptr, Selarea.sa_y2, 0, l); 526 *ptr = '\0'; 527 } 528 529 if (Selarea.sa_buf != NULL) { 530 free(Selarea.sa_buf); 531 Selarea.sa_buf = NULL; 532 } 533 534 if (str != NULL) { 535 Selarea.sa_buf = str; 536 Selarea.sa_buflen = ptr - str; 537 } 538} 539 540/* ---------------------------------------------------------------------- */ 541 542/* Starts a selection. */ 543static void 544selarea_start(void) 545{ 546 547 if (Selarea.sa_buf != NULL) { 548 free(Selarea.sa_buf); 549 Selarea.sa_buf = NULL; 550 } 551 552 Selarea.sa_y1 = Selmouse.sm_y; 553 Selarea.sa_x1 = Selmouse.sm_x; 554 selarea_calculate(); 555 Selmouse.sm_selecting = 1; 556} 557 558/* ---------------------------------------------------------------------- */ 559 560/* Ends a selection. Highlighted text is copied to the buffer. */ 561static void 562selarea_end(void) 563{ 564 size_t i; 565 566 selarea_calculate(); 567 568 /* Invert sel coordinates if needed */ 569 if (Selarea.sa_x1 > Selarea.sa_x2) { 570 i = Selarea.sa_x2; 571 Selarea.sa_x2 = Selarea.sa_x1; 572 Selarea.sa_x1 = i; 573 } 574 if (Selarea.sa_y1 > Selarea.sa_y2) { 575 i = Selarea.sa_y2; 576 Selarea.sa_y2 = Selarea.sa_y1; 577 Selarea.sa_y1 = i; 578 } 579 580 selarea_copy_text(); 581 Selmouse.sm_selecting = 0; 582} 583 584/* ---------------------------------------------------------------------- */ 585 586/* Calculates selection absolute positions in the screen buffer. */ 587static void 588selarea_calculate(void) 589{ 590 size_t i; 591 592 i = Selmouse.sm_max_x + 1; 593 Selarea.sa_y2 = Selmouse.sm_y; 594 Selarea.sa_x2 = Selmouse.sm_x; 595 Selarea.sa_startoff = Selarea.sa_y1 * i + Selarea.sa_x1; 596 Selarea.sa_endoff = Selarea.sa_y2 * i + Selarea.sa_x2; 597 598 if (Selarea.sa_startoff > Selarea.sa_endoff) { 599 i = Selarea.sa_endoff; 600 Selarea.sa_endoff = Selarea.sa_startoff; 601 Selarea.sa_startoff = i; 602 } 603} 604 605/* ---------------------------------------------------------------------- */ 606 607/* Hides the highlighted region, returning it to normal colors. */ 608static void 609selarea_hide(void) 610{ 611 size_t i; 612 613 for (i = Selarea.sa_startoff; i <= Selarea.sa_endoff; i++) 614 char_invert(0, i); 615} 616 617/* ---------------------------------------------------------------------- */ 618 619/* Highlights the selected region. */ 620static void 621selarea_show(void) 622{ 623 size_t i; 624 625 selarea_calculate(); 626 for (i = Selarea.sa_startoff; i <= Selarea.sa_endoff; i++) 627 char_invert(0, i); 628} 629 630/* ---------------------------------------------------------------------- */ 631 632/* Pastes selected text into the active console. */ 633static void 634selarea_paste(void) 635{ 636 size_t i; 637 638 if (Selarea.sa_buf == NULL) 639 return; 640 for (i = 0; i < Selarea.sa_buflen; i++) 641 if (ioctl(Selmouse.sm_ttyfd, TIOCSTI, 642 &Selarea.sa_buf[i]) == -1) 643 log_warn("ioctl(TIOCSTI)"); 644} 645