def __init__(self): signal.signal(signal.SIGINT, self._quit) signal.signal(signal.SIGTERM, self._quit) # SIGSEGV as a fail-safe signal.signal(signal.SIGSEGV, self._quit) # Initialize create_dir(VIDEO_WALLPAPER_PATH) self.config_handler = ConfigHandler(self._on_config_modified) self.config = self.config_handler.config self.current_video_path = self.config.video_path self.user_pause_playback = False self.is_any_maximized, self.is_any_fullscreen = False, False # We need to initialize X11 threads so we can use hardware decoding. # `libX11.so.6` fix for Fedora 33 x11 = None for lib in ["libX11.so", "libX11.so.6"]: try: x11 = ctypes.cdll.LoadLibrary(lib) except OSError: pass if x11 is not None: x11.XInitThreads() break self._build_context_menu() # Monitor Detect self.monitors = [] self.monitor_detect() self.start_all_monitors() self.active_handler = ActiveHandler(self._on_active_changed) if os.environ["DESKTOP_SESSION"] in ["gnome", "gnome-xorg"]: self.window_handler = WindowHandlerGnome(self._on_window_state_changed) else: self.window_handler = WindowHandler(self._on_window_state_changed) self.static_wallpaper_handler = StaticWallpaperHandler() self.static_wallpaper_handler.set_static_wallpaper() if self.config.video_path == "": # First time ControlPanel().run() elif not os.path.isfile(self.config.video_path): self._on_file_not_found(self.config.video_path) self.file_list = scan_dir() random.shuffle(self.file_list) self.current = 0 if self.config.video_path in self.file_list: self.current = self.file_list.index(self.config.video_path) Gtk.main()
def main(argv=None): # Short story: We'll create N Tasks # Each task is a genetic algorithm which has a population of individuals # (each individual represents a test and its represented in memory as # an array of bytes) # We create N tasks which will compete on W workers available, # ideally N should be greater significantly than W to optimize # stuff - that's what parameter tasksFactor stands for. # Each task will have a number of generations available to optimize things. # After this number of iterations, we do a merge operation to select the # bests tests from those N workers. # The bests tests will be outputed on a new folder with tests, # how many folders is defined by testsFolderCount. # TODO: many of this parameter should not be optional! # But we keep them optional for easy testing :) # TODO CPADURARU: environment variables # subprocess.Popen("sh stub_env_variables_genetic_code.sh", shell=True) Params = utils.readParams() ## logs dir is cleared by cardinal and input corpus is added by tracer.node configHandler = ConfigHandler(Params.config) mongoHandler = MongoHandler(Params.executableId, configHandler) traceImporter = TraceImporter(executableId=Params.executableId, configHandler=configHandler, mongoHandler=mongoHandler, driverPresent=Params.driver) try: runApp(configHandler, mongoHandler, traceImporter, Params) except (KeyboardInterrupt, SystemExit) as e: print("INFO: Received keyboard interrupt. Exiting ....") traceImporter.getQueueSize() traceImporter.clear() sys.exit(0)
def __init__(self): super().__init__(application_id='io.github.jeffshee.hidamari.gui') self.config_handler = ConfigHandler(self._on_config_modified) self.config = self.config_handler.config # Builder Initialization self.builder = Gtk.Builder() self.builder.add_from_file(GUI_GLADE_FILENAME) object_list = [ 'window', 'icon_view', 'volume', 'volume_adjustment', 'autostart', 'mute_audio', 'detect_maximized', 'static_wallpaper', 'blur_adjustment', 'blur_radius', 'apply' ] object_dict = defaultdict() for obj in object_list: object_dict[obj] = self.builder.get_object(obj) self.object = SimpleNamespace(**object_dict)
while True: logging.debug("Forward replies thread locked.") socket_event.wait() logging.debug("Forward replies thread unlocked.") reply = forward_socket.recv(4096) headers = create_post_header() payload = base64.b64encode(reply) payload = obf.randomize_payload(payload.decode("ascii")) params = {'payload': payload} r = requests.post(url + ressource, headers=headers, data=params) if __name__ == "__main__": logging.basicConfig(format='%(levelname)8s:%(asctime)s:%(funcName)20s():%(message)s', filename='client.log', level=logging.DEBUG) conf = ConfigHandler() conf.read_conf() http_server = conf[conf.SCT_TUNNELD][conf.OPT_HTTP_HOST] http_port = conf[conf.SCT_TUNNELD][conf.OPT_HTTP_PORT] forward_server = conf[conf.SCT_CLIENT][conf.OPT_FORWARD_HOST] forward_port = int(conf[conf.SCT_CLIENT][conf.OPT_FORWARD_PORT]) url = url.format(PROTOCOL, http_server, http_port) website = website.format(local_server, http_port) listen = "Listening on {0}:{1}.".format(http_server, http_port) forward = "Forwarding on {0}:{1}.".format(forward_server, forward_port) print(listen) print(forward) logging.info(listen)
class Player: def __init__(self): signal.signal(signal.SIGINT, self._quit) signal.signal(signal.SIGTERM, self._quit) # SIGSEGV as a fail-safe signal.signal(signal.SIGSEGV, self._quit) # Initialize create_dir(VIDEO_WALLPAPER_PATH) self.config_handler = ConfigHandler(self._on_config_modified) self.config = self.config_handler.config self.current_video_path = self.config.video_path self.user_pause_playback = False self.is_any_maximized, self.is_any_fullscreen = False, False # We need to initialize X11 threads so we can use hardware decoding. x11 = ctypes.cdll.LoadLibrary('libX11.so') x11.XInitThreads() # Monitor Detect self.monitors = {} self.monitor_detect() self._start_all_monitors() self.active_handler = ActiveHandler(self._on_active_changed) if os.environ["DESKTOP_SESSION"] in ["gnome", "gnome-xorg"]: self.window_handler = WindowHandlerGnome( self._on_window_state_changed) else: self.window_handler = WindowHandler(self._on_window_state_changed) self.static_wallpaper_handler = StaticWallpaperHandler() self.static_wallpaper_handler.set_static_wallpaper() if self.config.video_path == '': # First time ControlPanel().run() elif not os.path.isfile(self.config.video_path): self._on_file_not_found(self.config.video_path) self.file_list = scan_dir() random.shuffle(self.file_list) self.current = 0 if self.config.video_path in self.file_list: self.current = self.file_list.index(self.config.video_path) Gtk.main() def _start_all_monitors(self): first = True for monitor in self.monitors.values(): # Setup a VLC widget given the provided width and height. video_playback = VLCWidget(monitor["Width"], monitor["Height"]) media = video_playback.instance.media_new(self.config.video_path) """ This loops the media itself. Using -R / --repeat and/or -L / --loop don't seem to work. However, based on reading, this probably only repeats 65535 times, which is still a lot of time, but might cause the program to stop playback if it's left on for a very long time. """ media.add_option("input-repeat=65535") video_playback.player.set_media(media) # These are to allow us to right click. VLC can't hijack mouse input, and probably not key inputs either in # Case we want to add keyboard shortcuts later on. video_playback.player.video_set_mouse_input(False) video_playback.player.video_set_key_input(False) # Prevent awful ear-rape with multiple instances. if not first: media.add_option("no-audio") # Window settings window = Gtk.Window() window.add(video_playback) window.set_type_hint(Gdk.WindowTypeHint.DESKTOP) window.set_size_request(monitor["Width"], monitor["Height"]) window.move(monitor["X"], monitor["Y"]) # button event self._build_context_menu() window.connect('button-press-event', self._on_button_press_event) window.show_all() monitor["Video"] = video_playback monitor["Window"] = window first = False def _set_volume(self, volume): first = True for monitor in self.monitors.values(): if first: monitor["Video"].player.audio_set_volume(volume) first = False else: monitor["Video"].player.audio_set_volume(0) def pause_playback(self): for monitor in self.monitors.values(): monitor["Video"].player.pause() def start_playback(self): if not self.user_pause_playback: for monitor in self.monitors.values(): monitor["Video"].player.play() def _quit(self, *args): self.static_wallpaper_handler.restore_ori_wallpaper() Gtk.main_quit() for monitor in self.monitors.values(): monitor["Video"].player.release() def monitor_detect(self): display = Gdk.Display.get_default() screen = Gdk.Screen.get_default() x = 0 while True: monitor = display.get_monitor(x) if monitor is None: break geometry = monitor.get_geometry() scale_factor = monitor.get_scale_factor() # Since this happens every time a size change, we need to be aware that the key may already exist, and might # Have Video and Window attributes, so we just replace if it exists, otherwise we're creating the first # Dictionary and should create a new value associated with the monitor number. if x in self.monitors.keys(): self.monitors[x]["Width"] = scale_factor * geometry.width self.monitors[x]["Height"] = scale_factor * geometry.height self.monitors[x]["X"] = geometry.x self.monitors[x]["Y"] = geometry.y else: self.monitors[x] = { "Width": scale_factor * geometry.width, "Height": scale_factor * geometry.height, "X": geometry.x, "Y": geometry.y } x += 1 screen.connect('size-changed', self._on_size_changed) def _on_size_changed(self, *args): self.monitor_detect() for monitor in self.monitors.values(): monitor["Window"].resize(monitor["Width"], monitor["Height"]) monitor["Window"].move(monitor["X"], monitor["Y"]) def _on_active_changed(self, active): if active: self.pause_playback() else: if (self.is_any_maximized and self.config.detect_maximized) or self.is_any_fullscreen: self.pause_playback() else: self.start_playback() def _on_window_state_changed(self, state): self.is_any_maximized, self.is_any_fullscreen = state[ 'is_any_maximized'], state['is_any_fullscreen'] if (self.is_any_maximized and self.config.detect_maximized) or self.is_any_fullscreen: self.pause_playback() else: self.start_playback() def _on_config_modified(self): def _run(): # Get new config self.config = self.config_handler.config self.pause_playback() if self.current_video_path != self.config.video_path: for monitor in self.monitors.values(): media = monitor["Video"].instance.media_new( self.config.video_path) media.add_option("input-repeat=65535") monitor["Video"].player.set_media(media) monitor["Video"].player.set_position(0.0) self.current_video_path = self.config.video_path if self.config.mute_audio: self._set_volume(0) else: self._set_volume(int(self.config.audio_volume * 100)) self.start_playback() # To ensure thread safe GLib.idle_add(_run) def _on_menuitem_main_gui(self, *args): ControlPanel().run() def _on_menuitem_mute_audio(self, item): self.config.mute_audio = item.get_active() self.config_handler.save() def _on_menuitem_pause_playback(self, item): self.user_pause_playback = item.get_active() self.pause_playback( ) if self.user_pause_playback else self.start_playback() def _on_menuitem_feeling_lucky(self, *args): self.current += 1 if self.current % len(self.file_list) == 0: random.shuffle(self.file_list) self.config.video_path = self.file_list[self.current % len(self.file_list)] self.config_handler.save() def _on_menuitem_gnome_settings(self, *args): subprocess.Popen('gnome-control-center') def _on_menuitem_quit(self, *args): self._quit() def _build_context_menu(self): self.menu = Gtk.Menu() items = [('Show Hidamari', self._on_menuitem_main_gui, Gtk.MenuItem), ('Mute Audio', self._on_menuitem_mute_audio, Gtk.CheckMenuItem), ('Pause Playback', self._on_menuitem_pause_playback, Gtk.CheckMenuItem), ('I\'m Feeling Lucky', self._on_menuitem_feeling_lucky, Gtk.MenuItem), ('Quit Hidamari', self._on_menuitem_quit, Gtk.MenuItem)] self.menuitem = defaultdict() if 'gnome' in os.environ['XDG_CURRENT_DESKTOP'].lower(): items += [(None, None, Gtk.SeparatorMenuItem), ('GNOME Settings', self._on_menuitem_gnome_settings, Gtk.MenuItem)] for item in items: label, handler, item_type = item if item_type == Gtk.SeparatorMenuItem: self.menu.append(Gtk.SeparatorMenuItem()) else: menuitem = item_type.new_with_label(label) menuitem.connect('activate', handler) menuitem.set_margin_top(4) menuitem.set_margin_bottom(4) self.menu.append(menuitem) self.menuitem[label] = menuitem self.menu.show_all() def _on_button_press_event(self, widget, event): if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 3: self.menu.popup_at_pointer() return True def _on_not_implemented(self, *args): print('Not implemented!') message = Gtk.MessageDialog(type=Gtk.MessageType.INFO, buttons=Gtk.ButtonsType.OK, message_format='Not implemented!') message.connect("response", self._dialog_response) message.show() def _on_file_not_found(self, path): print('File not found!') message = Gtk.MessageDialog( type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.OK, message_format='File {} not found!'.format(path)) message.connect("response", self._dialog_response) message.show() def _dialog_response(self, widget, response_id): if response_id == Gtk.ResponseType.OK: widget.destroy()
class Player: def __init__(self): signal.signal(signal.SIGINT, self._quit) signal.signal(signal.SIGTERM, self._quit) # SIGSEGV as a fail-safe signal.signal(signal.SIGSEGV, self._quit) # Initialize create_dir(VIDEO_WALLPAPER_PATH) self.config_handler = ConfigHandler(self._on_config_modified) self.config = self.config_handler.config self.current_video_path = self.config.video_path self.user_pause_playback = False self.is_any_maximized, self.is_any_fullscreen = False, False # We need to initialize X11 threads so we can use hardware decoding. # `libX11.so.6` fix for Fedora 33 x11 = None for lib in ["libX11.so", "libX11.so.6"]: try: x11 = ctypes.cdll.LoadLibrary(lib) except OSError: pass if x11 is not None: x11.XInitThreads() break self._build_context_menu() # Monitor Detect self.monitors = [] self.monitor_detect() self.start_all_monitors() self.active_handler = ActiveHandler(self._on_active_changed) if os.environ["DESKTOP_SESSION"] in ["gnome", "gnome-xorg"]: self.window_handler = WindowHandlerGnome(self._on_window_state_changed) else: self.window_handler = WindowHandler(self._on_window_state_changed) self.static_wallpaper_handler = StaticWallpaperHandler() self.static_wallpaper_handler.set_static_wallpaper() if self.config.video_path == "": # First time ControlPanel().run() elif not os.path.isfile(self.config.video_path): self._on_file_not_found(self.config.video_path) self.file_list = scan_dir() random.shuffle(self.file_list) self.current = 0 if self.config.video_path in self.file_list: self.current = self.file_list.index(self.config.video_path) Gtk.main() def start_all_monitors(self): for monitor in self.monitors: if monitor.is_initialized: continue # Setup a VLC widget given the provided width and height. vlc_widget = VLCWidget(monitor.width, monitor.height) media = vlc_widget.instance.media_new(self.config.video_path) """ This loops the media itself. Using -R / --repeat and/or -L / --loop don't seem to work. However, based on reading, this probably only repeats 65535 times, which is still a lot of time, but might cause the program to stop playback if it's left on for a very long time. """ media.add_option("input-repeat=65535") # Prevent awful ear-rape with multiple instances. if not monitor.is_primary: media.add_option("no-audio") vlc_widget.player.set_media(media) # These are to allow us to right click. VLC can't hijack mouse input, and probably not key inputs either in # Case we want to add keyboard shortcuts later on. vlc_widget.player.video_set_mouse_input(False) vlc_widget.player.video_set_key_input(False) # Window settings window = Gtk.Window() window.add(vlc_widget) window.set_type_hint(Gdk.WindowTypeHint.DESKTOP) window.set_size_request(monitor.width, monitor.height) window.move(monitor.x, monitor.y) # Button event window.connect("button-press-event", self._on_button_press_event) window.show_all() monitor.initialize(window, vlc_widget) def set_volume(self, volume): for monitor in self.monitors: if monitor.is_primary: monitor.vlc_audio_set_volume(volume) def pause_playback(self): for monitor in self.monitors: monitor.vlc_pause() def start_playback(self): if not self.user_pause_playback: for monitor in self.monitors: monitor.vlc_play() def _quit(self, *args): self.static_wallpaper_handler.restore_ori_wallpaper() del self.monitors Gtk.main_quit() def monitor_detect(self): display = Gdk.Display.get_default() screen = display.get_default_screen() for i in range(display.get_n_monitors()): monitor = Monitor(display.get_monitor(i)) if monitor not in self.monitors: self.monitors.append(monitor) screen.connect("size-changed", self._on_size_changed) display.connect("monitor-added", self._on_monitor_added) display.connect("monitor-removed", self._on_monitor_removed) def monitor_sync(self): primary = 0 for i, monitor in enumerate(self.monitors): if monitor.is_primary: primary = i break for monitor in self.monitors: # `set_position()` method require the playback to be enabled before calling monitor.vlc_play() monitor.vlc_set_position(self.monitors[primary].vlc_get_position()) monitor.vlc_play() if self.monitors[primary].vlc_is_playing() else monitor.vlc_pause() def _on_size_changed(self, *args): print("size-changed") for monitor in self.monitors: monitor.win_resize(monitor.width, monitor.height) monitor.win_move(monitor.x, monitor.y) print(monitor.x, monitor.y, monitor.width, monitor.height) def _on_monitor_added(self, _, gdk_monitor, *args): print("monitor-added") new_monitor = Monitor(gdk_monitor) self.monitors.append(new_monitor) self.start_all_monitors() self.monitor_sync() def _on_monitor_removed(self, _, gdk_monitor, *args): print("monitor-removed") self.monitors.remove(Monitor(gdk_monitor)) def _on_active_changed(self, active): if active: self.pause_playback() else: if (self.is_any_maximized and self.config.detect_maximized) or self.is_any_fullscreen: self.pause_playback() else: self.start_playback() def _on_window_state_changed(self, state): self.is_any_maximized, self.is_any_fullscreen = state["is_any_maximized"], state["is_any_fullscreen"] if (self.is_any_maximized and self.config.detect_maximized) or self.is_any_fullscreen: self.pause_playback() else: self.start_playback() def _on_config_modified(self): def _run(): # Get new config self.config = self.config_handler.config self.pause_playback() if self.current_video_path != self.config.video_path: for monitor in self.monitors: media = monitor.vlc_media_new(self.config.video_path) media.add_option("input-repeat=65535") if not monitor.is_primary: media.add_option("no-audio") monitor.vlc_set_media(media) monitor.vlc_set_position(0.0) self.current_video_path = self.config.video_path if self.config.mute_audio: self.set_volume(0) else: self.set_volume(int(self.config.audio_volume * 100)) self.start_playback() # To ensure thread safe GLib.idle_add(_run) def _on_menuitem_main_gui(self, *args): ControlPanel().run() def _on_menuitem_mute_audio(self, item): self.config.mute_audio = item.get_active() self.config_handler.save() def _on_menuitem_pause_playback(self, item): self.user_pause_playback = item.get_active() self.pause_playback() if self.user_pause_playback else self.start_playback() def _on_menuitem_feeling_lucky(self, *args): self.current += 1 if self.current % len(self.file_list) == 0: random.shuffle(self.file_list) self.config.video_path = self.file_list[self.current % len(self.file_list)] self.config_handler.save() def _on_menuitem_gnome_settings(self, *args): subprocess.Popen('gnome-control-center') def _on_menuitem_quit(self, *args): self._quit() def _build_context_menu(self): self.menu = Gtk.Menu() items = [('Show Hidamari', self._on_menuitem_main_gui, Gtk.MenuItem), ('Mute Audio', self._on_menuitem_mute_audio, Gtk.CheckMenuItem), ('Pause Playback', self._on_menuitem_pause_playback, Gtk.CheckMenuItem), ('I\'m Feeling Lucky', self._on_menuitem_feeling_lucky, Gtk.MenuItem), ('Quit Hidamari', self._on_menuitem_quit, Gtk.MenuItem)] self.menuitem = defaultdict() if 'gnome' in os.environ['XDG_CURRENT_DESKTOP'].lower(): items += [(None, None, Gtk.SeparatorMenuItem), ('GNOME Settings', self._on_menuitem_gnome_settings, Gtk.MenuItem)] for item in items: label, handler, item_type = item if item_type == Gtk.SeparatorMenuItem: self.menu.append(Gtk.SeparatorMenuItem()) else: menuitem = item_type.new_with_label(label) menuitem.connect('activate', handler) menuitem.set_margin_top(4) menuitem.set_margin_bottom(4) self.menu.append(menuitem) self.menuitem[label] = menuitem self.menu.show_all() def _on_button_press_event(self, widget, event): if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 3: self.menu.popup_at_pointer() return True def _on_not_implemented(self, *args): print("Not implemented!") message = Gtk.MessageDialog(type=Gtk.MessageType.INFO, buttons=Gtk.ButtonsType.OK, message_format='Not implemented!') message.connect("response", self._dialog_response) message.show() def _on_file_not_found(self, path): print("File not found!") message = Gtk.MessageDialog(type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.OK, message_format='File {} not found!'.format(path)) message.connect("response", self._dialog_response) message.show() def _dialog_response(self, widget, response_id): if response_id == Gtk.ResponseType.OK: widget.destroy()
class ControlPanel(Gtk.Application): def __init__(self): super().__init__(application_id='io.github.jeffshee.hidamari.gui') self.config_handler = ConfigHandler(self._on_config_modified) self.config = self.config_handler.config # Builder Initialization self.builder = Gtk.Builder() self.builder.add_from_file(GUI_GLADE_FILENAME) object_list = [ 'window', 'icon_view', 'volume', 'volume_adjustment', 'autostart', 'mute_audio', 'detect_maximized', 'static_wallpaper', 'blur_adjustment', 'blur_radius', 'apply' ] object_dict = defaultdict() for obj in object_list: object_dict[obj] = self.builder.get_object(obj) self.object = SimpleNamespace(**object_dict) def do_startup(self): Gtk.Application.do_startup(self) # Object Initialization self.object.window.connect('destroy', Gtk.main_quit) self.object.window.show_all() self._reload_icon_view() self._reload_widget() self.builder.connect_signals({ 'on_apply_clicked': self._on_apply_clicked, 'on_cancel_clicked': self._on_cancel_clicked, 'on_value_changed': self._on_value_changed, 'on_refresh_clicked': self._on_refresh_clicked }) def do_activate(self): Gtk.main() def _on_config_modified(self): def _run(): self.config = self.config_handler.config self._reload_widget() # To ensure thread safe GLib.idle_add(_run) def _on_apply_clicked(self, *args): selected = self.object.icon_view.get_selected_items() if len(selected) != 0: icon_view_selection = selected[0].get_indices()[0] self.config.video_path = self.file_list[icon_view_selection] # setup_autostart(self.object.autostart.get_active()) self.config.static_wallpaper = self.object.static_wallpaper.get_active( ) self.config.detect_maximized = self.object.detect_maximized.get_active( ) self.config.mute_audio = self.object.mute_audio.get_active() self.config.static_wallpaper_blur_radius = self.object.blur_adjustment.get_value( ) self.config.audio_volume = self.object.volume_adjustment.get_value( ) / 100 self.config_handler.save() self.object.apply.set_sensitive(False) def _on_cancel_clicked(self, *args): self._reload_widget() def _on_refresh_clicked(self, *args): self._reload_icon_view() def _on_value_changed(self, *args): self.object.volume.set_sensitive( not self.object.mute_audio.get_active()) self.object.blur_radius.set_sensitive( self.object.static_wallpaper.get_active()) self.object.apply.set_sensitive(True) def _reload_icon_view(self): self.file_list = scan_dir() list_store = Gtk.ListStore(Pixbuf, str) self.object.icon_view.set_model(list_store) self.object.icon_view.set_pixbuf_column(0) self.object.icon_view.set_text_column(1) for idx, video in enumerate(self.file_list): icon_theme = Gtk.IconTheme().get_default() pixbuf = icon_theme.load_icon('video-x-generic', 96, 0) list_store.append([pixbuf, os.path.basename(video)]) thread = threading.Thread(target=get_thumbnail_gnome, args=(video, list_store, idx)) thread.daemon = True thread.start() def _reload_widget(self): self.object.autostart.set_active( os.path.isfile(AUTOSTART_DESKTOP_PATH)) self.object.static_wallpaper.set_active(self.config.static_wallpaper) self.object.detect_maximized.set_active(self.config.detect_maximized) self.object.mute_audio.set_active(self.config.mute_audio) self.object.volume_adjustment.set_value(self.config.audio_volume * 100) self.object.blur_adjustment.set_value( self.config.static_wallpaper_blur_radius) # self.object.icon_view.unselect_all() # self.object.volume.set_sensitive( not self.object.mute_audio.get_active()) self.object.blur_radius.set_sensitive( self.object.static_wallpaper.get_active()) self.object.apply.set_sensitive(False)
def __init__(self): signal.signal(signal.SIGINT, self._quit) signal.signal(signal.SIGTERM, self._quit) # SIGSEGV as a fail-safe signal.signal(signal.SIGSEGV, self._quit) # Initialize GtkClutter.init() ClutterGst.init() create_dir(VIDEO_WALLPAPER_PATH) self.config_handler = ConfigHandler(self._on_config_modified) self.config = self.config_handler.config self.current_video_path = self.config.video_path self.user_pause_playback = False self.is_any_maximized, self.is_any_fullscreen = False, False # Monitor Detect self.width, self.height = self.monitor_detect() # Actors initialize self.embed = GtkClutter.Embed() self.main_actor = self.embed.get_stage() self.main_actor.set_background_color(Clutter.Color.get_static(Clutter.StaticColor.BLACK)) # Video initialize self.video_playback = ClutterGst.Playback() self.video_content = ClutterGst.Content() self.video_content.set_player(self.video_playback) # Playback settings self.video_playback.set_filename(self.config.video_path) self.video_playback.set_audio_volume(0.0 if self.config.mute_audio else self.config.audio_volume) self.video_playback.set_playing(True) self.video_playback.connect('eos', self._on_eos) self.main_actor.set_content(self.video_content) # Window settings self.window = Gtk.Window() self.window.add(self.embed) self.window.set_type_hint(Gdk.WindowTypeHint.DESKTOP) self.window.set_size_request(self.width, self.height) # button event self._build_context_menu() self.window.connect('button-press-event', self._on_button_press_event) self.window.show_all() self.active_handler = ActiveHandler(self._on_active_changed) self.window_handler = WindowHandler(self._on_window_state_changed) self.static_wallpaper_handler = StaticWallpaperHandler() self.static_wallpaper_handler.set_static_wallpaper() if self.config.video_path == '': # First time ControlPanel().run() elif not os.path.isfile(self.config.video_path): self._on_file_not_found(self.config.video_path) self.file_list = scan_dir() random.shuffle(self.file_list) self.current = 0 if self.config.video_path in self.file_list: self.current = self.file_list.index(self.config.video_path) Gtk.main()
class Player: def __init__(self): signal.signal(signal.SIGINT, self._quit) signal.signal(signal.SIGTERM, self._quit) # SIGSEGV as a fail-safe signal.signal(signal.SIGSEGV, self._quit) # Initialize GtkClutter.init() ClutterGst.init() create_dir(VIDEO_WALLPAPER_PATH) self.config_handler = ConfigHandler(self._on_config_modified) self.config = self.config_handler.config self.current_video_path = self.config.video_path self.user_pause_playback = False self.is_any_maximized, self.is_any_fullscreen = False, False # Monitor Detect self.width, self.height = self.monitor_detect() # Actors initialize self.embed = GtkClutter.Embed() self.main_actor = self.embed.get_stage() self.main_actor.set_background_color(Clutter.Color.get_static(Clutter.StaticColor.BLACK)) # Video initialize self.video_playback = ClutterGst.Playback() self.video_content = ClutterGst.Content() self.video_content.set_player(self.video_playback) # Playback settings self.video_playback.set_filename(self.config.video_path) self.video_playback.set_audio_volume(0.0 if self.config.mute_audio else self.config.audio_volume) self.video_playback.set_playing(True) self.video_playback.connect('eos', self._on_eos) self.main_actor.set_content(self.video_content) # Window settings self.window = Gtk.Window() self.window.add(self.embed) self.window.set_type_hint(Gdk.WindowTypeHint.DESKTOP) self.window.set_size_request(self.width, self.height) # button event self._build_context_menu() self.window.connect('button-press-event', self._on_button_press_event) self.window.show_all() self.active_handler = ActiveHandler(self._on_active_changed) self.window_handler = WindowHandler(self._on_window_state_changed) self.static_wallpaper_handler = StaticWallpaperHandler() self.static_wallpaper_handler.set_static_wallpaper() if self.config.video_path == '': # First time ControlPanel().run() elif not os.path.isfile(self.config.video_path): self._on_file_not_found(self.config.video_path) self.file_list = scan_dir() random.shuffle(self.file_list) self.current = 0 if self.config.video_path in self.file_list: self.current = self.file_list.index(self.config.video_path) Gtk.main() def pause_playback(self): self.video_playback.set_playing(False) def start_playback(self): if not self.user_pause_playback: self.video_playback.set_playing(True) def _quit(self, *args): self.static_wallpaper_handler.restore_ori_wallpaper() Gtk.main_quit() def monitor_detect(self): display = Gdk.Display.get_default() screen = Gdk.Screen.get_default() monitor = display.get_primary_monitor() geometry = monitor.get_geometry() scale_factor = monitor.get_scale_factor() width = scale_factor * geometry.width height = scale_factor * geometry.height screen.connect('size-changed', self._on_size_changed) return width, height def _on_size_changed(self, *args): self.width, self.height = self.monitor_detect() self.window.resize(self.width, self.height) self.wallpaper_actor.set_size(self.width, self.height) def _on_active_changed(self, active): if active: self.pause_playback() else: if (self.is_any_maximized and self.config.detect_maximized) or self.is_any_fullscreen: self.pause_playback() else: self.start_playback() def _on_window_state_changed(self, state): self.is_any_maximized, self.is_any_fullscreen = state['is_any_maximized'], state['is_any_fullscreen'] if (self.is_any_maximized and self.config.detect_maximized) or self.is_any_fullscreen: self.pause_playback() else: self.start_playback() def _on_config_modified(self): def _run(): # Get new config self.config = self.config_handler.config self.pause_playback() if self.current_video_path != self.config.video_path: self.video_playback.set_filename(self.config.video_path) self.video_playback.set_progress(0.0) self.current_video_path = self.config.video_path self.video_playback.set_audio_volume(0.0 if self.config.mute_audio else self.config.audio_volume) self.start_playback() self.menuitem['Mute Audio'].set_active(self.config.mute_audio) # To ensure thread safe GLib.idle_add(_run) def _on_eos(self, *args): self.video_playback.set_progress(0.0) self.video_playback.set_audio_volume(0.0 if self.config.mute_audio else self.config.audio_volume) self.start_playback() def _on_menuitem_main_gui(self, *args): ControlPanel().run() def _on_menuitem_mute_audio(self, item): self.config.mute_audio = item.get_active() self.config_handler.save() def _on_menuitem_pause_playback(self, item): self.user_pause_playback = item.get_active() self.pause_playback() if self.user_pause_playback else self.start_playback() def _on_menuitem_feeling_lucky(self, *args): self.current += 1 if self.current % len(self.file_list) == 0: random.shuffle(self.file_list) self.config.video_path = self.file_list[self.current % len(self.file_list)] self.config_handler.save() def _on_menuitem_gnome_settings(self, *args): subprocess.Popen('gnome-control-center') def _on_menuitem_quit(self, *args): self._quit() def _build_context_menu(self): self.menu = Gtk.Menu() items = [('Show Hidamari', self._on_menuitem_main_gui, Gtk.MenuItem), ('Mute Audio', self._on_menuitem_mute_audio, Gtk.CheckMenuItem), ('Pause Playback', self._on_menuitem_pause_playback, Gtk.CheckMenuItem), ('I\'m Feeling Lucky', self._on_menuitem_feeling_lucky, Gtk.MenuItem), ('Quit Hidamari', self._on_menuitem_quit, Gtk.MenuItem)] self.menuitem = defaultdict() if 'gnome' in os.environ['XDG_CURRENT_DESKTOP'].lower(): items += [(None, None, Gtk.SeparatorMenuItem), ('GNOME Settings', self._on_menuitem_gnome_settings, Gtk.MenuItem)] for item in items: label, handler, item_type = item if item_type == Gtk.SeparatorMenuItem: self.menu.append(Gtk.SeparatorMenuItem()) else: menuitem = item_type.new_with_label(label) menuitem.connect('activate', handler) menuitem.set_margin_top(4) menuitem.set_margin_bottom(4) self.menu.append(menuitem) self.menuitem[label] = menuitem self.menu.show_all() def _on_button_press_event(self, widget, event): if event.type == Gdk.EventType.BUTTON_PRESS and event.button == 3: self.menu.popup_at_pointer() return True def _on_not_implemented(self, *args): print('Not implemented!') message = Gtk.MessageDialog(type=Gtk.MessageType.INFO, buttons=Gtk.ButtonsType.OK, message_format='Not implemented!') message.connect("response", self._dialog_response) message.show() def _on_file_not_found(self, path): print('File not found!') message = Gtk.MessageDialog(type=Gtk.MessageType.ERROR, buttons=Gtk.ButtonsType.OK, message_format='File {} not found!'.format(path)) message.connect("response", self._dialog_response) message.show() def _dialog_response(self, widget, response_id): if response_id == Gtk.ResponseType.OK: widget.destroy()
@staticmethod def process_commands(command): command = command.decode() print("Got command <" + str(command) +">") command = command.split(' ') if command[0] == "HANDSHAKE": pass elif command[0] == "200": pass elif command[0] == "TURNON": Device.set_high(int(command[1])) elif command[0] == "TURNOFF": Device.set_low(int(command[1])) else: print("Unknown command found: '" + str(command) +"'") @staticmethod def onboard_led(on): if on: Device.pins[2].on() else: Device.pins[2].off() ConfigHandler.set_config_name("wifi_config.json") ConfigHandler.load() NetworkHandler.connect() ConnectionHandler.find_server_ip() Device.setup_pins() ThreadObject(ConnectionHandler.listen(ConnectionHandler.get_server_addr, Device.process_commands)).start()