-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXlock_v2.c
More file actions
143 lines (119 loc) · 4.33 KB
/
Copy pathXlock_v2.c
File metadata and controls
143 lines (119 loc) · 4.33 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
#include <X11/Xlib.h>
#include <X11/Xft/Xft.h>
#include <X11/keysym.h>
#include <locale.h>
#include <stdlib.h>
#include <unistd.h>
#define PASSWORD "123"
#define FONT_NAME "DejaVu Sans:size=24:antialias=true"
#define DOT_RADIUS 8
#define DOT_SPACING 20
#define BACKGROUND_COLOR "#000000" // Черный фон
#define TEXT_COLOR "#FFFFFF" // Белый текст
typedef struct {
Display* dpy;
Window win;
XftFont* font;
XftDraw* draw;
XftColor text_color;
XftColor bg_color;
Visual* visual;
Colormap colormap;
int screen;
int width;
int height;
} LockState;
void init_x11(LockState* s) {
s->dpy = XOpenDisplay(NULL);
s->screen = DefaultScreen(s->dpy);
s->width = DisplayWidth(s->dpy, s->screen);
s->height = DisplayHeight(s->dpy, s->screen);
// Получаем визуал и цветовую карту
s->visual = DefaultVisual(s->dpy, s->screen);
s->colormap = DefaultColormap(s->dpy, s->screen);
// Настройка окна
XSetWindowAttributes attrs = {
.override_redirect = True,
.background_pixel = BlackPixel(s->dpy, s->screen),
.border_pixel = BlackPixel(s->dpy, s->screen)
};
s->win = XCreateWindow(s->dpy, RootWindow(s->dpy, s->screen),
0, 0, s->width, s->height, 0,
CopyFromParent, InputOutput, s->visual,
CWOverrideRedirect | CWBackPixel | CWBorderPixel,
&attrs);
XMapWindow(s->dpy, s->win);
XGrabKeyboard(s->dpy, s->win, True, GrabModeAsync, GrabModeAsync, CurrentTime);
// Инициализация Xft
s->font = XftFontOpenName(s->dpy, s->screen, FONT_NAME);
s->draw = XftDrawCreate(s->dpy, s->win, s->visual, s->colormap);
// Цвета
XftColorAllocName(s->dpy, s->visual, s->colormap, TEXT_COLOR, &s->text_color);
XftColorAllocName(s->dpy, s->visual, s->colormap, BACKGROUND_COLOR, &s->bg_color);
}
void draw_centered_text(LockState* s, const char* text,int k) {
XGlyphInfo extents;
XftTextExtentsUtf8(s->dpy, s->font, (FcChar8*)text, strlen(text), &extents);
int x = (s->width - extents.width) / 2;
int y = (s->height - extents.height) / 2;
XftDrawStringUtf8(s->draw, &s->text_color, s->font, x, y+k,
(FcChar8*)text, strlen(text));
}
void draw_password_dots(LockState* s, int count) {
int total_width = (DOT_RADIUS * 2 + DOT_SPACING) * count - DOT_SPACING;
int start_x = (s->width - total_width) / 2;
int y = s->height / 2 + 50;
for (int i = 0; i < count; i++) {
XFillArc(s->dpy, s->win, DefaultGC(s->dpy, s->screen),
start_x + i * (DOT_RADIUS * 2 + DOT_SPACING), y - DOT_RADIUS,
DOT_RADIUS * 2, DOT_RADIUS * 2, 0, 360 * 64);
}
}
void lock_loop(LockState* s) {
XEvent ev;
char pass[64] = {0};
int pass_len = 0;
int attempts = 0;
while (1) {
// Очистка экрана
XClearWindow(s->dpy, s->win);
// Отрисовка текста
draw_centered_text(s, "СИСТЕМА ЗАБЛОКИРОВАНА",-30);
draw_password_dots(s, pass_len);
// Отрисовка сообщения о неверном пароле
if (attempts > 0) {
char buf[64];
snprintf(buf, sizeof(buf), "Неверный пароль (%d попытка)", attempts);
draw_centered_text(s, buf,20);
}
XFlush(s->dpy);
// Обработка событий
XNextEvent(s->dpy, &ev);
if (ev.type == KeyPress) {
KeySym keysym;
char buffer[8];
int len = XLookupString(&ev.xkey, buffer, sizeof(buffer), &keysym, NULL);
if (keysym == XK_Return) {
if (strcmp(pass, PASSWORD) == 0) break;
attempts++;
pass[0] = '\0';
pass_len = 0;
} else if (keysym == XK_BackSpace && pass_len > 0) {
pass[--pass_len] = '\0';
} else if (pass_len < sizeof(pass) - 1 && len > 0) {
pass[pass_len++] = buffer[0];
pass[pass_len] = '\0';
}
}
}
}
int main() {
setlocale(LC_ALL, "");
LockState state = {0};
init_x11(&state);
lock_loop(&state);
XUngrabKeyboard(state.dpy, CurrentTime);
XDestroyWindow(state.dpy, state.win);
XCloseDisplay(state.dpy);
return 0;
}