Updated the date in the renderer-service-upnp.changes file
[profile/ivi/renderer-service-upnp.git] / test / cap.py
1 #!/usr/bin/python
2
3 # cap
4 #
5 # Copyright (C) 2012 Intel Corporation. All rights reserved.
6 #
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.
10 #
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
14 # for more details.
15 #
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.
19 #
20 # Mark Ryan <mark.d.ryan@intel.com>
21 #
22
23
24 from gi.repository import Gtk, Gdk, GdkPixbuf
25 import cairo
26 import dbus
27 import dbus.service
28 import dbus.mainloop.glib
29 import tempfile
30
31 class Renderer:
32
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')
41
42
43     def get_prop(self, prop_name, iface = ""):
44         return self.__propsIF.Get(iface, prop_name)
45
46     def push_file(self, fname):
47         try:
48             self.__hostIF.RemoveFile(fname)
49         except:
50             pass
51         self.__playerIF.Stop()
52         uri = self.__hostIF.HostFile(fname)
53         self.__playerIF.OpenUri(uri)
54         self.__playerIF.Play()
55
56 class Renderers:
57
58     def __init__(self, cb):
59         bus=dbus.SessionBus()
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')
64         self.__cb = cb
65         self.__manager.connect_to_signal("LostServer", self.__servers_changed)
66         self.__manager.connect_to_signal("FoundServer", self.__servers_changed)
67
68     def __servers_changed(self, server):
69         self.__cb()
70
71     def get_renderers(self):
72         retval = []
73         for path in self.__manager.GetServers():
74             retval.append((path, Renderer(path)))
75         return retval
76
77 class UI:
78
79     def delete_event(self, widget, event, data=None):
80         return False
81
82     def destroy(self, widget, data=None):
83         Gtk.main_quit()
84
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")])
89         return servers_store
90
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)
98         else:
99             entry.set_text("")
100
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)
106             ctx.fill()
107             widget.queue_draw_area(x -3, y -3, 6, 6)
108
109     def __mouse_button_pressed_cb(self, widget, event):
110         self.draw_rect(widget, event.x, event.y)
111         return True
112
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()
117         return True
118
119     def __draw_cb(self, da, ctx):
120         if self.__pixmap:
121             ctx.set_source_surface(self.__pixmap, 0, 0)
122             ctx.rectangle(0, 0,  da.get_allocated_width(),
123                           da.get_allocated_height())
124             ctx.fill()
125
126     @staticmethod
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)
132         ctx.fill()
133         return (new_pixmap, ctx)
134
135     def __configured_cb(self, widget, event):
136         allocation = widget.get_allocation()
137         width = allocation.width
138         height = allocation.height
139
140         new_pixmap, ctx = UI.__blank_pixmap(width, height)
141
142         if self.__pixmap:
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)
149             ctx.fill()
150
151         self.__pixmap = new_pixmap
152         return True
153
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)
161
162     def clear_cb(self, button):
163         allocation = self.__area.get_allocation()
164         self.__pixmap, ctx = UI.__blank_pixmap(allocation.width,
165                                                allocation.height)
166         self.__area.queue_draw_area(0,0, allocation.width, allocation.height)
167
168     def __init__(self):
169         self.__Renderers = Renderers(self.__reset_renderers)
170         self.__tmp_file = tempfile.mktemp(".png")
171         self.__pixmap = None
172         window = Gtk.Window()
173         window.set_default_size(640, 480)
174         window.set_title("Create and Push!")
175         container = Gtk.VBox(False, 0)
176
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)
181
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);
187
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)
204         window.show_all()
205         window.connect("delete_event", self.delete_event)
206         window.connect("destroy", self.destroy)
207         self.__window = window
208         self.__area = area
209
210 if __name__ == "__main__":
211     dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
212     ui = UI()
213     Gtk.main()