Xlib and newLisp
Posted: Thu Apr 12, 2007 7:47 pm
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)
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)