Window creation/X11: Difference between revisions

Content added Content deleted
No edit summary
(→‎{{header|Perl 6}}: Minimal changes to make runnable)
Line 824: Line 824:
=={{header|Perl 6}}==
=={{header|Perl 6}}==
{{trans|C}}
{{trans|C}}
{{broken|Perl 6}}


There is not yet a X11 library in Perl 6 but we can write the minimal C mappings for this task.
There is not yet a X11 library in Perl 6 but we can write the minimal C mappings for this task.
Notice that the mapping for XEvent requires a manual set up which depends on the number of bits of your machine. This is due to a current bug in NativeCall on MoarVM.


<lang perl6>use NativeCall;
<lang perl6>use NativeCall;


class Display is repr('CStruct') {}
class Display is repr('CStruct') {
has int32 $!screen;
class GC is repr('CStruct') {}
has int32 $!window;
}
class GC is repr('CStruct') {
has int32 $!context;
}
class XEvent is repr('CStruct') {
class XEvent is repr('CStruct') {
has int32 $.type; # for 32 bits machine
has int32 $.type;
#has int $.type; # for 64 bits machine
method init { $!type = 0 }
method init { $!type = 0 }
}
}


sub XOpenDisplay(Str $name = ':0') returns Display is native('libX11') { * }
sub XOpenDisplay(Str $name = ':0') returns Display is native('X11') { * }
sub XDefaultScreen(Display $) returns int is native('libX11') { * }
sub XDefaultScreen(Display $) returns int32 is native('X11') { * }
sub XRootWindow(Display $, int $screen_number) returns int is native('libX11') { * }
sub XRootWindow(Display $, int32 $screen_number) returns int32 is native('X11') { * }
sub XBlackPixel(Display $, int $screen_number) returns int is native('libX11') { * }
sub XBlackPixel(Display $, int32 $screen_number) returns int32 is native('X11') { * }
sub XWhitePixel(Display $, int $screen_number) returns int is native('libX11') { * }
sub XWhitePixel(Display $, int32 $screen_number) returns int32 is native('X11') { * }
sub XCreateSimpleWindow(
sub XCreateSimpleWindow(
Display $, int $parent_window, int $x, int $y,
Display $, int32 $parent_window, int32 $x, int32 $y,
int $width, int $height, int $border_width,
int32 $width, int32 $height, int32 $border_width,
int $border, int $background
int32 $border, int32 $background
) returns int is native('libX11') { * }
) returns int32 is native('X11') { * }
sub XMapWindow(Display $, int $window) is native('libX11') { * }
sub XMapWindow(Display $, int32 $window) is native('X11') { * }
sub XSelectInput(Display $, int $window, int $mask) is native('libX11') { * }
sub XSelectInput(Display $, int32 $window, int32 $mask) is native('X11') { * }
sub XFillRectangle(
sub XFillRectangle(
Display $, int $window, GC $, int $x, int $y, int $width, int $height
Display $, int32 $window, GC $, int32 $x, int32 $y, int32 $width, int32 $height
) is native('libX11') { * }
) is native('X11') { * }
sub XDrawString(
sub XDrawString(
Display $, int $window, GC $, int $x, int $y, Str $, int $str_length
Display $, int32 $window, GC $, int32 $x, int32 $y, Str $, int32 $str_length
) is native('libX11') { * }
) is native('X11') { * }
sub XDefaultGC(Display $, int $screen) returns GC is native('libX11') { * }
sub XDefaultGC(Display $, int32 $screen) returns GC is native('X11') { * }
sub XNextEvent(Display $, XEvent $e) is native('libX11') { * }
sub XNextEvent(Display $, XEvent $e) is native('X11') { * }
sub XCloseDisplay(Display $) is native('libX11') { * }
sub XCloseDisplay(Display $) is native('X11') { * }


my Display $display = XOpenDisplay()
my Display $display = XOpenDisplay()
Line 879: Line 881:
XNextEvent($display, $e);
XNextEvent($display, $e);
if $e.type == 12 {
if $e.type == 12 {
XFillRectangle($display, $window, XDefaultGC($display, $screen), 20, 20, 10, 10);
XFillRectangle($display, $window, XDefaultGC($display, $screen), 20, 20, 10, 10);
XDrawString($display, $window, XDefaultGC($display, $screen), 10, 50, $msg, my int $ = $msg.chars);
XDrawString($display, $window, XDefaultGC($display, $screen), 10, 50, $msg, my int $ = $msg.chars);
}
}
elsif $e.type == 2 {
elsif $e.type == 2 {
last;
last;
}
}
}
}