Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
8 #include <X11/keysym.h>
10 #include <X11/Xutil.h>
12 #include <X11/extensions/Xinerama.h>
17 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
18 #define MIN(a, b) ((a) < (b) ? (a) : (b))
19 #define MAX(a, b) ((a) > (b) ? (a) : (b))
20 #define IS_UTF8_1ST_CHAR(c) ((((c) & 0xc0) == 0xc0) || !((c) & 0x80))
22 /* forward declarations */
23 static void cleanup(void);
24 static void drawcursor(void);
25 static void drawinput(void);
26 static Bool
grabkeyboard(void);
27 static void kpress(XKeyEvent
*e
);
28 static void run(void);
29 static void setup(Bool topbar
);
34 static char *prompt
= NULL
;
35 static char text
[4096];
36 static int promptw
= 0;
39 static unsigned int cursor
= 0;
40 static unsigned int numlockmask
= 0;
41 static unsigned int mw
, mh
;
42 static unsigned long normcol
[ColLast
];
43 static unsigned long selcol
[ColLast
];
44 static Bool running
= True
;
47 static Window win
, root
;
52 XDestroyWindow(dpy
, win
);
53 XUngrabKeyboard(dpy
, CurrentTime
);
58 XRectangle r
= { dc
.x
, dc
.y
+ 2, 1, dc
.font
.height
- 2 };
60 r
.x
+= textnw(&dc
, text
, cursor
) + dc
.font
.height
/ 2;
62 XSetForeground(dpy
, dc
.gc
, normcol
[ColFG
]);
63 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
73 drawtext(&dc
, NULL
, normcol
, False
);
77 drawtext(&dc
, prompt
, selcol
, False
);
81 drawtext(&dc
, *text
? text
: NULL
, normcol
, False
);
83 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
91 for(len
= 1000; len
; len
--) {
92 if(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
101 kpress(XKeyEvent
*e
) {
102 char buf
[sizeof text
];
108 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
109 if(ksym
== XK_KP_Enter
)
111 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
112 ksym
= (ksym
- XK_KP_0
) + XK_0
;
113 else if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
114 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
115 || IsPrivateKeypadKey(ksym
))
117 /* first check if a control mask is omitted */
118 if(e
->state
& ControlMask
) {
119 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
;
259 /* init modifier map */
260 modmap
= XGetModifierMapping(dpy
);
261 for(i
= 0; i
< 8; i
++)
262 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
263 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
264 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
265 numlockmask
= (1 << i
);
267 XFreeModifiermap(modmap
);
270 normcol
[ColBG
] = getcolor(&dc
, normbgcolor
);
271 normcol
[ColFG
] = getcolor(&dc
, normfgcolor
);
272 selcol
[ColBG
] = getcolor(&dc
, selbgcolor
);
273 selcol
[ColFG
] = getcolor(&dc
, selfgcolor
);
277 wa
.override_redirect
= True
;
278 wa
.background_pixmap
= ParentRelative
;
279 wa
.event_mask
= ExposureMask
| KeyPressMask
| VisibilityChangeMask
;
281 /* input window geometry */
282 mh
= dc
.font
.height
+ 2;
284 if(XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
290 if(XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
291 for(i
= 0; i
< n
; i
++)
292 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
296 y
= topbar
? info
[i
].y_org
: info
[i
].y_org
+ info
[i
].height
- mh
;
304 y
= topbar
? 0 : DisplayHeight(dpy
, screen
) - mh
;
305 mw
= DisplayWidth(dpy
, screen
);
308 win
= XCreateWindow(dpy
, root
, x
, y
, mw
, mh
, 0,
309 DefaultDepth(dpy
, screen
), CopyFromParent
,
310 DefaultVisual(dpy
, screen
),
311 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
315 promptw
= MIN(textw(&dc
, prompt
), mw
/ 5);
316 cursor
= strlen(text
);
317 XMapRaised(dpy
, win
);
321 main(int argc
, char *argv
[]) {
325 /* command line args */
327 for(i
= 1; i
< argc
; i
++)
328 if(!strcmp(argv
[i
], "-i"))
330 else if(!strcmp(argv
[i
], "-b"))
332 else if(!strcmp(argv
[i
], "-l"))
333 i
++; /* ignore flag */
334 else if(!strcmp(argv
[i
], "-fn")) {
335 if(++i
< argc
) font
= argv
[i
];
337 else if(!strcmp(argv
[i
], "-nb")) {
338 if(++i
< argc
) normbgcolor
= argv
[i
];
340 else if(!strcmp(argv
[i
], "-nf")) {
341 if(++i
< argc
) normfgcolor
= argv
[i
];
343 else if(!strcmp(argv
[i
], "-p")) {
344 if(++i
< argc
) prompt
= argv
[i
];
346 else if(!strcmp(argv
[i
], "-sb")) {
347 if(++i
< argc
) selbgcolor
= argv
[i
];
349 else if(!strcmp(argv
[i
], "-sf")) {
350 if(++i
< argc
) selfgcolor
= argv
[i
];
352 else if(!strcmp(argv
[i
], "-v")) {
353 printf("dinput-"VERSION
", © 2006-2010 dinput engineers, see LICENSE for details\n");
357 strncpy(text
, argv
[i
], sizeof text
);
359 fputs("usage: dinput [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
360 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v] [<text>]\n", stderr
);
363 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
364 fprintf(stderr
, "dinput: warning: no locale support\n");
365 if(!(dpy
= XOpenDisplay(NULL
)))
366 eprint("cannot open display\n");
367 screen
= DefaultScreen(dpy
);
368 root
= RootWindow(dpy
, screen
);
370 running
= grabkeyboard();