commit 20d30cc
chld
·
2026-07-13 15:32:22 +0000 UTC
parent 20d30cc
init
1 files changed,
+145,
-0
A
xl.c
A
xl.c
+145,
-0
1@@ -0,0 +1,145 @@
2+#include<X11/Xlib.h>
3+#include<X11/keysym.h>
4+
5+#include<stdbool.h>
6+#include<stdio.h>
7+#include<stdlib.h>
8+#include<string.h>
9+
10+/*
11+ * a remake of the late `lnd`.
12+ * since X is less secure,
13+ * i can actually find the mouse position
14+ * and make it really dropdown.
15+ */
16+
17+int main(int ac, char **av)
18+{
19+ int wi=80, hi=150, x=0, y=0;
20+
21+ Display *d=XOpenDisplay(NULL);
22+ if(!d)
23+ {
24+ fprintf(stderr, "couldnt open display\n");
25+ return 1;
26+ }
27+
28+ int s=DefaultScreen(d);
29+ Window r=DefaultRootWindow(d);
30+ Window w_r;
31+ unsigned int m_r;
32+ int d1, d2;
33+
34+ if(XQueryPointer(d,r,
35+ &w_r,&w_r,&x,&y,
36+ &d1,&d2,
37+ &m_r)){}
38+
39+ Window w=XCreateSimpleWindow(d, RootWindow(d, s),
40+ x, y, wi, hi, 0, BlackPixel(d,s),
41+ WhitePixel(d, s));
42+ XStoreName(d, w, "xmenu");
43+ XSelectInput(d, w, ExposureMask|KeyPressMask|StructureNotifyMask|
44+ ButtonPressMask|ButtonReleaseMask|
45+ PointerMotionMask|LeaveWindowMask);
46+ XMapWindow(d, w);
47+ GC gc=XCreateGC(d, w, 0, NULL);
48+ XSetForeground(d, gc, WhitePixel(d,s));
49+ XFillRectangle(d, w,gc,0,0,wi,hi);
50+ XSetForeground(d, gc, BlackPixel(d,s));
51+
52+ char txt[255]; int txt_len=0;
53+ strcpy(txt, av[1]);
54+ txt_len=strlen(txt);
55+
56+ XFontStruct *font = XLoadQueryFont(d, "fixed");
57+ XSetFont(d, gc, font->fid);
58+
59+ int direction, as, des;
60+ XCharStruct ov;
61+
62+ XTextExtents(font, txt, strlen(txt),
63+ &direction,
64+ &as,
65+ &des,
66+ &ov);
67+ int32_t sel=0;
68+
69+ while(1)
70+ {
71+ XEvent ev;
72+ XNextEvent(d, &ev);
73+
74+ switch(ev.type)
75+ {
76+ case Expose:
77+ if (ev.xexpose.count != 0)
78+ break;
79+
80+ for(int j=0; j<5; ++j)
81+ {
82+ if(sel==j){
83+ XSetForeground(d, gc, WhitePixel(d,s));
84+ }else{
85+ XSetForeground(d, gc, BlackPixel(d,s));
86+ }
87+
88+ XFillRectangle(d,w,gc,0,(hi*j)/5,wi,hi/5);
89+ }
90+ XFlush(d);
91+ /* text drawing */
92+ XSetForeground(d, gc, WhitePixel(d,s));
93+ for(int i=0; i<5; ++i)
94+ {
95+ if(sel==i){
96+ XSetForeground(d, gc, BlackPixel(d,s));
97+ }else{
98+ XSetForeground(d, gc, WhitePixel(d,s));
99+ }
100+
101+ XDrawString(d,w,gc,
102+ (wi-ov.width)/2,
103+ (hi*i)/5+(hi/5+(as+des))/2,
104+ txt,txt_len);
105+ }
106+ XSetForeground(d, gc, BlackPixel(d,s));
107+ break;
108+ case ConfigureNotify:
109+ break;
110+ case LeaveNotify:
111+ goto q;
112+ break;
113+ case ButtonPress:
114+ fprintf(stderr, "%s %d\n", txt, sel);
115+ goto q;
116+ break;
117+ case ButtonRelease:
118+ break;
119+ case MotionNotify: {
120+ int ssel=ev.xmotion.y/(hi/5);
121+ if(ssel>=5) ssel=5-1;
122+
123+ if(ssel!=sel)
124+ {
125+ sel=ssel;
126+ XClearArea(d, w, 0, 0, 0, 0, True);
127+ }
128+ break;
129+ }
130+ case KeyPress: {
131+ KeySym k=XLookupKeysym(&ev.xkey, 0);
132+ if (k==XK_Escape) goto q;
133+
134+ break;
135+ }
136+ }
137+ }
138+
139+q:
140+ XFreeGC(d,gc);
141+ XDestroyWindow(d,w);
142+ XCloseDisplay(d);
143+ return 0;
144+
145+}
146+