5 # Copyright (C) 2012 Intel Corporation. All rights reserved.
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms and conditions of the GNU Lesser General Public License,
9 # version 2.1, as published by the Free Software Foundation.
11 # This program is distributed in the hope it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 # You should have received a copy of the GNU Lesser General Public License
17 # along with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 # Mark Ryan <mark.d.ryan@intel.com>
24 from gi.repository import Gtk, Gdk, GdkPixbuf
28 import dbus.mainloop.glib
33 def __init__(self, path):
34 bus = dbus.SessionBus()
35 obj = bus.get_object('com.intel.renderer-service-upnp', path)
36 self.__propsIF = dbus.Interface(obj, 'org.freedesktop.DBus.Properties')
37 self.__hostIF = dbus.Interface(obj,
38 'com.intel.RendererServiceUPnP.PushHost')
39 self.__playerIF = dbus.Interface(obj,
40 'org.mpris.MediaPlayer2.Player')
43 def get_prop(self, prop_name, iface = ""):
44 return self.__propsIF.Get(iface, prop_name)
46 def push_file(self, fname):
48 self.__hostIF.RemoveFile(fname)
51 self.__playerIF.Stop()
52 uri = self.__hostIF.HostFile(fname)
53 self.__playerIF.OpenUri(uri)
54 self.__playerIF.Play()
58 def __init__(self, cb):
60 obj = bus.get_object('com.intel.renderer-service-upnp',
61 '/com/intel/RendererServiceUPnP')
62 self.__manager = dbus.Interface(obj,
63 'com.intel.RendererServiceUPnP.Manager')
65 self.__manager.connect_to_signal("LostServer", self.__servers_changed)
66 self.__manager.connect_to_signal("FoundServer", self.__servers_changed)
68 def __servers_changed(self, server):
71 def get_renderers(self):
73 for path in self.__manager.GetServers():
74 retval.append((path, Renderer(path)))
79 def delete_event(self, widget, event, data=None):
82 def destroy(self, widget, data=None):
85 def __create_renderers_store(self):
86 servers_store = Gtk.ListStore(str, str)
87 for server in self.__Renderers.get_renderers():
88 servers_store.append([server[0], server[1].get_prop("Identity")])
91 def __reset_renderers(self):
92 print "Renderers Changed"
93 entry = self.__combo.get_child()
94 servers_store = self.__create_renderers_store()
95 self.__combo.set_model(servers_store)
96 if len(servers_store) > 0:
97 self.__combo.set_active(0)
101 def draw_rect(self, widget, x, y):
102 if self.__pixmap != None:
103 ctx = cairo.Context(self.__pixmap)
104 ctx.set_source_rgb(0, 0, 0)
105 ctx.rectangle(x -3, y -3, 6, 6)
107 widget.queue_draw_area(x -3, y -3, 6, 6)
109 def __mouse_button_pressed_cb(self, widget, event):
110 self.draw_rect(widget, event.x, event.y)
113 def __mouse_moved_cb(self, widget, event):
114 if event.state & Gdk.ModifierType.BUTTON1_MASK:
115 self.draw_rect(widget, event.x, event.y)
116 event.request_motions()
119 def __draw_cb(self, da, ctx):
121 ctx.set_source_surface(self.__pixmap, 0, 0)
122 ctx.rectangle(0, 0, da.get_allocated_width(),
123 da.get_allocated_height())
127 def __blank_pixmap(width, height):
128 new_pixmap = cairo.ImageSurface(cairo.FORMAT_RGB24, width, height)
129 ctx = cairo.Context(new_pixmap)
130 ctx.set_source_rgb(0xff, 0xff, 0xff)
131 ctx.rectangle(0, 0, width, height)
133 return (new_pixmap, ctx)
135 def __configured_cb(self, widget, event):
136 allocation = widget.get_allocation()
137 width = allocation.width
138 height = allocation.height
140 new_pixmap, ctx = UI.__blank_pixmap(width, height)
143 old_width = self.__pixmap.get_width()
144 old_height = self.__pixmap.get_height()
145 dest_x = (width - old_width) / 2
146 dest_y = (height - old_height) / 2
147 ctx.set_source_surface(self.__pixmap, dest_x, dest_y)
148 ctx.rectangle(0, 0, width, height)
151 self.__pixmap = new_pixmap
154 def push_cb(self, button):
155 tree_iter = self.__combo.get_active_iter()
156 if tree_iter != None:
157 self.__pixmap.write_to_png(self.__tmp_file)
158 model = self.__combo.get_model()
159 ren = Renderer(model[tree_iter][0])
160 ren.push_file(self.__tmp_file)
162 def clear_cb(self, button):
163 allocation = self.__area.get_allocation()
164 self.__pixmap, ctx = UI.__blank_pixmap(allocation.width,
166 self.__area.queue_draw_area(0,0, allocation.width, allocation.height)
169 self.__Renderers = Renderers(self.__reset_renderers)
170 self.__tmp_file = tempfile.mktemp(".png")
172 window = Gtk.Window()
173 window.set_default_size(640, 480)
174 window.set_title("Create and Push!")
175 container = Gtk.VBox(False, 0)
177 area = Gtk.DrawingArea()
178 area.set_events(Gdk.EventMask.BUTTON_PRESS_MASK |
179 Gdk.EventMask.POINTER_MOTION_MASK |
180 Gdk.EventMask.POINTER_MOTION_HINT_MASK)
182 area.connect("button_press_event", self.__mouse_button_pressed_cb)
183 area.connect("motion_notify_event", self.__mouse_moved_cb)
184 area.connect("configure-event", self.__configured_cb)
185 area.connect("draw", self.__draw_cb)
186 container.pack_start(area, True, True, 4);
188 button_bar = Gtk.HBox(False, 0)
189 push_button = Gtk.Button("Push");
190 push_button.connect("clicked", self.push_cb)
191 clear_button = Gtk.Button("Clear");
192 clear_button.connect("clicked", self.clear_cb)
193 servers_store = self.__create_renderers_store()
194 self.__combo = Gtk.ComboBox.new_with_model_and_entry(servers_store)
195 self.__combo.set_entry_text_column(1)
196 if len(servers_store) > 0:
197 self.__combo.set_active(0)
198 self.__combo.get_child().set_property("editable", False)
199 button_bar.pack_start(push_button, True, True, 4)
200 button_bar.pack_start(clear_button, True, True, 4)
201 button_bar.pack_start(self.__combo, True, True, 4)
202 container.pack_start(button_bar, False, False, 4);
203 window.add(container)
205 window.connect("delete_event", self.delete_event)
206 window.connect("destroy", self.destroy)
207 self.__window = window
210 if __name__ == "__main__":
211 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)