1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
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 #ifdef HAVE_SYS_ERRNO_H
43 #include <sys/errno.h>
46 #include <synfig/main.h>
64 #include <glibmm/dispatcher.h>
65 #include <synfig/mutex.h>
66 #include <synfig/string.h>
67 #include <glibmm/thread.h>
71 /* === U S I N G =========================================================== */
75 using namespace synfig;
76 using namespace studio;
78 /* === M A C R O S ========================================================= */
80 /* === G L O B A L S ======================================================= */
83 #define WIN32_PIPE_PATH "\\\\.\\pipe\\SynfigStudio.Cmd"
84 static synfig::Mutex cmd_mutex;
85 static std::list<synfig::String> cmd_queue;
86 static Glib::Dispatcher* cmd_dispatcher;
93 pipe_handle=CreateNamedPipe(
94 WIN32_PIPE_PATH, // pipe name
95 PIPE_ACCESS_INBOUND, // Access type
96 PIPE_READMODE_BYTE /*|PIPE_NOWAIT*/,
97 PIPE_UNLIMITED_INSTANCES,
100 NMPWAIT_USE_DEFAULT_WAIT,
103 if(pipe_handle==INVALID_HANDLE_VALUE)
105 synfig::error("IPC(): Call to CreateNamedPipe failed. Ignore next error. GetLastError=%d",GetLastError());
110 connected=ConnectNamedPipe(pipe_handle,NULL)?true:(GetLastError()==ERROR_PIPE_CONNECTED);
114 Glib::Thread::yield();
124 &c, // buffer pointer
129 if(success && read_bytes==1 && c!='\n')
132 synfig::Mutex::Lock lock(cmd_mutex);
133 cmd_queue.push_back(data);
134 cmd_dispatcher->emit();
135 } while(success && read_bytes);
137 CloseHandle(pipe_handle);
144 synfig::Mutex::Lock lock(cmd_mutex);
145 while(!cmd_queue.empty())
147 IPC::process_command(cmd_queue.front());
148 cmd_queue.pop_front();
154 /* === P R O C E D U R E S ================================================= */
156 /* === M E T H O D S ======================================================= */
162 cmd_dispatcher=new Glib::Dispatcher;
163 cmd_dispatcher->connect(sigc::ptr_fun(empty_cmd_queue));
165 Glib::Thread::create(
166 sigc::ptr_fun(pipe_listen_thread),
172 remove(fifo_path().c_str());
175 if(mkfifo(fifo_path().c_str(), S_IRWXU)!=0)
177 synfig::error("IPC(): mkfifo failed for "+fifo_path());
181 fd=open(fifo_path().c_str(),O_RDWR);
186 synfig::error("IPC(): Failed to open fifo \"%s\". (errno=?)",fifo_path().c_str());
187 //synfig::error("IPC(): Failed to open fifo \"%s\". (errno=%d)",fifo_path().c_str(),::errno);
191 file=SmartFILE(fdopen(fd,"r"));
193 Glib::signal_io().connect(
194 sigc::mem_fun(this,&IPC::fifo_activity),
196 Glib::IO_IN|Glib::IO_PRI|Glib::IO_ERR|Glib::IO_HUP|Glib::IO_NVAL
206 // fclose(file.get());
208 remove(fifo_path().c_str());
218 return WIN32_PIPE_PATH;
220 return Glib::build_filename(App::get_user_app_directory(),"fifo");
225 IPC::fifo_activity(Glib::IOCondition cond)
227 synfig::info(__FILE__":%d: fifo activity",__LINE__);
229 if(cond&(Glib::IO_ERR|Glib::IO_HUP|Glib::IO_NVAL))
231 if(cond&(Glib::IO_ERR))
232 synfig::error("IPC::fifo_activity(): IO_ERR");
233 if(cond&(Glib::IO_HUP))
234 synfig::error("IPC::fifo_activity(): IO_HUP");
235 if(cond&(Glib::IO_NVAL))
236 synfig::error("IPC::fifo_activity(): IO_NVAL");
239 synfig::info(__FILE__":%d: fifo activity",__LINE__);
245 if(read(fd,&tmp,sizeof(tmp))<=0)
252 process_command(command);
257 IPC::process_command(const synfig::String& command_line)
259 if(command_line.empty())
262 char cmd=command_line[0];
264 String args(command_line.begin()+1,command_line.end());
265 while(!args.empty() && args[0]==' ') args.erase(args.begin());
266 while(!args.empty() && args[args.size()-1]=='\n' || args[args.size()-1]==' ') args.erase(args.end()-1);
270 case 'F': // Focus/Foreground
271 App::signal_present_all()();
273 case 'N': // New file
274 App::signal_present_all()();
277 case 'O': // Open <arg>
278 App::signal_present_all()();
286 synfig::warning("Received unknown command '%c' with arg '%s'",cmd,args.c_str());
294 IPC::make_connection()
299 pipe_handle=CreateFile(
301 GENERIC_WRITE, // desired access
303 NULL, // security attributes
304 OPEN_EXISTING, // creation disposition
305 FILE_ATTRIBUTE_NORMAL, // flags and attributes
306 NULL // template file
308 if(pipe_handle==INVALID_HANDLE_VALUE)
310 DWORD error = GetLastError();
312 if( error != ERROR_FILE_NOT_FOUND )
314 synfig::warning("IPC::make_connection(): Unable to connect to previous instance. GetLastError=%d",error);
316 int fd=_open_osfhandle(reinterpret_cast<long int>(pipe_handle),_O_APPEND|O_WRONLY);
318 struct stat file_stat;
319 if(stat(fifo_path().c_str(),&file_stat)!=0)
322 if(!S_ISFIFO(file_stat.st_mode))
325 int fd=open(fifo_path().c_str(),O_WRONLY|O_NONBLOCK);
329 ret=SmartFILE(fdopen(fd,"w"));
332 synfig::info("uplink fd=%d",fd);