1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
35 #ifdef HAVE_SYS_TYPES_H
36 #include <sys/types.h>
39 #ifdef HAVE_SYS_STAT_H
43 #ifdef HAVE_SYS_ERRNO_H
44 #include <sys/errno.h>
47 #include <synfig/main.h>
65 #include <glibmm/dispatcher.h>
66 #include <synfig/mutex.h>
67 #include <synfig/string.h>
68 #include <glibmm/thread.h>
72 /* === U S I N G =========================================================== */
76 using namespace synfig;
77 using namespace studio;
79 /* === M A C R O S ========================================================= */
81 /* === G L O B A L S ======================================================= */
84 #define WIN32_PIPE_PATH "\\\\.\\pipe\\SynfigStudio.Cmd"
85 static synfig::Mutex cmd_mutex;
86 static std::list<synfig::String> cmd_queue;
87 static Glib::Dispatcher* cmd_dispatcher;
94 pipe_handle=CreateNamedPipe(
95 WIN32_PIPE_PATH, // pipe name
96 PIPE_ACCESS_INBOUND, // Access type
97 PIPE_READMODE_BYTE /*|PIPE_NOWAIT*/,
98 PIPE_UNLIMITED_INSTANCES,
101 NMPWAIT_USE_DEFAULT_WAIT,
104 if(pipe_handle==INVALID_HANDLE_VALUE)
106 synfig::error("IPC(): Call to CreateNamedPipe failed. Ignore next error. GetLastError=%d",GetLastError());
111 connected=ConnectNamedPipe(pipe_handle,NULL)?true:(GetLastError()==ERROR_PIPE_CONNECTED);
115 Glib::Thread::yield();
125 &c, // buffer pointer
130 if(success && read_bytes==1 && c!='\n')
133 synfig::Mutex::Lock lock(cmd_mutex);
134 cmd_queue.push_back(data);
135 cmd_dispatcher->emit();
136 } while(success && read_bytes);
138 CloseHandle(pipe_handle);
145 synfig::Mutex::Lock lock(cmd_mutex);
146 while(!cmd_queue.empty())
148 IPC::process_command(cmd_queue.front());
149 cmd_queue.pop_front();
155 /* === P R O C E D U R E S ================================================= */
157 /* === M E T H O D S ======================================================= */
163 cmd_dispatcher=new Glib::Dispatcher;
164 cmd_dispatcher->connect(sigc::ptr_fun(empty_cmd_queue));
166 Glib::Thread::create(
167 sigc::ptr_fun(pipe_listen_thread),
173 remove(fifo_path().c_str());
176 if(mkfifo(fifo_path().c_str(), S_IRWXU)!=0)
178 synfig::error("IPC(): mkfifo failed for "+fifo_path());
182 fd=open(fifo_path().c_str(),O_RDWR);
187 synfig::error("IPC(): Failed to open fifo \"%s\". (errno=?)",fifo_path().c_str());
188 //synfig::error("IPC(): Failed to open fifo \"%s\". (errno=%d)",fifo_path().c_str(),::errno);
192 file=SmartFILE(fdopen(fd,"r"));
194 Glib::signal_io().connect(
195 sigc::mem_fun(this,&IPC::fifo_activity),
197 Glib::IO_IN|Glib::IO_PRI|Glib::IO_ERR|Glib::IO_HUP|Glib::IO_NVAL
207 // fclose(file.get());
209 remove(fifo_path().c_str());
219 return WIN32_PIPE_PATH;
221 return Glib::build_filename(App::get_user_app_directory(),"fifo");
226 IPC::fifo_activity(Glib::IOCondition cond)
228 synfig::info(__FILE__":%d: fifo activity",__LINE__);
230 if(cond&(Glib::IO_ERR|Glib::IO_HUP|Glib::IO_NVAL))
232 if(cond&(Glib::IO_ERR))
233 synfig::error("IPC::fifo_activity(): IO_ERR");
234 if(cond&(Glib::IO_HUP))
235 synfig::error("IPC::fifo_activity(): IO_HUP");
236 if(cond&(Glib::IO_NVAL))
237 synfig::error("IPC::fifo_activity(): IO_NVAL");
240 synfig::info(__FILE__":%d: fifo activity",__LINE__);
246 if(read(fd,&tmp,sizeof(tmp))<=0)
253 process_command(command);
258 IPC::process_command(const synfig::String& command_line)
260 if(command_line.empty())
263 char cmd=command_line[0];
265 String args(command_line.begin()+1,command_line.end());
266 while(!args.empty() && args[0]==' ') args.erase(args.begin());
267 while(!args.empty() && args[args.size()-1]=='\n' || args[args.size()-1]==' ') args.erase(args.end()-1);
271 case 'F': // Focus/Foreground
272 App::signal_present_all()();
274 case 'N': // New file
275 App::signal_present_all()();
278 case 'O': // Open <arg>
279 App::signal_present_all()();
287 synfig::warning("Received unknown command '%c' with arg '%s'",cmd,args.c_str());
295 IPC::make_connection()
300 pipe_handle=CreateFile(
302 GENERIC_WRITE, // desired access
304 NULL, // security attributes
305 OPEN_EXISTING, // creation disposition
306 FILE_ATTRIBUTE_NORMAL, // flags and attributes
307 NULL // template file
309 if(pipe_handle==INVALID_HANDLE_VALUE)
311 DWORD error = GetLastError();
313 if( error != ERROR_FILE_NOT_FOUND )
315 synfig::warning("IPC::make_connection(): Unable to connect to previous instance. GetLastError=%d",error);
317 int fd=_open_osfhandle(reinterpret_cast<long int>(pipe_handle),_O_APPEND|O_WRONLY);
319 struct stat file_stat;
320 if(stat(fifo_path().c_str(),&file_stat)!=0)
323 if(!S_ISFIFO(file_stat.st_mode))
326 int fd=open(fifo_path().c_str(),O_WRONLY|O_NONBLOCK);
330 ret=SmartFILE(fdopen(fd,"w"));
333 // synfig::info("uplink fd=%d",fd);