1# -*- coding: utf-8 -*-
2# This file is part of the libCEC(R) library.
3#
4# libCEC(R) is Copyright (C) 2011-2015 Pulse-Eight Limited.
5# All rights reserved.
6# libCEC(R) is an original work, containing original code.
7#
8# libCEC(R) is a trademark of Pulse-Eight Limited.
9#
10# This program is dual-licensed; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23# 02110-1301  USA
24#
25#
26# Alternatively, you can license this library under a commercial license,
27# please contact Pulse-Eight Licensing for more information.
28#
29# For more information contact:
30# Pulse-Eight Licensing       <license@pulse-eight.com>
31#     http://www.pulse-eight.com/
32#     http://www.pulse-eight.net/
33#
34#
35# The code contained within this file also falls under the GNU license of
36# EventGhost
37#
38# Copyright © 2005-2016 EventGhost Project <http://www.eventghost.org/>
39#
40# EventGhost is free software: you can redistribute it and/or modify it under
41# the terms of the GNU General Public License as published by the Free
42# Software Foundation, either version 2 of the License, or (at your option)
43# any later version.
44#
45# EventGhost is distributed in the hope that it will be useful, but WITHOUT
46# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
47# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
48# more details.
49#
50# You should have received a copy of the GNU General Public License along
51# with EventGhost. If not, see <http://www.gnu.org/licenses/>.
52
53import eg
54import threading
55import wx
56from wx.lib.agw import ultimatelistctrl as ulc
57
58
59class Text(eg.TranslatableStrings):
60    name_lbl = 'Adapter Name'
61    avr_lbl = 'AVR Volume Control'
62    com_port_lbl = 'Adapter COM Port'
63    hdmi_lbl = 'Device HDMI Port'
64    adapter_lbl = 'Adapter:'
65    device_lbl = 'Device:'
66    poll_lbl = 'Polling Speed (ms)'
67
68
69class AdapterListCtrl(ulc.UltimateListCtrl):
70
71    @eg.LogIt
72    def __init__(self, parent):
73        self.lock = threading.Lock()
74
75        ulc.UltimateListCtrl.__init__(
76            self,
77            parent,
78            -1,
79            size=(600, 200),
80            agwStyle=(
81                wx.LC_REPORT |
82                wx.BORDER_SUNKEN |
83                wx.LC_EDIT_LABELS |
84                wx.LC_VRULES |
85                wx.LC_HRULES |
86                ulc.ULC_HAS_VARIABLE_ROW_HEIGHT |
87                ulc.ULC_BORDER_SELECT
88            )
89        )
90        self.InsertColumn(0, Text.name_lbl)
91        self.InsertColumn(1, Text.com_port_lbl)
92        self.InsertColumn(2, Text.hdmi_lbl)
93        self.InsertColumn(3, Text.avr_lbl)
94        self.InsertColumn(4, Text.poll_lbl)
95
96        self.SetColumnWidth(0, 100)
97        self.SetColumnWidth(1, 120)
98        self.SetColumnWidth(2, 115)
99        self.SetColumnWidth(3, 130)
100        self.SetColumnWidth(4, 130)
101
102        def get_value():
103            res = ()
104
105            for row in range(self.GetItemCount()):
106                name_item = self.GetItem(row, 0)
107                com_item = self.GetItem(row, 1)
108                hdmi_item = self.GetItem(row, 2)
109                avr_item = self.GetItem(row, 3)
110                poll_item = self.GetItem(row, 4)
111
112                adapter_name = str(name_item.GetText())
113                com_port = str(com_item.GetText())
114                hdmi_port = hdmi_item.GetWindow().GetValue()
115                use_avr = avr_item.GetWindow().GetValue()
116                poll_interval = poll_item.GetWindow().GetValue()
117
118                if adapter_name and adapter_name != 'ENTER NAME':
119                    res += ((
120                        com_port,
121                        adapter_name,
122                        hdmi_port,
123                        use_avr,
124                        poll_interval
125                    ),)
126            return res
127
128        self.GetValue = get_value
129
130    def add_cec_item(
131        self,
132        com_port,
133        adapter_name,
134        hdmi_port,
135        use_avr,
136        poll_interval,
137        _
138        # scan_type
139    ):
140        self.lock.acquire()
141        self.Freeze()
142
143        index = self.InsertStringItem(self.GetItemCount(), adapter_name)
144        self.SetStringItem(index, 1, com_port)
145        self.SetStringItem(index, 2, '')
146        self.SetStringItem(index, 3, '')
147        self.SetStringItem(index, 4, '')
148
149        com_item = self.GetItem(index, 0)
150        name_item = self.GetItem(index, 1)
151        hdmi_item = self.GetItem(index, 2)
152        avr_item = self.GetItem(index, 3)
153        poll_item = self.GetItem(index, 4)
154
155        hdmi_port_ctrl = eg.SpinIntCtrl(self, -1, hdmi_port, min=1, max=99)
156        hdmi_item.SetWindow(hdmi_port_ctrl)
157
158        avr_ctrl = wx.CheckBox(self, -1, '')
159        avr_ctrl.SetValue(use_avr)
160        avr_item.SetWindow(avr_ctrl)
161
162        poll_ctrl = eg.SpinNumCtrl(
163            self,
164            -1,
165            poll_interval,
166            min=0.1,
167            max=5.0,
168            increment=0.1
169        )
170        poll_item.SetWindow(poll_ctrl)
171
172        # if scan_type is None:
173        #     com_item.SetBackgroundColour((255, 255, 75))
174        #     name_item.SetBackgroundColour((255, 255, 75))
175        #     hdmi_port_ctrl.SetBackgroundColour((255, 255, 75))
176        #     hdmi_item.SetBackgroundColour((255, 255, 75))
177        #     avr_item.SetBackgroundColour((255, 255, 75))
178        #
179        # elif scan_type is False:
180        #     com_item.SetBackgroundColour((255, 0, 0))
181        #     name_item.SetBackgroundColour((255, 0, 0))
182        #     hdmi_item.SetBackgroundColour((255, 0, 0))
183        #     avr_item.SetBackgroundColour((255, 0, 0))
184
185        self.SetItem(com_item)
186        self.SetItem(name_item)
187        self.SetItem(hdmi_item)
188        self.SetItem(avr_item)
189        self.SetItem(poll_item)
190
191        self.Thaw()
192        self.Update()
193        self.lock.release()
194
195
196class AdapterCtrl(wx.Panel):
197
198    def __init__(self, parent, com_port, adapter_name, adapters):
199        wx.Panel.__init__(self, parent, -1)
200
201        choices = list(
202            adapter.name + ' : ' + adapter.com_port
203            for adapter in adapters
204        )
205
206        adapters_st = wx.StaticText(self, -1, Text.adapter_lbl)
207        adapters_ctrl = eg.Choice(self, 0, choices=sorted(choices))
208
209        adapters_ctrl.SetStringSelection(
210            str(adapter_name) + ' : ' + str(com_port)
211        )
212
213        def get_value():
214            value = adapters_ctrl.GetStringSelection()
215            a_name, c_port = value.split(' : ')
216            return c_port, a_name
217
218        adapters_sizer = wx.BoxSizer(wx.HORIZONTAL)
219        adapters_sizer.Add(adapters_st, 0, wx.EXPAND | wx.ALL, 5)
220        adapters_sizer.Add(adapters_ctrl, 0, wx.EXPAND | wx.ALL, 5)
221
222        self.GetValue = get_value
223        self.SetSizer(adapters_sizer)
224
225
226class DeviceCtrl(wx.Panel):
227
228    def __init__(self, parent, dev):
229        wx.Panel.__init__(self, parent, -1)
230
231        device_st = wx.StaticText(self, -1, Text.device_lbl)
232        device_ctrl = eg.Choice(self, 0, choices=[])
233        sizer = wx.BoxSizer(wx.HORIZONTAL)
234        sizer.Add(device_st, 0, wx.EXPAND | wx.ALL, 5)
235        sizer.Add(device_ctrl, 0, wx.EXPAND | wx.ALL, 5)
236
237        def on_choice(evt):
238            global dev
239            dev = device_ctrl.GetStringSelection()
240            evt.Skip()
241
242        device_ctrl.Bind(wx.EVT_CHOICE, on_choice)
243
244        def update_devices(adapter):
245            choices = list(d.name for d in adapter.devices)
246            device_ctrl.SetItems(choices)
247
248            if dev in choices:
249                device_ctrl.SetStringSelection(dev)
250            else:
251                device_ctrl.SetSelection(0)
252
253        def get_value():
254            return dev
255
256        self.GetValue = get_value
257        self.UpdateDevices = update_devices
258        self.SetSizer(sizer)
259