1
# Miro - an RSS based video player application
2
# Copyright (C) 2005-2010 Participatory Culture Foundation
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
# In addition, as a special exception, the copyright holders give
19
# permission to link the code of portions of this program with the OpenSSL
22
# You must obey the GNU General Public License in all respects for all of
23
# the code used other than OpenSSL. If you modify file(s) with this
24
# exception, you may extend this exception to your version of the file(s),
25
# but you are not obligated to do so. If you do not wish to do so, delete
26
# this exception statement from your version. If you delete this exception
27
# statement from all source files in the program, then also delete it here.
29
"""pipeipc.py -- Create a windows named pipe to controll IPC between different
32
The first proccess to start creates a named pipe, then a thread that listens
33
to that pipe. Subsequent processes send a message over that pipe containing
34
command line arguments. When the first process receives a message from the
35
pipe, we try to open them using the commandline module.
38
import cPickle as pickle
40
from ctypes.wintypes import DWORD, HANDLE, ULONG
44
from miro.plat import commandline
46
kernel32 = ctypes.windll.kernel32
48
ERROR_IO_PENDING = 997
50
ERROR_PIPE_CONNECTED = 535
51
ERROR_PIPE_LISTENING = 536
52
FILE_FLAG_OVERLAPPED = 0x40000000
54
INVALID_HANDLE_VALUE = -1
55
PIPE_ACCESS_DUPLEX = 0x00000003
56
PIPE_READMODE_MESSAGE = 0x00000002
57
PIPE_TYPE_MESSAGE = 0x00000004
58
PIPE_WAIT = 0x00000000
60
MIRO_IPC_PIPE_NAME = r'\\.\pipe\MiroIPC'
63
class PipeExists(Exception):
64
"""We tried to create a named pipe, but it already exists. Probably a
65
different Miro instance created it.
68
class QuitThread(Exception):
69
"""Raised to exit out of the pipe listening thread."""
71
class PipeError(IOError):
72
"""An IO Error occurred on a pipe."""
73
def __init__(self, description):
74
str = "%s failed: %d" % (description, kernel32.GetLastError())
75
IOError.__init__(self, str)
77
class _inner_struct(ctypes.Structure):
78
_fields_ = [('Offset', DWORD),
79
('OffsetHigh', DWORD),
82
class _inner_union(ctypes.Union):
83
_fields_ = [('anon_struct', _inner_struct), # struct
84
('Pointer', ctypes.c_void_p),
87
class OVERLAPPED(ctypes.Structure):
88
_fields_ = [('Internal', ctypes.POINTER(ULONG)),
89
('InternalHigh', ctypes.POINTER(ULONG)),
90
('union', _inner_union),
96
self.pipe = kernel32.CreateNamedPipeA(
98
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
99
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
100
1, 51200, 51200, 100, None)
101
if self.pipe == INVALID_HANDLE_VALUE:
103
self.pipe_event = kernel32.CreateEventA(None, True, False, None)
104
self.quit_event = kernel32.CreateEventA(None, True, False, None)
105
self.event_array = (HANDLE * 2)(self.pipe_event, self.quit_event)
106
self.overlapped = OVERLAPPED()
107
self.overlapped.hEvent = self.pipe_event
108
self.overlapped.Internal = None
109
self.overlapped.InternalHigh = None
110
self.overlapped.union.Pointer = None
111
self.message_handler = MessageHandler()
113
def start_process(self):
114
self.thread = threading.Thread(target=self._thread)
118
kernel32.SetEvent(self.quit_event)
121
def _wait_for_pipe(self):
122
kernel32.WaitForMultipleObjects(2, self.event_array, False, INFINITE)
123
if kernel32.WaitForSingleObject(self.quit_event, 0) == 0:
126
def _process_overlap(self, length):
127
return kernel32.GetOverlappedResult(self.pipe,
128
self._overlapped_ref(), ctypes.byref(length), False)
130
def _overlapped_ref(self):
131
return ctypes.byref(self.overlapped)
134
rv = kernel32.ConnectNamedPipe(self.pipe, self._overlapped_ref())
135
if not rv and kernel32.GetLastError() == ERROR_PIPE_CONNECTED:
137
if not rv and kernel32.GetLastError() == ERROR_IO_PENDING:
138
self._wait_for_pipe()
140
raise PipeError("ConnectNamedPipe")
145
buffer = ctypes.create_string_buffer(1024)
147
rv = kernel32.ReadFile(self.pipe, buffer, 1023,
148
ctypes.byref(c_length), self._overlapped_ref())
150
if kernel32.GetLastError() == ERROR_IO_PENDING:
151
self._wait_for_pipe()
152
elif kernel32.GetLastError() == ERROR_PIPE_LISTENING:
153
# Not sure why this happens, but it means we should just
154
# start at _connect()
156
elif kernel32.GetLastError() != ERROR_MORE_DATA:
157
raise PipeError("ReadFile")
159
rv = self._process_overlap(c_length)
161
buffer[c_length.value] = '\0'
162
read_in.append(buffer.value)
165
return ''.join(read_in)
167
def _write_out(self, data):
169
rv = kernel32.WriteFile(self.pipe, ctypes.create_string_buffer(data),
170
len(data), ctypes.byref(c_length), self._overlapped_ref())
171
if not rv and kernel32.GetLastError() == ERROR_IO_PENDING:
172
self._wait_for_pipe()
173
rv = self._process_overlap(c_length)
175
raise PipeError("WriteFile")
177
def _log_error(self, name):
178
logging.warn("%s failed: %d" % (name, kernel32.GetLastError()))
184
data = self._read_in()
187
self.message_handler.handle_message(data)
188
self._write_out("OK")
189
kernel32.DisconnectNamedPipe(self.pipe)
192
logging.info("Pipe reader finished")
194
class MessageHandler(object):
195
def handle_message(self, data):
196
# import this stuff inside the function, so that import errors don't
197
# mess with other code, which is part of the startup process
199
from miro import eventloop
200
from miro.commandline import parse_command_line_args
203
cmd_line = pickle.loads(data)
205
logging.warn("Error unpickling message (%r)" % data)
207
args = commandline.parse_command_line_string(cmd_line)
208
eventloop.addIdle(parse_command_line_args,
209
'parse command line', args=(args[1:],))
210
if (hasattr(app, "widgetapp") and app.widgetapp is not None and
211
app.widgetapp.window is not None):
212
gobject.idle_add(app.widgetapp.window._window.present)
214
def send_command_line_args():
215
message = pickle.dumps(commandline.get_command_line_string())
216
out_length = DWORD(0)
217
out_buffer = ctypes.create_string_buffer(1024)
218
kernel32.CallNamedPipeA(MIRO_IPC_PIPE_NAME,
219
ctypes.create_string_buffer(message), len(message), out_buffer,
220
1023, ctypes.byref(out_length), 20000)