Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// gcc x11cair.c `pkg-config --cflags --libs x11 cairo` -o x11cair | ||
|
||
|
||
#include<cairo.h> | ||
#include<cairo-xlib.h> | ||
#include<X11/Xlib.h> | ||
|
||
#include<stdio.h> | ||
#include<stdlib.h> | ||
|
||
#define SIZEX 320 | ||
#define SIZEY 240 | ||
|
||
void paint(cairo_surface_t *cs) | ||
{ | ||
cairo_t *c; | ||
|
||
c=cairo_create(cs); | ||
|
||
cairo_rectangle(c, 0.0, 0.0, SIZEX, SIZEY); | ||
cairo_set_source_rgb(c, 0.0, 0.0, 0.5); | ||
cairo_fill(c); | ||
cairo_move_to(c, 40.0, 50.0); | ||
cairo_set_source_rgb(c, 1.0, 1.0, 0.0); | ||
cairo_show_text(c, "Hello World!"); | ||
cairo_show_page(c); | ||
|
||
cairo_destroy(c); | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
Display *dpy; | ||
Window rootwin; | ||
Window win; | ||
Colormap cmap; | ||
XEvent e; | ||
int scr; | ||
GC gc; | ||
|
||
if(!(dpy=XOpenDisplay(NULL))) { | ||
fprintf(stderr, "ERROR: could not open display\n"); | ||
exit(1); | ||
} | ||
|
||
scr = DefaultScreen(dpy); | ||
rootwin = RootWindow(dpy, scr); | ||
cmap = DefaultColormap(dpy, scr); | ||
|
||
win=XCreateSimpleWindow(dpy, rootwin, 1, 1, SIZEX, SIZEY, 0, | ||
BlackPixel(dpy, scr), BlackPixel(dpy, scr)); | ||
|
||
XStoreName(dpy, win, "hello"); | ||
|
||
gc=XCreateGC(dpy, win, 0, NULL); | ||
XSetForeground(dpy, gc, WhitePixel(dpy, scr)); | ||
|
||
XSelectInput(dpy, win, ExposureMask|ButtonPressMask); | ||
|
||
XMapWindow(dpy, win); | ||
|
||
Atom wmDeleteMessage = XInternAtom(dpy, "WM_DELETE_WINDOW", False); | ||
XSetWMProtocols(dpy, win, &wmDeleteMessage, 1); | ||
|
||
cairo_surface_t *cs; | ||
cs=cairo_xlib_surface_create(dpy, win, DefaultVisual(dpy, 0), SIZEX, SIZEY); | ||
|
||
while(1) { | ||
XNextEvent(dpy, &e); | ||
if(e.type==Expose && e.xexpose.count<1) { | ||
XDrawString(dpy, win, gc, 10, 10, "Hello World!", 12); | ||
paint(cs); | ||
} | ||
else if(e.type==ButtonPress) break; | ||
else if(e.type==ClientMessage && e.xclient.data.l[0] == wmDeleteMessage) { | ||
break; | ||
} | ||
} | ||
|
||
printf("destroying\n"); | ||
|
||
cairo_surface_destroy(cs); | ||
//XDestroyWindow(dpy, win); | ||
XCloseDisplay(dpy); | ||
printf("closed display\n"); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import cairo, cairoxlib, x, xlib | ||
|
||
const | ||
SIZEX = 320 | ||
SIZEY = 240 | ||
|
||
|
||
proc paint(cs: PSurface) = | ||
var c: PContext = create(cs) | ||
|
||
c.rectangle(0.0, 0.0, SIZEX, SIZEY) | ||
c.set_source_rgb(0, 0, 0.5) | ||
c.fill | ||
c.move_to(40, 50) | ||
c.set_source_rgb(1, 1, 0) | ||
c.show_text("Hello World!") | ||
c.show_page | ||
|
||
c.destroy | ||
|
||
proc main() = | ||
var | ||
dpy: PDisplay | ||
rootwin: TWindow | ||
win: TWindow | ||
cmap: TColormap | ||
e: TXEvent | ||
scr: cint | ||
gc: TGC | ||
|
||
dpy = XOpenDisplay(nil) | ||
if dpy == nil: | ||
echo "Failed to open display!" | ||
quit(1) | ||
|
||
scr = XDefaultScreen(dpy) | ||
rootwin = XRootWindow(dpy, scr) | ||
cmap = XDefaultColorMap(dpy, scr) | ||
|
||
win = XCreateSimpleWindow(dpy, rootwin, 1, 1, SIZEX, SIZEY, 0, | ||
XBlackPixel(dpy, scr), XBlackPixel(dpy, scr)) | ||
|
||
discard XStoreName(dpy, win, "hello") | ||
|
||
gc = XCreateGC(dpy, win, 0, nil) | ||
discard XSetForeground(dpy, gc, XWhitePixel(dpy, scr)) | ||
|
||
discard XSelectInput(dpy, win, ExposureMask or ButtonPressMask) | ||
|
||
discard XMapWindow(dpy, win) | ||
|
||
var wmDeleteMessage: TAtom = XInternAtom(dpy, "WM_DELETE_WINDOW", | ||
cast[xlib.TBool](false)) | ||
discard XSetWMProtocols(dpy, win, addr(wmDeleteMessage), 1) | ||
|
||
var cs: PSurface = xlib_surface_create(dpy, win, XDefaultVisual(dpy, 0), | ||
SIZEX, SIZEY); | ||
|
||
while true: | ||
discard XNextEvent(dpy, addr(e)) | ||
if e.theType == Expose and cast[PXExposeEvent](e.addr).count < 1: | ||
discard XDrawString(dpy, win , gc, 10, 10, "Hello World!", 12) | ||
paint(cs) | ||
|
||
elif e.theType == ButtonPress: | ||
break | ||
elif e.theType == ClientMessage and | ||
cast[ptr cint]((cast[PXClientMessageEvent](e.addr).data.addr | ||
))[] == cast[cint](wmDeleteMessage): | ||
break | ||
|
||
echo "destroying" | ||
|
||
destroy(cs) | ||
|
||
discard XDestroyWindow(dpy, win) | ||
discard XCloseDisplay(dpy) | ||
|
||
echo "closed display" | ||
|
||
# ---- | ||
main() | ||
|