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 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
33 #ifdef HAVE_SYS_TYPES_H
34 #include <sys/types.h>
37 #ifdef HAVE_SYS_STAT_H
41 #include <synfig/main.h>
59 #include <glibmm/dispatcher.h>
60 #include <synfig/mutex.h>
61 #include <synfig/string.h>
62 #include <glibmm/thread.h>
66 /* === U S I N G =========================================================== */
70 using namespace synfig;
71 using namespace studio;
73 /* === M A C R O S ========================================================= */
75 /* === G L O B A L S ======================================================= */
78 #define WIN32_PIPE_PATH "\\\\.\\pipe\\SynfigStudio.Cmd"
79 static synfig::Mutex cmd_mutex;
80 static std::list<synfig::String> cmd_queue;
81 static Glib::Dispatcher* cmd_dispatcher;
88 pipe_handle=CreateNamedPipe(
89 WIN32_PIPE_PATH, // pipe name
90 PIPE_ACCESS_INBOUND, // Access type
91 PIPE_READMODE_BYTE /*|PIPE_NOWAIT*/,
92 PIPE_UNLIMITED_INSTANCES,
95 NMPWAIT_USE_DEFAULT_WAIT,
98 if(pipe_handle==INVALID_HANDLE_VALUE)
100 synfig::error("IPC(): Call to CreateNamedPipe failed. Ignore next error. GetLastError=%d",GetLastError());
105 connected=ConnectNamedPipe(pipe_handle,NULL)?true:(GetLastError()==ERROR_PIPE_CONNECTED);
109 Glib::Thread::yield();
119 &c, // buffer pointer
124 if(success && read_bytes==1 && c!='\n')
127 synfig::Mutex::Lock lock(cmd_mutex);
128 cmd_queue.push_back(data);
129 cmd_dispatcher->emit();
130 } while(success && read_bytes);
132 CloseHandle(pipe_handle);
139 synfig::Mutex::Lock lock(cmd_mutex);
140 while(!cmd_queue.empty())
142 IPC::process_command(cmd_queue.front());
143 cmd_queue.pop_front();
149 /* === P R O C E D U R E S ================================================= */
151 /* === M E T H O D S ======================================================= */
157 cmd_dispatcher=new Glib::Dispatcher;
158 cmd_dispatcher->connect(sigc::ptr_fun(empty_cmd_queue));
160 Glib::Thread::create(
161 sigc::ptr_fun(pipe_listen_thread),
167 remove(fifo_path().c_str());
170 if(mkfifo(fifo_path().c_str(), S_IRWXU)!=0)
172 synfig::error("IPC(): mkfifo failed for "+fifo_path());
176 fd=open(fifo_path().c_str(),O_RDWR);
181 synfig::error("IPC(): Failed to open fifo \"%s\". (errno=%d)",fifo_path().c_str(),errno);
185 file=SmartFILE(fdopen(fd,"r"));
187 Glib::signal_io().connect(
188 sigc::mem_fun(this,&IPC::fifo_activity),
190 Glib::IO_IN|Glib::IO_PRI|Glib::IO_ERR|Glib::IO_HUP|Glib::IO_NVAL
200 // fclose(file.get());
202 remove(fifo_path().c_str());
212 return WIN32_PIPE_PATH;
214 return Glib::build_filename(App::get_user_app_directory(),"fifo");
219 IPC::fifo_activity(Glib::IOCondition cond)
221 synfig::info(__FILE__":%d: fifo activity",__LINE__);
223 if(cond&(Glib::IO_ERR|Glib::IO_HUP|Glib::IO_NVAL))
225 if(cond&(Glib::IO_ERR))
226 synfig::error("IPC::fifo_activity(): IO_ERR");
227 if(cond&(Glib::IO_HUP))
228 synfig::error("IPC::fifo_activity(): IO_HUP");
229 if(cond&(Glib::IO_NVAL))
230 synfig::error("IPC::fifo_activity(): IO_NVAL");
233 synfig::info(__FILE__":%d: fifo activity",__LINE__);
239 if(read(fd,&tmp,sizeof(tmp))<=0)
246 process_command(command);
251 IPC::process_command(const synfig::String& command_line)
253 if(command_line.empty())
256 char cmd=command_line[0];
258 String args(command_line.begin()+1,command_line.end());
259 while(!args.empty() && args[0]==' ') args.erase(args.begin());
260 while(!args.empty() && args[args.size()-1]=='\n' || args[args.size()-1]==' ') args.erase(args.end()-1);
264 case 'F': // Focus/Foreground
265 App::signal_present_all()();
267 case 'N': // New file
268 App::signal_present_all()();
271 case 'O': // Open <arg>
272 App::signal_present_all()();
280 synfig::warning("Received unknown command '%c' with arg '%s'",cmd,args.c_str());
288 IPC::make_connection()
293 pipe_handle=CreateFile(
295 GENERIC_WRITE, // desired access
297 NULL, // security attributes
298 OPEN_EXISTING, // creation disposition
299 FILE_ATTRIBUTE_NORMAL, // flags and attributes
300 NULL // template file
302 if(pipe_handle==INVALID_HANDLE_VALUE)
304 synfig::warning("IPC::make_connection(): Unable to connect to previous instance. GetLastError=%d",GetLastError());
306 int fd=_open_osfhandle(reinterpret_cast<long int>(pipe_handle),_O_APPEND|O_WRONLY);
308 struct stat file_stat;
309 if(stat(fifo_path().c_str(),&file_stat)!=0)
312 if(!S_ISFIFO(file_stat.st_mode))
315 int fd=open(fifo_path().c_str(),O_WRONLY|O_NONBLOCK);
319 ret=SmartFILE(fdopen(fd,"w"));
321 synfig::info("uplink fd=%d",fd);