Page 1 of 1

CGPoint point = CGEventGetLocation(event)

Posted: Mon Nov 25, 2019 3:13 pm
by Rinzwind
How to translate this to PB? Thought it would be easy... but the structure return makes me hit a wall.

ImportC ""
CGEventCreate(e)
CGEventGetLocation(e)

EndImport

e = CGEventCreate(0)
*p.CGPoint = CGEventGetLocation(e)
Debug *p\x

-> Illegal instruction (executing binary data?)

Re: CGPoint point = CGEventGetLocation(event)

Posted: Mon Nov 25, 2019 3:30 pm
by wilbert
With PB x64, when you call CGEventGetLocation, the CGPoint structure is returned in XMM0 and XMM1.

Code: Select all

ImportC ""
  CGEventCreate(e)
  CGEventGetLocation(e)
EndImport

p.CGPoint
e = CGEventCreate(0)
CGEventGetLocation(e)
!movsd [v_p], xmm0
!movsd [v_p + 8], xmm1
Debug p\x
Debug p\y
Explanation:
Possible ways on 64 bit macOS and Linux a structure is returned

* XMM0 - XMM1
* RAX - RDX
* RAX - XMM0
* ST0 - ST1
* Memory pointer provided by the caller (like a hidden first argument)

If a returned structure can be returned by registers (like CGPoint), it will.
If not, it is returned by memory the caller has to provide.



Another way to get the returned coordinates is by using my new VCall module
viewtopic.php?f=12&t=74092

Code: Select all

UseModule VCall

VCReturn.VCReturn

*CGEventCreate = dlsym_(#RTLD_DEFAULT, "CGEventCreate")
*CGEventGetLocation = dlsym_(#RTLD_DEFAULT, "CGEventGetLocation")

Dim Args.VCArgument(1)
Args(0)\i = 0
e = VCall(*CGEventCreate, @Args(), @"i")

Args(0)\i = @VCReturn
Args(1)\i = e
VCall(*CGEventGetLocation, @Args(), @"ri")

p.CGPoint
p\x = VCReturn\xmm0
p\y = VCReturn\xmm1

Debug p\x
Debug p\y

Re: CGPoint point = CGEventGetLocation(event)

Posted: Mon Nov 25, 2019 9:52 pm
by Justin
It works here with pb 5.7064 and Mac OS 10.12 I don't get any memory error.