Skip to content

Commit

Permalink
[FEAT] integrated session lock protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
nots1dd committed Oct 16, 2024
1 parent 7affde1 commit 47312fa
Show file tree
Hide file tree
Showing 8 changed files with 451 additions and 170 deletions.
19 changes: 19 additions & 0 deletions client_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
#include <stdbool.h>
#include <xkbcommon/xkbcommon.h>

// Define a structure to store output-related information
struct output_state {
uint32_t id;
int32_t width;
int32_t height;
int32_t refresh_rate;
struct wl_output *wl_output;
};

struct pointer_event
{
uint32_t event_mask; // Masks for various event types
Expand All @@ -31,6 +40,7 @@ struct client_state
struct wl_compositor* wl_compositor; // Compositor interface
struct xdg_wm_base* xdg_wm_base; // XDG window manager base interface
struct wl_seat* wl_seat; // Input device seat
struct wl_output *wl_output;
/* Objects */
struct wl_surface* wl_surface; // Wayland surface
struct xdg_surface* xdg_surface; // XDG surface
Expand All @@ -52,6 +62,15 @@ struct client_state
char password[256];
int password_index;
bool authenticated;
bool locked;
/* SESSION LOCK */
bool surface_created;
bool surface_dirty;
struct ext_session_lock_manager_v1 *ext_session_lock_manager_v1;
struct ext_session_lock_v1 *ext_session_lock_v1;
struct ext_session_lock_surface_v1 *ext_session_lock_surface_v1;
/* wl_output */
struct output_state output_state;
};

#endif
128 changes: 80 additions & 48 deletions log.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,83 +12,115 @@
/* Define log importance levels */
enum log_importance
{
LOG_LEVEL_SILENT,
LOG_LEVEL_ERROR,
LOG_LEVEL_WARN,
LOG_LEVEL_INFO,
LOG_LEVEL_AUTH,
LOG_LEVEL_SUCCESS,
LOG_LEVEL_DEBUG,
LOG_IMPORTANCE_LAST // Keep this as the last element to define the range
LOG_LEVEL_SILENT,
LOG_LEVEL_ERROR,
LOG_LEVEL_WARN,
LOG_LEVEL_INFO,
LOG_LEVEL_AUTH,
LOG_LEVEL_SUCCESS,
LOG_LEVEL_DEBUG,
LOG_IMPORTANCE_LAST // Keep this as the last element to define the range
};

/* Current log importance level */
static enum log_importance log_importance = LOG_LEVEL_DEBUG;

/* Define verbosity colors */
static const char* verbosity_colors[] = {
[LOG_LEVEL_SILENT] = "",
[LOG_LEVEL_ERROR] = "\x1B[1;31m", // Red
[LOG_LEVEL_WARN] = "\x1B[1;33m", // Yellow
[LOG_LEVEL_INFO] = "\x1B[1;34m", // Blue
[LOG_LEVEL_DEBUG] = "\x1B[1;30m", // Dark Gray
[LOG_LEVEL_AUTH] = "\x1B[1;35m", // Pink (Magenta)
[LOG_LEVEL_SUCCESS] = "\x1B[1;32m" // Green
[LOG_LEVEL_SILENT] = "",
[LOG_LEVEL_ERROR] = "\x1B[1;31m", // Red
[LOG_LEVEL_WARN] = "\x1B[1;33m", // Yellow
[LOG_LEVEL_INFO] = "\x1B[1;34m", // Blue
[LOG_LEVEL_DEBUG] = "\x1B[1;30m", // Dark Gray
[LOG_LEVEL_AUTH] = "\x1B[1;35m", // Pink (Magenta)
[LOG_LEVEL_SUCCESS] = "\x1B[1;32m" // Green
};

/* Log file pointer */
static FILE* log_file = NULL;

/* Function to initialize logging with a specified verbosity level */
void log_init(enum log_importance verbosity)
{
if (verbosity < LOG_IMPORTANCE_LAST)
{
log_importance = verbosity;
}
if (verbosity < LOG_IMPORTANCE_LAST)
{
log_importance = verbosity;
}

// Open the log file for appending, create it if it does not exist
log_file = fopen("casuality.log", "a");
if (log_file == NULL)
{
fprintf(stderr, "Failed to open log file: %s\n", strerror(errno));
}
}

/* Logging function */
void log_message(enum log_importance verbosity, const char* fmt, ...)
{
if (verbosity > log_importance)
{
return;
}
if (verbosity > log_importance)
{
return;
}

va_list args;
va_start(args, fmt);

// Prefix the time to the log message
time_t t = time(NULL);
struct tm* tm_info = localtime(&t);
char buffer[26];
strftime(buffer, sizeof(buffer), "[%F %T] - ", tm_info);

// Log to console
fprintf(stderr, "%s", buffer);

va_list args;
va_start(args, fmt);
unsigned c = (verbosity < LOG_IMPORTANCE_LAST) ? verbosity : LOG_IMPORTANCE_LAST - 1;

// Prefix the time to the log message
time_t t = time(NULL);
struct tm* tm_info = localtime(&t);
char buffer[26];
strftime(buffer, sizeof(buffer), "[%F %T] - ", tm_info);
fprintf(stderr, "%s", buffer);
if (isatty(STDERR_FILENO))
{
fprintf(stderr, "%s", verbosity_colors[c]);
}

unsigned c = (verbosity < LOG_IMPORTANCE_LAST) ? verbosity : LOG_IMPORTANCE_LAST - 1;
vfprintf(stderr, fmt, args);

if (isatty(STDERR_FILENO))
{
fprintf(stderr, "%s", verbosity_colors[c]);
}
if (isatty(STDERR_FILENO))
{
fprintf(stderr, "\x1B[0m"); // Reset color
}

vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");

if (isatty(STDERR_FILENO))
{
fprintf(stderr, "\x1B[0m"); // Reset color
}
// Also log to the file
if (log_file != NULL)
{
fprintf(log_file, "%s", buffer);
vfprintf(log_file, fmt, args);
fprintf(log_file, "\n");
fflush(log_file); // Flush to ensure it is written immediately
}

fprintf(stderr, "\n");
va_end(args);
va_end(args);
}

/* Optional function to strip leading './' from file paths */
const char* strip_path(const char* filepath)
{
while (*filepath == '.' || *filepath == '/')
{
++filepath;
}
return filepath;
while (*filepath == '.' || *filepath == '/')
{
++filepath;
}
return filepath;
}

/* Function to clean up logging resources */
void log_cleanup()
{
if (log_file != NULL)
{
fclose(log_file);
log_file = NULL;
}
}

#endif // LOG_H
99 changes: 55 additions & 44 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
#include "wl_seat_handle.h"
#include "xdg_surface_handle.h"
#include "xdg_wm_base_handle.h"
#include "wl_output_handle.h"
#include <unistd.h>
#include <xkbcommon/xkbcommon.h>
/*#include "session_lock_handle.h"*/
#include "session_lock_handle.h"

/**********************************************
* @WAYLAND CLIENT EXAMPLE CODE
Expand Down Expand Up @@ -97,56 +98,66 @@
* the Wayland compositor through various protocols and listeners.
**********************************************/

int main(int argc, char* argv[])
{
struct client_state state = {0};
/* struct session_lock_state lock_state = { 0 }; // Add this line for session lock state */
int main(int argc, char* argv[]) {
struct client_state state = {0};

// Connect to the Wayland display
state.wl_display = wl_display_connect(NULL);
state.username = getlogin();
log_message(LOG_LEVEL_INFO, "Found User @ %s", state.username);
state.firstEnterPress = true;
if (!state.wl_display)
{
fprintf(stderr, "Failed to connect to Wayland display\n");
return -1;
}
// Initialize and connect to the Wayland display
state.wl_display = wl_display_connect(NULL);
state.username = getlogin();
log_message(LOG_LEVEL_INFO, "Found User @ %s", state.username);

// Get the registry and set up listeners
state.wl_registry = wl_display_get_registry(state.wl_display);
state.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
wl_registry_add_listener(state.wl_registry, &wl_registry_listener, &state);
wl_display_roundtrip(state.wl_display);
if (!state.wl_display) {
log_message(LOG_LEVEL_ERROR, "Failed to connect to Wayland display\n");
return -1;
}

// Bind session lock protocol
/* bind_session_lock(&lock_state); // Call the binding function */
// Get the registry and set up listeners
state.wl_registry = wl_display_get_registry(state.wl_display);
state.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);

// Create a Wayland surface and set it up as an XDG surface
state.wl_surface = wl_compositor_create_surface(state.wl_compositor);
state.xdg_surface = xdg_wm_base_get_xdg_surface(state.xdg_wm_base, state.wl_surface);
xdg_surface_add_listener(state.xdg_surface, &xdg_surface_listener, &state);
state.xdg_toplevel = xdg_surface_get_toplevel(state.xdg_surface);
xdg_toplevel_set_title(state.xdg_toplevel, "Something man");
// Add listeners for registry objects
wl_registry_add_listener(state.wl_registry, &wl_registry_listener, &state);

state.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
state.xkb_keymap =
xkb_keymap_new_from_names(state.xkb_context, NULL, XKB_KEYMAP_COMPILE_NO_FLAGS);
state.xkb_state = xkb_state_new(state.xkb_keymap);
// Roundtrip to process the registry and get the compositor, shm, seat, etc.
wl_display_roundtrip(state.wl_display);

// Add keyboard listener for capturing password input
wl_seat_add_listener(state.wl_seat, &wl_seat_listener, &state);
// Create the Wayland surface and initialize XDG shell surface
state.wl_surface = wl_compositor_create_surface(state.wl_compositor);
state.xdg_surface = xdg_wm_base_get_xdg_surface(state.xdg_wm_base, state.wl_surface);

// Add listeners for XDG surface events
xdg_surface_add_listener(state.xdg_surface, &xdg_surface_listener, &state);

wl_surface_commit(state.wl_surface);
// Create the XDG toplevel (window management)
state.xdg_toplevel = xdg_surface_get_toplevel(state.xdg_surface);
xdg_toplevel_set_title(state.xdg_toplevel, "Screen Lock");

// Continue running the event loop until the user authenticates
while (!state.authenticated && wl_display_dispatch(state.wl_display))
{
/* Process events */
}
// Initialize XKB for handling keyboard input
state.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
state.xkb_keymap = xkb_keymap_new_from_names(state.xkb_context, NULL, XKB_KEYMAP_COMPILE_NO_FLAGS);
state.xkb_state = xkb_state_new(state.xkb_keymap);

// Clean up Wayland resources before exiting
wl_display_disconnect(state.wl_display);
log_message(LOG_LEVEL_SUCCESS, "Unlocking...");
return 0;
// Initialize pointer and keyboard state
state.authenticated = false; // Initialize authentication state

// Add listeners for seat (input devices like keyboard)
wl_seat_add_listener(state.wl_seat, &wl_seat_listener, &state);

// Commit the surface to make it visible
wl_surface_commit(state.wl_surface);

// Enter event loop for handling lock state and input
while (!state.authenticated && wl_display_dispatch(state.wl_display) != -1) {
// Check if the session needs to be locked
if (!state.surface_created) {
initiate_session_lock(&state);
}
}

// Disconnect from the Wayland display
unlock_and_destroy_session_lock(&state);
wl_display_roundtrip(state.wl_display);
wl_display_disconnect(state.wl_display);
log_message(LOG_LEVEL_SUCCESS, "Unlocking...");
return 0;
}
Binary file added muvilock
Binary file not shown.
Loading

0 comments on commit 47312fa

Please sign in to comment.