Skip to content

Commit

Permalink
Merge branch 'channel'
Browse files Browse the repository at this point in the history
  • Loading branch information
telent committed Mar 18, 2013
2 parents fb43ad2 + c21d574 commit e7b74f9
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 27 deletions.
58 changes: 58 additions & 0 deletions src/psadan/channel.clj
@@ -0,0 +1,58 @@
(ns psadan.channel
(:use clojure.pprint)
(:require
[psadan.connection :as conn]
[psadan.protocol :as proto]
[psadan.buffer :as buf]))

(defmulti handle-message
(fn [conn m]
[(:name (:interface m)) (:message m)]))

(defmethod handle-message [:wl_registry :global] [conn m]
(println ["fake register global" (:args m)]))

(defmethod handle-message [:wl_callback :done] [conn m]
(let [object (conn/get-object conn (:object-id m))
promise (:promise object)]
(when promise
(deliver promise m))))

(defmethod handle-message :default [conn m]
(println ["unknown message" (:name (:interface m)) (:message m)]))

(defn listen [conn]
(let [buf (conn/read-buffer conn)
messages (buf/parse-messages buf conn :events)]
(printf "got a buffer, %d messages" (count messages))
(dorun (map #(handle-message conn %) messages)))
(send-off *agent* listen)
conn)

(defn open-channel [name]
(let [agent (agent (conn/open-connection name))]
(send-off agent listen)
agent))


(defn get-registry [channel]
(let [connection @channel
registry
(conn/remember-object
connection
{:id 2 :interface (proto/find-interface-by-name :wl_registry)})
promise (promise)
done-cb
(conn/remember-object
connection
{:id 3
:promise promise
:interface (proto/find-interface-by-name :wl_callback)})
]
(conn/write-buffer connection
(pack/pack-message (:display connection)
:requests :get_registry [registry]))
(conn/write-buffer connection
(pack/pack-message (:display connection)
:requests :sync [done-cb]))
@promise))
41 changes: 14 additions & 27 deletions src/psadan/core.clj
Expand Up @@ -3,60 +3,47 @@
(:require
[psadan.connection :as conn]
[psadan.protocol :as proto]
[psadan.pack :as pack]
[psadan.channel :as chan]
[psadan.buffer :as buf]))

;;; here endeth the code that will one day be library code. Below here
;;; it's all client code and/or mucking around


(def connection (conn/open-connection "/home/dan/private/wayland-0"))
(def channel (chan/open-channel "/home/dan/private/wayland-0"))

(defn fake-weston-info-client-send []
(. (:output connection)
(. (:output @channel)
;; this byte string is the initial client greeting performed by
;; weston-info, as made visible by strace
(write (.getBytes "\1\0\0\0\1\0\f\0\2\0\0\0\1\0\0\0\0\0\f\0\3\0\0\0"))))


(defn test-parse-messages []
(let [m (buf/parse-messages (vec (.getBytes "\1\0\0\0\1\0\f\0\2\0\0\0\1\0\0\0\0\0\f\0\3\0\0\0")) connection :requests)]
(let [connection @channel
m (buf/parse-messages (vec (.getBytes "\1\0\0\0\1\0\f\0\2\0\0\0\1\0\0\0\0\0\f\0\3\0\0\0")) connection :requests)]
(assert (= (map :message m) '(:get_registry :sync)))
(map :message m)))

(defn test-pack-message []
(let [callback (conn/remember-object
(let [connection @channel
callback (conn/remember-object
connection
{:id 2
{:id 2
:interface (proto/find-interface-by-name :wl_callback)})
callback1 (conn/remember-object
connection
{:id 3
:interface (proto/find-interface-by-name :wl_callback)})
]
(assert (=
(buf/pack-message connection (:display connection)
:requests :get_registry [callback])
(pack/pack-message connection (:display connection)
:requests :get_registry [callback])
[1 0 0 0 1 0 12 0 2 0 0 0]))
(assert (=
(buf/pack-message connection (:display connection)
:requests :sync [callback1])
(pack/pack-message connection (:display connection)
:requests :sync [callback1])
[1 0 0 0 0 0 12 0 3 0 0 0]))))


(defn test-send-message []
(let [registry
(conn/remember-object
connection
{:id 2 :interface (proto/find-interface-by-name :wl_registry)})
done-cb
(conn/remember-object
connection
{:id 3 :interface (proto/find-interface-by-name :wl_callback)})
]
(conn/write-buffer connection
(buf/pack-message connection (:display connection)
:requests :get_registry [registry]))
(conn/write-buffer connection
(buf/pack-message connection (:display connection)
:requests :sync [done-cb]))
registry))

0 comments on commit e7b74f9

Please sign in to comment.