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 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");
245 if(read(fd,&tmp,sizeof(tmp))<=0)
252 synfig::info("%s:%d: fifo activity: '%s'", __FILE__, __LINE__, command.c_str());
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());
267 // erase leading spaces
268 while (!args.empty() && args[0] == ' ')
269 args.erase(args.begin());
271 // erase trailing newlines and spaces
272 while (!args.empty() && (args[args.size()-1] == '\n' || args[args.size()-1] == ' '))
273 args.erase(args.end()-1);
277 case 'F': // Focus/Foreground
278 App::signal_present_all()();
280 case 'N': // New file
281 App::signal_present_all()();
284 case 'O': // Open <arg>
285 App::signal_present_all()();
293 synfig::warning("Received unknown command '%c' with arg '%s'",cmd,args.c_str());
301 IPC::make_connection()
306 pipe_handle=CreateFile(
308 GENERIC_WRITE, // desired access
310 NULL, // security attributes
311 OPEN_EXISTING, // creation disposition
312 FILE_ATTRIBUTE_NORMAL, // flags and attributes
313 NULL // template file
315 if(pipe_handle==INVALID_HANDLE_VALUE)
317 DWORD error = GetLastError();
319 if( error != ERROR_FILE_NOT_FOUND )
321 synfig::warning("IPC::make_connection(): Unable to connect to previous instance. GetLastError=%d",error);
323 int fd=_open_osfhandle(reinterpret_cast<long int>(pipe_handle),_O_APPEND|O_WRONLY);
325 struct stat file_stat;
326 if(stat(fifo_path().c_str(),&file_stat)!=0)
329 if(!S_ISFIFO(file_stat.st_mode))
332 int fd=open(fifo_path().c_str(),O_WRONLY|O_NONBLOCK);
336 ret=SmartFILE(fdopen(fd,"w"));
339 // synfig::info("uplink fd=%d",fd);