예제 #1
0
class PiPresents(object):
    def pipresents_version(self):
        vitems = self.pipresents_issue.split('.')
        if len(vitems) == 2:
            # cope with 2 digit version numbers before 1.3.2
            return 1000 * int(vitems[0]) + 100 * int(vitems[1])
        else:
            return 1000 * int(vitems[0]) + 100 * int(vitems[1]) + int(
                vitems[2])

    def __init__(self):
        # gc.set_debug(gc.DEBUG_UNCOLLECTABLE|gc.DEBUG_INSTANCES|gc.DEBUG_OBJECTS|gc.DEBUG_SAVEALL)
        gc.set_debug(gc.DEBUG_UNCOLLECTABLE | gc.DEBUG_SAVEALL)
        self.pipresents_issue = "1.4.4"
        self.pipresents_minorissue = '1.4.4a'
        # position and size of window without -f command line option
        self.nonfull_window_width = 0.45  # proportion of width
        self.nonfull_window_height = 0.7  # proportion of height
        self.nonfull_window_x = 0  # position of top left corner
        self.nonfull_window_y = 0  # position of top left corner

        StopWatch.global_enable = False

        # set up the handler for SIGTERM
        signal.signal(signal.SIGTERM, self.handle_sigterm)

        # ****************************************
        # Initialisation
        # ***************************************
        # get command line options
        self.options = command_options()
        # print (self.options)

        # get Pi Presents code directory
        pp_dir = sys.path[0]
        self.pp_dir = pp_dir

        if not os.path.exists(pp_dir + "/pipresents.py"):
            if self.options['manager'] is False:
                tkinter.messagebox.showwarning("Pi Presents",
                                               "Bad Application Directory")
            exit(102)

        # Initialise logging and tracing
        Monitor.log_path = pp_dir
        self.mon = Monitor()
        # Init in PiPresents only
        self.mon.init()

        # uncomment to enable control of logging from within a class
        # Monitor.enable_in_code = True # enables control of log level in the code for a class  - self.mon.set_log_level()

        # make a shorter list to log/trace only some classes without using enable_in_code.
        Monitor.classes = [
            'PiPresents', 'HyperlinkShow', 'RadioButtonShow', 'ArtLiveShow',
            'ArtMediaShow', 'MediaShow', 'LiveShow', 'MenuShow', 'GapShow',
            'Show', 'ArtShow', 'AudioPlayer', 'BrowserPlayer', 'ImagePlayer',
            'MenuPlayer', 'MessagePlayer', 'VideoPlayer', 'Player',
            'MediaList', 'LiveList', 'ShowList', 'PathManager',
            'ControlsManager', 'ShowManager', 'PluginManager',
            'IOPluginManager', 'MplayerDriver', 'OMXDriver', 'UZBLDriver',
            'TimeOfDay', 'ScreenDriver', 'Animate', 'OSCDriver',
            'CounterManager', 'BeepsManager', 'Network', 'Mailer'
        ]

        # Monitor.classes=['PiPresents','MediaShow','GapShow','Show','VideoPlayer','Player','OMXDriver']
        # Monitor.classes=['OSCDriver']

        # get global log level from command line
        Monitor.log_level = int(self.options['debug'])
        Monitor.manager = self.options['manager']
        # print self.options['manager']
        self.mon.newline(3)
        self.mon.sched(
            self, None,
            "Pi Presents is starting, Version:" + self.pipresents_minorissue +
            ' at ' + time.strftime("%Y-%m-%d %H:%M.%S"))
        self.mon.log(
            self,
            "Pi Presents is starting, Version:" + self.pipresents_minorissue +
            ' at ' + time.strftime("%Y-%m-%d %H:%M.%S"))
        # self.mon.log (self," OS and separator:" + os.name +'  ' + os.sep)
        self.mon.log(self, "sys.path[0] -  location of code: " + sys.path[0])

        # log versions of Raspbian and omxplayer, and GPU Memory
        with open("/boot/issue.txt") as ifile:
            self.mon.log(self, '\nRaspbian: ' + ifile.read())

        self.mon.log(
            self,
            '\n' + check_output(["omxplayer", "-v"], universal_newlines=True))
        self.mon.log(
            self,
            '\nGPU Memory: ' + check_output(["vcgencmd", "get_mem", "gpu"],
                                            universal_newlines=True))

        if os.geteuid() == 0:
            print('Do not run Pi Presents with sudo')
            self.mon.log(self, 'Do not run Pi Presents with sudo')
            self.mon.finish()
            sys.exit(102)

        if "DESKTOP_SESSION" not in os.environ:
            print('Pi Presents must be run from the Desktop')
            self.mon.log(self, 'Pi Presents must be run from the Desktop')
            self.mon.finish()
            sys.exit(102)
        else:
            self.mon.log(self, 'Desktop is ' + os.environ['DESKTOP_SESSION'])

        # optional other classes used
        self.root = None
        self.ppio = None
        self.tod = None
        self.animate = None
        self.ioplugin_manager = None
        self.oscdriver = None
        self.osc_enabled = False
        self.tod_enabled = False
        self.email_enabled = False

        user = os.getenv('USER')

        if user is None:
            tkinter.messagebox.showwarning(
                "You must be logged in to run Pi Presents")
            exit(102)

        if user != 'pi':
            self.mon.warn(self, "You must be logged as pi to use GPIO")

        self.mon.log(self, 'User is: ' + user)
        # self.mon.log(self,"os.getenv('HOME') -  user home directory (not used): " + os.getenv('HOME')) # does not work
        # self.mon.log(self,"os.path.expanduser('~') -  user home directory: " + os.path.expanduser('~'))   # does not work

        # check network is available
        self.network_connected = False
        self.network_details = False
        self.interface = ''
        self.ip = ''
        self.unit = ''

        # sets self.network_connected and self.network_details
        self.init_network()

        # start the mailer and send email when PP starts
        self.email_enabled = False
        if self.network_connected is True:
            self.init_mailer()
            if self.email_enabled is True and self.mailer.email_at_start is True:
                subject = '[Pi Presents] ' + self.unit + ': PP Started on ' + time.strftime(
                    "%Y-%m-%d %H:%M")
                message = time.strftime(
                    "%Y-%m-%d %H:%M"
                ) + '\nUnit: ' + self.unit + '   Profile: ' + self.options[
                    'profile'] + '\n ' + self.interface + '\n ' + self.ip
                self.send_email('start', subject, message)

        # get profile path from -p option
        if self.options['profile'] != '':
            self.pp_profile_path = "/pp_profiles/" + self.options['profile']
        else:
            self.mon.err(self, "Profile not specified in command ")
            self.end('error',
                     'Profile not specified with the commands -p option')

    # get directory containing pp_home from the command,
        if self.options['home'] == "":
            home = os.sep + 'home' + os.sep + user + os.sep + "pp_home"
        else:
            home = self.options['home'] + os.sep + "pp_home"
        self.mon.log(self, "pp_home directory is: " + home)

        # check if pp_home exists.
        # try for 10 seconds to allow usb stick to automount
        found = False
        for i in range(1, 10):
            self.mon.log(self,
                         "Trying pp_home at: " + home + " (" + str(i) + ')')
            if os.path.exists(home):
                found = True
                self.pp_home = home
                break
            time.sleep(1)
        if found is True:
            self.mon.log(
                self,
                "Found Requested Home Directory, using pp_home at: " + home)
        else:
            self.mon.err(self, "Failed to find pp_home directory at " + home)
            self.end('error', "Failed to find pp_home directory at " + home)

        # check profile exists
        self.pp_profile = self.pp_home + self.pp_profile_path
        if os.path.exists(self.pp_profile):
            self.mon.sched(self, None,
                           "Running profile: " + self.pp_profile_path)
            self.mon.log(
                self, "Found Requested profile - pp_profile directory is: " +
                self.pp_profile)
        else:
            self.mon.err(
                self, "Failed to find requested profile: " + self.pp_profile)
            self.end('error',
                     "Failed to find requested profile: " + self.pp_profile)

        self.mon.start_stats(self.options['profile'])

        if self.options['verify'] is True:
            self.mon.err(self,
                         "Validation option not supported - use the editor")
            self.end('error',
                     'Validation option not supported - use the editor')

        # initialise and read the showlist in the profile
        self.showlist = ShowList()
        self.showlist_file = self.pp_profile + "/pp_showlist.json"
        if os.path.exists(self.showlist_file):
            self.showlist.open_json(self.showlist_file)
        else:
            self.mon.err(self, "showlist not found at " + self.showlist_file)
            self.end('error', "showlist not found at " + self.showlist_file)

        # check profile and Pi Presents issues are compatible
        if self.showlist.profile_version() != self.pipresents_version():
            self.mon.err(
                self,
                "Version of showlist " + self.showlist.profile_version_string +
                " is not  same as Pi Presents")
            self.end(
                'error',
                "Version of showlist " + self.showlist.profile_version_string +
                " is not  same as Pi Presents")

        # get the 'start' show from the showlist
        index = self.showlist.index_of_start_show()
        if index >= 0:
            self.showlist.select(index)
            self.starter_show = self.showlist.selected_show()
        else:
            self.mon.err(self, "Show [start] not found in showlist")
            self.end('error', "Show [start] not found in showlist")

# ********************
# SET UP THE GUI
# ********************
# turn off the screenblanking and saver
        if self.options['noblank'] is True:
            call(["xset", "s", "off"])
            call(["xset", "s", "-dpms"])

        # find connected displays and create a canvas for each display
        self.dm = DisplayManager()
        status, message, self.root = self.dm.init(self.options,
                                                  self.handle_user_abort)
        if status != 'normal':
            self.mon.err(self, message)
            self.end('error', message)

        self.mon.log(
            self,
            str(DisplayManager.num_displays) + ' Displays are connected:')

        for display_name in DisplayManager.display_map:
            status, message, display_id, canvas_obj = self.dm.id_of_canvas(
                display_name)
            if status != 'normal':
                continue
            width, height = self.dm.real_display_dimensions(display_id)
            self.mon.log(
                self, '   - ' + self.dm.name_of_display(display_id) + ' Id: ' +
                str(display_id) + ' ' + str(width) + '*' + str(height))
            canvas_obj.config(bg=self.starter_show['background-colour'])

# ****************************************
# INITIALISE THE TOUCHSCREEN DRIVER
# ****************************************

# each driver takes a set of inputs, binds them to symboic names
# and sets up a callback which returns the symbolic name when an input event occurs

        self.sr = ScreenDriver()
        # read the screen click area config file
        reason, message = self.sr.read(pp_dir, self.pp_home, self.pp_profile)
        if reason == 'error':
            self.end('error', 'cannot find, or error in screen.cfg')

        # create click areas on the canvases, must be polygon as outline rectangles are not filled as far as find_closest goes
        reason, message = self.sr.make_click_areas(self.handle_input_event)
        if reason == 'error':
            self.mon.err(self, message)
            self.end('error', message)

# ****************************************
# INITIALISE THE APPLICATION AND START
# ****************************************
        self.shutdown_required = False
        self.reboot_required = False
        self.terminate_required = False
        self.exitpipresents_required = False

        # initialise the Beeps Manager
        self.beepsmanager = BeepsManager()
        self.beepsmanager.init(self.pp_home, self.pp_profile)

        # initialise the I/O plugins by importing their drivers
        self.ioplugin_manager = IOPluginManager()
        reason, message = self.ioplugin_manager.init(self.pp_dir,
                                                     self.pp_profile,
                                                     self.root,
                                                     self.handle_input_event,
                                                     self.pp_home)
        if reason == 'error':
            # self.mon.err(self,message)
            self.end('error', message)

        # kick off animation sequencer
        self.animate = Animate()
        self.animate.init(pp_dir, self.pp_home, self.pp_profile, self.root,
                          200, self.handle_output_event)
        self.animate.poll()

        #create a showmanager ready for time of day scheduler and osc server
        show_id = -1
        self.show_manager = ShowManager(show_id, self.showlist,
                                        self.starter_show, self.root,
                                        self.pp_dir, self.pp_profile,
                                        self.pp_home)
        # first time through set callback to terminate Pi Presents if all shows have ended.
        self.show_manager.init(self.all_shows_ended_callback,
                               self.handle_command, self.showlist)
        # Register all the shows in the showlist
        reason, message = self.show_manager.register_shows()
        if reason == 'error':
            self.mon.err(self, message)
            self.end('error', message)

        # Init OSCDriver, read config and start OSC server
        self.osc_enabled = False
        if self.network_connected is True:
            if os.path.exists(self.pp_profile + os.sep + 'pp_io_config' +
                              os.sep + 'osc.cfg'):
                self.oscdriver = OSCDriver()
                reason, message = self.oscdriver.init(
                    self.pp_profile, self.unit, self.interface, self.ip,
                    self.handle_command, self.handle_input_event,
                    self.e_osc_handle_animate)
                if reason == 'error':
                    self.mon.err(self, message)
                    self.end('error', message)
                else:
                    self.osc_enabled = True
                    self.root.after(1000, self.oscdriver.start_server())

        # initialise ToD scheduler calculating schedule for today
        self.tod = TimeOfDay()
        reason, message, self.tod_enabled = self.tod.init(
            pp_dir, self.pp_home, self.pp_profile, self.showlist, self.root,
            self.handle_command)
        if reason == 'error':
            self.mon.err(self, message)
            self.end('error', message)

        # warn if the network not available when ToD required
        if self.tod_enabled is True and self.network_connected is False:
            self.mon.warn(
                self,
                'Network not connected  so Time of Day scheduler may be using the internal clock'
            )

        # init the counter manager
        self.counter_manager = CounterManager()
        if self.starter_show['counters-store'] == 'yes':
            store_enable = True
        else:
            store_enable = False
        reason, message = self.counter_manager.init(
            self.pp_profile + '/counters.cfg', store_enable,
            self.options['loadcounters'],
            self.starter_show['counters-initial'])
        if reason == 'error':
            self.mon.err(self, message)
            self.end('error', message)

        # warn about start shows and scheduler

        if self.starter_show['start-show'] == '' and self.tod_enabled is False:
            self.mon.sched(
                self, None,
                "No Start Shows in Start Show and no shows scheduled")
            self.mon.warn(
                self, "No Start Shows in Start Show and no shows scheduled")

        if self.starter_show['start-show'] != '' and self.tod_enabled is True:
            self.mon.sched(
                self, None,
                "Start Shows in Start Show and shows scheduled - conflict?")
            self.mon.warn(
                self,
                "Start Shows in Start Show and shows scheduled - conflict?")

        # run the start shows
        self.run_start_shows()

        # kick off the time of day scheduler which may run additional shows
        if self.tod_enabled is True:
            self.tod.poll()

        # start the I/O plugins input event generation
        self.ioplugin_manager.start()

        # start Tkinters event loop
        self.root.mainloop()

# *********************
#  RUN START SHOWS
# ********************

    def run_start_shows(self):
        self.mon.trace(self, 'run start shows')
        # parse the start shows field and start the initial shows
        show_refs = self.starter_show['start-show'].split()
        for show_ref in show_refs:
            reason, message = self.show_manager.control_a_show(
                show_ref, 'open')
            if reason == 'error':
                self.mon.err(self, message)

# *********************
# User inputs
# ********************

    def e_osc_handle_animate(self, line):
        #jump  out of server thread
        self.root.after(1, lambda arg=line: self.osc_handle_animate(arg))

    def osc_handle_animate(self, line):
        self.mon.log(self, "animate command received: " + line)
        #osc sends output events as a string
        reason, message, delay, name, param_type, param_values = self.animate.parse_animate_fields(
            line)
        if reason == 'error':
            self.mon.err(self, message)
            self.end(reason, message)
        self.handle_output_event(name, param_type, param_values, 0)

    # output events are animate commands
    def handle_output_event(self, symbol, param_type, param_values, req_time):
        reason, message = self.ioplugin_manager.handle_output_event(
            symbol, param_type, param_values, req_time)
        if reason == 'error':
            self.mon.err(self, message)
            self.end(reason, message)

    # all input events call this callback providing a symbolic name.
    # handle events that affect PP overall, otherwise pass to all active shows
    def handle_input_event(self, symbol, source):
        self.mon.log(self, "event received: " + symbol + ' from ' + source)
        if symbol == 'pp-terminate':
            self.handle_user_abort()

        elif symbol == 'pp-shutdown':
            self.mon.err(
                self,
                'pp-shutdown removed in version 1.3.3a, see Release Notes')
            self.end(
                'error',
                'pp-shutdown removed in version 1.3.3a, see Release Notes')

        elif symbol == 'pp-shutdownnow':
            # need root.after to grt out of st thread
            self.root.after(1, self.shutdownnow_pressed)
            return

        elif symbol == 'pp-exitpipresents':
            self.exitpipresents_required = True
            if self.show_manager.all_shows_exited() is True:
                # need root.after to grt out of st thread
                self.root.after(1, self.e_all_shows_ended_callback)
                return
            reason, message = self.show_manager.exit_all_shows()
        else:
            # pass the input event to all registered shows
            for show in self.show_manager.shows:
                show_obj = show[ShowManager.SHOW_OBJ]
                if show_obj is not None:
                    show_obj.handle_input_event(symbol)

    # commands are generated by tracks and shows
    # they can open or close shows, generate input events and do special tasks
    # commands also generate osc outputs to other computers
    # handles one command provided as a line of text

    def handle_command(self, command_text, source='', show=''):
        # print 'PIPRESENTS ',command_text,'\n   Source',source,'from',show
        self.mon.log(self, "command received: " + command_text)
        if command_text.strip() == "":
            return

        fields = command_text.split()

        if fields[0] in ('osc', 'OSC'):
            if self.osc_enabled is True:
                status, message = self.oscdriver.parse_osc_command(fields[1:])
                if status == 'warn':
                    self.mon.warn(self, message)
                if status == 'error':
                    self.mon.err(self, message)
                    self.end('error', message)
                return
            else:
                return

        if fields[0] == 'counter':
            status, message = self.counter_manager.parse_counter_command(
                fields[1:])
            if status == 'error':
                self.mon.err(self, message)
                self.end('error', message)
            return

        if fields[0] == 'beep':
            # cheat, field 0 will always be beep
            message, fields = self.beepsmanager.parse_beep(command_text)
            if message != '':
                self.mon.err(self, message)
                self.end('error', message)
                return
            location = self.beepsmanager.complete_path(fields[1])
            if not os.path.exists(location):
                message = 'Beep file does not exist: ' + location
                self.mon.err(self, message)
                self.end('error', message)
                return
            else:
                self.beepsmanager.do_beep(location)
            return

        show_command = fields[0]
        if len(fields) > 1:
            show_ref = fields[1]
        else:
            show_ref = ''
        if show_command in ('open', 'close', 'closeall', 'openexclusive'):
            self.mon.sched(self, TimeOfDay.now,
                           command_text + ' received from show:' + show)
            if self.shutdown_required is False and self.terminate_required is False:
                reason, message = self.show_manager.control_a_show(
                    show_ref, show_command)
            else:
                return

        elif show_command == 'monitor':
            self.handle_monitor_command(show_ref)
            return

        elif show_command == 'cec':
            self.handle_cec_command(show_ref)
            return

        elif show_command == 'event':
            self.handle_input_event(show_ref, 'Show Control')
            return

        elif show_command == 'exitpipresents':
            self.exitpipresents_required = True
            if self.show_manager.all_shows_exited() is True:
                # need root.after to get out of st thread
                self.root.after(1, self.e_all_shows_ended_callback)
                return
            else:
                reason, message = self.show_manager.exit_all_shows()

        elif show_command == 'shutdownnow':
            # need root.after to get out of st thread
            self.root.after(1, self.shutdownnow_pressed)
            return

        elif show_command == 'reboot':
            # need root.after to get out of st thread
            self.root.after(1, self.reboot_pressed)
            return

        else:
            reason = 'error'
            message = 'command not recognised: ' + show_command

        if reason == 'error':
            self.mon.err(self, message)
        return

    def handle_monitor_command(self, command):
        if command == 'on':
            os.system('vcgencmd display_power 1 >/dev/null')
        elif command == 'off':
            os.system('vcgencmd display_power 0 >/dev/null')

    def handle_cec_command(self, command):
        if command == 'on':
            os.system('echo "on 0" | cec-client -s')
        elif command == 'standby':
            os.system('echo "standby 0" | cec-client -s')

        elif command == 'scan':
            os.system('echo scan | cec-client -s -d 1')

    # deal with differnt commands/input events

    def shutdownnow_pressed(self):
        self.shutdown_required = True
        if self.show_manager.all_shows_exited() is True:
            self.all_shows_ended_callback('normal', 'no shows running')
        else:
            # calls exit method of all shows, results in all_shows_closed_callback
            self.show_manager.exit_all_shows()

    def reboot_pressed(self):
        self.reboot_required = True
        if self.show_manager.all_shows_exited() is True:
            self.all_shows_ended_callback('normal', 'no shows running')
        else:
            # calls exit method of all shows, results in all_shows_closed_callback
            self.show_manager.exit_all_shows()

    def handle_sigterm(self, signum, fframe):
        self.mon.log(self, 'SIGTERM received - ' + str(signum))
        self.terminate()

    def handle_user_abort(self):
        self.mon.log(self, 'User abort received')
        self.terminate()

    def terminate(self):
        self.mon.log(self, "terminate received")
        self.terminate_required = True
        needs_termination = False
        for show in self.show_manager.shows:
            # print  show[ShowManager.SHOW_OBJ], show[ShowManager.SHOW_REF]
            if show[ShowManager.SHOW_OBJ] is not None:
                needs_termination = True
                self.mon.log(
                    self,
                    "Sent terminate to show " + show[ShowManager.SHOW_REF])
                # call shows terminate method
                # eventually the show will exit and after all shows have exited all_shows_callback will be executed.
                show[ShowManager.SHOW_OBJ].terminate()
        if needs_termination is False:
            self.end('killed', 'killed - no termination of shows required')

# ******************************
# Ending Pi Presents after all the showers and players are closed
# **************************

    def e_all_shows_ended_callback(self):
        self.all_shows_ended_callback('normal', 'no shows running')

    # callback from ShowManager when all shows have ended
    def all_shows_ended_callback(self, reason, message):
        for display_name in DisplayManager.display_map:
            status, message, display_id, canvas_obj = self.dm.id_of_canvas(
                display_name)
            if status != 'normal':
                continue
            canvas_obj.config(bg=self.starter_show['background-colour'])
        if reason in (
                'killed', 'error'
        ) or self.shutdown_required is True or self.exitpipresents_required is True or self.reboot_required is True:
            self.end(reason, message)

    def end(self, reason, message):
        self.mon.log(self, "Pi Presents ending with reason: " + reason)
        if self.root is not None:
            self.root.destroy()
        self.tidy_up()
        if reason == 'killed':
            if self.email_enabled is True and self.mailer.email_on_terminate is True:
                subject = '[Pi Presents] ' + self.unit + ': PP Exited with reason: Terminated'
                message = time.strftime(
                    "%Y-%m-%d %H:%M"
                ) + '\n ' + self.unit + '\n ' + self.interface + '\n ' + self.ip
                self.send_email(reason, subject, message)
            self.mon.sched(self, None, "Pi Presents Terminated, au revoir\n")
            self.mon.log(self, "Pi Presents Terminated, au revoir")

            # close logging files
            self.mon.finish()
            print('Uncollectable Garbage', gc.collect())
            # objgraph.show_backrefs(objgraph.by_type('Canvas'),filename='backrefs.png')
            sys.exit(101)

        elif reason == 'error':
            if self.email_enabled is True and self.mailer.email_on_error is True:
                subject = '[Pi Presents] ' + self.unit + ': PP Exited with reason: Error'
                message_text = 'Error message: ' + message + '\n' + time.strftime(
                    "%Y-%m-%d %H:%M"
                ) + '\n ' + self.unit + '\n ' + self.interface + '\n ' + self.ip
                self.send_email(reason, subject, message_text)
            self.mon.sched(self, None,
                           "Pi Presents closing because of error, sorry\n")
            self.mon.log(self, "Pi Presents closing because of error, sorry")

            # close logging files
            self.mon.finish()
            print('uncollectable garbage', gc.collect())
            sys.exit(102)

        else:
            self.mon.sched(self, None, "Pi Presents  exiting normally, bye\n")
            self.mon.log(self, "Pi Presents  exiting normally, bye")

            # close logging files
            self.mon.finish()
            if self.reboot_required is True:
                # print 'REBOOT'
                call(['sudo', 'reboot'])
            if self.shutdown_required is True:
                # print 'SHUTDOWN'
                call(['sudo', 'shutdown', 'now', 'SHUTTING DOWN'])
            print('uncollectable garbage', gc.collect())
            sys.exit(100)

    # tidy up all the peripheral bits of Pi Presents
    def tidy_up(self):
        self.handle_monitor_command('on')
        self.mon.log(self, "Tidying Up")
        # turn screen blanking back on
        if self.options['noblank'] is True:
            call(["xset", "s", "on"])
            call(["xset", "s", "+dpms"])

        # tidy up animation
        if self.animate is not None:
            self.animate.terminate()

        # tidy up i/o plugins
        if self.ioplugin_manager != None:
            self.ioplugin_manager.terminate()

        if self.osc_enabled is True:
            self.oscdriver.terminate()

        # tidy up time of day scheduler
        if self.tod_enabled is True:
            self.tod.terminate()


# *******************************
# Connecting to network and email
# *******************************

    def init_network(self):

        timeout = int(self.options['nonetwork'])
        if timeout == 0:
            self.network_connected = False
            self.unit = ''
            self.ip = ''
            self.interface = ''
            return

        self.network = Network()
        self.network_connected = False

        # try to connect to network
        self.mon.log(self,
                     'Waiting up to ' + str(timeout) + ' seconds for network')
        success = self.network.wait_for_network(timeout)
        if success is False:
            self.mon.warn(
                self, 'Failed to connect to network after ' + str(timeout) +
                ' seconds')
            # tkMessageBox.showwarning("Pi Presents","Failed to connect to network so using fake-hwclock")
            return

        self.network_connected = True
        self.mon.sched(
            self, None, 'Time after network check is ' +
            time.strftime("%Y-%m-%d %H:%M.%S"))
        self.mon.log(
            self, 'Time after network check is ' +
            time.strftime("%Y-%m-%d %H:%M.%S"))

        # Get web configuration
        self.network_details = False
        network_options_file_path = self.pp_dir + os.sep + 'pp_config' + os.sep + 'pp_web.cfg'
        if not os.path.exists(network_options_file_path):
            self.mon.warn(
                self, "pp_web.cfg not found at " + network_options_file_path)
            return
        self.mon.log(self, 'Found pp_web.cfg in ' + network_options_file_path)

        self.network.read_config(network_options_file_path)
        self.unit = self.network.unit

        # get interface and IP details of preferred interface
        self.interface, self.ip = self.network.get_preferred_ip()
        if self.interface == '':
            self.network_connected = False
            return
        self.network_details = True
        self.mon.log(
            self, 'Network details ' + self.unit + ' ' + self.interface + ' ' +
            self.ip)

    def init_mailer(self):

        self.email_enabled = False
        email_file_path = self.pp_dir + os.sep + 'pp_config' + os.sep + 'pp_email.cfg'
        if not os.path.exists(email_file_path):
            self.mon.log(self, 'pp_email.cfg not found at ' + email_file_path)
            return
        self.mon.log(self, 'Found pp_email.cfg at ' + email_file_path)
        self.mailer = Mailer()
        self.mailer.read_config(email_file_path)
        # all Ok so can enable email if config file allows it.
        if self.mailer.email_allowed is True:
            self.email_enabled = True
            self.mon.log(self, 'Email Enabled')

    def try_connect(self):
        tries = 1
        while True:
            success, error = self.mailer.connect()
            if success is True:
                return True
            else:
                self.mon.log(
                    self, 'Failed to connect to email SMTP server ' +
                    str(tries) + '\n ' + str(error))
                tries += 1
                if tries > 5:
                    self.mon.log(
                        self, 'Failed to connect to email SMTP server after ' +
                        str(tries))
                    return False

    def send_email(self, reason, subject, message):
        if self.try_connect() is False:
            return False
        else:
            success, error = self.mailer.send(subject, message)
            if success is False:
                self.mon.log(self, 'Failed to send email: ' + str(error))
                success, error = self.mailer.disconnect()
                if success is False:
                    self.mon.log(self,
                                 'Failed disconnect after send:' + str(error))
                return False
            else:
                self.mon.log(self, 'Sent email for ' + reason)
                success, error = self.mailer.disconnect()
                if success is False:
                    self.mon.log(
                        self,
                        'Failed disconnect from email server ' + str(error))
                return True
class ScreenDriver(object):
    image_obj = []

    def __init__(self):
        self.mon = Monitor()
        self.dm = DisplayManager()

    # read screen.cfg
    def read(self, pp_dir, pp_home, pp_profile):
        self.pp_dir = pp_dir
        self.pp_home = pp_home
        # try inside profile
        tryfile = pp_profile + os.sep + 'pp_io_config' + os.sep + 'screen.cfg'
        # self.mon.log(self,"Trying screen.cfg in profile at: "+ tryfile)
        if os.path.exists(tryfile):
            filename = tryfile
        else:
            #give congiparser an empty filename so it returns an empty config.
            filename = ''
        ScreenDriver.config = configparser.ConfigParser(
            inline_comment_prefixes=(';', ))
        ScreenDriver.config.read(filename)
        if filename != '':
            self.mon.log(self, "screen.cfg read from " + filename)
        return 'normal', 'screen.cfg read'

    def click_areas(self):
        return ScreenDriver.config.sections()

    def get(self, section, item):
        return ScreenDriver.config.get(section, item)

    def is_in_config(self, section, item):
        return ScreenDriver.config.has_option(section, item)

    def parse_displays(self, text):
        return text.split(' ')

    # make click areas on the screen, bind them to their symbolic name, and create a callback if it is clicked.
    # click areas must be polygon as outline rectangles are not filled as far as find_closest goes
    # canvas is the PiPresents canvas

    def make_click_areas(self, callback):
        self.callback = callback
        reason = ''
        ScreenDriver.image_obj = []
        for area in self.click_areas():
            if not self.is_in_config(area, 'displays'):
                reason = 'error'
                message = 'missing displays field in screen.cfg'
                break
            displays_list = self.parse_displays(self.get(area, 'displays'))
            # print ('\n\n',displays_list)
            reason, message, points = self.parse_points(
                self.get(area, 'points'), self.get(area, 'name'))
            if reason == 'error':
                break

            # calculate centre of polygon
            vertices = len(points) // 2
            # print area, 'vertices',vertices
            sum_x = 0
            sum_y = 0
            for i in range(0, vertices):
                # print i
                sum_x = sum_x + int(points[2 * i])
                # print int(points[2*i])
                sum_y = sum_y + int(points[2 * i + 1])
                # print int(points[2*i+1])
            polygon_centre_x = sum_x / vertices
            polygon_centre_y = sum_y / vertices

            for display_name in DisplayManager.display_map:
                if display_name in displays_list:
                    status, message, display_id, canvas = self.dm.id_of_canvas(
                        display_name)
                    if status != 'normal':
                        continue
                    canvas.create_polygon(
                        points,
                        fill=self.get(area, 'fill-colour'),
                        outline=self.get(area, 'outline-colour'),
                        tags=("pp-click-area", self.get(area, 'name')),
                        state='hidden')

                    # image for the button
                    if not self.is_in_config(area, 'image'):
                        reason = 'error'
                        message = 'missing image fields in screen.cfg'
                        break
                    image_name = self.get(area, 'image')
                    if image_name != '':
                        image_width = int(self.get(area, 'image-width'))
                        image_height = int(self.get(area, 'image-height'))
                        image_path = self.complete_path(image_name)
                        if os.path.exists(image_path) is True:
                            self.pil_image = Image.open(image_path)
                        else:
                            image_path = self.pp_dir + os.sep + 'pp_resources' + os.sep + 'button.jpg'
                            if os.path.exists(image_path) is True:
                                self.mon.warn(
                                    self,
                                    'Default button image used for ' + area)
                                self.pil_image = Image.open(image_path)
                            else:
                                self.mon.warn(
                                    self,
                                    'Button image does not exist for ' + area)
                                self.pil_image = None

                        if self.pil_image is not None:
                            self.pil_image = self.pil_image.resize(
                                (image_width - 1, image_height - 1))
                            photo_image_id = ImageTk.PhotoImage(self.pil_image)
                            # print (display_id, canvas,self.pil_image,photo_image_id,self.get(area,'name'))
                            image_id = canvas.create_image(
                                polygon_centre_x,
                                polygon_centre_y,
                                image=photo_image_id,
                                anchor=CENTER,
                                tags=('pp-click-area', self.get(area, 'name')),
                                state='hidden')
                            del self.pil_image
                            ScreenDriver.image_obj.append(photo_image_id)
                            # print (ScreenDriver.image_obj)

                    # write the label at the centroid
                    if self.get(area, 'text') != '':
                        canvas.create_text(polygon_centre_x,
                                           polygon_centre_y,
                                           text=self.get(area, 'text'),
                                           fill=self.get(area, 'text-colour'),
                                           font=self.get(area, 'text-font'),
                                           tags=('pp-click-area',
                                                 self.get(area, 'name')),
                                           state='hidden')

                    canvas.bind('<Button-1>', self.click_pressed)

        if reason == 'error':
            return 'error', message
        else:
            return 'normal', 'made click areas'

    # callback for click on screen
    def click_pressed(self, event):
        x = event.x
        y = event.y
        # fail to correct the pointer position on touch so set for mouse click
        # x,y,text=self.dm.correct_touchscreen_pointer(event,0,False)

        overlapping = event.widget.find_overlapping(x - 5, y - 5, x + 5, y + 5)
        for item in overlapping:
            # print ScreenDriver.canvas.gettags(item)
            if ('pp-click-area'
                    in event.widget.gettags(item)) and event.widget.itemcget(
                        item, 'state') == 'normal':
                self.mon.log(
                    self, "Click on screen: " + event.widget.gettags(item)[1])
                self.callback(event.widget.gettags(item)[1], 'SCREEN')
                # need break as find_overlapping returns two results for each click, one with 'current' one without.
                break

    def is_click_area(self, test_area, canvas):
        click_areas = canvas.find_withtag('pp-click-area')
        for area in click_areas:
            if test_area in canvas.gettags(area):
                return True
        return False

    # use links with the symbolic name of click areas to enable the click areas in a show
    def enable_click_areas(self, links, canvas):
        for link in links:
            if self.is_click_area(link[0], canvas) and link[1] != 'null':
                # print 'enabling link ',link[0]
                canvas.itemconfig(link[0], state='normal')

    def hide_click_areas(self, links, canvas):
        # hide  click areas
        for link in links:
            if self.is_click_area(link[0], canvas) and link[1] != 'null':
                # print 'disabling link ',link[0]
                canvas.itemconfig(link[0], state='hidden')

        # this does not seem to change the colour of the polygon
        # ScreenDriver.canvas.itemconfig('pp-click-area',state='hidden')
        canvas.update_idletasks()

    def parse_points(self, points_text, area):
        if points_text.strip() == '':
            return 'error', 'No points in click area: ' + area, []
        if '+' in points_text:
            # parse  x+y+width*height
            fields = points_text.split('+')
            if len(fields) != 3:
                return 'error','Do not understand click area points: '+area,[]
            dimensions = fields[2].split('*')
            if len(dimensions) != 2:
                return 'error','Do not understand click area points: '+area,[]

            if not fields[0].isdigit():
                return 'error','x1 is not a positive integer in click area: '+area,[]
            else:
                x1 = int(fields[0])

            if not fields[1].isdigit():
                return 'error','y1 is not a positive integer in click area: '+area,[]
            else:
                y1 = int(fields[1])

            if not dimensions[0].isdigit():
                return 'error','width1 is not a positive integer in click area: '+area,[]
            else:
                width = int(dimensions[0])

            if not dimensions[1].isdigit():
                return 'error','height is not a positive integer in click area: '+area,[]
            else:
                height = int(dimensions[1])

            return 'normal', '', [
                str(x1),
                str(y1),
                str(x1 + width),
                str(y1),
                str(x1 + width),
                str(y1 + height),
                str(x1),
                str(y1 + height)
            ]

        else:
            # parse unlimited set of x,y,coords
            points = points_text.split()
            if len(points) < 6:
                return 'error','Less than 3 vertices in click area: '+area,[]
            if len(points) % 2 != 0:
                return 'error','Odd number of points in click area: '+area,[]
            for point in points:
                if not point.isdigit():
                    return 'error','point is not a positive integer in click area: '+area,[]
            return 'normal', 'parsed points OK', points

    def complete_path(self, track_file):
        #  complete path of the filename of the selected entry
        if track_file != '' and track_file[0] == "+":
            track_file = self.pp_home + track_file[1:]
        elif track_file[0] == "@":
            track_file = self.pp_profile + track_file[1:]
        return track_file
예제 #3
0
class pp_kbddriver(object):

    # CLASS VARIABLES  (pp_gpiodriver.)
    driver_active = False
    title = ''

    config = None

    def __init__(self):
        self.mon = Monitor()
        self.dm = DisplayManager()

    def init(self,
             filename,
             filepath,
             widget,
             pp_dir,
             pp_home,
             pp_profile,
             callback=None):

        # instantiate arguments
        self.widget = widget
        self.filename = filename
        self.filepath = filepath
        self.callback = callback
        pp_kbddriver.driver_active = False
        # print filename,filepath
        # read .cfg file.
        reason, message = self._read(self.filename, self.filepath)
        if reason == 'error':
            return 'error', message
        if self.config.has_section('DRIVER') is False:
            return 'error', 'No DRIVER section in ' + self.filepath

        #read information from DRIVER section
        pp_kbddriver.title = self.config.get('DRIVER', 'title')
        self.bind_printing = self.config.get('DRIVER', 'bind-printing')

        # and bind the keys
        self._bind_keys(widget, callback)
        pp_kbddriver.driver_active = True
        return 'normal', pp_kbddriver.title + 'Initialised'

    def start(self):
        return

    # allow track plugins (or anyting else) to access analog input values
    def get_input(self, channel):
        return False, None

    def terminate(self):
        pp_kbddriver.driver_active = False
        return

    def is_active(self):
        return pp_kbddriver.driver_active

    def handle_output_event(self, name, param_type, param_values, req_time):
        return 'normal', 'no output methods'

    # sets up tkinter keyboard events such that any key press
    # does a callback to 'callback' with the event object and a symbolic name.
    def _bind_keys(self, widget, callback):

        for display_name in DisplayManager.display_map:
            status, message, display_id, canvas = self.dm.id_of_canvas(
                display_name)
            if status != 'normal':
                continue
            # bind all the normal keys that return a printing character such that x produces pp-key-x
            if self.bind_printing == 'yes':
                canvas.bind("<Key>",
                            lambda event: self._normal_key(callback, event))

            for option in self.config.items('keys'):
                condition = option[0]
                symbolic_name = option[1]
                # print condition,symbolic_name
                # print condition,symbolic_name
                canvas.bind(condition,
                            lambda event, name=symbolic_name: self.
                            _specific_key(callback, name))

    def _specific_key(self, callback, name):
        callback(name, pp_kbddriver.title)

    # alphanumeric keys- convert to symbolic by adding pp-key-
    def _normal_key(self, callback, event):
        key = event.char
        if key != '':
            callback('pp-key-' + key, pp_kbddriver.title)

    # read the key bindings from keys.cfg
    def _read(self, filename, filepath):
        if os.path.exists(filepath):
            self.config = configparser.ConfigParser(
                inline_comment_prefixes=(';', ))
            self.config.optionxform = str

            self.config.read(filepath)
            return 'normal', filename + ' read'
        else:
            return 'error', filename + ' not found at: ' + filepath
예제 #4
0
class ShowManager(object):
    """
    ShowManager manages PiPresents' concurrent shows. It does not manage sub-shows or child-shows but has a bit of common code to initilise them
    concurrent shows are always top level (level 0) shows:
    They can be opened/closed  by the start show(open only) or by 'open/close myshow' in the Show Control field of players, by time of day sceduler or by OSC
    
   Two shows with the same show reference cannot be run concurrently as there is no way to reference an individual instance.
   However a workaround is to make the secong instance a subshow of a mediashow with a different reference.

    """

    # Declare class variables
    shows = []
    canvas = None  #canvas for all shows
    shutdown_required = False
    SHOW_TEMPLATE = ['', None]
    SHOW_REF = 0  # show-reference  - name of the show as in editor
    SHOW_OBJ = 1  # the python object
    showlist = []

    # Initialise class variables, first time through only in pipresents.py

    def init(self, all_shows_ended_callback, command_callback, showlist):
        ShowManager.all_shows_ended_callback = all_shows_ended_callback
        ShowManager.shows = []
        ShowManager.shutdown_required = False
        ShowManager.command_callback = command_callback
        ShowManager.showlist = showlist

# **************************************
# functions to manipulate show register
# **************************************

    def register_shows(self):
        for show in ShowManager.showlist.shows():
            if show['show-ref'] != 'start':
                reason, message = self.register_show(show['show-ref'])
                if reason == 'error':
                    return reason, message
        return 'normal', 'shows regiistered'

    def register_show(self, ref):
        registered = self.show_registered(ref)
        if registered == -1:
            ShowManager.shows.append(copy.deepcopy(ShowManager.SHOW_TEMPLATE))
            index = len(ShowManager.shows) - 1
            ShowManager.shows[index][ShowManager.SHOW_REF] = ref
            ShowManager.shows[index][ShowManager.SHOW_OBJ] = None
            self.mon.trace(
                self, ' - register show: show_ref = ' + ref + ' index = ' +
                str(index))
            return 'normal', 'show registered'
        else:
            # self.mon.err(self, ' more than one show in showlist with show-ref: ' + ref )
            return 'error', ' more than one show in showlist with show-ref: ' + ref

    # is the show registered?
    # can be used to return the index to the show
    def show_registered(self, show_ref):
        index = 0
        for show in ShowManager.shows:
            if show[ShowManager.SHOW_REF] == show_ref:
                return index
            index += 1
        return -1

    # needs calling program to check that the show is not already running
    def set_running(self, index, show_obj):
        ShowManager.shows[index][ShowManager.SHOW_OBJ] = show_obj
        self.mon.trace(
            self,
            'show_ref= ' + ShowManager.shows[index][ShowManager.SHOW_REF] +
            ' show_id= ' + str(index))

    # is the show running?
    def show_running(self, index):
        if ShowManager.shows[index][ShowManager.SHOW_OBJ] is not None:
            return ShowManager.shows[index][ShowManager.SHOW_OBJ]
        else:
            return None

    def set_exited(self, index):
        ShowManager.shows[index][ShowManager.SHOW_OBJ] = None
        self.mon.trace(
            self,
            'show_ref= ' + ShowManager.shows[index][ShowManager.SHOW_REF] +
            ' show_id= ' + str(index))

    # are all shows exited?
    def all_shows_exited(self):
        all_exited = True
        for show in ShowManager.shows:
            if show[ShowManager.SHOW_OBJ] is not None:
                all_exited = False
        return all_exited

    # fromat for printing
    def pretty_shows(self):
        shows = '\n'
        for show in ShowManager.shows:
            shows += show[ShowManager.SHOW_REF] + '\n'
        return shows


# *********************************
# show control
# *********************************

# show manager can be initialised by a player, shower or by pipresents.py
# if by pipresents.py then show_id=-1

    def __init__(self, show_id, showlist, show_params, root, pp_dir,
                 pp_profile, pp_home):
        self.show_id = show_id
        self.showlist = showlist
        self.show_params = show_params
        self.root = root
        self.pp_dir = pp_dir
        self.pp_profile = pp_profile
        self.pp_home = pp_home

        self.dm = DisplayManager()

        self.mon = Monitor()

    def control_a_show(self, show_ref, show_command):
        if show_command == 'open':
            return self.start_show(show_ref)
        elif show_command == 'close':
            return self.exit_show(show_ref)
        elif show_command == 'closeall':
            return self.exit_all_shows()
        elif show_command == 'openexclusive':
            return self.open_exclusive(show_ref)
        else:
            return 'error', 'command not recognised ' + show_command

    def open_exclusive(self, show_ref):
        self.exit_all_shows()
        self.exclusive_show = show_ref
        self.wait_for_openexclusive()
        return 'normal', 'opened exclusive'

    def wait_for_openexclusive(self):
        if self.all_shows_exited() is False:
            self.root.after(1, self.wait_for_openexclusive)
            return
        self.start_show(self.exclusive_show)

    def exit_all_shows(self):
        for show in ShowManager.shows:
            self.exit_show(show[ShowManager.SHOW_REF])
        return 'normal', 'exited all shows'

    # kick off the exit sequence of a show by calling the shows exit method.
    # it will result in all the shows in a stack being closed and end_play_show being called
    def exit_show(self, show_ref):
        index = self.show_registered(show_ref)
        self.mon.log(
            self,
            "De-registering show " + show_ref + ' show index:' + str(index))
        show_obj = self.show_running(index)
        if show_obj is not None:
            self.mon.log(
                self, "Exiting show " + show_ref + ' show index:' + str(index))
            show_obj.exit()
        return 'normal', 'exited a concurrent show'

    def start_show(self, show_ref):
        index = self.show_registered(show_ref)
        if index < 0:
            return 'error', "Show not found in showlist: " + show_ref
        show_index = self.showlist.index_of_show(show_ref)
        show = self.showlist.show(show_index)
        reason, message, show_canvas = self.compute_show_canvas(show)
        if reason == 'error':
            return reason, message
        # print 'STARTING TOP LEVEL SHOW',show_canvas
        self.mon.sched(
            self, TimeOfDay.now, 'Starting Show: ' + show_ref +
            ' from show: ' + self.show_params['show-ref'])
        self.mon.log(
            self, 'Starting Show: ' + show_ref + ' from: ' +
            self.show_params['show-ref'])
        if self.show_running(index):
            self.mon.sched(
                self, TimeOfDay.now,
                "show already running so ignoring command: " + show_ref)
            self.mon.warn(
                self, "show already running so ignoring command: " + show_ref)
            return 'normal', 'this concurrent show already running'
        show_obj = self.init_show(index, show, show_canvas)
        if show_obj is None:
            return 'error', "unknown show type in start concurrent show - " + show[
                'type']
        else:
            self.set_running(index, show_obj)
            # params - end_callback, show_ready_callback, parent_kickback_signal, level
            show_obj.play(self._end_play_show, None, False, 0, [])
            return 'normal', 'concurrent show started'

    # used by shows to create subshows or child shows
    def init_subshow(self, show_id, show, show_canvas):
        return self.init_show(show_id, show, show_canvas)

    def _end_play_show(self, index, reason, message):
        show_ref_to_exit = ShowManager.shows[index][ShowManager.SHOW_REF]
        show_to_exit = ShowManager.shows[index][ShowManager.SHOW_OBJ]
        self.mon.sched(self, TimeOfDay.now, 'Closed show: ' + show_ref_to_exit)
        self.mon.log(
            self, 'Exited from show: ' + show_ref_to_exit + ' ' + str(index))
        self.mon.log(self, 'Exited with Reason = ' + reason)
        self.mon.trace(
            self,
            ' Show is: ' + show_ref_to_exit + ' show index ' + str(index))
        # closes the video/audio from last track then closes the track
        # print 'show to exit ',show_to_exit, show_to_exit.current_player,show_to_exit.previous_player
        self.set_exited(index)
        if self.all_shows_exited() is True:
            ShowManager.all_shows_ended_callback(reason, message)
        return reason, message

    # common function to initilaise the show by type
    def init_show(
        self,
        show_id,
        selected_show,
        show_canvas,
    ):
        if selected_show['type'] == "mediashow":
            return MediaShow(show_id, selected_show, self.root, show_canvas,
                             self.showlist, self.pp_dir, self.pp_home,
                             self.pp_profile, ShowManager.command_callback)

        elif selected_show['type'] == "liveshow":
            return LiveShow(show_id, selected_show, self.root, show_canvas,
                            self.showlist, self.pp_dir, self.pp_home,
                            self.pp_profile, ShowManager.command_callback)

        elif selected_show['type'] == "radiobuttonshow":
            return RadioButtonShow(show_id, selected_show, self.root,
                                   show_canvas, self.showlist, self.pp_dir,
                                   self.pp_home, self.pp_profile,
                                   ShowManager.command_callback)

        elif selected_show['type'] == "hyperlinkshow":
            return HyperlinkShow(show_id, selected_show, self.root,
                                 show_canvas, self.showlist, self.pp_dir,
                                 self.pp_home, self.pp_profile,
                                 ShowManager.command_callback)

        elif selected_show['type'] == "menu":
            return MenuShow(show_id, selected_show, self.root, show_canvas,
                            self.showlist, self.pp_dir, self.pp_home,
                            self.pp_profile, ShowManager.command_callback)

        elif selected_show['type'] == "artmediashow":
            return ArtMediaShow(show_id, selected_show, self.root, show_canvas,
                                self.showlist, self.pp_dir, self.pp_home,
                                self.pp_profile, ShowManager.command_callback)

        elif selected_show['type'] == "artliveshow":
            return ArtLiveShow(show_id, selected_show, self.root, show_canvas,
                               self.showlist, self.pp_dir, self.pp_home,
                               self.pp_profile, ShowManager.command_callback)
        else:
            return None

    def compute_show_canvas(self, show_params):
        canvas = {}
        display_name = show_params['display-name']
        status, message, self.display_id, canvas_obj = self.dm.id_of_canvas(
            display_name)
        if status != 'normal':
            return 'error', message, None
        canvas['canvas-obj'] = canvas_obj
        status, message, self.show_canvas_x1, self.show_canvas_y1, self.show_canvas_x2, self.show_canvas_y2 = self.parse_show_canvas(
            show_params['show-canvas'])
        if status == 'error':
            return 'error', 'show canvas error: ' + message + ' in ' + show_params[
                'show-canvas'], None
        else:
            self.show_canvas_width = self.show_canvas_x2 - self.show_canvas_x1
            self.show_canvas_height = self.show_canvas_y2 - self.show_canvas_y1
            self.show_canvas_centre_x = self.show_canvas_width / 2
            self.show_canvas_centre_y = self.show_canvas_height / 2
            canvas['show-canvas-x1'] = self.show_canvas_x1
            canvas['show-canvas-y1'] = self.show_canvas_y1
            canvas['show-canvas-x2'] = self.show_canvas_x2
            canvas['show-canvas-y2'] = self.show_canvas_y2
            canvas['show-canvas-width'] = self.show_canvas_width
            canvas['show-canvas-height'] = self.show_canvas_height
            canvas['show-canvas-centre-x'] = self.show_canvas_centre_x
            canvas['show-canvas-centre-y'] = self.show_canvas_centre_y
            canvas['display-name'] = display_name
            canvas['display-id'] = self.display_id
            return 'normal', '', canvas

    def parse_show_canvas(self, text):
        fields = text.split()
        # blank so show canvas is the whole screen
        if len(fields) < 1:
            #get canvas dimensions from the display manager
            width, height = self.dm.canvas_dimensions(self.display_id)
            return 'normal', '', 0, 0, width, height

        elif len(fields) in (1, 4):
            # window is specified
            status, message, x1, y1, x2, y2 = parse_rectangle(text)
            if status == 'error':
                return 'error', message, 0, 0, 0, 0
            else:
                return 'normal', '', x1, y1, x2, y2
        else:
            # error
            return 'error', 'Wrong number of fields in Show canvas: ' + text, 0, 0, 0, 0
예제 #5
0
class pp_kbddriver_plus(object):

    # control list items
    NAME = 0  # symbolic name for input and output
    DIRECTION = 1  # in/out
    MATCH = 2  # for input the character/string to match (no EOL)
    MODE = 3  # for input the match mode any-char,char,any-line,line

    TEMPLATE = ['', '', '', '']

    # CLASS VARIABLES  (pp_kbddriver_plus.)
    driver_active = False
    title = ''  # usd for error reporting and logging
    tick_interval = ''  # mS between polls of the serial input

    match_mode = ''  # char or line, whether input characters are matched for each character or a complete line

    inputs = {}

    # executed by main program and by each object using the driver
    def __init__(self):
        self.dm = DisplayManager()

    # executed once from main program
    def init(self,
             filename,
             filepath,
             widget,
             pp_dir,
             pp_home,
             pp_profile,
             event_callback=None):

        # instantiate arguments
        self.widget = widget
        self.filename = filename
        self.filepath = filepath
        self.event_callback = event_callback

        pp_kbddriver_plus.driver_active = False

        # read pp_kbddriver_plus.cfg file.
        reason, message = self._read(self.filename, self.filepath)
        if reason == 'error':
            return 'error', message
        if self.config.has_section('DRIVER') is False:
            return 'error', 'No DRIVER section in ' + self.filepath

        # all the below are used by another instance of pp_kbddriver_plus so must reference class variables
        # read information from DRIVER section
        pp_kbddriver_plus.title = self.config.get('DRIVER', 'title')
        pp_kbddriver_plus.bind_printing = self.config.get(
            'DRIVER', 'bind-printing')

        # construct the control list from the config file
        pp_kbddriver_plus.in_names = []
        pp_kbddriver_plus.out_names = []
        for section in self.config.sections():
            if section == 'DRIVER':
                continue
            entry = copy.deepcopy(pp_kbddriver_plus.TEMPLATE)
            entry[pp_kbddriver_plus.NAME] = self.config.get(section, 'name')
            entry[pp_kbddriver_plus.DIRECTION] = self.config.get(
                section, 'direction')
            if entry[pp_kbddriver_plus.DIRECTION] == 'none':
                continue
            elif entry[pp_kbddriver_plus.DIRECTION] == 'in':
                entry[pp_kbddriver_plus.MODE] = self.config.get(
                    section, 'mode')
                if entry[pp_kbddriver_plus.MODE] in ('specific-character',
                                                     'specific-line'):
                    entry[pp_kbddriver_plus.MATCH] = self.config.get(
                        section, 'match')
                pp_kbddriver_plus.in_names.append(copy.deepcopy(entry))
            else:
                return 'error', pp_kbddriver_plus.title + ' direction not in or out'
        # print pp_kbddriver_plus.in_names

        # bind the keys
        self._bind_keys(widget, self._key_received)

        # all ok so indicate the driver is active
        pp_kbddriver_plus.driver_active = True

        # init must return two arguments
        return 'normal', pp_kbddriver_plus.title + ' active'

    # sets up tkinter keyboard events such that any key press
    # does a callback to _key_received() with the event object
    def _bind_keys(self, widget, callback):
        for display_name in DisplayManager.display_map:
            status, message, display_id, canvas = self.dm.id_of_canvas(
                display_name)
            if status != 'normal':
                continue
            # bind all the normal keys that return a printing character such that x produces pp-key-x (but fileterd in _key_received)
            canvas.bind("<Key>",
                        lambda event, match='<Key>', name='': self.
                        _key_received(event, match, name))
            # print 'bind printing'

            # Bind <Return> so that eol detection works, <Return> cannot be used to trigger an input event
            # if you wnt that use keys.cfg
            canvas.bind("<Return>",
                        lambda event, match='<Return>', name='': self.
                        _key_received(event, match, name))
            # print 'bind Return to make eol work'

            # go through entries and bind all specific-character matches to _key_received
            for entry in pp_kbddriver_plus.in_names:
                if entry[pp_kbddriver_plus.MODE] == 'specific-character':
                    match = entry[pp_kbddriver_plus.MATCH]
                    name = entry[pp_kbddriver_plus.NAME]
                    canvas.bind(match,
                                lambda event, match=match, name=name: self.
                                _key_received(event, match, name))
                    # print 'bind specific-char', match,name

    # start method must be defined. If not using inputs just pass
    def start(self):
        pp_kbddriver_plus.inputs['current-character'] = ''
        pp_kbddriver_plus.inputs['current-line'] = ''
        pp_kbddriver_plus.inputs['previous-line'] = ''

    def _key_received(self, event, match, name):
        # generate the events with symbolic names if driver is active
        if pp_kbddriver_plus.driver_active is True:
            char = event.char
            # print 'received ',char,match,name

            # if char is eol then match the line and start a new line
            if match == '<Return>':
                # do match of line
                # print 'do match line',pp_kbddriver_plus.inputs['current-line']
                self.match_line(pp_kbddriver_plus.inputs['current-line'])
                # shuffle and empty the buffer
                pp_kbddriver_plus.inputs[
                    'previous-line'] = pp_kbddriver_plus.inputs['current-line']
                pp_kbddriver_plus.inputs['current-line'] = ''
                pp_kbddriver_plus.inputs['current-character'] = ''
                if name != '':
                    # print 'bound <Return> key'
                    if self.event_callback is not None:
                        self.event_callback(name, pp_kbddriver_plus.title)
            else:
                # process a character
                if char == '' and match == '<Key>':
                    # unbound special key
                    # print 'unbound special key ', match
                    pass
                else:
                    # a character has been received
                    pp_kbddriver_plus.inputs['current-character'] = char
                    pp_kbddriver_plus.inputs['current-line'] += char
                    # print pp_kbddriver_plus.inputs['current-character'],pp_kbddriver_plus.inputs['current-line']
                    if match == '<Key>' and char != '' and self.bind_printing == 'yes':
                        # print 'printable key, bind-printing is yes',char,match
                        # printable character without overiding section
                        if self.event_callback is not None:
                            self.event_callback('pp-key-' + char,
                                                pp_kbddriver_plus.title)
                    else:
                        if name != '':
                            # print 'bound non-printable character',char,name
                            if self.event_callback is not None:
                                self.event_callback(name,
                                                    pp_kbddriver_plus.title)

                    # look through entries for any-character
                    for entry in pp_kbddriver_plus.in_names:
                        if entry[pp_kbddriver_plus.MODE] == 'any-character':
                            # print 'match any character', char, 'current line is ',pp_kbddriver_plus.inputs['current-line']
                            if self.event_callback is not None:
                                self.event_callback(
                                    entry[pp_kbddriver_plus.NAME],
                                    pp_kbddriver_plus.title)

    def match_line(self, line):
        for entry in pp_kbddriver_plus.in_names:
            if entry[pp_kbddriver_plus.MODE] == 'any-line':
                # print 'match any line',line
                if self.event_callback is not None:
                    self.event_callback(entry[pp_kbddriver_plus.NAME],
                                        pp_kbddriver_plus.title)

            if entry[pp_kbddriver_plus.
                     MODE] == 'specific-line' and line == entry[
                         pp_kbddriver_plus.MATCH]:
                # print 'match specific line', line
                if self.event_callback is not None:
                    self.event_callback(entry[pp_kbddriver_plus.NAME],
                                        pp_kbddriver_plus.title)

    # allow track plugins (or anything else) to access analog input values
    def get_input(self, key):
        if key in pp_kbddriver_plus.inputs:
            return True, pp_kbddriver_plus.inputs[key]
        else:
            return False, None

    # allow querying of driver state
    def is_active(self):
        return pp_kbddriver_plus.driver_active

    # called by main program only. Called when PP is closed down
    def terminate(self):
        pp_kbddriver_plus.driver_active = False

# ************************************************
# output interface method
# this can be called from many objects so needs to operate on class variables
# ************************************************
# execute an output event

    def handle_output_event(self, name, param_type, param_values, req_time):
        return 'normal', 'no output methods'


# ***********************************
# reading .cfg file
# ************************************

    def _read(self, filename, filepath):
        if os.path.exists(filepath):
            self.config = configparser.ConfigParser(
                inline_comment_prefixes=(';', ))
            self.config.read(filepath)
            return 'normal', filename + ' read'
        else:
            return 'error', filename + ' not found at: ' + filepath