Ignore:
Timestamp:
09/19/2007 11:06:52 PM (19 years ago)
Author:
David Owen <dsowen@…>
branch-nick:
tui
revision id:
dsowen@fugue88.ws-20070919230652-gcf3h1qwf86lq8j7
Message:

Using my own menu library to ease memory management and pointer logic.

Location:
tag/last-working
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • tag/last-working/Makefile

    r1 r2  
    1 menu2.so: menu2
     1menu2.so: menu2.c
     2        rm -f menu2.so
     3        gcc -shared -fPIC -o menu2.so menu2.c -lmenu -Wall -pedantic
  • tag/last-working/curses.lisp

    r1 r2  
    2222
    2323
    24 (define-foreign-library menu
    25   (t (:default "libmenu")))
     24(define-foreign-library menu2
     25  (t "/home/dsowen/lisp/tui/menu2.so"))
    2626
    27 (use-foreign-library menu)
     27(use-foreign-library menu2)
    2828
    29 (defctype item* :pointer)
    30 (defctype menu* :pointer)
    31 
    32 (defcfun new-item item* (name :string) (description :string))
    33 (defcfun free-item :int (item item*))
    34 (defcfun new-menu menu* (items :pointer))
    35 (defcfun post-menu :int (menu menu*))
    36 (defcfun menu-driver :int (menu menu*) (c :int))
     29(defcfun menu2-create :int)
     30(defcfun menu2-add-item :int (menu :int) (name :string) (description :string))
     31(defcfun menu2-destroy :int (menu :int))
     32(defcfun menu2-post :int (menu :int))
     33(defcfun menu2-drive :int (menu :int) (c :int))
     34(defcfun menu2-unpost :int (menu :int))
    3735
    3836
    39 
    40 (defun make-menu (items)
    41   (let* ((items-vector
    42           (map 'vector
    43                (lambda (item)
    44                  (etypecase item
    45                    (string (new-item (foreign-string-alloc item) (foreign-string-alloc item)))
    46                    (cons (new-item (foreign-string-alloc (car item)) (foreign-string-alloc (cdr item))))))
    47                items))
    48          (foreign-items (foreign-alloc 'item*
    49                                        :count (length items-vector)
    50                                        :initial-contents items-vector
    51                                        :null-terminated-p t)))
    52     (new-menu foreign-items)))
    5337
    5438(defun init-screen ()
  • tag/last-working/menu2.c

    r1 r2  
    1212        char *item_names[_MAX_ITEMS];
    1313        char *item_descriptions[_MAX_ITEMS];
     14        ITEM *item_structs[_MAX_ITEMS + 1];
    1415        int item_count;
     16
     17        MENU *menu_struct; /* Only set when posted. */
    1518};
    1619
     
    4346{
    4447        struct menu2_t *m = _menus[menu - 1];
     48        int *i;
     49
    4550        if(!m) return 0;
    4651
    47         if(m->item_count == _MAX_ITEMS) return 0;
     52        i = &(m->item_count);
     53        if(*i == _MAX_ITEMS) return 0;
    4854
    49         m->item_names[m->item_count] = strdup(name);
    50         m->item_descriptions[m->item_count] =
    51                 strdup(description ? description : name);
     55        m->item_names[*i] = strdup(name);
     56        m->item_descriptions[*i] = strdup(description ? description : name);
     57        m->item_structs[*i] =
     58                new_item(m->item_names[*i], m->item_descriptions[*i]);
    5259       
    53         ++m->item_count;
     60        ++(*i);
    5461
    5562        return 1;
     
    6471
    6572        for(i = 0; i < m->item_count; i++) {
     73                free_item(m->item_structs[i]);
     74                free(m->item_descriptions[i]);
    6675                free(m->item_names[i]);
    67                 free(m->item_descriptions[i]);
    6876        }
    6977
     
    7987
    8088        if(!m) return 0;
     89        if(m->menu_struct) return 0;
    8190
    82         blah
     91        m->menu_struct = new_menu(m->item_structs);
     92        post_menu(m->menu_struct);
     93
     94        return 1;
    8395}
    8496
    85 menu2_drive(int, int);
     97int menu2_drive(menu2_h menu, int c)
     98{
     99        struct menu2_t *m = _menus[menu - 1];
     100
     101        if(!m) return 0;
     102        if(!m->menu_struct) return 0;
     103
     104        menu_driver(m->menu_struct, c);
     105
     106        return 1;
     107}
     108
     109int menu2_unpost(menu2_h menu)
     110{
     111        struct menu2_t *m = _menus[menu - 1];
     112
     113        if(!m) return 0;
     114        if(!m->menu_struct) return 0;
     115
     116        unpost_menu(m->menu_struct);
     117        free_menu(m->menu_struct);
     118        m->menu_struct = NULL;
     119
     120        return 1;
     121}
Note: See TracChangeset for help on using the changeset viewer.