selection.c revision 1.5
1/* $NetBSD: selection.c,v 1.5 2003/08/06 23:58:40 jmmv Exp $ */ 2 3/* 4 * Copyright (c) 2002, 2003 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.5 2003/08/06 23:58:40 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 open_tty(evt.value); 322 323 cursor_show(); 324 if (Selmouse.sm_selecting) 325 selarea_show(); 326 327 break; 328 } 329} 330 331/* ---------------------------------------------------------------------- */ 332 333/* Device polling has timed out, so we hide the mouse to avoid further 334 * console pollution. */ 335void 336selection_poll_timeout(void) 337{ 338 339 if (!Selmouse.sm_selecting) 340 cursor_hide(); 341} 342 343/* ---------------------------------------------------------------------- */ 344 345/* Hides the mouse pointer, if visible. */ 346static void 347cursor_hide(void) 348{ 349 350 if (Selmouse.sm_visible) { 351 char_invert(Selmouse.sm_y, Selmouse.sm_x); 352 Selmouse.sm_visible = 0; 353 } 354} 355 356/* ---------------------------------------------------------------------- */ 357 358/* Shows the mouse pointer, if not visible. */ 359static void 360cursor_show(void) 361{ 362 363 if (!Selmouse.sm_visible) { 364 char_invert(Selmouse.sm_y, Selmouse.sm_x); 365 Selmouse.sm_visible = 1; 366 } 367} 368 369/* ---------------------------------------------------------------------- */ 370 371/* Opens the specified TTY device, used when switching consoles. */ 372static void 373open_tty(int ttyno) 374{ 375 char buf[20]; 376 377 if (Selmouse.sm_ttyfd >= 0) 378 (void)close(Selmouse.sm_ttyfd); 379 380 if (!Selmouse.sm_mouse->m_disabled) { 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 390/* Flips the background and foreground colors of the specified screen 391 * position. */ 392static void 393char_invert(size_t row, size_t col) 394{ 395 int t; 396 struct wsdisplay_char ch; 397 398 ch.row = row; 399 ch.col = col; 400 401 if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_GETWSCHAR, &ch) == -1) { 402 log_warn("ioctl(WSDISPLAYIO_GETWSCHAR) failed"); 403 return; 404 } 405 406 t = ch.foreground; 407 ch.foreground = ch.background; 408 ch.background = t; 409 410 if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_PUTWSCHAR, &ch) == -1) 411 log_warn("ioctl(WSDISPLAYIO_PUTWSCHAR) failed"); 412} 413 414/* ---------------------------------------------------------------------- */ 415 416/* Allocates memory for a selection. This function is very simple but is 417 * used to get a consistent warning message. */ 418static void * 419alloc_sel(size_t len) 420{ 421 void *ptr; 422 423 ptr = malloc(len); 424 if (ptr == NULL) 425 log_warn("cannot allocate memory for selection (%lu bytes)", 426 (unsigned long)len); 427 return ptr; 428} 429 430/* ---------------------------------------------------------------------- */ 431 432/* Copies a region of a line inside the buffer pointed by `ptr'. */ 433static char * 434fill_buf(char *ptr, size_t row, size_t col, size_t end) 435{ 436 struct wsdisplay_char ch; 437 438 ch.row = row; 439 for (ch.col = col; ch.col < end; ch.col++) { 440 if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_GETWSCHAR, 441 &ch) == -1) { 442 log_warn("ioctl(WSDISPLAYIO_GETWSCHAR) failed"); 443 *ptr++ = ' '; 444 } else { 445 *ptr++ = ch.letter; 446 } 447 } 448 return ptr; 449} 450 451/* ---------------------------------------------------------------------- */ 452 453/* Scans the specified line and checks its length. Characters at the end 454 * of it which match isspace() are discarded. */ 455static size_t 456row_length(size_t row) 457{ 458 struct wsdisplay_char ch; 459 460 ch.col = Selmouse.sm_max_x; 461 ch.row = row; 462 do { 463 if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_GETWSCHAR, &ch) == -1) 464 log_warn("ioctl(WSDISPLAYIO_GETWSCHAR) failed"); 465 ch.col--; 466 } while (isspace((unsigned char)ch.letter) && ch.col >= 0); 467 return ch.col + 2; 468} 469 470/* ---------------------------------------------------------------------- */ 471 472/* Copies all the text selected to the selection buffer. Whitespace at 473 * end of lines is trimmed. */ 474static void 475selarea_copy_text(void) 476{ 477 char *ptr, *str; 478 size_t l, r; 479 480 if (Selarea.sa_y1 == Selarea.sa_y2) { 481 /* Selection is one row */ 482 l = row_length(Selarea.sa_y1); 483 if (Selarea.sa_x1 > l) 484 /* Selection is after last character, 485 * therefore it is empty */ 486 str = NULL; 487 else { 488 if (Selarea.sa_x2 > l) 489 Selarea.sa_x2 = l; 490 ptr = str = 491 alloc_sel(Selarea.sa_x2 - Selarea.sa_x1 + 1); 492 if (ptr == NULL) 493 return; 494 495 ptr = fill_buf(ptr, Selarea.sa_y1, Selarea.sa_x1, 496 Selarea.sa_x2); 497 *ptr = '\0'; 498 } 499 } else { 500 /* Selection is multiple rows */ 501 ptr = str = 502 alloc_sel(Selarea.sa_endoff - Selarea.sa_startoff + 1); 503 if (ptr == NULL) 504 return; 505 506 /* Calculate and copy first line */ 507 l = row_length(Selarea.sa_y1); 508 if (Selarea.sa_x1 < l) { 509 ptr = fill_buf(ptr, Selarea.sa_y1, Selarea.sa_x1, l); 510 *ptr++ = '\r'; 511 } 512 513 /* Copy mid lines if there are any */ 514 if ((Selarea.sa_y2 - Selarea.sa_y1) > 1) { 515 for (r = Selarea.sa_y1 + 1; r <= Selarea.sa_y2 - 1; 516 r++) { 517 ptr = fill_buf(ptr, r, 0, row_length(r)); 518 *ptr++ = '\r'; 519 } 520 } 521 522 /* Calculate and copy end line */ 523 l = row_length(Selarea.sa_y2); 524 if (Selarea.sa_x2 < l) 525 l = Selarea.sa_x2; 526 ptr = fill_buf(ptr, Selarea.sa_y2, 0, l); 527 *ptr = '\0'; 528 } 529 530 if (Selarea.sa_buf != NULL) { 531 free(Selarea.sa_buf); 532 Selarea.sa_buf = NULL; 533 } 534 535 if (str != NULL) { 536 Selarea.sa_buf = str; 537 Selarea.sa_buflen = ptr - str; 538 } 539} 540 541/* ---------------------------------------------------------------------- */ 542 543/* Starts a selection. */ 544static void 545selarea_start(void) 546{ 547 548 if (Selarea.sa_buf != NULL) { 549 free(Selarea.sa_buf); 550 Selarea.sa_buf = NULL; 551 } 552 553 Selarea.sa_y1 = Selmouse.sm_y; 554 Selarea.sa_x1 = Selmouse.sm_x; 555 selarea_calculate(); 556 Selmouse.sm_selecting = 1; 557} 558 559/* ---------------------------------------------------------------------- */ 560 561/* Ends a selection. Highlighted text is copied to the buffer. */ 562static void 563selarea_end(void) 564{ 565 size_t i; 566 567 selarea_calculate(); 568 569 /* Invert sel coordinates if needed */ 570 if (Selarea.sa_x1 > Selarea.sa_x2) { 571 i = Selarea.sa_x2; 572 Selarea.sa_x2 = Selarea.sa_x1; 573 Selarea.sa_x1 = i; 574 } 575 if (Selarea.sa_y1 > Selarea.sa_y2) { 576 i = Selarea.sa_y2; 577 Selarea.sa_y2 = Selarea.sa_y1; 578 Selarea.sa_y1 = i; 579 } 580 581 selarea_copy_text(); 582 Selmouse.sm_selecting = 0; 583} 584 585/* ---------------------------------------------------------------------- */ 586 587/* Calculates selection absolute positions in the screen buffer. */ 588static void 589selarea_calculate(void) 590{ 591 size_t i; 592 593 i = Selmouse.sm_max_x + 1; 594 Selarea.sa_y2 = Selmouse.sm_y; 595 Selarea.sa_x2 = Selmouse.sm_x; 596 Selarea.sa_startoff = Selarea.sa_y1 * i + Selarea.sa_x1; 597 Selarea.sa_endoff = Selarea.sa_y2 * i + Selarea.sa_x2; 598 599 if (Selarea.sa_startoff > Selarea.sa_endoff) { 600 i = Selarea.sa_endoff; 601 Selarea.sa_endoff = Selarea.sa_startoff; 602 Selarea.sa_startoff = i; 603 } 604} 605 606/* ---------------------------------------------------------------------- */ 607 608/* Hides the highlighted region, returning it to normal colors. */ 609static void 610selarea_hide(void) 611{ 612 size_t i; 613 614 for (i = Selarea.sa_startoff; i <= Selarea.sa_endoff; i++) 615 char_invert(0, i); 616} 617 618/* ---------------------------------------------------------------------- */ 619 620/* Highlights the selected region. */ 621static void 622selarea_show(void) 623{ 624 size_t i; 625 626 selarea_calculate(); 627 for (i = Selarea.sa_startoff; i <= Selarea.sa_endoff; i++) 628 char_invert(0, i); 629} 630 631/* ---------------------------------------------------------------------- */ 632 633/* Pastes selected text into the active console. */ 634static void 635selarea_paste(void) 636{ 637 size_t i; 638 639 if (Selarea.sa_buf == NULL) 640 return; 641 for (i = 0; i < Selarea.sa_buflen; i++) 642 if (ioctl(Selmouse.sm_ttyfd, TIOCSTI, 643 &Selarea.sa_buf[i]) == -1) 644 log_warn("ioctl(TIOCSTI)"); 645} 646