💾 Archived View for gemini.rmf-dev.com › repo › Vaati › dkee › files › b054f25fa42ad0971d502812013fb… captured on 2023-09-28 at 16:55:10. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2023-09-08)
-=-=-=-=-=-=-
0 #include <X11/Xlib.h>
1 #include <X11/Xutil.h>
2 #include <X11/Xos.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <locale.h>
6 #include <assert.h>
7
8 int text_box(char* input, size_t len) {
9 Display *dpy;
10 Window win;
11 XIM xim;
12 XIC ic;
13 int scr, ret = 0;
14 size_t pos = 0;
15 XClassHint *hint;
16
17 dpy = XOpenDisplay(NULL);
18 scr = DefaultScreen(dpy);
19 win = XCreateSimpleWindow(dpy, XDefaultRootWindow(dpy),
20 0, 0, 240, 30, 5,
21 BlackPixel(dpy, scr),
22 WhitePixel(dpy, scr));
23 XMapWindow(dpy, win);
24
25 hint = XAllocClassHint();
26 if (!hint) return -1;
27 hint->res_name = "dkee";
28 hint->res_class = "dkee";
29 XSetClassHint(dpy, win, hint);
30
31 xim = XOpenIM(dpy, NULL, NULL, NULL);
32 ic = XCreateIC(xim, XNInputStyle,
33 XIMPreeditNothing | XIMStatusNothing,
34 XNClientWindow, win, NULL);
35 XSetICFocus(ic);
36
37
38 XSelectInput(dpy, win, ExposureMask | KeyPressMask);
39 XMapWindow(dpy, win);
40
41 for (;;)
42 {
43 char hidden[256];
44 char buf[16];
45 size_t c;
46 int keysym;
47 KeySym ksym;
48 Status status;
49
50 XEvent ev;
51 XNextEvent(dpy, &ev);
52
53 if (XFilterEvent(&ev, None))
54 continue;
55 if (ev.type == Expose) goto draw;
56 if (ev.type != KeyPress) continue;
57 keysym = XLookupKeysym(&ev.xkey, 0);
58 if (keysym == XK_Escape) {
59 ret = -1;
60 break;
61 }
62 if (keysym == XK_Return)
63 break;
64 if (keysym == XK_BackSpace) {
65 if (pos < 1) continue;
66 pos--;
67 input[pos] = '\0';
68 goto draw;
69 }
70 c = Xutf8LookupString(ic, &ev.xkey, buf, sizeof(buf) - 1,
71 &ksym, &status);
72
73 if (status == XBufferOverflow) continue;
74 if (c < 1 || pos + c > len) continue;
75
76 memcpy(&input[pos], buf, c);
77 pos += c;
78 input[pos] = '\0';
79 memset(hidden, '*',
80 (pos > sizeof(hidden) - 1)?sizeof(hidden) - 1:pos);
81 draw:
82 XClearWindow(dpy, win);
83 XDrawString(dpy, win, DefaultGC(dpy, scr),
84 10, 20, hidden, pos);
85 }
86 XDestroyWindow(dpy, win);
87 XCloseDisplay(dpy);
88 return ret;
89 }
90