win-x11a.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  1. /*
  2. * This implementation of the screen functions for X11 uses the
  3. * portable implementation of the Inferno drawing operations (libmemdraw)
  4. * to do the work, then has flushmemscreen copy the result to the X11 display.
  5. * Thus it potentially supports all colour depths but with a possible
  6. * performance penalty (although it tries to use the X11 shared memory extension
  7. * to copy the result to the screen, which might reduce the latter).
  8. *
  9. * CraigN
  10. */
  11. #define _GNU_SOURCE 1
  12. #define XTHREADS
  13. #include "dat.h"
  14. #include "fns.h"
  15. #undef log2
  16. #include <draw.h>
  17. #include "cursor.h"
  18. #include "keyboard.h"
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <fcntl.h>
  22. #define Colormap XColormap
  23. #define Cursor XCursor
  24. #define Display XDisplay
  25. #define Drawable XDrawable
  26. #define Font XFont
  27. #define GC XGC
  28. #define Point XPoint
  29. #define Rectangle XRectangle
  30. #define Screen XScreen
  31. #define Visual XVisual
  32. #define Window XWindow
  33. #include <X11/Xlib.h>
  34. #include <X11/Xatom.h>
  35. #include <X11/Xutil.h>
  36. #include <X11/keysym.h>
  37. #include <X11/extensions/XShm.h>
  38. #include "keysym2ucs.h"
  39. #undef Colormap
  40. #undef Cursor
  41. #undef Display
  42. #undef XDrawable
  43. #undef Font
  44. #undef GC
  45. #undef Point
  46. #undef Rectangle
  47. #undef Screen
  48. #undef Visual
  49. #undef Window
  50. #include <sys/ipc.h>
  51. #include <sys/shm.h>
  52. static int displaydepth;
  53. extern ulong displaychan;
  54. enum
  55. {
  56. DblTime = 300 /* double click time in msec */
  57. };
  58. /* screen data .... */
  59. static uchar* gscreendata;
  60. static uchar* xscreendata;
  61. XColor map[256]; /* Inferno colormap array */
  62. XColor mapr[256]; /* Inferno red colormap array */
  63. XColor mapg[256]; /* Inferno green colormap array */
  64. XColor mapb[256]; /* Inferno blue colormap array */
  65. XColor map7[128]; /* Inferno colormap array */
  66. uchar map7to8[128][2];
  67. /* for copy/paste, lifted from plan9ports via drawterm */
  68. static Atom clipboard;
  69. static Atom utf8string;
  70. static Atom targets;
  71. static Atom text;
  72. static Atom compoundtext;
  73. static Atom cursorchange;
  74. static XColormap xcmap; /* Default shared colormap */
  75. static int infernotox11[256]; /* Values for mapping between */
  76. static int infernortox11[256]; /* Values for mapping between */
  77. static int infernogtox11[256]; /* Values for mapping between */
  78. static int infernobtox11[256]; /* Values for mapping between */
  79. static int triedscreen;
  80. static XDrawable xdrawable;
  81. static void xexpose(XEvent*);
  82. static void xmouse(XEvent*);
  83. static void xkeyboard(XEvent*);
  84. static void xsetcursor(XEvent*);
  85. static void xkbdproc(void*);
  86. static void xdestroy(XEvent*);
  87. static void xselect(XEvent*, XDisplay*);
  88. static void xproc(void*);
  89. static void xinitscreen(int, int, ulong, ulong*, int*);
  90. static void initxcmap(XWindow);
  91. static XGC creategc(XDrawable);
  92. static void graphicsgmap(XColor*, int);
  93. static void graphicscmap(XColor*);
  94. static void graphicsrgbmap(XColor*, XColor*, XColor*);
  95. static int xscreendepth;
  96. static XDisplay* xdisplay; /* used holding draw lock */
  97. static XDisplay* xmcon; /* used only in xproc */
  98. static XDisplay* xkbdcon; /* used only in xkbdproc */
  99. static XDisplay* xsnarfcon; /* used holding clip.lk */
  100. static XVisual *xvis;
  101. static XGC xgc;
  102. static XImage *img;
  103. static int is_shm;
  104. static int putsnarf, assertsnarf;
  105. char *gkscanid = "emu_x11";
  106. /*
  107. * The documentation for the XSHM extension implies that if the server
  108. * supports XSHM but is not the local machine, the XShm calls will
  109. * return False; but this turns out not to be the case. Instead, the
  110. * server throws a BadAccess error. So, we need to catch X errors
  111. * around all of our XSHM calls, sigh.
  112. */
  113. static int shm_got_x_error = 0;
  114. static XErrorHandler old_handler = 0;
  115. static XErrorHandler old_io_handler = 0;
  116. static int
  117. shm_ehandler(XDisplay *dpy, XErrorEvent *error)
  118. {
  119. shm_got_x_error = 1;
  120. return 0;
  121. }
  122. static void
  123. clean_errhandlers(void)
  124. {
  125. /* remove X11 error handler(s) */
  126. if(old_handler)
  127. XSetErrorHandler(old_handler);
  128. old_handler = 0;
  129. if(old_io_handler)
  130. XSetErrorHandler(old_io_handler);
  131. old_io_handler = 0;
  132. }
  133. static int
  134. makesharedfb(void)
  135. {
  136. XShmSegmentInfo *shminfo;
  137. shminfo = malloc(sizeof(XShmSegmentInfo));
  138. if(shminfo == nil) {
  139. fprint(2, "emu: cannot allocate XShmSegmentInfo\n");
  140. cleanexit(0);
  141. }
  142. /* setup to catch X11 error(s) */
  143. XSync(xdisplay, 0);
  144. shm_got_x_error = 0;
  145. if(old_handler != shm_ehandler)
  146. old_handler = XSetErrorHandler(shm_ehandler);
  147. if(old_io_handler != shm_ehandler)
  148. old_io_handler = XSetErrorHandler(shm_ehandler);
  149. img = XShmCreateImage(xdisplay, xvis, xscreendepth, ZPixmap,
  150. NULL, shminfo, Xsize, Ysize);
  151. XSync(xdisplay, 0);
  152. /* did we get an X11 error? if so then try without shm */
  153. if(shm_got_x_error) {
  154. free(shminfo);
  155. shminfo = NULL;
  156. clean_errhandlers();
  157. return 0;
  158. }
  159. if(img == nil) {
  160. fprint(2, "emu: cannot allocate virtual screen buffer\n");
  161. cleanexit(0);
  162. }
  163. shminfo->shmid = shmget(IPC_PRIVATE, img->bytes_per_line * img->height, IPC_CREAT|0777);
  164. shminfo->shmaddr = img->data = shmat(shminfo->shmid, 0, 0);
  165. shminfo->readOnly = True;
  166. if(!XShmAttach(xdisplay, shminfo)) {
  167. fprint(2, "emu: cannot allocate virtual screen buffer\n");
  168. cleanexit(0);
  169. }
  170. XSync(xdisplay, 0);
  171. /*
  172. * Delete the shared segment right now; the segment
  173. * won't actually go away until both the client and
  174. * server have deleted it. The server will delete it
  175. * as soon as the client disconnects, so we might as
  176. * well delete our side now as later.
  177. */
  178. shmctl(shminfo->shmid, IPC_RMID, 0);
  179. /* did we get an X11 error? if so then try without shm */
  180. if(shm_got_x_error) {
  181. XDestroyImage(img);
  182. XSync(xdisplay, 0);
  183. free(shminfo);
  184. shminfo = NULL;
  185. clean_errhandlers();
  186. return 0;
  187. }
  188. gscreendata = malloc(Xsize * Ysize * (displaydepth >> 3));
  189. if(gscreendata == nil) {
  190. fprint(2, "emu: cannot allocate screen buffer (%dx%dx%d)\n", Xsize, Ysize, displaydepth);
  191. cleanexit(0);
  192. }
  193. xscreendata = (uchar*)img->data;
  194. clean_errhandlers();
  195. return 1;
  196. }
  197. uchar*
  198. attachscreen(Rectangle *r, ulong *chan, int *d, int *width, int *softscreen)
  199. {
  200. int depth;
  201. Xsize &= ~0x3; /* ensure multiple of 4 */
  202. r->min.x = 0;
  203. r->min.y = 0;
  204. r->max.x = Xsize;
  205. r->max.y = Ysize;
  206. if(!triedscreen){
  207. xinitscreen(Xsize, Ysize, displaychan, chan, d);
  208. /*
  209. * moved xproc from here to end since it could cause an expose event and
  210. * hence a flushmemscreen before xscreendata is initialized
  211. */
  212. }
  213. else{
  214. *chan = displaychan;
  215. *d = displaydepth;
  216. }
  217. *width = (Xsize/4)*(*d/8);
  218. *softscreen = 1;
  219. displaychan = *chan;
  220. displaydepth = *d;
  221. /* check for X Shared Memory Extension */
  222. is_shm = XShmQueryExtension(xdisplay);
  223. if(!is_shm || !makesharedfb()){
  224. is_shm = 0;
  225. depth = xscreendepth;
  226. if(depth == 24)
  227. depth = 32;
  228. /* allocate virtual screen */
  229. gscreendata = malloc(Xsize * Ysize * (displaydepth >> 3));
  230. xscreendata = malloc(Xsize * Ysize * (depth >> 3));
  231. if(gscreendata == nil || xscreendata == nil) {
  232. fprint(2, "emu: can not allocate virtual screen buffer (%dx%dx%d[%d])\n", Xsize, Ysize, displaydepth, depth);
  233. return 0;
  234. }
  235. img = XCreateImage(xdisplay, xvis, xscreendepth, ZPixmap, 0,
  236. (char*)xscreendata, Xsize, Ysize, 8, Xsize * (depth >> 3));
  237. if(img == nil) {
  238. fprint(2, "emu: can not allocate virtual screen buffer (%dx%dx%d)\n", Xsize, Ysize, depth);
  239. return 0;
  240. }
  241. }
  242. if(!triedscreen){
  243. triedscreen = 1;
  244. kproc("xproc", xproc, xmcon, 0);
  245. kproc("xkbdproc", xkbdproc, xkbdcon, KPX11); /* silly stack size for bloated X11 */
  246. }
  247. return gscreendata;
  248. }
  249. static void
  250. copy32to32(Rectangle r)
  251. {
  252. int dx, width;
  253. uchar *p, *ep, *cp;
  254. u32int v, w, *dp, *wp, *edp, *lp;
  255. width = Dx(r);
  256. dx = Xsize - width;
  257. dp = (u32int*)(gscreendata + (r.min.y * Xsize + r.min.x) * 4);
  258. wp = (u32int*)(xscreendata + (r.min.y * Xsize + r.min.x) * 4);
  259. edp = (u32int*)(gscreendata + (r.max.y * Xsize + r.max.x) * 4);
  260. while(dp < edp) {
  261. lp = dp + width;
  262. while(dp < lp){
  263. v = *dp++;
  264. w = infernortox11[(v>>16)&0xff]<<16|infernogtox11[(v>>8)&0xff]<<8|infernobtox11[(v>>0)&0xff]<<0;
  265. *wp++ = w;
  266. }
  267. dp += dx;
  268. wp += dx;
  269. }
  270. }
  271. static void
  272. copy8to32(Rectangle r)
  273. {
  274. int dx, width;
  275. uchar *p, *ep, *lp;
  276. u32int *wp;
  277. width = Dx(r);
  278. dx = Xsize - width;
  279. p = gscreendata + r.min.y * Xsize + r.min.x;
  280. wp = (u32int *)(xscreendata + (r.min.y * Xsize + r.min.x) * 4);
  281. ep = gscreendata + r.max.y * Xsize + r.max.x;
  282. while(p < ep) {
  283. lp = p + width;
  284. while(p < lp)
  285. *wp++ = infernotox11[*p++];
  286. p += dx;
  287. wp += dx;
  288. }
  289. }
  290. static void
  291. copy8to24(Rectangle r)
  292. {
  293. int dx, width, v;
  294. uchar *p, *cp, *ep, *lp;
  295. width = Dx(r);
  296. dx = Xsize - width;
  297. p = gscreendata + r.min.y * Xsize + r.min.x;
  298. cp = xscreendata + (r.min.y * Xsize + r.min.x) * 3;
  299. ep = gscreendata + r.max.y * Xsize + r.max.x;
  300. while(p < ep) {
  301. lp = p + width;
  302. while(p < lp){
  303. v = infernotox11[*p++];
  304. cp[0] = (v>>16)&0xff;
  305. cp[1] = (v>>8)&0xff;
  306. cp[2] = (v>>0)&0xff;
  307. cp += 3;
  308. }
  309. p += dx;
  310. cp += 3*dx;
  311. }
  312. }
  313. static void
  314. copy8to16(Rectangle r)
  315. {
  316. int dx, width;
  317. uchar *p, *ep, *lp;
  318. u16int *sp;
  319. width = Dx(r);
  320. dx = Xsize - width;
  321. p = gscreendata + r.min.y * Xsize + r.min.x;
  322. sp = (unsigned short *)(xscreendata + (r.min.y * Xsize + r.min.x) * 2);
  323. ep = gscreendata + r.max.y * Xsize + r.max.x;
  324. while(p < ep) {
  325. lp = p + width;
  326. while(p < lp)
  327. *sp++ = infernotox11[*p++];
  328. p += dx;
  329. sp += dx;
  330. }
  331. }
  332. static void
  333. copy8to8(Rectangle r)
  334. {
  335. int dx, width;
  336. uchar *p, *cp, *ep, *lp;
  337. width = Dx(r);
  338. dx = Xsize - width;
  339. p = gscreendata + r.min.y * Xsize + r.min.x;
  340. cp = xscreendata + r.min.y * Xsize + r.min.x;
  341. ep = gscreendata + r.max.y * Xsize + r.max.x;
  342. while(p < ep) {
  343. lp = p + width;
  344. while(p < lp)
  345. *cp++ = infernotox11[*p++];
  346. p += dx;
  347. cp += dx;
  348. }
  349. }
  350. static void
  351. copy8topixel(Rectangle r)
  352. {
  353. int x, y;
  354. uchar *p;
  355. /* mainly for 4-bit greyscale */
  356. for (y = r.min.y; y < r.max.y; y++) {
  357. x = r.min.x;
  358. p = gscreendata + y * Xsize + x;
  359. while (x < r.max.x)
  360. XPutPixel(img, x++, y, infernotox11[*p++]);
  361. }
  362. }
  363. void
  364. flushmemscreen(Rectangle r)
  365. {
  366. char chanbuf[16];
  367. // Clip to screen
  368. if(r.min.x < 0)
  369. r.min.x = 0;
  370. if(r.min.y < 0)
  371. r.min.y = 0;
  372. if(r.max.x >= Xsize)
  373. r.max.x = Xsize - 1;
  374. if(r.max.y >= Ysize)
  375. r.max.y = Ysize - 1;
  376. if(r.max.x <= r.min.x || r.max.y <= r.min.y)
  377. return;
  378. switch(displaydepth){
  379. case 32:
  380. copy32to32(r);
  381. break;
  382. case 8:
  383. switch(xscreendepth){
  384. case 24:
  385. /* copy8to24(r); */ /* doesn't happen? */
  386. /* break */
  387. case 32:
  388. copy8to32(r);
  389. break;
  390. case 16:
  391. copy8to16(r);
  392. break;
  393. case 8:
  394. copy8to8(r);
  395. break;
  396. default:
  397. copy8topixel(r);
  398. break;
  399. }
  400. break;
  401. default:
  402. fprint(2, "emu: bad display depth %d chan %s xscreendepth %d\n", displaydepth,
  403. chantostr(chanbuf, displaychan), xscreendepth);
  404. cleanexit(0);
  405. }
  406. XLockDisplay(xdisplay);
  407. /* Display image on X11 */
  408. if(is_shm)
  409. XShmPutImage(xdisplay, xdrawable, xgc, img, r.min.x, r.min.y, r.min.x, r.min.y, Dx(r), Dy(r), 0);
  410. else
  411. XPutImage(xdisplay, xdrawable, xgc, img, r.min.x, r.min.y, r.min.x, r.min.y, Dx(r), Dy(r));
  412. XSync(xdisplay, 0);
  413. XUnlockDisplay(xdisplay);
  414. }
  415. static int
  416. revbyte(int b)
  417. {
  418. int r;
  419. r = 0;
  420. r |= (b&0x01) << 7;
  421. r |= (b&0x02) << 5;
  422. r |= (b&0x04) << 3;
  423. r |= (b&0x08) << 1;
  424. r |= (b&0x10) >> 1;
  425. r |= (b&0x20) >> 3;
  426. r |= (b&0x40) >> 5;
  427. r |= (b&0x80) >> 7;
  428. return r;
  429. }
  430. void
  431. setpointer(int x, int y)
  432. {
  433. drawqlock();
  434. XLockDisplay(xdisplay);
  435. XWarpPointer(xdisplay, None, xdrawable, 0, 0, 0, 0, x, y);
  436. XFlush(xdisplay);
  437. XUnlockDisplay(xdisplay);
  438. drawqunlock();
  439. }
  440. static void
  441. xkbdproc(void *arg)
  442. {
  443. XEvent event;
  444. XDisplay *xd;
  445. xd = arg;
  446. /* BEWARE: the value of up is not defined for this proc on some systems */
  447. XLockDisplay(xd); /* should be ours alone */
  448. XSelectInput(xd, xdrawable, KeyPressMask | KeyReleaseMask);
  449. for(;;){
  450. XNextEvent(xd, &event);
  451. xkeyboard(&event);
  452. xsetcursor(&event);
  453. }
  454. }
  455. static void
  456. xproc(void *arg)
  457. {
  458. ulong mask;
  459. XEvent event;
  460. XDisplay *xd;
  461. closepgrp(up->env->pgrp);
  462. closefgrp(up->env->fgrp);
  463. closeegrp(up->env->egrp);
  464. closesigs(up->env->sigs);
  465. xd = arg;
  466. mask = ButtonPressMask|
  467. ButtonReleaseMask|
  468. PointerMotionMask|
  469. Button1MotionMask|
  470. Button2MotionMask|
  471. Button3MotionMask|
  472. Button4MotionMask|
  473. Button5MotionMask|
  474. ExposureMask|
  475. StructureNotifyMask;
  476. XLockDisplay(xd); /* should be ours alone */
  477. XSelectInput(xd, xdrawable, mask);
  478. for(;;){
  479. XNextEvent(xd, &event);
  480. xselect(&event, xd);
  481. xmouse(&event);
  482. xexpose(&event);
  483. xdestroy(&event);
  484. }
  485. }
  486. /*
  487. * this crud is here because X11 can put huge amount of data
  488. * on the stack during keyboard translation and cursor changing(!).
  489. * we do both in a dedicated process with lots of stack, perhaps even enough.
  490. */
  491. enum {
  492. CursorSize= 32 /* biggest cursor size */
  493. };
  494. typedef struct ICursor ICursor;
  495. struct ICursor {
  496. int inuse;
  497. int modify;
  498. int hotx;
  499. int hoty;
  500. int w;
  501. int h;
  502. uchar src[(CursorSize/8)*CursorSize]; /* image and mask bitmaps */
  503. uchar mask[(CursorSize/8)*CursorSize];
  504. };
  505. static ICursor icursor;
  506. static void
  507. xcurslock(void)
  508. {
  509. while(_tas(&icursor.inuse) != 0)
  510. osyield();
  511. }
  512. static void
  513. xcursunlock(void)
  514. {
  515. coherence();
  516. icursor.inuse = 0;
  517. }
  518. static void
  519. xcursnotify(void)
  520. {
  521. XClientMessageEvent e;
  522. memset(&e, 0, sizeof e);
  523. e.type = ClientMessage;
  524. e.window = xdrawable;
  525. e.message_type = cursorchange;
  526. e.format = 8;
  527. XLockDisplay(xdisplay);
  528. XSendEvent(xdisplay, xdrawable, True, KeyPressMask, (XEvent*)&e);
  529. XFlush(xdisplay);
  530. XUnlockDisplay(xdisplay);
  531. }
  532. void
  533. drawcursor(Drawcursor* c)
  534. {
  535. uchar *bs, *bc, *ps, *pm;
  536. int i, j, w, h, bpl;
  537. if(c->data == nil){
  538. drawqlock();
  539. if(icursor.h != 0){
  540. xcurslock();
  541. icursor.h = 0;
  542. icursor.modify = 1;
  543. xcursunlock();
  544. }
  545. xcursnotify();
  546. drawqunlock();
  547. return;
  548. }
  549. drawqlock();
  550. xcurslock();
  551. icursor.modify = 0; /* xsetcursor will now ignore it */
  552. xcursunlock();
  553. h = (c->maxy-c->miny)/2; /* image, then mask */
  554. if(h > CursorSize)
  555. h = CursorSize;
  556. bpl = bytesperline(Rect(c->minx, c->miny, c->maxx, c->maxy), 1);
  557. w = bpl;
  558. if(w > CursorSize/8)
  559. w = CursorSize/8;
  560. ps = icursor.src;
  561. pm = icursor.mask;
  562. bc = c->data;
  563. bs = c->data + h*bpl;
  564. for(i = 0; i < h; i++){
  565. for(j = 0; j < bpl && j < w; j++) {
  566. *ps++ = revbyte(bs[j]);
  567. *pm++ = revbyte(bs[j] | bc[j]);
  568. }
  569. bs += bpl;
  570. bc += bpl;
  571. }
  572. icursor.h = h;
  573. icursor.w = w*8;
  574. icursor.hotx = c->hotx;
  575. icursor.hoty = c->hoty;
  576. icursor.modify = 1;
  577. xcursnotify();
  578. drawqunlock();
  579. }
  580. static void
  581. xsetcursor(XEvent *e)
  582. {
  583. ICursor ic;
  584. XCursor xc;
  585. XColor fg, bg;
  586. Pixmap xsrc, xmask;
  587. static XCursor xcursor;
  588. if(e->type != ClientMessage || !e->xclient.send_event || e->xclient.message_type != cursorchange)
  589. return;
  590. xcurslock();
  591. if(icursor.modify == 0){
  592. xcursunlock();
  593. return;
  594. }
  595. icursor.modify = 0;
  596. if(icursor.h == 0){
  597. xcursunlock();
  598. /* set the default system cursor */
  599. if(xcursor != 0) {
  600. XFreeCursor(xkbdcon, xcursor);
  601. xcursor = 0;
  602. }
  603. XUndefineCursor(xkbdcon, xdrawable);
  604. XFlush(xkbdcon);
  605. return;
  606. }
  607. ic = icursor;
  608. xcursunlock();
  609. xsrc = XCreateBitmapFromData(xkbdcon, xdrawable, (char*)ic.src, ic.w, ic.h);
  610. xmask = XCreateBitmapFromData(xkbdcon, xdrawable, (char*)ic.mask, ic.w, ic.h);
  611. fg = map[0];
  612. bg = map[255];
  613. fg.pixel = infernotox11[0];
  614. bg.pixel = infernotox11[255];
  615. xc = XCreatePixmapCursor(xkbdcon, xsrc, xmask, &fg, &bg, -ic.hotx, -ic.hoty);
  616. if(xc != 0) {
  617. XDefineCursor(xkbdcon, xdrawable, xc);
  618. if(xcursor != 0)
  619. XFreeCursor(xkbdcon, xcursor);
  620. xcursor = xc;
  621. }
  622. XFreePixmap(xkbdcon, xsrc);
  623. XFreePixmap(xkbdcon, xmask);
  624. XFlush(xkbdcon);
  625. }
  626. typedef struct Mg Mg;
  627. struct Mg
  628. {
  629. int code;
  630. int bit;
  631. int len;
  632. ulong mask;
  633. };
  634. static int
  635. maskx(Mg* g, int code, ulong mask)
  636. {
  637. int i;
  638. for(i=0; i<32; i++)
  639. if(mask & (1<<i))
  640. break;
  641. if(i == 32)
  642. return 0;
  643. g->code = code;
  644. g->bit = i;
  645. g->mask = mask;
  646. for(g->len = 0; i<32 && (mask & (1<<i))!=0; i++)
  647. g->len++;
  648. return 1;
  649. }
  650. /*
  651. * for a given depth, we need to check the available formats
  652. * to find how many actual bits are used per pixel.
  653. */
  654. static int
  655. xactualdepth(int screenno, int depth)
  656. {
  657. XPixmapFormatValues *pfmt;
  658. int i, n;
  659. pfmt = XListPixmapFormats(xdisplay, &n);
  660. for(i=0; i<n; i++)
  661. if(pfmt[i].depth == depth)
  662. return pfmt[i].bits_per_pixel;
  663. return -1;
  664. }
  665. static int
  666. xtruevisual(int screenno, int reqdepth, XVisualInfo *vi, ulong *chan)
  667. {
  668. XVisual *xv;
  669. Mg r, g, b;
  670. int pad, d;
  671. ulong c;
  672. char buf[30];
  673. if(XMatchVisualInfo(xdisplay, screenno, reqdepth, TrueColor, vi) ||
  674. XMatchVisualInfo(xdisplay, screenno, reqdepth, DirectColor, vi)){
  675. xv = vi->visual;
  676. if(maskx(&r, CRed, xv->red_mask) &&
  677. maskx(&g, CGreen, xv->green_mask) &&
  678. maskx(&b, CBlue, xv->blue_mask)){
  679. d = xactualdepth(screenno, reqdepth);
  680. if(d < 0)
  681. return 0;
  682. pad = d - (r.len + g.len + b.len);
  683. if(0){
  684. fprint(2, "r: %8.8lux %d %d\ng: %8.8lux %d %d\nb: %8.8lux %d %d\n",
  685. xv->red_mask, r.bit, r.len, xv->green_mask, g.bit, g.len, xv->blue_mask, b.bit, b.len);
  686. }
  687. if(r.bit > b.bit)
  688. c = CHAN3(CRed, r.len, CGreen, g.len, CBlue, b.len);
  689. else
  690. c = CHAN3(CBlue, b.len, CGreen, g.len, CRed, r.len);
  691. if(pad > 0)
  692. c |= CHAN1(CIgnore, pad) << 24;
  693. *chan = c;
  694. xscreendepth = reqdepth;
  695. if(0)
  696. fprint(2, "chan=%s reqdepth=%d bits=%d\n", chantostr(buf, c), reqdepth, d);
  697. return 1;
  698. }
  699. }
  700. return 0;
  701. }
  702. static int
  703. xmapvisual(int screenno, XVisualInfo *vi, ulong *chan)
  704. {
  705. if(XMatchVisualInfo(xdisplay, screenno, 8, PseudoColor, vi) ||
  706. XMatchVisualInfo(xdisplay, screenno, 8, StaticColor, vi)){
  707. *chan = CMAP8;
  708. xscreendepth = 8;
  709. return 1;
  710. }
  711. return 0;
  712. }
  713. static void
  714. xinitscreen(int xsize, int ysize, ulong reqchan, ulong *chan, int *d)
  715. {
  716. char *argv[2];
  717. char *dispname;
  718. XWindow rootwin;
  719. XWMHints hints;
  720. XVisualInfo xvi;
  721. XScreen *screen;
  722. int rootscreennum;
  723. XTextProperty name;
  724. XClassHint classhints;
  725. XSizeHints normalhints;
  726. XSetWindowAttributes attrs;
  727. char buf[30];
  728. int i;
  729. xdrawable = 0;
  730. dispname = getenv("DISPLAY");
  731. if(dispname == nil)
  732. dispname = "not set";
  733. XInitThreads();
  734. xdisplay = XOpenDisplay(NULL);
  735. if(xdisplay == 0){
  736. fprint(2, "emu: win-x11 open %r, DISPLAY is %s\n", dispname);
  737. cleanexit(0);
  738. }
  739. rootscreennum = DefaultScreen(xdisplay);
  740. rootwin = DefaultRootWindow(xdisplay);
  741. xscreendepth = DefaultDepth(xdisplay, rootscreennum);
  742. xvis = DefaultVisual(xdisplay, rootscreennum);
  743. screen = DefaultScreenOfDisplay(xdisplay);
  744. xcmap = DefaultColormapOfScreen(screen);
  745. if(reqchan == 0){
  746. *chan = 0;
  747. if(xscreendepth <= 16){ /* try for better colour */
  748. xtruevisual(rootscreennum, 16, &xvi, chan) ||
  749. xtruevisual(rootscreennum, 15, &xvi, chan) ||
  750. xtruevisual(rootscreennum, 24, &xvi, chan) ||
  751. xmapvisual(rootscreennum, &xvi, chan);
  752. }else{
  753. xtruevisual(rootscreennum, xscreendepth, &xvi, chan) ||
  754. xtruevisual(rootscreennum, 24, &xvi, chan);
  755. }
  756. if(*chan == 0){
  757. fprint(2, "emu: could not find suitable x11 pixel format for depth %d on this display\n", xscreendepth);
  758. cleanexit(0);
  759. }
  760. reqchan = *chan;
  761. *d = chantodepth(reqchan);
  762. xvis = xvi.visual;
  763. }else{
  764. *chan = reqchan; /* not every channel description will work */
  765. *d = chantodepth(reqchan);
  766. if(*d != xactualdepth(rootscreennum, *d)){
  767. fprint(2, "emu: current x11 display configuration does not support %s (depth %d) directly\n",
  768. chantostr(buf, reqchan), *d);
  769. cleanexit(0);
  770. }
  771. }
  772. if(xvis->class != StaticColor) {
  773. if(TYPE(*chan) == CGrey)
  774. graphicsgmap(map, NBITS(reqchan));
  775. else{
  776. graphicscmap(map);
  777. graphicsrgbmap(mapr, mapg, mapb);
  778. }
  779. initxcmap(rootwin);
  780. }
  781. memset(&attrs, 0, sizeof(attrs));
  782. attrs.colormap = xcmap;
  783. attrs.background_pixel = 0;
  784. attrs.border_pixel = 0;
  785. /* attrs.override_redirect = 1;*/ /* WM leave me alone! |CWOverrideRedirect */
  786. xdrawable = XCreateWindow(xdisplay, rootwin, 0, 0, xsize, ysize, 0, xscreendepth,
  787. InputOutput, xvis, CWBackPixel|CWBorderPixel|CWColormap, &attrs);
  788. /*
  789. * set up property as required by ICCCM
  790. */
  791. memset(&name, 0, sizeof(name));
  792. name.value = (uchar*)"inferno";
  793. name.encoding = XA_STRING;
  794. name.format = 8;
  795. name.nitems = strlen((char*)name.value);
  796. memset(&normalhints, 0, sizeof(normalhints));
  797. normalhints.flags = USSize|PMaxSize;
  798. normalhints.max_width = normalhints.width = xsize;
  799. normalhints.max_height = normalhints.height = ysize;
  800. hints.flags = InputHint|StateHint;
  801. hints.input = 1;
  802. hints.initial_state = NormalState;
  803. memset(&classhints, 0, sizeof(classhints));
  804. classhints.res_name = "inferno";
  805. classhints.res_class = "Inferno";
  806. argv[0] = "inferno";
  807. argv[1] = nil;
  808. XSetWMProperties(xdisplay, xdrawable,
  809. &name, /* XA_WM_NAME property for ICCCM */
  810. &name, /* XA_WM_ICON_NAME */
  811. argv, /* XA_WM_COMMAND */
  812. 1, /* argc */
  813. &normalhints, /* XA_WM_NORMAL_HINTS */
  814. &hints, /* XA_WM_HINTS */
  815. &classhints); /* XA_WM_CLASS */
  816. XMapWindow(xdisplay, xdrawable);
  817. XFlush(xdisplay);
  818. xgc = creategc(xdrawable);
  819. xmcon = XOpenDisplay(NULL);
  820. xsnarfcon = XOpenDisplay(NULL);
  821. xkbdcon = XOpenDisplay(NULL);
  822. if(xmcon == 0 || xsnarfcon == 0 || xkbdcon == 0){
  823. fprint(2, "emu: win-x11 open %r, DISPLAY is %s\n", dispname);
  824. cleanexit(0);
  825. }
  826. clipboard = XInternAtom(xmcon, "CLIPBOARD", False);
  827. utf8string = XInternAtom(xmcon, "UTF8_STRING", False);
  828. targets = XInternAtom(xmcon, "TARGETS", False);
  829. text = XInternAtom(xmcon, "TEXT", False);
  830. compoundtext = XInternAtom(xmcon, "COMPOUND_TEXT", False);
  831. cursorchange = XInternAtom(xkbdcon, "TheCursorHasChanged", False);
  832. }
  833. static void
  834. graphicsgmap(XColor *map, int d)
  835. {
  836. int i, j, s, m, p;
  837. s = 8-d;
  838. m = 1;
  839. while(--d >= 0)
  840. m *= 2;
  841. m = 255/(m-1);
  842. for(i=0; i < 256; i++){
  843. j = (i>>s)*m;
  844. p = 255-i;
  845. map[p].red = map[p].green = map[p].blue = (255-j)*0x0101;
  846. map[p].pixel = p;
  847. map[p].flags = DoRed|DoGreen|DoBlue;
  848. }
  849. }
  850. static void
  851. graphicscmap(XColor *map)
  852. {
  853. int r, g, b, cr, cg, cb, v, num, den, idx, v7, idx7;
  854. for(r=0; r!=4; r++) {
  855. for(g = 0; g != 4; g++) {
  856. for(b = 0; b!=4; b++) {
  857. for(v = 0; v!=4; v++) {
  858. den=r;
  859. if(g > den)
  860. den=g;
  861. if(b > den)
  862. den=b;
  863. /* divide check -- pick grey shades */
  864. if(den==0)
  865. cr=cg=cb=v*17;
  866. else {
  867. num=17*(4*den+v);
  868. cr=r*num/den;
  869. cg=g*num/den;
  870. cb=b*num/den;
  871. }
  872. idx = r*64 + v*16 + ((g*4 + b + v - r) & 15);
  873. /* was idx = 255 - idx; */
  874. map[idx].red = cr*0x0101;
  875. map[idx].green = cg*0x0101;
  876. map[idx].blue = cb*0x0101;
  877. map[idx].pixel = idx;
  878. map[idx].flags = DoRed|DoGreen|DoBlue;
  879. v7 = v >> 1;
  880. idx7 = r*32 + v7*16 + g*4 + b;
  881. if((v & 1) == v7){
  882. map7to8[idx7][0] = idx;
  883. if(den == 0) { /* divide check -- pick grey shades */
  884. cr = ((255.0/7.0)*v7)+0.5;
  885. cg = cr;
  886. cb = cr;
  887. }
  888. else {
  889. num=17*15*(4*den+v7*2)/14;
  890. cr=r*num/den;
  891. cg=g*num/den;
  892. cb=b*num/den;
  893. }
  894. map7[idx7].red = cr*0x0101;
  895. map7[idx7].green = cg*0x0101;
  896. map7[idx7].blue = cb*0x0101;
  897. map7[idx7].pixel = idx7;
  898. map7[idx7].flags = DoRed|DoGreen|DoBlue;
  899. }
  900. else
  901. map7to8[idx7][1] = idx;
  902. }
  903. }
  904. }
  905. }
  906. }
  907. static void
  908. graphicsrgbmap(XColor *mapr, XColor *mapg, XColor *mapb)
  909. {
  910. int i;
  911. memset(mapr, 0, 256*sizeof(XColor));
  912. memset(mapg, 0, 256*sizeof(XColor));
  913. memset(mapb, 0, 256*sizeof(XColor));
  914. for(i=0; i < 256; i++){
  915. mapr[i].red = mapg[i].green = mapb[i].blue = i*0x0101;
  916. mapr[i].pixel = mapg[i].pixel = mapb[i].pixel = i;
  917. mapr[i].flags = mapg[i].flags = mapb[i].flags = DoRed|DoGreen|DoBlue;
  918. }
  919. }
  920. /*
  921. * Initialize and install the Inferno colormap as a private colormap for this
  922. * application. Inferno gets the best colors here when it has the cursor focus.
  923. */
  924. static void
  925. initxcmap(XWindow w)
  926. {
  927. XColor c;
  928. int i;
  929. if(xscreendepth <= 1)
  930. return;
  931. switch(xvis->class){
  932. case TrueColor:
  933. case DirectColor:
  934. for(i = 0; i < 256; i++) {
  935. c = map[i];
  936. /* find index into colormap for our RGB */
  937. if(!XAllocColor(xdisplay, xcmap, &c)) {
  938. fprint(2, "emu: win-x11 can't alloc color\n");
  939. cleanexit(0);
  940. }
  941. infernotox11[map[i].pixel] = c.pixel;
  942. if(xscreendepth >= 24){
  943. c = mapr[i];
  944. XAllocColor(xdisplay, xcmap, &c);
  945. infernortox11[i] = (c.pixel>>16)&0xff;
  946. c = mapg[i];
  947. XAllocColor(xdisplay, xcmap, &c);
  948. infernogtox11[i] = (c.pixel>>8)&0xff;
  949. c = mapb[i];
  950. XAllocColor(xdisplay, xcmap, &c);
  951. infernobtox11[i] = (c.pixel>>0)&0xff;
  952. }
  953. }
  954. if(0){int i, j; for(i=0;i<256; i+=16){print("%3d", i); for(j=i; j<i+16; j++)print(" %2.2ux/%2.2ux/%2.2ux", infernortox11[j], infernogtox11[j],infernobtox11[j]); print("\n");}}
  955. /* TO DO: if the map(s) used give the identity map, don't use the map during copy */
  956. break;
  957. case PseudoColor:
  958. if(xtblbit == 0){
  959. xcmap = XCreateColormap(xdisplay, w, xvis, AllocAll);
  960. XStoreColors(xdisplay, xcmap, map, 256);
  961. for(i = 0; i < 256; i++)
  962. infernotox11[i] = i;
  963. /* TO DO: the map is the identity, so don't need the map in copy */
  964. } else {
  965. for(i = 0; i < 128; i++) {
  966. c = map7[i];
  967. if(!XAllocColor(xdisplay, xcmap, &c)) {
  968. fprint(2, "emu: win-x11 can't alloc colors in default map, don't use -7\n");
  969. cleanexit(0);
  970. }
  971. infernotox11[map7to8[i][0]] = c.pixel;
  972. infernotox11[map7to8[i][1]] = c.pixel;
  973. }
  974. }
  975. break;
  976. default:
  977. xtblbit = 0;
  978. fprint(2, "emu: win-x11 unsupported visual class %d\n", xvis->class);
  979. break;
  980. }
  981. }
  982. static void
  983. xdestroy(XEvent *e)
  984. {
  985. XDestroyWindowEvent *xe;
  986. if(e->type != DestroyNotify)
  987. return;
  988. xe = (XDestroyWindowEvent*)e;
  989. if(xe->window == xdrawable)
  990. cleanexit(0);
  991. }
  992. /*
  993. * Disable generation of GraphicsExpose/NoExpose events in the XGC.
  994. */
  995. static XGC
  996. creategc(XDrawable d)
  997. {
  998. XGCValues gcv;
  999. gcv.function = GXcopy;
  1000. gcv.graphics_exposures = False;
  1001. return XCreateGC(xdisplay, d, GCFunction|GCGraphicsExposures, &gcv);
  1002. }
  1003. static void
  1004. xexpose(XEvent *e)
  1005. {
  1006. Rectangle r;
  1007. XExposeEvent *xe;
  1008. if(e->type != Expose)
  1009. return;
  1010. xe = (XExposeEvent*)e;
  1011. r.min.x = xe->x;
  1012. r.min.y = xe->y;
  1013. r.max.x = xe->x + xe->width;
  1014. r.max.y = xe->y + xe->height;
  1015. drawqlock();
  1016. flushmemscreen(r);
  1017. drawqunlock();
  1018. }
  1019. static void
  1020. xkeyboard(XEvent *e)
  1021. {
  1022. int ind, md;
  1023. KeySym k;
  1024. if(gkscanq != nil && (e->type == KeyPress || e->type == KeyRelease)){
  1025. uchar ch = e->xkey.keycode;
  1026. if(e->xany.type == KeyRelease)
  1027. ch |= 0x80;
  1028. qproduce(gkscanq, &ch, 1);
  1029. return;
  1030. }
  1031. /*
  1032. * I tried using XtGetActionKeysym, but it didn't seem to
  1033. * do case conversion properly
  1034. * (at least, with Xterminal servers and R4 intrinsics)
  1035. */
  1036. if(e->xany.type != KeyPress)
  1037. return;
  1038. md = e->xkey.state;
  1039. ind = 0;
  1040. if(md & ShiftMask)
  1041. ind = 1;
  1042. if(0){
  1043. k = XKeycodeToKeysym(e->xany.display, (KeyCode)e->xkey.keycode, ind);
  1044. /* May have to try unshifted version */
  1045. if(k == NoSymbol && ind == 1)
  1046. k = XKeycodeToKeysym(e->xany.display, (KeyCode)e->xkey.keycode, 0);
  1047. }else
  1048. XLookupString((XKeyEvent*)e, NULL, 0, &k, NULL);
  1049. if(k == XK_Multi_key || k == NoSymbol)
  1050. return;
  1051. if(k&0xFF00){
  1052. switch(k){
  1053. case XK_BackSpace:
  1054. case XK_Tab:
  1055. case XK_Escape:
  1056. case XK_Delete:
  1057. case XK_KP_0:
  1058. case XK_KP_1:
  1059. case XK_KP_2:
  1060. case XK_KP_3:
  1061. case XK_KP_4:
  1062. case XK_KP_5:
  1063. case XK_KP_6:
  1064. case XK_KP_7:
  1065. case XK_KP_8:
  1066. case XK_KP_9:
  1067. case XK_KP_Divide:
  1068. case XK_KP_Multiply:
  1069. case XK_KP_Subtract:
  1070. case XK_KP_Add:
  1071. case XK_KP_Decimal:
  1072. k &= 0x7F;
  1073. break;
  1074. case XK_Linefeed:
  1075. k = '\r';
  1076. break;
  1077. case XK_KP_Space:
  1078. k = ' ';
  1079. break;
  1080. case XK_Home:
  1081. case XK_KP_Home:
  1082. k = Home;
  1083. break;
  1084. case XK_Left:
  1085. case XK_KP_Left:
  1086. k = Left;
  1087. break;
  1088. case XK_Up:
  1089. case XK_KP_Up:
  1090. k = Up;
  1091. break;
  1092. case XK_Down:
  1093. case XK_KP_Down:
  1094. k = Down;
  1095. break;
  1096. case XK_Right:
  1097. case XK_KP_Right:
  1098. k = Right;
  1099. break;
  1100. case XK_Page_Down:
  1101. case XK_KP_Page_Down:
  1102. k = Pgdown;
  1103. break;
  1104. case XK_End:
  1105. case XK_KP_End:
  1106. k = End;
  1107. break;
  1108. case XK_Page_Up:
  1109. case XK_KP_Page_Up:
  1110. k = Pgup;
  1111. break;
  1112. case XK_Insert:
  1113. case XK_KP_Insert:
  1114. k = Ins;
  1115. break;
  1116. case XK_KP_Enter:
  1117. case XK_Return:
  1118. k = '\n';
  1119. break;
  1120. case XK_Alt_L:
  1121. case XK_Alt_R:
  1122. k = Latin;
  1123. break;
  1124. case XK_Shift_L:
  1125. case XK_Shift_R:
  1126. case XK_Control_L:
  1127. case XK_Control_R:
  1128. case XK_Caps_Lock:
  1129. case XK_Shift_Lock:
  1130. case XK_Meta_L:
  1131. case XK_Meta_R:
  1132. case XK_Super_L:
  1133. case XK_Super_R:
  1134. case XK_Hyper_L:
  1135. case XK_Hyper_R:
  1136. return;
  1137. default: /* not ISO-1 or tty control */
  1138. if(k>0xff){
  1139. k = keysym2ucs(k); /* supplied by X */
  1140. if(k == -1)
  1141. return;
  1142. }
  1143. break;
  1144. }
  1145. }
  1146. /* Compensate for servers that call a minus a hyphen */
  1147. if(k == XK_hyphen)
  1148. k = XK_minus;
  1149. /* Do control mapping ourselves if translator doesn't */
  1150. if(md & ControlMask)
  1151. k &= 0x9f;
  1152. if(0){
  1153. if(k == '\t' && ind)
  1154. k = BackTab;
  1155. if(md & Mod1Mask)
  1156. k = APP|(k&0xff);
  1157. }
  1158. if(k == NoSymbol)
  1159. return;
  1160. gkbdputc(gkbdq, k);
  1161. }
  1162. static void
  1163. xmouse(XEvent *e)
  1164. {
  1165. int s, dbl;
  1166. XButtonEvent *be;
  1167. XMotionEvent *me;
  1168. XEvent motion;
  1169. int x, y, b;
  1170. static ulong lastb, lastt;
  1171. if(putsnarf != assertsnarf){
  1172. assertsnarf = putsnarf;
  1173. XSetSelectionOwner(xmcon, XA_PRIMARY, xdrawable, CurrentTime);
  1174. if(clipboard != None)
  1175. XSetSelectionOwner(xmcon, clipboard, xdrawable, CurrentTime);
  1176. XFlush(xmcon);
  1177. }
  1178. dbl = 0;
  1179. switch(e->type){
  1180. case ButtonPress:
  1181. be = (XButtonEvent *)e;
  1182. /*
  1183. * Fake message, just sent to make us announce snarf.
  1184. * Apparently state and button are 16 and 8 bits on
  1185. * the wire, since they are truncated by the time they
  1186. * get to us.
  1187. */
  1188. if(be->send_event
  1189. && (~be->state&0xFFFF)==0
  1190. && (~be->button&0xFF)==0)
  1191. return;
  1192. x = be->x;
  1193. y = be->y;
  1194. s = be->state;
  1195. if(be->button == lastb && be->time - lastt < DblTime)
  1196. dbl = 1;
  1197. lastb = be->button;
  1198. lastt = be->time;
  1199. switch(be->button){
  1200. case 1:
  1201. s |= Button1Mask;
  1202. break;
  1203. case 2:
  1204. s |= Button2Mask;
  1205. break;
  1206. case 3:
  1207. s |= Button3Mask;
  1208. break;
  1209. case 4:
  1210. s |= Button4Mask;
  1211. break;
  1212. case 5:
  1213. s |= Button5Mask;
  1214. break;
  1215. }
  1216. break;
  1217. case ButtonRelease:
  1218. be = (XButtonEvent *)e;
  1219. x = be->x;
  1220. y = be->y;
  1221. s = be->state;
  1222. switch(be->button){
  1223. case 1:
  1224. s &= ~Button1Mask;
  1225. break;
  1226. case 2:
  1227. s &= ~Button2Mask;
  1228. break;
  1229. case 3:
  1230. s &= ~Button3Mask;
  1231. break;
  1232. case 4:
  1233. s &= ~Button4Mask;
  1234. break;
  1235. case 5:
  1236. s &= ~Button5Mask;
  1237. break;
  1238. }
  1239. break;
  1240. case MotionNotify:
  1241. me = (XMotionEvent *) e;
  1242. /* remove excess MotionNotify events from queue and keep last one */
  1243. while(XCheckTypedWindowEvent(xmcon, xdrawable, MotionNotify, &motion) == True)
  1244. me = (XMotionEvent *) &motion;
  1245. s = me->state;
  1246. x = me->x;
  1247. y = me->y;
  1248. break;
  1249. default:
  1250. return;
  1251. }
  1252. b = 0;
  1253. if(s & Button1Mask)
  1254. b |= 1;
  1255. if(s & Button2Mask)
  1256. b |= 2;
  1257. if(s & Button3Mask)
  1258. b |= 4;
  1259. if(s & Button4Mask)
  1260. b |= 8;
  1261. if(s & Button5Mask)
  1262. b |= 16;
  1263. if(dbl)
  1264. b |= 1<<8;
  1265. mousetrack(b, x, y, 0);
  1266. }
  1267. #include "x11-keysym2ucs.c"
  1268. /*
  1269. * Cut and paste. Just couldn't stand to make this simple...
  1270. */
  1271. enum{
  1272. SnarfSize= 100*1024
  1273. };
  1274. typedef struct Clip Clip;
  1275. struct Clip
  1276. {
  1277. char buf[SnarfSize];
  1278. QLock lk;
  1279. };
  1280. Clip clip;
  1281. #undef long /* sic */
  1282. #undef ulong
  1283. static char*
  1284. _xgetsnarf(XDisplay *xd)
  1285. {
  1286. uchar *data, *xdata;
  1287. Atom clipboard, type, prop;
  1288. unsigned long len, lastlen, dummy;
  1289. int fmt, i;
  1290. XWindow w;
  1291. qlock(&clip.lk);
  1292. /*
  1293. * Have we snarfed recently and the X server hasn't caught up?
  1294. */
  1295. if(putsnarf != assertsnarf)
  1296. goto mine;
  1297. /*
  1298. * Is there a primary selection (highlighted text in an xterm)?
  1299. */
  1300. clipboard = XA_PRIMARY;
  1301. w = XGetSelectionOwner(xd, XA_PRIMARY);
  1302. if(w == xdrawable){
  1303. mine:
  1304. data = (uchar*)strdup(clip.buf);
  1305. goto out;
  1306. }
  1307. /*
  1308. * If not, is there a clipboard selection?
  1309. */
  1310. if(w == None && clipboard != None){
  1311. clipboard = clipboard;
  1312. w = XGetSelectionOwner(xd, clipboard);
  1313. if(w == xdrawable)
  1314. goto mine;
  1315. }
  1316. /*
  1317. * If not, give up.
  1318. */
  1319. if(w == None){
  1320. data = nil;
  1321. goto out;
  1322. }
  1323. /*
  1324. * We should be waiting for SelectionNotify here, but it might never
  1325. * come, and we have no way to time out. Instead, we will clear
  1326. * local property #1, request our buddy to fill it in for us, and poll
  1327. * until he's done or we get tired of waiting.
  1328. *
  1329. * We should try to go for utf8string instead of XA_STRING,
  1330. * but that would add to the polling.
  1331. */
  1332. prop = 1;
  1333. XChangeProperty(xd, xdrawable, prop, XA_STRING, 8, PropModeReplace, (uchar*)"", 0);
  1334. XConvertSelection(xd, clipboard, XA_STRING, prop, xdrawable, CurrentTime);
  1335. XFlush(xd);
  1336. lastlen = 0;
  1337. for(i=0; i<10 || (lastlen!=0 && i<30); i++){
  1338. osmillisleep(100);
  1339. XGetWindowProperty(xd, xdrawable, prop, 0, 0, 0, AnyPropertyType,
  1340. &type, &fmt, &dummy, &len, &data);
  1341. if(lastlen == len && len > 0)
  1342. break;
  1343. lastlen = len;
  1344. }
  1345. if(i == 10){
  1346. data = nil;
  1347. goto out;
  1348. }
  1349. /* get the property */
  1350. data = nil;
  1351. XGetWindowProperty(xd, xdrawable, prop, 0, SnarfSize/sizeof(unsigned long), 0,
  1352. AnyPropertyType, &type, &fmt, &len, &dummy, &xdata);
  1353. if((type != XA_STRING && type != utf8string) || len == 0){
  1354. if(xdata)
  1355. XFree(xdata);
  1356. data = nil;
  1357. }else{
  1358. if(xdata){
  1359. data = (uchar*)strdup((char*)xdata);
  1360. XFree(xdata);
  1361. }else
  1362. data = nil;
  1363. }
  1364. out:
  1365. qunlock(&clip.lk);
  1366. return (char*)data;
  1367. }
  1368. static void
  1369. _xputsnarf(XDisplay *xd, char *data)
  1370. {
  1371. XButtonEvent e;
  1372. if(strlen(data) >= SnarfSize)
  1373. return;
  1374. qlock(&clip.lk);
  1375. strcpy(clip.buf, data);
  1376. /* leave note for mouse proc to assert selection ownership */
  1377. putsnarf++;
  1378. /* send mouse a fake event so snarf is announced */
  1379. memset(&e, 0, sizeof e);
  1380. e.type = ButtonPress;
  1381. e.window = xdrawable;
  1382. e.state = ~0;
  1383. e.button = ~0;
  1384. XSendEvent(xd, xdrawable, True, ButtonPressMask, (XEvent*)&e);
  1385. XFlush(xd);
  1386. qunlock(&clip.lk);
  1387. }
  1388. static void
  1389. xselect(XEvent *e, XDisplay *xd)
  1390. {
  1391. char *name;
  1392. XEvent r;
  1393. XSelectionRequestEvent *xe;
  1394. Atom a[4];
  1395. if(e->xany.type != SelectionRequest)
  1396. return;
  1397. memset(&r, 0, sizeof r);
  1398. xe = (XSelectionRequestEvent*)e;
  1399. if(0) iprint("xselect target=%d requestor=%d property=%d selection=%d\n",
  1400. xe->target, xe->requestor, xe->property, xe->selection);
  1401. r.xselection.property = xe->property;
  1402. if(xe->target == targets){
  1403. a[0] = XA_STRING;
  1404. a[1] = utf8string;
  1405. a[2] = text;
  1406. a[3] = compoundtext;
  1407. XChangeProperty(xd, xe->requestor, xe->property, xe->target,
  1408. 8, PropModeReplace, (uchar*)a, sizeof a);
  1409. }else if(xe->target == XA_STRING || xe->target == utf8string || xe->target == text || xe->target == compoundtext){
  1410. /* if the target is STRING we're supposed to reply with Latin1 XXX */
  1411. qlock(&clip.lk);
  1412. XChangeProperty(xd, xe->requestor, xe->property, xe->target,
  1413. 8, PropModeReplace, (uchar*)clip.buf, strlen(clip.buf));
  1414. qunlock(&clip.lk);
  1415. }else{
  1416. iprint("get %d\n", xe->target);
  1417. name = XGetAtomName(xd, xe->target);
  1418. if(name == nil)
  1419. iprint("XGetAtomName failed\n");
  1420. else if(strcmp(name, "TIMESTAMP") != 0)
  1421. iprint("%s: cannot handle selection request for '%s' (%d)\n", argv0, name, (int)xe->target);
  1422. r.xselection.property = None;
  1423. }
  1424. r.xselection.display = xe->display;
  1425. /* r.xselection.property filled above */
  1426. r.xselection.target = xe->target;
  1427. r.xselection.type = SelectionNotify;
  1428. r.xselection.requestor = xe->requestor;
  1429. r.xselection.time = xe->time;
  1430. r.xselection.send_event = True;
  1431. r.xselection.selection = xe->selection;
  1432. XSendEvent(xd, xe->requestor, False, 0, &r);
  1433. XFlush(xd);
  1434. }
  1435. char*
  1436. clipread(void)
  1437. {
  1438. char *p;
  1439. if(xsnarfcon == nil)
  1440. return nil;
  1441. XLockDisplay(xsnarfcon);
  1442. p = _xgetsnarf(xsnarfcon);
  1443. XUnlockDisplay(xsnarfcon);
  1444. return p;
  1445. }
  1446. int
  1447. clipwrite(char *buf)
  1448. {
  1449. if(xsnarfcon == nil)
  1450. return 0;
  1451. XLockDisplay(xsnarfcon);
  1452. _xputsnarf(xsnarfcon, buf);
  1453. XUnlockDisplay(xsnarfcon);
  1454. return 0;
  1455. }