I'm making a mess of this C code on Mac!

BlitzMax Forums/BlitzMax Programming/I'm making a mess of this C code on Mac!

Grey Alien(Posted 2007) [#1]
Hi, I'm trying to get a handle to the active window on Mac OS so that I can use it later to check the window position. But I'm having trouble passing values back from the C function into Bmax. Can anyone help please as this is really not my area of expertise...Thanks!

First here's the C code I've got (saved in GAGMacLib.m):

#include <AppKit/AppKit.h>

NSWindow macGetActiveWindow(){

    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];

    NSApplication *myApp;

    myApp=[NSApplication sharedApplication];

    NSWindow *myWindow = [myApp keyWindow];

    [pool release];

	return *myWindow;
}


It supposed to return an NSWindow class to the calling code.

I want to store that class (well a pointer to it) in Blitz so that later on I can call this C code:

void macGetWindowOrigin(NSWindow *myWindow, int *x, int *y){
    NSRect myFrame = [myWindow frame];
    *x = myFrame.origin.x;
    *y = myFrame.origin.y;
}


So how do I store a pointer to the NSWindow class and then pass it into the second function later? I have this code in which print appears to print some kind of value but then the app just crashes.


Strict

Import "../include/GAGMacLib.m"

Extern
	Function macGetWindowOrigin(handle: Int Ptr, x:Int Ptr, y:Int Ptr)
	Function macGetActiveWindow:Int Ptr()
End Extern
Graphics 640,480, 0

Global WindowHandle: Int Ptr = macGetActiveWindow()
Print WindowHandle[0]

While Not KeyDown(KEY_ESCAPE)

    Cls

    DrawText "testing...",10,10
    Flip

Wend

End




grable(Posted 2007) [#2]
I think you need to return the pointer to the class.

NSWindow* macGetActiveWindow(){

    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];

    NSApplication *myApp;

    myApp=[NSApplication sharedApplication];

    NSWindow *myWindow = [myApp keyWindow];

    [pool release];

	return myWindow;
}


And rather store it as an int, no need for blitz to know its a pointer.

Try it, i have no mac ;)


Grey Alien(Posted 2007) [#3]
Yes it works, thanks!


Grey Alien(Posted 2007) [#4]
OK little more help please. I'm calling Grable's code like this:

Global WindowHandle: Int Ptr = macGetActiveWindow()


and then passing it into this C function:

void macGetWindowOrigin(NSWindow *myWindow, int *x, int *y){
    NSRect myFrame = [myWindow frame];
    *x = myFrame.origin.x;
    *y = myFrame.origin.y;
}


like this:

Local p:TPoint = New TPoint
	macGetWindowOrigin(WindowHandle, Varptr p.x,Varptr p.y)


This all works fine and I get the expected result. However, I'd really like to store the WindowHandle as an INT not an Int Ptr because I've already got an Integer WindowHandle variable in my framework which is returned by GetActiveWindow() in Windows. So I'd like to use the same variable to store the Macs WindowHandle instead of having a separate one which is an Int Ptr.

oh the externs look like this:

Extern
	Function macGetCursorPos(x:Int Ptr, y:Int Ptr)
	Function macGetWindowOrigin(handle: Int Ptr, x:Int Ptr, y:Int Ptr)
	Function macGetActiveWindow:Int Ptr()
End Extern

There *must* be a really simple way of converting an Int Ptr into and int (for storage) and then back to an Int Ptr later on when passing into the macGetWindowOrigin function, but I just can't figure it out. Have tried all sorts. Any ideas? Thanks.


grable(Posted 2007) [#5]
Just make macGetActiveWindow return an Int and make macGetWindowOrigin take an Int. they wont mind.

And btw, make those X/Y int ptr's into Var params, saves you some typing ;)


N(Posted 2007) [#6]
By the way, that's not C. That's Objective-C. Just thought I'd point out that there's a difference.


Grey Alien(Posted 2007) [#7]
Yeah I figured out that the weird square bracket stuff was abnormal for C.