diff --git a/common/bitwise.ml b/common/bitwise.ml new file mode 100644 index 0000000..5fe02be --- /dev/null +++ b/common/bitwise.ml @@ -0,0 +1,40 @@ +open Ctypes + +module type Size = sig + type t + val zero : t + val logor : t -> t -> t + val logand : t -> t -> t +end + +module type Elems = sig + type t + module Size : Size + val size : Size.t typ + val desc : (t * Size.t) list +end + +module type Enum = sig + type elem + type t + val t : t +end + +module Make (E : Elems) : Enum = struct + type elem = E.t + type t = E.t list typ + let t : t = + let read i = + List.filter_map (fun (x, cst) -> + if (E.Size.logand i cst) <> E.Size.zero then + Some x + else None + ) E.desc + in + let write items = + List.fold_left (fun i item -> + E.Size.logor (List.assoc item E.desc) i + ) E.Size.zero items + in + view ~read ~write E.size +end diff --git a/common/dune b/common/dune index e500ad6..1e9637c 100644 --- a/common/dune +++ b/common/dune @@ -1,6 +1,7 @@ (library (name wlroots_common) (public_name wlroots.common) + (modules utils bitwise sigs) (libraries ctypes)) ;; xdg-shell-protocol.h