18.6 A complete example

This section outlines how the functions from the Unix curses library can be made available to Objective Caml programs. First of all, here is the interface curses.mli that declares the curses primitives and data types:

type window                   (* The type "window" remains abstract *)
external initscr: unit -> window = "curses_initscr"
external endwin: unit -> unit = "curses_endwin"
external refresh: unit -> unit = "curses_refresh"
external wrefresh : window -> unit = "curses_wrefresh"
external newwin: int -> int -> int -> int -> window = "curses_newwin"
external addch: char -> unit = "curses_addch"
external mvwaddch: window -> int -> int -> char -> unit = "curses_mvwaddch"
external addstr: string -> unit = "curses_addstr"
external mvwaddstr: window -> int -> int -> string -> unit = "curses_mvwaddstr"

(* lots more omitted *)

To compile this interface:

ocamlc -c curses.mli

To implement these functions, we just have to provide the stub code; the core functions are already implemented in the curses library. The stub code file, curses_stubs.c, looks like this:

#include <curses.h>
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/custom.h>

/* Encapsulation of opaque window handles (of type WINDOW *)
   as Caml custom blocks. */

static struct custom_operations curses_window_ops = {

/* Accessing the WINDOW * part of a Caml custom block */
#define Window_val(v) (*((WINDOW **) Data_custom_val(v)))

/* Allocating a Caml custom block to hold the given WINDOW * */
static value alloc_window(WINDOW * w)
  value v = alloc_custom(&curses_window_ops, sizeof(WINDOW *), 0, 1);
  Window_val(v) = w;
  return v;

value caml_curses_initscr(value unit)
  CAMLparam1 (unit);
  CAMLreturn (alloc_window(initscr()));

value caml_curses_endwin(value unit)
  CAMLparam1 (unit);
  CAMLreturn (Val_unit);

value caml_curses_refresh(value unit)
  CAMLparam1 (unit);
  CAMLreturn (Val_unit);

value caml_curses_wrefresh(value win)
  CAMLparam1 (win);
  CAMLreturn (Val_unit);

value caml_curses_newwin(value nlines, value ncols, value x0, value y0)
  CAMLparam4 (nlines, ncols, x0, y0);
  CAMLreturn (alloc_window(newwin(Int_val(nlines), Int_val(ncols),
                                  Int_val(x0), Int_val(y0))));

value caml_curses_addch(value c)
  CAMLparam1 (c);
  addch(Int_val(c));            /* Characters are encoded like integers */
  CAMLreturn (Val_unit);

value caml_curses_mvwaddch(value win, value x, value y, value c)
  CAMLparam4 (win, x, y, c);
  mvwaddch(Window_val(win), Int_val(x), Int_val(y), Int_val(c));
  CAMLreturn (Val_unit);

value caml_curses_addstr(value s)
  CAMLparam1 (s);
  CAMLreturn (Val_unit);

value caml_curses_mvwaddstr(value win, value x, value y, value s)
  CAMLparam4 (win, x, y, s);
  mvwaddstr(Window_val(win), Int_val(x), Int_val(y), String_val(s));
  CAMLreturn (Val_unit);

/* This goes on for pages. */

The file curses_stubs.c can be compiled with:

cc -c -I/usr/local/lib/ocaml curses.c

or, even simpler,

ocamlc -c curses.c

(When passed a .c file, the ocamlc command simply calls the C compiler on that file, with the right -I option.)

Now, here is a sample Caml program test.ml that uses the curses module:

open Curses
let main_window = initscr () in
let small_window = newwin 10 5 20 10 in
  mvwaddstr main_window 10 2 "Hello";
  mvwaddstr small_window 4 3 "world";
  Unix.sleep 5;

To compile and link this program, run:

ocamlc -custom -o test unix.cma test.ml curses_stubs.o -cclib -lcurses

(On some machines, you may need to put -cclib -ltermcap or -cclib -lcurses -cclib -ltermcap instead of -cclib -lcurses.)