# Asynchronous xlib/xcb library

**URL:** https://discuss.ocaml.org/t/asynchronous-xlib-xcb-library/4587
**Category:** Community
**Created:** [October 26, 2019, 5:41am UTC](https://discuss.ocaml.org/t/asynchronous-xlib-xcb-library/4587 "2019-10-26T05:41:32Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Chimrod](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chimrod/32/6042_2.png) [@Chimrod](https://discuss.ocaml.org/u/Chimrod)
#### Post date: [October 26, 2019, 5:41am UTC](https://discuss.ocaml.org/t/asynchronous-xlib-xcb-library/4587/1 "2019-10-26T05:41:33Z")

</div>

Hello,

I’m looking for a low-level X library with lwt support. but I did not found anything on opam, even the OCaml-Xlib is not referenced.

Is there any code I can use ?

---

<div class="post-metadata">

### Author: ![Chimrod](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chimrod/32/6042_2.png) [@Chimrod](https://discuss.ocaml.org/u/Chimrod)
#### Post date: [October 27, 2019, 7:50pm UTC](https://discuss.ocaml.org/t/asynchronous-xlib-xcb-library/4587/2 "2019-10-27T19:50:01Z")

</div>

After some exploration in the xcb and ctypes documentation, I’ve managed a working proof of concept and to to recreate the xcb `hello world` :

xcb publish the underlying [file descriptor](https://xcb.freedesktop.org/PublicApi/#index8h2), which allow to use Lwt for waiting events.

An example is given below :

```
open Xcb

let () = begin
  match Xcb.connect () with
  | None -> ()
  | Some connexion ->
    Xcb.get_setup connexion
    |> Xcb.setup_roots_iterator
    |> fun iter -> (

      let screen = Xcb.getf iter Xcb.data in
      let width = ((screen |.-> Xcb.width_in_pixel) / 2)
      and height = ((screen |.-> Xcb.height_in_pixel) / 2)
      and depth = ((screen |.-> Xcb.root_depth)) in

      Printf.printf "Creating window (%d, %d) %d\n%!" width height depth;
      match Xcb.create_window
        connexion
        ~depth:(screen |.-> Xcb.root_depth)
        ~parent:(screen |.-> Xcb.root)
        ~x:10
        ~y:10
        ~width
        ~height
        ~border:0
        1
        ~visual:(screen |.-> Xcb.root_visual)
        [
            Back_pixel (screen |.-> Xcb.white_pixel);
            Event_mask ([Exposure; Key_press]);
        ]
      with
      | None -> print_endline "Error"
      | Some id ->
      let _ = Xcb.map_window connexion id in
      let _ = Xcb.flush connexion in
      print_endline "w created";

      let rec p () = begin
        match%lwt Xcb.poll_event connexion with
        | Key_press -> print_endline "Key_press"; Lwt.return_unit
        | _ -> print_endline "got event"; p()

      end in
      Lwt_main.run (p ())

    )
end

```

[xcb.ml](https://framabin.org/p/?15bbe8382c87d5bb#viINWF/LW4TP7dDZ92+ZQmR29Oer41nGRjQYb6faiBc=)

I need to review the code and provide something more _high level_, but this is a first step 🙂

---

<div class="post-metadata">

### Author: ![steinuil](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/steinuil/32/993_2.png) [@steinuil](https://discuss.ocaml.org/u/steinuil)
#### Post date: [October 28, 2019, 4:31pm UTC](https://discuss.ocaml.org/t/asynchronous-xlib-xcb-library/4587/3 "2019-10-28T16:31:18Z")

</div>

I think there’s only some old unmaintained bindings to the C libraries which definitely don’t use Lwt.

Anyway, I’m writing [a bindings generator](https://github.com/steinuil/xobl) that will output bindings with Lwt support… once it’s done. Haven’t done much progress lately. Though I’d be happy to return to it were somebody willing to help.
