Your Search Results

    Standard Libraries per OS

    Windows

    To learn about all the Windows functions and which DLL file you will need to call, there are two options. Go to the MSDN website and find the function then scroll down to the "Requirements" section and in it will be a line titled "Library" and "DLL", that is the library you will want to use in the ctypes.open statement.

    Alternatively you can download and use the PowerBASIC WIN32.HLP file. Download it here: http://www.powerbasic.com/files/pub/mstools/win32.zip

    Example: GetCursorPos

    I wanted to get the mouse cursor position without using a MouseMove listener as this is a high overhead event listener. So I searched on Bing, "Get cursor coordinates" and I came across GetCursorPos for Windows. I then searched it on MSDN and saw that it needed the user32.dll. (MSDN - GetCursorPos)

    This example was created on Windows XP SP2. It is a full working example, just copy & paste it to try it out.

    Components.utils.import("resource://gre/modules/ctypes.jsm");
    var lib = ctypes.open("user32.dll");
    
    /* Declare the signature of the function we are going to call */
    /* note: if you go to GetCursorPos page on MSDN it says that x and y are
     * type Long, and in data types Long on MSDN it says it's a 64-byte signed
     * integer, so before ctypes.int I was trying ctypes.int64_t
     * but it wouldnt work, no clue why ctypes.int was the solution */
    const struct_Point = new ctypes.StructType("Point",
                                               [ { "x": ctypes.int },
                                                 { "y": ctypes.int } ]);
    var GetCursorPos = lib.declare('GetCursorPos',
                                   ctypes.winapi_abi,
                                   ctypes.bool,
                                   struct_Point.ptr);
    
    /* Use it like this */
    var point = new struct_Point;
    var ret = GetCursorPos(point.address());
    
    Components.utils.reportError(ret);
    Components.utils.reportError(point);
    
    lib.close();
    

    Linux

    Example: XQueryPointer

    Get mouse cursor position and some extra information.

    Components.utils.import("resource://gre/modules/ctypes.jsm");
    let X11 = ctypes.open("libX11.so.6");
    let Display_ptr = ctypes.voidptr_t;
    let XOpenDisplay = X11.declare("XOpenDisplay",
                                   ctypes.default_abi,
                                   Display_ptr,
                                   ctypes.char.ptr);
    let Window = ctypes.int;
    let XRootWindow = X11.declare("XRootWindow",
                                  ctypes.default_abi,
                                  Window,
                                  Display_ptr,
                                  ctypes.int);
    let XQueryPointer = X11.declare("XQueryPointer",
                                    ctypes.default_abi,
                                    ctypes.bool,
                                    Display_ptr, Window,
                                    Window.ptr, Window.ptr,
                                    ctypes.int.ptr, ctypes.int.ptr,
                                    ctypes.int.ptr, ctypes.int.ptr,
                                    ctypes.unsigned_int.ptr);
    let XCloseDisplay = X11.declare("XCloseDisplay",
                                    ctypes.default_abi,
                                    ctypes.int,
                                    Display_ptr);
    
    let display = XOpenDisplay(null);
    let rootWindow = XRootWindow(display, 0);
    
    let root = new Window();
    let child = new Window();
    let rootX = new ctypes.int();
    let rootY = new ctypes.int();
    let windowX = new ctypes.int();
    let windowY = new ctypes.int();
    let mask = new ctypes.unsigned_int();
    
    XQueryPointer(display, rootWindow,
                  root.address(), child.address(),
                  rootX.address(), rootY.address(),
                  windowX.address(), windowY.address(),
                  mask.address());
    
    XCloseDisplay(display);
    
    Components.utils.reportError(rootX.value + "," + rootY.value);
    
    X11.close();
    

    Mac OS X

    Mac OS X has two way to call Standard OS Library, C-based API (Carbon, Core Graphics, etc) and Objective-C based API (Cocoa).

    Core Graphics

    To learn about all the Mac OS X APIs and which library file you will need to call, go to Mac Developer Library website and find the function then scroll down to "Declared In" section, and find which Framework contains the header file.

    Example: CGEventGetLocation

    For example, CGEventGetLocation function is declared in CGEvent.h header file, which is contained in CoreGraphics.framework. so /System/Library/Frameworks/CoreGraphics.framework/CoreGraphics is the library path to pass to ctypes.open function call.

    Components.utils.import("resource://gre/modules/ctypes.jsm");
    let CoreGraphics = ctypes.open("/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics");
    let CoreFoundation = ctypes.open("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation");
    
    let CGEventRef = ctypes.voidptr_t;
    let CGEventSourceRef = ctypes.voidptr_t;
    let CGEventCreate = CoreGraphics.declare("CGEventCreate",
                                             ctypes.default_abi,
                                             CGEventRef,
                                             CGEventSourceRef);
    let CGFloat = ctypes.float64_t;
    let CGPoint = new ctypes.StructType("CGPoint",
                                         [ { "x" : CGFloat },
                                           { "y" : CGFloat } ]);
    let CGEventGetLocation = CoreGraphics.declare("CGEventGetLocation",
                                                  ctypes.default_abi,
                                                  CGPoint,
                                                  CGEventRef);
    let CFTypeRef = ctypes.voidptr_t;
    let CFRelease = CoreFoundation.declare("CFRelease",
                                           ctypes.default_abi,
                                           ctypes.void_t,
                                           CGEventRef);
    
    let event = CGEventCreate(null);
    let cursor = CGEventGetLocation(event);
    
    CFRelease(event);
    
    Components.utils.reportError(cursor);
    
    CoreGraphics.close();
    CoreFoundation.close();
    

    Cocoa

    To call Objective-C based API from ctypes, use following functions in libobjc.dylib:

    • objc_getClass to get Class
    • sel_registerName to register selector name
    • objc_msgSend and some variants to send message to class and instance

    You can get any Objective-C class by calling objc_msgSend..

    Objective-C code can be translated into C code in following rule:

    // Objective-C code
    NSEvent loc = [NSEvent mouseLocation];
    
    // pseudo C code
    NSEvent loc = (NSPoint)objc_msgSend(objc_getClass("NSEvent"),
                                        sel_registerName("mouseLocation"));
    

    Example: [NSEvent mouseLocation]

    Components.utils.import("resource://gre/modules/ctypes.jsm");
    let objc = ctypes.open(ctypes.libraryName("objc"));
    
    let id = ctypes.voidptr_t;
    let SEL = ctypes.voidptr_t;
    let objc_getClass = objc.declare("objc_getClass",
                                     ctypes.default_abi,
                                     id,
                                     ctypes.char.ptr);
    let sel_registerName = objc.declare("sel_registerName",
                                        ctypes.default_abi,
                                        SEL,
                                        ctypes.char.ptr);
    let objc_msgSend = objc.declare("objc_msgSend",
                                    ctypes.default_abi,
                                    id,
                                    id,
                                    SEL,
                                    "...");
    let CGFloat = ctypes.float64_t;
    let NSPoint = new ctypes.StructType("NSPoint",
                                        [ { "x" : CGFloat },
                                          { "y" : CGFloat } ]);
    // note: [NSEvent mouseLocation] returns NSPoint struct,
    // which is small enough to return in register,
    // so we don't need to use objc_msgSend_stret.
    let objc_msgSend_NSPoint = objc.declare("objc_msgSend",
                                            ctypes.default_abi,
                                            NSPoint,
                                            id,
                                            SEL,
                                            "...");
    
    // pool = [[NSAutoreleasePool alloc] init]
    let NSAutoreleasePool = objc_getClass("NSAutoreleasePool");
    let alloc = sel_registerName("alloc");
    let init = sel_registerName("init");
    let pool = objc_msgSend(objc_msgSend(NSAutoreleasePool, alloc), init);
    
    // loc = [NSEvent mouseLocation]
    let NSEvent = objc_getClass("NSEvent");
    let mouseLocation = sel_registerName("mouseLocation");
    let loc = objc_msgSend_NSPoint(NSEvent, mouseLocation);
    
    // [pool mouseLocation]
    let release = sel_registerName("release");
    objc_msgSend(pool, release);
    
    Components.utils.reportError(loc);
    
    objc.close();
    

    Document Tags and Contributors

    Contributors to this page: Noitidart
    Last updated by: Noitidart,