Skip to content

Instantly share code, notes, and snippets.

@gbluma
Created December 25, 2015 03:35
Show Gist options
  • Save gbluma/e8248ede07954e54a0bf to your computer and use it in GitHub Desktop.
Save gbluma/e8248ede07954e54a0bf to your computer and use it in GitHub Desktop.
X11 demo in Felix
Description: Linux X11 support
requires_dlibs: -lX11
requires_slibs: -lX11
export requires header "#include <X11/Xlib.h>", package "x11";
cproc HandleEvents(width:int, height:int) = {
println$ "In Felix: width: " + width.str + " height: " + height.str;
}
proc init_x11() = { cstmt """
Display *display;
Visual *visual;
int depth;
int text_x;
int text_y;
XSetWindowAttributes frame_attributes;
Window frame_window;
XFontStruct *fontinfo;
XGCValues gr_values;
GC graphical_context;
XKeyEvent event;
char hello_string[] = "Hello World From Felix";
int hello_string_length = strlen(hello_string);
display = XOpenDisplay(NULL);
visual = DefaultVisual(display, 0);
depth = DefaultDepth(display, 0);
frame_attributes.background_pixel = XWhitePixel(display, 0);
/* create the application window */
frame_window = XCreateWindow(display, XRootWindow(display, 0),
0, 0, 400, 400, 5, depth,
InputOutput, visual, CWBackPixel,
&frame_attributes);
XStoreName(display, frame_window, "Hello World Example");
XSelectInput(display, frame_window, ExposureMask | StructureNotifyMask);
fontinfo = XLoadQueryFont(display, "10x20");
gr_values.font = fontinfo->fid;
gr_values.foreground = XBlackPixel(display, 0);
graphical_context = XCreateGC(display, frame_window,
GCFont+GCForeground, &gr_values);
XMapWindow(display, frame_window);
while ( 1 ) {
XNextEvent(display, (XEvent *)&event);
switch ( event.type ) {
case Expose:
{
XWindowAttributes window_attributes;
int font_direction, font_ascent, font_descent;
XCharStruct text_structure;
XTextExtents(fontinfo, hello_string, hello_string_length,
&font_direction, &font_ascent, &font_descent,
&text_structure);
XGetWindowAttributes(display, frame_window, &window_attributes);
// call our felix procedure
$1(window_attributes.width, window_attributes.height);
text_x = (window_attributes.width - text_structure.width)/2;
text_y = (window_attributes.height -
(text_structure.ascent+text_structure.descent))/2;
XDrawString(display, frame_window, graphical_context,
text_x, text_y, hello_string, hello_string_length);
break;
}
default:
break;
}
}
""" HandleEvents;
}
init_x11();
@gbluma
Copy link
Author

gbluma commented Dec 25, 2015

Note, the HandleEvents procedure is passed into the C++ code via substitution on line 57.

@gbluma
Copy link
Author

gbluma commented Dec 25, 2015

screenshot - 12242015 - 09 44 31 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment