Xlib and newLisp

Guiserver, GTK-server, OpenGL, PostScript,
HTML 5, MIDI, IDE
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Xlib and newLisp

Post by newdep »

As part of a personal project im currently creating, I did today some Xlib testing.
Now Xlib programming is quiet boring and you need to get the steps in the
right order, further more Xlib is realy for the C programmer.

I wont continue on Xlib because its simply too time consuming and not related
to what I had in mind. But the newlisp script that came from it is a nice handout
for those who like to build more in Xlib (direct IO). Its quiet simple this example.

If I have finished my initital project (and newlisp will be part of it) Ill post
it here again ;-) Then you will see where this example fits in place ;-)

Enjoy the code....hope it runs on your machine ;-)

#!/usr/bin/newlisp


;; ----------------------------------------------------------------------------------------------
;; Signals part
;; ----------------------------------------------------------------------------------------------
(define (ctrlc) (XCloseDisplay display) (exit))
(signal 2 'ctrlc)

;; ----------------------------------------------------------------------------------------------
;; X11 part
;; ----------------------------------------------------------------------------------------------
(setq X11 '(
"XOpenDisplay"
"XQueryPointer"
"XDefaultRootWindow"
"XScreenOfDisplay"
"XWidthOfScreen"
"XHeightOfScreen"
"XDefaultScreenOfDisplay"
"XDefaultDepthOfScreen"
"XCreateSimpleWindow"
"XMapWindow"
"XSync"
"XFlush"
"XSetForeground"
"XSetBackground"
"XBlackPixel"
"XWhitePixel"
"XCloseDisplay"
"XCreateGC"
"XDrawPoint"
"XDrawLine"
"XDefaultColormap"
"XAllocNamedColor"
))

(define (import-X11) (dolist (x X11 ) (import "/usr/X11/lib/libX11.so" x)))

(import-X11)
(setq display (XOpenDisplay NULL)) ;; get current display
(setq window (XDefaultRootWindow display)) ;; get current window
(setq screen (XDefaultScreenOfDisplay display)) ;; get current default screen

(setq screen_width (XWidthOfScreen screen)) ;; get screen width
(setq screen_height (XHeightOfScreen screen)) ;; get screen height
(setq screen_depth (XDefaultDepthOfScreen screen)) ;; get screen depth

;; ----------------------------------------------------------------------------------------------
;; QueryXY
;; ----------------------------------------------------------------------------------------------
(setq child_return (pack "ld" "\000")) ;; Returns the child window that the pointer is located in, if any.
(setq root_return (pack "ld" "\000")) ;; Returns the root window that the pointer is in.
(setq root_x (pack "ld" "\000")) ;; Return the pointer coordinates relative to the root window's origin.
(setq root_y (pack "ld" "\000")) ;; Return the pointer coordinates relative to the root window's origin.
(setq win_x (pack "ld" "\000")) ;; Return the pointer coordinates relative to the specified window.
(setq win_y (pack "ld" "\000")) ;; Return the pointer coordinates relative to the specified window.
(setq mask_return (pack "ld" "\000")) ;; Returns the current state of the modifier keys and pointer buttons.

(define (QueryXY)
(XQueryPointer display window root_return child_return root_x root_y win_x win_y mask_return))

;; ----------------------------------------------------------------------------------------------
;; Make a window
;; ----------------------------------------------------------------------------------------------
(println "Screen size: " screen_width "x" screen_height ":" screen_depth)

(setq GC (pack "ld ld ld ld ld ld ld ld ld ld ld ld ld ld ld ld ld ld ld ld ld ld ld" "\000"))

(setq win (XCreateSimpleWindow display window 0 0 (/ screen_width 2) (/ screen_height 2) 2 (XWhitePixel display (get-int screen)) (XBlackPixel display (get-int screen))))
(XMapWindow display win)
(XSync display 0)

;; create image
(setq gc (XCreateGC display win 0 (address GC)))

; white on black
(XSetBackground display gc (XBlackPixel display (get-int screen)))
(XSetForeground display gc (XWhitePixel display (get-int screen)))

; simple event loop, not X wordy!
(while true
(QueryXY)
(XDrawPoint display win gc (/ (get-int root_x) 2) (/ (get-int root_y) 2))
(XFlush display)
(sleep 1)
)

(XCloseDisplay display)
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

this looks suspicous ;-)

Code: Select all

(setq GC (pack "ld ld ld ld ld ld ld ld ld ld ld ld ld ld ld ld ld ld ld ld ld ld ld" "\000"))
If you want to reserve space for 23 32-bit integers initilialized to zero you would have to specify 23 zero values for the 23 ld formats. Your code creates a 4-byte buffer containing the address of a zero length string. This could get you in trouble with overwriting memory.

I believe you want this:

Code: Select all

(setq GC (dup "\000" (* 23 4)))
Which would create a buffer of 23 * 4 = 92 bytes initialized with all binary 0s. I assume GC is a Graphics Context (big data structure for Xlib ?)

Lutz

ps: your code creates a nice window on MacOS X (with X-windows installed) when using this library: /usr/X11R6/lib/libX11.dylib and mouse movements leave a dotted trace.

pps: I am travelling tomorrow and will not be available until later on Saturday

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Haha yes your right the GC was more a long write-out , forgot
to simplyfy it ;-) I always use a long write-out for counting during
programming... Good to hear it runs too on the Mac ;-)
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Also, for reserving pointer space for win_x, win_y etc.m you do better:

Code: Select all

(setq win_x (dup "\000" 4)) ;; reserve space for an int32 pointer

; or

(setq win_x (pack "ld" 0)) ;; reserve space for an int32 pointer and init to 0
Lutz

Locked