Xinqi Bao's Git
39889b31b393f7e7bedf2d52b129bd51e295430b
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;
42 static unsigned int cursor
= 0;
43 static unsigned int numlockmask
= 0;
44 static unsigned int mw
, mh
;
45 static Bool running
= True
;
48 static Window win
, parent
;
53 XDestroyWindow(dpy
, win
);
54 XUngrabKeyboard(dpy
, CurrentTime
);
59 XRectangle r
= { dc
.x
, dc
.y
+ 2, 1, dc
.font
.height
- 2 };
61 r
.x
+= textnw(&dc
, text
, cursor
) + dc
.font
.height
/ 2;
63 XSetForeground(dpy
, dc
.gc
, dc
.norm
[ColFG
]);
64 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
74 drawtext(&dc
, NULL
, dc
.norm
);
78 drawtext(&dc
, prompt
, dc
.sel
);
82 drawtext(&dc
, *text
? text
: NULL
, dc
.norm
);
84 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
92 for(len
= 1000; len
; len
--) {
93 if(XGrabKeyboard(dpy
, parent
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
102 kpress(XKeyEvent
* e
) {
103 char buf
[sizeof text
];
109 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
110 if(ksym
== XK_KP_Enter
)
112 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
113 ksym
= (ksym
- XK_KP_0
) + XK_0
;
114 else if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
115 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
116 || IsPrivateKeypadKey(ksym
))
118 /* first check if a control mask is omitted */
119 if(e
->state
& ControlMask
) {
120 switch(tolower(ksym
)) {
148 memmove(text
, text
+ cursor
, sizeof text
- cursor
+ 1);
154 while(i
-- > 0 && text
[i
] == ' ');
155 while(i
-- > 0 && text
[i
] != ' ');
156 memmove(text
+ i
+ 1, text
+ cursor
, sizeof text
- cursor
+ 1);
164 if(!(fp
= popen("sselp", "r")))
165 eprint("cannot popen sselp\n");
166 s
= fgets(buf
, sizeof buf
, fp
);
172 if(num
&& buf
[num
-1] == '\n')
179 num
= MIN(num
, sizeof text
- cursor
);
180 if(num
&& !iscntrl((int) buf
[0])) {
181 memmove(text
+ cursor
+ num
, text
+ cursor
, sizeof text
- cursor
- num
);
182 memcpy(text
+ cursor
, buf
, num
);
189 for(i
= 1; cursor
- i
> 0 && !IS_UTF8_1ST_CHAR(text
[cursor
- i
]); i
++);
190 memmove(text
+ cursor
- i
, text
+ cursor
, sizeof text
- cursor
+ i
);
196 for(i
= 1; cursor
+ i
< len
&& !IS_UTF8_1ST_CHAR(text
[cursor
+ i
]); i
++);
197 memmove(text
+ cursor
, text
+ cursor
+ i
, sizeof text
- cursor
);
212 while(cursor
-- > 0 && !IS_UTF8_1ST_CHAR(text
[cursor
]));
215 fprintf(stdout
, "%s", text
);
222 while(cursor
++ < len
&& !IS_UTF8_1ST_CHAR(text
[cursor
]));
232 /* main event loop */
233 while(running
&& !XNextEvent(dpy
, &ev
))
239 if(ev
.xexpose
.count
== 0)
242 case VisibilityNotify
:
243 if (ev
.xvisibility
.state
!= VisibilityUnobscured
)
244 XRaiseWindow(dpy
, win
);
254 XineramaScreenInfo
*info
= NULL
;
256 XModifierKeymap
*modmap
;
257 XSetWindowAttributes wa
;
258 XWindowAttributes pwa
;
260 /* init modifier map */
261 modmap
= XGetModifierMapping(dpy
);
262 for(i
= 0; i
< 8; i
++)
263 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
264 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
265 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
266 numlockmask
= (1 << i
);
268 XFreeModifiermap(modmap
);
271 dc
.norm
[ColBG
] = getcolor(&dc
, normbgcolor
);
272 dc
.norm
[ColFG
] = getcolor(&dc
, normfgcolor
);
273 dc
.sel
[ColBG
] = getcolor(&dc
, selbgcolor
);
274 dc
.sel
[ColFG
] = getcolor(&dc
, selfgcolor
);
276 fprintf(stderr
, "dc.font.xfont: %u\n", (size_t)dc
.font
.xfont
);
279 wa
.override_redirect
= True
;
280 wa
.background_pixmap
= ParentRelative
;
281 wa
.event_mask
= ExposureMask
| ButtonPressMask
| KeyPressMask
| VisibilityChangeMask
;
283 /* input window geometry */
284 mh
= dc
.font
.height
+ 2;
286 if(parent
== RootWindow(dpy
, screen
) && XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
292 if(XQueryPointer(dpy
, parent
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
293 for(i
= 0; i
< n
; i
++)
294 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
298 y
= topbar
? info
[i
].y_org
: info
[i
].y_org
+ info
[i
].height
- mh
;
305 XGetWindowAttributes(dpy
, parent
, &pwa
);
307 y
= topbar
? 0 : pwa
.height
- mh
;
311 win
= XCreateWindow(dpy
, parent
, x
, y
, mw
, mh
, 0,
312 DefaultDepth(dpy
, screen
), CopyFromParent
,
313 DefaultVisual(dpy
, screen
),
314 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
318 promptw
= MIN(textw(&dc
, prompt
), mw
/ 5);
319 cursor
= strlen(text
);
320 XMapRaised(dpy
, win
);
324 main(int argc
, char *argv
[]) {
328 /* command line args */
330 for(i
= 1; i
< argc
; i
++)
331 if(!strcmp(argv
[i
], "-b"))
333 else if(!strcmp(argv
[i
], "-e")) {
334 if(++i
< argc
) parent
= atoi(argv
[i
]);
336 else if(!strcmp(argv
[i
], "-fn")) {
337 if(++i
< argc
) font
= argv
[i
];
339 else if(!strcmp(argv
[i
], "-nb")) {
340 if(++i
< argc
) normbgcolor
= argv
[i
];
342 else if(!strcmp(argv
[i
], "-nf")) {
343 if(++i
< argc
) normfgcolor
= argv
[i
];
345 else if(!strcmp(argv
[i
], "-p")) {
346 if(++i
< argc
) prompt
= argv
[i
];
348 else if(!strcmp(argv
[i
], "-sb")) {
349 if(++i
< argc
) selbgcolor
= argv
[i
];
351 else if(!strcmp(argv
[i
], "-sf")) {
352 if(++i
< argc
) selfgcolor
= argv
[i
];
354 else if(!strcmp(argv
[i
], "-v"))
355 eprint("dinput-"VERSION
", © 2006-2010 dinput engineers, see LICENSE for details\n");
357 strncpy(text
, argv
[i
], sizeof text
);
359 eprint("usage: dinput [-b] [-e <xid>] [-fn <font>] [-nb <color>] [-nf <color>]\n"
360 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v] [<text>]\n");
361 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
362 fprintf(stderr
, "dinput: warning: no locale support\n");
363 if(!(dpy
= XOpenDisplay(NULL
)))
364 eprint("cannot open display\n");
365 screen
= DefaultScreen(dpy
);
367 parent
= RootWindow(dpy
, screen
);
369 running
= grabkeyboard();