1;;
2;;  Copyright (c) 2006-2013 uim Project https://github.com/uim/uim
3;;
4;;  All rights reserved.
5;;
6;;  Redistribution and use in source and binary forms, with or
7;;  without modification, are permitted provided that the
8;;  following conditions are met:
9;;
10;;  1. Redistributions of source code must retain the above
11;;     copyright notice, this list of conditions and the
12;;     following disclaimer.
13;;  2. Redistributions in binary form must reproduce the above
14;;     copyright notice, this list of conditions and the
15;;     following disclaimer in the documentation and/or other
16;;     materials provided with the distribution.
17;;  3. Neither the name of authors nor the names of its
18;;     contributors may be used to endorse or promote products
19;;     derived from this software without specific prior written
20;;     permission.
21;;
22;;  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
23;;  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
24;;  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25;;  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26;;  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27;;  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28;;  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29;;  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30;;  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31;;  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32;;  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33;;  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34;;  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35;;
36
37(defun uim-helper-send-message (helperstr)
38  (mapcar
39   '(lambda (x)
40      (process-send-string uim-el-helper-agent-process (concat x "\n"))
41      )
42   helperstr)
43  )
44
45
46(defun uim-helper-handler (msg)
47  (let* ((message (substring msg 0 (- (length msg) 1)))
48	 (cmd (format "%d HELPER %s" uim-context-id message)))
49    (cond ((or (eq (string-match "prop_activate" message) 0)
50	       (eq (string-match "im_change_whole_desktop" message) 0)
51	       (eq (string-match "im_change_this_text_area_only" message) 0))
52	   (uim-do-send-recv-cmd cmd)
53	   ;; update all buffer
54	   (save-current-buffer
55	     (mapcar
56	      '(lambda (x)
57		 (set-buffer x)
58		 (if (and (boundp 'uim-mode) uim-mode)
59		     (uim-do-send-recv-cmd (format "%d NOP" uim-context-id)))
60		 )
61	      (buffer-list)))
62	   )
63	  (t
64	   (uim-do-send-recv-cmd cmd))))
65  )
66
67
68(defun uim-helper-message-processor ()
69    (let (eom msg)
70      (while (setq eom (string-match "\n" uim-helper-message))
71	(setq msg (substring uim-helper-message 0 (+ eom 1)))
72	(setq uim-helper-message (substring uim-helper-message (+ eom 1)))
73	(if (> (length msg) 0)
74	    (uim-helper-handler msg))
75	)
76      )
77    )
78
79(defun uim-helper-filter (process output)
80  (let ((inhibit-quit t))
81    (setq uim-helper-message (concat uim-helper-message output))
82    (uim-helper-message-processor)
83    )
84  )
85
86
87(defun uim-helper-process-sentinel (proc stat)
88  (message "uim-helper.el: %s" stat)
89  (setq uim-el-helper-agent-process nil)
90  )
91
92
93
94(defun uim-el-helper-agent-start ()
95  (let (proc
96	(buffer (get-buffer-create uim-el-helper-agent-buffer-name)) )
97
98    (save-current-buffer
99      (set-buffer buffer) (erase-buffer))
100
101    (message "uim.el: starting uim-el-helper-agent...")
102
103    (setq proc (start-process "uim-el-helper-agent"
104			      buffer uim-el-helper-agent))
105
106    (if (not proc)
107	(error "uim.el: Couldn't invoke uim-el-helper-agent."))
108
109    (set-process-query-on-exit-flag proc nil)
110
111    ;; wait "OK"
112    (let ((patience uim-startup-timeout) (ok nil))
113      (save-current-buffer
114	(set-buffer buffer)
115	(while (not ok)
116	  (accept-process-output proc 0 uim-el-agent-accept-timeout)
117	  (goto-char 1)
118	  (end-of-line)
119	  (if (string= "OK" (buffer-substring 1 (point)))
120	      (setq ok t)
121	    (setq patience (- patience (/ uim-el-agent-accept-timeout 1000)))
122	    (if (<= patience 0)
123		(progn
124		  (kill-process (process-name proc))
125		  (error "uim.el: uim-el-helper-agent is unreponsive.  Giving up.")))))
126	(erase-buffer)))
127
128
129    (set-process-filter proc 'uim-helper-filter)
130
131    (setq uim-el-helper-agent-buffer buffer)
132
133    (set-process-sentinel proc 'uim-helper-process-sentinel)
134
135    (message "uim.el: starting uim-el-helper-agent... done")
136
137    proc
138    ))
139
140
141(provide 'uim-helper)
142