Initial test with X11 on Haxe!

Hello everyone, how are you?

I made initial with cpp on Ubuntu 22.04 and I expect that X11 works fine on without hxcpp. It works fine like in C++/C and It can bind with @:native() and makes simply.

I expect that haxe works more then C#. Is it correctly? Since I have big problem with C# DLLImport with forcing structures with void*. C# can’t do for XRectangle* or XRectangle[] like in XDrawRectangles()

I don’t know how does haxe work? I will test soon…

Code: Example:

package;

import cpp.UInt32;
import cpp.Int32;
import cpp.UInt64;

@:native("Display")
@:include("/usr/include/X11/Xlib.h")
extern private class XDisplay {}
typedef Display = cpp.Pointer<XDisplay>;

@:native("XID")
@:include("/usr/include/X11/Xlib.h")
extern private class XID {}
@:native("Drawable")
@:include("/usr/include/X11/Xlib.h")
typedef Drawable = XID;
@:native("Window")
@:include("/usr/include/X11/Xlib.h")
typedef Window = Drawable;

@:include("/usr/include/X11/Xlib.h")
extern class Xlib
{
    public static inline final NONE:UInt64 = 0;

    @:native("XOpenDisplay")
    static public function OpenDisplay(display_name:String):Display;

    @:native("XCloseDisplay")
    static public function CloseDisplay(display:Display):Void;

    @:native("XDefaultScreen")
    static public function DefaultScreen(display:Display):Int32;

    @:native("XRootWindow")
    static public function RootWindow(display:Display, screen_number:Int32):Window;

    @:native("XCreateSimpleWindow")
    static public function CreateSimpleWindow(display:Display, parent:Window, x:Int32, y:Int32, w:UInt32, h:UInt32, border_width:UInt32, border:UInt64, background:UInt64):Window;

    @:native("XPending")
    static public function Pending(display:Display):Int32;

    @:native("XMapWindow")
    static public function MapWindow(display:Display, window:Window):Int32;

    @:native("XDestroyWindow")
    static public function DestroyWindow(display:Display, window:Window):Void;

    @:native("XStoreName")
    static public function StoreName(display:Display, window:Window, window_title:String):Int32;
}

@:buildXml("
    <target id='haxe'>
       <lib name='-lX11' if='linux'/>
    </target>
")

class Main
{
    static var display:Display = null;
    static var screen_number:Int32;
    static var main_window:Window = null;

	public static function main():Void
	{
		display = Xlib.OpenDisplay(null);
        if (display == null)
        {
            trace("Error: Initializing display of X11");
        }

        screen_number = Xlib.DefaultScreen(display);

        var root:Window = Xlib.RootWindow(display, screen_number);

        main_window = Xlib.CreateSimpleWindow(display, root, 100, 100, 400, 320, 10, 0xffff0000, 0xffff5500);
        if (main_window == null)
        {
            trace("Error: creating window of X11");
        }
        Xlib.StoreName(display, main_window, "Haxe X11!");
        trace("Wohoo! X11 Window on Haxe!");
        while (Xlib.Pending(display) == 0)
        {
            Xlib.MapWindow(display, main_window);
        }
        Xlib.CloseDisplay(display);
	}
}

and build.hxml:

-cp src
-main Main
-lib hxcpp

-cpp build/haxe_x11

Type haxe build.hxm and it runs like charm.
Enjoy your happy coding on Ubuntu, OpenSuse or Distros

3 Likes

PS: Please help me! I have problem with creating library because it throws error like src/Main.hx:6: characters 20-27 : Missing ;

I feel annoying that. Because I want create library with haxelib.json but it seems nothing find like i have tried to haxelib dev x11 x11 ← current directory from x11 and I want import to Main.hx but it seems failed. why???

Please help me. I have already readden creating library then it looks nothing successfully.

but I already imported for dependencies:

    "dependencies": {
        "hxcpp":"4.2.1"
    }

It doesn’t work if I import cpp.Pointer;
then it shows error underline under vscode but why?
image

But why???

Can I have to know how do I fix if I make haxe library for haxelib.

Hello,

cpp.Pointer is underlined because

  • your build target is not cpp
  • you use import or using after a declaration
1 Like