CGPoint point = CGEventGetLocation(event)

Mac OSX specific forum
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

CGPoint point = CGEventGetLocation(event)

Post 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?)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: CGPoint point = CGEventGetLocation(event)

Post 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
Last edited by wilbert on Tue Nov 26, 2019 6:11 am, edited 1 time in total.
Windows (x64)
Raspberry Pi OS (Arm64)
Justin
Addict
Addict
Posts: 832
Joined: Sat Apr 26, 2003 2:49 pm

Re: CGPoint point = CGEventGetLocation(event)

Post by Justin »

It works here with pb 5.7064 and Mac OS 10.12 I don't get any memory error.
Post Reply