Subject: [clisp-list] linux:ioctl

From: Peter Wood <[email-masked]>
Date: Tue Mar 5 11:39:06 2002

Hi,

The ffi definition for ioctl in linux.lisp is this:

(def-c-call-out ioctl (:arguments (fd int)
                                  (request int)
                                  (arg c-pointer))
                       (:return-type int))

Which I think is wrong. Since ioctl can either take 2 or 3 args, and
in the case  of 2 args, you need some way to get the answer.  I
changed it to the following:

(def-c-call-out ioctl-read
  (:name "ioctl")
  (:arguments (fd int)
	      (request int)
	      (arg (c-ptr int) :out :alloca))
  (:return-type int))

(def-c-call-out ioctl-write
  (:name "ioctl")
  (:arguments (fd int) (request int) (arg int))
  (:return-type int))

The following can be used to test it from the console (won't work
under X):

(defun check-leds (fd)
  (let ((KDGETLED #x4b31)
	(LED_SCR 1)
	(LED_NUM 2)
	(LED_CAP 4))
    (multiple-value-bind (r s)
	(linux:ioctl-read fd KDGETLED)
      (cond ((= LED_SCR s) 'scroll)
	    ((= LED_NUM s) 'num)
	    ((= LED_CAP s) 'caps)
	    (t 'none)))))

(defun flash-leds (fd n)
;  (let ((fd (linux:open "/dev/tty" linux:O_NOCTTY 0))

;this works but I think it messes up the flags which control the leds
;since they won't turn on aftwrwards on the console on which the
;function has run.  Don't worry, just temporary.
  
  (let ((KDSETLED  #x4b32))
    (unwind-protect (dotimes (v n t)
		      (dolist (led '(1 2 4) nil)
			(linux:ioctl-write fd KDSETLED led)
			(sleep 0.050)) (sleep 0.025))
      (linux:ioctl-write fd KDSETLED 0))))

Where 'fd' is the number of the tty you are on.

I have some questions:

Could ioctl be defined as one function with :in-out parameters,
instead of being split up into two?

If a C function expects a (pointer to) a function in its argument
list, can I give it a lisp function.  Ie, will the ffi convert it to
the correct type for C, automatically.  What should the allocation be?  

Regards,
Peter

---

From: Sam Steingold <[email-masked]>
Date: Tue Mar 5 22:10:04 2002

> * In message <[email-masked]>
> * On the subject of "[clisp-list] linux:ioctl"
> * Sent on Tue, 5 Mar 2002 20:26:03 +0100
> * Honorable Peter Wood <[email-masked]> writes:
>
>   (let ((KDSETLED  #x4b32))

[1]> (flash-leds 1 3)

*** - 1 cannot be converted to the foreign type FFI:C-POINTER
1. Break [2]> 
*** - 0 cannot be converted to the foreign type FFI:C-POINTER
1. Break [3]> 

-- 
Sam Steingold (http://www.podval.org/~sds) running RedHat7.2 GNU/Linux
Keep Jerusalem united! <http://www.onejerusalem.org/Petition.asp>
Read, think and remember! <http://www.iris.org.il> <http://www.memri.org/>
If Perl is the solution, you're solving the wrong problem. - Erik Naggum

---

From: Peter Wood <[email-masked]>
Date: Tue Mar 5 22:37:03 2002

Hi,

What did you change the ffi definition for ioctl to?

I get this:

#<output buffered file-stream character #P"ioctl.txt" @1>
[2]> (load #"lisp/Linux/ioctl.lisp")
;; Loading file lisp/Linux/ioctl.lisp ...
;; Loading of file lisp/Linux/ioctl.lisp is finished.
t
[3]> (check-leds 4)
none
[4]> (check-leds 4)
none
[5]> (check-leds 0)
num
[6]> (check-leds 0)
caps
[7]> (flash-leds 0 10)
t
[8]> (dribble)

Works fine ... with the definitions I sent in the previous post.

Regards,
Peter