-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathos_linux.pas
673 lines (561 loc) · 18.1 KB
/
os_linux.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
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
{
This file is part of the Mufasa Macro Library (MML)
Copyright (c) 2009-2012 by Raymond van Venetië and Merlijn Wajer
MML is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MML 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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MML. If not, see <http://www.gnu.org/licenses/>.
See the file COPYING, included in this distribution,
for details about the copyright.
Linux OS specific implementation for Mufasa Macro Library
}
{$mode objfpc}{$H+}
unit os_linux;
{
TODO's:
- Allow selecting a different X display
- Fix keyboard layout / SendString
}
interface
uses
Classes, SysUtils, mufasatypes, mufasabase, IOManager,
xlib, x, xutil, XKeyInput, ctypes, syncobjs;
type
TNativeWindow = x.TWindow;
TKeyInput = class(TXKeyInput)
public
procedure Down(Key: Word);
procedure Up(Key: Word);
end;
{ TWindow }
TWindow = class(TWindow_Abstract)
public
constructor Create(display: PDisplay; screennum: integer; window: x.TWindow);
destructor Destroy; override;
procedure GetTargetDimensions(out w, h: integer); override;
procedure GetTargetPosition(out left, top: integer); override;
function ReturnData(xs, ys, width, height: Integer): TRetData; override;
procedure FreeReturnData; override;
function GetError: String; override;
function ReceivedError: Boolean; override;
procedure ResetError; override;
function TargetValid: boolean; override;
function MouseSetClientArea(x1, y1, x2, y2: integer): boolean; override;
procedure MouseResetClientArea; override;
function ImageSetClientArea(x1, y1, x2, y2: integer): boolean; override;
procedure ImageResetClientArea; override;
procedure ActivateClient; override;
procedure GetMousePosition(out x,y: integer); override;
procedure MoveMouse(x,y: integer); override;
procedure HoldMouse(x,y: integer; button: TClickType); override;
procedure ReleaseMouse(x,y: integer; button: TClickType); override;
function IsMouseButtonHeld( button : TClickType) : boolean;override;
procedure SendString(str: string; keywait, keymodwait: integer); override;
procedure HoldKey(key: integer); override;
procedure ReleaseKey(key: integer); override;
function IsKeyHeld(key: integer): boolean; override;
function GetKeyCode(c : char) : integer;override;
function GetNativeWindow: TNativeWindow;
private
{ display is the connection to the X server }
display: PDisplay;
{ screen-number and selected window }
screennum: integer;
window: x.TWindow;
{ Reference to the XImage }
buffer: PXImage;
{ For memory-leak checks }
dirty: Boolean; //true if image loaded
{ KeyInput class }
keyinput: TKeyInput;
{ X Error Handler }
oldXHandler: TXErrorHandler;
{ (Forced) Client Area }
mx1, my1, mx2, my2: integer;
ix1, iy1, ix2, iy2: integer;
mcaset, icaset: Boolean;
procedure MouseApplyAreaOffset(var x, y: integer);
procedure ImageApplyAreaOffset(var x, y: integer);
end;
TIOManager = class(TIOManager_Abstract)
public
constructor Create;
constructor Create(plugin_dir: string);
function SetTarget(target: TNativeWindow): integer; overload;
procedure SetDesktop; override;
function GetProcesses: TSysProcArr; override;
procedure SetTargetEx(Proc: TSysProc); overload;
private
procedure NativeInit; override;
procedure NativeFree; override;
public
display: PDisplay;
screennum: integer;
desktop: x.TWindow;
end;
function MufasaXErrorHandler(para1:PDisplay; para2:PXErrorEvent):cint; cdecl;
implementation
uses GraphType, interfacebase, lcltype;
{ PROBLEM: .Create is called on the main thread. ErrorCS etc aren't
created on other threads. We will create them on the fly...
More info below...}
threadvar
xerror: string;
threadvar
ErrorCS: syncobjs.TCriticalSection;
{
This is extremely hacky, but also very useful.
We have to install a X error handler, because otherwise X
will terminate our entire app on error.
Since we want the right thread to recieve the right error, we have to
fiddle a bit with threadvars, mutexes / semaphores.
Another problem is that the script thread is initialised on the main thread.
This means that all (threadvar!) semaphores initialised on the mainthread
are NOT initialised on the script thread, which has yet to be started.
Therefore, we check if it hasn't been created yet.
** Horrible solution, but WFM **
This is the Handler function.
}
function MufasaXErrorHandler(para1:PDisplay; para2:PXErrorEvent):cint; cdecl;
begin
case para2^.error_code of
1: xerror := 'BadRequest';
2: xerror := 'BadValue';
3: xerror := 'BadWindow';
4: xerror := 'BadPixmap';
5: xerror := 'BadAtom';
6: xerror := 'BadCursor';
7: xerror := 'BadFont';
8: xerror := 'BadMatch';
9: xerror := 'BadDrawable';
10: xerror := 'BadAccess';
11: xerror := 'BadAlloc';
12: xerror := 'BadColor';
13: xerror := 'BadGC';
14: xerror := 'BadIDChoice';
15: xerror := 'BadName';
16: xerror := 'BadLength';
17: xerror := 'BadImplementation';
else
xerror := 'UNKNOWN';
end;
result := 0;
mDebugLn('X Error: ' + xerror);
mDebugLn('Error code: ' + inttostr(para2^.error_code));
mDebugLn('Display: ' + inttostr(LongWord(para2^.display)));
mDebugLn('Minor code: ' + inttostr(para2^.minor_code));
mDebugLn('Request code: ' + inttostr(para2^.request_code));
mDebugLn('Resource ID: ' + inttostr(para2^.resourceid));
mDebugLn('Serial: ' + inttostr(para2^.serial));
mDebugLn('Type: ' + inttostr(para2^._type));
end;
{ TKeyInput }
procedure TKeyInput.Down(Key: Word);
begin
DoDown(Key);
end;
procedure TKeyInput.Up(Key: Word);
begin
DoUp(Key);
end;
{ TWindow }
function TWindow.GetError: String;
begin
exit(xerror);
end;
function TWindow.ReceivedError: Boolean;
begin
result := xerror <> '';
end;
procedure TWindow.ResetError;
begin
xerror := '';
end;
{ See if the semaphores / CS are initialised }
constructor TWindow.Create(display: PDisplay; screennum: integer; window: x.TWindow);
begin
inherited Create;
self.display:= display;
self.screennum:= screennum;
self.window:= window;
self.dirty:= false;
self.keyinput:= TKeyInput.Create;
self.mx1 := 0; self.my1 := 0; self.mx2 := 0; self.my2 := 0;
self.mcaset := false;
self.ix1 := 0; self.iy1 := 0; self.ix2 := 0; self.iy2 := 0;
self.icaset := false;
xerror := '';
{ XXX FIXME TODO O GOD WTF }
if not assigned(ErrorCS) then
ErrorCS := syncobjs.TCriticalSection.Create;
ErrorCS.Enter;
try
oldXHandler:=XSetErrorHandler(@MufasaXErrorHandler);
finally
ErrorCS.Leave;
end;
end;
destructor TWindow.Destroy;
var
erh: TXErrorHandler;
begin
FreeReturnData;
keyinput.Free;
{ XXX FIXME TODO O GOD WTF }
if not assigned(ErrorCS) then
ErrorCS := syncobjs.TCriticalSection.Create;
ErrorCS.Enter;
erh := XSetErrorHandler(oldXHandler);
try
if erh <> @MufasaXErrorHandler then
XSetErrorHandler(erh);
finally
ErrorCS.Leave;
end;
inherited Destroy;
end;
function TWindow.GetNativeWindow: TNativeWindow;
begin
result := self.window;
end;
procedure TWindow.GetTargetDimensions(out w, h: integer);
var
Attrib: TXWindowAttributes;
begin
if icaset then
begin
w := ix2 - ix1;
h := iy2 - iy1;
exit;
end;
if XGetWindowAttributes(display, window, @Attrib) <> 0 Then
begin
W := Attrib.Width;
H := Attrib.Height;
end else
begin
W := -1;
H := -1;
end;
end;
procedure TWindow.GetTargetPosition(out left, top: integer);
var
Attrib: TXWindowAttributes;
begin
if XGetWindowAttributes(display, window, @Attrib) <> 0 Then
begin
left := Attrib.x;
top := attrib.y;
end else
begin
// XXX: this is tricky; what do we return when it doesn't exist?
// The window can very well be at -1, -1. We'll return 0 for now.
left := 0;
top := 0;
end;
end;
function TWindow.TargetValid: boolean;
var
Attrib: TXWindowAttributes;
begin
XGetWindowAttributes(display, window, @Attrib);
result := not ReceivedError;
end;
{ SetClientArea allows you to use a part of the actual client as a virtual
client. Im other words, all mouse,find functions will be relative to the
virtual client.
XXX:
I realise I can simply add a[x,y]1 to all of the functions rather than check
for caset (since they're 0 if it's not set), but I figured it would be more
clear this way.
}
function TWindow.MouseSetClientArea(x1, y1, x2, y2: integer): boolean;
var w, h: integer;
begin
{ TODO: What if the client resizes (shrinks) and our ``area'' is too large? }
GetTargetDimensions(w, h);
if ((x2 - x1) > w) or ((y2 - y1) > h) then
exit(False);
if (x1 < 0) or (y1 < 0) then
exit(False);
mx1 := x1; my1 := y1; mx2 := x2; my2 := y2;
mcaset := True;
end;
procedure TWindow.MouseResetClientArea;
begin
mx1 := 0; my1 := 0; mx2 := 0; my2 := 0;
mcaset := False;
end;
function TWindow.ImageSetClientArea(x1, y1, x2, y2: integer): boolean;
var w, h: integer;
begin
{ TODO: What if the client resizes (shrinks) and our ``area'' is too large? }
GetTargetDimensions(w, h);
if ((x2 - x1) > w) or ((y2 - y1) > h) then
exit(False);
if (x1 < 0) or (y1 < 0) then
exit(False);
ix1 := x1; iy1 := y1; ix2 := x2; iy2 := y2;
icaset := True;
end;
procedure TWindow.ImageResetClientArea;
begin
ix1 := 0; iy1 := 0; ix2 := 0; iy2 := 0;
icaset := False;
end;
procedure TWindow.MouseApplyAreaOffset(var x, y: integer);
begin
if mcaset then
begin
x := x + mx1;
y := y + my1;
end;
end;
procedure TWindow.ImageApplyAreaOffset(var x, y: integer);
begin
if icaset then
begin
x := x + ix1;
y := y + iy1;
end;
end;
procedure TWindow.ActivateClient;
begin
XSetInputFocus(display,window,RevertToParent,CurrentTime);
XFlush(display);
if ReceivedError then
raise Exception.Create('Error: ActivateClient: ' + GetError);
end;
function TWindow.ReturnData(xs, ys, width, height: Integer): TRetData;
var
w,h: integer;
begin
GetTargetDimensions(w,h);
if (xs < 0) or (xs + width > w) or (ys < 0) or (ys + height > h) then
raise Exception.CreateFMT('TMWindow.ReturnData: The parameters passed are wrong; xs,ys %d,%d width,height %d,%d',[xs,ys,width,height]);
if dirty then
raise Exception.CreateFmt('ReturnData was called again without freeing'+
' the previously used data. Do not forget to'+
' call FreeReturnData', []);
ImageApplyAreaOffset(xs, ys);
buffer := XGetImage(display, window, xs, ys, width, height, AllPlanes, ZPixmap);
if buffer = nil then
begin
mDebugLn('ReturnData: XGetImage Error. Dumping data now:');
mDebugLn('xs, ys, width, height: ' + inttostr(xs) + ', ' + inttostr(ys) +
', ' + inttostr(width) + ', ' + inttostr(height));
Result.Ptr := nil;
Result.IncPtrWith := 0;
raise Exception.CreateFMT('TMWindow.ReturnData: ReturnData: XGetImage Error', []);
exit;
end;
Result.Ptr := PRGB32(buffer^.data);
Result.IncPtrWith := 0;
Result.RowLen := width;
dirty:= true;
//XSetErrorHandler(Old_Handler);
end;
procedure TWindow.FreeReturnData;
begin
if dirty then
begin
if (buffer <> nil) then
XDestroyImage(buffer);
buffer:= nil;
dirty:= false;
end;
end;
procedure TWindow.GetMousePosition(out x,y: integer);
var
event: TXButtonEvent;
begin
FillChar(event, SizeOf(event), 0);
XQueryPointer(display, window,
@event.root, @event.window,
@event.x_root, @event.y_root,
@event.x, @event.y,
@event.state);
x := event.x;
y := event.y;
x := x - mx1;
y := y - my1;
end;
procedure TWindow.MoveMouse(x,y: integer);
begin
MouseApplyAreaOffset(x, y);
XWarpPointer(display, None, window, 0, 0, 0, 0, X, Y);
XFlush(display);
end;
procedure TWindow.HoldMouse(x,y: integer; button: TClickType);
var
event: TXButtonPressedEvent;
begin
FillChar(event, SizeOf(event), 0);
case button of
mouse_Left: event.button := Button1;
mouse_Middle: event.button := Button2;
mouse_Right: event.button := Button3;
end;
event.same_screen := cint(0);
event.subwindow := window;
while (event.subwindow <> None) do
begin
event.window := event.subwindow;
XQueryPointer(display, event.window,
@event.root, @event.subwindow,
@event.x_root, @event.y_root,
@event.x, @event.y,
@event.state);
end;
event._type := ButtonPress;
XSendEvent(display, PointerWindow, True, ButtonPressMask, @event);
XFlush(display);
end;
procedure TWindow.ReleaseMouse(x,y: integer; button: TClickType);
var
event: TXButtonReleasedEvent;
begin
FillChar(event, SizeOf(event), 0);
case button of
mouse_Left: event.button := Button1;
mouse_Middle: event.button := Button2;
mouse_Right: event.button := Button3;
end;
event.same_screen := cint(0);
event.subwindow := window;
while (event.subwindow <> None) do
begin
event.window := event.subwindow;
XQueryPointer(display, event.window,
@event.root, @event.subwindow,
@event.x_root, @event.y_root,
@event.x, @event.y,
@event.state);
end;
event._type := ButtonRelease;
XSendEvent(display, PointerWindow, True, ButtonReleaseMask, @event);
XFlush(display);
end;
function TWindow.IsMouseButtonHeld(button: TClickType): boolean;
var
event: TXEvent;
begin
FillChar(event, SizeOf(event), 0);
XQueryPointer(display, event.xbutton.window,
@event.xbutton.root, @event.xbutton.window,
@event.xbutton.x_root, @event.xbutton.y_root,
@event.xbutton.x, @event.xbutton.y,
@event.xbutton.state);
case button of
mouse_Left: Result := ((event.xbutton.state and Button1Mask) > 0);
mouse_Middle: Result := ((event.xbutton.state and Button2Mask) > 0);
mouse_Right: Result := ((event.xbutton.state and Button3Mask) > 0);
else
Result := False;
end;
end;
{ TODO: Check if this supports multiple keyboard layouts, probably not }
procedure TWindow.SendString(str: string; keywait, keymodwait: integer);
var
I, L: Integer;
K: Byte;
HoldShift: Boolean;
begin
HoldShift := False;
L := Length(str);
for I := 1 to L do
begin
if (((str[I] >= 'A') and (str[I] <= 'Z')) or
((str[I] >= '!') and (str[I] <= '&')) or
((str[I] >= '(') and (str[I] <= '+')) or
(str[I] = ':') or
((str[I] >= '<') and (str[I] <= '@')) or
((str[I] >= '^') and (str[I] <= '_')) or
((str[I] >= '{') and (str[I] <= '~'))) then
begin
HoldKey(VK_SHIFT);
HoldShift := True;
sleep(keymodwait shr 1);
end;
K := GetKeyCode(str[I]);
HoldKey(K);
if keywait <> 0 then
Sleep(keywait);
ReleaseKey(K);
if (HoldShift) then
begin
HoldShift := False;
sleep(keymodwait shr 1);
ReleaseKey(VK_SHIFT);
end;
end;
end;
procedure TWindow.HoldKey(key: integer);
begin
keyinput.Down(key);
end;
procedure TWindow.ReleaseKey(key: integer);
begin
keyinput.Up(key);
end;
function TWindow.IsKeyHeld(key: integer): boolean;
begin
raise Exception.CreateFmt('IsKeyDown isn''t implemented yet on Linux', []);
end;
function TWindow.GetKeyCode(c: char): integer;
begin
case C of
'0'..'9' :Result := VK_0 + Ord(C) - Ord('0');
'a'..'z' :Result := VK_A + Ord(C) - Ord('a');
'A'..'Z' :Result := VK_A + Ord(C) - Ord('A');
' ' : result := VK_SPACE;
else
Raise Exception.CreateFMT('GetSimpleKeyCode - char (%s) is not in A..z',[c]);
end
end;
{ ***implementation*** IOManager }
constructor TIOManager.Create;
begin
inherited Create;
end;
constructor TIOManager.Create(plugin_dir: string);
begin
inherited Create(plugin_dir);
end;
procedure TIOManager.NativeInit;
begin
display := XOpenDisplay(nil);
if display = nil then
raise Exception.Create('Could not open a connection to the X Display');
{ DefaultScreen }
screennum:= DefaultScreen(display);
{ Get the Desktop Window }
desktop:= RootWindow(display,screennum)
end;
procedure TIOManager.NativeFree;
begin
XCloseDisplay(display);
end;
procedure TIOManager.SetDesktop;
begin
SetBothTargets(TWindow.Create(display, screennum, desktop));
end;
function TIOManager.SetTarget(target: x.TWindow): integer;
begin
result := SetBothTargets(TWindow.Create(display, screennum, target))
end;
function TIOManager.GetProcesses: TSysProcArr;
begin
raise Exception.Create('GetProcesses: Not Implemented.');
end;
procedure TIOManager.SetTargetEx(Proc: TSysProc);
begin
raise Exception.Create('SetTargetEx: Not Implemented.');
end;
end.