Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
10 #include <X11/keysym.h>
12 #include <X11/Xutil.h>
14 #include <X11/extensions/Xinerama.h>
18 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
19 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
20 #define MIN(a, b) ((a) < (b) ? (a) : (b))
21 #define MAX(a, b) ((a) > (b) ? (a) : (b))
22 #define IS_UTF8_1ST_CHAR(c) ((((c) & 0xc0) == 0xc0) || !((c) & 0x80))
24 /* forward declarations */
25 static void cleanup(void);
26 static void drawcursor(void);
27 static void drawinput(void);
28 static void eprint(const char *errstr
, ...);
29 static Bool
grabkeyboard(void);
30 static void kpress(XKeyEvent
* e
);
31 static void run(void);
32 static void setup(Bool topbar
);
37 static char *prompt
= NULL
;
38 static char text
[4096];
39 static int promptw
= 0;
42 static unsigned int mw
, mh
;
43 static unsigned int cursor
= 0;
44 static unsigned int numlockmask
= 0;
45 static Bool running
= True
;
47 static Window parent
, win
;
54 XDestroyWindow(dpy
, win
);
55 XUngrabKeyboard(dpy
, CurrentTime
);
60 XRectangle r
= { dc
.x
, dc
.y
+ 2, 1, dc
.font
.height
- 2 };
62 r
.x
+= textnw(text
, cursor
) + dc
.font
.height
/ 2;
64 XSetForeground(dpy
, dc
.gc
, dc
.norm
[ColFG
]);
65 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
75 drawtext(NULL
, dc
.norm
);
79 drawtext(prompt
, dc
.sel
);
83 drawtext(*text
? text
: NULL
, dc
.norm
);
85 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
90 eprint(const char *errstr
, ...) {
94 vfprintf(stderr
, errstr
, ap
);
103 for(len
= 1000; len
; len
--) {
104 if(XGrabKeyboard(dpy
, parent
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
113 kpress(XKeyEvent
* e
) {
114 char buf
[sizeof text
];
120 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
121 if(ksym
== XK_KP_Enter
)
123 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
124 ksym
= (ksym
- XK_KP_0
) + XK_0
;
125 else if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
126 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
127 || IsPrivateKeypadKey(ksym
))
129 /* first check if a control mask is omitted */
130 if(e
->state
& ControlMask
) {
131 switch(tolower(ksym
)) {
159 memmove(text
, text
+ cursor
, sizeof text
- cursor
+ 1);
165 while(i
-- > 0 && text
[i
] == ' ');
166 while(i
-- > 0 && text
[i
] != ' ');
167 memmove(text
+ i
+ 1, text
+ cursor
, sizeof text
- cursor
+ 1);
175 if(!(fp
= popen("sselp", "r")))
176 eprint("dinput: cannot popen sselp\n");
177 s
= fgets(buf
, sizeof buf
, fp
);
183 if(num
&& buf
[num
-1] == '\n')
190 num
= MIN(num
, sizeof text
- cursor
);
191 if(num
&& !iscntrl((int) buf
[0])) {
192 memmove(text
+ cursor
+ num
, text
+ cursor
, sizeof text
- cursor
- num
);
193 memcpy(text
+ cursor
, buf
, num
);
200 for(i
= 1; cursor
- i
> 0 && !IS_UTF8_1ST_CHAR(text
[cursor
- i
]); i
++);
201 memmove(text
+ cursor
- i
, text
+ cursor
, sizeof text
- cursor
+ i
);
207 for(i
= 1; cursor
+ i
< len
&& !IS_UTF8_1ST_CHAR(text
[cursor
+ i
]); i
++);
208 memmove(text
+ cursor
, text
+ cursor
+ i
, sizeof text
- cursor
);
223 while(cursor
-- > 0 && !IS_UTF8_1ST_CHAR(text
[cursor
]));
226 fprintf(stdout
, "%s", text
);
233 while(cursor
++ < len
&& !IS_UTF8_1ST_CHAR(text
[cursor
]));
243 /* main event loop */
244 while(running
&& !XNextEvent(dpy
, &ev
))
250 if(ev
.xexpose
.count
== 0)
253 case VisibilityNotify
:
254 if (ev
.xvisibility
.state
!= VisibilityUnobscured
)
255 XRaiseWindow(dpy
, win
);
265 XineramaScreenInfo
*info
= NULL
;
267 XModifierKeymap
*modmap
;
268 XSetWindowAttributes wa
;
269 XWindowAttributes pwa
;
271 /* init modifier map */
272 modmap
= XGetModifierMapping(dpy
);
273 for(i
= 0; i
< 8; i
++)
274 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
275 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
276 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
277 numlockmask
= (1 << i
);
279 XFreeModifiermap(modmap
);
284 wa
.override_redirect
= True
;
285 wa
.background_pixmap
= ParentRelative
;
286 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
| VisibilityChangeMask
;
288 /* menu window geometry */
289 mh
= (dc
.font
.height
+ 2);
291 if(parent
== RootWindow(dpy
, screen
) && XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
297 if(XQueryPointer(dpy
, parent
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
298 for(i
= 0; i
< n
; i
++)
299 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
303 y
= topbar
? info
[i
].y_org
: info
[i
].y_org
+ info
[i
].height
- mh
;
310 XGetWindowAttributes(dpy
, parent
, &pwa
);
312 y
= topbar
? 0 : pwa
.height
- mh
;
316 win
= XCreateWindow(dpy
, parent
, x
, y
, mw
, mh
, 0,
317 DefaultDepth(dpy
, screen
), CopyFromParent
,
318 DefaultVisual(dpy
, screen
),
319 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
323 promptw
= MIN(textw(prompt
), mw
/ 5);
324 cursor
= strlen(text
);
325 XMapRaised(dpy
, win
);
329 main(int argc
, char *argv
[]) {
333 /* command line args */
334 for(i
= 1; i
< argc
; i
++)
335 if(!strcmp(argv
[i
], "-b"))
337 else if(!strcmp(argv
[i
], "-e")) {
338 if(++i
< argc
) parent
= atoi(argv
[i
]);
340 else if(!strcmp(argv
[i
], "-fn")) {
341 if(++i
< argc
) font
= argv
[i
];
343 else if(!strcmp(argv
[i
], "-nb")) {
344 if(++i
< argc
) normbgcolor
= argv
[i
];
346 else if(!strcmp(argv
[i
], "-nf")) {
347 if(++i
< argc
) normfgcolor
= argv
[i
];
349 else if(!strcmp(argv
[i
], "-p")) {
350 if(++i
< argc
) prompt
= argv
[i
];
352 else if(!strcmp(argv
[i
], "-sb")) {
353 if(++i
< argc
) selbgcolor
= argv
[i
];
355 else if(!strcmp(argv
[i
], "-sf")) {
356 if(++i
< argc
) selfgcolor
= argv
[i
];
358 else if(!strcmp(argv
[i
], "-v"))
359 eprint("dinput-"VERSION
", © 2006-2010 dinput engineers, see LICENSE for details\n");
361 strncpy(text
, argv
[i
], sizeof text
);
363 eprint("usage: dinput [-b] [-e <xid>] [-fn <font>] [-nb <color>] [-nf <color>]\n"
364 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v] [<text>]\n");
365 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
366 fprintf(stderr
, "dinput: warning: no locale support\n");
367 if(!(dpy
= XOpenDisplay(NULL
)))
368 eprint("dinput: cannot open display\n");
369 screen
= DefaultScreen(dpy
);
371 parent
= RootWindow(dpy
, screen
);
373 running
= grabkeyboard();