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
|
/* $OpenBSD: pcvt_mouse.c,v 1.2 2000/09/04 17:59:50 aaron Exp $ */
/*
* Copyright (c) 2000 Jean-Baptiste Marchand, Julien Montagne and Jerome Verdon
*
* All rights reserved.
*
* This code is for mouse console support under the pcvt console driver.
*
* 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. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by
* Hellmuth Michaelis, Brian Dunford-Shore, Joerg Wunsch, Scott Turner
* and Charles Hannum.
* 4. 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 "pcvt_hdr.h"
void mouse_moverel(char dx, char dy);
void inverse_char(unsigned short c);
void inverse_region(unsigned short start, unsigned short end);
unsigned char skip_spc_right(char);
unsigned char skip_spc_left(void);
unsigned char skip_char_right(void);
unsigned char skip_char_left(void);
void mouse_copy_start(void);
void mouse_copy_end(void);
void mouse_copy_word(void);
void mouse_copy_line(void);
void mouse_copy_extend(void);
void remove_selection(void);
void mouse_copy_selection(void);
void mouse_paste(void);
uid_t current_uid(void);
void mouse_zaxis(int z);
void mouse_button(int button, int clicks);
#define NO_BORDER 0
#define BORDER 1
#define MAXCOL (vsp->maxcol - 1)
#define MAXROW (vsp->screen_rows - 1)
#define XY_TO_X(col,row) (((row) * (vsp->maxcol)) + (col))
#define XABS_TO_XREL(col) (((col) - vsp->Crtat) % vsp->maxcol)
#define IS_ALPHANUM(pos) ((*(vsp->Crtat + (pos)) & 0x00ff) != ' ')
#define IS_SPACE(c) ((c) == ' ')
int
mouse_ioctl(Dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
{
int device = minor(dev);
mouse_info_t mouse_infos = *(mouse_info_t *) data;
unsigned char c;
video_state *cs;
if (device == PCVTCTL_MINOR && cmd == PCVT_MOUSECTL) {
switch (mouse_infos.operation) {
case MOUSE_INIT:
for (c = 0; c < PCVT_NSCREENS; c++) {
cs = &vs[c];
cs->mouse = (cs->maxcol *
cs->screen_rows) / 2;
cs->mouse_flags = 0;
}
Paste_avail = 0;
break;
case MOUSE_HIDE:
remove_selection();
mouse_hide();
break;
case MOUSE_MOTION_EVENT :
mouse_moverel(mouse_infos.u.data.x,
mouse_infos.u.data.y);
if (mouse_infos.u.data.z)
mouse_zaxis(mouse_infos.u.data.z);
break;
case MOUSE_BUTTON_EVENT :
mouse_button(mouse_infos.u.event.id,
mouse_infos.u.event.value);
break;
default:
return 0;
}
return 0;
}
return (-1); /* continue treatment in pcioctl */
}
void
mouse_hide(void)
{
if (IS_MOUSE_VISIBLE(vsp)) {
inverse_char(vsp->mouse);
vsp->mouse_flags &= ~MOUSE_VISIBLE;
}
}
/*
* function to move the cursor to a relative new position
*/
void
mouse_moverel(char dx, char dy)
{
unsigned short old_mouse = vsp->mouse;
unsigned char mouse_col = (vsp->mouse % vsp->maxcol);
unsigned char mouse_row = (vsp->mouse / vsp->maxcol);
if (scrnsv_active) /* if the screen saver is active */
pcvt_scrnsv_reset(); /* unblank NOW ! */
/* update position */
if (mouse_col + dx >= MAXCOL)
mouse_col = MAXCOL;
else
if (mouse_col + dx <= 0)
mouse_col = 0;
else
mouse_col += dx;
if (mouse_row + dy >= MAXROW)
mouse_row = MAXROW;
else
if (mouse_row + dy <= 0)
mouse_row = 0;
else
mouse_row += dy;
vsp->mouse = XY_TO_X(mouse_col, mouse_row);
/* if we have moved */
if (old_mouse != vsp->mouse) {
/* hide the previous cursor, if not in a selection */
if (IS_MOUSE_VISIBLE(vsp) && (!IS_SEL_IN_PROGRESS(vsp)))
inverse_char(old_mouse);
if (IS_SEL_IN_PROGRESS(vsp)) {
/* selection in progress */
mouse_copy_extend();
}
else {
inverse_char(vsp->mouse);
vsp->mouse_flags |= MOUSE_VISIBLE;
}
}
}
/*
* function to video inverse a character of the display
*/
void
inverse_char(unsigned short pos)
{
u_short *current_char = vsp->Crtat + pos;
u_short inverse = *current_char;
if ((inverse >> 8) == 0)
inverse = (FG_LIGHTGREY << 8);
inverse = (((inverse >> 8) & 0x88) | ((((inverse >> 8) >> 4) |
((inverse >> 8) << 4)) & 0x77)) << 8;
*current_char = inverse | ((*current_char) & 0x00ff);
}
/*
* Function to video inverse a region of the display
* start must be inferior to end
*/
void
inverse_region(unsigned short start, unsigned short end)
{
unsigned short current_pos;
unsigned char c = 0;
current_pos = start;
while (current_pos <= end) {
inverse_char(current_pos++);
c++;
}
}
/*
* Function which returns the number of contiguous blank characters between
* the right margin if border == 1 or between the next non-blank character
* and the current mouse cursor if border == 0
*/
unsigned char
skip_spc_right(char border)
{
unsigned short *current;
unsigned short *limit;
unsigned char mouse_col = (vsp->cpy_end % vsp->maxcol);
unsigned char res = 0;
current = vsp->Crtat + vsp->cpy_end;
limit = current + (vsp->maxcol - mouse_col - 1);
while (((*current & 0x00ff) == ' ') && (current <= limit)) {
current++;
res++;
}
if (border == BORDER) {
if (current > limit)
return (res - 1);
else
return (0);
}
else
return (res - 1);
}
/*
* Function which returns the number of contiguous blank characters between
* the first of the contiguous blank characters and the current mouse cursor
*/
unsigned char
skip_spc_left(void)
{
unsigned short *current;
unsigned short *limit;
unsigned char mouse_col = (vsp->mouse % vsp->maxcol);
unsigned char res = 0;
current = vsp->Crtat + vsp->cpy_start;
limit = current - mouse_col;
while (((*current & 0x00ff) == ' ') && (current >= limit)) {
current--;
res++;
}
if (res)
res--;
return (res);
}
/*
* Function which find the first blank beginning after the current cursor
* position
*/
unsigned char
skip_char_right(void)
{
unsigned short *current;
unsigned short *limit;
unsigned char mouse_col = (vsp->mouse % vsp->maxcol);
unsigned char res = 0;
current = vsp->Crtat + vsp->cpy_end;
limit = current + (vsp->maxcol - mouse_col - 1);
while (((*current & 0x00ff) != ' ') && (current <= limit)) {
current++;
res++;
}
if (res)
res--;
return (res);
}
/*
* Function which find the first non-blank character before the cursor position
*/
unsigned char
skip_char_left(void)
{
unsigned short *current;
unsigned short *limit;
unsigned char mouse_col = (vsp->mouse % vsp->maxcol);
unsigned char res = 0;
current = vsp->Crtat + vsp->cpy_start;
limit = current - mouse_col;
while (((*current & 0x00ff) != ' ') && (current >= limit)) {
current--;
res++;
}
if (res)
res--;
return (res);
}
/*
* Function to handle beginning of a copy operation
*/
void
mouse_copy_start(void)
{
unsigned char right;
/* if no selection, then that's the first one */
if (!Paste_avail)
Paste_avail = 1;
/* remove the previous selection */
if (IS_SEL_EXISTS(vsp)) {
remove_selection();
}
/* initial show of the cursor */
if (!IS_MOUSE_VISIBLE(vsp))
inverse_char(vsp->mouse);
vsp->cpy_start = vsp->mouse;
vsp->cpy_end = vsp->mouse;
vsp->cpy_orig = vsp->cpy_start; /* for correct action in
remove_selection() */
right = skip_spc_right(BORDER); /* useful later, in mouse_copy_extend */
if (right)
vsp->mouse_flags |= BLANK_TO_EOL;
vsp->mouse_flags |= SEL_IN_PROGRESS;
vsp->mouse_flags |= SEL_EXISTS;
/* mouse cursor hidden in the selection */
vsp->mouse_flags &= ~MOUSE_VISIBLE;
}
/*
* Function to handle copy of the word under the cursor
*/
void
mouse_copy_word()
{
unsigned char right;
unsigned char left;
if (IS_SEL_EXISTS(vsp))
remove_selection();
if (IS_SEL_IN_PROGRESS(vsp))
vsp->mouse_flags &= ~SEL_IN_PROGRESS;
if (IS_MOUSE_VISIBLE(vsp))
inverse_char(vsp->mouse);
vsp->cpy_start = vsp->mouse;
vsp->cpy_end = vsp->mouse;
if (IS_ALPHANUM(vsp->mouse)) {
right = skip_char_right();
left = skip_char_left();
}
else {
right = skip_spc_right(NO_BORDER);
left = skip_spc_left();
}
vsp->cpy_start -= left;
vsp->cpy_end += right;
vsp->cpy_orig = vsp->cpy_start;
inverse_region(vsp->cpy_start, vsp->cpy_end);
vsp->mouse_flags |= SEL_EXISTS;
/* mouse cursor hidden in the selection */
vsp->mouse_flags &= ~MOUSE_VISIBLE;
}
/*
* Function to handle copy of the current line
*/
void
mouse_copy_line(void)
{
unsigned char row = vsp->mouse / vsp->maxcol;
if (IS_SEL_EXISTS(vsp))
remove_selection();
if (IS_SEL_IN_PROGRESS(vsp))
vsp->mouse_flags &= ~(SEL_IN_PROGRESS);
if (IS_MOUSE_VISIBLE(vsp))
inverse_char(vsp->mouse);
vsp->cpy_start = row * vsp->maxcol;
vsp->cpy_end = vsp->cpy_start + MAXCOL;
vsp->cpy_orig = vsp->cpy_start;
inverse_region(vsp->cpy_start, vsp->cpy_end);
vsp->mouse_flags |= SEL_EXISTS;
vsp->mouse_flags &= ~MOUSE_VISIBLE;
}
/*
* Function to handle the end of a copy operation
*/
void
mouse_copy_end(void)
{
vsp->mouse_flags &= ~(SEL_IN_PROGRESS);
}
/*
* Function to extend a previously selected region
*/
void
mouse_copy_extend()
{
unsigned char right;
if (IS_SEL_EXISTS(vsp)) {
if (IS_BLANK_TO_EOL(vsp)) {
/*
* First extension of selection. We handle special
* cases of blank characters to eol
*/
right = skip_spc_right(BORDER);
if (vsp->mouse > vsp->cpy_orig) {
/* the selection goes to the lower part of
the screen */
/* remove the previous cursor, start of
selection is now next line */
inverse_char(vsp->cpy_start);
vsp->cpy_start += (right + 1);
vsp->cpy_end = vsp->cpy_start;
vsp->cpy_orig = vsp->cpy_start;
/* simulate the initial mark */
inverse_char(vsp->cpy_start);
}
else {
/* the selection goes to the upper part
of the screen */
/* remove the previous cursorm, start of
selection is now at the eol */
inverse_char(vsp->cpy_start);
vsp->cpy_orig += (right + 1);
vsp->cpy_start = vsp->cpy_orig - 1;
vsp->cpy_end = vsp->cpy_orig - 1;
/* simulate the initial mark */
inverse_char(vsp->cpy_start);
}
vsp->mouse_flags &= ~ BLANK_TO_EOL;
}
if (vsp->mouse < vsp->cpy_orig
&& vsp->cpy_end >= vsp->cpy_orig) {
/* we go to the upper part of the screen */
/* reverse the old selection region */
remove_selection();
vsp->cpy_end = vsp->cpy_orig - 1;
vsp->cpy_start = vsp->cpy_orig;
}
if (vsp->cpy_start < vsp->cpy_orig
&& vsp->mouse >= vsp->cpy_orig) {
/* we go back to the lower part of the screen */
/* reverse the old selection region */
remove_selection();
vsp->cpy_start = vsp->cpy_orig;
vsp->cpy_end = vsp->cpy_orig - 1;
}
if (vsp->mouse >= vsp->cpy_orig) {
/* lower part of the screen */
if (vsp->mouse > vsp->cpy_end)
/* extending selection */
inverse_region(vsp->cpy_end + 1,vsp->mouse);
else
/* reducing selection */
inverse_region(vsp->mouse + 1,vsp->cpy_end);
vsp->cpy_end = vsp->mouse;
}
else {
/* upper part of the screen */
if (vsp->mouse < vsp->cpy_start)
/* extending selection */
inverse_region(vsp->mouse,vsp->cpy_start - 1);
else
/* reducing selection */
inverse_region(vsp->cpy_start,vsp->mouse - 1);
vsp->cpy_start = vsp->mouse;
}
}
else {
/* no selection yet! */
sysbeep(PCVT_SYSBEEPF / 1500, hz / 4);
}
}
/*
* Function to remove a previously selected region
*/
void
remove_selection()
{
if (vsp->cpy_start < vsp->cpy_orig)
/* backward selection */
inverse_region(vsp->cpy_start, vsp->cpy_orig - 1);
else
/* forward selection */
inverse_region(vsp->cpy_start, vsp->cpy_end);
}
/*
* Function to get the uid of the user behind the *shell* on the current tty
* We handle su and sudo cases...
*/
uid_t
current_uid(void)
{
pid_t pg; /* process group id */
struct proc *owner_proc;
pg = vsp->vs_tty->t_pgrp->pg_id;
owner_proc = pfind(pg);
if (!owner_proc) {
Paste_avail = 0; /* this selection will never be available
because the process doesn't exist... */
return (0); /* the uid of root, just to be sure */
}
/*
* We use the real user id and not the *effective* one, as a foreground
* setuid process could permit to paste selection of another user
*/
return (owner_proc->p_cred->p_ruid);
}
/*
* Function to put the current visual selection in the selection buffer
*/
void
mouse_copy_selection(void)
{
unsigned short current = 0;
unsigned short blank = current;
unsigned short buf_end = ((vsp->maxcol + 1) * vsp->screen_rows);
unsigned short *sel_cur;
unsigned short *sel_end;
if (vsp->cpy_start < vsp->cpy_orig) {
/* backward selection */
sel_cur = vsp->Crtat + vsp->cpy_end;
sel_end = vsp->Crtat + vsp->cpy_orig - 1;
}
else {
/* forward selection */
sel_cur = vsp->Crtat + vsp->cpy_start;
sel_end = vsp->Crtat + vsp->cpy_end;
}
while (sel_cur <= sel_end && current < buf_end - 1) {
Copybuffer[current] = ((*sel_cur) & 0x00ff);
if (!IS_SPACE(Copybuffer[current]))
blank = current + 1; /* first blank after non-blank */
current++;
if (XABS_TO_XREL(sel_cur) == MAXCOL) {
/* we are on the last col of the screen */
Copybuffer[blank] = '\r'; /* carriage return */
current = blank + 1; /* restart just after the carriage
return in the buffer */
blank = current;
}
sel_cur++;
}
Copybuffer[current] = '\0';
Copyowner = current_uid();
}
/*
* Function to paste the current selection
*/
void
mouse_paste(void)
{
unsigned short len;
char *current = Copybuffer;
uid_t cur_uid;
cur_uid = current_uid();
if (Paste_avail && ((cur_uid == Copyowner) || !cur_uid)) {
/* either the owner of the selection or root */
len = strlen(Copybuffer);
for (; len > 0; len--) {
(*linesw[vsp->vs_tty->t_line].l_rint)
(*current++, vsp->vs_tty);
}
}
else
sysbeep(PCVT_SYSBEEPF / 1500, hz / 4);
}
/*
* Function to handle button clicks
*/
void
mouse_button(int button, int clicks)
{
if (scrnsv_active) /* if the screen saver is active */
pcvt_scrnsv_reset(); /* unblank NOW ! */
switch (button) {
case MOUSE_COPY_BUTTON:
switch (clicks % 4) {
case 0: /* button is up */
mouse_copy_end();
mouse_copy_selection();
break;
case 1: /* single click */
mouse_copy_start();
mouse_copy_selection();
break;
case 2: /* double click */
mouse_copy_word();
mouse_copy_selection();
break;
case 3: /* triple click */
mouse_copy_line();
mouse_copy_selection();
break;
default:
break;
}
break;
case MOUSE_PASTE_BUTTON:
switch (clicks) {
case 0: /* button is up */
break;
default: /* paste */
mouse_paste();
break;
}
break;
case MOUSE_EXTEND_BUTTON:
switch (clicks) {
case 0: /* button is up */
break;
default: /* extend the selection */
mouse_copy_extend();
mouse_copy_selection();
break;
}
break;
default:
break;
}
}
/*
* Function to handle the z axis
* The z axis (roller or wheel) is mapped by default to scrollback
*/
void
mouse_zaxis(int z)
{
scrollback_mouse(z);
}
|