Skip to content

Commit

Permalink
Merge pull request #43 from jovanlanik/xml-ui
Browse files Browse the repository at this point in the history
XML UI
  • Loading branch information
jovanlanik authored Oct 16, 2022
2 parents b0869f0 + 1ba86fd commit f15184a
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 196 deletions.
2 changes: 0 additions & 2 deletions include/gtklock.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ struct GtkLock {
GArray *modules;
};

struct Window *gtklock_window_by_widget(struct GtkLock *gtklock, GtkWidget *window);
struct Window *gtklock_window_by_monitor(struct GtkLock *gtklock, GdkMonitor *monitor);
void gtklock_remove_window(struct GtkLock *gtklock, struct Window *win);
void gtklock_focus_window(struct GtkLock *gtklock, struct Window *win);
void gtklock_update_clocks(struct GtkLock *gtklock);
Expand Down
4 changes: 2 additions & 2 deletions include/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ GModule *module_load(const char *name);
void module_on_activation(struct GtkLock *gtklock);
void module_on_output_change(struct GtkLock *gtklock);
void module_on_focus_change(struct GtkLock *gtklock, struct Window *win, struct Window *old);
void module_on_window_empty(struct GtkLock *gtklock, struct Window *ctx);
void module_on_body_empty(struct GtkLock *gtklock, struct Window *ctx);
void module_on_idle_hide(struct GtkLock *gtklock);
void module_on_idle_show(struct GtkLock *gtklock);
void module_on_window_create(struct GtkLock *gtklock, struct Window *win);
void module_on_window_destroy(struct GtkLock *gtklock, struct Window *win);

6 changes: 5 additions & 1 deletion include/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct Window {
GtkWidget *window;
GtkWidget *overlay;
GtkWidget *window_box;
GtkWidget *body_revealer;
GtkWidget *body_grid;
GtkWidget *input_label;
GtkWidget *input_field;
Expand All @@ -27,8 +28,11 @@ struct Window {
void *module_data[];
};

struct Window *window_by_widget(GtkWidget *window);
struct Window *window_by_monitor(GdkMonitor *monitor);
struct Window *create_window(GdkMonitor *monitor);
void window_configure(struct Window *win);
void window_idle_hide(struct Window *win);
void window_idle_show(struct Window *win);
void window_update_clock(struct Window *ctx);
void window_swap_focus(struct Window *win, struct Window *old);

21 changes: 15 additions & 6 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@
# Makefile

NAME := gtklock
MAJOR_VERSION := 1
MINOR_VERSION := 4
MAJOR_VERSION := 2
MINOR_VERSION := 0
MICRO_VERSION := 0

PREFIX = /usr/local
INSTALL = install

LIBS := pam wayland-client gtk+-wayland-3.0 gtk-layer-shell-0 gmodule-no-export-2.0
LIBS := pam wayland-client gtk+-wayland-3.0 gtk-layer-shell-0 gmodule-export-2.0
CFLAGS += -std=c11 -Iinclude -DPREFIX=$(PREFIX) $(shell pkg-config --cflags $(LIBS))
CFLAGS += -DMAJOR_VERSION=$(MAJOR_VERSION) -DMINOR_VERSION=$(MINOR_VERSION) -DMICRO_VERSION=$(MICRO_VERSION)
LDLIBS += -Wl,--export-dynamic $(shell pkg-config --libs $(LIBS))

SRC = $(wildcard src/*.c)
OBJ = wlr-input-inhibitor-unstable-v1-client-protocol.o $(SRC:src/%.c=%.o)
OBJ = wlr-input-inhibitor-unstable-v1-client-protocol.o
OBJ += $(patsubst %.c, %.o, $(wildcard src/*.c))
OBJ += $(patsubst res/%.gresource.xml, %.gresource.o, $(wildcard res/*.gresource.xml))

TRASH = $(OBJ) $(NAME) $(NAME).1 $(wildcard *-client-protocol.c) $(wildcard include/*-client-protocol.h)
TRASH = $(OBJ) $(NAME) $(NAME).1
TRASH += $(wildcard *.gresource.c) $(wildcard *.gresource.h)
TRASH += $(wildcard *-client-protocol.c) $(wildcard include/*-client-protocol.h)

VPATH = src
.PHONY: all clean install install-bin install-data uninstall
Expand Down Expand Up @@ -49,6 +52,12 @@ uninstall:
$(NAME): $(OBJ)
$(LINK.c) $^ $(LDLIBS) -o $@

%.gresource.c: res/%.gresource.xml
glib-compile-resources --generate-source $< --target=$@ --sourcedir=res

%.gresource.h: res/%.gresource.xml
glib-compile-resources --generate-header $< --target=$@ --sourcedir=res

%-client-protocol.c: wayland/%.xml
wayland-scanner private-code $< $@

Expand Down
102 changes: 102 additions & 0 deletions res/gtklock.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<object class="GtkBox" id="window-box">
<property name="name">window-box</property>
<property name="margin">100</property>
<property name="halign">center</property>
<property name="valign">center</property>
<property name="orientation">vertical</property>
<property name="spacing">5</property>
<child>
<object class="GtkLabel" id="clock-label">
<property name="name">clock-label</property>
<property name="halign">center</property>
<property name="margin-bottom">10</property>
<property name="label"></property>
</object>
</child>
<child>
<object class="GtkRevealer" id="body-revealer">
<property name="transition-type">none</property>
<property name="reveal-child">True</property>
<child>
<object class="GtkGrid" id="body-grid">
<property name="row-spacing">5</property>
<property name="column-spacing">5</property>
<child>
<object class="GtkLabel" id="input-label">
<property name="name">input-label</property>
<property name="label">Password:</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkEntry" id="input-field">
<property name="name">input-field</property>
<property name="width-request">380</property>
<property name="visibility">False</property>
<property name="caps-lock-warning">False</property>
<property name="input-purpose">password</property>
<signal name="icon-release" handler="window_pw_toggle_vis"/>
<signal name="activate" handler="window_pw_check"/>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkBox" id="message-box">
<property name="no-show-all">True</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">1</property>
<property name="width">2</property>
</packing>
</child>
<child>
<object class="GtkBox">
<property name="halign">end</property>
<property name="spacing">5</property>
<child>
<object class="GtkLabel" id="warning-label">
<property name="name">warning-label</property>
<property name="label"></property>
</object>
</child>
<child>
<object class="GtkLabel" id="error-label">
<property name="name">error-label</property>
<property name="label"></property>
</object>
</child>
<child>
<object class="GtkButton" id="unlock-button">
<property name="name">unlock-button</property>
<property name="label">Unlock</property>
<property name="can-focus">False</property>
<property name="receives-default">False</property>
<style>
<class name="suggested-action"/>
</style>
<signal name="clicked" handler="window_pw_check"/>
</object>
</child>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">2</property>
<property name="width">2</property>
</packing>
</child>
</object>
</child>
</object>
</child>
</object>
</interface>
6 changes: 6 additions & 0 deletions res/ui.gresource.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/gtklock">
<file preprocess="xml-stripblanks">gtklock.ui</file>
</gresource>
</gresources>
32 changes: 10 additions & 22 deletions src/gtklock.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,6 @@
#include "module.h"
#include "input-inhibitor.h"

struct Window* gtklock_window_by_widget(struct GtkLock *gtklock, GtkWidget *window) {
for(guint idx = 0; idx < gtklock->windows->len; idx++) {
struct Window *ctx = g_array_index(gtklock->windows, struct Window *, idx);
if(ctx->window == window) return ctx;
}
return NULL;
}

struct Window* gtklock_window_by_monitor(struct GtkLock *gtklock, GdkMonitor *monitor) {
for(guint idx = 0; idx < gtklock->windows->len; idx++) {
struct Window *ctx = g_array_index(gtklock->windows, struct Window *, idx);
if(ctx->monitor == monitor) return ctx;
}
return NULL;
}

void gtklock_remove_window(struct GtkLock *gtklock, struct Window *win) {
for(guint idx = 0; idx < gtklock->windows->len; idx++) {
struct Window *ctx = g_array_index(gtklock->windows, struct Window *, idx);
Expand All @@ -42,10 +26,6 @@ void gtklock_focus_window(struct GtkLock *gtklock, struct Window* win) {
struct Window *old = gtklock->focused_window;
gtklock->focused_window = win;
window_swap_focus(win, old);
for(guint idx = 0; idx < gtklock->windows->len; idx++) {
struct Window *ctx = g_array_index(gtklock->windows, struct Window *, idx);
if(ctx != win) window_configure(ctx);
}
module_on_focus_change(gtklock, win, old);
}

Expand Down Expand Up @@ -78,17 +58,25 @@ void gtklock_idle_hide(struct GtkLock *gtklock) {
if(!gtklock->use_idle_hide || gtklock->hidden || g_application_get_is_busy(G_APPLICATION(gtklock->app)))
return;
gtklock->hidden = TRUE;
if(gtklock->focused_window) window_configure(gtklock->focused_window);
module_on_idle_hide(gtklock);

for(guint idx = 0; idx < gtklock->windows->len; idx++) {
struct Window *ctx = g_array_index(gtklock->windows, struct Window *, idx);
window_idle_hide(ctx);
}
}

void gtklock_idle_show(struct GtkLock *gtklock) {
if(gtklock->hidden) {
gtklock->hidden = FALSE;
if(gtklock->focused_window) window_configure(gtklock->focused_window);
module_on_idle_show(gtklock);
}

for(guint idx = 0; idx < gtklock->windows->len; idx++) {
struct Window *ctx = g_array_index(gtklock->windows, struct Window *, idx);
window_idle_show(ctx);
}

if(!gtklock->use_idle_hide) return;
if(gtklock->idle_hide_source > 0) g_source_remove(gtklock->idle_hide_source);
gtklock->idle_hide_source = g_timeout_add_seconds(gtklock->idle_timeout, gtklock_idle_handler, gtklock);
Expand Down
24 changes: 12 additions & 12 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,35 +93,35 @@ void module_on_focus_change(struct GtkLock *gtklock, struct Window *win, struct
}
}

void module_on_window_empty(struct GtkLock *gtklock, struct Window *ctx) {
void module_on_idle_hide(struct GtkLock *gtklock) {
for(guint idx = 0; idx < gtklock->modules->len; idx++) {
void (*fn)(struct GtkLock *, struct Window *) = NULL;
void (*fn)(struct GtkLock *) = NULL;
GModule *module = g_array_index(gtklock->modules, GModule *, idx);
if(g_module_symbol(module, "on_window_empty", (gpointer *)&fn)) fn(gtklock, ctx);
if(g_module_symbol(module, "on_idle_hide", (gpointer *)&fn)) fn(gtklock);
}
}

void module_on_body_empty(struct GtkLock *gtklock, struct Window *ctx) {
void module_on_idle_show(struct GtkLock *gtklock) {
for(guint idx = 0; idx < gtklock->modules->len; idx++) {
void (*fn)(struct GtkLock *, struct Window *) = NULL;
void (*fn)(struct GtkLock *) = NULL;
GModule *module = g_array_index(gtklock->modules, GModule *, idx);
if(g_module_symbol(module, "on_body_empty", (gpointer *)&fn)) fn(gtklock, ctx);
if(g_module_symbol(module, "on_idle_show", (gpointer *)&fn)) fn(gtklock);
}
}

void module_on_idle_hide(struct GtkLock *gtklock) {
void module_on_window_create(struct GtkLock *gtklock, struct Window *win) {
for(guint idx = 0; idx < gtklock->modules->len; idx++) {
void (*fn)(struct GtkLock *) = NULL;
void (*fn)(struct GtkLock *, struct Window *) = NULL;
GModule *module = g_array_index(gtklock->modules, GModule *, idx);
if(g_module_symbol(module, "on_idle_hide", (gpointer *)&fn)) fn(gtklock);
if(g_module_symbol(module, "on_window_create", (gpointer *)&fn)) fn(gtklock, win);
}
}

void module_on_idle_show(struct GtkLock *gtklock) {
void module_on_window_destroy(struct GtkLock *gtklock, struct Window *win) {
for(guint idx = 0; idx < gtklock->modules->len; idx++) {
void (*fn)(struct GtkLock *) = NULL;
void (*fn)(struct GtkLock *, struct Window *) = NULL;
GModule *module = g_array_index(gtklock->modules, GModule *, idx);
if(g_module_symbol(module, "on_idle_show", (gpointer *)&fn)) fn(gtklock);
if(g_module_symbol(module, "on_window_destroy", (gpointer *)&fn)) fn(gtklock, win);
}
}

27 changes: 13 additions & 14 deletions src/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static void reload_outputs(void) {
struct Window *new = NULL;
for(int i = 0; i < gdk_display_get_n_monitors(display); i++) {
GdkMonitor *monitor = gdk_display_get_monitor(display, i);
struct Window *w = gtklock_window_by_monitor(gtklock, monitor);
struct Window *w = window_by_monitor(monitor);
if(w != NULL) {
// We already have this monitor, remove from dead_windows list
for(guint idx = 0; idx < dead_windows->len; idx++) {
Expand All @@ -101,7 +101,10 @@ static void reload_outputs(void) {
break;
}
}
} else w = create_window(monitor);
} else {
w = create_window(monitor);
gtklock_focus_window(gtklock, w);
}
new = w;
}

Expand All @@ -115,11 +118,6 @@ static void reload_outputs(void) {
gtk_widget_destroy(w->window);
}

for(guint idx = 0; idx < gtklock->windows->len; idx++) {
struct Window *w = g_array_index(gtklock->windows, struct Window*, idx);
window_configure(w);
}

g_array_unref(dead_windows);
module_on_output_change(gtklock);
}
Expand All @@ -129,13 +127,14 @@ static void monitors_changed(GdkDisplay *display, GdkMonitor *monitor) {
}

static gboolean setup_layer_shell(void) {
if(gtklock->use_layer_shell) {
reload_outputs();
GdkDisplay *display = gdk_display_get_default();
g_signal_connect(display, "monitor-added", G_CALLBACK(monitors_changed), NULL);
g_signal_connect(display, "monitor-removed", G_CALLBACK(monitors_changed), NULL);
return TRUE;
} else return FALSE;
if(!gtklock->use_layer_shell) return FALSE;

reload_outputs();

GdkDisplay *display = gdk_display_get_default();
g_signal_connect(display, "monitor-added", G_CALLBACK(monitors_changed), NULL);
g_signal_connect(display, "monitor-removed", G_CALLBACK(monitors_changed), NULL);
return TRUE;
}

static void activate(GtkApplication *app, gpointer user_data) {
Expand Down
Loading

0 comments on commit f15184a

Please sign in to comment.