This repository was archived by the owner on Oct 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
145 lines (123 loc) · 4.32 KB
/
main.go
File metadata and controls
145 lines (123 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
il "github.com/tuxx/fancylock/internal"
)
func main() {
// Parse command-line flags
configPath := flag.String("c", "", "Path to configuration file")
flag.StringVar(configPath, "config", "", "Path to configuration file")
lockScreen := flag.Bool("l", false, "Lock the screen immediately")
flag.BoolVar(lockScreen, "lock", false, "Lock the screen immediately")
helpFlag := flag.Bool("h", false, "Display help information")
flag.BoolVar(helpFlag, "help", false, "Display help information")
debugExit := flag.Bool("debug-exit", false, "Enable exit with ESC or Q key (for debugging)")
debugMode := flag.Bool("log", false, "Enable debug logging")
flagVersion := flag.Bool("v", false, "Show version info")
flag.BoolVar(flagVersion, "version", false, "Show version info")
// Set custom usage output
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "FancyLock: A media-playing screen locker\n\n")
fmt.Fprintf(os.Stderr, "Usage: %s [options]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Options:\n")
fmt.Fprintf(os.Stderr, " -c, --config string\n Path to configuration file\n")
fmt.Fprintf(os.Stderr, " -l, --lock\n Lock the screen immediately\n")
fmt.Fprintf(os.Stderr, " -h, --help\n Display help information\n")
fmt.Fprintf(os.Stderr, " --debug-exit\n Enable exit with ESC or Q key (for debugging)\n")
fmt.Fprintf(os.Stderr, " --log\n Enable debug logging\n")
fmt.Fprintf(os.Stderr, " -v, --version\n Show version info\n")
fmt.Fprintf(os.Stderr, "\nExamples:\n")
fmt.Fprintf(os.Stderr, " %s -l # Lock screen immediately\n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s -c /path/to/config # Use specific config file\n", os.Args[0])
}
flag.Parse()
// Initialize the logger
if *debugMode {
il.InitLogger(il.LevelDebug, true)
il.Debug("Debug logging enabled")
} else {
il.InitLogger(il.LevelError, false)
}
// Show help if explicitly requested or if no arguments provided and no action flags set
if *helpFlag || (flag.NFlag() == 0 && !*lockScreen) {
flag.Usage()
return
}
if *flagVersion {
fmt.Printf("Fancylock version %s (%s) built on %s\n", il.Version, il.Commit, il.BuildDate)
return
}
// Load default configuration
config := il.DefaultConfig()
config.LockScreen = *lockScreen
config.DebugExit = *debugExit
// Try to find and load config file
if *configPath == "" {
// Try default locations
homeDir, err := os.UserHomeDir()
if err == nil {
defaultConfigPath := filepath.Join(homeDir, ".config", "fancylock", "config.json")
if _, err := os.Stat(defaultConfigPath); err == nil {
// Default config exists, use it
log.Printf("Using default config file: %s", defaultConfigPath)
*configPath = defaultConfigPath
}
}
}
// If config file is provided or found, load it
if *configPath != "" {
err := il.LoadConfig(*configPath, &config)
if err != nil {
log.Printf("Error loading config: %v", err)
// Continue with default config
}
}
// Initialize display server detection
displayServer := DetectDisplayServer()
fmt.Printf("Detected display server: %s\n", displayServer)
// Initialize the screen locker based on display server
var locker il.ScreenLocker
switch displayServer {
case "hyprland":
log.Printf("Using Hyprland-specific Wayland locker")
locker = il.NewWaylandLocker(config)
case "wayland":
// We'll implement Wayland support later
log.Fatalf("Wayland support not yet implemented")
case "x11":
locker = il.NewX11Locker(config)
default:
log.Fatalf("Unsupported display server: %s", displayServer)
}
// If -l/--lock flag is set, lock immediately
if config.LockScreen {
// Not a WaylandLocker, use the regular Lock method
if err := locker.Lock(); err != nil {
log.Fatalf("Failed to lock screen: %v", err)
}
}
}
// DetectDisplayServer detects whether X11 or Wayland is being used
func DetectDisplayServer() string {
// Check for Hyprland specifically
hyprlandSignature := os.Getenv("HYPRLAND_INSTANCE_SIGNATURE")
if hyprlandSignature != "" {
return "hyprland"
}
// Check for Wayland session
waylandDisplay := os.Getenv("WAYLAND_DISPLAY")
if waylandDisplay != "" {
return "wayland"
}
// Check for X11 session
xdgSession := os.Getenv("XDG_SESSION_TYPE")
if xdgSession == "x11" {
return "x11"
}
// Default to X11 if can't determine
return "x11"
}