Changeset main,28 for main/cdk.lisp


Ignore:
Timestamp:
11/04/2007 06:19:33 AM (19 years ago)
Author:
David Owen <dsowen@…>
branch-nick:
tui-new
revision id:
dsowen@fugue88.ws-20071104061933-b5oiof728551luy5
Message:

Major overhaul to get rid of CDK stuff, and modularize all the ncurses stuff.
Using CLOS for genericity.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/cdk.lisp

    r27 r28  
    11(defpackage #:cdk
    2   (:use #:cl #:cffi)
    3   (:export #:with-screen #:with-der-window #:add-string #:add-clipped-string
    4            #:wmove))
     2  (:use #:cl #:cffi #:dso-util)
     3  (:export #:+err+ #:+ok+))
    54
    65(in-package #:cdk)
     
    1716;;; Global constants.
    1817
    19 (defctype chtype :uint64)
    20 (defctype wchar :int32)                 ; On my 64-bit laptop.
     18
     19
     20(defconstant +err+ -1)
     21(defconstant +ok+ 0)
    2122
    2223(defcvar ("cdk_exit_early" +cdk-exit-early+) :int :read-only t)
     
    2728;;; Screen-wide, initialization, and finalization routines.
    2829
    29 (defcfun ("initscr" c-init-curses-screen) :pointer)
    3030
    3131(defcfun ("start_color" c-curs-start-color) :int)
    3232
    33 (defcfun ("endwin" c-end-curses) :int)
    34 
    35 (defmacro with-screen ((screen) &body body)
    36   `(let ((,screen (c-init-curses-screen)))
    37      (unwind-protect (progn ,@body)
    38        (c-end-curses))))
    39 
    40 (defcfun ("initCDKScreen" c-init-cdk-screen) :pointer
    41   (curseswindow :pointer))
    42 
    43 (defcfun ("initCDKColor" c-init-color) :void)
    44 
    45 (defcfun ("eraseCDKScreen" c-erase-screen) :void
    46   (screen :pointer))
    47 
    48 (defcfun ("destroyCDKScreen" c-destroy-screen) :void
    49   (screen :pointer))
    50 
    51 (defcfun ("endCDK" c-end-cdk) :void)
    52 
    53 (defcfun ("refreshCDKScreen" c-refresh-screen) :void
    54   (screen :pointer))
    55 
    56 (defcfun ("newwin" c-new-window) :pointer
    57   (lines :int)
    58   (columns :int)
    59   (y :int)
    60   (x :int))
    61 
    62 (defcfun ("subwin" c-sub-window) :pointer
    63   (orig :pointer)
    64   (lines :int)
    65   (columns :int)
    66   (y :int)
    67   (x :int))
    68 
    69 (defcfun ("derwin" c-der-window) :pointer
    70   (orig :pointer)
    71   (lines :int)
    72   (columns :int)
    73   (y :int)
    74   (x :int))
    75 
    76 (defcfun ("delwin" c-delete-window) :int
    77   (window :pointer))
    78 
    79 (defmacro with-der-window ((window parent lines columns y x) &body body)
    80   `(let ((,window (c-der-window ,parent ,lines ,columns ,y ,x)))
    81      (when (null-pointer-p ,window)
    82        ;; TODO: Replace this with a condition and a restart.
    83        (error "Couldn't create window."))
    84      (unwind-protect (progn ,@body)
    85        (c-delete-window ,window))))
    86 
    87 (defcfun ("box" c-box) :int
    88   (window :pointer)
    89   (vertical-ch chtype)
    90   (horizontal-ch chtype))
    91 
    92 (defcfun ("wborder" c-window-border) :int
    93   (window :pointer)
    94   (ls chtype)
    95   (rs chtype)
    96   (ts chtype)
    97   (bs chtype)
    98   (tl chtype)
    99   (tr chtype)
    100   (bl chtype)
    101   (br chtype))
    102 
    103 (defcfun ("wbkgd" c-set-window-background) :int
    104   (window :pointer)
    105   (ch chtype))
    106 
    107 (defcfun "wbkgrnd" :int
    108   (window :pointer)
    109   (wch :pointer))
    110 
    111 (defcfun ("werase" c-erase-window) :int
    112   (window :pointer))
    113 
    114 (defcfun "getcury" :int
    115   (window :pointer))
    116 
    117 (defcfun "getcurx" :int
    118   (window :pointer))
    119 
    120 (defcfun "getmaxy" :int
    121   (window :pointer))
    122 
    123 (defcfun "getmaxx" :int
    124   (window :pointer))
    125 
    126 (defcfun "wmove" :int
    127   (window :pointer)
    128   (y :int)
    129   (x :int))
    130 
    131 (defcfun "wadd_wch" :int
    132   (window :pointer)
    133   (wch :pointer))
    134 
    135 (defcstruct cchar_t
    136   (attr chtype)
    137   (char :uint32 :count 5))
    13833
    13934
    14035
    141 (defcvar ("_nc_wacs" nc-wacs) :pointer :read-only t)
    14236
    143 (defun wacs-char (ch)
    144   (let ((code (char-code (ecase ch
    145                            (:ul-corner #\l)
    146                            (:ll-corner #\m)
    147                            (:ur-corner #\k)
    148                            (:lr-corner #\j)
    149                            (:r-tee #\u)
    150                            (:l-tee #\t)
    151                            (:b-tee #\v)
    152                            (:t-tee #\w)
    153                            (:h-line #\q)
    154                            (:v-line #\x)
    155                            (:plus #\n)
    156                            (:solid #\0)))))
    157     (inc-pointer nc-wacs (* (foreign-type-size 'cchar_t) code))))
    158 
    159 (defcfun "wadd_wchnstr" :int
    160   (window :pointer)
    161   (cchstr :pointer)
    162   (n :int))
    163 
    164 (defun clear-complex-char (cc)
    165   (with-foreign-slots ((attr char) cc cchar_t)
    166     (setf attr 0)
    167     (dotimes (i 5 cc)
    168       (setf (mem-aref char :uint32 i) 0))))
    169 
    170 (defun extract-complex-char (cc)
    171   (let ((x (make-sequence '(vector integer) 5 :initial-element 0)))
    172     (with-foreign-slots ((char) cc cchar_t)
    173       (dotimes (i 5 x)
    174         (setf (aref x i) (mem-aref char :uint32 i))))))
    175 
    176 (defun copy-complex-char (dst src-vector)
    177   (with-foreign-slots ((char) dst cchar_t)
    178     (dotimes (i 5 dst)
    179       (setf (mem-aref char :uint32 i) (aref src-vector i)))))
    180 
    181 (defun set-complex-char (cc ch)
    182   (with-foreign-slots ((char) cc cchar_t)
    183     (etypecase ch
    184       (character
    185        (setf (mem-aref char :uint32) (char-code ch)))
    186       (symbol
    187        (copy-complex-char cc (extract-complex-char (wacs-char ch)))))))
    188 
    189 (defun add-complex-string (window s)
    190   (let ((n (length s)))
    191     (with-foreign-object (a 'cchar_t n)
    192       (dotimes (i n)
    193         (let ((cc (mem-aref a 'cchar_t i)))
    194           (clear-complex-char cc)
    195           (set-complex-char cc (aref s i))))
    196       (wadd-wchnstr window a n))))
    197 
    198 (defcfun ("wmove" c-wmove) :int
    199   (window :pointer)
    200   (y :int)
    201   (x :int))
    202 
    203 (defcfun "waddnwstr" :int
    204   (window :pointer)
    205   (wstr :pointer)
    206   (n :int))
    207 
    208 (defun add-string (window s)
    209   (let ((n (length s)))
    210     (with-foreign-object (a 'wchar n)
    211       (dotimes (i n)
    212         (setf (mem-aref a 'wchar i) (char-code (aref s i))))
    213       (waddnwstr window a n))))
    214 
    215 (defun move-and-add-string (window y x s)
    216   (wmove window y x)
    217   (add-string window s))
    218 
    219 (defun add-clipped-string (window y x s)
    220   (let ((maxy (getmaxy window)))
    221     (unless (<= 0 y maxy) (return-from add-clipped-string)))
    222   (let ((maxx (getmaxx window)))
    223     (when (< x 0)
    224       (setf s (subseq s (- x))
    225             x 0))
    226     (let ((over (- (+ x (length s)) maxx)))
    227       (when (> over 0)
    228         (setf s (subseq s 0 (- (length s) over)))))
    229     (move-and-add-string window y x s)))
    230 
    231 (defcfun ("wrefresh" c-refresh-window) :int
    232   (window :pointer))
    23337
    23438(defun color-pair (n)
     
    23741
    23842
    239 ;;; Generic functions.
    24043
    241 (defcfun ("bindCDKObject" c-bind-key) :void
    242   (object-type :int)
    243   (object :pointer)
    244   (key chtype)
    245   (function :pointer)
    246   (user-data :pointer))
    247 
    248 (defcfun ("cleanCDKObjectBindings" c-clear-bindings) :void
    249   (object-type :int)
    250   (object :pointer))
    251 
    252 (defcfun ("setCDKObjectBackgroundColor" c-set-background-color) :void
    253   (object :pointer)
    254   (color :string))
    255 
    256 
    257 
    258 ;;; Labels.
    259 
    260 (defcfun ("newCDKLabel" c-new-label) :pointer
    261   (screen :pointer)
    262   (x-pos :int)
    263   (y-pos :int)
    264   (message :pointer)
    265   (message-lines :int)
    266   (box :boolean)
    267   (shadow :boolean))
    268 
    269 (defcfun ("freeCDKLabel" c-free-label) :void
    270   (label :pointer))
    271 
    272 (defcfun ("waitCDKLabel" c-wait-on-label) :char
    273   (label :pointer)
    274   (key :char))
    275 
    276 (defun new-label (screen row column text &optional box shadow)
    277   (let* ((cstr (foreign-string-alloc text))
    278          (array (foreign-alloc :pointer :initial-element cstr :count 1))
    279          (label (c-new-label screen column row array 1 box shadow)))
    280     (foreign-free array)
    281     (foreign-string-free cstr)
    282     label))
    283 
    284 
    285 
    286 ;;; Entries.
    287 
    288 (defcfun ("newCDKEntry" c-new-entry) :pointer
    289   (screen :pointer)
    290   (x-pos :int)
    291   (y-pos :int)
    292   (title :string)
    293   (label :string)
    294   (attribute :int)
    295   (filler :int)
    296   (display-type :int)
    297   (width :int)
    298   (minimum-length :int)
    299   (maximum-length :int)
    300   (box :boolean)
    301   (shadow :boolean))
    302 
    303 (defcfun ("freeCDKEntry" c-free-entry) :void
    304   (entry :pointer))
    305 
    306 (defcfun ("activateCDKEntry" c-activate-entry) :string
    307   (entry :pointer)
    308   (actions :string))
    309 
    310 (defcfun ("setCDKEntryHighlight" c-set-entry-highlight) :void
    311   (entry :pointer)
    312   (highlight :unsigned-int)             ; Still not sure about this.
    313   (cursor :boolean))
    314 
    315 (defcfun ("getCDKEntryExitType" c-entry-exit-type) :int
    316   (entry :pointer))
    317 
    318 (defcfun ("getCDKEntryValue" c-get-entry-value) :string
    319   (entry :pointer))
    320 
    321 (defcfun ("setCDKEntryValue" c-set-entry-value) :void
    322   (entry :pointer)
    323   (new-value :string))
    324 
    325 (defcfun ("setCDKEntryExitType" c-set-entry-exit-type) :void
    326   (entry :pointer)
    327   (ch chtype))
    328 
    329 
    330 
    331 (defcfun ("wattron" c-wattron) :int
    332   (window :pointer)
    333   (attrs :int))
    334 
    335 (defcfun ("wattroff" c-wattroff) :int
    336   (window :pointer)
    337   (attrs :int))
    33844
    33945(defcfun ("wgetch" c-wgetch) :int
     
    34955
    35056(defcfun ("nonl" c-nonl) :int)
     57
     58(defcfun ("wattron" c-wattron) :int
     59  (window :pointer)
     60  (attrs :int))
     61
     62(defcfun ("wattroff" c-wattroff) :int
     63  (window :pointer)
     64  (attrs :int))
     65
Note: See TracChangeset for help on using the changeset viewer.