Skip to content

Instantly share code, notes, and snippets.

@scriptum
Created February 27, 2017 21:32
Show Gist options
  • Save scriptum/10b85893a82ac85cd65d08bfbaa3d359 to your computer and use it in GitHub Desktop.
Save scriptum/10b85893a82ac85cd65d08bfbaa3d359 to your computer and use it in GitHub Desktop.
public class ScreenSaverApp : Gtk.Application {
private Gtk.CssProvider provider;
private Gee.ArrayList<string> files;
private const string images_dir = "/usr/share/mga/screensaver/";
private int files_max = 0;
private int files_current = 0;
private const string css_tmpl = """
* {
transition-property: background-color, background-image;
transition-duration: 1s;
background-color: #000;
background-size: contain;
background-size: cover;
background-origin: content-box;
background-repeat: no-repeat;
background-position: center;
background-image: url("%s");
}
""";
private bool change_css()
{
try {
provider.load_from_data (css_tmpl.printf (images_dir + files[files_current]), -1);
} catch (Error e) {
stderr.printf ("Error: %s\n", e.message);
}
files_current++;
if (files_current > files_max) {
files_current = 0;
}
return true;
}
public ScreenSaverApp () {
Object(application_id: "z.team.ScreenSaver",
flags: ApplicationFlags.FLAGS_NONE);
}
private int parse_xwin (string? xwinstr) {
var xwin = 0;
if (xwinstr != null)
{
xwin = int.parse (xwinstr);
if (xwin == 0) {
xwinstr.scanf ("0[xX]%x", &xwin);
}
if (xwin == 0) {
xwinstr.scanf ("0[xX]%X", &xwin);
}
}
return xwin;
}
public override void activate () {
try {
files = new Gee.ArrayList<string>();
var directory = File.new_for_path (images_dir);
var enumerator = directory.enumerate_children (FileAttribute.STANDARD_NAME, 0);
FileInfo file_info;
while ((file_info = enumerator.next_file ()) != null) {
files_max++;
files.insert (GLib.Random.int_range (0, files_max), file_info.get_name ());
}
files_max--;
} catch (Error e) {
stderr.printf ("Error: %s\n", e.message);
return;
}
var win = new Gtk.Window ();
win.destroy.connect (Gtk.main_quit);
provider = new Gtk.CssProvider ();
win.get_style_context ().add_provider (provider, uint.MAX);
change_css();
GLib.Timeout.add (10000, change_css);
var xwinstr = GLib.Environment.get_variable ("XSCREENSAVER_WINDOW");
var xwin = parse_xwin(xwinstr);
if (xwin != 0) {
win.realize ();
var display = Gdk.X11.Display.lookup_for_xdisplay (Gdk.X11.get_default_xdisplay ());
var fwin = new Gdk.X11.Window.foreign_for_display (display, xwin);
var old_win = win.get_window ();
var screen = Gdk.Screen.get_default ();
var middle_monitor = ((screen.get_n_monitors () - 1)/2);
Gdk.Rectangle geometry;
screen.get_monitor_geometry (middle_monitor, out geometry);
old_win.reparent (fwin, 0, 0);
win.move (geometry.x, geometry.y);
win.set_default_size (geometry.width, geometry.height);
}
win.show_all ();
Gtk.main ();
}
public static int main (string[] args) {
var app = new ScreenSaverApp ();
return app.run (args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment