Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
9 #include <X11/keysym.h>
11 #include <X11/Xutil.h>
13 #include <X11/extensions/Xinerama.h>
17 #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
18 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
19 #define MIN(a, b) ((a) < (b) ? (a) : (b))
20 #define MAX(a, b) ((a) > (b) ? (a) : (b))
21 #define IS_UTF8_1ST_CHAR(c) ((((c) & 0xc0) == 0xc0) || !((c) & 0x80))
23 /* forward declarations */
24 static void cleanup(void);
25 static void drawcursor(void);
26 static void drawinput(void);
27 static Bool
grabkeyboard(void);
28 static void kpress(XKeyEvent
*e
);
29 static void run(void);
30 static void setup(Bool topbar
);
36 static char *prompt
= NULL
;
37 static char text
[4096];
38 static int promptw
= 0;
41 static unsigned int cursor
= 0;
42 static unsigned int numlockmask
= 0;
43 static unsigned int mw
, mh
;
44 static unsigned long normcol
[ColLast
];
45 static unsigned long selcol
[ColLast
];
46 static Bool running
= True
;
49 static Window win
, parent
;
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(&dc
, text
, cursor
) + dc
.font
.height
/ 2;
64 XSetForeground(dpy
, dc
.gc
, normcol
[ColFG
]);
65 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
75 drawtext(&dc
, NULL
, normcol
, False
);
79 drawtext(&dc
, prompt
, selcol
, False
);
83 drawtext(&dc
, *text
? text
: NULL
, normcol
, False
);
85 XCopyArea(dpy
, dc
.drawable
, win
, dc
.gc
, 0, 0, mw
, mh
, 0, 0);
93 for(len
= 1000; len
; len
--) {
94 if(XGrabKeyboard(dpy
, parent
, True
, GrabModeAsync
, GrabModeAsync
, CurrentTime
)
103 kpress(XKeyEvent
*e
) {
104 char buf
[sizeof text
];
110 num
= XLookupString(e
, buf
, sizeof buf
, &ksym
, NULL
);
111 if(ksym
== XK_KP_Enter
)
113 else if(ksym
>= XK_KP_0
&& ksym
<= XK_KP_9
)
114 ksym
= (ksym
- XK_KP_0
) + XK_0
;
115 else if(IsFunctionKey(ksym
) || IsKeypadKey(ksym
)
116 || IsMiscFunctionKey(ksym
) || IsPFKey(ksym
)
117 || IsPrivateKeypadKey(ksym
))
119 /* first check if a control mask is omitted */
120 if(e
->state
& ControlMask
) {
121 switch(tolower(ksym
)) {
150 memmove(text
, text
+ cursor
, sizeof text
- cursor
+ 1);
156 while(i
-- > 0 && text
[i
] == ' ');
157 while(i
-- > 0 && text
[i
] != ' ');
158 memmove(text
+ i
+ 1, text
+ cursor
, sizeof text
- cursor
+ 1);
166 if(!(fp
= popen("sselp", "r")))
167 eprint("cannot popen sselp\n");
168 s
= fgets(buf
, sizeof buf
, fp
);
174 if(num
&& buf
[num
-1] == '\n')
181 num
= MIN(num
, sizeof text
- cursor
);
182 if(num
&& !iscntrl((int) buf
[0])) {
183 memmove(text
+ cursor
+ num
, text
+ cursor
, sizeof text
- cursor
- num
);
184 memcpy(text
+ cursor
, buf
, num
);
191 for(i
= 1; cursor
- i
> 0 && !IS_UTF8_1ST_CHAR(text
[cursor
- i
]); i
++);
192 memmove(text
+ cursor
- i
, text
+ cursor
, sizeof text
- cursor
+ i
);
198 for(i
= 1; cursor
+ i
< len
&& !IS_UTF8_1ST_CHAR(text
[cursor
+ i
]); i
++);
199 memmove(text
+ cursor
, text
+ cursor
+ i
, sizeof text
- cursor
);
214 while(cursor
-- > 0 && !IS_UTF8_1ST_CHAR(text
[cursor
]));
217 fprintf(stdout
, "%s", text
);
224 while(cursor
++ < len
&& !IS_UTF8_1ST_CHAR(text
[cursor
]));
234 /* main event loop */
235 while(running
&& !XNextEvent(dpy
, &ev
))
241 if(ev
.xexpose
.count
== 0)
244 case VisibilityNotify
:
245 if (ev
.xvisibility
.state
!= VisibilityUnobscured
)
246 XRaiseWindow(dpy
, win
);
256 XineramaScreenInfo
*info
= NULL
;
258 XModifierKeymap
*modmap
;
259 XSetWindowAttributes wa
;
260 XWindowAttributes pwa
;
262 /* init modifier map */
263 modmap
= XGetModifierMapping(dpy
);
264 for(i
= 0; i
< 8; i
++)
265 for(j
= 0; j
< modmap
->max_keypermod
; j
++) {
266 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
267 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
268 numlockmask
= (1 << i
);
270 XFreeModifiermap(modmap
);
273 normcol
[ColBG
] = getcolor(&dc
, normbgcolor
);
274 normcol
[ColFG
] = getcolor(&dc
, normfgcolor
);
275 selcol
[ColBG
] = getcolor(&dc
, selbgcolor
);
276 selcol
[ColFG
] = getcolor(&dc
, selfgcolor
);
280 wa
.override_redirect
= True
;
281 wa
.background_pixmap
= ParentRelative
;
282 wa
.event_mask
= ExposureMask
| KeyPressMask
| VisibilityChangeMask
;
284 /* input window geometry */
285 mh
= dc
.font
.height
+ 2;
287 if(parent
== RootWindow(dpy
, screen
) && XineramaIsActive(dpy
) && (info
= XineramaQueryScreens(dpy
, &n
))) {
293 if(XQueryPointer(dpy
, parent
, &dummy
, &dummy
, &x
, &y
, &di
, &di
, &dui
))
294 for(i
= 0; i
< n
; i
++)
295 if(INRECT(x
, y
, info
[i
].x_org
, info
[i
].y_org
, info
[i
].width
, info
[i
].height
))
299 y
= topbar
? info
[i
].y_org
: info
[i
].y_org
+ info
[i
].height
- mh
;
306 XGetWindowAttributes(dpy
, parent
, &pwa
);
308 y
= topbar
? 0 : pwa
.height
- mh
;
312 win
= XCreateWindow(dpy
, parent
, x
, y
, mw
, mh
, 0,
313 DefaultDepth(dpy
, screen
), CopyFromParent
,
314 DefaultVisual(dpy
, screen
),
315 CWOverrideRedirect
| CWBackPixmap
| CWEventMask
, &wa
);
319 promptw
= MIN(textw(&dc
, prompt
), mw
/ 5);
320 cursor
= strlen(text
);
321 XMapRaised(dpy
, win
);
325 main(int argc
, char *argv
[]) {
329 /* command line args */
331 for(i
= 1; i
< argc
; i
++)
332 if(!strcmp(argv
[i
], "-b"))
334 else if(!strcmp(argv
[i
], "-e")) {
335 if(++i
< argc
) parent
= atoi(argv
[i
]);
337 else if(!strcmp(argv
[i
], "-fn")) {
338 if(++i
< argc
) font
= argv
[i
];
340 else if(!strcmp(argv
[i
], "-nb")) {
341 if(++i
< argc
) normbgcolor
= argv
[i
];
343 else if(!strcmp(argv
[i
], "-nf")) {
344 if(++i
< argc
) normfgcolor
= argv
[i
];
346 else if(!strcmp(argv
[i
], "-p")) {
347 if(++i
< argc
) prompt
= argv
[i
];
349 else if(!strcmp(argv
[i
], "-sb")) {
350 if(++i
< argc
) selbgcolor
= argv
[i
];
352 else if(!strcmp(argv
[i
], "-sf")) {
353 if(++i
< argc
) selfgcolor
= argv
[i
];
355 else if(!strcmp(argv
[i
], "-v"))
356 eprint("dinput-"VERSION
", © 2006-2010 dinput engineers, see LICENSE for details\n");
358 strncpy(text
, argv
[i
], sizeof text
);
360 eprint("usage: dinput [-b] [-e <xid>] [-fn <font>] [-nb <color>] [-nf <color>]\n"
361 " [-p <prompt>] [-sb <color>] [-sf <color>] [-v] [<text>]\n");
362 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
363 fprintf(stderr
, "dinput: warning: no locale support\n");
364 if(!(dpy
= XOpenDisplay(NULL
)))
365 eprint("cannot open display\n");
366 screen
= DefaultScreen(dpy
);
368 parent
= RootWindow(dpy
, screen
);
370 running
= grabkeyboard();