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>
74 /* === U S I N G =========================================================== */
78 using namespace synfig;
79 using namespace studio;
81 /* === M A C R O S ========================================================= */
83 /* === G L O B A L S ======================================================= */
86 #define WIN32_PIPE_PATH "\\\\.\\pipe\\SynfigStudio.Cmd"
87 static synfig::Mutex cmd_mutex;
88 static std::list<synfig::String> cmd_queue;
89 static Glib::Dispatcher* cmd_dispatcher;
96 pipe_handle=CreateNamedPipe(
97 WIN32_PIPE_PATH, // pipe name
98 PIPE_ACCESS_INBOUND, // Access type
99 PIPE_READMODE_BYTE /*|PIPE_NOWAIT*/,
100 PIPE_UNLIMITED_INSTANCES,
103 NMPWAIT_USE_DEFAULT_WAIT,
106 if(pipe_handle==INVALID_HANDLE_VALUE)
108 synfig::error("IPC(): Call to CreateNamedPipe failed. Ignore next error. GetLastError=%d",GetLastError());
113 connected=ConnectNamedPipe(pipe_handle,NULL)?true:(GetLastError()==ERROR_PIPE_CONNECTED);
117 Glib::Thread::yield();
127 &c, // buffer pointer
132 if(success && read_bytes==1 && c!='\n')
135 synfig::Mutex::Lock lock(cmd_mutex);
136 cmd_queue.push_back(data);
137 cmd_dispatcher->emit();
138 } while(success && read_bytes);
140 CloseHandle(pipe_handle);
147 synfig::Mutex::Lock lock(cmd_mutex);
148 while(!cmd_queue.empty())
150 IPC::process_command(cmd_queue.front());
151 cmd_queue.pop_front();
157 /* === P R O C E D U R E S ================================================= */
159 /* === M E T H O D S ======================================================= */
165 cmd_dispatcher=new Glib::Dispatcher;
166 cmd_dispatcher->connect(sigc::ptr_fun(empty_cmd_queue));
168 Glib::Thread::create(
169 sigc::ptr_fun(pipe_listen_thread),
175 remove(fifo_path().c_str());
178 if(mkfifo(fifo_path().c_str(), S_IRWXU)!=0)
180 synfig::error("IPC(): mkfifo failed for "+fifo_path());
184 fd=open(fifo_path().c_str(),O_RDWR);
189 synfig::error("IPC(): Failed to open fifo \"%s\". (errno=?)",fifo_path().c_str());
190 //synfig::error("IPC(): Failed to open fifo \"%s\". (errno=%d)",fifo_path().c_str(),::errno);
194 file=SmartFILE(fdopen(fd,"r"));
196 Glib::signal_io().connect(
197 sigc::mem_fun(this,&IPC::fifo_activity),
199 Glib::IO_IN|Glib::IO_PRI|Glib::IO_ERR|Glib::IO_HUP|Glib::IO_NVAL
209 // fclose(file.get());
211 remove(fifo_path().c_str());
221 return WIN32_PIPE_PATH;
223 return Glib::build_filename(App::get_user_app_directory(),"fifo");
228 IPC::fifo_activity(Glib::IOCondition cond)
230 synfig::info(__FILE__":%d: fifo activity",__LINE__);
232 if(cond&(Glib::IO_ERR|Glib::IO_HUP|Glib::IO_NVAL))
234 if(cond&(Glib::IO_ERR))
235 synfig::error("IPC::fifo_activity(): IO_ERR");
236 if(cond&(Glib::IO_HUP))
237 synfig::error("IPC::fifo_activity(): IO_HUP");
238 if(cond&(Glib::IO_NVAL))
239 synfig::error("IPC::fifo_activity(): IO_NVAL");
242 synfig::info(__FILE__":%d: fifo activity",__LINE__);
248 if(read(fd,&tmp,sizeof(tmp))<=0)
255 process_command(command);
260 IPC::process_command(const synfig::String& command_line)
262 if(command_line.empty())
265 char cmd=command_line[0];
267 String args(command_line.begin()+1,command_line.end());
268 while(!args.empty() && args[0]==' ') args.erase(args.begin());
269 while(!args.empty() && args[args.size()-1]=='\n' || args[args.size()-1]==' ') args.erase(args.end()-1);
273 case 'F': // Focus/Foreground
274 App::signal_present_all()();
276 case 'N': // New file
277 App::signal_present_all()();
280 case 'O': // Open <arg>
281 App::signal_present_all()();
289 synfig::warning("Received unknown command '%c' with arg '%s'",cmd,args.c_str());
297 IPC::make_connection()
302 pipe_handle=CreateFile(
304 GENERIC_WRITE, // desired access
306 NULL, // security attributes
307 OPEN_EXISTING, // creation disposition
308 FILE_ATTRIBUTE_NORMAL, // flags and attributes
309 NULL // template file
311 if(pipe_handle==INVALID_HANDLE_VALUE)
313 DWORD error = GetLastError();
315 if( error != ERROR_FILE_NOT_FOUND )
317 synfig::warning("IPC::make_connection(): Unable to connect to previous instance. GetLastError=%d",error);
319 int fd=_open_osfhandle(reinterpret_cast<long int>(pipe_handle),_O_APPEND|O_WRONLY);
321 struct stat file_stat;
322 if(stat(fifo_path().c_str(),&file_stat)!=0)
325 if(!S_ISFIFO(file_stat.st_mode))
328 int fd=open(fifo_path().c_str(),O_WRONLY|O_NONBLOCK);
332 ret=SmartFILE(fdopen(fd,"w"));
335 // synfig::info("uplink fd=%d",fd);