Carbon / LW problem
I've been writing small programs in LW using the FLI to call various Carbon
function calls. I first write the small program in C, then convert it to
LW by hand. I've done this for a couple different ones and they have been
working. However, in one I call CGPostMouseEvent and it does not seem to
function properly.
Instead of synthesizing a mouse click as it should (and as the C version
does), it goes to the top left corner of the screen, (0,0) I suppose, and
somewhat stalls LW. LW can be revived by pressing apple-option-esc
(actually force quitting it is not necessary, closing the force quit window
is enough to bring LW back)
The translation is fine, since it returns a 0, noErr. I used
CGPostKeyboardEvent, synthesizing a keyboard event, in a different program
and that one worked fine. I don't believe it to be a Carbon problem since
the C version works, and I can't see a problem with the lisp version.
I have included both the lisp version and the C version at the end of the
email. Note, in the lisp version the two commented out lines display the
current position of the cursor, which are correct, I don't know why its
moving to point (0,0). The function CGPostMouseEvent moves the cursor to a
point and synthesizes a click, I provide it with the current position of
the cursor so that it doesn't move the mouse, only clicks it.
Thank you for any help!
Stefano B
the C version
-----------------------------------------
//cc -prebind -no-cpp-precomp -O3 -framework Carbon -o mouseclick
mouse-click.c
#import <Carbon/Carbon.h>
int main(int argc, char* argv[])
{
Point mousePoint;
CGPoint position;
CGEventErr err1, err2;
GetGlobalMouse(&mousePoint);
//printf("%d\t%d\n", mousePoint.v, mousePoint.h);
position.x = mousePoint.h;
position.y = mousePoint.v;
err1 = CGPostMouseEvent(position, 1, 1, 1);
err2 = CGPostMouseEvent(position, 1, 1, 0);
//printf("%d\t%d\n", err1, err2);
return 0;
}
the lisp version
-----------------------------------------
(fli:define-c-struct (point (:foreign-name "Point"))
(y :short)
(x :short))
(fli:define-c-typedef (point (:foreign-name "Point"))
(:struct point))
(fli:define-c-struct (cgpoint (:foreign-name "CGPoint"))
(x :short)
(y :short))
(fli:define-c-typedef (cgpoint (:foreign-name "CGPoint"))
(:struct cgpoint))
(fli:define-c-typedef (boolean-t (:foreign-name "boolean_t")) :int)
(fli:define-c-typedef (cgbuttoncount (:foreign-name "CGButtonCount")) :int)
(fli:define-foreign-function (getglobalmouse "GetGlobalMouse" :source)
((p (:pointer point)))
:result-type :void
:language :c)
(fli:define-foreign-function (cgpostmouseevent "CGPostMouseEvent" :source)
((mouse-position cgpoint)
(update-mouse-position (:boolean :int))
(button-count cgbuttoncount)
(mouse-button-down (:boolean :int)))
:result-type :int
:language :c)
(defun click-mouse ()
(fli:with-dynamic-foreign-objects ()
(let ((point (fli:allocate-foreign-object :type 'point))
(cg-mouse-point (fli:allocate-foreign-object :type 'cgpoint))
(bool-val (fli:allocate-foreign-object :type 'boolean-t)))
(getglobalmouse point)
(setf bool-val 1)
(setf (fli:foreign-slot-value cg-mouse-point 'x)
(fli:foreign-slot-value point 'x))
(setf (fli:foreign-slot-value cg-mouse-point 'y)
(fli:foreign-slot-value point 'y))
;(pprint (fli:foreign-slot-value cg-mouse-point 'x))
;(pprint (fli:foreign-slot-value cg-mouse-point 'y))
(cgpostmouseevent (fli:dereference
cg-mouse-point
:copy-foreign-object nil) bool-val 1 bool-val)
(cgpostmouseevent (fli:dereference
cg-mouse-point
:copy-foreign-object nil) 1 1 0)
)))