Xinqi Bao's Git
f2b504aa4e1fdc52ddcead24bb381065d80cd278
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 Bool
grabkeyboard(void);
29 static void kpress(XKeyEvent
* e
);
30 static void run(void);
31 static void setup(Bool topbar
);
37 static char *prompt
= NULL
;
38 static char text
[4096];
39 static int promptw
= 0;
41 static unsigned int cursor
= 0;
42 static unsigned int numlockmask
= 0;
43 static Bool running
= True
;
49 XDestroyWindow(dpy
, win
);
50 XUngrabKeyboard(dpy
, CurrentTime
);
55 XRectangle r
= { dc
.x
, dc
.y
+ 2, 1, dc
.font
.height
- 2 };
57 r
.x
+= textnw(text
, cursor
) + dc
.font
.height
/ 2;
59 XSetForeground(dpy
, dc
.gc
, dc
.norm
[ColFG
]);
60 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
70 drawtext(NULL
, dc
.norm
);
74 drawtext(prompt
, dc
.sel
);
78 drawtext(*text
? text
: NULL
, dc
.norm
);
80 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
88 for(len
= 1000; len
; len
--) {
89 if(XGrabKeyboard(dpy
, parent
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
98 kpress(XKeyEvent
* e
) {
99 char buf
[sizeof text
];
105 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
106 if(ksym
== XK_KP_Enter
)
108 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
109 ksym
= (ksym
- XK_KP_0
) + XK_0
;
110 else if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
111 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
112 || IsPrivateKeypadKey(ksym
))
114 /* first check if a control mask is omitted */
115 if(e
->state
& ControlMask
) {
116 switch(tolower(ksym
)) {
144 memmove(text
, text
+ cursor
, sizeof text
- cursor
+ 1);
150 while(i
-- > 0 && text
[i
] == ' ');
151 while(i
-- > 0 && text
[i
] != ' ');
152 memmove(text
+ i
+ 1, text
+ cursor
, sizeof text
- cursor
+ 1);
160 if(!(fp
= popen("sselp", "r")))
161 eprint("dinput: cannot popen sselp\n");
162 s
= fgets(buf
, sizeof buf
, fp
);
168 if(num
&& buf
[num
-1] == '\n')
175 num
= MIN(num
, sizeof text
- cursor
);
176 if(num
&& !iscntrl((int) buf
[0])) {
177 memmove(text
+ cursor
+ num
, text
+ cursor
, sizeof text
- cursor
- num
);
178 memcpy(text
+ cursor
, buf
, num
);
185 for(i
= 1; cursor
- i
> 0 && !IS_UTF8_1ST_CHAR(text
[cursor
- i
]); i
++);
186 memmove(text
+ cursor
- i
, text
+ cursor
, sizeof text
- cursor
+ i
);
192 for(i
= 1; cursor
+ i
< len
&& !IS_UTF8_1ST_CHAR(text
[cursor
+ i
]); i
++);
193 memmove(text
+ cursor
, text
+ cursor
+ i
, sizeof text
- cursor
);
208 while(cursor
-- > 0 && !IS_UTF8_1ST_CHAR(text
[cursor
]));
211 fprintf(stdout
, "%s", text
);
218 while(cursor
++ < len
&& !IS_UTF8_1ST_CHAR(text
[cursor
]));
228 /* main event loop */
229 while(running
&& !XNextEvent(dpy
, &ev
))
235 if(ev
.xexpose
.count
== 0)
238 case VisibilityNotify
:
239 if (ev
.xvisibility
.state
!= VisibilityUnobscured
)
240 XRaiseWindow(dpy
, win
);
250 XineramaScreenInfo
*info
= NULL
;
252 XModifierKeymap
*modmap
;
253 XSetWindowAttributes wa
;
254 XWindowAttributes pwa
;
256 /* init modifier map */
257 modmap
= XGetModifierMapping(dpy
);
258 for(i
= 0; i
< 8; i
++)
259 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
260 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
261 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
262 numlockmask
= (1 << i
);
264 XFreeModifiermap(modmap
);
269 wa
.override_redirect
= True
;
270 wa
.background_pixmap
= ParentRelative
;
271 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
| VisibilityChangeMask
;
273 /* menu window geometry */
274 mh
= (dc
.font
.height
+ 2);
276 if(parent
== RootWindow(dpy
, screen
) && XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
282 if(XQueryPointer(dpy
, parent
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
283 for(i
= 0; i
< n
; i
++)
284 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
288 y
= topbar
? info
[i
].y_org
: info
[i
].y_org
+ info
[i
].height
- mh
;
295 XGetWindowAttributes(dpy
, parent
, &pwa
);
297 y
= topbar
? 0 : pwa
.height
- mh
;
301 win
= XCreateWindow(dpy
, parent
, x
, y
, mw
, mh
, 0,
302 DefaultDepth(dpy
, screen
), CopyFromParent
,
303 DefaultVisual(dpy
, screen
),
304 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
308 promptw
= MIN(textw(prompt
), mw
/ 5);
309 cursor
= strlen(text
);
310 XMapRaised(dpy
, win
);
314 main(int argc
, char *argv
[]) {
318 /* command line args */
319 for(i
= 1; i
< argc
; i
++)
320 if(!strcmp(argv
[i
], "-b"))
322 else if(!strcmp(argv
[i
], "-e")) {
323 if(++i
< argc
) parent
= atoi(argv
[i
]);
325 else if(!strcmp(argv
[i
], "-fn")) {
326 if(++i
< argc
) font
= argv
[i
];
328 else if(!strcmp(argv
[i
], "-nb")) {
329 if(++i
< argc
) normbgcolor
= argv
[i
];
331 else if(!strcmp(argv
[i
], "-nf")) {
332 if(++i
< argc
) normfgcolor
= argv
[i
];
334 else if(!strcmp(argv
[i
], "-p")) {
335 if(++i
< argc
) prompt
= argv
[i
];
337 else if(!strcmp(argv
[i
], "-sb")) {
338 if(++i
< argc
) selbgcolor
= argv
[i
];
340 else if(!strcmp(argv
[i
], "-sf")) {
341 if(++i
< argc
) selfgcolor
= argv
[i
];
343 else if(!strcmp(argv
[i
], "-v"))
344 eprint("dinput-"VERSION
", © 2006-2010 dinput engineers, see LICENSE for details\n");
346 strncpy(text
, argv
[i
], sizeof text
);
348 eprint("usage: dinput [-b] [-e <xid>] [-fn <font>] [-nb <color>] [-nf <color>]\n"
349 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v] [<text>]\n");
350 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
351 fprintf(stderr
, "dinput: warning: no locale support\n");
352 if(!(dpy
= XOpenDisplay(NULL
)))
353 eprint("dinput: cannot open display\n");
354 screen
= DefaultScreen(dpy
);
356 parent
= RootWindow(dpy
, screen
);
358 running
= grabkeyboard();