You're confusing somewhat a Display, which is an object that you use to interact with X, and a display name, which is a string that you can use to identify and connect to a Display.
Also, you're best off using repr('CPointer')
, not repr('CStruct')
for an XDisplay *
, since the Xlib documentation says that it should be treated as an opaque pointer and everything accessed through functions, and translating the whole struct to Raku would be laborious (and not very useful).
Since it is like an object in that way, you have the option of encapsulating all of the Xlib functions inside of the Display
class, and wrapping them in methods, as shown in the pointers section of the NativeCall docs. My translation of the code you have so far to this style would be:
use NativeCall;
class Display is repr('CPointer') {
sub XOpenDisplay(Str) returns Display is native('X11') { * }
method new(Str $name = ':0') {
XOpenDisplay($name);
}
# You can wrap a libX11 function in a method explicitly, like this...
sub XDisplayString(Display) returns Str is native('X11') { * }
method DisplayString {
XDisplayString(self);
}
# Or implicitly, by putting 'is native' on a method declaration
# and using 'is symbol' to match the names
# (self will be passed as the first argument to the libX11 function)
method DefaultScreen() returns int32 is native('X11')
is symbol('XDefaultScreen') { * }
method Str {
"Display({ self.DisplayString })";
}
}
my Display $display .= new
or die "Can not open display";
my int $screen = $display.DefaultScreen;
print "display = <" ~ $display ~ ">\n";
print "screen = <" ~ $screen ~ ">\n";
I provided a method Str
for Display
, so that it can be printed directly, but again, don't confuse the object with the string that you use to get hold of one. You would go on from here by adding more methods to Display
, as well as defining Screen
, Window
, etc. in the same way. You don't have to do it this way -- you're free to define the subs publicly and use calls to XOpenDisplay and everything else, as if you were writing C, but the OO style tends to be a bit cleaner. The choice is yours.