Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
903 lines (842 sloc) 22.5 KB
#! winxed
// GuitorNci.winxed
// Xlib NCI for Guitor
namespace Guitor
{
//**************************************************************
// Internal helper functions
//**************************************************************
namespace Xlib__nci
{
// Variable names
const string
XLIB = "xlib",
XFTLIB = "xftlib",
XPMLIB = "xpmlib",
EVENTPAD = "eventpad",
ANYVIEW = "anyview",
BUTTONVIEW = "buttonview",
KEYVIEW = "keyview",
MOTIONVIEW = "motionview",
STRUCTUREVIEW = "structureview",
EXPOSEVIEW = "exposeview",
FOCUSVIEW = "focusview",
MAPVIEW = "mapview",
CROSSINGVIEW = "crossingview",
SELECTIONCLEARVIEW = "selectionclearview",
SELECTIONVIEW = "selectionview",
SELECTIONREQUESTVIEW = "selectionrequestview",
CLIENTVIEW = "clientview",
XTEXTPROPERTY = "xtextproperty",
XAWMHINTSVIEW = "xawmhintsview",
ATOMVIEW = "atomview",
XCOLORVIEW = "xcolorview",
XFTCOLORVIEW = "xftcolorview",
XFONTVIEW = "xfontview",
XFTFONTVIEW = "xftfontview",
GLYPHINFOVIEW = "glyphinfoview";
function loadXlib()
{
string libs[] = [
"libX11",
"libX11.so",
"libX11.so.6",
"/usr/lib/libX11.so",
"/usr/lib/libX11.so.6",
"cygX11-6"
];
var l;
for (string lib in libs) {
l= loadlib(lib);
if (l) break;
}
return l;
}
function loadXftlib()
{
string libs[] = [
"libXft",
"libXft.so",
"libXft.so.2"
];
var l;
for (string lib in libs) {
l= loadlib(lib);
if (l) break;
}
return l;
}
function loadXpmlib()
{
string libs[] = [
"libXpm",
"libXpm.so",
"libXpm.so.4"
];
var l;
for (string lib in libs) {
l= loadlib(lib);
if (l) break;
}
return l;
}
function getlib()
{
var ns = namespace Xlib__nci;
var l = ns[XLIB];
if (l == null)
ns[XLIB] = l = loadXlib();
return l;
}
function getxftlib()
{
var ns = namespace Xlib__nci;
var l = ns[XFTLIB];
if (l == null)
ns[XFTLIB] = l = loadXftlib();
return l;
}
function getxpmlib()
{
var ns = namespace Xlib__nci;
var l = ns[XPMLIB];
if (l == null)
ns[XPMLIB] = l = loadXpmlib();
return l;
}
}
namespace Xlib__private
{
$include_const "datatypes.pasm";
using namespace Xlib__nci;
//**************************************************************
/*
Event structure definition.
Values used for sizes and offsets are derived
from Xlib docs and testing in several platforms.
*/
const int XLIB_Bool = DATATYPE_INT32;
const int XLIB_Atom = DATATYPE_LONG;
const int XLIB_time_t = DATATYPE_LONG;
const int XLIB_Window = DATATYPE_LONG;
//**************************************************************
function geteventpad()
{
var ns = namespace Xlib__nci;
var st = ns[EVENTPAD];
if (st == null) {
const int PAD = 24;
int viewdata [PAD + 2] = [ DATATYPE_STRUCT, PAD ];
for (int i = 0; i < PAD; ++i)
viewdata[i + 2] = DATATYPE_LONG;
ns[EVENTPAD] = st = new ["StructView"](viewdata);
}
return st;
}
function getanyview()
{
var ns = namespace Xlib__nci;
var st = ns[ANYVIEW];
if (st == null)
ns[ANYVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 5,
DATATYPE_INT, // type
DATATYPE_LONG, // serial
XLIB_Bool, // send event
DATATYPE_PTR, // display
XLIB_Window // window
] );
return st;
}
function getbuttonview()
{
var ns = namespace Xlib__nci;
var st = ns[BUTTONVIEW];
if (st == null)
ns[BUTTONVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 14,
DATATYPE_INT, // type
DATATYPE_LONG, // serial
XLIB_Bool, // send event
DATATYPE_PTR, // display
XLIB_Window, // window
XLIB_Window, // root
XLIB_Window, // subwindow
XLIB_time_t, // time
DATATYPE_INT, // x
DATATYPE_INT, // y
DATATYPE_INT, // x_root
DATATYPE_INT, // y_root
DATATYPE_INT, // state
DATATYPE_INT, // button
XLIB_Bool // same_screen
] );
return st;
}
function getkeyview()
{
var ns = namespace Xlib__nci;
var st = ns[KEYVIEW];
if (st == null)
ns[KEYVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 14,
DATATYPE_INT, // type
DATATYPE_LONG, // serial
XLIB_Bool, // send event
DATATYPE_PTR, // display
XLIB_Window, // window
XLIB_Window, // root
XLIB_Window, // subwindow
XLIB_time_t, // time
DATATYPE_INT, // x
DATATYPE_INT, // y
DATATYPE_INT, // x_root
DATATYPE_INT, // y_root
DATATYPE_INT, // state
DATATYPE_INT, // keycode
XLIB_Bool // same_screen
] );
return st;
}
function getmotionview()
{
var ns = namespace Xlib__nci;
var st = ns[MOTIONVIEW];
if (st == null)
ns[MOTIONVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 14,
DATATYPE_INT, // type
DATATYPE_LONG, // serial
XLIB_Bool, // send event
DATATYPE_PTR, // display
XLIB_Window, // window
XLIB_Window, // root
XLIB_Window, // subwindow
XLIB_time_t, // time
DATATYPE_INT, // x
DATATYPE_INT, // y
DATATYPE_INT, // x_root
DATATYPE_INT, // y_root
DATATYPE_INT, // state
DATATYPE_INT // keycode
] );
return st;
}
function getstructureview()
{
var ns = namespace Xlib__nci;
var st = ns[STRUCTUREVIEW];
if (st == null)
ns[STRUCTUREVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 13,
DATATYPE_INT, // type
DATATYPE_LONG, // serial
XLIB_Bool, // send event
DATATYPE_PTR, // display
XLIB_Window, // event window
XLIB_Window, // window
DATATYPE_INT, // x
DATATYPE_INT, // y
DATATYPE_INT, // width
DATATYPE_INT, // height
DATATYPE_INT, // border_width
XLIB_Window, // above
XLIB_Bool // override_redirect
] );
return st;
}
function getexposeview()
{
var ns = namespace Xlib__nci;
var st = ns[EXPOSEVIEW];
if (st == null)
ns[EXPOSEVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 10,
DATATYPE_INT, // type
DATATYPE_LONG, // serial
XLIB_Bool, // send event
DATATYPE_PTR, // display
XLIB_Window, // window
DATATYPE_INT, // x
DATATYPE_INT, // y
DATATYPE_INT, // width
DATATYPE_INT, // height
DATATYPE_INT // count
] );
return st;
}
function getfocusview()
{
var ns = namespace Xlib__nci;
var st = ns[FOCUSVIEW];
if (st == null)
ns[FOCUSVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 7,
DATATYPE_INT, // type
DATATYPE_LONG, // serial
XLIB_Bool, // send event
DATATYPE_PTR, // display
XLIB_Window, // window
DATATYPE_INT, // mode
DATATYPE_INT // detail
] );
return st;
}
function getmapview()
{
// Used for Map and Unmap Notify
var ns = namespace Xlib__nci;
var st = ns[MAPVIEW];
if (st == null)
ns[FOCUSVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 7,
DATATYPE_INT, // type
DATATYPE_LONG, // serial
XLIB_Bool, // send event
DATATYPE_PTR, // display
XLIB_Window, // event window
XLIB_Window, // window mapped
XLIB_Bool // dverride_redirect or from_configure
] );
return st;
}
function getcrossingview()
{
var ns = namespace Xlib__nci;
var st = ns[CROSSINGVIEW];
if (st == null)
ns[CROSSINGVIEW] = st = new ["StructView"] ( [ DATATYPE_STRUCT, 17,
DATATYPE_INT, // type
DATATYPE_LONG, // serial
XLIB_Bool, // send event
DATATYPE_PTR, // display
XLIB_Window, // window
XLIB_Window, // root
XLIB_Window, // subwindow
XLIB_time_t, // time
DATATYPE_INT, // x
DATATYPE_INT, // y
DATATYPE_INT, // x_root
DATATYPE_INT, // y_root
DATATYPE_INT, // mode
DATATYPE_INT, // detail
XLIB_Bool, // same_screen
XLIB_Bool, // focus
DATATYPE_INT // state
] );
return st;
}
function getselectionclearview()
{
var ns = namespace Xlib__nci;
var st = ns[SELECTIONCLEARVIEW];
if (st == null)
ns[SELECTIONCLEARVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 7,
DATATYPE_INT, // type
DATATYPE_LONG, // serial
XLIB_Bool, // send event
DATATYPE_PTR, // display
XLIB_Window, // window
XLIB_Atom, // selection
XLIB_time_t // time
] );
return st;
}
function getselectionrequestview()
{
var ns = namespace Xlib__nci;
var st = ns[SELECTIONREQUESTVIEW];
if (st == null)
ns[SELECTIONREQUESTVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 10,
DATATYPE_INT, // type
DATATYPE_LONG, // serial
XLIB_Bool, // send event
DATATYPE_PTR, // display
XLIB_Window, // owner
XLIB_Window, // requestor
XLIB_Atom, // selection
XLIB_Atom, // target
XLIB_Atom, // property
XLIB_time_t // time
] );
return st;
}
function getselectionview()
{
var ns = namespace Xlib__nci;
var st = ns[SELECTIONVIEW];
if (st == null)
ns[SELECTIONVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 9,
DATATYPE_INT, // type
DATATYPE_LONG, // serial
XLIB_Bool, // send event
DATATYPE_PTR, // display
XLIB_Window, // requestor
XLIB_Atom, // selection
XLIB_Atom, // target
XLIB_Atom, // property
XLIB_time_t // time
] );
return st;
}
function getclientview()
{
var ns = namespace Xlib__nci;
var st = ns[CLIENTVIEW];
if (st == null)
ns[CLIENTVIEW] = st = new ["StructView"] ( [ DATATYPE_STRUCT, 10,
DATATYPE_INT, // type
DATATYPE_LONG, // serial
XLIB_Bool, // send event
DATATYPE_PTR, // display
DATATYPE_INT, // window
XLIB_Atom, // message_type
DATATYPE_INT, // format
DATATYPE_LONG, // data l 0
DATATYPE_LONG, // data l 1
DATATYPE_LONG // data l 2
] );
return st;
}
//**************************************************************
// Other structures
function getatomview(int n)
{
int init[2 + n] = [ DATATYPE_STRUCT, n ];
for (int i = 0; i < n; ++i)
init[i + 2] = XLIB_Atom;
return new ["StructView"] (init);
}
function gettextpropertyview()
{
var ns = namespace Xlib__nci;
var st = ns[XTEXTPROPERTY];
if (st == null)
ns[XTEXTPROPERTY] = st = new ["StructView"]( [ DATATYPE_STRUCT, 4,
DATATYPE_PTR, // value
XLIB_Atom, // encoding
DATATYPE_INT, // format
DATATYPE_LONG // nitems
] );
return st;
}
function textpropertyfromstring(string str, int encoding)
{
var view = gettextpropertyview();
var prop = view.alloc();
var pstr = str_to_achar_utf8(str);
int l = elements(pstr);
view[prop, 0] = pstr;
view[prop, 1] = encoding;
view[prop, 2] = 8;
view[prop, 3] = l;
return prop;
}
function getxawmhints()
{
var ns = namespace Xlib__nci;
var st = ns[XAWMHINTSVIEW];
if (st == null)
ns[XAWMHINTSVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 9,
DATATYPE_LONG, // flags
XLIB_Bool, // input
DATATYPE_INT, // initial_state
DATATYPE_PTR, // icon_pixmap
DATATYPE_PTR, // icon_window
DATATYPE_INT, // icon_x
DATATYPE_INT, // icon_y
DATATYPE_PTR, // icon_mask
DATATYPE_PTR // window_group
] );
return st;
}
function getxcolorview()
{
var ns = namespace Xlib__nci;
var st = ns[XCOLORVIEW];
if (st == null)
ns[XCOLORVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 6,
DATATYPE_LONG, // pixel
DATATYPE_SHORT, // red
DATATYPE_SHORT, // green
DATATYPE_SHORT, // blue
DATATYPE_CHAR, // flags
DATATYPE_CHAR // pad
] );
return st;
}
function getxftcolorview()
{
var ns = namespace Xlib__nci;
var st = ns[XFTCOLORVIEW];
if (st == null)
ns[XFTCOLORVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 5,
DATATYPE_LONG, // pixel
DATATYPE_SHORT, // red
DATATYPE_SHORT, // green
DATATYPE_SHORT, // blue
DATATYPE_SHORT // alpha
] );
return st;
}
function getglyphinfoview()
{
var ns = namespace Xlib__nci;
var st = ns[GLYPHINFOVIEW];
if (st == null)
ns[GLYPHINFOVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 6,
DATATYPE_SHORT, // width
DATATYPE_SHORT, // height
DATATYPE_SHORT, // x
DATATYPE_SHORT, // y
DATATYPE_SHORT, // xOff
DATATYPE_SHORT // yOff
] );
return st;
}
function getxfontview()
{
var ns = namespace Xlib__nci;
var st = ns[XFONTVIEW];
if (st == null)
ns[XFONTVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 24,
DATATYPE_PTR, // ext_data
DATATYPE_INT, // fid
DATATYPE_INT, // direction
DATATYPE_INT, // min_char_or_byte2
DATATYPE_INT, // max_char_or_byte2
DATATYPE_INT, // min_byte1
DATATYPE_INT, // max_byte1
XLIB_Bool, // all_chars_exists
DATATYPE_INT, // default_char
DATATYPE_INT, // n_properties
DATATYPE_PTR, // properties
// min_bounds
DATATYPE_SHORT, // lbearing
DATATYPE_SHORT, // rbearing
DATATYPE_SHORT, // width
DATATYPE_SHORT, // ascent
DATATYPE_SHORT, // descent
// max_bounds
DATATYPE_SHORT, // lbearing
DATATYPE_SHORT, // rbearing
DATATYPE_SHORT, // width
DATATYPE_SHORT, // ascent
DATATYPE_SHORT, // descent
DATATYPE_PTR, // per_char
DATATYPE_INT, // ascent
DATATYPE_INT // descent
] );
return st;
}
function getxftfontview()
{
var ns = namespace Xlib__nci;
var st = ns[XFTFONTVIEW];
if (st == null)
ns[XFTFONTVIEW] = st = new ["StructView"]( [ DATATYPE_STRUCT, 5,
DATATYPE_INT, // ascent
DATATYPE_INT, // descent
DATATYPE_INT, // height
DATATYPE_INT, // max_advance_width
DATATYPE_PTR, // charset
DATATYPE_PTR // pattern
] );
return st;
}
//**************************************************************
function create_function(string funcname)
{
string sig;
switch (funcname) {
case "XFree":
case "XFlush":
sig = "vp";
break;
case "XSync":
case "XFreePixmap":
case "XRaiseWindow":
sig = "vpi";
break;
case "XDisplayName":
case "XOpenDisplay":
sig = "pp";
break;
case "XCloseDisplay":
case "XDefaultScreen":
case "XStringToKeysym":
case "XSetLocaleModifiers":
case "XPending":
sig = "ip";
break;
case "setlocale":
sig = "pip";
break;
case "XDefaultGC":
case "XDefaultVisual":
case "XDefaultColormap":
sig = "ppi";
break;
case "XRootWindow":
case "XDefaultDepth":
case "XBlackPixel":
case "XWhitePixel":
case "XDisplayWidth":
case "XDisplayWidthMM":
case "XDisplayHeight":
case "XDisplayHeightMM":
case "XMapWindow":
case "XUnmapWindow":
case "XDestroyWindow":
case "XFilterEvent":
case "XKeysymToKeycode":
sig = "ipi";
break;
case "XCreateGC":
sig = "ppipi";
break;
case "XSetForeground":
case "XSetBackground":
case "XSetFunction":
case "XSetSubwindowMode":
case "XSetClipMask":
sig = "vppi";
break;
case "XCreatePixmap":
sig = "ipiiii";
break;
case "XSetInputFocus":
sig = "vpiil";
break;
case "XSelectInput":
case "XSetTransientForHint":
sig = "vpii";
break;
case "XNextEvent":
sig = "vpp";
break;
case "XSendEvent":
sig = "ipiilp";
break;
case "XKeycodeToKeysym":
sig = "ipii";
break;
case "XKeysymToString":
sig = "pi";
break;
case "XLookupString":
sig = "ippipp";
break;
case "XmbLookupString":
case "Xutf8LookupString":
sig = "ipppipp";
break;
case "XOpenIM":
sig = "ppppp";
break;
case "XCreateIC":
sig = "ppplplplp";
break;
case "XInternAtom":
sig = "ippi";
break;
case "XGetAtomName":
sig = "ppl";
break;
case "XStoreName":
sig = "ipip";
break;
case "XSetWMName":
sig = "vpip";
break;
case "XDrawPoint":
sig = "ipipii";
break;
case "XDrawLine":
case "XDrawRectangle":
case "XFillRectangle":
sig = "ipipiiii";
break;
case "XDrawLines":
sig = "vpippii";
break;
case "XFillPolygon":
sig = "vpippiii";
break;
case "XClearArea":
sig = "ipiiiiii";
break;
case "XCreateSimpleWindow":
sig = "ipiiiiiiii";
break;
case "XSetWMProtocols":
sig = "ipipi";
break;
case "XMoveWindow":
case "XResizeWindow":
sig = "ipiii";
break;
case "XMoveResizeWindow":
sig = "ipiiiii";
break;
case "XParseColor":
sig = "ipppp";
break;
case "XAllocColor":
sig = "ippp";
break;
case "XTextWidth":
case "XmbTextEscapement":
sig = "ippi";
break;
case "XDrawString":
case "XDrawImageString":
sig = "vpipiipi";
break;
case "XmbDrawString":
case "XmbDrawImageString":
case "Xutf8DrawString":
sig = "vpippiipi";
break;
case "XCreateFontSet":
sig = "pppppp";
break;
case "XSetLineAttributes":
sig = "vppiiii";
break;
case "XListProperties":
sig = "ppip";
break;
case "XQueryPointer":
sig = "ipippppppp";
break;
case "XWarpPointer":
sig = "vpiiiiiiii";
break;
case "XGrabPointer":
sig = "ipiiiiiiil";
break;
case "XUngrabPointer":
sig = "vpl";
break;
case "XGetWindowProperty":
sig = "ipiilliippppp";
break;
case "XChangeProperty":
sig = "vpiiiiipi";
break;
case "XSetSelectionOwner":
sig = "vpiil";
break;
case "XGetSelectionOwner":
sig = "ipi";
break;
case "XConvertSelection":
sig = "vpiiiil";
break;
case "XCopyArea":
sig = "vpiipiiiiii";
break;
default:
throw "Function " + funcname + " unknown";
}
var func = dlfunc(getlib(), funcname, sig);
return func;
}
function create_xft_function(string funcname)
{
string sig;
switch (funcname) {
case "XftDrawCreate":
sig = "ppipp";
break;
case "XftDrawDestroy":
sig = "vp";
break;
case "XftDrawRect":
sig = "vppiiii";
break;
case "XftColorAllocName":
sig = "ippppp";
break;
case "XftFontOpenXlfd":
case "XftFontOpenName":
sig = "ppip";
break;
case "XftTextExtentsUtf8":
sig = "vpppip";
break;
case "XftDrawStringUtf8":
sig = "vpppiipi";
break;
default:
throw "Function " + funcname + " unknown";
}
var func = dlfunc(getxftlib(), funcname, sig);
return func;
}
function create_xpm_function(string funcname)
{
string sig;
switch (funcname) {
case "XpmCreatePixmapFromBuffer":
sig = "ipipppp";
break;
default:
throw "Function " + funcname + " unknown";
}
var func = dlfunc(getxpmlib(), funcname, sig);
return func;
}
//**************************************************************
function str_to_achar(string s)
{
var cstring = new ["ByteBuffer"];
cstring =: s;
return cstring;
}
function str_to_achar_utf8(string s)
{
var cstring = new ["ByteBuffer"];
cstring =: trans_encoding(s, "utf8");
return cstring;
}
function str_to_cstring(string s)
{
var cstring = new ["ByteBuffer"];
cstring =: trans_encoding(s, "utf8");
push(cstring, 0);
return cstring;
}
function string_from_nci(p)
{
string s;
if (p != null)
s = p.as_string("utf8");
return s;
}
function newAtomlist(int n)
{
var view = getatomview(n);
var data = view.alloc();
return data;
}
function getfun(string funcname)
{
var ns = namespace Xlib__nci;
var fun = ns[funcname];
if (fun == null)
ns[funcname] = fun = create_function(funcname);
return fun;
}
function getxftfun(string funcname)
{
var ns = namespace Xlib__nci;
var fun = ns[funcname];
if (fun == null)
ns[funcname] = fun = create_xft_function(funcname);
return fun;
}
function getxpmfun(string funcname)
{
var ns = namespace Xlib__nci;
var fun = ns[funcname];
if (fun == null)
ns[funcname] = fun = create_xpm_function(funcname);
return fun;
}
} // namespace Xlib__private
} // namespace Guitor
// End