From e0c31472ee629d4544d0d58b55153bdaca02671e Mon Sep 17 00:00:00 2001 From: ketmar Date: Tue, 23 Nov 2010 19:00:26 +0200 Subject: [PATCH] more X11 primitives --- modules/x11/x11basewindow.st | 8 +++++ modules/x11/x11singleton.st | 29 ++++++++++++--- samples/x11/x11test.st | 35 ++++++++++-------- src/primlib/x11lib/lstpl_x11.c | 81 ++++++++++++++++++++++++++++++++++++------ 4 files changed, 124 insertions(+), 29 deletions(-) rewrite samples/x11/x11test.st (61%) diff --git a/modules/x11/x11basewindow.st b/modules/x11/x11basewindow.st index dc2026a..90198ab 100644 --- a/modules/x11/x11basewindow.st +++ b/modules/x11/x11basewindow.st @@ -27,6 +27,14 @@ class: X11BaseWindow [ ^gc ] + fgColor: r g: g b: b [ + X11Singleton fgColor: gc color: (X11Singleton color: r g: g b: b) + ] + + bgColor: r g: g b: b [ + X11Singleton bgColor: gc color: (X11Singleton color: r g: g b: b) + ] + passEvent: aWid event: evt [ "returns true if event handler found" "'me: ' print. (X11Singleton wid2str: wid) print. diff --git a/modules/x11/x11singleton.st b/modules/x11/x11singleton.st index 541f610..3fdd990 100644 --- a/modules/x11/x11singleton.st +++ b/modules/x11/x11singleton.st @@ -2,17 +2,28 @@ Package [ X11 ] -class: X11Singleton | widgetlist quitflag | [ +class: X11Singleton | widgetlist quitflag disp | [ ^wid2str: wid [ <#X11Do 200 wid>. self primitiveFailed ] + ^sameWids: w0 and: w1 [ + <#X11Do 201 w0 w1>. + self primitiveFailed + ] + ^subtypeName: aX11 [ <#X11Do 0 aX11>. ^nil ] + ^initialize [ + disp := X11Singleton XOpenDisplay: nil. + X11Singleton defaultDispay: disp. + ] + + ^XOpenDisplay: aName [ <#X11Do 1>. self primitiveFailed @@ -73,13 +84,23 @@ class: X11Singleton | widgetlist quitflag | [ self primitiveFailed ] - ^sameWids: w0 and: w1 [ - <#X11Do 11 w0 w1>. + ^color: r g: g b: b [ + <#X11Do 11 r g b>. + self primitiveFailed + ] + + ^fgColor: gc color: clr [ + <#X11Do 12 true gc clr>. + self primitiveFailed + ] + + ^bgColor: gc color: clr [ + <#X11Do 12 false gc clr>. self primitiveFailed ] ^XDrawLine: wid gc: gc x0: x0 y0: y0 x1: x1 y1: y1 [ - <#X11Do 12 wid gc x0 y0 x1 y1>. + <#X11Do 13 wid gc x0 y0 x1 y1>. self primitiveFailed ] diff --git a/samples/x11/x11test.st b/samples/x11/x11test.st dissimilarity index 61% index 9b82c1e..d303608 100644 --- a/samples/x11/x11test.st +++ b/samples/x11/x11test.st @@ -1,14 +1,21 @@ -Requires [ x11 ] - - -{ - | disp w | - disp := X11Singleton XOpenDisplay: nil. - X11Singleton defaultDispay: disp. - "(X11Singleton subtypeName: disp) printNl." - w := X11Window new: nil x: 0 y: 0 width: 200 height: 100. - w onExpose: [:w | X11Singleton XDrawLine: w wid gc: w gc x0: 1 y0: 1 x1: 80 y1: 60 ]. - X11Singleton add: w. - w show. - X11Singleton eventLoop. -} +Requires [ x11 ] + + +{ + | w | + X11Singleton initialize. + w := X11Window new: nil x: 0 y: 0 width: 200 height: 100. + w onExpose: [:w | + w fgColor: 1.0 g: 0 b: 0. + X11Singleton XDrawLine: w wid gc: w gc x0: 1 y0: 1 x1: 80 y1: 60. + w fgColor: 0 g: 1.0 b: 0. + X11Singleton XDrawLine: w wid gc: w gc x0: 1 y0: 10 x1: 80 y1: 60. + w fgColor: 0 g: 0 b: 1.0. + X11Singleton XDrawLine: w wid gc: w gc x0: 1 y0: 20 x1: 80 y1: 60. + w fgColor: 0.5 g: 0 b: 0. + X11Singleton XDrawLine: w wid gc: w gc x0: 1 y0: 50 x1: 80 y1: 60. + ]. + X11Singleton add: w. + w show. + X11Singleton eventLoop. +} diff --git a/src/primlib/x11lib/lstpl_x11.c b/src/primlib/x11lib/lstpl_x11.c index 52f1110..5d6350a 100644 --- a/src/primlib/x11lib/lstpl_x11.c +++ b/src/primlib/x11lib/lstpl_x11.c @@ -41,6 +41,8 @@ enum { static Display *curDisp = NULL; +static Colormap curCMap; +static int cmapIsHere = 0; #define MAX_QUEUE 128 static XEvent eQueue[MAX_QUEUE]; static int eqHead = 0; @@ -119,6 +121,23 @@ static X11Info *getX11 (lstObject *o) { } +static int getColor (lstObject *o, unsigned short *c) { + int cc; + if (LST_IS_SMALLINT(o)) { + cc = lstIntValue(o); + if (cc < 0) cc = 0; else if (cc > 65535) cc = 65535; + } else if (o->stclass == lstIntegerClass) { + LstLInt n = lstLIntValue(o); + if (n < 0) cc = 0; else if (n > 65535) cc = 65535; else cc = n; + } else if (o->stclass == lstFloatClass) { + LstFloat n = lstFloatValue(o); + if (n < 0.0) cc = 0; else if (n >= 1.0) cc = 65535; else cc = (int)(n*65535); + } else return -1; + *c = cc; + return 0; +} + + LST_PRIMFN(lpX11Do) { if (LST_PRIMARGC < 1 || !LST_IS_SMALLINT(LST_PRIMARG(0))) return NULL; int action = lstIntValue(LST_PRIMARG(0)), tmp; @@ -184,7 +203,16 @@ LST_PRIMFN(lpX11Do) { if (xi->subtype != X11_DISPLAY || !xi->handle) return NULL; dpy = (Display *)xi->handle; } - if (dpy != curDisp) { curDisp = dpy; eqHead = eqTail = eqCount = 0; } + if (dpy != curDisp) { + curDisp = dpy; eqHead = eqTail = eqCount = 0; + /*if (cmapIsHere) {}*/ + Window win = XCreateSimpleWindow(curDisp, DefaultRootWindow(curDisp), 0, 0, 1, 1, 0, + BlackPixel(curDisp, DefaultScreen(curDisp)), BlackPixel(curDisp, DefaultScreen(curDisp))); + /*fprintf(stderr, "%p\n", (void *)win);*/ + curCMap = XCreateColormap(curDisp, win, DefaultVisual(curDisp, DefaultScreen(curDisp)), AllocNone); + XDestroyWindow(curDisp, win); + cmapIsHere = 1; + } } else { if (curDisp) res = newX11(curDisp, X11_DISPLAY, NULL); } @@ -250,17 +278,38 @@ LST_PRIMFN(lpX11Do) { res = newX11((void *)gc, X11_GC, killGC); if ((xi = getX11(res))) xi->dpy = curDisp; break; } - case 11: { /* SameWID w0 w1 */ - if (LST_PRIMARGC < 3) return NULL; - if (!(xi = getX11(LST_PRIMARG(1)))) break; - if (xi->subtype != X11_WINDOW || !xi->handle) break; - Window w0 = (Window)xi->handle; - if (!(xi = getX11(LST_PRIMARG(2)))) break; - if (xi->subtype != X11_WINDOW || !xi->handle) break; - Window w1 = (Window)xi->handle; - res = w0==w1 ? lstTrueObj : lstFalseObj; + case 11: { /* newColor r g b */ + if (!curDisp || LST_PRIMARGC < 4) return NULL; +/* + op = LST_PRIMARG(1); if (!LST_IS_SMALLINT(op)) return NULL; + int r = lstIntValue(op); + op = LST_PRIMARG(2); if (!LST_IS_SMALLINT(op)) return NULL; + int g = lstIntValue(op); + op = LST_PRIMARG(3); if (!LST_IS_SMALLINT(op)) return NULL; + int b = lstIntValue(op); +*/ + XColor cc; + /*cc.red = r; cc.green = g; cc.blue = b;*/ + if (getColor(LST_PRIMARG(1), &cc.red)) return NULL; + if (getColor(LST_PRIMARG(2), &cc.green)) return NULL; + if (getColor(LST_PRIMARG(3), &cc.blue)) return NULL; + /*Status rc = */XAllocColor(curDisp, curCMap, &cc); + /*fprintf(stderr, "rc=%d; r=%d; g=%d; b=%d; pixel=%p\n", (int)rc, r, g, b, (void *)cc.pixel);*/ + int pix = cc.pixel; + /*if (rc == 0)*/ res = lstNewInteger(pix); break; } - case 12: { /* XDrawLine wid gc x0 y0 x1 y1 */ + case 12: { /* SetColor flag gc color */ + if (!curDisp || LST_PRIMARGC < 4) return NULL; + if (!(xi = getX11(LST_PRIMARG(2)))) return NULL; + if (xi->subtype != X11_GC || !xi->handle) return NULL; + GC gc = (GC)xi->handle; + op = LST_PRIMARG(3); if (!LST_IS_SMALLINT(op)) return NULL; + int clr = lstIntValue(op); + op = LST_PRIMARG(1); + if (op == lstTrueObj) XSetForeground(curDisp, gc, clr); + else XSetBackground(curDisp, gc, clr); + break; } + case 13: { /* XDrawLine wid gc x0 y0 x1 y1 */ if (LST_PRIMARGC < 7 || !curDisp) return NULL; int x0, y0, x1, y1; Window parent; @@ -291,6 +340,16 @@ LST_PRIMFN(lpX11Do) { sprintf(buf, "%p", (void *)xi->handle); res = lstNewString(buf); break; } + case 201: { /* SameWID w0 w1 */ + if (LST_PRIMARGC < 3) return NULL; + if (!(xi = getX11(LST_PRIMARG(1)))) break; + if (xi->subtype != X11_WINDOW || !xi->handle) break; + Window w0 = (Window)xi->handle; + if (!(xi = getX11(LST_PRIMARG(2)))) break; + if (xi->subtype != X11_WINDOW || !xi->handle) break; + Window w1 = (Window)xi->handle; + res = w0==w1 ? lstTrueObj : lstFalseObj; + break; } default: return NULL; } return res; -- 2.11.4.GIT