Esempio n. 1
0
    def port_to_str(self, port):
        if port["type"] is not None:
            port_type = shared.USBPhysicalPortTypes(port["type"]) if self.settings["show_friendly_types"] else shared.USBPhysicalPortTypes(port["type"]).value
        elif port["guessed"] is not None:
            port_type = (str(shared.USBPhysicalPortTypes(port["guessed"])) if self.settings["show_friendly_types"] else str(shared.USBPhysicalPortTypes(port["guessed"]).value)) + " (guessed)"
        else:
            port_type = "unknown"

        return f"{port['name']} | {shared.USBDeviceSpeeds(port['class'])} | " + (str(port_type) if self.settings["show_friendly_types"] else f"Type {port_type}")
Esempio n. 2
0
    def select_ports(self):
        if not self.controllers_historical:
            utils.TUIMenu("Select Ports and Build Kext",
                          "Select an option: ",
                          in_between=["No ports! Use the discovery mode."],
                          loop=True).start()
            return

        selection_index = 1
        by_port = []
        for controller in self.controllers_historical:
            controller["selected_count"] = 0
            for port in controller["ports"]:
                if "selected" not in port:
                    port["selected"] = bool(port["devices"])
                    port["selected"] = port["selected"] or (
                        bool(self.get_companion_port(port)["devices"])
                        if self.get_companion_port(port) else False)
                controller["selected_count"] += 1 if port["selected"] else 0
                port["selection_index"] = selection_index
                selection_index += 1
                by_port.append(port)

        while True:
            self.dump_historical()
            for controller in self.controllers_historical:
                controller["selected_count"] = sum(
                    1 if port["selected"] else 0
                    for port in controller["ports"])

            utils.header("Select Ports and Build Kext")
            print()
            for controller in self.controllers_historical:
                port_count_str = f"{controller['selected_count']}/{len(controller['ports'])}"
                port_count_str = color(port_count_str).red if controller[
                    "selected_count"] > 15 else color(port_count_str).green
                print(
                    self.controller_to_str(controller) +
                    f" | {port_count_str} ports")
                for port in controller["ports"]:
                    port_info = f"[{'#' if port['selected'] else ' '}]  {port['selection_index']}.{(len(str(selection_index)) - len(str(port['selection_index'])) + 1) * ' ' }" + self.port_to_str(
                        port)
                    companion = self.get_companion_port(port)
                    if companion:
                        port_info += f" | Companion to {companion['selection_index']}"
                    if port["selected"]:
                        print(color(port_info).green.bold)
                    else:
                        print(port_info)
                    if port["comment"]:
                        print(
                            len(f"[{'#' if port['selected'] else ' '}]  {port['selection_index']}.{(len(str(selection_index)) - len(str(port['selection_index'])) + 1) * ' ' }"
                                ) * " " + color(port["comment"]).blue.bold)
                    for device in port["devices"]:
                        self.print_devices(device,
                                           indentation="      " +
                                           len(str(selection_index)) * " " * 2)
                print()

            print(
                f"Binding companions is currently {color('on').green if self.settings['auto_bind_companions'] else color('off').red}.\n"
            )

            print(
                textwrap.dedent(f"""\
                K. Build {'USBMap' if self.settings['use_native'] else 'UTBMap'}.kext
                A. Select All
                N. Select None
                P. Enable All Populated Ports
                D. Disable All Empty Ports
                T. Show Types

                B. Back

                - Select ports to toggle with comma-delimited lists (eg. 1,2,3,4,5)
                - Change types using this formula T:1,2,3,4,5:t where t is the type
                - Set custom names using this formula C:1:Name - Name = None to clear"""
                                ))

            output = input("Select an option: ")
            if not output:
                continue
            elif output.upper() == "B":
                break
            elif output.upper() == "K":
                if not self.validate_selections():
                    continue
                self.build_kext()
                continue
            elif output.upper() in ("N", "A"):
                for port in by_port:
                    port["selected"] = output.upper() == "A"
            elif output.upper() == "P":
                for port in by_port:
                    if port["devices"] or (
                            self.get_companion_port(port)["devices"]
                            if self.get_companion_port(port) else False):
                        port["selected"] = True
            elif output.upper() == "D":
                for port in by_port:
                    if not port["devices"] and not (
                            self.get_companion_port(port)["devices"]
                            if self.get_companion_port(port) else False):
                        port["selected"] = False
            elif output.upper() == "T":
                self.print_types()
                continue
            elif output[0].upper() == "T":
                # We should have a type
                if len(output.split(":")) != 3:
                    continue
                try:
                    port_nums, port_type = output.split(":")[1:]
                    port_nums = port_nums.replace(" ", "").split(",")
                    port_type = shared.USBPhysicalPortTypes(int(port_type))

                    for port_num in list(port_nums):
                        if port_num not in port_nums:
                            continue

                        port_num = int(port_num) - 1

                        if port_num not in range(len(by_port)):
                            continue

                        companion = self.get_companion_port(by_port[port_num])
                        if self.settings["auto_bind_companions"] and companion:
                            companion["type"] = port_type
                            if str(companion["selection_index"]) in port_nums:
                                port_nums.remove(
                                    str(companion["selection_index"]))
                        by_port[port_num]["type"] = port_type
                except ValueError:
                    continue
            elif output[0].upper() == "C":
                # We should have a new name
                if len(output.split(":")) < 2:
                    continue
                try:
                    port_nums = output.split(":")[1].replace(" ",
                                                             "").split(",")
                    port_comment = output.split(":", 2)[2:]

                    for port_num in list(port_nums):
                        if port_num not in port_nums:
                            continue

                        port_num = int(port_num) - 1

                        if port_num not in range(len(by_port)):
                            continue

                        by_port[port_num]["comment"] = port_comment[
                            0] if port_comment else None
                except ValueError:
                    continue
            else:
                try:
                    port_nums = output.replace(" ", "").split(",")

                    for port_num in list(port_nums):
                        if port_num not in port_nums:
                            continue

                        port_num = int(port_num) - 1

                        if port_num not in range(len(by_port)):
                            continue

                        companion = self.get_companion_port(by_port[port_num])
                        if self.settings["auto_bind_companions"] and companion:
                            companion[
                                "selected"] = not by_port[port_num]["selected"]
                            if str(companion["selection_index"]) in port_nums:
                                port_nums.remove(
                                    str(companion["selection_index"]))
                        by_port[port_num][
                            "selected"] = not by_port[port_num]["selected"]
                except ValueError:
                    continue