Source file wkey.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290

open Tsdl

type handler =
  { cond : (unit -> bool) ;
    cback : (unit -> unit) ;
  }

module H =
  struct
    type t = Key.keystate * handler

    let filter_with_mask (t:Key.keystate) l =
      List.filter
        (fun a -> not (Key.keystate_equal (fst a) t)) l

    let find_handlers ~kmod ~key l =
      List.map snd
        (List.filter
         (fun (t,_) -> Key.match_keys t ~key ~kmod)
           l
        )

  end

let (table : (Oid.t, H.t list ref) Hashtbl.t) = Hashtbl.create 13

let key_press (w:Widget.widget) (ev:Widget.key_ev) =
  let key = ev.key in
  let modifiers = ev.mods in
  try
    let (r : H.t list ref) = Hashtbl.find table w#id in
    let l = H.find_handlers ~kmod:modifiers ~key !r in
    match l with
    | [] -> false
    | _ ->
        List.iter
          (fun h ->
             if h.cond () then
               try h.cback ()
               with e ->
                 Log.err (fun m -> m "%s" (Printexc.to_string e))
             else ()
          )
          l;
        true
  with
    Not_found -> false

let associate_key_press (w:Widget.widget) =
  let _ = w#connect Widget.Key_pressed (key_press w) in
  ()

let remove_widget (w : Widget.widget) () =
  try
    let r = Hashtbl.find table w#id in
    r := []
  with
    Not_found ->
      ()

let add1 ?(remove=false) (w:Widget.widget)
  ?(cond=(fun () -> true)) ks callback =
  Log.debug (fun m -> m "Wkey.add1 w=%s ks=%a" w#me Key.pp_keystate ks);
  let r =
    try Hashtbl.find table w#id
    with Not_found ->
        let r = ref [] in
        Hashtbl.add table w#id r;
        let _ = w#connect Widget.Destroy
          (fun () -> remove_widget w (); false)
        in
        associate_key_press w;
        r
  in
  let new_h = { cond = cond ; cback = callback } in
  if remove then
    (
     let l = H.filter_with_mask ks !r in
     r := (ks, new_h) :: l
    )
  else
    r := (ks, new_h) :: !r


let add w ?(cond=(fun () -> true)) ks callback =
    add1 w ~cond ks callback

let add_list w ?(cond=(fun () -> true)) ks_list callback =
    List.iter (fun ks -> add w ~cond ks callback) ks_list

let set w ?(cond=(fun () -> true)) ks callback =
    add1 ~remove: true w ~cond ks callback

let set_list w ?(cond=(fun () -> true)) ks_list callback =
    List.iter (fun ks -> set w ~cond ks callback) ks_list

(** {2 Trees of handlers, a la emacs} *)

let ignored_keys =
  let open Sdl.K in
  ref [
    numlockclear ;
    scrolllock ;
    pause ;
    capslock ;
  ]

type handler_tree_node =
  | Handler of handler
  | Node of handler_tree list
and handler_tree =
  { mutable hst_spec : Key.keystate ;
    mutable hst_v : handler_tree_node ;
  }

let string_of_handler_trees =
  let rec iter b margin t =
    match t.hst_v with
    | Handler _ ->
        Printf.bprintf b "%s%s: <handler>\n"
          margin (Key.string_of_keystate t.hst_spec)
  | Node l ->
        Printf.bprintf b "%s%s:\n"
          margin (Key.string_of_keystate t.hst_spec);
        List.iter (iter b (margin^"  ")) l
  in
  fun l ->
    let b = Buffer.create 256 in
    List.iter (iter b "") l;
    Buffer.contents b

let pp_handler_trees ppf l =
  Format.fprintf ppf "%s"
    (string_of_handler_trees l)

type keyhit_state = (Sdl.keymod * Sdl.keycode) list

  (** associations between a widget object id and its associated key press callback
     and its "key hit state" *)
let (states_table : (Oid.t, Events.callback_id * (Sdl.keymod * Sdl.keycode) list) Hashtbl.t) =
  Hashtbl.create 37

let reset_state w =
  let oid = w#id in
  try
    let (evid,state) = Hashtbl.find states_table oid in
    Hashtbl.replace states_table oid (evid, [])
  with Not_found ->
      ()

let rec trees_of_state trees state =
  match state with
  | [] -> trees
  | (mods,k) :: q ->
      let hst = List.find
        (fun t -> Key.match_keys t.hst_spec ~key:k ~kmod:mods) trees
      in
      match hst.hst_v with
      | Handler _ -> raise Not_found
      | Node l -> trees_of_state l q

let on_key_press ?display_state ks_stop f_trees
  (w:Widget.widget) ev =
    try
      let oid = w#id in
      let key = ev.Widget.key in
      let mods = ev.mods in
      Log.debug (fun m ->
         let ks = Key.keystate ~mods key in
         m "%s: key pressed: %a" w#me Key.pp_keystate ks);
      let (evid,state) = Hashtbl.find states_table oid in
      if List.mem key !ignored_keys then raise Not_found;
      if Key.key_is_mod key then
        true
      else
        (
         (*    prerr_endline (Printf.sprintf "key=%X" key);*)
         let disp_state after_handler st =
           match display_state with
           | None -> ()
           | Some f -> f ~after_handler st
         in
         let set_state ?(after_handler=false) st =
           Hashtbl.replace states_table oid (evid, st);
           disp_state after_handler st
         in
         if Key.match_keys ks_stop ~key ~kmod:mods then
           (
            set_state [] ;
            true
           )
         else
           let trees =
             try trees_of_state (f_trees ()) state
             with Not_found ->
                 set_state [] ;
                 raise Not_found
           in
           match List.find_opt
             (fun t -> Key.match_keys t.hst_spec ~key ~kmod:mods) trees
           with
           | None -> set_state [] ; false
           | Some hst ->
               match hst.hst_v with
               | Node _ ->
                   (* only keep mods and key in state, so that masked
                      modifiers are not displayed *)
                   let mods = hst.hst_spec.mods in
                   let key = hst.hst_spec.key in
                   let new_state = state @ [mods,key] in
                   set_state new_state ;
                   true
               | Handler h ->
                   let () =
                     if h.cond () then
                       try h.cback ()
                       with e ->
                           Log.err (fun m -> m "%s" (Printexc.to_string e))
                     else ()
                   in
                   set_state ~after_handler: true [] ;
                   true
        )
  with Not_found ->
      false

let set_handler_trees ?(stop=Key.keystate ~mods:Sdl.Kmod.ctrl Sdl.K.g) f_trees ?display_state
  (w : Widget.widget) =
  let add () =
    let id = w#connect Widget.Key_pressed
      (on_key_press ?display_state stop f_trees w)
    in
    Hashtbl.add states_table w#id (id, []);
    ()
  in
  try
    let (id, _) = Hashtbl.find states_table w#id in
    w#disconnect id ;
    add ()
  with
  | Not_found -> add ()

let handler ?(cond=(fun () -> true)) callback =
  { cond = cond ; cback = callback }

let handler_tree ks v =
  { hst_spec = ks ;
    hst_v = v ;
  }

let rec trees_of_list l =
  match l with
  | [] -> []
  | ([],_) :: q -> trees_of_list q
  | ([ks],f) :: q ->
      (handler_tree ks (Handler (handler f))) ::
        (trees_of_list q)
  | ((ks::b),_) :: q ->
      let pred = function
        ([],_) -> false
      | ((ks2::_),_) -> Key.keystate_equal ks ks2
      in
      let (same,diff) = List.partition pred l in
      let subs = List.map
        (function
             ((_::q),f) -> (q,f)
         | _ -> assert false
        )
          same
      in
      (handler_tree ks (Node (trees_of_list subs))) ::
        (trees_of_list diff)

let string_of_keyhit_state state =
  String.concat ", "
    (List.map
     (fun (kmod,key) ->
        Printf.sprintf "%s%s"
          (if kmod = Sdl.Kmod.none
           then ""
           else (Key.string_of_mods kmod)^"-")
          (Sdl.get_key_name key))
       state)

let pp_keyhit_state fmt s = Format.fprintf fmt "%s"
  (string_of_keyhit_state s)