1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include "renderer/unix/unix_server.h"
31
32 #include <fcntl.h>
33 #include <unistd.h>
34
35 #include <memory>
36
37 #include "base/logging.h"
38 #include "protocol/renderer_command.pb.h"
39 #include "renderer/unix/window_manager.h"
40
41 using std::unique_ptr;
42
43 namespace mozc {
44 namespace renderer {
45 namespace gtk {
46 namespace {
47
mozc_prepare(GSource * source,int * timeout)48 gboolean mozc_prepare(GSource *source, int *timeout) {
49 *timeout = -1;
50 return FALSE;
51 }
52
mozc_check(GSource * source)53 gboolean mozc_check(GSource *source) {
54 UnixServer::MozcWatchSource *watch
55 = reinterpret_cast<UnixServer::MozcWatchSource*>(source);
56 return watch->poll_fd.revents != 0;
57 }
58
mozc_dispatch(GSource * source,GSourceFunc callback,gpointer user_data)59 gboolean mozc_dispatch(GSource *source, GSourceFunc callback,
60 gpointer user_data) {
61 UnixServer::MozcWatchSource *watch
62 = reinterpret_cast<UnixServer::MozcWatchSource*>(source);
63 char buf[8];
64 // Discards read data.
65 while (read(watch->poll_fd.fd, buf, 8) > 0) {}
66 watch->unix_server->Render();
67 return TRUE;
68 }
69 } // namespace
70
UnixServer(GtkWrapperInterface * gtk)71 UnixServer::UnixServer(GtkWrapperInterface *gtk)
72 : gtk_(gtk) {
73 }
74
~UnixServer()75 UnixServer::~UnixServer() {
76 }
77
AsyncHide()78 void UnixServer::AsyncHide() {
79 // obsolete callback
80 }
81
AsyncQuit()82 void UnixServer::AsyncQuit() {
83 gtk_->GtkMainQuit();
84 }
85
Render()86 bool UnixServer::Render() {
87 string message;
88 {
89 scoped_lock l(&mutex_);
90 message.assign(message_);
91 }
92
93 commands::RendererCommand command;
94 if (command.ParseFromString(message)) {
95 ExecCommandInternal(command);
96 } else {
97 LOG(WARNING) << "Parse From String Failed";
98 }
99 return true;
100 }
101
AsyncExecCommand(string * proto_message)102 bool UnixServer::AsyncExecCommand(string *proto_message) {
103 {
104 // Take the ownership of |proto_message|.
105 unique_ptr<string> proto_message_owner(proto_message);
106 scoped_lock l(&mutex_);
107 if (message_ == *proto_message_owner.get()) {
108 // This is exactly the same to the previous message. Theoretically it is
109 // safe to do nothing here.
110 return true;
111 }
112 // Since mozc rendering protocol is state-less, we can always ignore the
113 // previous content of |message_|.
114 message_.swap(*proto_message_owner.get());
115 }
116
117 const char dummy_data = 0;
118 if (write(pipefd_[1], &dummy_data, sizeof(dummy_data)) < 0) {
119 LOG(ERROR) << "Pipe write failed";
120 }
121 return true;
122 }
123
StartMessageLoop()124 int UnixServer::StartMessageLoop() {
125 GSourceFuncs src_funcs = {
126 mozc_prepare,
127 mozc_check,
128 mozc_dispatch,
129 NULL
130 };
131
132 MozcWatchSource *watch = reinterpret_cast<MozcWatchSource*>(
133 gtk_->GSourceNew(&src_funcs, sizeof(MozcWatchSource)));
134 gtk_->GSourceSetCanRecurse(&watch->source, TRUE);
135 gtk_->GSourceAttach(&watch->source, NULL);
136 gtk_->GSourceSetCallback(&watch->source,
137 NULL,
138 (gpointer)this,
139 NULL);
140
141 watch->poll_fd.fd = pipefd_[0];
142 watch->poll_fd.events = G_IO_IN | G_IO_HUP;
143 watch->poll_fd.revents = 0;
144 watch->unix_server = this;
145
146 gtk_->GSourceAddPoll(reinterpret_cast<GSource*>(watch), &watch->poll_fd);
147
148 gtk_->GdkThreadsEnter();
149 gtk_->GtkMain();
150 gtk_->GdkThreadsLeave();
151
152 return 0;
153 }
154
OpenPipe()155 void UnixServer::OpenPipe() {
156 if (pipe(pipefd_) == 1) {
157 LOG(FATAL) << "popen failed";
158 return;
159 }
160
161 // TODO(nona): Close unused fd.
162 fcntl(pipefd_[0], F_SETFL, O_NONBLOCK);
163 fcntl(pipefd_[1], F_SETFL, O_NONBLOCK);
164 }
165
166 } // namespace gtk
167 } // namespace renderer
168 } // namespace mozc
169