Skip to content

Commit

Permalink
Merge pull request #3 from kotontrion/docs/examples
Browse files Browse the repository at this point in the history
add examples
  • Loading branch information
Cu3PO42 authored Apr 2, 2024
2 parents 00599bf + b47da38 commit 99bd456
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This is a library to use [GTK 3](https://www.gtk.org/) to build screen lockers using the secure [ext-session-lock-v1](https://wayland.app/protocols/ext-session-lock-v1) protocol. This Library is compatible with C, C++ and any language that supports GObject introspection files (Python, Vala, etc, see using the library below).

This library is a fork of the incrediable [gtk-layer-shell](https://github.com/wmww/gtk-layer-shell), which has laid all the groundwork necessary to make this happen.
This library is a fork of the incredible [gtk-layer-shell](https://github.com/wmww/gtk-layer-shell), which has laid all the groundwork necessary to make this happen.

## Reporting Bugs
To report a crash or other problem using this library open a new [issue on Github](https://github.com/Cu3PO42/gtk-session-lock/issues). Try to include a minimum reproducer if possible (ideally in C). **DO NOT REPORT GTK SESSION LOCK BUGS TO UPSTREAM GTK**. If you can reproduce the problem without including or linking to the gtk-session-lock library **at all** then and only then report it to GTK instead of here.
Expand Down
6 changes: 6 additions & 0 deletions examples/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
executable(
'simple-example-c',
files('simple-example.c'),
build_by_default: get_option('examples'),
dependencies: [gtk, gtk_session_lock],
install: false)
59 changes: 59 additions & 0 deletions examples/simple-example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "gtk-session-lock.h"
#include <gtk/gtk.h>
#include <stdio.h>

GtkSessionLockLock *lock;
GtkApplication *app;

static void unlock( GtkWidget *widget, gpointer data) {

gtk_session_lock_lock_unlock_and_destroy(lock);

gdk_display_sync(gdk_display_get_default());
g_application_quit(G_APPLICATION(app));
}

static GtkWindow* create_lock_window() {
GtkWindow *window = GTK_WINDOW (gtk_application_window_new(app));
GtkWidget *entry = gtk_entry_new();
gtk_entry_set_visibility(GTK_ENTRY(entry),FALSE);
gtk_widget_set_valign(entry, GTK_ALIGN_CENTER);
gtk_widget_set_halign(entry, GTK_ALIGN_CENTER);

gtk_container_add(GTK_CONTAINER(window), entry);

g_signal_connect(G_OBJECT(entry), "activate", G_CALLBACK(unlock), NULL);

return window;
}

static void activate (GtkApplication* app, void *_data) {

if (!gtk_session_lock_is_supported()) {
printf("Your Wayland compositor does not support the ext-session-lock protocol\n");
g_application_quit(G_APPLICATION(app));
}

lock = gtk_session_lock_prepare_lock();

gtk_session_lock_lock_lock(lock);

GdkDisplay *display = gdk_display_get_default();
for (int i = 0; i < gdk_display_get_n_monitors(display); ++i) {
GdkMonitor *monitor = gdk_display_get_monitor(gdk_display_get_default(), i);

GtkWindow *window = create_lock_window();
gtk_session_lock_lock_new_surface(lock, window, monitor);

gtk_widget_show_all(GTK_WIDGET(window));
}
}

int main (int argc, char **argv) {

app = gtk_application_new ("Cu3PO42.gtk-session-lock-example", G_APPLICATION_DEFAULT_FLAGS);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
int status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
35 changes: 35 additions & 0 deletions examples/simple-example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('GtkSessionLock', '0.1')
from gi.repository import Gtk, Gdk, GtkSessionLock

if(not GtkSessionLock.is_supported()):
quit()

lock = GtkSessionLock.prepare_lock()
display = Gdk.Display.get_default()

def unlock(widget):
lock.unlock_and_destroy()
display.sync()
quit()

def create_lock_window():
window = Gtk.Window()
entry = Gtk.Entry(visibility=False,
valign=Gtk.Align.CENTER,
halign=Gtk.Align.CENTER)
entry.connect("activate", unlock)
window.add(entry)
return window

lock.lock_lock()

for i in range(display.get_n_monitors()):
window = create_lock_window()
monitor = display.get_monitor(i)
lock.new_surface(window, monitor)
window.show_all()


Gtk.main()
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ gtk_session_lock = declare_dependency(
link_with: gtk_session_lock_lib,
include_directories: gtk_session_lock_inc)

#subdir('examples')
subdir('examples')

if get_option('tests')
subdir('test')
Expand Down

0 comments on commit 99bd456

Please sign in to comment.