This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmouse.pas
505 lines (453 loc) · 11.5 KB
/
mouse.pas
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
{
This file is part of the Free Pascal run time library.
Copyright (c) 1999-2000 by Florian Klaempfl
member of the Free Pascal development team
Mouse unit for linux
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**********************************************************************}
unit Mouse;
interface
{$if defined(aix) or defined(bsd) or defined(solaris)}
{$define NOMOUSE}
{$endif}
{$ifdef NOMOUSE}
{$DEFINE NOGPM}
{$ENDIF}
{$i mouseh.inc}
implementation
uses
BaseUnix,Video
{$ifndef NOGPM}
,gpm,linuxvcs
{$endif ndef NOGPM}
;
{$i mouse.inc}
{$ifndef NOMOUSE}
const
WaitMouseMove : boolean = false;
PrintMouseCur : boolean = false;
mousecurofs : longint = -1;
var
mousecurcell : TVideoCell;
SysLastMouseEvent : TMouseEvent;
const
gpm_fs : longint = -1;
{$ifndef NOGPM}
procedure GPMEvent2MouseEvent(const e:Tgpm_event;var mouseevent:tmouseevent);
var
PrevButtons : byte;
begin
PrevButtons:=SysLastMouseEvent.Buttons;
if e.x>0 then
mouseevent.x:=e.x-1
else
MouseEvent.x:=0;
if e.y>0 then
MouseEvent.y:=e.y-1
else
MouseEvent.y:=0;
MouseEvent.buttons:=0;
if e.buttons and Gpm_b_left<>0 then
inc(MouseEvent.buttons,1);
if e.buttons and Gpm_b_right<>0 then
inc(MouseEvent.buttons,2);
if e.buttons and Gpm_b_middle<>0 then
inc(MouseEvent.buttons,4);
case (e.EventType and $f) of
GPM_MOVE,
GPM_DRAG :
begin
MouseEvent.Action:=MouseActionMove;
WaitMouseMove:=false;
end;
GPM_DOWN :
begin
MouseEvent.Action:=MouseActionDown;
WaitMouseMove:=false;
end;
GPM_UP :
begin
{ gpm apparently sends the button that is left up
while mouse unit expects the button state after
the button was released PM }
if MouseEvent.Buttons<>0 then
begin
MouseEvent.Buttons:=MouseEvent.Buttons xor PrevButtons;
MouseEvent.Action:=MouseActionUp;
end
{ this does probably never happen...
but its just a security PM }
else
MouseEvent.Action:=MouseActionMove;
WaitMouseMove:=false;
end;
else
MouseEvent.Action:=MouseActionMove;
end;
end;
{$ENDIF}
procedure PlaceMouseCur(ofs:longint);
var
upd : boolean;
begin
if (VideoBuf=nil) or (MouseCurOfs=Ofs) then
exit;
upd:=false;
if (MouseCurOfs<>-1) and (VideoBuf^[MouseCurOfs]=MouseCurCell) then
begin
VideoBuf^[MouseCurOfs]:=MouseCurCell xor $7f00;
upd:=true;
end;
MouseCurOfs:=ofs;
if (MouseCurOfs<>-1) then
begin
MouseCurCell:=VideoBuf^[MouseCurOfs] xor $7f00;
VideoBuf^[MouseCurOfs]:=MouseCurCell;
upd:=true;
end;
if upd then
Updatescreen(false);
end;
{Note: libgpm will initialize an xterm mouse if TERM=xterm.
However, this check sucks, because xterm is not the only terminal
with mouse. To make it worse, it assumes gpm should be used on
anything not xterm, while in reality only the Linux console has gpm.
Some distributions use a patched libgpm to work around this, but
to avoid this mess, we detect the xterm mouse ourselves (we need to
be able to do this anyway for the NOGPM case), and don't do any libgpm
call at all if an xterm mouse is detected. Of course, we use the
Pascal libgpm translation, doing it here allows us to keep the Pascal
one compatible with the external C one.
}
function detect_xterm_mouse:word;
const mouse_terminals:array[0..6] of string[7]=('cons','eterm','gnome',
'konsole','rxvt','screen',
'xterm');
xterm=6;
mouse_1003_capable=[xterm]; {xterm only for now}
var term:string;
i,t:shortint;
begin
detect_xterm_mouse:=0;
t:=-1;
term:=fpgetenv('TERM');
for i:=low(mouse_terminals) to high(mouse_terminals) do
if copy(term,1,length(mouse_terminals[i]))=mouse_terminals[i] then
begin
t:=i;
break;
end;
if t=xterm then
begin
{Rxvt sets TERM=xterm and COLORTERM=rxvt. Gnome does something similar.}
term:=fpgetenv('COLORTERM');
for i:=low(mouse_terminals) to high(mouse_terminals) do
if copy(term,1,length(mouse_terminals[i]))=mouse_terminals[i] then
begin
t:=i;
break;
end;
end;
if t>0 then
begin
detect_xterm_mouse:=1000;
{Can the terminal report all mouse events?}
if t in mouse_1003_capable then
detect_xterm_mouse:=1003;
end;
end;
procedure SysInitMouse;
{$ifndef NOGPM}
var connect:TGPMConnect;
e:Tgpm_event;
{$endif ndef NOGPM}
begin
{ if gpm_fs<>-1 then
runerror(240);}
{Test wether to use X-terminals.}
case detect_xterm_mouse of
1000:
begin
{Use the xterm mouse, report button events only.}
gpm_fs:=-1000;
{write(#27'[?1001s');} { save old hilit tracking }
write(#27'[?1000h'); { enable mouse tracking }
end;
1003:
begin
{Use the xterm mouse, report all mouse events.}
gpm_fs:=-1003;
write(#27'[?1003h'); { enable mouse tracking }
end;
end;
{$ifndef NOGPM}
{Use the gpm mouse?}
if (gpm_fs=-1) and (vcs_device<>-1) then
begin
{ open gpm }
connect.EventMask:=GPM_MOVE or GPM_DRAG or GPM_DOWN or GPM_UP;
connect.DefaultMask:=0;
connect.MinMod:=0;
connect.MaxMod:=0;
gpm_fs:=gpm_open(connect,0);
{ initialize SysLastMouseEvent }
if gpm_fs<>-1 then
begin
Gpm_GetSnapshot(e);
GPMEvent2MouseEvent(e,SysLastMouseEvent);
end;
end;
{$endif NOGPM}
end;
procedure SysDoneMouse;
begin
case gpm_fs of
-1:
HideMouse;
-1000:
begin
{xterm mouse}
write(#27'[?1000l'); { disable mouse tracking }
{write(#27'[?1001r');} { Restore old hilit tracking }
end;
-1003:
write(#27'[?1003l'); { disable mouse tracking }
{$ifndef NOGPM}
else
gpm_close;
{$endif}
end;
gpm_fs:=-1;
end;
function SysDetectMouse:byte;
{$ifndef NOGPM}
var
connect : TGPMConnect;
fds : tFDSet;
e : Tgpm_event;
{$endif ndef NOGPM}
begin
if detect_xterm_mouse<>0 then
SysDetectMouse:=2
{$ifndef NOGPM}
else
begin
if gpm_fs=-1 then
begin
connect.EventMask:=GPM_MOVE or GPM_DRAG or GPM_DOWN or GPM_UP;
connect.DefaultMask:=0;
connect.MinMod:=0;
connect.MaxMod:=0;
gpm_fs:=gpm_open(connect,0);
end;
if gpm_fs>=0 then
begin
fpFD_ZERO(fds);
fpFD_SET(gpm_fs,fds);
while fpSelect(gpm_fs+1,@fds,nil,nil,1)>0 do
begin
fillchar(e,sizeof(e),#0);
Gpm_GetEvent(e);
end;
end;
if gpm_fs<>-1 then
SysDetectMouse:=Gpm_GetSnapshot(nil)
else
SysDetectMouse:=0;
end
{$endif NOGPM};
end;
procedure SysGetMouseEvent(var MouseEvent: TMouseEvent);
{$ifndef NOGPM}
var
e : Tgpm_event;
{$endif ndef NOGPM}
begin
fillchar(MouseEvent,SizeOf(TMouseEvent),#0);
if gpm_fs<0 then
exit;
{$ifndef NOGPM}
Gpm_GetEvent(e);
GPMEvent2MouseEvent(e,MouseEvent);
SysLastMouseEvent:=MouseEvent;
{ update mouse cursor }
if PrintMouseCur then
PlaceMouseCur(MouseEvent.y*ScreenWidth+MouseEvent.x);
{$endif ndef NOGPM}
end;
function SysPollMouseEvent(var MouseEvent: TMouseEvent):boolean;
{$ifndef NOGPM}
var
e : Tgpm_event;
fds : tFDSet;
{$endif ndef NOGPM}
begin
fillchar(MouseEvent,SizeOf(TMouseEvent),#0);
{$ifndef NOGPM}
if gpm_fs<0 then
exit(false);
if gpm_fs>0 then
begin
fpFD_ZERO(fds);
fpFD_SET(gpm_fs,fds);
end;
if (fpSelect(gpm_fs+1,@fds,nil,nil,1)>0) then
begin
FillChar(e,SizeOf(e),#0);
{ Gpm_snapshot does not work here PM }
Gpm_GetEvent(e);
GPMEvent2MouseEvent(e,MouseEvent);
SysLastMouseEvent:=MouseEvent;
if (MouseEvent.Action<>0) then
begin
{ As we now use Gpm_GetEvent, we need to put in
in the MouseEvent queue PM }
PutMouseEvent(MouseEvent);
SysPollMouseEvent:=true;
{ update mouse cursor is also required here
as next call will read MouseEvent from queue }
if PrintMouseCur then
PlaceMouseCur(MouseEvent.y*ScreenWidth+MouseEvent.x);
end
else
SysPollMouseEvent:=false;
end
else
{$endif NOGPM}
SysPollMouseEvent:=false;
end;
function SysGetMouseX:word;
{$ifndef NOGPM}
var
me : TMouseEvent;
{$endif ndef NOGPM}
begin
if gpm_fs<0 then
exit(0);
{$ifndef NOGPM}
if PollMouseEvent(ME) then
begin
{ Remove mouse event, we are only interrested in
the X,Y so all other events can be thrown away }
GetMouseEvent(ME);
SysGetMouseX:=ME.X
end
else
begin
SysGetMouseX:=SysLastMouseEvent.x;
end;
{$endif ndef NOGPM}
end;
function SysGetMouseY:word;
{$ifndef NOGPM}
var
me : TMouseEvent;
{$endif ndef NOGPM}
begin
if gpm_fs<0 then
exit(0);
{$ifndef NOGPM}
if PollMouseEvent(ME) then
begin
{ Remove mouse event, we are only interrested in
the X,Y so all other events can be thrown away }
GetMouseEvent(ME);
SysGetMouseY:=ME.Y
end
else
begin
SysGetMouseY:=SysLastMouseEvent.y;
end;
{$endif ndef NOGPM}
end;
procedure SysShowMouse;
var
x,y : word;
begin
PrintMouseCur:=true;
{ Wait with showing the cursor until the mouse has moved. Else the
cursor updates will be to quickly }
if WaitMouseMove then
exit;
if (MouseCurOfs>=0) or (gpm_fs=-1) then
PlaceMouseCur(MouseCurOfs)
else
begin
x:=SysGetMouseX;
y:=SysGetMouseY;
if (x<=ScreenWidth) and (y<=ScreenHeight) then
PlaceMouseCur(Y*ScreenWidth+X)
else
PlaceMouseCur(MouseCurOfs);
end;
end;
procedure SysHideMouse;
begin
if (MouseCurOfs>=0) then
PlaceMouseCur(-1);
WaitMouseMove:=true;
PrintMouseCur:=false;
end;
function SysGetMouseButtons:word;
{$ifndef NOGPM}
var
me : TMouseEvent;
{$endif ndef NOGPM}
begin
if gpm_fs<0 then
exit(0);
{$ifndef NOGPM}
if PollMouseEvent(ME) then
begin
{ Remove mouse event, we are only interrested in
the buttons so all other events can be thrown away }
GetMouseEvent(ME);
SysGetMouseButtons:=ME.Buttons;
end
else
begin
SysGetMouseButtons:=SysLastMouseEvent.buttons;
end;
{$endif ndef NOGPM}
end;
Const
SysMouseDriver : TMouseDriver = (
UseDefaultQueue : true;
InitDriver : @SysInitMouse;
DoneDriver : @SysDoneMouse;
DetectMouse : @SysDetectMouse;
ShowMouse : @SysShowMouse;
HideMouse : @SysHideMouse;
GetMouseX : @SysGetMouseX;
GetMouseY : @SysGetMouseY;
GetMouseButtons : @SysGetMouseButtons;
SetMouseXY : Nil;
GetMouseEvent : @SysGetMouseEvent;
PollMouseEvent : @SysPollMouseEvent;
PutMouseEvent : Nil;
);
{$else ifndef NOMOUSE}
Const
SysMouseDriver : TMouseDriver = (
UseDefaultQueue : true;
InitDriver : Nil;
DoneDriver : Nil;
DetectMouse : Nil;
ShowMouse : Nil;
HideMouse : Nil;
GetMouseX : Nil;
GetMouseY : Nil;
GetMouseButtons : Nil;
SetMouseXY : Nil;
GetMouseEvent : Nil;
PollMouseEvent : Nil;
PutMouseEvent : Nil;
);
{$endif}
Begin
SetMouseDriver(SysMouseDriver);
end.