-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import gi | ||
|
||
gi.require_version('Gtk', '4.0') | ||
gi.require_version('Adw', '1') | ||
from gi.repository import Gtk, Adw, GLib, Gio | ||
import os | ||
|
||
|
||
class ScreenRecorder: | ||
def __init__(self, parent_window): | ||
self.window = parent_window | ||
self.recording = False | ||
self.output_path = os.path.join(GLib.get_user_cache_dir(), "screen") | ||
self.init_proxy() | ||
|
||
def init_proxy(self): | ||
try: | ||
self.proxy = Gio.DBusProxy.new_for_bus_sync( | ||
Gio.BusType.SESSION, | ||
Gio.DBusProxyFlags.NONE, | ||
None, | ||
'org.gnome.Shell.Screencast', | ||
'/org/gnome/Shell/Screencast', | ||
'org.gnome.Shell.Screencast', | ||
None | ||
) | ||
except GLib.Error as e: | ||
self.show_error(str(e)) | ||
|
||
def start(self): | ||
if not self.recording: | ||
try: | ||
old_file = self.output_path + ".mp4" | ||
if os.path.exists(old_file): | ||
try: | ||
os.remove(old_file) | ||
except OSError as e: | ||
self.show_error(str(e)) | ||
return False | ||
|
||
success, path = self.proxy.call_sync( | ||
'Screencast', | ||
GLib.Variant( | ||
'(sa{sv})', | ||
(self.output_path, {}) | ||
), | ||
Gio.DBusCallFlags.NONE, | ||
-1, | ||
None | ||
) | ||
if success: | ||
self.recording = True | ||
return True | ||
|
||
except GLib.Error as e: | ||
self.show_error(str(e)) | ||
return False | ||
|
||
def stop(self, *args): | ||
if self.recording: | ||
try: | ||
self.proxy.call_sync( | ||
'StopScreencast', | ||
None, | ||
Gio.DBusCallFlags.NONE, | ||
-1, | ||
None | ||
) | ||
self.recording = False | ||
except GLib.Error as e: | ||
self.show_error(str(e)) | ||
|
||
def show_error(self, message): | ||
dialog = Adw.MessageDialog.new(self.window) | ||
dialog.set_heading("Error") | ||
dialog.set_body(str(message)) | ||
dialog.set_modal(True) | ||
dialog.add_response("ok", "OK") | ||
dialog.present() |