x11/x11base.scm (key-event): Free buffers. Handle null translation.
authorMatt Birkholz <puck@birchwood-abbey.net>
Fri, 5 Aug 2016 23:30:53 +0000 (16:30 -0700)
committerMatt Birkholz <puck@birchwood-abbey.net>
Sun, 7 Aug 2016 18:08:05 +0000 (11:08 -0700)
Do not c-peek-bytes when nbytes is zero.

src/x11/x11base.scm

index 84c0739ca9d67b7eadb1e6dcdd84022147cef03a..ad657e4055a4ce37ac26a5104af01bb36f8419b9 100644 (file)
@@ -261,28 +261,36 @@ Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
                                                      (C-enum "LockMask"))))
       (let* ((nbytes (C-call "x_lookup_string"
                             event buffer buffer-size keysym-buffer))
-            (keysym (C-> keysym-buffer "KeySym")))
-       (and (not (= keysym (C-enum "NoSymbol")))
-            (not (= (C-enum "True") (C-call "IsModifierKey" keysym)))
-            (vector type
-                    window
-                    ;; If the BackSpace keysym is received, and
-                    ;; XLookupString has translated it into ASCII
-                    ;; backspace, substitute ASCII DEL instead.
-                    (if (and (= keysym (C-enum "XK_BackSpace"))
-                             (= nbytes 1)
-                             (= (C-> buffer "char") (char->ascii #\b)))
-                        (char->string #\Delete)
-                        (let ((string (make-string nbytes)))
-                          (c-peek-bytes buffer 0 nbytes string 0)
-                          string))
-                    ;; Create Scheme bucky bits (kept independent of
-                    ;; the character).  X has already controlified, so
-                    ;; Scheme may choose to ignore the control bucky
-                    ;; bit.
-                    (C-call "x_modifier_mask_to_bucky_bits" state window)
-                    keysym
-                    (C-> event "XKeyEvent time")))))))
+            (keysym (C-> keysym-buffer "KeySym"))
+            (event (and (not (= keysym (C-enum "NoSymbol")))
+                        (not (= (C-enum "True")
+                                (C-call "IsModifierKey" keysym)))
+                        (vector type
+                                window
+                                ;; If the BackSpace keysym is received, and
+                                ;; XLookupString has translated it into ASCII
+                                ;; backspace, substitute ASCII DEL instead.
+                                (cond ((and (= keysym (C-enum "XK_BackSpace"))
+                                            (= nbytes 1)
+                                            (= (C-> buffer "char")
+                                               (char->ascii #\b)))
+                                       (char->string #\Delete))
+                                      ((> nbytes 0)
+                                       (let ((s (make-string nbytes)))
+                                         (c-peek-bytes buffer 0 nbytes s 0)
+                                         s))
+                                      (else ""))
+                                ;; Create Scheme bucky bits (kept independent
+                                ;; of the character).  X has already
+                                ;; controlified, so Scheme may choose to
+                                ;; ignore the control bucky bit.
+                                (C-call "x_modifier_mask_to_bucky_bits"
+                                        state window)
+                                keysym
+                                (C-> event "XKeyEvent time")))))
+       (free keysym-buffer)
+       (free buffer)
+       event))))
 
 (define key-event-state-mask
   (+ (C-enum "ShiftMask")