Window creation/X11: Difference between revisions

Added Yabasic
(→‎{{header|TXR}}: Correct link. We can't link to the X11 sub-section because #X11_3 is ambiguous.)
(Added Yabasic)
 
(36 intermediate revisions by 18 users not shown)
Line 6:
Implementations of this task should   ''avoid using a toolkit''   as much as possible.
<br><br>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program creatFenX1164.s */
/* link with gcc options -lX11 -L/usr/lpp/X11/lib */
 
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
.equ ClientMessage, 33
 
/*******************************************/
/* Initialized data */
/*******************************************/
.data
szRetourligne: .asciz "\n"
szMessErreur: .asciz "Server X11 not found.\n"
szMessErrfen: .asciz "Error create X11 window.\n"
 
szLibDW: .asciz "WM_DELETE_WINDOW" // message close window
 
/*******************************************/
/* UnInitialized data */
/*******************************************/
.bss
.align 4
qDisplay: .skip 8 // Display address
qDefScreen: .skip 8 // Default screen address
identWin: .skip 8 // window ident
wmDeleteMessage: .skip 16 // ident close message
stEvent: .skip 400 // provisional size
 
buffer: .skip 500
 
/**********************************************/
/* -- Code section */
/**********************************************/
.text
.global main // program entry
main:
mov x0,#0 // open server X
bl XOpenDisplay
cmp x0,#0
beq erreur
// Ok return Display address
ldr x1,qAdrqDisplay
str x0,[x1] // store Display address for future use
mov x28,x0 // and in register 28
// load default screen
ldr x2,[x0,#264] // at location 264
ldr x1,qAdrqDefScreen
str x2,[x1] //store default_screen
mov x2,x0
ldr x0,[x2,#232] // screen list
 
//screen areas
ldr x5,[x0,#+88] // white pixel
ldr x3,[x0,#+96] // black pixel
ldr x4,[x0,#+56] // bits par pixel
ldr x1,[x0,#+16] // root windows
// create window x11
mov x0,x28 //display
mov x2,#0 // position X
mov x3,#0 // position Y
mov x4,600 // weight
mov x5,400 // height
mov x6,0 // bordure ???
ldr x7,0 // ?
ldr x8,qBlanc // background
str x8,[sp,-16]! // argument fot stack
bl XCreateSimpleWindow
add sp,sp,16 // for stack alignement
cmp x0,#0 // error ?
beq erreurF
//mov x3,sp
ldr x1,qAdridentWin
str x0,[x1] // store window ident for future use
mov x27,x0 // and in register 27
 
// Correction of window closing error
mov x0,x28 // Display address
ldr x1,qAdrszLibDW // atom name address
mov x2,#1 // False create atom if not exist
bl XInternAtom
cmp x0,#0
ble erreurF
ldr x1,qAdrwmDeleteMessage // address message
str x0,[x1]
mov x2,x1 // address atom create
mov x0,x28 // display address
mov x1,x27 // window ident
mov x3,#1 // number of protocoles
bl XSetWMProtocols
cmp x0,#0
ble erreurF
// Display window
mov x1,x27 // ident window
mov x0,x28 // Display address
bl XMapWindow
 
1: // events loop
mov x0,x28 // Display address
ldr x1,qAdrstEvent // events structure address
bl XNextEvent
ldr x0,qAdrstEvent // events structure address
ldr w0,[x0] // type in 4 fist bytes
cmp w0,#ClientMessage // message for close window
bne 1b // no -> loop
 
ldr x0,qAdrstEvent // events structure address
ldr x1,[x0,56] // location message code
ldr x2,qAdrwmDeleteMessage // equal ?
ldr x2,[x2]
cmp x1,x2
bne 1b // no loop
 
mov x0,0 // end Ok
b 100f
erreurF: // error create window
ldr x0,qAdrszMessErrfen
bl affichageMess
mov x0,1
b 100f
erreur: // error no server x11 active
ldr x0,qAdrszMessErreur
bl affichageMess
mov x0,1
100: // program standard end
mov x8,EXIT
svc 0
qBlanc: .quad 0xF0F0F0F0
qAdrqDisplay: .quad qDisplay
qAdrqDefScreen: .quad qDefScreen
qAdridentWin: .quad identWin
qAdrstEvent: .quad stEvent
qAdrszMessErrfen: .quad szMessErrfen
qAdrszMessErreur: .quad szMessErreur
qAdrwmDeleteMessage: .quad wmDeleteMessage
qAdrszLibDW: .quad szLibDW
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
 
</syntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 12 ⟶ 163:
Using the X11 & plotutil libraries is not part of any of the original UNESCO/IFIP [[ALGOL 68]]
reports. As at December 2008 only [[ALGOL 68G]] comes with these built in.
<langsyntaxhighlight lang="algol68">FILE window;
draw device (window, "X", "600x400");
open (window, "Hello, World!", stand draw channel);
Line 21 ⟶ 172:
draw show (window);
VOID (read char);
close (window)</langsyntaxhighlight>
 
{{omit from|AutoHotkey}}
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
 
/* ARM assembly Raspberry PI */
/* program windows1.s */
 
/* compile with as */
/* link with gcc and options -lX11 -L/usr/lpp/X11/lib */
 
/********************************************/
/*Constantes */
/********************************************/
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
/* constantes X11 */
.equ KeyPressed, 2
.equ ButtonPress, 4
.equ MotionNotify, 6
.equ EnterNotify, 7
.equ LeaveNotify, 8
.equ Expose, 12
.equ ClientMessage, 33
.equ KeyPressMask, 1
.equ ButtonPressMask, 4
.equ ButtonReleaseMask, 8
.equ ExposureMask, 1<<15
.equ StructureNotifyMask, 1<<17
.equ EnterWindowMask, 1<<4
.equ LeaveWindowMask, 1<<5
.equ ConfigureNotify, 22
 
/*******************************************/
/* DONNEES INITIALISEES */
/*******************************************/
.data
szWindowName: .asciz "Windows Raspberry"
szRetourligne: .asciz "\n"
szMessDebutPgm: .asciz "Program start. \n"
szMessErreur: .asciz "Server X not found.\n"
szMessErrfen: .asciz "Can not create window.\n"
szMessErreurX11: .asciz "Error call function X11. \n"
szMessErrGc: .asciz "Can not create graphics context.\n"
szTitreFenRed: .asciz "Pi"
szTexte1: .asciz "Hello world."
.equ LGTEXTE1, . - szTexte1
szTexte2: .asciz "Press q for close window or clic X in system menu."
.equ LGTEXTE2, . - szTexte2
szLibDW: .asciz "WM_DELETE_WINDOW" @ special label for correct close error
 
/*************************************************/
szMessErr: .ascii "Error code hexa : "
sHexa: .space 9,' '
.ascii " decimal : "
sDeci: .space 15,' '
.asciz "\n"
 
/*******************************************/
/* DONNEES NON INITIALISEES */
/*******************************************/
.bss
.align 4
ptDisplay: .skip 4 @ pointer display
ptEcranDef: .skip 4 @ pointer screen default
ptFenetre: .skip 4 @ pointer window
ptGC: .skip 4 @ pointer graphic context
key: .skip 4 @ key code
wmDeleteMessage: .skip 8 @ ident close message
event: .skip 400 @ TODO event size ??
PrpNomFenetre: .skip 100 @ window name proprety
buffer: .skip 500
iWhite: .skip 4 @ rgb code for white pixel
iBlack: .skip 4 @ rgb code for black pixel
/**********************************************/
/* -- Code section */
/**********************************************/
.text
.global main
 
main: @ entry of program
ldr r0,iAdrszMessDebutPgm @
bl affichageMess @ display start message on console linux
/* attention r6 pointer display*/
/* attention r8 pointer graphic context */
/* attention r9 ident window */
/*****************************/
/* OPEN SERVER X11 */
/*****************************/
mov r0,#0
bl XOpenDisplay @ open X server
cmp r0,#0 @ error ?
beq erreurServeur
ldr r1,iAdrptDisplay
str r0,[r1] @ store display address
mov r6,r0 @ and in register r6
ldr r2,[r0,#+132] @ load default_screen
ldr r1,iAdrptEcranDef
str r2,[r1] @ store default_screen
mov r2,r0
ldr r0,[r2,#+140] @ load pointer screen list
ldr r5,[r0,#+52] @ load value white pixel
ldr r4,iAdrWhite @ and store in memory
str r5,[r4]
ldr r3,[r0,#+56] @ load value black pixel
ldr r4,iAdrBlack @ and store in memory
str r3,[r4]
ldr r4,[r0,#+28] @ load bits par pixel
ldr r1,[r0,#+8] @ load root windows
/**************************/
/* CREATE WINDOW */
/**************************/
mov r0,r6 @ address display
mov r2,#0 @ window position X
mov r3,#0 @ window position Y
mov r8,#0 @ for stack alignement
push {r8}
push {r3} @ background = black pixel
push {r5} @ border = white pixel
mov r8,#2 @ border size
push {r8}
mov r8,#400 @ hauteur
push {r8}
mov r8,#600 @ largeur
push {r8}
bl XCreateSimpleWindow
add sp,#24 @ stack alignement 6 push (4 bytes * 6)
cmp r0,#0 @ error ?
beq erreurF
 
ldr r1,iAdrptFenetre
str r0,[r1] @ store window address in memory
mov r9,r0 @ and in register r9
/*****************************/
/* add window property */
/*****************************/
mov r0,r6 @ display address
mov r1,r9 @ window address
ldr r2,iAdrszWindowName @ window name
ldr r3,iAdrszTitreFenRed @ window name reduced
mov r4,#0
push {r4} @ parameters not use
push {r4}
push {r4}
push {r4}
bl XSetStandardProperties
add sp,sp,#16 @ stack alignement for 4 push
/**************************************/
/* for correction window close error */
/**************************************/
mov r0,r6 @ display address
ldr r1,iAdrszLibDW @ atom address
mov r2,#1 @ False créate atom if not exists
bl XInternAtom
cmp r0,#0 @ error X11 ?
ble erreurX11
ldr r1,iAdrwmDeleteMessage @ recept address
str r0,[r1]
mov r2,r1 @ return address
mov r0,r6 @ display address
mov r1,r9 @ window address
mov r3,#1 @ number of protocols
bl XSetWMProtocols
cmp r0,#0 @ error X11 ?
ble erreurX11
/**********************************/
/* create graphic context */
/**********************************/
mov r0,r6 @ display address
mov r1,r9 @ window address
mov r2,#0 @ not use for simply context
mov r3,#0
bl XCreateGC
cmp r0,#0 @ error ?
beq erreurGC
ldr r1,iAdrptGC
str r0,[r1] @ store address graphic context
mov r8,r0 @ and in r8
/****************************/
/* modif window background */
/****************************/
mov r0,r6 @ display address
mov r1,r9 @ window address
ldr r2,iGris1 @ background color
bl XSetWindowBackground
cmp r0,#0 @ error ?
ble erreurX11
/***************************/
/* OUF!! window display */
/***************************/
mov r0,r6 @ display address
mov r1,r9 @ window address
bl XMapWindow
/****************************/
/* Write text in the window */
/****************************/
mov r0,r6 @ display address
mov r1,r9 @ window address
mov r2,r8 @ address graphic context
mov r3,#50 @ position x
sub sp,#4 @ stack alignement
mov r4,#LGTEXTE1 - 1 @ size string
push {r4} @ on the stack
ldr r4,iAdrszTexte1 @ string address
push {r4}
mov r4,#100 @ position y
push {r4}
bl XDrawString
add sp,sp,#16 @ stack alignement 3 push and 1 stack alignement
cmp r0,#0 @ error ?
blt erreurX11
/* write text 2 */
mov r0,r6 @ display address
mov r1,r9 @ window address
mov r2,r8 @ address graphic context
mov r3,#10 @ position x
sub sp,#4 @ stack alignement
mov r4,#LGTEXTE2 - 1 @ size string
push {r4} @ on the stack
ldr r4,iAdrszTexte2 @ string address
push {r4}
mov r4,#350 @ position y
push {r4}
bl XDrawString
add sp,sp,#16 @ stack alignement 3 push and 1 stack alignement
cmp r0,#0 @ error ?
blt erreurX11
/****************************/
/* Autorisations */
/****************************/
mov r0,r6 @ display address
mov r1,r9 @ window address
ldr r2,iFenetreMask @ autorisation mask
bl XSelectInput
cmp r0,#0 @ error ?
ble erreurX11
/****************************/
/* Events loop */
/****************************/
1:
mov r0,r6 @ display address
ldr r1,iAdrevent @ events address
bl XNextEvent @ event ?
ldr r0,iAdrevent
ldr r0,[r0] @ code event
cmp r0,#KeyPressed @ key ?
bne 2f
ldr r0,iAdrevent @ yes read key in buffer
ldr r1,iAdrbuffer
mov r2,#255
ldr r3,iAdrkey
mov r4,#0
push {r4} @ stack alignement
push {r4}
bl XLookupString
add sp,#8 @ stack alignement 2 push
cmp r0,#1 @ is character key ?
bne 2f
ldr r0,iAdrbuffer @ yes -> load first buffer character
ldrb r0,[r0]
cmp r0,#0x71 @ character q for quit
beq 5f @ yes -> end
b 4f
2:
/* */
/* for example clic mouse button */
/************************************/
cmp r0,#ButtonPress @ clic mouse buton
bne 3f
ldr r0,iAdrevent
ldr r1,[r0,#+32] @ position X mouse clic
ldr r2,[r0,#+36] @ position Y
@ etc for eventuel use
b 4f
3:
cmp r0,#ClientMessage @ code for close window within error
bne 4f
ldr r0,iAdrevent
ldr r1,[r0,#+28] @ code message address
ldr r2,iAdrwmDeleteMessage @ equal code window créate ???
ldr r2,[r2]
cmp r1,r2
beq 5f @ yes -> end window
 
4: @ loop for other event
b 1b
/***********************************/
/* Close window -> free ressources */
/***********************************/
5:
mov r0,r6 @ display address
ldr r1,iAdrptGC
ldr r1,[r1] @ load context graphic address
bl XFreeGC
cmp r0,#0
blt erreurX11
mov r0,r6 @ display address
mov r1,r9 @ window address
bl XDestroyWindow
cmp r0,#0
blt erreurX11
mov r0,r6 @ display address
bl XCloseDisplay
cmp r0,#0
blt erreurX11
mov r0,#0 @ return code OK
b 100f
erreurF: @ create error window but possible not necessary. Display error by server
ldr r1,iAdrszMessErrfen
bl displayError
mov r0,#1 @ return error code
b 100f
erreurGC: @ error create graphic context
ldr r1,iAdrszMessErrGc
bl displayError
mov r0,#1
b 100f
erreurX11: @ erreur X11
ldr r1,iAdrszMessErreurX11
bl displayError
mov r0,#1
b 100f
erreurServeur: @ error no found X11 server see doc putty and Xming
ldr r1,iAdrszMessErreur
bl displayError
mov r0,#1
b 100f
 
100: @ standard end of the program
mov r7, #EXIT
svc 0
iFenetreMask: .int KeyPressMask|ButtonPressMask|StructureNotifyMask
iGris1: .int 0xFFA0A0A0
iAdrWhite: .int iWhite
iAdrBlack: .int iBlack
iAdrptDisplay: .int ptDisplay
iAdrptEcranDef: .int ptEcranDef
iAdrptFenetre: .int ptFenetre
iAdrptGC: .int ptGC
iAdrevent: .int event
iAdrbuffer: .int buffer
iAdrkey: .int key
iAdrszLibDW: .int szLibDW
iAdrszMessDebutPgm: .int szMessDebutPgm
iAdrszMessErreurX11: .int szMessErreurX11
iAdrszMessErrGc: .int szMessErrGc
iAdrszMessErreur: .int szMessErreur
iAdrszMessErrfen: .int szMessErrfen
iAdrszWindowName: .int szWindowName
iAdrszTitreFenRed: .int szTitreFenRed
iAdrszTexte1: .int szTexte1
iAdrszTexte2: .int szTexte2
iAdrPrpNomFenetre: .int PrpNomFenetre
iAdrwmDeleteMessage: .int wmDeleteMessage
/******************************************************************/
/* display text with size calculation */
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
push {r0,r1,r2,r7,lr} @ save registres
mov r2,#0 @ counter length
1: @ loop length calculation
ldrb r1,[r0,r2] @ read octet start position + index
cmp r1,#0 @ if 0 its over
addne r2,r2,#1 @ else add 1 in the length
bne 1b @ and loop
@ so here r2 contains the length of the message
mov r1,r0 @ address message in r1
mov r0,#STDOUT @ code to write to the standard output Linux
mov r7, #WRITE @ code call system "write"
svc #0 @ call systeme
pop {r0,r1,r2,r7,lr} @ restaur registers */
bx lr @ return
/***************************************************/
/* affichage message d erreur */
/***************************************************/
/* r0 contains error code r1 : message address */
displayError:
push {r0-r2,lr} @ save registers
mov r2,r0 @ save error code
mov r0,r1
bl affichageMess
mov r0,r2 @ error code
ldr r1,iAdrsHexa
bl conversion16 @ conversion hexa
mov r0,r2 @ error code
ldr r1,iAdrsDeci @ result address
bl conversion10 @ conversion decimale
ldr r0,iAdrszMessErr @ display error message
bl affichageMess
100:
pop {r0-r2,lr} @ restaur registers
bx lr @ return
iAdrszMessErr: .int szMessErr
iAdrsHexa: .int sHexa
iAdrsDeci: .int sDeci
/******************************************************************/
/* Converting a register to hexadecimal */
/******************************************************************/
/* r0 contains value and r1 address area */
conversion16:
push {r1-r4,lr} @ save registers
mov r2,#28 @ start bit position
mov r4,#0xF0000000 @ mask
mov r3,r0 @ save entry value
1: @ start loop
and r0,r3,r4 @value register and mask
lsr r0,r2 @ move right
cmp r0,#10 @ compare value
addlt r0,#48 @ <10 ->digit
addge r0,#55 @ >10 ->letter A-F
strb r0,[r1],#1 @ store digit on area and + 1 in area address
lsr r4,#4 @ shift mask 4 positions
subs r2,#4 @ counter bits - 4 <= zero ?
bge 1b @ no -> loop
 
100:
pop {r1-r4,lr} @ restaur registers
bx lr @return
/******************************************************************/
/* Converting a register to a decimal unsigned */
/******************************************************************/
/* r0 contains value and r1 address area */
/* r0 return size of result (no zero final in area) */
/* area size => 11 bytes */
.equ LGZONECAL, 10
conversion10:
push {r1-r4,lr} @ save registers
mov r3,r1
mov r2,#LGZONECAL
1: @ start loop
bl divisionpar10U @ unsigned r0 <- dividende. quotient ->r0 reste -> r1
add r1,#48 @ digit
strb r1,[r3,r2] @ store digit on area
cmp r0,#0 @ stop if quotient = 0
subne r2,#1 @ else previous position
bne 1b @ and loop
@ and move digit from left of area
mov r4,#0
2:
ldrb r1,[r3,r2]
strb r1,[r3,r4]
add r2,#1
add r4,#1
cmp r2,#LGZONECAL
ble 2b
@ and move spaces in end on area
mov r0,r4 @ result length
mov r1,#' ' @ space
3:
strb r1,[r3,r4] @ store space in area
add r4,#1 @ next position
cmp r4,#LGZONECAL
ble 3b @ loop if r4 <= area size
100:
pop {r1-r4,lr} @ restaur registres
bx lr @return
/***************************************************/
/* division par 10 unsigned */
/***************************************************/
/* r0 dividende */
/* r0 quotient */
/* r1 remainder */
divisionpar10U:
push {r2,r3,r4, lr}
mov r4,r0 @ save value
ldr r3,iMagicNumber @ r3 <- magic_number raspberry 1 2
umull r1, r2, r3, r0 @ r1<- Lower32Bits(r1*r0) r2<- Upper32Bits(r1*r0)
mov r0, r2, LSR #3 @ r2 <- r2 >> shift 3
add r2,r0,r0, lsl #2 @ r2 <- r0 * 5
sub r1,r4,r2, lsl #1 @ r1 <- r4 - (r2 * 2) = r4 - (r0 * 10)
pop {r2,r3,r4,lr}
bx lr @ leave function
iMagicNumber: .int 0xCCCCCCCD
</syntaxhighlight>
 
 
=={{header|BaCon}}==
 
<syntaxhighlight lang="c">
'--- added a flush to exit cleanly
PRAGMA LDFLAGS `pkg-config --cflags --libs x11`
PRAGMA INCLUDE <X11/Xlib.h>
PRAGMA INCLUDE <X11/Xutil.h>
 
OPTION PARSE FALSE
'---XLIB is so ugly
ALIAS XNextEvent TO EVENT
ALIAS XOpenDisplay TO DISPLAY
ALIAS DefaultScreen TO SCREEN
ALIAS XCreateSimpleWindow TO CREATE
ALIAS XCloseDisplay TO CLOSE_DISPLAY
ALIAS XSelectInput TO EVENT_TYPE
ALIAS XMapWindow TO MAP_EVENT
ALIAS XFillRectangle TO FILL_RECTANGLE
ALIAS XDrawString TO DRAW_STRING
ALIAS XFlush TO FLUSH
 
'---pointer to X Display structure
DECLARE d TYPE Display*
'---pointer to the newly created window
'DECLARE w TYPE WINDOW
'---pointer to the XEvent
DECLARE e TYPE XEvent
DECLARE msg TYPE char*
'--- number of screen to place the window on
DECLARE s TYPE int
msg = "Hello, World!"
d = DISPLAY(NULL)
IF d == NULL THEN
EPRINT "Cannot open display" FORMAT "%s%s\n"
END
END IF
s = SCREEN(d)
w = CREATE(d, RootWindow(d, s), 10, 10, 100, 100, 1,BlackPixel(d, s), WhitePixel(d, s))
EVENT_TYPE(d, w, ExposureMask | KeyPressMask)
MAP_EVENT(d, w)
WHILE (1)
EVENT(d, &e)
IF e.type == Expose THEN
FILL_RECTANGLE(d, w, DefaultGC(d, s), 20, 20, 10, 10)
DRAW_STRING(d, w, DefaultGC(d, s), 10, 50, msg, strlen(msg))
END IF
IF e.type == KeyPress THEN
BREAK
END IF
WEND
FLUSH(d)
CLOSE_DISPLAY(d)
</syntaxhighlight>
 
=={{header|C}}==
Line 31 ⟶ 728:
* gcc hello-x.c -L/usr/X11R6/lib -lX11 -o hello-x
 
<langsyntaxhighlight lang="c">#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
Line 40 ⟶ 737:
Window w;
XEvent e;
const char *msg = "Hello, World!";
int s;
 
Line 67 ⟶ 764:
XCloseDisplay(d);
return 0;
}</langsyntaxhighlight>
 
=== XCB ===
Line 75 ⟶ 772:
* gcc -o helloxcb helloxcb.c -lxcb
 
<langsyntaxhighlight lang="c">#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Line 157 ⟶ 854:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|COBOL}}==
Tested with GnuCOBOL, and only on 64bit architecture, GNU/Linux. Nods to the C XLib version.
 
<langsyntaxhighlight lang="cobol"> identification division.
program-id. x11-hello.
installation. cobc -x x11-hello.cob -lX11
Line 336 ⟶ 1,033:
 
goback.
end program x11-hello.</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
 
{{field attention|Common Lisp|X11|This example was written in near-total ignorance of X11 by consulting the [http://www.cliki.net/CLX%20Manual CLX manual] to find equivalents for the parts of the C example. It was also only tested with [[Mac OS X]] X11, which is not exactly normal. — (ps: Testing on Linux (Fedora 21/x86-64), it seems to display black-on-black, so the "Hello" is lost.)}}. Testing new code changes (i.e. adding background and foreground) should fix the issue, it was tested on LinuxMint 17.1/x86-64.
Also tested on Debian; with a minor change on the text position it creates a pixel perfect replica of the C example. Could this warning be removed then?}}
 
{{trans|C}}
Line 348 ⟶ 1,046:
This example uses CLX, which is the de facto standard X11 library for Common Lisp. CLX is not a binding to Xlib; it is a Lisp library implementing the X11 protocol.
 
<langsyntaxhighlight lang="lisp">;;; Single-file/interactive setup; large applications should define an ASDF system instead
(cl:require :asdf)
(asdf:operate 'asdf:load-op :clx)
(cl:defpackage #:rc-xlib-window
(:use #:cl #:xlib))
(cl:in-package #:rc-xlib-window)
 
(let* ((display (open-default-display)))
(screen (display-default-screen display))
(root-window (screen-root screen))
(black-pixel (screen-black-pixel screen))
(white-pixel (screen-white-pixel screen))
(window (create-window :parent root-window
:x 10 :y 10
:width 100 :height 100
:background white-pixel
:event-mask '(:exposure :key-press)))
(gc (create-gcontext :drawable window
:foreground black-pixel
:background white-pixel)))
(map-window window)
(unwind-protect
(event-case (display :discard-p t)
(let* ((window (create-window :parent (screen-root (display-default-screen display))
(exposure ()
:x 10
(draw-rectangle window gc 20 20 10 10 :y 10t)
(draw-glyphs window gc 10 50 "Hello, :width 100World!")
nil #| continue receiving events :height 100|#)
:event-mask '(:exposure :key-press)) ()
(gc (create t #| non-gcontextnil result signals event-case to :drawableexit window)|#))
(map-windowwhen window)
(destroy-window window))
(event-case (display :discard-p t)
(exposurewhen ()gc
(drawfree-rectangle windowgcontext gc 20 20 10 10 t))
(close-display display)))
(draw-glyphs window gc 10 40 "Hello, World!")
 
nil #| continue receiving events |#)
</syntaxhighlight>
(key-press ()
=={{header|Forth}}==
t #| non-nil result signals event-case to exit |#))))
Running Ubuntu 20.04 64-bit
(close-display display))</lang>
Using : Gforth 0.7.9_20211014
 
<syntaxhighlight lang="forth">
warnings off
require xlib.fs
 
0 value X11-D \ Display
0 value X11-S \ Screen
0 value X11-root
0 value X11-GC
0 value X11-W \ Window
0 value X11-Black
0 value X11-White
9 value X11-Top
0 value X11-Left
create X11-ev 96 allot
 
variable wm_delete
 
: X11-D-S X11-D X11-S ;
: X11-D-G X11-D X11-GC ;
: X11-D-W X11-D X11-W ;
: X11-D-W-G X11-D-W X11-GC ;
 
: open-X11 ( -- )
X11-D 0= if 0 XOpendisplay to X11-D
X11-D 0= abort" can't connect to X server"
X11-D XDefaultscreen to X11-S
X11-D-S XRootwindow to X11-root
X11-D-S XDefaultGC to X11-GC
X11-D-S XBlackPixel to X11-Black
X11-D-S XWhitePixel to X11-White
then
X11-W 0= if X11-D X11-root X11-top X11-left 400 220 0 0 $808080 XCreateSimplewindow to X11-W
X11-W 0= abort" failed to create X11-window"
X11-D-W $28043 XSelectInput drop
X11-D s" WM_DELETE_WEINDOW" 1 XInternAtom wm_delete !
X11-D-W wm_delete 1 XSetWMProtocols drop
X11-D-W XMapwindow drop
X11-D XFlush drop
then ;
 
: close-graphics ( -- )
X11-W if X11-D-W XDestroywindow drop 0 to X11-W
then
X11-D if X11-D XClosedisplay 0 to X11-D
then ;
 
: foreground >r X11-D-G r> XSetForeground drop ;
: background >r X11-D-G r> XSetBackground drop ;
: keysym X11-ev 0 XLookupKeysym ;
 
: ev-loop
begin X11-D X11-ev XNextEvent throw
X11-White foreground
X11-Black background
X11-D-W-G 100 100 s" Hello World" XDrawString drop
X11-D-W-G 100 120 150 25 XDrawRectangle drop
X11-D-W-G 110 135 s" Press ESC to exit ..." XDrawString drop
case X11-ev @ $ffffffff and
3 of keysym XK_Escape = if exit then endof
endcase
again ;
\ #### Test #####
0 open-X11
ev-loop
close-graphics
bye
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
=== Xlib ===
{{libheader|Xlib}}
<syntaxhighlight lang="vbnet">#include once "X11/Xlib.bi"
 
Dim As Display Ptr dpy
Dim As Window win
Dim As GC gc
Dim As XEvent ev
 
dpy = XOpenDisplay(NULL)
win = XCreateSimpleWindow(dpy, XDefaultRootWindow(dpy), 0, 0, 400, 300, 0, 0, 0)
gc = XCreateGC(dpy, win, 0, NULL)
XSelectInput(dpy, win, ExposureMask Or KeyPressMask)
XMapWindow(dpy, win)
 
While XNextEvent(dpy, @ev) = 0
If ev.type = Expose Then
XDrawString(dpy, win, gc, 200, 150, "Hello World", 11)
EndIf
Wend
 
XCloseDisplay(dpy)</syntaxhighlight>
 
=={{header|Go}}==
{{trans|C}}
<langsyntaxhighlight lang="go">package main
 
// Copyright (c) 2013 Alex Kesling
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
import (
"log"
"code.google.com/p/x-go-binding/xgb"
"fmtgithub.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
)
 
func main() {
// Open the connection to the X server
c, err := xgb.Dial("")
X, err := xgb.NewConn()
if err != nil {
fmtlog.PrintlnFatal(err)
return
}
defer c.Close()
 
// geometric objects
strBytes := []byte("Hello XGB!")
rectanglespoints := []xgbxproto.RectanglePoint{{40, 40, 20, 20}}
{10, 10},
{10, 20},
{20, 10},
{20, 20}};
 
polyline := []xproto.Point{
// get the first screen
s := c.DefaultScreen() {50, 10},
{ 5, 20}, // rest of points are relative
{25,-20},
{10, 10}};
 
segments := []xproto.Segment{
// root window
{100, 10, 140, 30},
win := s.Root
{110, 25, 130, 60}};
 
rectangles := []xproto.Rectangle{
// create black (foreground) graphic context
{ 10, 50, 40, 20},
fg := c.NewId()
{ 80, 50, 10, 40}};
mask := uint32(xgb.GCForeground | xgb.GCGraphicsExposures)
values := []uint32{s.BlackPixel, 0}
c.CreateGC(fg, win, mask, values)
 
arcs := []xproto.Arc{
// create white (background) graphic context
{10, 100, 60, 40, 0, 90 << 6},
bg := c.NewId()
{90, 100, 55, 40, 0, 270 << 6}};
mask = uint32(xgb.GCBackground | xgb.GCGraphicsExposures)
values[0] = s.WhitePixel // (values[1] still 0)
c.CreateGC(bg, win, mask, values)
 
setup := xproto.Setup(X)
// create the window
win// =Get c.NewId()the first screen
screen := setup.DefaultScreen(X)
mask = xgb.CWBackPixel | xgb.CWEventMask
 
// values[0] still s.WhitePixel
// Create black (foreground) graphic context
values[1] = xgb.EventMaskExposure | xgb.EventMaskKeyPress
foreground, _ := xproto.NewGcontextId(X)
c.CreateWindow(0, win, s.Root, 0, 0, 150, 150, 10,
mask := uint32(xproto.GcForeground | xproto.GcGraphicsExposures)
xgb.WindowClassInputOutput, s.RootVisual, mask, values)
values := []uint32{screen.BlackPixel, 0}
c.MapWindow(win)
xproto.CreateGC(X, foreground, xproto.Drawable(screen.Root), mask, values)
 
// Ask for our window's Id
win, _ := xproto.NewWindowId(X)
winDrawable := xproto.Drawable(win)
 
// Create the window
mask = uint32(xproto.CwBackPixel | xproto.CwEventMask)
values = []uint32{screen.WhitePixel, xproto.EventMaskExposure}
xproto.CreateWindow(X, // Connection
screen.RootDepth, // Depth
win, // Window Id
screen.Root, // Parent Window
0, 0, // x, y
150, 150, // width, height
10, // border_width
xproto.WindowClassInputOutput, // class
screen.RootVisual, // visual
mask, values) // masks
 
// Map the window on the screen
xproto.MapWindow(X, win)
 
for {
eventevt, err := cX.WaitForEvent()
ifswitch err != nilevt.(type) {
fmtcase xproto.Println(err)ExposeEvent:
return /* We draw the points */
xproto.PolyPoint(X, xproto.CoordModeOrigin, winDrawable, foreground, points)
 
/* We draw the polygonal line */
xproto.PolyLine(X, xproto.CoordModePrevious, winDrawable, foreground, polyline)
 
/* We draw the segments */
xproto.PolySegment(X, winDrawable, foreground, segments)
 
/* We draw the rectangles */
xproto.PolyRectangle(X, winDrawable, foreground, rectangles)
 
/* We draw the arcs */
xproto.PolyArc(X, winDrawable, foreground, arcs)
 
default:
/* Unknown event type, ignore it */
}
 
switch event.(type) {
caseif xgb.ExposeEvent:err != nil {
clog.PolyRectangleFatal(win, fg, rectangleserr)
c.ImageText8(win, bg, 20, 20, strBytes)
case xgb.KeyPressEvent:
return
}
}
return
}</lang>
}</syntaxhighlight>
Screen capture:
 
Line 444 ⟶ 1,300:
groovy WindowCreation.groovy
 
<langsyntaxhighlight lang="groovy">import javax.swing.*
import java.awt.*
import java.awt.event.WindowAdapter
Line 474 ⟶ 1,330:
}
}
}</langsyntaxhighlight>
 
=={{header|GUISS}}==
Graphical User Interface Support Script is really a language for operating a computer, rather than programming one, so we cannot do this via X11 libraries. The example uses leafpad for our open window, and the box symbols to enclose our text:
 
<langsyntaxhighlight lang="guiss">Start,Programs,Applications,Editors,Leafpad,Textbox,
Type:[openbox]Hello World[pling][closebox]</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Icon and Unicon provide a portable graphics implementation that does not rely upon a toolkit. The intent is to be platform independent and the same code runs on multiple platforms without change and producing results with only minor variations. Icon and Unicon graphics are implemented in X-Windows as well as MS-Windows and others. There are additional 3D graphics capabilities implemented using opengl.
<lang Icon>procedure main()
W1 := open("X-Window","g","size=250,250","bg=black","fg=red") | stop("unable to open window")
FillRectangle(W1,50,50,150,150)
WDone(W1)
end
 
link graphics</lang>
 
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/graphics.icn graphics.icn provides graphics]
 
Additionally, the '''WOpen''' procedure and Window.App methods are available.
 
=={{header|Haskell}}==
Using {{libheader|X11}} from [http://hackage.haskell.org/packages/hackage.html HackageDB]
<langsyntaxhighlight lang="haskell">import Graphics.X11.Xlib
import Control.Concurrent (threadDelay)
 
Line 521 ⟶ 1,362:
destroyWindow display xwin
closeDisplay display
</syntaxhighlight>
</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
Icon and Unicon provide a portable graphics implementation that does not rely upon a toolkit. The intent is to be platform independent and the same code runs on multiple platforms without change and producing results with only minor variations. Icon and Unicon graphics are implemented in X-Windows as well as MS-Windows and others. There are additional 3D graphics capabilities implemented using opengl.
<syntaxhighlight lang="icon">procedure main()
W1 := open("X-Window","g","size=250,250","bg=black","fg=red") | stop("unable to open window")
FillRectangle(W1,50,50,150,150)
WDone(W1)
end
 
link graphics</syntaxhighlight>
 
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/graphics.icn graphics.icn provides graphics]
 
Additionally, the '''WOpen''' procedure and Window.App methods are available.
 
=={{header|Java}}==
Line 528 ⟶ 1,384:
run
java WindowExample
<langsyntaxhighlight lang="java">import javax.swing.JFrame;
import javax.swing.SwingUtilities;
 
Line 548 ⟶ 1,404:
frame.setVisible(true);
}
}</langsyntaxhighlight>The previous example works but doesn't write any text or draw any box; the following does both.<langsyntaxhighlight lang="java">import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
Line 574 ⟶ 1,430:
f.setVisible(true);
}
}</langsyntaxhighlight>
 
=={{header|MathematicaJulia}}==
This was based on https://en.wikipedia.org/wiki/Xlib, and mostly quoted from from the XLib.jl test2() testing code function.
Note that GUIKit is a high-level wrapper for Swing.
<syntaxhighlight lang="julia">
using Xlib
 
function x11demo()
<lang>
# Open connection to the server.
Needs["GUIKit`"]
dpy = XOpenDisplay(C_NULL)
dpy == C_NULL && error("unable to open display")
scr = DefaultScreen(dpy)
 
# Create a window.
win = XCreateSimpleWindow(dpy, RootWindow(dpy, scr), 10, 10, 300, 100, 1,
BlackPixel(dpy, scr), WhitePixel(dpy, scr))
 
# Select the kind of events we are interested in.
XSelectInput(dpy, win, ExposureMask | KeyPressMask)
 
# Show or in x11 terms map window.
XMapWindow(dpy, win)
 
# Run event loop.
evt = Ref(XEvent())
while true
XNextEvent(dpy, evt)
 
# Draw or redraw the window.
if EventType(evt) == Expose
XFillRectangle(dpy, win, DefaultGC(dpy, scr), 24, 24, 16, 16)
XDrawString(dpy, win, DefaultGC(dpy, scr), 50, 50, "Hello, World! Press any key to exit.")
end
 
# Exit whenever a key is pressed.
if EventType(evt) == KeyPress
break
end
end
 
# Shutdown server connection
XCloseDisplay(dpy)
end
 
x11demo()
</syntaxhighlight>
 
=={{header|Kotlin}}==
{{trans|C}}
{{libheader|Xlib}}
<syntaxhighlight lang="scala">// Kotlin Native v0.3
 
import kotlinx.cinterop.*
import Xlib.*
 
fun main(args: Array<String>) {
val msg = "Hello, World!"
val d = XOpenDisplay(null)
if (d == null) {
println("Cannot open display")
return
}
 
val s = XDefaultScreen(d)
val w = XCreateSimpleWindow(d, XRootWindow(d, s), 10, 10, 160, 160, 1,
XBlackPixel(d, s), XWhitePixel(d, s))
XSelectInput(d, w, ExposureMask or KeyPressMask)
XMapWindow(d, w)
val e = nativeHeap.alloc<XEvent>()
while (true) {
XNextEvent(d, e.ptr)
if (e.type == Expose) {
XFillRectangle(d, w, XDefaultGC(d, s), 55, 40, 50, 50)
XDrawString(d, w, XDefaultGC(d, s), 45, 120, msg, msg.length)
}
else if (e.type == KeyPress) break
}
 
XCloseDisplay(d)
nativeHeap.free(e)
}</syntaxhighlight>
 
=={{header|M2000 Interpreter}}==
M2000 interpteter is a Visual Basic 6 (vb6) gui application, so we can't use X11. When we use Wine, connecting to X11 may occur but this is invisible for interpreter scope.
 
Ao for this task we use the M2000 way to make a form and do something on it.
 
<syntaxhighlight lang="m2000 interpreter">
\\ M2000 froms (windows) based on a flat, empty with no title bar, vb6 window and a user control.
\\ title bar is a user control in M2000 form
\\ On linux, wine application run M2000 interpreter traslating vb6 calls to window system.
Module SquareAndText2Window {
Const black=0, white=15
Declare form1 form
\\ defaultwindow title is the name of variable,here is: form1
Rem With form1, "Title", "A title for this window"
Method form1,"move", 2000,3000,10000,8000 ' in twips
layer form1 {
Cls white
Font "Verdana"
Rem Window 12 , 10000,8000; ' hide modr 13 using a REM before
Rem
Mode 12 ' 12 in pt
Cls white, 2 ' fill white, set third raw for top of scrolling frame
Pen black
Move 1000,2000 ' absolute coordinated in twips - use Step for relative coordinates
\\ polygon use relative coordinates in twips
polygon white, 1000,0,0,-1000,-1000,0,0,-1000
\\ write text using graphic coordinates
Move 1000, 3000 : Legend "Hello World", "Arial", 12
\\ write text using layer as console (font Verdana)
Print @(15,5),"Goodbye Wolrd"
\\ 9120 7920 if we use window 12,10000, 8000 (cut exactly for console use)
\\ 10000 8000 if we use Mode 12
\\ scale.y include window height (including header)
\\ the layer extend bellow
Print scale.x, scale.y
}
\\ show form1 modal (continue to next line when we close the window)
Method form1,"Show", 1
\\ closing meand hide in M2000
wait 1000 ' in msec
Method form1,"move", random(2000, 10000),random(3000, 8000)
\\ now show again
Method form1,"Show", 1
\\ now we close the form, releasing resources
Declare form1 Nothing
}
SquareAndText2Window
</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Note that GUIKit is a high-level wrapper for Swing.
<syntaxhighlight lang="text">Needs["GUIKit`"]
ref = GUIRun[Widget["Panel", {
Widget[
Line 587 ⟶ 1,571:
"GIF"]]}],
Widget["Label", { "text" -> "Hello World!"}]}
]]</syntaxhighlight>
 
</lang>
=={{header|Nim}}==
<syntaxhighlight lang="nim">import x11/[xlib,xutil,x]
 
const
windowWidth = 1000
windowHeight = 600
borderWidth = 5
eventMask = ButtonPressMask or KeyPressMask or ExposureMask
 
var
display: PDisplay
window: Window
deleteMessage: Atom
graphicsContext: GC
 
proc init() =
display = XOpenDisplay(nil)
if display == nil:
quit "Failed to open display"
 
let
screen = XDefaultScreen(display)
rootWindow = XRootWindow(display, screen)
foregroundColor = XBlackPixel(display, screen)
backgroundColor = XWhitePixel(display, screen)
 
window = XCreateSimpleWindow(display, rootWindow, -1, -1, windowWidth,
windowHeight, borderWidth, foregroundColor, backgroundColor)
 
discard XSetStandardProperties(display, window, "X11 Example", "window", 0,
nil, 0, nil)
 
discard XSelectInput(display, window, eventMask)
discard XMapWindow(display, window)
 
deleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", false.XBool)
discard XSetWMProtocols(display, window, deleteMessage.addr, 1)
 
graphicsContext = XDefaultGC(display, screen)
 
 
proc drawWindow() =
const text = "Hello, Nim programmers."
discard XDrawString(display, window, graphicsContext, 10, 50, text, text.len)
discard XFillRectangle(display, window, graphicsContext, 20, 20, 10, 10)
 
 
proc mainLoop() =
## Process events until the quit event is received
var event: XEvent
while true:
discard XNextEvent(display, event.addr)
case event.theType
of Expose:
drawWindow()
of ClientMessage:
if cast[Atom](event.xclient.data.l[0]) == deleteMessage:
break
of KeyPress:
let key = XLookupKeysym(cast[PXKeyEvent](event.addr), 0)
if key != 0:
echo "Key ", key, " pressed"
of ButtonPressMask:
echo "Mouse button ", event.xbutton.button, " pressed at ",
event.xbutton.x, ",", event.xbutton.y
else:
discard
 
 
proc main() =
init()
mainLoop()
discard XDestroyWindow(display, window)
discard XCloseDisplay(display)
 
 
main()</syntaxhighlight>
 
=={{header|OCaml}}==
Line 603 ⟶ 1,664:
#load "Xlib.cma"
 
<langsyntaxhighlight lang="ocaml">open Xlib
 
let () =
Line 626 ⟶ 1,687:
main_loop();
xCloseDisplay d;
;;</langsyntaxhighlight>
 
 
{{omit from|TI-83 BASIC}} {{omit from|TI-89 BASIC}} <!-- Does not have a local X server or network access. -->
{{omit from|PureBasic}}
 
=={{header|Pascal}}==
Line 637 ⟶ 1,694:
from wiki.freepascal.org/X11#Examples.
Compiled with Freepascal 2.6.4-32
<langsyntaxhighlight lang="pascal">program xshowwindow;
{$mode objfpc}{$H+}
 
Line 694 ⟶ 1,751:
ModalShowX11Window('Hello, World!');
end.
</syntaxhighlight>
</lang>
 
=={{header|Perl}}==
=== X11::Protocol ===
<langsyntaxhighlight lang="perl">#!/usr/bin/perl -w
use strict;
use X11::Protocol;
Line 742 ⟶ 1,799:
for (;;) {
$X->handle_input;
}</langsyntaxhighlight>
 
=={{header|Perl 6Phix}}==
{{trans|CTXR}}
See [[Window_creation#Phix]]
 
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;
 
class Display is repr('CStruct') {}
class GC is repr('CStruct') {}
class XEvent is repr('CStruct') {
has int32 $.type; # for 32 bits machine
#has int $.type; # for 64 bits machine
method init { $!type = 0 }
}
 
sub XOpenDisplay(Str $name = ':0') returns Display is native('libX11') { * }
sub XDefaultScreen(Display $) returns int is native('libX11') { * }
sub XRootWindow(Display $, int $screen_number) returns int is native('libX11') { * }
sub XBlackPixel(Display $, int $screen_number) returns int is native('libX11') { * }
sub XWhitePixel(Display $, int $screen_number) returns int is native('libX11') { * }
sub XCreateSimpleWindow(
Display $, int $parent_window, int $x, int $y,
int $width, int $height, int $border_width,
int $border, int $background
) returns int is native('libX11') { * }
sub XMapWindow(Display $, int $window) is native('libX11') { * }
sub XSelectInput(Display $, int $window, int $mask) is native('libX11') { * }
sub XFillRectangle(
Display $, int $window, GC $, int $x, int $y, int $width, int $height
) is native('libX11') { * }
sub XDrawString(
Display $, int $window, GC $, int $x, int $y, Str $, int $str_length
) is native('libX11') { * }
sub XDefaultGC(Display $, int $screen) returns GC is native('libX11') { * }
sub XNextEvent(Display $, XEvent $e) is native('libX11') { * }
sub XCloseDisplay(Display $) is native('libX11') { * }
 
my Display $display = XOpenDisplay()
or die "Can not open display";
 
my int $screen = XDefaultScreen($display);
my int $window = XCreateSimpleWindow(
$display,
XRootWindow($display, $screen),
10, 10, 100, 100, 1,
XBlackPixel($display, $screen), XWhitePixel($display, $screen)
);
XSelectInput($display, $window, 1 +< 15 +| 1);
XMapWindow($display, $window);
 
my Str $msg = 'Hello, World!';
my XEvent $e .= new; $e.init;
loop {
XNextEvent($display, $e);
if $e.type == 12 {
XFillRectangle($display, $window, XDefaultGC($display, $screen), 20, 20, 10, 10);
XDrawString($display, $window, XDefaultGC($display, $screen), 10, 50, $msg, my int $ = $msg.chars);
}
elsif $e.type == 2 {
last;
}
}
XCloseDisplay($display);</lang>
 
=={{header|PicoLisp}}==
The following script works in the 32-bit version, using inlined C code
<langsyntaxhighlight PicoLisplang="picolisp">#!/usr/bin/picolisp /usr/lib/picolisp/lib.l
 
(load "@lib/misc.l" "@lib/gcc.l")
Line 858 ⟶ 1,854:
 
(simpleWin 300 200 "Hello World")
(bye)</langsyntaxhighlight>
 
=={{header|PythonPureBasic}}==
PureBasic does not have direct support for the X11 library like Xlib or XCB.
 
See [[Window_creation#PureBasic]].
 
=={{header|Python}}==
=== Xlib ===
{{field attention|Python|X11|Note (stolen from CLX example): This example was written in near-total ignorance of X11 by consulting the python-xlib's examples (included in its distribution) to find equivalents for the parts of the C example.}}
Line 874 ⟶ 1,874:
* python xlib_hello_world.py
 
<langsyntaxhighlight lang="python">from Xlib import X, display
 
class Window:
Line 907 ⟶ 1,907:
if __name__ == "__main__":
Window(display.Display(), "Hello, World!").loop()</langsyntaxhighlight>
 
=== XCB ===
{{libheader|python-xcb}}
 
<langsyntaxhighlight lang="python">import xcb
from xcb.xproto import *
import xcb.render
Line 954 ⟶ 1,954:
conn.disconnect()
 
main()</langsyntaxhighlight>
 
=={{header|Racket}}==
Using Racket's GUI which is implemented using gtk. It's not low level, but OTOH it works on Windows and OS X too.
 
<langsyntaxhighlight Racketlang="racket">#lang racket/gui
 
(define frame (new frame%
Line 971 ⟶ 1,971:
(send dc set-text-foreground "blue")
(send dc draw-text "Don't Panic!" 0 0))])
(send frame show #t)</langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
{{trans|C}}
 
There is not yet a X11 library in Raku but we can write the minimal C mappings for this task.
 
<syntaxhighlight lang="raku" line>use NativeCall;
 
class Display is repr('CStruct') {
has int32 $!screen;
has int32 $!window;
}
class GC is repr('CStruct') {
has int32 $!context;
}
class XEvent is repr('CStruct') {
has int32 $.type;
method init { $!type = 0 }
}
 
sub XOpenDisplay(Str $name = ':0') returns Display is native('X11') { * }
sub XDefaultScreen(Display $) returns int32 is native('X11') { * }
sub XRootWindow(Display $, int32 $screen_number) returns int32 is native('X11') { * }
sub XBlackPixel(Display $, int32 $screen_number) returns int32 is native('X11') { * }
sub XWhitePixel(Display $, int32 $screen_number) returns int32 is native('X11') { * }
sub XCreateSimpleWindow(
Display $, int32 $parent_window, int32 $x, int32 $y,
int32 $width, int32 $height, int32 $border_width,
int32 $border, int32 $background
) returns int32 is native('X11') { * }
sub XMapWindow(Display $, int32 $window) is native('X11') { * }
sub XSelectInput(Display $, int32 $window, int32 $mask) is native('X11') { * }
sub XFillRectangle(
Display $, int32 $window, GC $, int32 $x, int32 $y, int32 $width, int32 $height
) is native('X11') { * }
sub XDrawString(
Display $, int32 $window, GC $, int32 $x, int32 $y, Str $, int32 $str_length
) is native('X11') { * }
sub XDefaultGC(Display $, int32 $screen) returns GC is native('X11') { * }
sub XNextEvent(Display $, XEvent $e) is native('X11') { * }
sub XCloseDisplay(Display $) is native('X11') { * }
 
my Display $display = XOpenDisplay()
or die "Can not open display";
 
my int $screen = XDefaultScreen($display);
my int $window = XCreateSimpleWindow(
$display,
XRootWindow($display, $screen),
10, 10, 100, 100, 1,
XBlackPixel($display, $screen), XWhitePixel($display, $screen)
);
XSelectInput($display, $window, 1 +< 15 +| 1);
XMapWindow($display, $window);
 
my Str $msg = 'Hello, World!';
my XEvent $e .= new; $e.init;
loop {
XNextEvent($display, $e);
if $e.type == 12 {
XFillRectangle($display, $window, XDefaultGC($display, $screen), 20, 20, 10, 10);
XDrawString($display, $window, XDefaultGC($display, $screen), 10, 50, $msg, my int $ = $msg.chars);
}
elsif $e.type == 2 {
last;
}
}
XCloseDisplay($display);</syntaxhighlight>
 
=={{header|Scala}}==
{{libheader|Scala}}<langsyntaxhighlight Scalalang="scala">import scala.swing.{ MainFrame, SimpleSwingApplication }
import scala.swing.Swing.pair2Dimension
 
Line 983 ⟶ 2,052:
preferredSize = ((200, 150))
}
}</langsyntaxhighlight>
 
=={{header|Standard ML}}==
Works with PolyML
<syntaxhighlight lang="standard ml">open XWindows ;
val dp = XOpenDisplay "" ;
val w = XCreateSimpleWindow (RootWindow dp) origin (Area {x=0,y=0,w=400,h=300}) 3 0 0xffffff ;
XMapWindow w;
XFlush dp ;
XDrawString w (DefaultGC dp) (XPoint {x=10,y=50}) "Hello World!" ;
XFlush dp ;</syntaxhighlight>
 
=={{header|Tcl}}==
Line 990 ⟶ 2,069:
===Low level interface===
{{libheader|critcl}}
<langsyntaxhighlight lang="tcl">package provide xlib 1
package require critcl
 
Line 1,054 ⟶ 2,133:
const char *str = Tcl_GetStringFromObj(msg, &len);
XDrawString(d, (Window)w, gc, x, y, str, len);
}</langsyntaxhighlight>
Note that this only does enough for this demo. A full adaptation is too long for RosettaCode...
 
This could then be used like this:
<langsyntaxhighlight lang="tcl">package require xlib
 
XOpenDisplay {}
Line 1,068 ⟶ 2,147:
}
XDestroyWindow $w
XCloseDisplay</langsyntaxhighlight>
===Higher level interface===
Just because there is a low level package does not mean that it is pleasant to use from Tcl code. Therefore this second package wraps it up inside a higher-level package that provides a more natural way of interacting.
<br>
{{works with|Tcl|8.6}} or {{libheader|TclOO}}
<langsyntaxhighlight lang="tcl">package require TclOO
package provide x11 1
 
Line 1,125 ⟶ 2,204:
}
}
}</langsyntaxhighlight>
This script puts the pieces together to carry out the details of the task.
<langsyntaxhighlight lang="tcl">package require x11
 
# With a display connection open, create and map a window
Line 1,147 ⟶ 2,226:
 
$w destroy
}</langsyntaxhighlight>
Improving this by adding more sophisticated event handling and more window methods is left as an exercise.
 
Line 1,153 ⟶ 2,232:
 
See [[Window_creation#TXR]].
 
=={{header|Wren}}==
{{trans|C}}
{{libheader|Xlib}}
<br>
As it's not currently possible for Wren-cli to access Xlib directly, we embed a Wren script in a C application to complete this task.
<syntaxhighlight lang="wren">/* Window_creation_X11.wren */
 
var KeyPressMask = 1 << 0
var ExposureMask = 1 << 15
var KeyPress = 2
var Expose = 12
 
foreign class XGC {
construct default(display, screenNumber) {}
}
 
foreign class XEvent {
construct new() {}
 
foreign eventType
}
 
foreign class XDisplay {
construct openDisplay(displayName) {}
 
foreign defaultScreen()
 
foreign rootWindow(screenNumber)
 
foreign blackPixel(screenNumber)
 
foreign whitePixel(screenNumber)
 
foreign selectInput(w, eventMask)
 
foreign mapWindow(w)
 
foreign closeDisplay()
 
foreign nextEvent(eventReturn)
 
foreign createSimpleWindow(parent, x, y, width, height, borderWidth, border, background)
 
foreign fillRectangle(d, gc, x, y, width, height)
 
foreign drawString(d, gc, x, y, string, length)
}
 
var xd = XDisplay.openDisplay("")
if (xd == 0) {
System.print("Cannot open display.")
return
}
var s = xd.defaultScreen()
var w = xd.createSimpleWindow(xd.rootWindow(s), 10, 10, 100, 100, 1, xd.blackPixel(s), xd.whitePixel(s))
xd.selectInput(w, ExposureMask | KeyPressMask)
xd.mapWindow(w)
var msg = "Hello, World!"
var e = XEvent.new()
while (true) {
xd.nextEvent(e)
var gc = XGC.default(xd, s)
if (e.eventType == Expose) {
xd.fillRectangle(w, gc, 20, 20, 10, 10)
xd.drawString(w, gc, 10, 50, msg, msg.count)
}
if (e.eventType == KeyPress) break
}
xd.closeDisplay()</syntaxhighlight>
<br>
We now embed this Wren script in the following C program, compile and run it.
<syntaxhighlight lang="c">/* gcc Window_creation_X11.c -o Window_creation_X11 -lX11 -lwren -lm */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include "wren.h"
 
/* C <=> Wren interface functions */
 
void C_displayAllocate(WrenVM* vm) {
Display** pdisplay = (Display**)wrenSetSlotNewForeign(vm, 0, 0, sizeof(Display*));
const char *displayName = wrenGetSlotString(vm, 1);
if (displayName == "") {
*pdisplay = XOpenDisplay(NULL);
} else {
*pdisplay = XOpenDisplay(displayName);
}
}
 
void C_gcAllocate(WrenVM* vm) {
GC *pgc = (GC *)wrenSetSlotNewForeign(vm, 0, 0, sizeof(GC));
Display* display = *(Display**)wrenGetSlotForeign(vm, 1);
int s = (int)wrenGetSlotDouble(vm, 2);
*pgc = DefaultGC(display, s);
}
 
void C_eventAllocate(WrenVM* vm) {
wrenSetSlotNewForeign(vm, 0, 0, sizeof(XEvent));
}
 
void C_eventType(WrenVM* vm) {
XEvent e = *(XEvent *)wrenGetSlotForeign(vm, 0);
wrenSetSlotDouble(vm, 0, (double)e.type);
}
 
void C_defaultScreen(WrenVM* vm) {
Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
int screenNumber = DefaultScreen(display);
wrenSetSlotDouble(vm, 0, (double)screenNumber);
}
 
void C_rootWindow(WrenVM* vm) {
Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
int screenNumber = (int)wrenGetSlotDouble(vm, 1);
Window w = RootWindow(display, screenNumber);
wrenSetSlotDouble(vm, 0, (double)w);
}
 
void C_blackPixel(WrenVM* vm) {
Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
int screenNumber = (int)wrenGetSlotDouble(vm, 1);
unsigned long p = BlackPixel(display, screenNumber);
wrenSetSlotDouble(vm, 0, (double)p);
}
 
void C_whitePixel(WrenVM* vm) {
Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
int screenNumber = (int)wrenGetSlotDouble(vm, 1);
unsigned long p = WhitePixel(display, screenNumber);
wrenSetSlotDouble(vm, 0, (double)p);
}
 
void C_selectInput(WrenVM* vm) {
Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
Window w = (Window)wrenGetSlotDouble(vm, 1);
long eventMask = (long)wrenGetSlotDouble(vm, 2);
XSelectInput(display, w, eventMask);
}
 
void C_mapWindow(WrenVM* vm) {
Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
Window w = (Window)wrenGetSlotDouble(vm, 1);
XMapWindow(display, w);
}
 
void C_closeDisplay(WrenVM* vm) {
Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
XCloseDisplay(display);
}
 
void C_nextEvent(WrenVM* vm) {
Display* display = *(Display**)wrenGetSlotForeign(vm, 0);
XEvent* pe = (XEvent*)wrenGetSlotForeign(vm, 1);
XNextEvent(display, pe);
}
 
void C_createSimpleWindow(WrenVM* vm) {
Display *display = *(Display**)wrenGetSlotForeign(vm, 0);
Window parent = (Window)wrenGetSlotDouble(vm, 1);
int x = (int)wrenGetSlotDouble(vm, 2);
int y = (int)wrenGetSlotDouble(vm, 3);
unsigned int width = (unsigned int)wrenGetSlotDouble(vm, 4);
unsigned int height = (unsigned int)wrenGetSlotDouble(vm, 5);
unsigned int borderWidth = (unsigned int)wrenGetSlotDouble(vm, 6);
unsigned long border = (unsigned long)wrenGetSlotDouble(vm, 7);
unsigned long background = (unsigned long)wrenGetSlotDouble(vm, 8);
Window w = XCreateSimpleWindow(display, parent, x, y, width, height, borderWidth, border, background);
wrenSetSlotDouble(vm, 0, (double)w);
}
 
void C_fillRectangle(WrenVM* vm) {
Display *display = *(Display**)wrenGetSlotForeign(vm, 0);
Drawable d = (Drawable)wrenGetSlotDouble(vm, 1);
GC gc = *(GC *)wrenGetSlotForeign(vm, 2);
int x = (int)wrenGetSlotDouble(vm, 3);
int y = (int)wrenGetSlotDouble(vm, 4);
unsigned int width = (unsigned int)wrenGetSlotDouble(vm, 5);
unsigned int height = (unsigned int)wrenGetSlotDouble(vm, 6);
XFillRectangle(display, d, gc, x, y, width, height);
}
 
void C_drawString(WrenVM* vm) {
Display *display = *(Display**)wrenGetSlotForeign(vm, 0);
Drawable d = (Drawable)wrenGetSlotDouble(vm, 1);
GC gc = *(GC *)wrenGetSlotForeign(vm, 2);
int x = (int)wrenGetSlotDouble(vm, 3);
int y = (int)wrenGetSlotDouble(vm, 4);
const char *string = wrenGetSlotString(vm, 5);
int length = (int)wrenGetSlotDouble(vm, 6);
XDrawString(display, d, gc, x, y, string, length);
}
 
WrenForeignClassMethods bindForeignClass(WrenVM* vm, const char* module, const char* className) {
WrenForeignClassMethods methods;
methods.finalize = NULL;
if (strcmp(className, "XDisplay") == 0) {
methods.allocate = C_displayAllocate;
} else if (strcmp(className, "XGC") == 0) {
methods.allocate = C_gcAllocate;
} else if (strcmp(className, "XEvent") == 0) {
methods.allocate = C_eventAllocate;
} else {
methods.allocate = NULL;
}
return methods;
}
 
WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "main") == 0) {
if (strcmp(className, "XEvent") == 0) {
if (!isStatic && strcmp(signature, "eventType") == 0) return C_eventType;
} else if (strcmp(className, "XDisplay") == 0) {
if (!isStatic && strcmp(signature, "defaultScreen()") == 0) return C_defaultScreen;
if (!isStatic && strcmp(signature, "rootWindow(_)") == 0) return C_rootWindow;
if (!isStatic && strcmp(signature, "blackPixel(_)") == 0) return C_blackPixel;
if (!isStatic && strcmp(signature, "whitePixel(_)") == 0) return C_whitePixel;
if (!isStatic && strcmp(signature, "selectInput(_,_)") == 0) return C_selectInput;
if (!isStatic && strcmp(signature, "mapWindow(_)") == 0) return C_mapWindow;
if (!isStatic && strcmp(signature, "closeDisplay()") == 0) return C_closeDisplay;
if (!isStatic && strcmp(signature, "nextEvent(_)") == 0) return C_nextEvent;
if (!isStatic && strcmp(signature, "createSimpleWindow(_,_,_,_,_,_,_,_)") == 0) return C_createSimpleWindow;
if (!isStatic && strcmp(signature, "fillRectangle(_,_,_,_,_,_)") == 0) return C_fillRectangle;
if (!isStatic && strcmp(signature, "drawString(_,_,_,_,_,_)") == 0) return C_drawString;
}
}
return NULL;
}
 
static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
 
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
switch (errorType) {
case WREN_ERROR_COMPILE:
printf("[%s line %d] [Error] %s\n", module, line, msg);
break;
case WREN_ERROR_STACK_TRACE:
printf("[%s line %d] in %s\n", module, line, msg);
break;
case WREN_ERROR_RUNTIME:
printf("[Runtime Error] %s\n", msg);
break;
}
}
 
char *readFile(const char *fileName) {
FILE *f = fopen(fileName, "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
rewind(f);
char *script = malloc(fsize + 1);
fread(script, 1, fsize, f);
fclose(f);
script[fsize] = 0;
return script;
}
 
int main(int argc, char **argv) {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.errorFn = &errorFn;
config.bindForeignClassFn = &bindForeignClass;
config.bindForeignMethodFn = &bindForeignMethod;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "Window_creation_X11.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
switch (result) {
case WREN_RESULT_COMPILE_ERROR:
printf("Compile Error!\n");
break;
case WREN_RESULT_RUNTIME_ERROR:
printf("Runtime Error!\n");
break;
case WREN_RESULT_SUCCESS:
break;
}
wrenFreeVM(vm);
free(script);
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>Same as C example.</pre>
 
=={{header|Yabasic}}==
<syntaxhighlight lang="vbnet">open window 300,200
 
text 150, 100, "Hello World"
 
rectangle 10,10 to 90,90
fill rectangle 40,40,60,60
 
clear screen
 
inkey$
close window</syntaxhighlight>
 
{{omit from|ACL2}}
{{omit from|AutoHotkey}}
{{omit from|Batch File|Does not have the necessary capabilities of interfacing with an X server.}}
{{omit from|AWK|Does not provide native X11 support}}
{{omit from|Batch File|Does not have the necessary capabilities of interfacing with an X server.}}
{{omit from|Erlang|Does not provide native X11 support}}
{{omit from|Locomotive Basic}}
Line 1,166 ⟶ 2,554:
{{omit from|PostScript}}
{{omit from|PowerBASIC|DOS & Windows only}}
{{omit from|PureBasic}}
{{omit from|SNOBOL4}}
{{omit from|SQL PL|It does not handle GUI}}
{{omit from|TI-83 BASIC|No X11 server access.}}
{{omit from|TI-89 BASIC|No X11 server access.}} <!-- Does not have a local X server or network access. -->
{{omit from|TPP}}
{{omit from|Unlambda|No X11 server access.}}
2,122

edits