Exemple #1
0
def schedule_script(script_in, x, y):
    global threads
    global to_run
    global running
    coords = "(" + str(x) + ", " + str(y) + ")"

    if threads[x][y] != None:
        if threads[x][y].is_alive():
            print("[scripts] " + coords + " Script already running, killing script....")
            threads[x][y].kill.set()
            return

    if (x, y) in [l[1:] for l in to_run]:
        print("[scripts] " + coords + " Script already scheduled, unscheduling...")
        indexes = [i for i, v in enumerate(to_run) if ((v[1] == x) and (v[2] == y))]
        for index in indexes[::-1]:
            temp = to_run.pop(index)
        return

    if script_in.split("\n")[0].split(" ")[0] in ASYNC_HEADERS:
        print("[scripts] " + coords + " Starting asynchronous script in background...")
        threads[x][y] = threading.Thread(target=run_script, args=(script_in,x,y))
        threads[x][y].kill = threading.Event()
        threads[x][y].start()
    elif not running:
        print("[scripts] " + coords + " No script running, starting script in background...")
        threads[x][y] = threading.Thread(target=run_script_and_run_next, args=(script_in,x,y))
        threads[x][y].kill = threading.Event()
        threads[x][y].start()
    else:
        print("[scripts] " + coords + " A script is already running, scheduling...")
        to_run.append((script_in, x, y))
    lp_colors.updateXY(x, y)
Exemple #2
0
 def save_script(self, window, x, y, script_text, open_editor = False, color=None):
     global colors_to_set
     
     script_text = script_text.strip()
     
     def open_editor_func():
         nonlocal x, y
         if open_editor:
                 self.script_entry_window(x, y, script_text, color)
     try:
         script_validate = scripts.validate_script(script_text)
     except:
         self.popup(window, "Script Validation Error", self.error_image, "Fatal error while attempting to validate script.\nPlease see LPHK.log for more information.", "OK", end_command = open_editor_func)
         raise
     if script_validate == True:
         if script_text != "":
             script_text = files.strip_lines(script_text)
             scripts.bind(x, y, script_text, colors_to_set[x][y])
             self.draw_canvas()
             lp_colors.updateXY(x, y)
             window.destroy()
         else:
             self.popup(window, "No Script Entered", self.info_image, "Please enter a script to bind.", "OK", end_command = open_editor_func)
     else:
         self.popup(window, "(" + str(x) + ", " + str(y) + ") Syntax Error", self.error_image, "Error in line: " + script_validate[1] + "\n" + script_validate[0], "OK", end_command = open_editor_func)
Exemple #3
0
def copy(x1, y1, x2, y2):
    global text
    color_1 = lp_colors.curr_colors[x1][y1]

    script_1 = text[x1][y1]

    unbind(x2, y2)
    if script_1 != "":
        bind(x2, y2, script_1, color_1)
    lp_colors.updateXY(x2, y2)
    files.layout_changed_since_load = True
Exemple #4
0
def Copy(x1, y1, x2, y2):
    global buttons

    color_1 = lp_colors.curr_colors[x1][y1]  # Get colour of btn to be copied

    script_1 = buttons[x1, y1].script_str  # Get script to be copied

    Unbind(x2, y2)  # Unbind the destination
    if script_1 != "":  # If we're copying a button with a script...
        Bind(x2, y2, script_1,
             color_1)  # ...bind the details to the destination
    lp_colors.updateXY(x2, y2)  # Update the colours

    files.layout_changed_since_load = True  # Flag the layout as changed
Exemple #5
0
    def save_script(self, window, x, y, color, script_text):
        script_text = script_text.strip()

        script_validate = scripts.validate_script(script_text)
        if script_validate == True:
            if script_text != "":
                script_text = files.strip_lines(script_text)
                scripts.bind(x, y, script_text, color)
                self.draw_canvas()
                lp_colors.updateXY(x, y)
                window.destroy()
            else:
                self.popup(window, "No Script Entered", self.info_image, "Please enter a script to bind.", "OK")
        else:
            self.popup(window, "Syntax Error", self.warning_image, "Error in line: " + script_validate[1] + "\n" + script_validate[0], "OK")
Exemple #6
0
def Move(x1, y1, x2, y2):
    global buttons
    if (x1, y1) == (x2, y2):
        return

    color_1 = lp_colors.curr_colors[x1][y1]  # Get source button colour

    btn_1 = buttons[x1][y1]  # Get source button script

    Unbind(x1, y1)  # Unbind *both* buttons
    Unbind(x2, y2)

    if btn_1.script != "":  # If the source had a script...
        Bind(x2, y2, btn_1, color_1)  # ...bind it to the destination
    lp_colors.updateXY(x2, y2)  # Update the destination colours

    files.layout_changed_since_load = True  # And flag the layout as changed
Exemple #7
0
def run(lp_object):
    global timer
    while True:
        event = lp_object.ButtonStateXY()
        if event != []:
            x = event[0]
            y = event[1]
            if event[2] == 0:
                pressed[x][y] = False
            else:
                pressed[x][y] = True
                press_funcs[x][y](x, y)
            lp_colors.updateXY(x, y)
        else:
            break
    init(lp_object)
    timer.start()
Exemple #8
0
def swap(x1, y1, x2, y2):
    global text
    color_1 = lp_colors.curr_colors[x1][y1]
    color_2 = lp_colors.curr_colors[x2][y2]

    script_1 = text[x1][y1]
    script_2 = text[x2][y2]

    unbind(x1, y1)
    if script_2 != "":
        bind(x1, y1, script_2, color_2)
    lp_colors.updateXY(x1, y1)

    unbind(x2, y2)
    if script_1 != "":
        bind(x2, y2, script_1, color_1)
    lp_colors.updateXY(x2, y2)
    files.layout_changed_since_load = True
Exemple #9
0
def run(lp_object):
    global timer
    while True:  # loop forever
        event = lp_object.ButtonStateXY()  # get a pending event
        if event != []:  # if there is an event
            x = event[0]  # determine the button it is
            y = event[1]
            if event[2] == 0:  # is this button released?
                pressed[x][y] = False
            else:  # I presume this is "button pressed"
                pressed[x][y] = True
                press_funcs[x][y](
                    x, y)  # do whatever you need to do with a pressed button
            lp_colors.updateXY(x, y)  # and update the button colour
        else:  # but if there's no event pending
            break  # break out of the loop
    init(lp_object)  # and schedule this button to run after the delay
    timer.start()
Exemple #10
0
    def Schedule_script(self):
        # @@@ may be worth checking to see if it's a subroutine.  Because subroutines shouldn't use this
        global to_run

        if self.thread != None:
            if self.thread.is_alive():
                # @@@ The following code creates a problem if a script is looking for a second keypress
                # @@@ Maybe we need an option to make a script un-interruptable, or alternately require
                # @@@ *something* else (maybe ctrl-alt) to be pressed to allow the kill to take place.
                print("[scripts] " + self.coords +
                      " Script already running, killing script....")
                self.thread.kill.set()
                return

        if (self.x, self.y) in [l[1:] for l in to_run]:
            print("[scripts] " + self.coords +
                  " Script already scheduled, unscheduling...")
            indexes = [
                i for i, v in enumerate(to_run)
                if ((v[1] == self.x) and (v[2] == self.y))
            ]
            for index in indexes[::-1]:
                temp = to_run.pop(index)
            return

        if self.is_async:
            print("[scripts] " + self.coords +
                  " Starting asynchronous script in background...")
            self.thread = threading.Thread(target=Run_script, args=())
            self.thread.kill = threading.Event()
            self.thread.start()
        elif not self.running():
            print("[scripts] " + self.coords +
                  " No script running, starting script in background...")
            self.thread = threading.Thread(target=self.Run_script_and_run_next,
                                           args=())
            self.thread.kill = threading.Event()
            self.thread.start()
        else:
            print("[scripts] " + self.coords +
                  " A script is already running, scheduling...")
            to_run.append((self.x, self.y))

        lp_colors.updateXY(self.x, self.y)
Exemple #11
0
def Swap(x1, y1, x2, y2):
    global text

    color_1 = lp_colors.curr_colors[x1][y1]  # Colour for btn #1
    color_2 = lp_colors.curr_colors[x2][y2]  # Colour for btn #2

    btn_1 = buttons[x1][y1]  # btn #1
    btn_2 = buttons[x2][y2]  # btn #2

    Unbind(x1, y1)  # Unbind #1
    if btn_2.script != "":  # If there is a script #2...
        Bind(x1, y1, btn_2, color_2)  # ...bind it to #1
    lp_colors.updateXY(x1, y1)  # Update the colours for btn #1

    Unbind(x2, y2)  # Do the reverse for #2
    if btn_1.script != "":
        Bind(x2, y2, btn_1, color_1)
    lp_colors.updateXY(x2, y2)

    files.layout_changed_since_load = True  # Flag that the layout has changed
Exemple #12
0
    def main_logic(idx):
        nonlocal m_pos
        if threads[x][y].kill.is_set():
            print("[scripts] " + coords + " Recieved exit flag, script exiting...")
            threads[x][y].kill.clear()
            if not is_async:
                running = False
            threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()
            return idx + 1
        line = script_lines[idx].strip()
        if line == "":
            print("[scripts] " + coords + "    Empty line")
        elif line[0] == "-":
            print("[scripts] " + coords + "    Comment: " + line[1:])
        else:
            split_line = line.split(" ")
            if split_line[0] == "STRING":
                type_string = " ".join(split_line[1:])
                print("[scripts] " + coords + "    Type out string " + type_string)
                kb.keyboard.write(type_string)
            elif split_line[0] == "DELAY":
                print("[scripts] " + coords + "    Delay for " + split_line[1] + " seconds")
                
                delay = float(split_line[1])
                while delay > DELAY_EXIT_CHECK:
                    sleep(DELAY_EXIT_CHECK)
                    delay -= DELAY_EXIT_CHECK
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords + " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not is_async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()
                        return -1
                if delay > 0:
                    sleep(delay)
            elif split_line[0] == "TAP":
                key = kb.sp(split_line[1])
                if len(split_line) <= 2:
                    print("[scripts] " + coords + "    Tap key " + split_line[1])
                    kb.tap(key)
                elif len(split_line) <= 3:
                    print("[scripts] " + coords + "    Tap key " + split_line[1] + " " + split_line[2] + " times")
                    
                    taps = int(split_line[2])
                    for tap in range(taps):
                        if threads[x][y].kill.is_set():
                            print("[scripts] " + coords + " Recieved exit flag, script exiting...")
                            threads[x][y].kill.clear()
                            if not is_async:
                                running = False
                            kb.release(split_line[1])
                            threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()
                            return idx + 1
                        kb.tap(key)
                else:
                    print("[scripts] " + coords + "    Tap key " + split_line[1] + " " + split_line[2] + " times for " + str(split_line[3]) + " seconds each")
                    
                    taps = int(split_line[2])
                    delay = float(split_line[3])
                    for tap in range(taps):
                        temp_delay = delay
                        if threads[x][y].kill.is_set():
                            print("[scripts] " + coords + " Recieved exit flag, script exiting...")
                            threads[x][y].kill.clear()
                            if not is_async:
                                running = False
                            kb.release(key)
                            threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()
                            return -1

                        kb.press(key)
                        while temp_delay > DELAY_EXIT_CHECK:
                            sleep(DELAY_EXIT_CHECK)
                            temp_delay -= DELAY_EXIT_CHECK
                            if threads[x][y].kill.is_set():
                                print("[scripts] " + coords + " Recieved exit flag, script exiting...")
                                threads[x][y].kill.clear()
                                if not is_async:
                                    running = False
                                kb.release(key)
                                threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()
                                return -1
                        if temp_delay > 0:
                            sleep(temp_delay)
                        kb.release(key)
            elif split_line[0] == "PRESS":
                print("[scripts] " + coords + "    Press key " + split_line[1])
                key = kb.sp(split_line[1])
                kb.press(key)
            elif split_line[0] == "RELEASE":
                print("[scripts] " + coords + "    Release key " + split_line[1])
                key = kb.sp(split_line[1])
                kb.release(key)
            elif split_line[0] == "WEB":
                link = split_line[1]
                if "http" not in link:
                    link = "http://" + link
                print("[scripts] " + coords + "    Open website " + link + " in default browser")
                webbrowser.open(link)
            elif split_line[0] == "WEB_NEW":
                link = split_line[1]
                if "http" not in link:
                    link = "http://" + link
                print("[scripts] " + coords + "    Open website " + link + " in default browser, try to make a new window")
                webbrowser.open_new(link)
            elif split_line[0] == "SOUND":
                if len(split_line) > 2:
                    print("[scripts] " + coords + "    Play sound file " + split_line[1] + " at volume " + str(split_line[2]))
                    sound.play(split_line[1], float(split_line[2]))
                else:
                    print("[scripts] " + coords + "    Play sound file " + split_line[1])
                    sound.play(split_line[1])
            elif split_line[0] == "WAIT_UNPRESSED":
                print("[scripts] " + coords + "    Wait for script key to be unpressed")
                while lp_events.pressed[x][y]:
                    sleep(DELAY_EXIT_CHECK)
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords + " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not is_async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()
                        return idx + 1
            elif split_line[0] == "M_STORE":
                print("[scripts] " + coords + "    Store mouse position")
                m_pos = ms.getXY()
            elif split_line[0] == "M_RECALL":
                if m_pos == tuple():
                    print("[scripts] " + coords + "    No 'M_STORE' command has been run, cannot do 'M_RECALL'")
                else:
                    print("[scripts] " + coords + "    Recall mouse position " + str(m_pos))
                    ms.setXY(m_pos[0], m_pos[1])
            elif split_line[0] == "M_RECALL_LINE":
                x1, y1 = m_pos

                delay = None
                if len(split_line) > 1:
                    delay = float(split_line[1]) / 1000.0

                skip = 1
                if len(split_line) > 2:
                    skip = int(split_line[2])

                if (delay == None) or (delay <= 0):
                    print("[scripts] " + coords + "    Recall mouse position " + str(m_pos) + " in a line by " + str(skip) + " pixels per step")
                else:
                    print("[scripts] " + coords + "    Recall mouse position " + str(m_pos) + " in a line by " + str(skip) + " pixels per step and wait " + split_line[1] + " milliseconds between each step")

                x_C, y_C = ms.getXY()
                points = ms.line_coords(x_C, y_C, x1, y1)
                for x_M, y_M in points[::skip]:
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords + " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not is_async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()
                        return -1
                    ms.setXY(x_M, y_M)
                    if (delay != None) and (delay > 0):
                        temp_delay = delay
                        while temp_delay > DELAY_EXIT_CHECK:
                            sleep(DELAY_EXIT_CHECK)
                            temp_delay -= DELAY_EXIT_CHECK
                            if threads[x][y].kill.is_set():
                                print("[scripts] " + coords + " Recieved exit flag, script exiting...")
                                threads[x][y].kill.clear()
                                if not is_async:
                                    running = False
                                threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()
                                return -1
                        if temp_delay > 0:
                            sleep(temp_delay)
            elif split_line[0] == "M_MOVE":
                if len(split_line) >= 3:
                    print("[scripts] " + coords + "    Relative mouse movement (" + split_line[1] + ", " + str(split_line[2]) + ")")
                    ms.moveXY(float(split_line[1]), float(split_line[2]))
                else:
                    print("[scripts] " + coords + "    Both X and Y are required for mouse movement, skipping...")
            elif split_line[0] == "M_SET":
                if len(split_line) >= 3:
                    print("[scripts] " + coords + "    Set mouse position to (" + split_line[1] + ", " + str(split_line[2]) + ")")
                    ms.setXY(float(split_line[1]), float(split_line[2]))
                else:
                    print("[scripts] " + coords + "    Both X and Y are required for mouse positioning, skipping...")
            elif split_line[0] == "M_SCROLL":
                if len(split_line) > 2:
                    print("[scripts] " + coords + "    Scroll (" + split_line[1] + ", " + split_line[2] + ")")
                    ms.scroll(float(split_line[2]), float(split_line[1]))
                else:
                    print("[scripts] " + coords + "    Scroll " + split_line[1])
                    ms.scroll(0, float(split_line[1]))
            elif split_line[0] == "M_LINE":
                x1 = int(split_line[1])
                y1 = int(split_line[2])
                x2 = int(split_line[3])
                y2 = int(split_line[4])

                delay = None
                if len(split_line) > 5:
                    delay = float(split_line[5]) / 1000.0

                skip = 1
                if len(split_line) > 6:
                    skip = int(split_line[6])

                if (delay == None) or (delay <= 0):
                    print("[scripts] " + coords + "    Mouse line from (" + split_line[1] + ", " + split_line[2] + ") to (" + split_line[3] + ", " + split_line[4] + ") by " + str(skip) + " pixels per step")
                else:
                    print("[scripts] " + coords + "    Mouse line from (" + split_line[1] + ", " + split_line[2] + ") to (" + split_line[3] + ", " + split_line[4] + ") by " + str(skip) + " pixels per step and wait " + split_line[5] + " milliseconds between each step")

                points = ms.line_coords(x1, y1, x2, y2)
                for x_M, y_M in points[::skip]:
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords + " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not is_async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()
                        return -1
                    ms.setXY(x_M, y_M)
                    if (delay != None) and (delay > 0):
                        temp_delay = delay
                        while temp_delay > DELAY_EXIT_CHECK:
                            sleep(DELAY_EXIT_CHECK)
                            temp_delay -= DELAY_EXIT_CHECK
                            if threads[x][y].kill.is_set():
                                print("[scripts] " + coords + " Recieved exit flag, script exiting...")
                                threads[x][y].kill.clear()
                                if not is_async:
                                    running = False
                                threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()
                                return -1
                        if temp_delay > 0:
                            sleep(temp_delay)
            elif split_line[0] == "M_LINE_MOVE":
                x1 = int(split_line[1])
                y1 = int(split_line[2])

                delay = None
                if len(split_line) > 3:
                    delay = float(split_line[3]) / 1000.0

                skip = 1
                if len(split_line) > 4:
                    skip = int(split_line[4])

                if (delay == None) or (delay <= 0):
                    print("[scripts] " + coords + "    Mouse line move relative (" + split_line[1] + ", " + split_line[2] + ") by " + str(skip) + " pixels per step")
                else:
                    print("[scripts] " + coords + "    Mouse line move relative (" + split_line[1] + ", " + split_line[2] + ") by " + str(skip) + " pixels per step and wait " + split_line[3] + " milliseconds between each step")

                x_C, y_C = ms.getXY()
                x_N, y_N = x_C + x1, y_C + y1
                points = ms.line_coords(x_C, y_C, x_N, y_N)
                for x_M, y_M in points[::skip]:
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords + " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not is_async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()
                        return -1
                    ms.setXY(x_M, y_M)
                    if (delay != None) and (delay > 0):
                        temp_delay = delay
                        while temp_delay > DELAY_EXIT_CHECK:
                            sleep(DELAY_EXIT_CHECK)
                            temp_delay -= DELAY_EXIT_CHECK
                            if threads[x][y].kill.is_set():
                                print("[scripts] " + coords + " Recieved exit flag, script exiting...")
                                threads[x][y].kill.clear()
                                if not is_async:
                                    running = False
                                threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()
                                return -1
                        if temp_delay > 0:
                            sleep(temp_delay)
            elif split_line[0] == "M_LINE_SET":
                x1 = int(split_line[1])
                y1 = int(split_line[2])

                delay = None
                if len(split_line) > 3:
                    delay = float(split_line[3]) / 1000.0

                skip = 1
                if len(split_line) > 4:
                    skip = int(split_line[4])

                if (delay == None) or (delay <= 0):
                    print("[scripts] " + coords + "    Mouse line set (" + split_line[1] + ", " + split_line[2] + ") by " + str(skip) + " pixels per step")
                else:
                    print("[scripts] " + coords + "    Mouse line set (" + split_line[1] + ", " + split_line[2] + ") by " + str(skip) + " pixels per step and wait " + split_line[3] + " milliseconds between each step")

                x_C, y_C = ms.getXY()
                points = ms.line_coords(x_C, y_C, x1, y1)
                for x_M, y_M in points[::skip]:
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords + " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not is_async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()
                        return -1
                    ms.setXY(x_M, y_M)
                    if (delay != None) and (delay > 0):
                        temp_delay = delay
                        while temp_delay > DELAY_EXIT_CHECK:
                            sleep(DELAY_EXIT_CHECK)
                            temp_delay -= DELAY_EXIT_CHECK
                            if threads[x][y].kill.is_set():
                                print("[scripts] " + coords + " Recieved exit flag, script exiting...")
                                threads[x][y].kill.clear()
                                if not is_async:
                                    running = False
                                threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()
                                return -1
                        if temp_delay > 0:
                            sleep(temp_delay)
            elif split_line[0] == "LABEL":
               print("[scripts] " + coords + "    Label: " + split_line[1])
               return idx + 1
            elif split_line[0] == "IF_PRESSED_GOTO_LABEL":
                print("[scripts] " + coords + "    If key is pressed goto LABEL " + split_line[1])
                if lp_events.pressed[x][y]:
                    return labels[split_line[1]]
            elif split_line[0] == "IF_UNPRESSED_GOTO_LABEL":
                print("[scripts] " + coords + "    If key is not pressed goto LABEL " + split_line[1])
                if not lp_events.pressed[x][y]:
                    return labels[split_line[1]]
            elif split_line[0] == "GOTO_LABEL":
                print("[scripts] " + coords + "    Goto LABEL " + split_line[1])
                return labels[split_line[1]]
            elif split_line[0] == "REPEAT_LABEL":
                print("[scripts] " + coords + "    Repeat LABEL " + split_line[1] + " " + split_line[2] + " times max")
                if idx in repeats:
                    if repeats[idx] > 0:
                        print("[scripts] " + coords + "        " + str(repeats[idx]) + " repeats left.")
                        repeats[idx] -= 1
                        return labels[split_line[1]]
                    else:
                        print("[scripts] " + coords + "        No repeats left, not repeating.")
                else:
                    repeats[idx] = int(split_line[2])
                    repeats_original[idx] = int(split_line[2])
                    print("[scripts] " + coords + "        " + str(repeats[idx]) + " repeats left.")
                    repeats[idx] -= 1
                    return labels[split_line[1]]
            elif split_line[0] == "IF_PRESSED_REPEAT_LABEL":
                print("[scripts] " + coords + "    If key is pressed repeat LABEL " + split_line[1] + " " + split_line[2] + " times max")
                if lp_events.pressed[x][y]:
                    if idx in repeats:
                        if repeats[idx] > 0:
                            print("[scripts] " + coords + "        " + str(repeats[idx]) + " repeats left.")
                            repeats[idx] -= 1
                            return labels[split_line[1]]
                        else:
                            print("[scripts] " + coords + "        No repeats left, not repeating.")
                    else:
                        repeats[idx] = int(split_line[2])
                        print("[scripts] " + coords + "        " + str(repeats[idx]) + " repeats left.")
                        repeats[idx] -= 1
                        return labels[split_line[1]]
            elif split_line[0] == "IF_UNPRESSED_REPEAT_LABEL":
                print("[scripts] " + coords + "    If key is not pressed repeat LABEL " + split_line[1] + " " + split_line[2] + " times max")
                if not lp_events.pressed[x][y]:
                    if idx in repeats:
                        if repeats[idx] > 0:
                            print("[scripts] " + coords + "        " + str(repeats[idx]) + " repeats left.")
                            repeats[idx] -= 1
                            return labels[split_line[1]]
                        else:
                            print("[scripts] " + coords + "        No repeats left, not repeating.")
                    else:
                        repeats[idx] = int(split_line[2])
                        print("[scripts] " + coords + "        " + str(repeats[idx]) + " repeats left.")
                        repeats[idx] -= 1
                        return labels[split_line[1]]
            elif split_line[0] == "@SIMPLE":
                print("[scripts] " + coords + "    Simple keybind: " + split_line[1])
                
                #PRESS
                key = kb.sp(split_line[1])
                kb.press(key)
                
                #WAIT_UNPRESSED
                while lp_events.pressed[x][y]:
                    sleep(DELAY_EXIT_CHECK)
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords + " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not is_async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()
                        return idx + 1
                #RELEASE
                kb.release(key)
            elif split_line[0] == "OPEN":
                path_name = " ".join(split_line[1:])
                print("[scripts] " + coords + "    Open file or folder " + path_name)
                files.open_file_folder(path_name)
            elif split_line[0] == "RELEASE_ALL":
                print("[scripts] " + coords + "    Release all keys")
                kb.release_all()
            elif split_line[0] == "RESET_REPEATS":
                print("[scripts] " + coords + "    Reset all repeats")
                for i in repeats:
                    repeats[i] = repeats_original[i]
            elif split_line[0] == "LOAD_LAYOUT":
                files.load_layout(split_line[1])
            elif split_line[0] == "SET_COLOR":
                color = split_line[1]
                try:
                    color = int(color)
                except:
                    color = color.split(",")
                    if len(color) != 3:
                        print("[scripts] " + coords + "    Invalid color for command SET_COLOR")
                        return idx + 1
                    try:
                        color = [int(color[0]), int(color[1]), int(color[2])]
                    except:
                        print("[scripts] " + coords + "    Invalid color for command SET_COLOR")
                        return idx + 1
                    if color[0] > 50 or color[1] > 50 or color[2] > 50:
                        print("[scripts] " + coords + "    Invalid color for command SET_COLOR")
                        return idx + 1
                finally:
                    x1 = x
                    y1 = y
                    if len(split_line) > 3:
                        x1 = int(split_line[2])
                        x2 = int(split_line[3])
                    lp_colors.setXY(x1, y1, color, "solid")
                    lp_colors.updateXY(x1, y1)
            elif split_line[0] == "TOGGLE":
                new = not toggles[x][y]
                if len(split_line) > 1:
                    new = split_line[1] == "on"
                toggles[x][y] = new
            elif split_line[0] == "IF_TOGGLED_GOTO_LABEL":
                if toggles[x][y]:
                    return labels[split_line[1]]
            elif split_line[0] == "EXIT":
                return -1
            elif split_line[0] == "SELECT_WINDOW":
                windows.select_window(split_line[1])
            elif split_line[0] == "SET_COLOR_MODE":
                try:
                    color = int(split_line[1])
                except:
                    print("[scripts] " + coords + "    Invalid color for command SET_COLOR_MODE")
                    return idx + 1
                if color > 50:
                    print("[scripts] " + coords + "    Invalid color for command SET_COLOR_MODE")
                    return idx + 1
                mode = split_line[2]
                if mode != "solid" and mode != "flash" and mode != "pulse":
                    print("[scripts] " + coords + "    Invalid mode for command SET_COLOR_MODE")
                    return idx + 1
                x1 = x
                y1 = y
                if len(split_line) > 4:
                    x1 = int(split_line[3])
                    x2 = int(split_line[4])
                lp_colors.setXY(x1, y1, color, mode)
                lp_colors.updateXY(x1, y1)
            else:
                print("[scripts] " + coords + "    Invalid command: " + split_line[0] + ", skipping...")
        return idx + 1
Exemple #13
0
def unbind(x, y):
    global press_funcs
    press_funcs[x][y] = unbound_press
    lp_colors.setXY(x, y, lp_colors.BLACK)
    lp_colors.updateXY(x, y)
Exemple #14
0
def run_script(script_str, x, y):
    global running
    global exit

    lp_colors.updateXY(x, y)
    coords = "(" + str(x) + ", " + str(y) + ")"

    script_lines = script_str.split("\n")

    async = False
    if script_lines[0].split(" ")[0] == "@ASYNC":
        async = True
        temp = script_lines.pop(0)
    else:
        running = True

    print("[scripts] " + coords + " Now running script...")
    for line in script_lines:
        if threads[x][y].kill.is_set():
            print("[scripts] " + coords +
                  " Recieved exit flag, script exiting...")
            threads[x][y].kill.clear()
            if not async:
                running = False
            threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY,
                            (x, y)).start()
            return
        line = line.strip()
        if line == "":
            print("[scripts] " + coords + "    Empty line")
        elif line[0] == "-":
            print("[scripts] " + coords + "    Comment " + line[1:])
        else:
            split_line = line.split(" ")
            if split_line[0] == "STRING":
                type_string = " ".join(split_line[1:])
                print("[scripts] " + coords + "    Type out string " +
                      type_string)
                keyboard.controller.type(type_string)
            elif split_line[0] == "DELAY":
                delay = None
                try:
                    delay = float(split_line[1])
                except:
                    print("[scripts] " + coords +
                          "     Invalid time to delay, skipping...")

                if delay != None:
                    print("[scripts] " + coords + "    Delay for " +
                          split_line[1] + " seconds")
                    while delay > DELAY_EXIT_CHECK:
                        sleep(DELAY_EXIT_CHECK)
                        delay -= DELAY_EXIT_CHECK
                        if threads[x][y].kill.is_set():
                            print("[scripts] " + coords +
                                  " Recieved exit flag, script exiting...")
                            threads[x][y].kill.clear()
                            if not async:
                                running = False
                            threading.Timer(EXIT_UPDATE_DELAY,
                                            lp_colors.updateXY,
                                            (x, y)).start()
                            return
                    if delay > 0:
                        sleep(delay)
            elif split_line[0] == "TAP":
                if len(split_line) < 3:
                    print("[scripts] " + coords + "    Tap key " +
                          split_line[1])
                    keyboard.tap(split_line[1])
                else:
                    delay = None
                    try:
                        delay = float(split_line[2])
                    except:
                        print("[scripts] " + coords +
                              "     Invalid time to tap, skipping...")

                    if delay != None:
                        print("[scripts] " + coords + "    Tap key " +
                              split_line[1] + " for " + str(split_line[2]) +
                              " seconds")
                        keyboard.press(split_line[1])

                        while delay > DELAY_EXIT_CHECK:
                            sleep(DELAY_EXIT_CHECK)
                            delay -= DELAY_EXIT_CHECK
                            if threads[x][y].kill.is_set():
                                print("[scripts] " + coords +
                                      " Recieved exit flag, script exiting...")
                                threads[x][y].kill.clear()
                                if not async:
                                    running = False
                                keyboard.release(split_line[1])
                                threading.Timer(EXIT_UPDATE_DELAY,
                                                lp_colors.updateXY,
                                                (x, y)).start()
                                return
                        if delay > 0:
                            sleep(delay)

                        keyboard.release(split_line[1])

            elif split_line[0] == "PRESS":
                print("[scripts] " + coords + "    Press key " + split_line[1])
                keyboard.press(split_line[1])
            elif split_line[0] == "RELEASE":
                print("[scripts] " + coords + " (" + str(x) + ", " + str(y) +
                      ")    Release key " + split_line[1])
                keyboard.release(split_line[1])
            elif split_line[0] == "SP_TAP":
                if keyboard.sp(split_line[1]) != None:
                    delay = None
                    if len(split_line) < 3:
                        print("[scripts] " + coords + "    Tap special key " +
                              split_line[1])
                        keyboard.tap(keyboard.sp(split_line[1]))
                    else:
                        delay = None
                        try:
                            delay = float(split_line[2])
                        except:
                            print(
                                "[scripts] " + coords +
                                "     Invalid time to special tap, skipping..."
                            )

                        if delay != None:
                            print("[scripts] " + coords +
                                  "    Tap special key " + split_line[1] +
                                  " for " + str(split_line[2]) + " seconds")

                            key = keyboard.sp(split_line[1])

                            keyboard.press(key)

                            while delay > DELAY_EXIT_CHECK:
                                sleep(DELAY_EXIT_CHECK)
                                delay -= DELAY_EXIT_CHECK
                                if threads[x][y].kill.is_set():
                                    print(
                                        "[scripts] " + coords +
                                        " Recieved exit flag, script exiting..."
                                    )
                                    threads[x][y].kill.clear()
                                    if not async:
                                        running = False
                                    keyboard.release(key)
                                    threading.Timer(EXIT_UPDATE_DELAY,
                                                    lp_colors.updateXY,
                                                    (x, y)).start()
                                    return
                            if delay > 0:
                                sleep(delay)

                            keyboard.release(key)
                else:
                    print("[scripts] " + coords +
                          "    Invalid special character to tap: " +
                          split_line[1] + ", skipping...")
            elif split_line[0] == "SP_PRESS":
                if keyboard.sp(split_line[1]) != None:
                    print("[scripts] " + coords + "    Press special key " +
                          split_line[1])
                    keyboard.press(keyboard.sp(split_line[1]))
                else:
                    print("[scripts] " + coords +
                          "    Invalid special character to press: " +
                          split_line[1] + ", skipping...")
            elif split_line[0] == "SP_RELEASE":
                if keyboard.sp(split_line[1]) != None:
                    print("[scripts] " + coords + "    Release special key " +
                          split_line[1])
                    keyboard.release(keyboard.sp(split_line[1]))
                else:
                    print("[scripts] " + coords +
                          "    Invalid special character to release: " +
                          split_line[1] + ", skipping...")
            elif split_line[0] == "WEB":
                link = split_line[1]
                if "http" not in link:
                    link = "http://" + link
                print("[scripts] " + coords + "    Open website " + link +
                      " in default browser")
                webbrowser.open(link)
            elif split_line[0] == "WEB_NEW":
                link = split_line[1]
                if "http" not in link:
                    link = "http://" + link
                print("[scripts] " + coords + "    Open website " + link +
                      " in default browser, try to make a new window")
                webbrowser.open_new(link)
            elif split_line[0] == "SOUND":
                if len(split_line) > 2:
                    print("[scripts] " + coords + "    Play sound file " +
                          split_line[1] + " at volume " + str(split_line[2]))
                    sound.play(split_line[1], float(split_line[2]))
                else:
                    print("[scripts] " + coords + "    Play sound file " +
                          split_line[1])
                    sound.play(split_line[1])
            elif split_line[0] == "WAIT_UNPRESSED":
                print("[scripts] " + coords +
                      "    Wait for script key to be unpressed")
                while lp_events.pressed[x][y]:
                    sleep(DELAY_EXIT_CHECK)
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords +
                              " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY,
                                        (x, y)).start()
                        return
            else:
                print("[scripts] " + coords + "    Invalid command: " +
                      split_line[0] + ", skipping...")
    print("[scripts] (" + str(x) + ", " + str(y) + ") Script done running.")

    if not async:
        running = False
    threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()
Exemple #15
0
    def Run_script(self):
        # @@@ maybe check we're not a subroutine (subroutines should not use this)
        lp_colors.updateXY(self.x, self.y)

        if self.Validate_script() != True:
            return

        print("[scripts] " + self.coords + " Now running script...")

        self.running(not self.is_async)

        cmd_txt = "RESET_REPEATS"  # before we run, we want to rest loop counters
        if cmd_txt in VALID_COMMANDS:
            command = VALID_COMMANDS[cmd_txt]
            command.Run(self, -1, [cmd_txt])

        if len(self.script_lines) > 0:
            self.running(True)

            def Main_logic(idx):  # the main logic to run a line of a script
                if self.Check_kill(
                ):  # first check to see if we've been asked to die
                    return idx + 1  # we just return the next line, @@@ returning -1 is better

                line = self.Line(idx)  # get the line of the script

                # Handle completely blank lines
                if line == "":
                    return idx + 1

                # Get the command text
                cmd_txt = self.Split_cmd_text(
                    line)  # Just get the command name leaving the line intact

                # Now get the command object
                if cmd_txt in VALID_COMMANDS:  # make sure it's a valid command
                    command = VALID_COMMANDS[
                        cmd_txt]  # get the command object that will execute the command

                    split_line = self.Split_text(
                        command, cmd_txt, line
                    )  # get all the parameters as a list, including quoted parameters

                    if type(
                            split_line
                    ) == tuple:  # bad news if we get a tuple rather than a list
                        print("[scripts] " + self.coords + "    Error in: '" +
                              cmd_txt + "' - " + split_line[0] +
                              ", skipping...")
                    else:
                        # now run the command
                        return command.Run(
                            self, idx, split_line
                        )  # otherwise we can ask the command to execute itself with the parameters we've parsed out
                else:
                    print("[scripts] " + self.coords +
                          "    Invalid command: '" + cmd_txt +
                          "', skipping...")

                return idx + 1  # defaut action is to ask for the next line

            run = True  # flag that we're running
            idx = 0  # point at the first line
            while run:  # and while we're still running
                idx = Main_logic(idx)  # run the current line
                if (idx < 0) or (idx >= len(
                        self.script_lines)):  # if the next line isn't valid
                    run = False  # then we're not going to keep running!

            if not self.is_async:  # async commands don't just end
                self.running(False)  # they have to say they're not running

            threading.Timer(
                EXIT_UPDATE_DELAY, lp_colors.updateXY,
                (self.x, self.y
                 )).start()  # queue up a request to update the button colours

        print("[scripts] " + self.coords +
              " Script ended.")  # and print (log?) that the script is complete
Exemple #16
0
def run_script(script_str, x, y):
    global running
    global exit

    lp_colors.updateXY(x, y)
    coords = "(" + str(x) + ", " + str(y) + ")"

    script_lines = script_str.split("\n")

    is_async = False
    if script_lines[0].split(" ")[0] in ASYNC_HEADERS:
        is_async = True
    else:
        running = True

    if script_lines[0].split(" ")[0] == "@ASYNC":
        temp = script_lines.pop(0)

    print("[scripts] " + coords + " Now running script...")

    #parse labels
    labels = dict()
    for idx, line in enumerate(script_lines):
        line = line.strip()
        split_line = line.split(" ")
        if split_line[0] == "LABEL":
            labels[split_line[1]] = idx

    #prepare repeat counter {idx:repeats_left}
    repeats = dict()
    repeats_original = dict()

    m_pos = ()

    def main_logic(idx):
        nonlocal m_pos
        if threads[x][y].kill.is_set():
            print("[scripts] " + coords +
                  " Recieved exit flag, script exiting...")
            threads[x][y].kill.clear()
            if not is_async:
                running = False
            threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY,
                            (x, y)).start()
            return idx + 1
        line = script_lines[idx].strip()
        if line == "":
            print("[scripts] " + coords + "    Empty line")
        elif line[0] == "-":
            print("[scripts] " + coords + "    Comment: " + line[1:])
        else:
            split_line = line.split(" ")
            if split_line[0] == "STRING":
                type_string = " ".join(split_line[1:])
                print("[scripts] " + coords + "    Type out string " +
                      type_string)
                kb.keyboard.write(type_string)
            elif split_line[0] == "DELAY":
                print("[scripts] " + coords + "    Delay for " +
                      split_line[1] + " seconds")

                delay = float(split_line[1])
                while delay > DELAY_EXIT_CHECK:
                    sleep(DELAY_EXIT_CHECK)
                    delay -= DELAY_EXIT_CHECK
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords +
                              " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not is_async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY,
                                        (x, y)).start()
                        return -1
                if delay > 0:
                    sleep(delay)
            elif split_line[0] == "TAP":
                key = kb.sp(split_line[1])
                if len(split_line) <= 2:
                    print("[scripts] " + coords + "    Tap key " +
                          split_line[1])
                    kb.tap(key)
                elif len(split_line) <= 3:
                    print("[scripts] " + coords + "    Tap key " +
                          split_line[1] + " " + split_line[2] + " times")

                    taps = int(split_line[2])
                    for tap in range(taps):
                        if threads[x][y].kill.is_set():
                            print("[scripts] " + coords +
                                  " Recieved exit flag, script exiting...")
                            threads[x][y].kill.clear()
                            if not is_async:
                                running = False
                            kb.release(split_line[1])
                            threading.Timer(EXIT_UPDATE_DELAY,
                                            lp_colors.updateXY,
                                            (x, y)).start()
                            return idx + 1
                        kb.tap(key)
                else:
                    print("[scripts] " + coords + "    Tap key " +
                          split_line[1] + " " + split_line[2] + " times for " +
                          str(split_line[3]) + " seconds each")

                    taps = int(split_line[2])
                    delay = float(split_line[3])
                    for tap in range(taps):
                        temp_delay = delay
                        if threads[x][y].kill.is_set():
                            print("[scripts] " + coords +
                                  " Recieved exit flag, script exiting...")
                            threads[x][y].kill.clear()
                            if not is_async:
                                running = False
                            kb.release(key)
                            threading.Timer(EXIT_UPDATE_DELAY,
                                            lp_colors.updateXY,
                                            (x, y)).start()
                            return -1

                        kb.press(key)
                        while temp_delay > DELAY_EXIT_CHECK:
                            sleep(DELAY_EXIT_CHECK)
                            temp_delay -= DELAY_EXIT_CHECK
                            if threads[x][y].kill.is_set():
                                print("[scripts] " + coords +
                                      " Recieved exit flag, script exiting...")
                                threads[x][y].kill.clear()
                                if not is_async:
                                    running = False
                                kb.release(key)
                                threading.Timer(EXIT_UPDATE_DELAY,
                                                lp_colors.updateXY,
                                                (x, y)).start()
                                return -1
                        if temp_delay > 0:
                            sleep(temp_delay)
                        kb.release(key)
            elif split_line[0] == "PRESS":
                print("[scripts] " + coords + "    Press key " + split_line[1])
                key = kb.sp(split_line[1])
                kb.press(key)
            elif split_line[0] == "RELEASE":
                print("[scripts] " + coords + "    Release key " +
                      split_line[1])
                key = kb.sp(split_line[1])
                kb.release(key)
            elif split_line[0] == "WEB":
                link = split_line[1]
                if "http" not in link:
                    link = "http://" + link
                print("[scripts] " + coords + "    Open website " + link +
                      " in default browser")
                webbrowser.open(link)
            elif split_line[0] == "WEB_NEW":
                link = split_line[1]
                if "http" not in link:
                    link = "http://" + link
                print("[scripts] " + coords + "    Open website " + link +
                      " in default browser, try to make a new window")
                webbrowser.open_new(link)
            elif split_line[0] == "SOUND":
                if len(split_line) > 2:
                    print("[scripts] " + coords + "    Play sound file " +
                          split_line[1] + " at volume " + str(split_line[2]))
                    sound.play(split_line[1], float(split_line[2]))
                else:
                    print("[scripts] " + coords + "    Play sound file " +
                          split_line[1])
                    sound.play(split_line[1])
            elif split_line[0] == "WAIT_UNPRESSED":
                print("[scripts] " + coords +
                      "    Wait for script key to be unpressed")
                while lp_events.pressed[x][y]:
                    sleep(DELAY_EXIT_CHECK)
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords +
                              " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not is_async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY,
                                        (x, y)).start()
                        return idx + 1
            elif split_line[0] == "M_STORE":
                print("[scripts] " + coords + "    Store mouse position")
                m_pos = ms.getXY()
            elif split_line[0] == "M_RECALL":
                if m_pos == tuple():
                    print(
                        "[scripts] " + coords +
                        "    No 'M_STORE' command has been run, cannot do 'M_RECALL'"
                    )
                else:
                    print("[scripts] " + coords +
                          "    Recall mouse position " + str(m_pos))
                    ms.setXY(m_pos[0], m_pos[1])
            elif split_line[0] == "M_RECALL_LINE":
                x1, y1 = m_pos

                delay = None
                if len(split_line) > 1:
                    delay = float(split_line[1]) / 1000.0

                skip = 1
                if len(split_line) > 2:
                    skip = int(split_line[2])

                if (delay == None) or (delay <= 0):
                    print("[scripts] " + coords +
                          "    Recall mouse position " + str(m_pos) +
                          " in a line by " + str(skip) + " pixels per step")
                else:
                    print("[scripts] " + coords +
                          "    Recall mouse position " + str(m_pos) +
                          " in a line by " + str(skip) +
                          " pixels per step and wait " + split_line[1] +
                          " milliseconds between each step")

                x_C, y_C = ms.getXY()
                points = ms.line_coords(x_C, y_C, x1, y1)
                for x_M, y_M in points[::skip]:
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords +
                              " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not is_async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY,
                                        (x, y)).start()
                        return -1
                    ms.setXY(x_M, y_M)
                    if (delay != None) and (delay > 0):
                        temp_delay = delay
                        while temp_delay > DELAY_EXIT_CHECK:
                            sleep(DELAY_EXIT_CHECK)
                            temp_delay -= DELAY_EXIT_CHECK
                            if threads[x][y].kill.is_set():
                                print("[scripts] " + coords +
                                      " Recieved exit flag, script exiting...")
                                threads[x][y].kill.clear()
                                if not is_async:
                                    running = False
                                threading.Timer(EXIT_UPDATE_DELAY,
                                                lp_colors.updateXY,
                                                (x, y)).start()
                                return -1
                        if temp_delay > 0:
                            sleep(temp_delay)
            elif split_line[0] == "M_MOVE":
                if len(split_line) >= 3:
                    print("[scripts] " + coords +
                          "    Relative mouse movement (" + split_line[1] +
                          ", " + str(split_line[2]) + ")")
                    ms.moveXY(float(split_line[1]), float(split_line[2]))
                else:
                    print(
                        "[scripts] " + coords +
                        "    Both X and Y are required for mouse movement, skipping..."
                    )
            elif split_line[0] == "M_SET":
                if len(split_line) >= 3:
                    print("[scripts] " + coords +
                          "    Set mouse position to (" + split_line[1] +
                          ", " + str(split_line[2]) + ")")
                    ms.setXY(float(split_line[1]), float(split_line[2]))
                else:
                    print(
                        "[scripts] " + coords +
                        "    Both X and Y are required for mouse positioning, skipping..."
                    )
            elif split_line[0] == "M_SCROLL":
                if len(split_line) > 2:
                    print("[scripts] " + coords + "    Scroll (" +
                          split_line[1] + ", " + split_line[2] + ")")
                    ms.scroll(float(split_line[2]), float(split_line[1]))
                else:
                    print("[scripts] " + coords + "    Scroll " +
                          split_line[1])
                    ms.scroll(0, float(split_line[1]))
            elif split_line[0] == "M_LINE":
                x1 = int(split_line[1])
                y1 = int(split_line[2])
                x2 = int(split_line[3])
                y2 = int(split_line[4])

                delay = None
                if len(split_line) > 5:
                    delay = float(split_line[5]) / 1000.0

                skip = 1
                if len(split_line) > 6:
                    skip = int(split_line[6])

                if (delay == None) or (delay <= 0):
                    print("[scripts] " + coords + "    Mouse line from (" +
                          split_line[1] + ", " + split_line[2] + ") to (" +
                          split_line[3] + ", " + split_line[4] + ") by " +
                          str(skip) + " pixels per step")
                else:
                    print("[scripts] " + coords + "    Mouse line from (" +
                          split_line[1] + ", " + split_line[2] + ") to (" +
                          split_line[3] + ", " + split_line[4] + ") by " +
                          str(skip) + " pixels per step and wait " +
                          split_line[5] + " milliseconds between each step")

                points = ms.line_coords(x1, y1, x2, y2)
                for x_M, y_M in points[::skip]:
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords +
                              " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not is_async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY,
                                        (x, y)).start()
                        return -1
                    ms.setXY(x_M, y_M)
                    if (delay != None) and (delay > 0):
                        temp_delay = delay
                        while temp_delay > DELAY_EXIT_CHECK:
                            sleep(DELAY_EXIT_CHECK)
                            temp_delay -= DELAY_EXIT_CHECK
                            if threads[x][y].kill.is_set():
                                print("[scripts] " + coords +
                                      " Recieved exit flag, script exiting...")
                                threads[x][y].kill.clear()
                                if not is_async:
                                    running = False
                                threading.Timer(EXIT_UPDATE_DELAY,
                                                lp_colors.updateXY,
                                                (x, y)).start()
                                return -1
                        if temp_delay > 0:
                            sleep(temp_delay)
            elif split_line[0] == "M_LINE_MOVE":
                x1 = int(split_line[1])
                y1 = int(split_line[2])

                delay = None
                if len(split_line) > 3:
                    delay = float(split_line[3]) / 1000.0

                skip = 1
                if len(split_line) > 4:
                    skip = int(split_line[4])

                if (delay == None) or (delay <= 0):
                    print("[scripts] " + coords +
                          "    Mouse line move relative (" + split_line[1] +
                          ", " + split_line[2] + ") by " + str(skip) +
                          " pixels per step")
                else:
                    print("[scripts] " + coords +
                          "    Mouse line move relative (" + split_line[1] +
                          ", " + split_line[2] + ") by " + str(skip) +
                          " pixels per step and wait " + split_line[3] +
                          " milliseconds between each step")

                x_C, y_C = ms.getXY()
                x_N, y_N = x_C + x1, y_C + y1
                points = ms.line_coords(x_C, y_C, x_N, y_N)
                for x_M, y_M in points[::skip]:
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords +
                              " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not is_async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY,
                                        (x, y)).start()
                        return -1
                    ms.setXY(x_M, y_M)
                    if (delay != None) and (delay > 0):
                        temp_delay = delay
                        while temp_delay > DELAY_EXIT_CHECK:
                            sleep(DELAY_EXIT_CHECK)
                            temp_delay -= DELAY_EXIT_CHECK
                            if threads[x][y].kill.is_set():
                                print("[scripts] " + coords +
                                      " Recieved exit flag, script exiting...")
                                threads[x][y].kill.clear()
                                if not is_async:
                                    running = False
                                threading.Timer(EXIT_UPDATE_DELAY,
                                                lp_colors.updateXY,
                                                (x, y)).start()
                                return -1
                        if temp_delay > 0:
                            sleep(temp_delay)
            elif split_line[0] == "M_LINE_SET":
                x1 = int(split_line[1])
                y1 = int(split_line[2])

                delay = None
                if len(split_line) > 3:
                    delay = float(split_line[3]) / 1000.0

                skip = 1
                if len(split_line) > 4:
                    skip = int(split_line[4])

                if (delay == None) or (delay <= 0):
                    print("[scripts] " + coords + "    Mouse line set (" +
                          split_line[1] + ", " + split_line[2] + ") by " +
                          str(skip) + " pixels per step")
                else:
                    print("[scripts] " + coords + "    Mouse line set (" +
                          split_line[1] + ", " + split_line[2] + ") by " +
                          str(skip) + " pixels per step and wait " +
                          split_line[3] + " milliseconds between each step")

                x_C, y_C = ms.getXY()
                points = ms.line_coords(x_C, y_C, x1, y1)
                for x_M, y_M in points[::skip]:
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords +
                              " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not is_async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY,
                                        (x, y)).start()
                        return -1
                    ms.setXY(x_M, y_M)
                    if (delay != None) and (delay > 0):
                        temp_delay = delay
                        while temp_delay > DELAY_EXIT_CHECK:
                            sleep(DELAY_EXIT_CHECK)
                            temp_delay -= DELAY_EXIT_CHECK
                            if threads[x][y].kill.is_set():
                                print("[scripts] " + coords +
                                      " Recieved exit flag, script exiting...")
                                threads[x][y].kill.clear()
                                if not is_async:
                                    running = False
                                threading.Timer(EXIT_UPDATE_DELAY,
                                                lp_colors.updateXY,
                                                (x, y)).start()
                                return -1
                        if temp_delay > 0:
                            sleep(temp_delay)
            elif split_line[0] == "LABEL":
                print("[scripts] " + coords + "    Label: " + split_line[1])
                return idx + 1
            elif split_line[0] == "IF_PRESSED_GOTO_LABEL":
                print("[scripts] " + coords +
                      "    If key is pressed goto LABEL " + split_line[1])
                if lp_events.pressed[x][y]:
                    return labels[split_line[1]]
            elif split_line[0] == "IF_UNPRESSED_GOTO_LABEL":
                print("[scripts] " + coords +
                      "    If key is not pressed goto LABEL " + split_line[1])
                if not lp_events.pressed[x][y]:
                    return labels[split_line[1]]
            elif split_line[0] == "GOTO_LABEL":
                print("[scripts] " + coords + "    Goto LABEL " +
                      split_line[1])
                return labels[split_line[1]]
            elif split_line[0] == "REPEAT_LABEL":
                print("[scripts] " + coords + "    Repeat LABEL " +
                      split_line[1] + " " + split_line[2] + " times max")
                if idx in repeats:
                    if repeats[idx] > 0:
                        print("[scripts] " + coords + "        " +
                              str(repeats[idx]) + " repeats left.")
                        repeats[idx] -= 1
                        return labels[split_line[1]]
                    else:
                        print("[scripts] " + coords +
                              "        No repeats left, not repeating.")
                else:
                    repeats[idx] = int(split_line[2])
                    repeats_original[idx] = int(split_line[2])
                    print("[scripts] " + coords + "        " +
                          str(repeats[idx]) + " repeats left.")
                    repeats[idx] -= 1
                    return labels[split_line[1]]
            elif split_line[0] == "IF_PRESSED_REPEAT_LABEL":
                print("[scripts] " + coords +
                      "    If key is pressed repeat LABEL " + split_line[1] +
                      " " + split_line[2] + " times max")
                if lp_events.pressed[x][y]:
                    if idx in repeats:
                        if repeats[idx] > 0:
                            print("[scripts] " + coords + "        " +
                                  str(repeats[idx]) + " repeats left.")
                            repeats[idx] -= 1
                            return labels[split_line[1]]
                        else:
                            print("[scripts] " + coords +
                                  "        No repeats left, not repeating.")
                    else:
                        repeats[idx] = int(split_line[2])
                        print("[scripts] " + coords + "        " +
                              str(repeats[idx]) + " repeats left.")
                        repeats[idx] -= 1
                        return labels[split_line[1]]
            elif split_line[0] == "IF_UNPRESSED_REPEAT_LABEL":
                print("[scripts] " + coords +
                      "    If key is not pressed repeat LABEL " +
                      split_line[1] + " " + split_line[2] + " times max")
                if not lp_events.pressed[x][y]:
                    if idx in repeats:
                        if repeats[idx] > 0:
                            print("[scripts] " + coords + "        " +
                                  str(repeats[idx]) + " repeats left.")
                            repeats[idx] -= 1
                            return labels[split_line[1]]
                        else:
                            print("[scripts] " + coords +
                                  "        No repeats left, not repeating.")
                    else:
                        repeats[idx] = int(split_line[2])
                        print("[scripts] " + coords + "        " +
                              str(repeats[idx]) + " repeats left.")
                        repeats[idx] -= 1
                        return labels[split_line[1]]
            elif split_line[0] == "@SIMPLE":
                print("[scripts] " + coords + "    Simple keybind: " +
                      split_line[1])

                #PRESS
                key = kb.sp(split_line[1])
                kb.press(key)

                #WAIT_UNPRESSED
                while lp_events.pressed[x][y]:
                    sleep(DELAY_EXIT_CHECK)
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords +
                              " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not is_async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY,
                                        (x, y)).start()
                        return idx + 1
                #RELEASE
                kb.release(key)
            elif split_line[0] == "OPEN":
                path_name = " ".join(split_line[1:])
                print("[scripts] " + coords + "    Open file or folder " +
                      path_name)
                files.open_file_folder(path_name)
            elif split_line[0] == "RELEASE_ALL":
                print("[scripts] " + coords + "    Release all keys")
                kb.release_all()
            elif split_line[0] == "RESET_REPEATS":
                print("[scripts] " + coords + "    Reset all repeats")
                for i in repeats:
                    repeats[i] = repeats_original[i]
            else:
                print("[scripts] " + coords + "    Invalid command: " +
                      split_line[0] + ", skipping...")
        return idx + 1

    run = True
    idx = 0
    while run:
        idx = main_logic(idx)
        if (idx < 0) or (idx >= len(script_lines)):
            run = False

    print("[scripts] (" + str(x) + ", " + str(y) + ") Script done running.")

    if not is_async:
        running = False
    threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()
Exemple #17
0
def run_script(script_str, x, y):
    global running
    global exit

    lp_colors.updateXY(x, y)
    coords = "(" + str(x) + ", " + str(y) + ")"

    script_lines = script_str.split("\n")

    is_async = False
    if script_lines[0].split(" ")[0] == "@ASYNC":
        is_async = True
        temp = script_lines.pop(0)
    else:
        running = True

    print("[scripts] " + coords + " Now running script...")
    for line in script_lines:
        if threads[x][y].kill.is_set():
            print("[scripts] " + coords +
                  " Recieved exit flag, script exiting...")
            threads[x][y].kill.clear()
            if not is_async:
                running = False
            threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY,
                            (x, y)).start()
            return
        line = line.strip()
        if line == "":
            print("[scripts] " + coords + "    Empty line")
        elif line[0] == "-":
            print("[scripts] " + coords + "    Comment " + line[1:])
        else:
            split_line = line.split(" ")
            if split_line[0] == "STRING":
                type_string = " ".join(split_line[1:])
                print("[scripts] " + coords + "    Type out string " +
                      type_string)
                keyboard.controller.type(type_string)
            elif split_line[0] == "DELAY":
                delay = None
                try:
                    delay = float(split_line[1])
                except:
                    print("[scripts] " + coords +
                          "     Invalid time to delay, skipping...")

                if delay != None:
                    print("[scripts] " + coords + "    Delay for " +
                          split_line[1] + " seconds")
                    while delay > DELAY_EXIT_CHECK:
                        sleep(DELAY_EXIT_CHECK)
                        delay -= DELAY_EXIT_CHECK
                        if threads[x][y].kill.is_set():
                            print("[scripts] " + coords +
                                  " Recieved exit flag, script exiting...")
                            threads[x][y].kill.clear()
                            if not is_async:
                                running = False
                            threading.Timer(EXIT_UPDATE_DELAY,
                                            lp_colors.updateXY,
                                            (x, y)).start()
                            return
                    if delay > 0:
                        sleep(delay)
            elif split_line[0] == "TAP":
                if len(split_line) <= 2:
                    print("[scripts] " + coords + "    Tap key " +
                          split_line[1])
                    keyboard.tap(split_line[1])
                elif len(split_line) <= 3:
                    taps = None
                    try:
                        taps = int(split_line[2])
                    except:
                        print(
                            "[scripts] " + coords +
                            "     Invalid number of times to tap, skipping...")

                    if (taps != None):
                        print("[scripts] " + coords + "    Tap key " +
                              split_line[1] + " " + split_line[2] + " times")

                        for tap in range(taps):
                            if threads[x][y].kill.is_set():
                                print("[scripts] " + coords +
                                      " Recieved exit flag, script exiting...")
                                threads[x][y].kill.clear()
                                if not is_async:
                                    running = False
                                keyboard.release(split_line[1])
                                threading.Timer(EXIT_UPDATE_DELAY,
                                                lp_colors.updateXY,
                                                (x, y)).start()
                                return
                            keyboard.tap(split_line[1])
                else:
                    taps = None
                    try:
                        taps = int(split_line[2])
                    except:
                        print(
                            "[scripts] " + coords +
                            "     Invalid number of times to tap, skipping...")
                    delay = None
                    try:
                        delay = float(split_line[3])
                    except:
                        print("[scripts] " + coords +
                              "     Invalid time to tap, skipping...")

                    if (taps != None) and (delay != None):
                        print("[scripts] " + coords + "    Tap key " +
                              split_line[1] + " " + split_line[2] +
                              " times for " + str(split_line[3]) +
                              " seconds each")
                        for tap in range(taps):
                            temp_delay = delay
                            if threads[x][y].kill.is_set():
                                print("[scripts] " + coords +
                                      " Recieved exit flag, script exiting...")
                                threads[x][y].kill.clear()
                                if not is_async:
                                    running = False
                                keyboard.release(split_line[1])
                                threading.Timer(EXIT_UPDATE_DELAY,
                                                lp_colors.updateXY,
                                                (x, y)).start()
                                return

                            keyboard.press(split_line[1])
                            while temp_delay > DELAY_EXIT_CHECK:
                                sleep(DELAY_EXIT_CHECK)
                                temp_delay -= DELAY_EXIT_CHECK
                                if threads[x][y].kill.is_set():
                                    print(
                                        "[scripts] " + coords +
                                        " Recieved exit flag, script exiting..."
                                    )
                                    threads[x][y].kill.clear()
                                    if not is_async:
                                        running = False
                                    keyboard.release(split_line[1])
                                    threading.Timer(EXIT_UPDATE_DELAY,
                                                    lp_colors.updateXY,
                                                    (x, y)).start()
                                    return
                            if temp_delay > 0:
                                sleep(temp_delay)
                            keyboard.release(split_line[1])
            elif split_line[0] == "PRESS":
                print("[scripts] " + coords + "    Press key " + split_line[1])
                keyboard.press(split_line[1])
            elif split_line[0] == "RELEASE":
                print("[scripts] " + coords + "    Release key " +
                      split_line[1])
                keyboard.release(split_line[1])
            elif split_line[0] == "SP_TAP":
                if keyboard.sp(split_line[1]) != None:
                    key = keyboard.sp(split_line[1])
                    if len(split_line) <= 2:
                        print("[scripts] " + coords + "    Tap special key " +
                              split_line[1])
                        keyboard.tap(key)
                    elif len(split_line) <= 3:
                        taps = None
                        try:
                            taps = int(split_line[2])
                        except:
                            print(
                                "[scripts] " + coords +
                                "     Invalid number of times to tap, skipping..."
                            )

                        if (taps != None):
                            print("[scripts] " + coords +
                                  "    Tap special key " + split_line[1] +
                                  " " + split_line[2] + " times")

                            for tap in range(taps):
                                if threads[x][y].kill.is_set():
                                    print(
                                        "[scripts] " + coords +
                                        " Recieved exit flag, script exiting..."
                                    )
                                    threads[x][y].kill.clear()
                                    if not is_async:
                                        running = False
                                    keyboard.release(key)
                                    threading.Timer(EXIT_UPDATE_DELAY,
                                                    lp_colors.updateXY,
                                                    (x, y)).start()
                                    return
                                keyboard.tap(key)
                    else:
                        taps = None
                        try:
                            taps = int(split_line[2])
                        except:
                            print(
                                "[scripts] " + coords +
                                "     Invalid number of times to tap, skipping..."
                            )
                        delay = None
                        try:
                            delay = float(split_line[3])
                        except:
                            print("[scripts] " + coords +
                                  "     Invalid time to tap, skipping...")

                        if (taps != None) and (delay != None):
                            print("[scripts] " + coords +
                                  "    Tap special key " + split_line[1] +
                                  " " + split_line[2] + " times for " +
                                  str(split_line[3]) + " seconds each")
                            for tap in range(taps):
                                temp_delay = delay
                                if threads[x][y].kill.is_set():
                                    print(
                                        "[scripts] " + coords +
                                        " Recieved exit flag, script exiting..."
                                    )
                                    threads[x][y].kill.clear()
                                    if not is_async:
                                        running = False
                                    keyboard.release(key)
                                    threading.Timer(EXIT_UPDATE_DELAY,
                                                    lp_colors.updateXY,
                                                    (x, y)).start()
                                    return

                                keyboard.press(key)
                                while temp_delay > DELAY_EXIT_CHECK:
                                    sleep(DELAY_EXIT_CHECK)
                                    temp_delay -= DELAY_EXIT_CHECK
                                    if threads[x][y].kill.is_set():
                                        print(
                                            "[scripts] " + coords +
                                            " Recieved exit flag, script exiting..."
                                        )
                                        threads[x][y].kill.clear()
                                        if not is_async:
                                            running = False
                                        keyboard.release(key)
                                        threading.Timer(
                                            EXIT_UPDATE_DELAY,
                                            lp_colors.updateXY,
                                            (x, y)).start()
                                        return
                                if temp_delay > 0:
                                    sleep(temp_delay)
                                keyboard.release(key)
                else:
                    print("[scripts] " + coords +
                          "    Invalid special character to tap: " +
                          split_line[1] + ", skipping...")
            elif split_line[0] == "SP_PRESS":
                if keyboard.sp(split_line[1]) != None:
                    print("[scripts] " + coords + "    Press special key " +
                          split_line[1])
                    keyboard.press(keyboard.sp(split_line[1]))
                else:
                    print("[scripts] " + coords +
                          "    Invalid special character to press: " +
                          split_line[1] + ", skipping...")
            elif split_line[0] == "SP_RELEASE":
                if keyboard.sp(split_line[1]) != None:
                    print("[scripts] " + coords + "    Release special key " +
                          split_line[1])
                    keyboard.release(keyboard.sp(split_line[1]))
                else:
                    print("[scripts] " + coords +
                          "    Invalid special character to release: " +
                          split_line[1] + ", skipping...")
            elif split_line[0] == "WEB":
                link = split_line[1]
                if "http" not in link:
                    link = "http://" + link
                print("[scripts] " + coords + "    Open website " + link +
                      " in default browser")
                webbrowser.open(link)
            elif split_line[0] == "WEB_NEW":
                link = split_line[1]
                if "http" not in link:
                    link = "http://" + link
                print("[scripts] " + coords + "    Open website " + link +
                      " in default browser, try to make a new window")
                webbrowser.open_new(link)
            elif split_line[0] == "SOUND":
                if len(split_line) > 2:
                    print("[scripts] " + coords + "    Play sound file " +
                          split_line[1] + " at volume " + str(split_line[2]))
                    sound.play(split_line[1], float(split_line[2]))
                else:
                    print("[scripts] " + coords + "    Play sound file " +
                          split_line[1])
                    sound.play(split_line[1])
            elif split_line[0] == "WAIT_UNPRESSED":
                print("[scripts] " + coords +
                      "    Wait for script key to be unpressed")
                while lp_events.pressed[x][y]:
                    sleep(DELAY_EXIT_CHECK)
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords +
                              " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not is_async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY,
                                        (x, y)).start()
                        return
            elif split_line[0] == "M_MOVE":
                if len(split_line) >= 3:
                    print("[scripts] " + coords +
                          "    Relative mouse movement (" + split_line[1] +
                          ", " + str(split_line[2]) + ")")
                    mouse.moveXY(float(split_line[1]), float(split_line[2]))
                else:
                    print(
                        "[scripts] " + coords +
                        "    Both X and Y are required for mouse movement, skipping..."
                    )
            elif split_line[0] == "M_SET":
                if len(split_line) >= 3:
                    print("[scripts] " + coords +
                          "    Set mouse position to (" + split_line[1] +
                          ", " + str(split_line[2]) + ")")
                    mouse.setXY(float(split_line[1]), float(split_line[2]))
                else:
                    print(
                        "[scripts] " + coords +
                        "    Both X and Y are required for mouse positioning, skipping..."
                    )
            elif split_line[0] == "M_PRESS":
                print("[scripts] " + coords + "    Press mouse button " +
                      split_line[1])
                mouse.press(split_line[1])
            elif split_line[0] == "M_RELEASE":
                print("[scripts] " + coords + "    Release mouse button " +
                      split_line[1])
                mouse.release(split_line[1])
            elif split_line[0] == "M_SCROLL":
                if len(split_line) > 2:
                    print("[scripts] " + coords + "    Scroll (" +
                          split_line[1] + ", " + split_line[2] + ")")
                    mouse.scroll(float(split_line[2]), float(split_line[1]))
                else:
                    print("[scripts] " + coords + "    Scroll " +
                          split_line[1])
                    mouse.scroll(0, float(split_line[1]))
            elif split_line[0] == "M_TAP":
                if len(split_line) <= 2:
                    print("[scripts] " + coords + "    Tap mouse button " +
                          split_line[1])
                    mouse.click(split_line[1])
                elif len(split_line) <= 3:
                    taps = None
                    try:
                        taps = int(split_line[2])
                    except:
                        print(
                            "[scripts] " + coords +
                            "     Invalid number of times to tap, skipping...")

                    if (taps != None):
                        print("[scripts] " + coords + "    Tap mouse button " +
                              split_line[1] + " " + split_line[2] + " times")
                        mouse.click(split_line[1], taps)
                else:
                    taps = None
                    try:
                        taps = int(split_line[2])
                    except:
                        print(
                            "[scripts] " + coords +
                            "     Invalid number of times to tap, skipping...")
                    delay = None
                    try:
                        delay = float(split_line[3])
                    except:
                        print("[scripts] " + coords +
                              "     Invalid time to tap, skipping...")

                    if (taps != None) and (delay != None):
                        print("[scripts] " + coords + "    Tap mouse button " +
                              split_line[1] + " " + split_line[2] +
                              " times for " + str(split_line[3]) +
                              " seconds each")
                        for tap in range(taps):
                            temp_delay = delay
                            if threads[x][y].kill.is_set():
                                print("[scripts] " + coords +
                                      " Recieved exit flag, script exiting...")
                                threads[x][y].kill.clear()
                                if not is_async:
                                    running = False
                                mouse.release(split_line[1])
                                threading.Timer(EXIT_UPDATE_DELAY,
                                                lp_colors.updateXY,
                                                (x, y)).start()
                                return

                            mouse.press(split_line[1])
                            while temp_delay > DELAY_EXIT_CHECK:
                                sleep(DELAY_EXIT_CHECK)
                                temp_delay -= DELAY_EXIT_CHECK
                                if threads[x][y].kill.is_set():
                                    print(
                                        "[scripts] " + coords +
                                        " Recieved exit flag, script exiting..."
                                    )
                                    threads[x][y].kill.clear()
                                    if not is_async:
                                        running = False
                                    mouse.release(split_line[1])
                                    threading.Timer(EXIT_UPDATE_DELAY,
                                                    lp_colors.updateXY,
                                                    (x, y)).start()
                                    return
                            if temp_delay > 0:
                                sleep(temp_delay)
                            mouse.release(split_line[1])
            elif split_line[0] == "M_LINE":
                x1 = int(split_line[1])
                y1 = int(split_line[2])
                x2 = int(split_line[3])
                y2 = int(split_line[4])

                delay = None
                if len(split_line) > 5:
                    delay = float(split_line[5]) / 1000.0

                skip = 1
                if len(split_line) > 6:
                    skip = int(split_line[6])

                if (delay == None) or (delay <= 0):
                    print("[scripts] " + coords + "    Mouse line from (" +
                          split_line[1] + ", " + split_line[2] + ") to (" +
                          split_line[3] + ", " + split_line[4] + ") by " +
                          str(skip) + " pixels per step")
                else:
                    print("[scripts] " + coords + "    Mouse line from (" +
                          split_line[1] + ", " + split_line[2] + ") to (" +
                          split_line[3] + ", " + split_line[4] + ") by " +
                          str(skip) + " pixels per step and wait " +
                          split_line[5] + " milliseconds between each step")

                points = mouse.line_coords(x1, y1, x2, y2)
                for x_M, y_M in points[::skip]:
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords +
                              " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not is_async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY,
                                        (x, y)).start()
                        return
                    mouse.setXY(x_M, y_M)
                    if (delay != None) and (delay > 0):
                        temp_delay = delay
                        while temp_delay > DELAY_EXIT_CHECK:
                            sleep(DELAY_EXIT_CHECK)
                            temp_delay -= DELAY_EXIT_CHECK
                            if threads[x][y].kill.is_set():
                                print("[scripts] " + coords +
                                      " Recieved exit flag, script exiting...")
                                threads[x][y].kill.clear()
                                if not is_async:
                                    running = False
                                threading.Timer(EXIT_UPDATE_DELAY,
                                                lp_colors.updateXY,
                                                (x, y)).start()
                                return
                        if temp_delay > 0:
                            sleep(temp_delay)
            elif split_line[0] == "M_LINE_MOVE":
                x1 = int(split_line[1])
                y1 = int(split_line[2])

                delay = None
                if len(split_line) > 3:
                    delay = float(split_line[3]) / 1000.0

                skip = 1
                if len(split_line) > 4:
                    skip = int(split_line[4])

                if (delay == None) or (delay <= 0):
                    print("[scripts] " + coords +
                          "    Mouse line move relative (" + split_line[1] +
                          ", " + split_line[2] + ") by " + str(skip) +
                          " pixels per step")
                else:
                    print("[scripts] " + coords +
                          "    Mouse line move relative (" + split_line[1] +
                          ", " + split_line[2] + ") by " + str(skip) +
                          " pixels per step and wait " + split_line[3] +
                          " milliseconds between each step")

                x_C, y_C = mouse.getXY()
                x_N, y_N = x_C + x1, y_C + y1
                points = mouse.line_coords(x_C, y_C, x_N, y_N)
                for x_M, y_M in points[::skip]:
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords +
                              " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not is_async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY,
                                        (x, y)).start()
                        return
                    mouse.setXY(x_M, y_M)
                    if (delay != None) and (delay > 0):
                        temp_delay = delay
                        while temp_delay > DELAY_EXIT_CHECK:
                            sleep(DELAY_EXIT_CHECK)
                            temp_delay -= DELAY_EXIT_CHECK
                            if threads[x][y].kill.is_set():
                                print("[scripts] " + coords +
                                      " Recieved exit flag, script exiting...")
                                threads[x][y].kill.clear()
                                if not is_async:
                                    running = False
                                threading.Timer(EXIT_UPDATE_DELAY,
                                                lp_colors.updateXY,
                                                (x, y)).start()
                                return
                        if temp_delay > 0:
                            sleep(temp_delay)
            elif split_line[0] == "M_LINE_SET":
                x1 = int(split_line[1])
                y1 = int(split_line[2])

                delay = None
                if len(split_line) > 3:
                    delay = float(split_line[3]) / 1000.0

                skip = 1
                if len(split_line) > 4:
                    skip = int(split_line[4])

                if (delay == None) or (delay <= 0):
                    print("[scripts] " + coords + "    Mouse line set (" +
                          split_line[1] + ", " + split_line[2] + ") by " +
                          str(skip) + " pixels per step")
                else:
                    print("[scripts] " + coords + "    Mouse line set (" +
                          split_line[1] + ", " + split_line[2] + ") by " +
                          str(skip) + " pixels per step and wait " +
                          split_line[3] + " milliseconds between each step")

                x_C, y_C = mouse.getXY()
                points = mouse.line_coords(x_C, y_C, x1, y1)
                for x_M, y_M in points[::skip]:
                    if threads[x][y].kill.is_set():
                        print("[scripts] " + coords +
                              " Recieved exit flag, script exiting...")
                        threads[x][y].kill.clear()
                        if not is_async:
                            running = False
                        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY,
                                        (x, y)).start()
                        return
                    mouse.setXY(x_M, y_M)
                    if (delay != None) and (delay > 0):
                        temp_delay = delay
                        while temp_delay > DELAY_EXIT_CHECK:
                            sleep(DELAY_EXIT_CHECK)
                            temp_delay -= DELAY_EXIT_CHECK
                            if threads[x][y].kill.is_set():
                                print("[scripts] " + coords +
                                      " Recieved exit flag, script exiting...")
                                threads[x][y].kill.clear()
                                if not is_async:
                                    running = False
                                threading.Timer(EXIT_UPDATE_DELAY,
                                                lp_colors.updateXY,
                                                (x, y)).start()
                                return
                        if temp_delay > 0:
                            sleep(temp_delay)
            else:
                print("[scripts] " + coords + "    Invalid command: " +
                      split_line[0] + ", skipping...")
    print("[scripts] (" + str(x) + ", " + str(y) + ") Script done running.")

    if not is_async:
        running = False
    threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()
Exemple #18
0
def run_script(script_str, x, y):
    global running
    global exit

    lp_colors.updateXY(x, y)
    coords = "(" + str(x) + ", " + str(y) + ")"

    print("[scripts] " + coords + " Now running script...")

    script_lines = script_str.split("\n")

    script_lines = [i.strip() for i in script_lines]

    #remove comments
    if len(script_lines) > 0:
        while (is_ignorable_line(script_lines[0])):
            line = script_lines.pop(0)
            if line != "":
                print("[scripts] " + coords + "    Comment: " + line[1:])
            if len(script_lines) <= 0:
                break

    if len(script_lines) > 0:
        is_async = False
        if script_lines[0].split(" ")[0] in ASYNC_HEADERS:
            is_async = True
        else:
            running = True

        if script_lines[0].split(" ")[0] == "@ASYNC":
            temp = script_lines.pop(0)

        #parse labels
        labels = dict()
        for idx, line in enumerate(script_lines):
            split_line = line.split(" ")
            if split_line[0] == "LABEL":
                labels[split_line[1]] = idx

        #prepare repeat counter {idx:repeats_left}
        repeats = dict()
        repeats_original = dict()

        m_pos = ()

        def main_logic(idx):
            nonlocal m_pos

            if check_kill(x, y, is_async):
                return idx + 1

            line = script_lines[idx]
            if line == "":
                return idx + 1
            if line[0] == "-":
                print("[scripts] " + coords + "    Comment: " + line[1:])
            else:
                split_line = line.split(" ")
                if split_line[0] == "STRING":
                    type_string = " ".join(split_line[1:])
                    print("[scripts] " + coords + "    Type out string " +
                          type_string)
                    kb.write(type_string)
                elif split_line[0] == "DELAY":
                    print("[scripts] " + coords + "    Delay for " +
                          split_line[1] + " seconds")
                    delay = float(split_line[1])
                    if not safe_sleep(delay, x, y, is_async):
                        return -1
                elif split_line[0] == "TAP":
                    key = kb.sp(split_line[1])
                    releasefunc = lambda: kb.release(key)
                    if len(split_line) <= 2:
                        print("[scripts] " + coords + "    Tap key " +
                              split_line[1])
                        kb.tap(key)
                    elif len(split_line) <= 3:
                        print("[scripts] " + coords + "    Tap key " +
                              split_line[1] + " " + split_line[2] + " times")
                        taps = int(split_line[2])
                        for tap in range(taps):
                            if check_kill(x, y, is_async, releasefunc):
                                return idx + 1
                            kb.tap(key)
                    else:
                        print("[scripts] " + coords + "    Tap key " +
                              split_line[1] + " " + split_line[2] +
                              " times for " + str(split_line[3]) +
                              " seconds each")
                        taps = int(split_line[2])
                        delay = float(split_line[3])
                        for tap in range(taps):
                            if check_kill(x, y, is_async, releasefunc):
                                return -1
                            kb.press(key)
                            if not safe_sleep(delay, x, y, is_async,
                                              releasefunc):
                                return -1
                elif split_line[0] == "PRESS":
                    print("[scripts] " + coords + "    Press key " +
                          split_line[1])
                    key = kb.sp(split_line[1])
                    kb.press(key)
                elif split_line[0] == "RELEASE":
                    print("[scripts] " + coords + "    Release key " +
                          split_line[1])
                    key = kb.sp(split_line[1])
                    kb.release(key)
                elif split_line[0] == "WEB":
                    link = split_line[1]
                    if "http" not in link:
                        link = "http://" + link
                    print("[scripts] " + coords + "    Open website " + link +
                          " in default browser")
                    webbrowser.open(link)
                elif split_line[0] == "WEB_NEW":
                    link = split_line[1]
                    if "http" not in link:
                        link = "http://" + link
                    print("[scripts] " + coords + "    Open website " + link +
                          " in default browser, try to make a new window")
                    webbrowser.open_new(link)
                elif split_line[0] == "CODE":
                    args = " ".join(split_line[1:])
                    print("[scripts] " + coords + "    Running code: " + args)
                    try:
                        subprocess.run(args)
                    except Exception as e:
                        print("[scripts] " + coords +
                              "    Error with running code: " + str(e))
                elif split_line[0] == "SOUND":
                    if len(split_line) > 2:
                        print("[scripts] " + coords + "    Play sound file " +
                              split_line[1] + " at volume " +
                              str(split_line[2]))
                        sound.play(split_line[1], float(split_line[2]))
                    else:
                        print("[scripts] " + coords + "    Play sound file " +
                              split_line[1])
                        sound.play(split_line[1])
                elif split_line[0] == "WAIT_UNPRESSED":
                    print("[scripts] " + coords +
                          "    Wait for script key to be unpressed")
                    while lp_events.pressed[x][y]:
                        sleep(DELAY_EXIT_CHECK)
                        if check_kill(x, y, is_async):
                            return idx + 1
                elif split_line[0] == "M_STORE":
                    print("[scripts] " + coords + "    Store mouse position")
                    m_pos = ms.get_pos()
                elif split_line[0] == "M_RECALL":
                    if m_pos == tuple():
                        print(
                            "[scripts] " + coords +
                            "    No 'M_STORE' command has been run, cannot do 'M_RECALL'"
                        )
                    else:
                        print("[scripts] " + coords +
                              "    Recall mouse position " + str(m_pos))
                        ms.set_pos(m_pos[0], m_pos[1])
                elif split_line[0] == "M_RECALL_LINE":
                    x1, y1 = m_pos

                    delay = None
                    if len(split_line) > 1:
                        delay = float(split_line[1]) / 1000.0

                    skip = 1
                    if len(split_line) > 2:
                        skip = int(split_line[2])

                    if (delay == None) or (delay <= 0):
                        print("[scripts] " + coords +
                              "    Recall mouse position " + str(m_pos) +
                              " in a line by " + str(skip) +
                              " pixels per step")
                    else:
                        print("[scripts] " + coords +
                              "    Recall mouse position " + str(m_pos) +
                              " in a line by " + str(skip) +
                              " pixels per step and wait " + split_line[1] +
                              " milliseconds between each step")

                    x_C, y_C = ms.get_pos()
                    points = ms.line_coords(x_C, y_C, x1, y1)
                    for x_M, y_M in points[::skip]:
                        if check_kill(x, y, is_async):
                            return -1
                        ms.set_pos(x_M, y_M)
                        if (delay != None) and (delay > 0):
                            if not safe_sleep(delay, x, y, is_async):
                                return -1
                elif split_line[0] == "M_MOVE":
                    if len(split_line) >= 3:
                        print("[scripts] " + coords +
                              "    Relative mouse movement (" + split_line[1] +
                              ", " + str(split_line[2]) + ")")
                        ms.move_to_pos(float(split_line[1]),
                                       float(split_line[2]))
                    else:
                        print(
                            "[scripts] " + coords +
                            "    Both X and Y are required for mouse movement, skipping..."
                        )
                elif split_line[0] == "M_SET":
                    if len(split_line) >= 3:
                        print("[scripts] " + coords +
                              "    Set mouse position to (" + split_line[1] +
                              ", " + str(split_line[2]) + ")")
                        ms.set_pos(float(split_line[1]), float(split_line[2]))
                    else:
                        print(
                            "[scripts] " + coords +
                            "    Both X and Y are required for mouse positioning, skipping..."
                        )
                elif split_line[0] == "M_SCROLL":
                    if len(split_line) > 2:
                        print("[scripts] " + coords + "    Scroll (" +
                              split_line[1] + ", " + split_line[2] + ")")
                        ms.scroll(float(split_line[2]), float(split_line[1]))
                    else:
                        print("[scripts] " + coords + "    Scroll " +
                              split_line[1])
                        ms.scroll(0, float(split_line[1]))
                elif split_line[0] == "M_LINE":
                    x1 = int(split_line[1])
                    y1 = int(split_line[2])
                    x2 = int(split_line[3])
                    y2 = int(split_line[4])

                    delay = None
                    if len(split_line) > 5:
                        delay = float(split_line[5]) / 1000.0

                    skip = 1
                    if len(split_line) > 6:
                        skip = int(split_line[6])

                    if (delay == None) or (delay <= 0):
                        print("[scripts] " + coords + "    Mouse line from (" +
                              split_line[1] + ", " + split_line[2] + ") to (" +
                              split_line[3] + ", " + split_line[4] + ") by " +
                              str(skip) + " pixels per step")
                    else:
                        print("[scripts] " + coords + "    Mouse line from (" +
                              split_line[1] + ", " + split_line[2] + ") to (" +
                              split_line[3] + ", " + split_line[4] + ") by " +
                              str(skip) + " pixels per step and wait " +
                              split_line[5] +
                              " milliseconds between each step")

                    points = ms.line_coords(x1, y1, x2, y2)
                    for x_M, y_M in points[::skip]:
                        if check_kill(x, y, is_async):
                            return -1
                        ms.set_pos(x_M, y_M)
                        if (delay != None) and (delay > 0):
                            if not safe_sleep(delay, x, y, is_async):
                                return -1
                elif split_line[0] == "M_LINE_MOVE":
                    x1 = int(split_line[1])
                    y1 = int(split_line[2])

                    delay = None
                    if len(split_line) > 3:
                        delay = float(split_line[3]) / 1000.0

                    skip = 1
                    if len(split_line) > 4:
                        skip = int(split_line[4])

                    if (delay == None) or (delay <= 0):
                        print("[scripts] " + coords +
                              "    Mouse line move relative (" +
                              split_line[1] + ", " + split_line[2] + ") by " +
                              str(skip) + " pixels per step")
                    else:
                        print("[scripts] " + coords +
                              "    Mouse line move relative (" +
                              split_line[1] + ", " + split_line[2] + ") by " +
                              str(skip) + " pixels per step and wait " +
                              split_line[3] +
                              " milliseconds between each step")

                    x_C, y_C = ms.get_pos()
                    x_N, y_N = x_C + x1, y_C + y1
                    points = ms.line_coords(x_C, y_C, x_N, y_N)
                    for x_M, y_M in points[::skip]:
                        if check_kill(x, y, is_async):
                            return -1
                        ms.set_pos(x_M, y_M)
                        if (delay != None) and (delay > 0):
                            if not safe_sleep(delay, x, y, is_async):
                                return -1
                elif split_line[0] == "M_LINE_SET":
                    x1 = int(split_line[1])
                    y1 = int(split_line[2])

                    delay = None
                    if len(split_line) > 3:
                        delay = float(split_line[3]) / 1000.0

                    skip = 1
                    if len(split_line) > 4:
                        skip = int(split_line[4])

                    if (delay == None) or (delay <= 0):
                        print("[scripts] " + coords + "    Mouse line set (" +
                              split_line[1] + ", " + split_line[2] + ") by " +
                              str(skip) + " pixels per step")
                    else:
                        print("[scripts] " + coords + "    Mouse line set (" +
                              split_line[1] + ", " + split_line[2] + ") by " +
                              str(skip) + " pixels per step and wait " +
                              split_line[3] +
                              " milliseconds between each step")

                    x_C, y_C = ms.get_pos()
                    points = ms.line_coords(x_C, y_C, x1, y1)
                    for x_M, y_M in points[::skip]:
                        if check_kill(x, y, is_async):
                            return -1
                        ms.set_pos(x_M, y_M)
                        if (delay != None) and (delay > 0):
                            if not safe_sleep(delay, x, y, is_async):
                                return -1
                elif split_line[0] == "LABEL":
                    print("[scripts] " + coords + "    Label: " +
                          split_line[1])
                    return idx + 1
                elif split_line[0] == "IF_PRESSED_GOTO_LABEL":
                    print("[scripts] " + coords +
                          "    If key is pressed goto LABEL " + split_line[1])
                    if lp_events.pressed[x][y]:
                        return labels[split_line[1]]
                elif split_line[0] == "IF_UNPRESSED_GOTO_LABEL":
                    print("[scripts] " + coords +
                          "    If key is not pressed goto LABEL " +
                          split_line[1])
                    if not lp_events.pressed[x][y]:
                        return labels[split_line[1]]
                elif split_line[0] == "GOTO_LABEL":
                    print("[scripts] " + coords + "    Goto LABEL " +
                          split_line[1])
                    return labels[split_line[1]]
                elif split_line[0] == "REPEAT_LABEL":
                    print("[scripts] " + coords + "    Repeat LABEL " +
                          split_line[1] + " " + split_line[2] + " times max")
                    if idx in repeats:
                        if repeats[idx] > 0:
                            print("[scripts] " + coords + "        " +
                                  str(repeats[idx]) + " repeats left.")
                            repeats[idx] -= 1
                            return labels[split_line[1]]
                        else:
                            print("[scripts] " + coords +
                                  "        No repeats left, not repeating.")
                    else:
                        repeats[idx] = int(split_line[2])
                        repeats_original[idx] = int(split_line[2])
                        print("[scripts] " + coords + "        " +
                              str(repeats[idx]) + " repeats left.")
                        repeats[idx] -= 1
                        return labels[split_line[1]]
                elif split_line[0] == "IF_PRESSED_REPEAT_LABEL":
                    print("[scripts] " + coords +
                          "    If key is pressed repeat LABEL " +
                          split_line[1] + " " + split_line[2] + " times max")
                    if lp_events.pressed[x][y]:
                        if idx in repeats:
                            if repeats[idx] > 0:
                                print("[scripts] " + coords + "        " +
                                      str(repeats[idx]) + " repeats left.")
                                repeats[idx] -= 1
                                return labels[split_line[1]]
                            else:
                                print(
                                    "[scripts] " + coords +
                                    "        No repeats left, not repeating.")
                        else:
                            repeats[idx] = int(split_line[2])
                            print("[scripts] " + coords + "        " +
                                  str(repeats[idx]) + " repeats left.")
                            repeats[idx] -= 1
                            return labels[split_line[1]]
                elif split_line[0] == "IF_UNPRESSED_REPEAT_LABEL":
                    print("[scripts] " + coords +
                          "    If key is not pressed repeat LABEL " +
                          split_line[1] + " " + split_line[2] + " times max")
                    if not lp_events.pressed[x][y]:
                        if idx in repeats:
                            if repeats[idx] > 0:
                                print("[scripts] " + coords + "        " +
                                      str(repeats[idx]) + " repeats left.")
                                repeats[idx] -= 1
                                return labels[split_line[1]]
                            else:
                                print(
                                    "[scripts] " + coords +
                                    "        No repeats left, not repeating.")
                        else:
                            repeats[idx] = int(split_line[2])
                            print("[scripts] " + coords + "        " +
                                  str(repeats[idx]) + " repeats left.")
                            repeats[idx] -= 1
                            return labels[split_line[1]]
                elif split_line[0] == "@SIMPLE":
                    print("[scripts] " + coords + "    Simple keybind: " +
                          split_line[1])
                    #PRESS
                    key = kb.sp(split_line[1])
                    releasefunc = lambda: kb.release(key)
                    kb.press(key)
                    #WAIT_UNPRESSED
                    while lp_events.pressed[x][y]:
                        sleep(DELAY_EXIT_CHECK)
                        if check_kill(x, y, is_async, releasefunc):
                            return idx + 1
                    #RELEASE
                    kb.release(key)
                elif split_line[0] == "@LOAD_LAYOUT":
                    layout_name = " ".join(split_line[1:])
                    print("[scripts] " + coords + "    Load layout " +
                          layout_name)
                    layout_path = os.path.join(files.LAYOUT_PATH, layout_name)
                    if not os.path.isfile(layout_path):
                        print("[scripts] " + coords +
                              "        ERROR: Layout file does not exist.")
                        return -1
                    try:
                        layout = files.load_layout(layout_path,
                                                   popups=False,
                                                   save_converted=False)
                    except files.json.decoder.JSONDecodeError:
                        print("[scripts] " + coords +
                              "        ERROR: Layout is malformated.")
                        return -1
                    if files.layout_changed_since_load:
                        files.save_lp_to_layout(files.curr_layout)
                    files.load_layout_to_lp(layout_path,
                                            popups=False,
                                            save_converted=False,
                                            preload=layout)
                elif split_line[0] == "OPEN":
                    path_name = " ".join(split_line[1:])
                    print("[scripts] " + coords + "    Open file or folder " +
                          path_name)
                    files.open_file_folder(path_name)
                elif split_line[0] == "RELEASE_ALL":
                    print("[scripts] " + coords + "    Release all keys")
                    kb.release_all()
                elif split_line[0] == "RESET_REPEATS":
                    print("[scripts] " + coords + "    Reset all repeats")
                    for i in repeats:
                        repeats[i] = repeats_original[i]
                else:
                    print("[scripts] " + coords + "    Invalid command: " +
                          split_line[0] + ", skipping...")
            return idx + 1

        run = True
        idx = 0
        while run:
            idx = main_logic(idx)
            if (idx < 0) or (idx >= len(script_lines)):
                run = False

        if not is_async:
            running = False
        threading.Timer(EXIT_UPDATE_DELAY, lp_colors.updateXY, (x, y)).start()

    print("[scripts] (" + str(x) + ", " + str(y) + ") Script done running.")