x.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080
  1. /* See LICENSE for license details. */
  2. #include <errno.h>
  3. #include <math.h>
  4. #include <limits.h>
  5. #include <locale.h>
  6. #include <signal.h>
  7. #include <sys/select.h>
  8. #include <time.h>
  9. #include <unistd.h>
  10. #include <libgen.h>
  11. #include <X11/Xatom.h>
  12. #include <X11/Xlib.h>
  13. #include <X11/cursorfont.h>
  14. #include <X11/keysym.h>
  15. #include <X11/Xft/Xft.h>
  16. #include <X11/XKBlib.h>
  17. #include <X11/Xresource.h>
  18. static char *argv0;
  19. #include "arg.h"
  20. #include "st.h"
  21. #include "win.h"
  22. /* types used in config.h */
  23. typedef struct {
  24. uint mod;
  25. KeySym keysym;
  26. void (*func)(const Arg *);
  27. const Arg arg;
  28. } Shortcut;
  29. typedef struct {
  30. uint b;
  31. uint mask;
  32. char *s;
  33. } MouseShortcut;
  34. typedef struct {
  35. KeySym k;
  36. uint mask;
  37. char *s;
  38. /* three-valued logic variables: 0 indifferent, 1 on, -1 off */
  39. signed char appkey; /* application keypad */
  40. signed char appcursor; /* application cursor */
  41. } Key;
  42. /* Xresources preferences */
  43. enum resource_type {
  44. STRING = 0,
  45. INTEGER = 1,
  46. FLOAT = 2
  47. };
  48. typedef struct {
  49. char *name;
  50. enum resource_type type;
  51. void *dst;
  52. } ResourcePref;
  53. /* X modifiers */
  54. #define XK_ANY_MOD UINT_MAX
  55. #define XK_NO_MOD 0
  56. #define XK_SWITCH_MOD (1<<13)
  57. /* alpha */
  58. #define OPAQUE 0Xff
  59. #define USE_ARGB (alpha != OPAQUE && opt_embed == NULL)
  60. /* function definitions used in config.h */
  61. static void clipcopy(const Arg *);
  62. static void clippaste(const Arg *);
  63. static void numlock(const Arg *);
  64. static void selpaste(const Arg *);
  65. static void zoom(const Arg *);
  66. static void zoomabs(const Arg *);
  67. static void zoomreset(const Arg *);
  68. /* config.h for applying patches and the configuration. */
  69. #include "config.h"
  70. /* XEMBED messages */
  71. #define XEMBED_FOCUS_IN 4
  72. #define XEMBED_FOCUS_OUT 5
  73. /* macros */
  74. #define IS_SET(flag) ((win.mode & (flag)) != 0)
  75. #define TRUERED(x) (((x) & 0xff0000) >> 8)
  76. #define TRUEGREEN(x) (((x) & 0xff00))
  77. #define TRUEBLUE(x) (((x) & 0xff) << 8)
  78. typedef XftDraw *Draw;
  79. typedef XftColor Color;
  80. typedef XftGlyphFontSpec GlyphFontSpec;
  81. /* Purely graphic info */
  82. typedef struct {
  83. int tw, th; /* tty width and height */
  84. int w, h; /* window width and height */
  85. int ch; /* char height */
  86. int cw; /* char width */
  87. int mode; /* window state/mode flags */
  88. int cursor; /* cursor style */
  89. } TermWindow;
  90. typedef struct {
  91. Display *dpy;
  92. Colormap cmap;
  93. Window win;
  94. Drawable buf;
  95. GlyphFontSpec *specbuf; /* font spec buffer used for rendering */
  96. Atom xembed, wmdeletewin, netwmname, netwmpid;
  97. XIM xim;
  98. XIC xic;
  99. Draw draw;
  100. Visual *vis;
  101. XSetWindowAttributes attrs;
  102. int scr;
  103. int isfixed; /* is fixed geometry? */
  104. int depth; /* bit depth */
  105. int l, t; /* left and top offset */
  106. int gm; /* geometry mask */
  107. } XWindow;
  108. typedef struct {
  109. Atom xtarget;
  110. char *primary, *clipboard;
  111. struct timespec tclick1;
  112. struct timespec tclick2;
  113. } XSelection;
  114. /* Font structure */
  115. #define Font Font_
  116. typedef struct {
  117. int height;
  118. int width;
  119. int ascent;
  120. int descent;
  121. int badslant;
  122. int badweight;
  123. short lbearing;
  124. short rbearing;
  125. XftFont *match;
  126. FcFontSet *set;
  127. FcPattern *pattern;
  128. } Font;
  129. /* Drawing Context */
  130. typedef struct {
  131. Color *col;
  132. size_t collen;
  133. Font font, bfont, ifont, ibfont;
  134. GC gc;
  135. } DC;
  136. static inline ushort sixd_to_16bit(int);
  137. static int xmakeglyphfontspecs(XftGlyphFontSpec *, const Glyph *, int, int, int);
  138. static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int);
  139. static void xdrawglyph(Glyph, int, int);
  140. static void xclear(int, int, int, int);
  141. static int xgeommasktogravity(int);
  142. static void xinit(int, int);
  143. static void cresize(int, int);
  144. static void xresize(int, int);
  145. static void xhints(void);
  146. static int xloadcolor(int, const char *, Color *);
  147. static int xloadfont(Font *, FcPattern *);
  148. static void xloadfonts(char *, double);
  149. static void xunloadfont(Font *);
  150. static void xunloadfonts(void);
  151. static void xsetenv(void);
  152. static void xseturgency(int);
  153. static int evcol(XEvent *);
  154. static int evrow(XEvent *);
  155. static void expose(XEvent *);
  156. static void visibility(XEvent *);
  157. static void unmap(XEvent *);
  158. static void kpress(XEvent *);
  159. static void cmessage(XEvent *);
  160. static void resize(XEvent *);
  161. static void focus(XEvent *);
  162. static void brelease(XEvent *);
  163. static void bpress(XEvent *);
  164. static void bmotion(XEvent *);
  165. static void propnotify(XEvent *);
  166. static void selnotify(XEvent *);
  167. static void selclear_(XEvent *);
  168. static void selrequest(XEvent *);
  169. static void setsel(char *, Time);
  170. static void mousesel(XEvent *, int);
  171. static void mousereport(XEvent *);
  172. static char *kmap(KeySym, uint);
  173. static int match(uint, uint);
  174. static void run(void);
  175. static void usage(void);
  176. static void (*handler[LASTEvent])(XEvent *) = {
  177. [KeyPress] = kpress,
  178. [ClientMessage] = cmessage,
  179. [ConfigureNotify] = resize,
  180. [VisibilityNotify] = visibility,
  181. [UnmapNotify] = unmap,
  182. [Expose] = expose,
  183. [FocusIn] = focus,
  184. [FocusOut] = focus,
  185. [MotionNotify] = bmotion,
  186. [ButtonPress] = bpress,
  187. [ButtonRelease] = brelease,
  188. /*
  189. * Uncomment if you want the selection to disappear when you select something
  190. * different in another window.
  191. */
  192. /* [SelectionClear] = selclear_, */
  193. [SelectionNotify] = selnotify,
  194. /*
  195. * PropertyNotify is only turned on when there is some INCR transfer happening
  196. * for the selection retrieval.
  197. */
  198. [PropertyNotify] = propnotify,
  199. [SelectionRequest] = selrequest,
  200. };
  201. /* Globals */
  202. static DC dc;
  203. static XWindow xw;
  204. static XSelection xsel;
  205. static TermWindow win;
  206. /* Font Ring Cache */
  207. enum {
  208. FRC_NORMAL,
  209. FRC_ITALIC,
  210. FRC_BOLD,
  211. FRC_ITALICBOLD
  212. };
  213. typedef struct {
  214. XftFont *font;
  215. int flags;
  216. Rune unicodep;
  217. } Fontcache;
  218. /* Fontcache is an array now. A new font will be appended to the array. */
  219. static Fontcache frc[16];
  220. static int frclen = 0;
  221. static char *usedfont = NULL;
  222. static double usedfontsize = 0;
  223. static double defaultfontsize = 0;
  224. static char *opt_class = NULL;
  225. static char **opt_cmd = NULL;
  226. static char *opt_embed = NULL;
  227. static char *opt_font = NULL;
  228. static char *opt_io = NULL;
  229. static char *opt_line = NULL;
  230. static char *opt_name = NULL;
  231. static char *opt_title = NULL;
  232. static int oldbutton = 3; /* button event on startup: 3 = release */
  233. void
  234. clipcopy(const Arg *dummy)
  235. {
  236. Atom clipboard;
  237. free(xsel.clipboard);
  238. xsel.clipboard = NULL;
  239. if (xsel.primary != NULL) {
  240. xsel.clipboard = xstrdup(xsel.primary);
  241. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  242. XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
  243. }
  244. }
  245. void
  246. clippaste(const Arg *dummy)
  247. {
  248. Atom clipboard;
  249. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  250. XConvertSelection(xw.dpy, clipboard, xsel.xtarget, clipboard,
  251. xw.win, CurrentTime);
  252. }
  253. void
  254. selpaste(const Arg *dummy)
  255. {
  256. XConvertSelection(xw.dpy, XA_PRIMARY, xsel.xtarget, XA_PRIMARY,
  257. xw.win, CurrentTime);
  258. }
  259. void
  260. numlock(const Arg *dummy)
  261. {
  262. win.mode ^= MODE_NUMLOCK;
  263. }
  264. void
  265. zoom(const Arg *arg)
  266. {
  267. Arg larg;
  268. larg.f = usedfontsize + arg->f;
  269. zoomabs(&larg);
  270. }
  271. void
  272. zoomabs(const Arg *arg)
  273. {
  274. xunloadfonts();
  275. xloadfonts(usedfont, arg->f);
  276. cresize(0, 0);
  277. redraw();
  278. xhints();
  279. }
  280. void
  281. zoomreset(const Arg *arg)
  282. {
  283. Arg larg;
  284. if (defaultfontsize > 0) {
  285. larg.f = defaultfontsize;
  286. zoomabs(&larg);
  287. }
  288. }
  289. int
  290. evcol(XEvent *e)
  291. {
  292. int x = e->xbutton.x - borderpx;
  293. LIMIT(x, 0, win.tw - 1);
  294. return x / win.cw;
  295. }
  296. int
  297. evrow(XEvent *e)
  298. {
  299. int y = e->xbutton.y - borderpx;
  300. LIMIT(y, 0, win.th - 1);
  301. return y / win.ch;
  302. }
  303. void
  304. mousesel(XEvent *e, int done)
  305. {
  306. int type, seltype = SEL_REGULAR;
  307. uint state = e->xbutton.state & ~(Button1Mask | forceselmod);
  308. for (type = 1; type < LEN(selmasks); ++type) {
  309. if (match(selmasks[type], state)) {
  310. seltype = type;
  311. break;
  312. }
  313. }
  314. selextend(evcol(e), evrow(e), seltype, done);
  315. if (done)
  316. setsel(getsel(), e->xbutton.time);
  317. }
  318. void
  319. mousereport(XEvent *e)
  320. {
  321. int len, x = evcol(e), y = evrow(e),
  322. button = e->xbutton.button, state = e->xbutton.state;
  323. char buf[40];
  324. static int ox, oy;
  325. /* from urxvt */
  326. if (e->xbutton.type == MotionNotify) {
  327. if (x == ox && y == oy)
  328. return;
  329. if (!IS_SET(MODE_MOUSEMOTION) && !IS_SET(MODE_MOUSEMANY))
  330. return;
  331. /* MOUSE_MOTION: no reporting if no button is pressed */
  332. if (IS_SET(MODE_MOUSEMOTION) && oldbutton == 3)
  333. return;
  334. button = oldbutton + 32;
  335. ox = x;
  336. oy = y;
  337. } else {
  338. if (!IS_SET(MODE_MOUSESGR) && e->xbutton.type == ButtonRelease) {
  339. button = 3;
  340. } else {
  341. button -= Button1;
  342. if (button >= 3)
  343. button += 64 - 3;
  344. }
  345. if (e->xbutton.type == ButtonPress) {
  346. oldbutton = button;
  347. ox = x;
  348. oy = y;
  349. } else if (e->xbutton.type == ButtonRelease) {
  350. oldbutton = 3;
  351. /* MODE_MOUSEX10: no button release reporting */
  352. if (IS_SET(MODE_MOUSEX10))
  353. return;
  354. if (button == 64 || button == 65)
  355. return;
  356. }
  357. }
  358. if (!IS_SET(MODE_MOUSEX10)) {
  359. button += ((state & ShiftMask ) ? 4 : 0)
  360. + ((state & Mod4Mask ) ? 8 : 0)
  361. + ((state & ControlMask) ? 16 : 0);
  362. }
  363. if (IS_SET(MODE_MOUSESGR)) {
  364. len = snprintf(buf, sizeof(buf), "\033[<%d;%d;%d%c",
  365. button, x+1, y+1,
  366. e->xbutton.type == ButtonRelease ? 'm' : 'M');
  367. } else if (x < 223 && y < 223) {
  368. len = snprintf(buf, sizeof(buf), "\033[M%c%c%c",
  369. 32+button, 32+x+1, 32+y+1);
  370. } else {
  371. return;
  372. }
  373. ttywrite(buf, len, 0);
  374. }
  375. void
  376. bpress(XEvent *e)
  377. {
  378. struct timespec now;
  379. MouseShortcut *ms;
  380. int snap;
  381. if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
  382. mousereport(e);
  383. return;
  384. }
  385. for (ms = mshortcuts; ms < mshortcuts + LEN(mshortcuts); ms++) {
  386. if (e->xbutton.button == ms->b
  387. && match(ms->mask, e->xbutton.state)) {
  388. ttywrite(ms->s, strlen(ms->s), 1);
  389. return;
  390. }
  391. }
  392. if (e->xbutton.button == Button1) {
  393. /*
  394. * If the user clicks below predefined timeouts specific
  395. * snapping behaviour is exposed.
  396. */
  397. clock_gettime(CLOCK_MONOTONIC, &now);
  398. if (TIMEDIFF(now, xsel.tclick2) <= tripleclicktimeout) {
  399. snap = SNAP_LINE;
  400. } else if (TIMEDIFF(now, xsel.tclick1) <= doubleclicktimeout) {
  401. snap = SNAP_WORD;
  402. } else {
  403. snap = 0;
  404. }
  405. xsel.tclick2 = xsel.tclick1;
  406. xsel.tclick1 = now;
  407. selstart(evcol(e), evrow(e), snap);
  408. }
  409. }
  410. void
  411. propnotify(XEvent *e)
  412. {
  413. XPropertyEvent *xpev;
  414. Atom clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  415. xpev = &e->xproperty;
  416. if (xpev->state == PropertyNewValue &&
  417. (xpev->atom == XA_PRIMARY ||
  418. xpev->atom == clipboard)) {
  419. selnotify(e);
  420. }
  421. }
  422. void
  423. selnotify(XEvent *e)
  424. {
  425. ulong nitems, ofs, rem;
  426. int format;
  427. uchar *data, *last, *repl;
  428. Atom type, incratom, property = None;
  429. incratom = XInternAtom(xw.dpy, "INCR", 0);
  430. ofs = 0;
  431. if (e->type == SelectionNotify)
  432. property = e->xselection.property;
  433. else if (e->type == PropertyNotify)
  434. property = e->xproperty.atom;
  435. if (property == None)
  436. return;
  437. do {
  438. if (XGetWindowProperty(xw.dpy, xw.win, property, ofs,
  439. BUFSIZ/4, False, AnyPropertyType,
  440. &type, &format, &nitems, &rem,
  441. &data)) {
  442. fprintf(stderr, "Clipboard allocation failed\n");
  443. return;
  444. }
  445. if (e->type == PropertyNotify && nitems == 0 && rem == 0) {
  446. /*
  447. * If there is some PropertyNotify with no data, then
  448. * this is the signal of the selection owner that all
  449. * data has been transferred. We won't need to receive
  450. * PropertyNotify events anymore.
  451. */
  452. MODBIT(xw.attrs.event_mask, 0, PropertyChangeMask);
  453. XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
  454. &xw.attrs);
  455. }
  456. if (type == incratom) {
  457. /*
  458. * Activate the PropertyNotify events so we receive
  459. * when the selection owner does send us the next
  460. * chunk of data.
  461. */
  462. MODBIT(xw.attrs.event_mask, 1, PropertyChangeMask);
  463. XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
  464. &xw.attrs);
  465. /*
  466. * Deleting the property is the transfer start signal.
  467. */
  468. XDeleteProperty(xw.dpy, xw.win, (int)property);
  469. continue;
  470. }
  471. /*
  472. * As seen in getsel:
  473. * Line endings are inconsistent in the terminal and GUI world
  474. * copy and pasting. When receiving some selection data,
  475. * replace all '\n' with '\r'.
  476. * FIXME: Fix the computer world.
  477. */
  478. repl = data;
  479. last = data + nitems * format / 8;
  480. while ((repl = memchr(repl, '\n', last - repl))) {
  481. *repl++ = '\r';
  482. }
  483. if (IS_SET(MODE_BRCKTPASTE) && ofs == 0)
  484. ttywrite("\033[200~", 6, 0);
  485. ttywrite((char *)data, nitems * format / 8, 1);
  486. if (IS_SET(MODE_BRCKTPASTE) && rem == 0)
  487. ttywrite("\033[201~", 6, 0);
  488. XFree(data);
  489. /* number of 32-bit chunks returned */
  490. ofs += nitems * format / 32;
  491. } while (rem > 0);
  492. /*
  493. * Deleting the property again tells the selection owner to send the
  494. * next data chunk in the property.
  495. */
  496. XDeleteProperty(xw.dpy, xw.win, (int)property);
  497. }
  498. void
  499. xclipcopy(void)
  500. {
  501. clipcopy(NULL);
  502. }
  503. void
  504. selclear_(XEvent *e)
  505. {
  506. selclear();
  507. }
  508. void
  509. selrequest(XEvent *e)
  510. {
  511. XSelectionRequestEvent *xsre;
  512. XSelectionEvent xev;
  513. Atom xa_targets, string, clipboard;
  514. char *seltext;
  515. xsre = (XSelectionRequestEvent *) e;
  516. xev.type = SelectionNotify;
  517. xev.requestor = xsre->requestor;
  518. xev.selection = xsre->selection;
  519. xev.target = xsre->target;
  520. xev.time = xsre->time;
  521. if (xsre->property == None)
  522. xsre->property = xsre->target;
  523. /* reject */
  524. xev.property = None;
  525. xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
  526. if (xsre->target == xa_targets) {
  527. /* respond with the supported type */
  528. string = xsel.xtarget;
  529. XChangeProperty(xsre->display, xsre->requestor, xsre->property,
  530. XA_ATOM, 32, PropModeReplace,
  531. (uchar *) &string, 1);
  532. xev.property = xsre->property;
  533. } else if (xsre->target == xsel.xtarget || xsre->target == XA_STRING) {
  534. /*
  535. * xith XA_STRING non ascii characters may be incorrect in the
  536. * requestor. It is not our problem, use utf8.
  537. */
  538. clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
  539. if (xsre->selection == XA_PRIMARY) {
  540. seltext = xsel.primary;
  541. } else if (xsre->selection == clipboard) {
  542. seltext = xsel.clipboard;
  543. } else {
  544. fprintf(stderr,
  545. "Unhandled clipboard selection 0x%lx\n",
  546. xsre->selection);
  547. return;
  548. }
  549. if (seltext != NULL) {
  550. XChangeProperty(xsre->display, xsre->requestor,
  551. xsre->property, xsre->target,
  552. 8, PropModeReplace,
  553. (uchar *)seltext, strlen(seltext));
  554. xev.property = xsre->property;
  555. }
  556. }
  557. /* all done, send a notification to the listener */
  558. if (!XSendEvent(xsre->display, xsre->requestor, 1, 0, (XEvent *) &xev))
  559. fprintf(stderr, "Error sending SelectionNotify event\n");
  560. }
  561. void
  562. setsel(char *str, Time t)
  563. {
  564. if (!str)
  565. return;
  566. free(xsel.primary);
  567. xsel.primary = str;
  568. XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, t);
  569. if (XGetSelectionOwner(xw.dpy, XA_PRIMARY) != xw.win)
  570. selclear();
  571. }
  572. void
  573. xsetsel(char *str)
  574. {
  575. setsel(str, CurrentTime);
  576. }
  577. void
  578. brelease(XEvent *e)
  579. {
  580. if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
  581. mousereport(e);
  582. return;
  583. }
  584. if (e->xbutton.button == Button2)
  585. selpaste(NULL);
  586. else if (e->xbutton.button == Button1)
  587. mousesel(e, 1);
  588. }
  589. void
  590. bmotion(XEvent *e)
  591. {
  592. if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forceselmod)) {
  593. mousereport(e);
  594. return;
  595. }
  596. mousesel(e, 0);
  597. }
  598. void
  599. cresize(int width, int height)
  600. {
  601. int col, row;
  602. if (width != 0)
  603. win.w = width;
  604. if (height != 0)
  605. win.h = height;
  606. col = (win.w - 2 * borderpx) / win.cw;
  607. row = (win.h - 2 * borderpx) / win.ch;
  608. tresize(col, row);
  609. xresize(col, row);
  610. ttyresize(win.tw, win.th);
  611. }
  612. void
  613. xresize(int col, int row)
  614. {
  615. win.tw = MAX(1, col * win.cw);
  616. win.th = MAX(1, row * win.ch);
  617. XFreePixmap(xw.dpy, xw.buf);
  618. xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
  619. xw.depth);
  620. XftDrawChange(xw.draw, xw.buf);
  621. xclear(0, 0, win.w, win.h);
  622. /* resize to new width */
  623. xw.specbuf = xrealloc(xw.specbuf, col * sizeof(GlyphFontSpec));
  624. }
  625. ushort
  626. sixd_to_16bit(int x)
  627. {
  628. return x == 0 ? 0 : 0x3737 + 0x2828 * x;
  629. }
  630. int
  631. xloadcolor(int i, const char *name, Color *ncolor)
  632. {
  633. XRenderColor color = { .alpha = 0xffff };
  634. if (!name) {
  635. if (BETWEEN(i, 16, 255)) { /* 256 color */
  636. if (i < 6*6*6+16) { /* same colors as xterm */
  637. color.red = sixd_to_16bit( ((i-16)/36)%6 );
  638. color.green = sixd_to_16bit( ((i-16)/6) %6 );
  639. color.blue = sixd_to_16bit( ((i-16)/1) %6 );
  640. } else { /* greyscale */
  641. color.red = 0x0808 + 0x0a0a * (i - (6*6*6+16));
  642. color.green = color.blue = color.red;
  643. }
  644. return XftColorAllocValue(xw.dpy, xw.vis,
  645. xw.cmap, &color, ncolor);
  646. } else
  647. name = colorname[i];
  648. }
  649. return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
  650. }
  651. void
  652. xloadcols(void)
  653. {
  654. int i;
  655. static int loaded;
  656. Color *cp;
  657. dc.collen = MAX(LEN(colorname), 256);
  658. dc.col = xmalloc(dc.collen * sizeof(Color));
  659. if (loaded) {
  660. for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp)
  661. XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
  662. }
  663. for (i = 0; i < dc.collen; i++)
  664. if (!xloadcolor(i, NULL, &dc.col[i])) {
  665. if (colorname[i])
  666. die("could not allocate color '%s'\n", colorname[i]);
  667. else
  668. die("could not allocate color %d\n", i);
  669. }
  670. /* set alpha value of bg color */
  671. if (USE_ARGB) {
  672. dc.col[defaultbg].color.alpha = (0xffff * alpha) / OPAQUE;
  673. dc.col[defaultbg].pixel &= 0x00111111;
  674. dc.col[defaultbg].pixel |= alpha << 24;
  675. }
  676. loaded = 1;
  677. }
  678. int
  679. xsetcolorname(int x, const char *name)
  680. {
  681. Color ncolor;
  682. if (!BETWEEN(x, 0, dc.collen))
  683. return 1;
  684. if (!xloadcolor(x, name, &ncolor))
  685. return 1;
  686. XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
  687. dc.col[x] = ncolor;
  688. return 0;
  689. }
  690. void
  691. xtermclear(int col1, int row1, int col2, int row2)
  692. {
  693. XftDrawRect(xw.draw,
  694. &dc.col[IS_SET(MODE_REVERSE) ? defaultfg : defaultbg],
  695. borderpx + col1 * win.cw,
  696. borderpx + row1 * win.ch,
  697. (col2-col1+1) * win.cw,
  698. (row2-row1+1) * win.ch);
  699. }
  700. /*
  701. * Absolute coordinates.
  702. */
  703. void
  704. xclear(int x1, int y1, int x2, int y2)
  705. {
  706. XftDrawRect(xw.draw,
  707. &dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg],
  708. x1, y1, x2-x1, y2-y1);
  709. }
  710. void
  711. xhints(void)
  712. {
  713. XClassHint class = {opt_name ? opt_name : "st",
  714. opt_class ? opt_class : "St"};
  715. XWMHints wm = {.flags = InputHint, .input = 1};
  716. XSizeHints *sizeh;
  717. sizeh = XAllocSizeHints();
  718. sizeh->flags = PSize | PResizeInc | PBaseSize;
  719. sizeh->height = win.h;
  720. sizeh->width = win.w;
  721. sizeh->height_inc = win.ch;
  722. sizeh->width_inc = win.cw;
  723. sizeh->base_height = 2 * borderpx;
  724. sizeh->base_width = 2 * borderpx;
  725. if (xw.isfixed) {
  726. sizeh->flags |= PMaxSize | PMinSize;
  727. sizeh->min_width = sizeh->max_width = win.w;
  728. sizeh->min_height = sizeh->max_height = win.h;
  729. }
  730. if (xw.gm & (XValue|YValue)) {
  731. sizeh->flags |= USPosition | PWinGravity;
  732. sizeh->x = xw.l;
  733. sizeh->y = xw.t;
  734. sizeh->win_gravity = xgeommasktogravity(xw.gm);
  735. }
  736. XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm,
  737. &class);
  738. XFree(sizeh);
  739. }
  740. int
  741. xgeommasktogravity(int mask)
  742. {
  743. switch (mask & (XNegative|YNegative)) {
  744. case 0:
  745. return NorthWestGravity;
  746. case XNegative:
  747. return NorthEastGravity;
  748. case YNegative:
  749. return SouthWestGravity;
  750. }
  751. return SouthEastGravity;
  752. }
  753. int
  754. xloadfont(Font *f, FcPattern *pattern)
  755. {
  756. FcPattern *configured;
  757. FcPattern *match;
  758. FcResult result;
  759. XGlyphInfo extents;
  760. int wantattr, haveattr;
  761. /*
  762. * Manually configure instead of calling XftMatchFont
  763. * so that we can use the configured pattern for
  764. * "missing glyph" lookups.
  765. */
  766. configured = FcPatternDuplicate(pattern);
  767. if (!configured)
  768. return 1;
  769. FcConfigSubstitute(NULL, configured, FcMatchPattern);
  770. XftDefaultSubstitute(xw.dpy, xw.scr, configured);
  771. match = FcFontMatch(NULL, configured, &result);
  772. if (!match) {
  773. FcPatternDestroy(configured);
  774. return 1;
  775. }
  776. if (!(f->match = XftFontOpenPattern(xw.dpy, match))) {
  777. FcPatternDestroy(configured);
  778. FcPatternDestroy(match);
  779. return 1;
  780. }
  781. if ((XftPatternGetInteger(pattern, "slant", 0, &wantattr) ==
  782. XftResultMatch)) {
  783. /*
  784. * Check if xft was unable to find a font with the appropriate
  785. * slant but gave us one anyway. Try to mitigate.
  786. */
  787. if ((XftPatternGetInteger(f->match->pattern, "slant", 0,
  788. &haveattr) != XftResultMatch) || haveattr < wantattr) {
  789. f->badslant = 1;
  790. fputs("font slant does not match\n", stderr);
  791. }
  792. }
  793. if ((XftPatternGetInteger(pattern, "weight", 0, &wantattr) ==
  794. XftResultMatch)) {
  795. if ((XftPatternGetInteger(f->match->pattern, "weight", 0,
  796. &haveattr) != XftResultMatch) || haveattr != wantattr) {
  797. f->badweight = 1;
  798. fputs("font weight does not match\n", stderr);
  799. }
  800. }
  801. XftTextExtentsUtf8(xw.dpy, f->match,
  802. (const FcChar8 *) ascii_printable,
  803. strlen(ascii_printable), &extents);
  804. f->set = NULL;
  805. f->pattern = configured;
  806. f->ascent = f->match->ascent;
  807. f->descent = f->match->descent;
  808. f->lbearing = 0;
  809. f->rbearing = f->match->max_advance_width;
  810. f->height = f->ascent + f->descent;
  811. f->width = DIVCEIL(extents.xOff, strlen(ascii_printable));
  812. return 0;
  813. }
  814. void
  815. xloadfonts(char *fontstr, double fontsize)
  816. {
  817. FcPattern *pattern;
  818. double fontval;
  819. if (fontstr[0] == '-')
  820. pattern = XftXlfdParse(fontstr, False, False);
  821. else
  822. pattern = FcNameParse((FcChar8 *)fontstr);
  823. if (!pattern)
  824. die("can't open font %s\n", fontstr);
  825. if (fontsize > 1) {
  826. FcPatternDel(pattern, FC_PIXEL_SIZE);
  827. FcPatternDel(pattern, FC_SIZE);
  828. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, (double)fontsize);
  829. usedfontsize = fontsize;
  830. } else {
  831. if (FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval) ==
  832. FcResultMatch) {
  833. usedfontsize = fontval;
  834. } else if (FcPatternGetDouble(pattern, FC_SIZE, 0, &fontval) ==
  835. FcResultMatch) {
  836. usedfontsize = -1;
  837. } else {
  838. /*
  839. * Default font size is 12, if none given. This is to
  840. * have a known usedfontsize value.
  841. */
  842. FcPatternAddDouble(pattern, FC_PIXEL_SIZE, 12);
  843. usedfontsize = 12;
  844. }
  845. defaultfontsize = usedfontsize;
  846. }
  847. if (xloadfont(&dc.font, pattern))
  848. die("can't open font %s\n", fontstr);
  849. if (usedfontsize < 0) {
  850. FcPatternGetDouble(dc.font.match->pattern,
  851. FC_PIXEL_SIZE, 0, &fontval);
  852. usedfontsize = fontval;
  853. if (fontsize == 0)
  854. defaultfontsize = fontval;
  855. }
  856. /* Setting character width and height. */
  857. win.cw = ceilf(dc.font.width * cwscale);
  858. win.ch = ceilf(dc.font.height * chscale);
  859. FcPatternDel(pattern, FC_SLANT);
  860. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
  861. if (xloadfont(&dc.ifont, pattern))
  862. die("can't open font %s\n", fontstr);
  863. FcPatternDel(pattern, FC_WEIGHT);
  864. FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
  865. if (xloadfont(&dc.ibfont, pattern))
  866. die("can't open font %s\n", fontstr);
  867. FcPatternDel(pattern, FC_SLANT);
  868. FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
  869. if (xloadfont(&dc.bfont, pattern))
  870. die("can't open font %s\n", fontstr);
  871. FcPatternDestroy(pattern);
  872. }
  873. void
  874. xunloadfont(Font *f)
  875. {
  876. XftFontClose(xw.dpy, f->match);
  877. FcPatternDestroy(f->pattern);
  878. if (f->set)
  879. FcFontSetDestroy(f->set);
  880. }
  881. void
  882. xunloadfonts(void)
  883. {
  884. /* Free the loaded fonts in the font cache. */
  885. while (frclen > 0)
  886. XftFontClose(xw.dpy, frc[--frclen].font);
  887. xunloadfont(&dc.font);
  888. xunloadfont(&dc.bfont);
  889. xunloadfont(&dc.ifont);
  890. xunloadfont(&dc.ibfont);
  891. }
  892. void
  893. xinit(int cols, int rows)
  894. {
  895. XGCValues gcvalues;
  896. Cursor cursor;
  897. Window parent;
  898. pid_t thispid = getpid();
  899. XColor xmousefg, xmousebg;
  900. xw.scr = XDefaultScreen(xw.dpy);
  901. xw.depth = (USE_ARGB) ? 32: XDefaultDepth(xw.dpy, xw.scr);
  902. if (!USE_ARGB)
  903. xw.vis = XDefaultVisual(xw.dpy, xw.scr);
  904. else {
  905. XVisualInfo *vis;
  906. XRenderPictFormat *fmt;
  907. int nvi;
  908. int i;
  909. XVisualInfo tpl = {
  910. .screen = xw.scr,
  911. .depth = 32,
  912. .class = TrueColor
  913. };
  914. vis = XGetVisualInfo(xw.dpy,
  915. VisualScreenMask | VisualDepthMask | VisualClassMask,
  916. &tpl, &nvi);
  917. xw.vis = NULL;
  918. for (i = 0; i < nvi; i++) {
  919. fmt = XRenderFindVisualFormat(xw.dpy, vis[i].visual);
  920. if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) {
  921. xw.vis = vis[i].visual;
  922. break;
  923. }
  924. }
  925. XFree(vis);
  926. if (!xw.vis) {
  927. fprintf(stderr, "Couldn't find ARGB visual.\n");
  928. exit(1);
  929. }
  930. }
  931. /* font */
  932. if (!FcInit())
  933. die("could not init fontconfig.\n");
  934. usedfont = (opt_font == NULL)? font : opt_font;
  935. xloadfonts(usedfont, 0);
  936. /* colors */
  937. if (!USE_ARGB)
  938. xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
  939. else
  940. xw.cmap = XCreateColormap(xw.dpy, XRootWindow(xw.dpy, xw.scr),
  941. xw.vis, None);
  942. xloadcols();
  943. /* adjust fixed window geometry */
  944. win.w = 2 * borderpx + cols * win.cw;
  945. win.h = 2 * borderpx + rows * win.ch;
  946. if (xw.gm & XNegative)
  947. xw.l += DisplayWidth(xw.dpy, xw.scr) - win.w - 2;
  948. if (xw.gm & YNegative)
  949. xw.t += DisplayHeight(xw.dpy, xw.scr) - win.h - 2;
  950. /* Events */
  951. xw.attrs.background_pixel = dc.col[defaultbg].pixel;
  952. xw.attrs.border_pixel = dc.col[defaultbg].pixel;
  953. xw.attrs.bit_gravity = NorthWestGravity;
  954. xw.attrs.event_mask = FocusChangeMask | KeyPressMask
  955. | ExposureMask | VisibilityChangeMask | StructureNotifyMask
  956. | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
  957. xw.attrs.colormap = xw.cmap;
  958. if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0))))
  959. parent = XRootWindow(xw.dpy, xw.scr);
  960. xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
  961. win.w, win.h, 0, xw.depth, InputOutput,
  962. xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
  963. | CWEventMask | CWColormap, &xw.attrs);
  964. memset(&gcvalues, 0, sizeof(gcvalues));
  965. gcvalues.graphics_exposures = False;
  966. xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h, xw.depth);
  967. dc.gc = XCreateGC(xw.dpy, (USE_ARGB) ? xw.buf: parent,
  968. GCGraphicsExposures, &gcvalues);
  969. XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
  970. XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h);
  971. /* font spec buffer */
  972. xw.specbuf = xmalloc(cols * sizeof(GlyphFontSpec));
  973. /* Xft rendering context */
  974. xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
  975. /* input methods */
  976. if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
  977. XSetLocaleModifiers("@im=local");
  978. if ((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
  979. XSetLocaleModifiers("@im=");
  980. if ((xw.xim = XOpenIM(xw.dpy,
  981. NULL, NULL, NULL)) == NULL) {
  982. die("XOpenIM failed. Could not open input"
  983. " device.\n");
  984. }
  985. }
  986. }
  987. xw.xic = XCreateIC(xw.xim, XNInputStyle, XIMPreeditNothing
  988. | XIMStatusNothing, XNClientWindow, xw.win,
  989. XNFocusWindow, xw.win, NULL);
  990. if (xw.xic == NULL)
  991. die("XCreateIC failed. Could not obtain input method.\n");
  992. /* white cursor, black outline */
  993. cursor = XCreateFontCursor(xw.dpy, mouseshape);
  994. XDefineCursor(xw.dpy, xw.win, cursor);
  995. if (XParseColor(xw.dpy, xw.cmap, colorname[mousefg], &xmousefg) == 0) {
  996. xmousefg.red = 0xffff;
  997. xmousefg.green = 0xffff;
  998. xmousefg.blue = 0xffff;
  999. }
  1000. if (XParseColor(xw.dpy, xw.cmap, colorname[mousebg], &xmousebg) == 0) {
  1001. xmousebg.red = 0x0000;
  1002. xmousebg.green = 0x0000;
  1003. xmousebg.blue = 0x0000;
  1004. }
  1005. XRecolorCursor(xw.dpy, cursor, &xmousefg, &xmousebg);
  1006. xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
  1007. xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
  1008. xw.netwmname = XInternAtom(xw.dpy, "_NET_WM_NAME", False);
  1009. XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
  1010. xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False);
  1011. XChangeProperty(xw.dpy, xw.win, xw.netwmpid, XA_CARDINAL, 32,
  1012. PropModeReplace, (uchar *)&thispid, 1);
  1013. win.mode = MODE_NUMLOCK;
  1014. resettitle();
  1015. XMapWindow(xw.dpy, xw.win);
  1016. xhints();
  1017. XSync(xw.dpy, False);
  1018. clock_gettime(CLOCK_MONOTONIC, &xsel.tclick1);
  1019. clock_gettime(CLOCK_MONOTONIC, &xsel.tclick2);
  1020. xsel.primary = NULL;
  1021. xsel.clipboard = NULL;
  1022. xsel.xtarget = XInternAtom(xw.dpy, "UTF8_STRING", 0);
  1023. if (xsel.xtarget == None)
  1024. xsel.xtarget = XA_STRING;
  1025. }
  1026. int
  1027. xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x, int y)
  1028. {
  1029. float winx = borderpx + x * win.cw, winy = borderpx + y * win.ch, xp, yp;
  1030. ushort mode, prevmode = USHRT_MAX;
  1031. Font *font = &dc.font;
  1032. int frcflags = FRC_NORMAL;
  1033. float runewidth = win.cw;
  1034. Rune rune;
  1035. FT_UInt glyphidx;
  1036. FcResult fcres;
  1037. FcPattern *fcpattern, *fontpattern;
  1038. FcFontSet *fcsets[] = { NULL };
  1039. FcCharSet *fccharset;
  1040. int i, f, numspecs = 0;
  1041. for (i = 0, xp = winx, yp = winy + font->ascent; i < len; ++i) {
  1042. /* Fetch rune and mode for current glyph. */
  1043. rune = glyphs[i].u;
  1044. mode = glyphs[i].mode;
  1045. /* Skip dummy wide-character spacing. */
  1046. if (mode == ATTR_WDUMMY)
  1047. continue;
  1048. /* Determine font for glyph if different from previous glyph. */
  1049. if (prevmode != mode) {
  1050. prevmode = mode;
  1051. font = &dc.font;
  1052. frcflags = FRC_NORMAL;
  1053. runewidth = win.cw * ((mode & ATTR_WIDE) ? 2.0f : 1.0f);
  1054. if ((mode & ATTR_ITALIC) && (mode & ATTR_BOLD)) {
  1055. font = &dc.ibfont;
  1056. frcflags = FRC_ITALICBOLD;
  1057. } else if (mode & ATTR_ITALIC) {
  1058. font = &dc.ifont;
  1059. frcflags = FRC_ITALIC;
  1060. } else if (mode & ATTR_BOLD) {
  1061. font = &dc.bfont;
  1062. frcflags = FRC_BOLD;
  1063. }
  1064. yp = winy + font->ascent;
  1065. }
  1066. /* Lookup character index with default font. */
  1067. glyphidx = XftCharIndex(xw.dpy, font->match, rune);
  1068. if (glyphidx) {
  1069. specs[numspecs].font = font->match;
  1070. specs[numspecs].glyph = glyphidx;
  1071. specs[numspecs].x = (short)xp;
  1072. specs[numspecs].y = (short)yp;
  1073. xp += runewidth;
  1074. numspecs++;
  1075. continue;
  1076. }
  1077. /* Fallback on font cache, search the font cache for match. */
  1078. for (f = 0; f < frclen; f++) {
  1079. glyphidx = XftCharIndex(xw.dpy, frc[f].font, rune);
  1080. /* Everything correct. */
  1081. if (glyphidx && frc[f].flags == frcflags)
  1082. break;
  1083. /* We got a default font for a not found glyph. */
  1084. if (!glyphidx && frc[f].flags == frcflags
  1085. && frc[f].unicodep == rune) {
  1086. break;
  1087. }
  1088. }
  1089. /* Nothing was found. Use fontconfig to find matching font. */
  1090. if (f >= frclen) {
  1091. if (!font->set)
  1092. font->set = FcFontSort(0, font->pattern,
  1093. 1, 0, &fcres);
  1094. fcsets[0] = font->set;
  1095. /*
  1096. * Nothing was found in the cache. Now use
  1097. * some dozen of Fontconfig calls to get the
  1098. * font for one single character.
  1099. *
  1100. * Xft and fontconfig are design failures.
  1101. */
  1102. fcpattern = FcPatternDuplicate(font->pattern);
  1103. fccharset = FcCharSetCreate();
  1104. FcCharSetAddChar(fccharset, rune);
  1105. FcPatternAddCharSet(fcpattern, FC_CHARSET,
  1106. fccharset);
  1107. FcPatternAddBool(fcpattern, FC_SCALABLE, 1);
  1108. FcConfigSubstitute(0, fcpattern,
  1109. FcMatchPattern);
  1110. FcDefaultSubstitute(fcpattern);
  1111. fontpattern = FcFontSetMatch(0, fcsets, 1,
  1112. fcpattern, &fcres);
  1113. /*
  1114. * Overwrite or create the new cache entry.
  1115. */
  1116. if (frclen >= LEN(frc)) {
  1117. frclen = LEN(frc) - 1;
  1118. XftFontClose(xw.dpy, frc[frclen].font);
  1119. frc[frclen].unicodep = 0;
  1120. }
  1121. frc[frclen].font = XftFontOpenPattern(xw.dpy,
  1122. fontpattern);
  1123. if (!frc[frclen].font)
  1124. die("XftFontOpenPattern failed seeking fallback font: %s\n",
  1125. strerror(errno));
  1126. frc[frclen].flags = frcflags;
  1127. frc[frclen].unicodep = rune;
  1128. glyphidx = XftCharIndex(xw.dpy, frc[frclen].font, rune);
  1129. f = frclen;
  1130. frclen++;
  1131. FcPatternDestroy(fcpattern);
  1132. FcCharSetDestroy(fccharset);
  1133. }
  1134. specs[numspecs].font = frc[f].font;
  1135. specs[numspecs].glyph = glyphidx;
  1136. specs[numspecs].x = (short)xp;
  1137. specs[numspecs].y = (short)yp;
  1138. xp += runewidth;
  1139. numspecs++;
  1140. }
  1141. return numspecs;
  1142. }
  1143. void
  1144. xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, int y)
  1145. {
  1146. int charlen = len * ((base.mode & ATTR_WIDE) ? 2 : 1);
  1147. int winx = borderpx + x * win.cw, winy = borderpx + y * win.ch,
  1148. width = charlen * win.cw;
  1149. Color *fg, *bg, *temp, revfg, revbg, truefg, truebg;
  1150. XRenderColor colfg, colbg;
  1151. XRectangle r;
  1152. /* Fallback on color display for attributes not supported by the font */
  1153. if (base.mode & ATTR_ITALIC && base.mode & ATTR_BOLD) {
  1154. if (dc.ibfont.badslant || dc.ibfont.badweight)
  1155. base.fg = defaultattr;
  1156. } else if ((base.mode & ATTR_ITALIC && dc.ifont.badslant) ||
  1157. (base.mode & ATTR_BOLD && dc.bfont.badweight)) {
  1158. base.fg = defaultattr;
  1159. }
  1160. if (IS_TRUECOL(base.fg)) {
  1161. colfg.alpha = 0xffff;
  1162. colfg.red = TRUERED(base.fg);
  1163. colfg.green = TRUEGREEN(base.fg);
  1164. colfg.blue = TRUEBLUE(base.fg);
  1165. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &truefg);
  1166. fg = &truefg;
  1167. } else {
  1168. fg = &dc.col[base.fg];
  1169. }
  1170. if (IS_TRUECOL(base.bg)) {
  1171. colbg.alpha = 0xffff;
  1172. colbg.green = TRUEGREEN(base.bg);
  1173. colbg.red = TRUERED(base.bg);
  1174. colbg.blue = TRUEBLUE(base.bg);
  1175. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg, &truebg);
  1176. bg = &truebg;
  1177. } else {
  1178. bg = &dc.col[base.bg];
  1179. }
  1180. /* Change basic system colors [0-7] to bright system colors [8-15] */
  1181. if ((base.mode & ATTR_BOLD_FAINT) == ATTR_BOLD && BETWEEN(base.fg, 0, 7))
  1182. fg = &dc.col[base.fg + 8];
  1183. if (IS_SET(MODE_REVERSE)) {
  1184. if (fg == &dc.col[defaultfg]) {
  1185. fg = &dc.col[defaultbg];
  1186. } else {
  1187. colfg.red = ~fg->color.red;
  1188. colfg.green = ~fg->color.green;
  1189. colfg.blue = ~fg->color.blue;
  1190. colfg.alpha = fg->color.alpha;
  1191. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg,
  1192. &revfg);
  1193. fg = &revfg;
  1194. }
  1195. if (bg == &dc.col[defaultbg]) {
  1196. bg = &dc.col[defaultfg];
  1197. } else {
  1198. colbg.red = ~bg->color.red;
  1199. colbg.green = ~bg->color.green;
  1200. colbg.blue = ~bg->color.blue;
  1201. colbg.alpha = bg->color.alpha;
  1202. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colbg,
  1203. &revbg);
  1204. bg = &revbg;
  1205. }
  1206. }
  1207. if ((base.mode & ATTR_BOLD_FAINT) == ATTR_FAINT) {
  1208. colfg.red = fg->color.red / 2;
  1209. colfg.green = fg->color.green / 2;
  1210. colfg.blue = fg->color.blue / 2;
  1211. colfg.alpha = fg->color.alpha;
  1212. XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &colfg, &revfg);
  1213. fg = &revfg;
  1214. }
  1215. if (base.mode & ATTR_REVERSE) {
  1216. temp = fg;
  1217. fg = bg;
  1218. bg = temp;
  1219. }
  1220. if (base.mode & ATTR_BLINK && win.mode & MODE_BLINK)
  1221. fg = bg;
  1222. if (base.mode & ATTR_INVISIBLE)
  1223. fg = bg;
  1224. /* Intelligent cleaning up of the borders. */
  1225. if (x == 0) {
  1226. xclear(0, (y == 0)? 0 : winy, borderpx,
  1227. winy + win.ch +
  1228. ((winy + win.ch >= borderpx + win.th)? win.h : 0));
  1229. }
  1230. if (winx + width >= borderpx + win.tw) {
  1231. xclear(winx + width, (y == 0)? 0 : winy, win.w,
  1232. ((winy + win.ch >= borderpx + win.th)? win.h : (winy + win.ch)));
  1233. }
  1234. if (y == 0)
  1235. xclear(winx, 0, winx + width, borderpx);
  1236. if (winy + win.ch >= borderpx + win.th)
  1237. xclear(winx, winy + win.ch, winx + width, win.h);
  1238. /* Clean up the region we want to draw to. */
  1239. XftDrawRect(xw.draw, bg, winx, winy, width, win.ch);
  1240. /* Set the clip region because Xft is sometimes dirty. */
  1241. r.x = 0;
  1242. r.y = 0;
  1243. r.height = win.ch;
  1244. r.width = width;
  1245. XftDrawSetClipRectangles(xw.draw, winx, winy, &r, 1);
  1246. /* Render the glyphs. */
  1247. XftDrawGlyphFontSpec(xw.draw, fg, specs, len);
  1248. /* Render underline and strikethrough. */
  1249. if (base.mode & ATTR_UNDERLINE) {
  1250. XftDrawRect(xw.draw, fg, winx, winy + dc.font.ascent + 1,
  1251. width, 1);
  1252. }
  1253. if (base.mode & ATTR_STRUCK) {
  1254. XftDrawRect(xw.draw, fg, winx, winy + 2 * dc.font.ascent / 3,
  1255. width, 1);
  1256. }
  1257. /* Reset clip to none. */
  1258. XftDrawSetClip(xw.draw, 0);
  1259. }
  1260. void
  1261. xdrawglyph(Glyph g, int x, int y)
  1262. {
  1263. int numspecs;
  1264. XftGlyphFontSpec spec;
  1265. numspecs = xmakeglyphfontspecs(&spec, &g, 1, x, y);
  1266. xdrawglyphfontspecs(&spec, g, numspecs, x, y);
  1267. }
  1268. void
  1269. xdrawcursor(int cx, int cy, Glyph g, int ox, int oy, Glyph og)
  1270. {
  1271. Color drawcol;
  1272. /* remove the old cursor */
  1273. if (selected(ox, oy))
  1274. og.mode ^= ATTR_REVERSE;
  1275. xdrawglyph(og, ox, oy);
  1276. if (IS_SET(MODE_HIDE))
  1277. return;
  1278. /*
  1279. * Select the right color for the right mode.
  1280. */
  1281. g.mode &= ATTR_BOLD|ATTR_ITALIC|ATTR_UNDERLINE|ATTR_STRUCK|ATTR_WIDE;
  1282. if (IS_SET(MODE_REVERSE)) {
  1283. g.mode |= ATTR_REVERSE;
  1284. g.bg = defaultfg;
  1285. if (selected(cx, cy)) {
  1286. drawcol = dc.col[defaultcs];
  1287. g.fg = defaultrcs;
  1288. } else {
  1289. drawcol = dc.col[defaultrcs];
  1290. g.fg = defaultcs;
  1291. }
  1292. } else {
  1293. if (selected(cx, cy)) {
  1294. g.fg = defaultfg;
  1295. g.bg = defaultrcs;
  1296. } else {
  1297. g.fg = defaultbg;
  1298. g.bg = defaultcs;
  1299. }
  1300. drawcol = dc.col[g.bg];
  1301. }
  1302. /* draw the new one */
  1303. if (IS_SET(MODE_FOCUSED)) {
  1304. switch (win.cursor) {
  1305. case 7: /* st extension: snowman (U+2603) */
  1306. g.u = 0x2603;
  1307. case 0: /* Blinking Block */
  1308. case 1: /* Blinking Block (Default) */
  1309. case 2: /* Steady Block */
  1310. xdrawglyph(g, cx, cy);
  1311. break;
  1312. case 3: /* Blinking Underline */
  1313. case 4: /* Steady Underline */
  1314. XftDrawRect(xw.draw, &drawcol,
  1315. borderpx + cx * win.cw,
  1316. borderpx + (cy + 1) * win.ch - \
  1317. cursorthickness,
  1318. win.cw, cursorthickness);
  1319. break;
  1320. case 5: /* Blinking bar */
  1321. case 6: /* Steady bar */
  1322. XftDrawRect(xw.draw, &drawcol,
  1323. borderpx + cx * win.cw,
  1324. borderpx + cy * win.ch,
  1325. cursorthickness, win.ch);
  1326. break;
  1327. }
  1328. } else {
  1329. XftDrawRect(xw.draw, &drawcol,
  1330. borderpx + cx * win.cw,
  1331. borderpx + cy * win.ch,
  1332. win.cw - 1, 1);
  1333. XftDrawRect(xw.draw, &drawcol,
  1334. borderpx + cx * win.cw,
  1335. borderpx + cy * win.ch,
  1336. 1, win.ch - 1);
  1337. XftDrawRect(xw.draw, &drawcol,
  1338. borderpx + (cx + 1) * win.cw - 1,
  1339. borderpx + cy * win.ch,
  1340. 1, win.ch - 1);
  1341. XftDrawRect(xw.draw, &drawcol,
  1342. borderpx + cx * win.cw,
  1343. borderpx + (cy + 1) * win.ch - 1,
  1344. win.cw, 1);
  1345. }
  1346. }
  1347. void
  1348. xsetenv(void)
  1349. {
  1350. char buf[sizeof(long) * 8 + 1];
  1351. snprintf(buf, sizeof(buf), "%lu", xw.win);
  1352. setenv("WINDOWID", buf, 1);
  1353. }
  1354. void
  1355. xsettitle(char *p)
  1356. {
  1357. XTextProperty prop;
  1358. DEFAULT(p, opt_title);
  1359. Xutf8TextListToTextProperty(xw.dpy, &p, 1, XUTF8StringStyle,
  1360. &prop);
  1361. XSetWMName(xw.dpy, xw.win, &prop);
  1362. XSetTextProperty(xw.dpy, xw.win, &prop, xw.netwmname);
  1363. XFree(prop.value);
  1364. }
  1365. int
  1366. xstartdraw(void)
  1367. {
  1368. return IS_SET(MODE_VISIBLE);
  1369. }
  1370. void
  1371. xdrawline(Line line, int x1, int y1, int x2)
  1372. {
  1373. int i, x, ox, numspecs;
  1374. Glyph base, new;
  1375. XftGlyphFontSpec *specs = xw.specbuf;
  1376. numspecs = xmakeglyphfontspecs(specs, &line[x1], x2 - x1, x1, y1);
  1377. i = ox = 0;
  1378. for (x = x1; x < x2 && i < numspecs; x++) {
  1379. new = line[x];
  1380. if (new.mode == ATTR_WDUMMY)
  1381. continue;
  1382. if (selected(x, y1))
  1383. new.mode ^= ATTR_REVERSE;
  1384. if (i > 0 && ATTRCMP(base, new)) {
  1385. xdrawglyphfontspecs(specs, base, i, ox, y1);
  1386. specs += i;
  1387. numspecs -= i;
  1388. i = 0;
  1389. }
  1390. if (i == 0) {
  1391. ox = x;
  1392. base = new;
  1393. }
  1394. i++;
  1395. }
  1396. if (i > 0)
  1397. xdrawglyphfontspecs(specs, base, i, ox, y1);
  1398. }
  1399. void
  1400. xfinishdraw(void)
  1401. {
  1402. XCopyArea(xw.dpy, xw.buf, xw.win, dc.gc, 0, 0, win.w,
  1403. win.h, 0, 0);
  1404. XSetForeground(xw.dpy, dc.gc,
  1405. dc.col[IS_SET(MODE_REVERSE)?
  1406. defaultfg : defaultbg].pixel);
  1407. }
  1408. void
  1409. expose(XEvent *ev)
  1410. {
  1411. redraw();
  1412. }
  1413. void
  1414. visibility(XEvent *ev)
  1415. {
  1416. XVisibilityEvent *e = &ev->xvisibility;
  1417. MODBIT(win.mode, e->state != VisibilityFullyObscured, MODE_VISIBLE);
  1418. }
  1419. void
  1420. unmap(XEvent *ev)
  1421. {
  1422. win.mode &= ~MODE_VISIBLE;
  1423. }
  1424. void
  1425. xsetpointermotion(int set)
  1426. {
  1427. MODBIT(xw.attrs.event_mask, set, PointerMotionMask);
  1428. XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs);
  1429. }
  1430. void
  1431. xsetmode(int set, unsigned int flags)
  1432. {
  1433. int mode = win.mode;
  1434. MODBIT(win.mode, set, flags);
  1435. if ((win.mode & MODE_REVERSE) != (mode & MODE_REVERSE))
  1436. redraw();
  1437. }
  1438. int
  1439. xsetcursor(int cursor)
  1440. {
  1441. DEFAULT(cursor, 1);
  1442. if (!BETWEEN(cursor, 0, 6))
  1443. return 1;
  1444. win.cursor = cursor;
  1445. return 0;
  1446. }
  1447. void
  1448. xseturgency(int add)
  1449. {
  1450. XWMHints *h = XGetWMHints(xw.dpy, xw.win);
  1451. MODBIT(h->flags, add, XUrgencyHint);
  1452. XSetWMHints(xw.dpy, xw.win, h);
  1453. XFree(h);
  1454. }
  1455. void
  1456. xbell(void)
  1457. {
  1458. if (!(IS_SET(MODE_FOCUSED)))
  1459. xseturgency(1);
  1460. if (bellvolume)
  1461. XkbBell(xw.dpy, xw.win, bellvolume, (Atom)NULL);
  1462. }
  1463. void
  1464. focus(XEvent *ev)
  1465. {
  1466. XFocusChangeEvent *e = &ev->xfocus;
  1467. if (e->mode == NotifyGrab)
  1468. return;
  1469. if (ev->type == FocusIn) {
  1470. XSetICFocus(xw.xic);
  1471. win.mode |= MODE_FOCUSED;
  1472. xseturgency(0);
  1473. if (IS_SET(MODE_FOCUS))
  1474. ttywrite("\033[I", 3, 0);
  1475. } else {
  1476. XUnsetICFocus(xw.xic);
  1477. win.mode &= ~MODE_FOCUSED;
  1478. if (IS_SET(MODE_FOCUS))
  1479. ttywrite("\033[O", 3, 0);
  1480. }
  1481. }
  1482. int
  1483. match(uint mask, uint state)
  1484. {
  1485. return mask == XK_ANY_MOD || mask == (state & ~ignoremod);
  1486. }
  1487. char*
  1488. kmap(KeySym k, uint state)
  1489. {
  1490. Key *kp;
  1491. int i;
  1492. /* Check for mapped keys out of X11 function keys. */
  1493. for (i = 0; i < LEN(mappedkeys); i++) {
  1494. if (mappedkeys[i] == k)
  1495. break;
  1496. }
  1497. if (i == LEN(mappedkeys)) {
  1498. if ((k & 0xFFFF) < 0xFD00)
  1499. return NULL;
  1500. }
  1501. for (kp = key; kp < key + LEN(key); kp++) {
  1502. if (kp->k != k)
  1503. continue;
  1504. if (!match(kp->mask, state))
  1505. continue;
  1506. if (IS_SET(MODE_APPKEYPAD) ? kp->appkey < 0 : kp->appkey > 0)
  1507. continue;
  1508. if (IS_SET(MODE_NUMLOCK) && kp->appkey == 2)
  1509. continue;
  1510. if (IS_SET(MODE_APPCURSOR) ? kp->appcursor < 0 : kp->appcursor > 0)
  1511. continue;
  1512. return kp->s;
  1513. }
  1514. return NULL;
  1515. }
  1516. void
  1517. kpress(XEvent *ev)
  1518. {
  1519. XKeyEvent *e = &ev->xkey;
  1520. KeySym ksym;
  1521. char buf[32], *customkey;
  1522. int len;
  1523. Rune c;
  1524. Status status;
  1525. Shortcut *bp;
  1526. if (IS_SET(MODE_KBDLOCK))
  1527. return;
  1528. len = XmbLookupString(xw.xic, e, buf, sizeof buf, &ksym, &status);
  1529. /* 1. shortcuts */
  1530. for (bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
  1531. if (ksym == bp->keysym && match(bp->mod, e->state)) {
  1532. bp->func(&(bp->arg));
  1533. return;
  1534. }
  1535. }
  1536. /* 2. custom keys from config.h */
  1537. if ((customkey = kmap(ksym, e->state))) {
  1538. ttywrite(customkey, strlen(customkey), 1);
  1539. return;
  1540. }
  1541. /* 3. composed string from input method */
  1542. if (len == 0)
  1543. return;
  1544. if (len == 1 && e->state & Mod1Mask) {
  1545. if (IS_SET(MODE_8BIT)) {
  1546. if (*buf < 0177) {
  1547. c = *buf | 0x80;
  1548. len = utf8encode(c, buf);
  1549. }
  1550. } else {
  1551. buf[1] = buf[0];
  1552. buf[0] = '\033';
  1553. len = 2;
  1554. }
  1555. }
  1556. ttywrite(buf, len, 1);
  1557. }
  1558. void
  1559. cmessage(XEvent *e)
  1560. {
  1561. /*
  1562. * See xembed specs
  1563. * http://standards.freedesktop.org/xembed-spec/xembed-spec-latest.html
  1564. */
  1565. if (e->xclient.message_type == xw.xembed && e->xclient.format == 32) {
  1566. if (e->xclient.data.l[1] == XEMBED_FOCUS_IN) {
  1567. win.mode |= MODE_FOCUSED;
  1568. xseturgency(0);
  1569. } else if (e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
  1570. win.mode &= ~MODE_FOCUSED;
  1571. }
  1572. } else if (e->xclient.data.l[0] == xw.wmdeletewin) {
  1573. ttyhangup();
  1574. exit(0);
  1575. }
  1576. }
  1577. void
  1578. resize(XEvent *e)
  1579. {
  1580. if (e->xconfigure.width == win.w && e->xconfigure.height == win.h)
  1581. return;
  1582. cresize(e->xconfigure.width, e->xconfigure.height);
  1583. }
  1584. void
  1585. run(void)
  1586. {
  1587. XEvent ev;
  1588. int w = win.w, h = win.h;
  1589. fd_set rfd;
  1590. int xfd = XConnectionNumber(xw.dpy), xev, blinkset = 0, dodraw = 0;
  1591. int ttyfd;
  1592. struct timespec drawtimeout, *tv = NULL, now, last, lastblink;
  1593. long deltatime;
  1594. /* Waiting for window mapping */
  1595. do {
  1596. XNextEvent(xw.dpy, &ev);
  1597. /*
  1598. * This XFilterEvent call is required because of XOpenIM. It
  1599. * does filter out the key event and some client message for
  1600. * the input method too.
  1601. */
  1602. if (XFilterEvent(&ev, None))
  1603. continue;
  1604. if (ev.type == ConfigureNotify) {
  1605. w = ev.xconfigure.width;
  1606. h = ev.xconfigure.height;
  1607. }
  1608. } while (ev.type != MapNotify);
  1609. ttyfd = ttynew(opt_line, shell, opt_io, opt_cmd);
  1610. cresize(w, h);
  1611. clock_gettime(CLOCK_MONOTONIC, &last);
  1612. lastblink = last;
  1613. for (xev = actionfps;;) {
  1614. FD_ZERO(&rfd);
  1615. FD_SET(ttyfd, &rfd);
  1616. FD_SET(xfd, &rfd);
  1617. if (pselect(MAX(xfd, ttyfd)+1, &rfd, NULL, NULL, tv, NULL) < 0) {
  1618. if (errno == EINTR)
  1619. continue;
  1620. die("select failed: %s\n", strerror(errno));
  1621. }
  1622. if (FD_ISSET(ttyfd, &rfd)) {
  1623. ttyread();
  1624. if (blinktimeout) {
  1625. blinkset = tattrset(ATTR_BLINK);
  1626. if (!blinkset)
  1627. MODBIT(win.mode, 0, MODE_BLINK);
  1628. }
  1629. }
  1630. if (FD_ISSET(xfd, &rfd))
  1631. xev = actionfps;
  1632. clock_gettime(CLOCK_MONOTONIC, &now);
  1633. drawtimeout.tv_sec = 0;
  1634. drawtimeout.tv_nsec = (1000 * 1E6)/ xfps;
  1635. tv = &drawtimeout;
  1636. dodraw = 0;
  1637. if (blinktimeout && TIMEDIFF(now, lastblink) > blinktimeout) {
  1638. tsetdirtattr(ATTR_BLINK);
  1639. win.mode ^= MODE_BLINK;
  1640. lastblink = now;
  1641. dodraw = 1;
  1642. }
  1643. deltatime = TIMEDIFF(now, last);
  1644. if (deltatime > 1000 / (xev ? xfps : actionfps)) {
  1645. dodraw = 1;
  1646. last = now;
  1647. }
  1648. if (dodraw) {
  1649. while (XPending(xw.dpy)) {
  1650. XNextEvent(xw.dpy, &ev);
  1651. if (XFilterEvent(&ev, None))
  1652. continue;
  1653. if (handler[ev.type])
  1654. (handler[ev.type])(&ev);
  1655. }
  1656. draw();
  1657. XFlush(xw.dpy);
  1658. if (xev && !FD_ISSET(xfd, &rfd))
  1659. xev--;
  1660. if (!FD_ISSET(ttyfd, &rfd) && !FD_ISSET(xfd, &rfd)) {
  1661. if (blinkset) {
  1662. if (TIMEDIFF(now, lastblink) \
  1663. > blinktimeout) {
  1664. drawtimeout.tv_nsec = 1000;
  1665. } else {
  1666. drawtimeout.tv_nsec = (1E6 * \
  1667. (blinktimeout - \
  1668. TIMEDIFF(now,
  1669. lastblink)));
  1670. }
  1671. drawtimeout.tv_sec = \
  1672. drawtimeout.tv_nsec / 1E9;
  1673. drawtimeout.tv_nsec %= (long)1E9;
  1674. } else {
  1675. tv = NULL;
  1676. }
  1677. }
  1678. }
  1679. }
  1680. }
  1681. int
  1682. resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst)
  1683. {
  1684. char **sdst = dst;
  1685. int *idst = dst;
  1686. float *fdst = dst;
  1687. char fullname[256];
  1688. char fullclass[256];
  1689. char *type;
  1690. XrmValue ret;
  1691. snprintf(fullname, sizeof(fullname), "%s.%s",
  1692. opt_name ? opt_name : "st", name);
  1693. snprintf(fullclass, sizeof(fullclass), "%s.%s",
  1694. opt_class ? opt_class : "St", name);
  1695. fullname[sizeof(fullname) - 1] = fullclass[sizeof(fullclass) - 1] = '\0';
  1696. XrmGetResource(db, fullname, fullclass, &type, &ret);
  1697. if (ret.addr == NULL || strncmp("String", type, 64))
  1698. return 1;
  1699. switch (rtype) {
  1700. case STRING:
  1701. *sdst = ret.addr;
  1702. break;
  1703. case INTEGER:
  1704. *idst = strtoul(ret.addr, NULL, 10);
  1705. break;
  1706. case FLOAT:
  1707. *fdst = strtof(ret.addr, NULL);
  1708. break;
  1709. }
  1710. return 0;
  1711. }
  1712. void
  1713. config_init(void)
  1714. {
  1715. char *resm;
  1716. XrmDatabase db;
  1717. ResourcePref *p;
  1718. XrmInitialize();
  1719. resm = XResourceManagerString(xw.dpy);
  1720. if (!resm)
  1721. return;
  1722. db = XrmGetStringDatabase(resm);
  1723. for (p = resources; p < resources + LEN(resources); p++)
  1724. resource_load(db, p->name, p->type, p->dst);
  1725. }
  1726. void
  1727. usage(void)
  1728. {
  1729. die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
  1730. " [-n name] [-o file]\n"
  1731. " [-T title] [-t title] [-w windowid]"
  1732. " [[-e] command [args ...]]\n"
  1733. " %s [-aiv] [-c class] [-f font] [-g geometry]"
  1734. " [-n name] [-o file]\n"
  1735. " [-T title] [-t title] [-w windowid] -l line"
  1736. " [stty_args ...]\n", argv0, argv0);
  1737. }
  1738. int
  1739. main(int argc, char *argv[])
  1740. {
  1741. xw.l = xw.t = 0;
  1742. xw.isfixed = False;
  1743. win.cursor = cursorshape;
  1744. ARGBEGIN {
  1745. case 'a':
  1746. allowaltscreen = 0;
  1747. break;
  1748. case 'c':
  1749. opt_class = EARGF(usage());
  1750. break;
  1751. case 'e':
  1752. if (argc > 0)
  1753. --argc, ++argv;
  1754. goto run;
  1755. case 'f':
  1756. opt_font = EARGF(usage());
  1757. break;
  1758. case 'g':
  1759. xw.gm = XParseGeometry(EARGF(usage()),
  1760. &xw.l, &xw.t, &cols, &rows);
  1761. break;
  1762. case 'i':
  1763. xw.isfixed = 1;
  1764. break;
  1765. case 'o':
  1766. opt_io = EARGF(usage());
  1767. break;
  1768. case 'l':
  1769. opt_line = EARGF(usage());
  1770. break;
  1771. case 'n':
  1772. opt_name = EARGF(usage());
  1773. break;
  1774. case 't':
  1775. case 'T':
  1776. opt_title = EARGF(usage());
  1777. break;
  1778. case 'w':
  1779. opt_embed = EARGF(usage());
  1780. break;
  1781. case 'v':
  1782. die("%s " VERSION "\n", argv0);
  1783. break;
  1784. default:
  1785. usage();
  1786. } ARGEND;
  1787. run:
  1788. if (argc > 0) /* eat all remaining arguments */
  1789. opt_cmd = argv;
  1790. if (!opt_title)
  1791. opt_title = (opt_line || !opt_cmd) ? "st" : opt_cmd[0];
  1792. setlocale(LC_CTYPE, "");
  1793. XSetLocaleModifiers("");
  1794. if(!(xw.dpy = XOpenDisplay(NULL)))
  1795. die("Can't open display\n");
  1796. config_init();
  1797. cols = MAX(cols, 1);
  1798. rows = MAX(rows, 1);
  1799. tnew(cols, rows);
  1800. xinit(cols, rows);
  1801. xsetenv();
  1802. selinit();
  1803. run();
  1804. return 0;
  1805. }