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 drawinput(void);
25 static void grabkeyboard(void);
26 static void kpress(XKeyEvent
*e
);
27 static void run(void);
28 static void setup(void);
33 static char *prompt
= NULL
;
34 static char text
[4096];
35 static int promptw
= 0;
37 static size_t cursor
= 0;
38 static unsigned int numlockmask
= 0;
39 static unsigned int mw
, mh
;
40 static unsigned long normcol
[ColLast
];
41 static unsigned long selcol
[ColLast
];
42 static Bool topbar
= True
;
45 static Window win
, root
;
50 XDestroyWindow(dpy
, win
);
51 XUngrabKeyboard(dpy
, CurrentTime
);
62 drawtext(&dc
, NULL
, normcol
);
66 drawtext(&dc
, prompt
, selcol
);
70 drawtext(&dc
, text
, normcol
);
71 drawcursor(&dc
, text
, cursor
, normcol
);
79 for(len
= 1000; len
; len
--) {
80 if(XGrabKeyboard(dpy
, root
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
89 kpress(XKeyEvent
*e
) {
90 char buf
[sizeof text
];
96 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
97 if(ksym
== XK_KP_Enter
)
99 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
100 ksym
= (ksym
- XK_KP_0
) + XK_0
;
101 else if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
102 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
103 || IsPrivateKeypadKey(ksym
))
105 /* first check if a control mask is omitted */
106 if(e
->state
& ControlMask
) {
107 switch(tolower(ksym
)) {
136 memmove(text
, text
+ cursor
, sizeof text
- cursor
+ 1);
142 while(i
-- > 0 && text
[i
] == ' ');
143 while(i
-- > 0 && text
[i
] != ' ');
144 memmove(text
+ i
+ 1, text
+ cursor
, sizeof text
- cursor
+ 1);
152 if(!(fp
= popen("sselp", "r")))
153 eprint("cannot popen sselp\n");
154 s
= fgets(buf
, sizeof buf
, fp
);
160 if(num
&& buf
[num
-1] == '\n')
167 num
= MIN(num
, sizeof text
- cursor
);
168 if(num
&& !iscntrl((int) buf
[0])) {
169 memmove(text
+ cursor
+ num
, text
+ cursor
, sizeof text
- cursor
- num
);
170 memcpy(text
+ cursor
, buf
, num
);
177 for(i
= 1; cursor
- i
> 0 && !IS_UTF8_1ST_CHAR(text
[cursor
- i
]); i
++);
178 memmove(text
+ cursor
- i
, text
+ cursor
, sizeof text
- cursor
+ i
);
184 for(i
= 1; cursor
+ i
< len
&& !IS_UTF8_1ST_CHAR(text
[cursor
+ i
]); i
++);
185 memmove(text
+ cursor
, text
+ cursor
+ i
, sizeof text
- cursor
);
198 while(cursor
-- > 0 && !IS_UTF8_1ST_CHAR(text
[cursor
]));
201 fprintf(stdout
, "%s", text
);
207 while(cursor
++ < len
&& !IS_UTF8_1ST_CHAR(text
[cursor
]));
217 /* main event loop */
219 while(!XNextEvent(dpy
, &ev
))
225 if(ev
.xexpose
.count
== 0)
228 case VisibilityNotify
:
229 if(ev
.xvisibility
.state
!= VisibilityUnobscured
)
230 XRaiseWindow(dpy
, win
);
241 XineramaScreenInfo
*info
= NULL
;
243 XModifierKeymap
*modmap
;
244 XSetWindowAttributes wa
;
246 /* init modifier map */
247 modmap
= XGetModifierMapping(dpy
);
248 for(i
= 0; i
< 8; i
++)
249 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
250 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
251 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
252 numlockmask
= (1 << i
);
254 XFreeModifiermap(modmap
);
257 normcol
[ColBG
] = getcolor(&dc
, normbgcolor
);
258 normcol
[ColFG
] = getcolor(&dc
, normfgcolor
);
259 selcol
[ColBG
] = getcolor(&dc
, selbgcolor
);
260 selcol
[ColFG
] = getcolor(&dc
, selfgcolor
);
264 wa
.override_redirect
= True
;
265 wa
.background_pixmap
= ParentRelative
;
266 wa
.event_mask
= ExposureMask
| KeyPressMask
| VisibilityChangeMask
;
268 /* input window geometry */
269 mh
= dc
.font
.height
+ 2;
271 if(XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
277 if(XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
278 for(i
= 0; i
< n
; i
++)
279 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
283 y
= topbar
? info
[i
].y_org
: info
[i
].y_org
+ info
[i
].height
- mh
;
291 y
= topbar
? 0 : DisplayHeight(dpy
, screen
) - mh
;
292 mw
= DisplayWidth(dpy
, screen
);
295 win
= XCreateWindow(dpy
, root
, x
, y
, mw
, mh
, 0,
296 DefaultDepth(dpy
, screen
), CopyFromParent
,
297 DefaultVisual(dpy
, screen
),
298 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
302 promptw
= MIN(textw(&dc
, prompt
), mw
/ 5);
303 cursor
= strlen(text
);
304 XMapRaised(dpy
, win
);
308 main(int argc
, char *argv
[]) {
311 /* command line args */
313 for(i
= 1; i
< argc
; i
++)
314 if(!strcmp(argv
[i
], "-i"))
316 else if(!strcmp(argv
[i
], "-b"))
318 else if(!strcmp(argv
[i
], "-l"))
319 i
++; /* ignore flag */
320 else if(!strcmp(argv
[i
], "-fn")) {
321 if(++i
< argc
) font
= argv
[i
];
323 else if(!strcmp(argv
[i
], "-nb")) {
324 if(++i
< argc
) normbgcolor
= argv
[i
];
326 else if(!strcmp(argv
[i
], "-nf")) {
327 if(++i
< argc
) normfgcolor
= argv
[i
];
329 else if(!strcmp(argv
[i
], "-p")) {
330 if(++i
< argc
) prompt
= argv
[i
];
332 else if(!strcmp(argv
[i
], "-sb")) {
333 if(++i
< argc
) selbgcolor
= argv
[i
];
335 else if(!strcmp(argv
[i
], "-sf")) {
336 if(++i
< argc
) selfgcolor
= argv
[i
];
338 else if(!strcmp(argv
[i
], "-v")) {
339 printf("dinput-"VERSION
", © 2006-2010 dinput engineers, see LICENSE for details\n");
343 strncpy(text
, argv
[i
], sizeof text
);
345 fputs("usage: dinput [-b] [-fn <font>] [-nb <color>] [-nf <color>]\n"
346 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v] [<text>]\n", stderr
);
349 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
350 fprintf(stderr
, "dinput: warning: no locale support\n");
351 if(!(dpy
= XOpenDisplay(NULL
)))
352 eprint("cannot open display\n");
353 if(atexit(&cleanup
) != 0)
354 eprint("cannot register cleanup\n");
355 screen
= DefaultScreen(dpy
);
356 root
= RootWindow(dpy
, screen
);