Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/* $NetBSD: selection.c,v 1.12 2023/02/07 20:37:48 mlelstv Exp $ */

/*
 * Copyright (c) 2002, 2003, 2004, 2007 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Julio M. Merino Vidal.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name authors may not be used to endorse or promote products
 *    derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>

#ifndef lint
__RCSID("$NetBSD: selection.c,v 1.12 2023/02/07 20:37:48 mlelstv Exp $");
#endif /* not lint */

#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/tty.h>
#include <dev/wscons/wsconsio.h>

#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "pathnames.h"
#include "wsmoused.h"

/* ---------------------------------------------------------------------- */

/*
 * Public interface exported by the `selection' mode.
 */

int  selection_startup(struct mouse *m);
int  selection_cleanup(void);
void selection_wsmouse_event(struct wscons_event);
void selection_wscons_event(struct wscons_event, int);
void selection_poll_timeout(void);

struct mode_bootstrap Selection_Mode = {
	"selection",
	selection_startup,
	selection_cleanup,
	selection_wsmouse_event,
	selection_wscons_event,
	selection_poll_timeout
};

/* ---------------------------------------------------------------------- */

/*
 * Structures used in this module only.
 */

/* The `selarea' structure is used to describe a selection in the screen.
   It also holds a copy of the selected text. */
struct selarea {
	size_t sa_x1;           /* Start column */
	size_t sa_y1;           /* Start row */
	size_t sa_x2;           /* End column */
	size_t sa_y2;           /* End row */
	size_t sa_startoff;     /* Absolute offset of start position */
	size_t sa_endoff;       /* Absolute offset of end position */
	size_t sa_buflen;       /* Length of selected text */
	char  *sa_buf;          /* A copy of the selected text */
};

/* The `selmouse' structure extends the `mouse' structure adding all fields
   required for this module to work. */
struct selmouse {
	struct mouse *sm_mouse; /* Pointer to parent structure */

	int sm_ttyfd;           /* Active TTY file descriptor */

	size_t sm_x;            /* Mouse pointer column */
	size_t sm_y;            /* Mouse pointer row */
	size_t sm_max_x;        /* Maximun column allowed */
	size_t sm_max_y;        /* Maximun row allowed */

	size_t sm_slowdown_x;   /* X axis slowdown */
	size_t sm_slowdown_y;   /* Y axis slowdown */
	size_t sm_count_x;      /* Number of X movements skipped */
	size_t sm_count_y;      /* Number of Y movements skipped */

	int sm_visible;         /* Whether pointer is visible or not */
	int sm_selecting;       /* Whether we are selecting or not */

	int sm_but_select;      /* Button number to select an area */
	int sm_but_paste;       /* Button number to paste buffer */
};

/* ---------------------------------------------------------------------- */

/*
 * Global variables.
 */

static struct selmouse Selmouse;
static struct selarea Selarea;
static int Initialized = 0;

/* ---------------------------------------------------------------------- */

/*
 * Prototypes for functions private to this module.
 */

static void cursor_hide(void);
static void cursor_show(void);
static void open_tty(int);
static void char_invert(size_t, size_t);
static void *alloc_sel(size_t);
static char *fill_buf(char *, size_t, size_t, size_t);
static size_t row_length(size_t);
static void selarea_copy_text(void);
static void selarea_start(void);
static void selarea_end(void);
static void selarea_calculate(void);
static void selarea_getrowcol(size_t, size_t *, size_t *);
static void selarea_hide(void);
static void selarea_show(void);
static void selarea_paste(void);

/* ---------------------------------------------------------------------- */

/* Mode initialization.  Reads configuration, checks if the kernel has
 * support for mouse pointer and opens required files. */
int
selection_startup(struct mouse *m)
{
	int i;
	struct wsdisplay_char ch;
	struct block *conf;

	if (Initialized) {
		log_warnx("selection mode already initialized");
		return 1;
	}

	(void)memset(&Selmouse, 0, sizeof(struct selmouse));
	Selmouse.sm_mouse = m;

	conf = config_get_mode("selection");
	Selmouse.sm_slowdown_x = block_get_propval_int(conf, "slowdown_x", 0);
	Selmouse.sm_slowdown_y = block_get_propval_int(conf, "slowdown_y", 3);
	if (block_get_propval_int(conf, "lefthanded", 0)) {
		Selmouse.sm_but_select = 2;
		Selmouse.sm_but_paste = 0;
	} else {
		Selmouse.sm_but_select = 0;
		Selmouse.sm_but_paste = 2;
	}

	/* Open current tty */
	(void)ioctl(Selmouse.sm_mouse->m_statfd, WSDISPLAYIO_GETACTIVESCREEN,
	    &i);
	Selmouse.sm_ttyfd = -1;
	open_tty(i);

	/* Check if the kernel has character functions */
	ch.row = ch.col = 0;
	if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_GETWSCHAR, &ch) < 0) {
		(void)close(Selmouse.sm_ttyfd);
		log_warn("ioctl(WSDISPLAYIO_GETWSCHAR) failed");
		return 0;
	}

	assert(Selmouse.sm_max_y != 0); /* Initialized by open_tty above. */
	assert(Selmouse.sm_max_x != 0); /* Initialized by open_tty above. */
	Selmouse.sm_y = Selmouse.sm_max_y / 2;
	Selmouse.sm_x = Selmouse.sm_max_x / 2;
	Selmouse.sm_count_y = 0;
	Selmouse.sm_count_x = 0;
	Selmouse.sm_visible = 0;
	Selmouse.sm_selecting = 0;
	Initialized = 1;

	return 1;
}

/* ---------------------------------------------------------------------- */

/* Mode cleanup. */
int
selection_cleanup(void)
{

	cursor_hide();
	if (Selmouse.sm_ttyfd >= 0)
		(void)close(Selmouse.sm_ttyfd);
	return 1;
}

/* ---------------------------------------------------------------------- */

/* Parse wsmouse events.  Both motion and button events are handled.  The
 * former move the mouse across the screen and the later create a new
 * selection or paste the buffer. */
void
selection_wsmouse_event(struct wscons_event evt)
{
	const struct wsmouse_calibcoords *abs = &Selmouse.sm_mouse->m_calib;

	if (IS_MOTION_EVENT(evt.type)) {
		if (Selmouse.sm_selecting)
			selarea_hide();
		cursor_hide();

		switch (evt.type) {
		case WSCONS_EVENT_MOUSE_DELTA_X:
			if (Selmouse.sm_count_x >= Selmouse.sm_slowdown_x) {
				Selmouse.sm_count_x = 0;
				if (evt.value > 0)
					Selmouse.sm_x++;
				else if (Selmouse.sm_x != 0)
					Selmouse.sm_x--;
				if (Selmouse.sm_x > Selmouse.sm_max_x)
					Selmouse.sm_x = Selmouse.sm_max_x;
			} else
				Selmouse.sm_count_x++;
			break;

		case WSCONS_EVENT_MOUSE_DELTA_Y:
			if (Selmouse.sm_count_y >= Selmouse.sm_slowdown_y) {
				Selmouse.sm_count_y = 0;
				if (evt.value < 0)
					Selmouse.sm_y++;
				else if (Selmouse.sm_y != 0)
					Selmouse.sm_y--;
				if (Selmouse.sm_y > Selmouse.sm_max_y)
					Selmouse.sm_y = Selmouse.sm_max_y;
			} else
				Selmouse.sm_count_y++;
			break;

		case WSCONS_EVENT_MOUSE_DELTA_Z: /* FALLTHROUGH */
		case WSCONS_EVENT_MOUSE_DELTA_W:
			break;

		case WSCONS_EVENT_MOUSE_ABSOLUTE_X:
			if (!Selmouse.sm_mouse->m_doabs)
				break;
			/* max x is inclusive in both selmouse and tpcalib */
			Selmouse.sm_x
			    = ((evt.value - abs->minx) * (Selmouse.sm_max_x + 1))
			      / (abs->maxx - abs->minx + 1);
			break;

		case WSCONS_EVENT_MOUSE_ABSOLUTE_Y:
			if (!Selmouse.sm_mouse->m_doabs)
				break;
			/* max y is inclusive in both selmouse and tpcalib */
			Selmouse.sm_y
			    = ((evt.value - abs->miny) * (Selmouse.sm_max_y + 1))
			      / (abs->maxy - abs->miny + 1);
			break;

		case WSCONS_EVENT_MOUSE_ABSOLUTE_Z: /* FALLTHROUGH */
		case WSCONS_EVENT_MOUSE_ABSOLUTE_W:
			break;

		default:
			log_warnx("unknown event");
		}

		if (Selmouse.sm_selecting)
			selarea_show();
		cursor_show();

	} else if (IS_BUTTON_EVENT(evt.type)) {
		switch (evt.type) {
		case WSCONS_EVENT_MOUSE_UP:
			if (evt.value == Selmouse.sm_but_select) {
				/* End selection */
				selarea_end();
				selarea_hide();
			}
			break;

		case WSCONS_EVENT_MOUSE_DOWN:
			if (evt.value == Selmouse.sm_but_select) {
				/* Start selection */
				selarea_start();
				cursor_hide();
				selarea_show();
			} else if (evt.value == Selmouse.sm_but_paste) {
				/* Paste selection */
				selarea_paste();
				break;
			}
			break;

		default:
			log_warnx("unknown button event");
		}
	}
}

/* ---------------------------------------------------------------------- */

/* Parse wscons status events. */
void
selection_wscons_event(struct wscons_event evt, int preclose)
{

	switch (evt.type) {
	case WSCONS_EVENT_SCREEN_SWITCH:
		if (preclose) {
			if (Selmouse.sm_selecting)
				selarea_hide();
			cursor_hide();
		} else {
			if (!Selmouse.sm_mouse->m_disabled)
				open_tty(evt.value);

			cursor_show();
			if (Selmouse.sm_selecting)
				selarea_show();
		}

		break;
	}
}

/* ---------------------------------------------------------------------- */

/* Device polling has timed out, so we hide the mouse to avoid further
 * console pollution. */
void
selection_poll_timeout(void)
{

	if (!Selmouse.sm_selecting)
		cursor_hide();
}

/* ---------------------------------------------------------------------- */

/* Hides the mouse pointer, if visible. */
static void
cursor_hide(void)
{

	if (Selmouse.sm_visible) {
		char_invert(Selmouse.sm_y, Selmouse.sm_x);
		Selmouse.sm_visible = 0;
	}
}

/* ---------------------------------------------------------------------- */

/* Shows the mouse pointer, if not visible. */
static void
cursor_show(void)
{

	if (!Selmouse.sm_visible) {
		char_invert(Selmouse.sm_y, Selmouse.sm_x);
		Selmouse.sm_visible = 1;
	}
}

/* ---------------------------------------------------------------------- */

/* Opens the specified TTY device, used when switching consoles.
 * Takes care to adjust the pointer status to make sense within the new
 * console, whose dimensions may differ from the previous one. */
static void
open_tty(int ttyno)
{
	char buf[20];
	struct winsize ws;

	if (Selmouse.sm_ttyfd >= 0)
		(void)close(Selmouse.sm_ttyfd);

	(void)snprintf(buf, sizeof(buf), _PATH_TTYPREFIX "%d", ttyno);
	Selmouse.sm_ttyfd = open(buf, O_RDONLY | O_NONBLOCK);
	if (Selmouse.sm_ttyfd < 0)
		log_warnx("cannot open %s", buf);

	/* Get terminal size and update the maximum cursor coordinates. */
	if (ioctl(Selmouse.sm_ttyfd, TIOCGWINSZ, &ws) < 0) {
		log_warn("cannot get terminal size");
		/* Some defaults; not guaranteed to be correct but we
		 * cannot do better. */
		Selmouse.sm_max_y = 24;
		Selmouse.sm_max_x = 79;
	} else {
		Selmouse.sm_max_y = ws.ws_row - 1;
		Selmouse.sm_max_x = ws.ws_col - 1;
	}

	/* Adjust current mouse position in case the terminal's size has
	 * changed. */
	if (Selmouse.sm_x > Selmouse.sm_max_x)
		Selmouse.sm_x = Selmouse.sm_max_x;
	if (Selmouse.sm_y > Selmouse.sm_max_y)
		Selmouse.sm_y = Selmouse.sm_max_y;
}

/* ---------------------------------------------------------------------- */

/* Flips the background and foreground colors of the specified screen
 * position. */
static void
char_invert(size_t row, size_t col)
{
	int t;
	struct wsdisplay_char ch;

	ch.row = row;
	ch.col = col;

	if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_GETWSCHAR, &ch) == -1) {
		log_warn("ioctl(WSDISPLAYIO_GETWSCHAR) failed");
		return;
	}

	t = ch.foreground;
	ch.foreground = ch.background;
	ch.background = t;

	if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_PUTWSCHAR, &ch) == -1)
		log_warn("ioctl(WSDISPLAYIO_PUTWSCHAR) failed");
}

/* ---------------------------------------------------------------------- */

/* Allocates memory for a selection.  This function is very simple but is
 * used to get a consistent warning message. */
static void *
alloc_sel(size_t len)
{
	void *ptr;

	ptr = malloc(len);
	if (ptr == NULL)
		log_warn("cannot allocate memory for selection (%lu bytes)",
		    (unsigned long)len);
	return ptr;
}

/* ---------------------------------------------------------------------- */

/* Copies a region of a line inside the buffer pointed by `ptr'. */
static char *
fill_buf(char *ptr, size_t row, size_t col, size_t end)
{
	struct wsdisplay_char ch;

	ch.row = row;
	for (ch.col = col; ch.col < end; ch.col++) {
		if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_GETWSCHAR,
		    &ch) == -1) {
			log_warn("ioctl(WSDISPLAYIO_GETWSCHAR) failed");
			*ptr++ = ' ';
		} else {
			*ptr++ = ch.letter;
		}
	}
	return ptr;
}

/* ---------------------------------------------------------------------- */

/* Scans the specified line and checks its length.  Characters at the end
 * of it which match isspace() are discarded. */
static size_t
row_length(size_t row)
{
	struct wsdisplay_char ch;

	ch.col = Selmouse.sm_max_x;
	ch.row = row;
	do {
		if (ioctl(Selmouse.sm_ttyfd, WSDISPLAYIO_GETWSCHAR, &ch) == -1)
			log_warn("ioctl(WSDISPLAYIO_GETWSCHAR) failed");
		ch.col--;
	} while (isspace((unsigned char)ch.letter) && ch.col >= 0);
	return ch.col + 2;
}

/* ---------------------------------------------------------------------- */

/* Copies all the text selected to the selection buffer.  Whitespace at
 * end of lines is trimmed. */
static void
selarea_copy_text(void)
{
	char *ptr, *str;
	size_t l, r;

	ptr = NULL;	/* XXXGCC -Wuninitialized */

	if (Selarea.sa_y1 == Selarea.sa_y2) {
		/* Selection is one row */
		l = row_length(Selarea.sa_y1);
		if (Selarea.sa_x1 > l)
			/* Selection is after last character,
			 * therefore it is empty */
			str = NULL;
		else {
			if (Selarea.sa_x2 > l)
				Selarea.sa_x2 = l;
			ptr = str =
			    alloc_sel(Selarea.sa_x2 - Selarea.sa_x1 + 1);
			if (ptr == NULL)
				return;

			ptr = fill_buf(ptr, Selarea.sa_y1, Selarea.sa_x1,
			    Selarea.sa_x2);
			*ptr = '\0';
		}
	} else {
		/* Selection is multiple rows */
		ptr = str =
		    alloc_sel(Selarea.sa_endoff - Selarea.sa_startoff + 1);
		if (ptr == NULL)
			return;

		/* Calculate and copy first line */
		l = row_length(Selarea.sa_y1);
		if (Selarea.sa_x1 < l) {
			ptr = fill_buf(ptr, Selarea.sa_y1, Selarea.sa_x1, l);
			*ptr++ = '\r';
		}

		/* Copy mid lines if there are any */
		if ((Selarea.sa_y2 - Selarea.sa_y1) > 1) {
			for (r = Selarea.sa_y1 + 1; r <= Selarea.sa_y2 - 1;
			    r++) {
				ptr = fill_buf(ptr, r, 0, row_length(r));
				*ptr++ = '\r';
			}
		}

		/* Calculate and copy end line */
		l = row_length(Selarea.sa_y2);
		if (Selarea.sa_x2 < l)
			l = Selarea.sa_x2;
		ptr = fill_buf(ptr, Selarea.sa_y2, 0, l);
		*ptr = '\0';
	}

	if (Selarea.sa_buf != NULL) {
		free(Selarea.sa_buf);
		Selarea.sa_buf = NULL;
	}

	if (str != NULL) {
		Selarea.sa_buf = str;
		Selarea.sa_buflen = ptr - str;
	}
}

/* ---------------------------------------------------------------------- */

/* Starts a selection. */
static void
selarea_start(void)
{

	if (Selarea.sa_buf != NULL) {
		free(Selarea.sa_buf);
		Selarea.sa_buf = NULL;
	}

	Selarea.sa_y1 = Selmouse.sm_y;
	Selarea.sa_x1 = Selmouse.sm_x;
	selarea_calculate();
	Selmouse.sm_selecting = 1;
}

/* ---------------------------------------------------------------------- */

/* Ends a selection.  Highlighted text is copied to the buffer. */
static void
selarea_end(void)
{
	size_t i;

	selarea_calculate();

	/* Invert sel coordinates if needed */
	if (Selarea.sa_x1 > Selarea.sa_x2) {
		i = Selarea.sa_x2;
		Selarea.sa_x2 = Selarea.sa_x1;
		Selarea.sa_x1 = i;
	}
	if (Selarea.sa_y1 > Selarea.sa_y2) {
		i = Selarea.sa_y2;
		Selarea.sa_y2 = Selarea.sa_y1;
		Selarea.sa_y1 = i;
	}

	selarea_copy_text();
	Selmouse.sm_selecting = 0;
}

/* ---------------------------------------------------------------------- */

/* Calculates selection absolute positions in the screen buffer. */
static void
selarea_calculate(void)
{
	size_t i;
	
	i = Selmouse.sm_max_x + 1;
	Selarea.sa_y2 = Selmouse.sm_y;
	Selarea.sa_x2 = Selmouse.sm_x;
	Selarea.sa_startoff = Selarea.sa_y1 * i + Selarea.sa_x1;
	Selarea.sa_endoff = Selarea.sa_y2 * i + Selarea.sa_x2;

	if (Selarea.sa_startoff > Selarea.sa_endoff) {
		i = Selarea.sa_endoff;
		Selarea.sa_endoff = Selarea.sa_startoff;
		Selarea.sa_startoff = i;
	}
}

/* ---------------------------------------------------------------------- */

/* Turns selection absolute position in the screen buffer back into
   row, col co-ordinates */
static void
selarea_getrowcol(size_t pos, size_t* row, size_t* col)
{
	size_t xres = Selmouse.sm_max_x + 1;

	*row = pos / xres;
	*col = pos - (*row * xres);
}

/* ---------------------------------------------------------------------- */

/* Hides the highlighted region, returning it to normal colors. */
static void
selarea_hide(void)
{
	size_t i, row, col;

	for (i = Selarea.sa_startoff; i <= Selarea.sa_endoff; i++) {
		selarea_getrowcol(i, &row, &col);
		char_invert(row, col);
	}
}

/* ---------------------------------------------------------------------- */

/* Highlights the selected region. */
static void
selarea_show(void)
{
	size_t i, row, col;

	selarea_calculate();
	for (i = Selarea.sa_startoff; i <= Selarea.sa_endoff; i++) {
		selarea_getrowcol(i, &row, &col);
		char_invert(row, col);
	}
}

/* ---------------------------------------------------------------------- */

/* Pastes selected text into the active console. */
static void
selarea_paste(void)
{
	size_t i;

	if (Selarea.sa_buf == NULL)
		return;
	for (i = 0; i < Selarea.sa_buflen; i++)
		if (ioctl(Selmouse.sm_ttyfd, TIOCSTI,
		    &Selarea.sa_buf[i]) == -1)
			log_warn("ioctl(TIOCSTI)");
}