1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
5 ** $Id: ipc.cpp,v 1.6 2005/01/16 19:55:57 darco Exp $
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
38 #ifdef HAVE_SYS_STAT_H
42 #include <synfig/main.h>
60 #include <glibmm/dispatcher.h>
61 #include <synfig/mutex.h>
62 #include <synfig/string.h>
63 #include <glibmm/thread.h>
67 /* === U S I N G =========================================================== */
71 using namespace synfig;
72 using namespace studio;
74 /* === M A C R O S ========================================================= */
76 /* === G L O B A L S ======================================================= */
79 #define WIN32_PIPE_PATH "\\\\.\\pipe\\SynfigStudio.Cmd"
80 static synfig::Mutex cmd_mutex;
81 static std::list<synfig::String> cmd_queue;
82 static Glib::Dispatcher* cmd_dispatcher;
89 pipe_handle=CreateNamedPipe(
90 WIN32_PIPE_PATH, // pipe name
91 PIPE_ACCESS_INBOUND, // Access type
92 PIPE_READMODE_BYTE /*|PIPE_NOWAIT*/,
93 PIPE_UNLIMITED_INSTANCES,
96 NMPWAIT_USE_DEFAULT_WAIT,
99 if(pipe_handle==INVALID_HANDLE_VALUE)
101 synfig::error("IPC(): Call to CreateNamedPipe failed. Ignore next error. GetLastError=%d",GetLastError());
106 connected=ConnectNamedPipe(pipe_handle,NULL)?true:(GetLastError()==ERROR_PIPE_CONNECTED);
110 Glib::Thread::yield();
120 &c, // buffer pointer
125 if(success && read_bytes==1 && c!='\n')
128 synfig::Mutex::Lock lock(cmd_mutex);
129 cmd_queue.push_back(data);
130 cmd_dispatcher->emit();
131 } while(success && read_bytes);
133 CloseHandle(pipe_handle);
140 synfig::Mutex::Lock lock(cmd_mutex);
141 while(!cmd_queue.empty())
143 IPC::process_command(cmd_queue.front());
144 cmd_queue.pop_front();
150 /* === P R O C E D U R E S ================================================= */
152 /* === M E T H O D S ======================================================= */
158 cmd_dispatcher=new Glib::Dispatcher;
159 cmd_dispatcher->connect(sigc::ptr_fun(empty_cmd_queue));
161 Glib::Thread::create(
162 sigc::ptr_fun(pipe_listen_thread),
168 remove(fifo_path().c_str());
171 if(mkfifo(fifo_path().c_str(), S_IRWXU)!=0)
173 synfig::error("IPC(): mkfifo failed for "+fifo_path());
177 fd=open(fifo_path().c_str(),O_RDWR);
182 synfig::error("IPC(): Failed to open fifo \"%s\". (errno=%d)",fifo_path().c_str(),errno);
186 file=SmartFILE(fdopen(fd,"r"));
188 Glib::signal_io().connect(
189 sigc::mem_fun(this,&IPC::fifo_activity),
191 Glib::IO_IN|Glib::IO_PRI|Glib::IO_ERR|Glib::IO_HUP|Glib::IO_NVAL
201 // fclose(file.get());
203 remove(fifo_path().c_str());
213 return WIN32_PIPE_PATH;
215 return Glib::build_filename(App::get_user_app_directory(),"fifo");
220 IPC::fifo_activity(Glib::IOCondition cond)
222 synfig::info(__FILE__":%d: fifo activity",__LINE__);
224 if(cond&(Glib::IO_ERR|Glib::IO_HUP|Glib::IO_NVAL))
226 if(cond&(Glib::IO_ERR))
227 synfig::error("IPC::fifo_activity(): IO_ERR");
228 if(cond&(Glib::IO_HUP))
229 synfig::error("IPC::fifo_activity(): IO_HUP");
230 if(cond&(Glib::IO_NVAL))
231 synfig::error("IPC::fifo_activity(): IO_NVAL");
234 synfig::info(__FILE__":%d: fifo activity",__LINE__);
240 if(read(fd,&tmp,sizeof(tmp))<=0)
247 process_command(command);
252 IPC::process_command(const synfig::String& command_line)
254 if(command_line.empty())
257 char cmd=command_line[0];
259 String args(command_line.begin()+1,command_line.end());
260 while(!args.empty() && args[0]==' ') args.erase(args.begin());
261 while(!args.empty() && args[args.size()-1]=='\n' || args[args.size()-1]==' ') args.erase(args.end()-1);
265 case 'F': // Focus/Foreground
266 App::signal_present_all()();
268 case 'N': // New file
269 App::signal_present_all()();
272 case 'O': // Open <arg>
273 App::signal_present_all()();
281 synfig::warning("Received unknown command '%c' with arg '%s'",cmd,args.c_str());
289 IPC::make_connection()
294 pipe_handle=CreateFile(
296 GENERIC_WRITE, // desired access
298 NULL, // security attributes
299 OPEN_EXISTING, // creation disposition
300 FILE_ATTRIBUTE_NORMAL, // flags and attributes
301 NULL // template file
303 if(pipe_handle==INVALID_HANDLE_VALUE)
305 synfig::warning("IPC::make_connection(): Unable to connect to previous instance. GetLastError=%d",GetLastError());
307 int fd=_open_osfhandle(reinterpret_cast<long int>(pipe_handle),_O_APPEND|O_WRONLY);
309 struct stat file_stat;
310 if(stat(fifo_path().c_str(),&file_stat)!=0)
313 if(!S_ISFIFO(file_stat.st_mode))
316 int fd=open(fifo_path().c_str(),O_WRONLY|O_NONBLOCK);
320 ret=SmartFILE(fdopen(fd,"w"));
322 synfig::info("uplink fd=%d",fd);