You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TXWindow.cxx 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. //
  19. // TXWindow.cxx
  20. //
  21. #include <X11/Xatom.h>
  22. #include "TXWindow.h"
  23. #include <list>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <rfb/util.h>
  27. std::list<TXWindow*> windows;
  28. Atom wmProtocols, wmDeleteWindow, wmTakeFocus;
  29. Atom xaTIMESTAMP, xaTARGETS, xaSELECTION_TIME, xaSELECTION_STRING;
  30. Atom xaCLIPBOARD;
  31. unsigned long TXWindow::black, TXWindow::white;
  32. unsigned long TXWindow::defaultFg, TXWindow::defaultBg;
  33. unsigned long TXWindow::lightBg, TXWindow::darkBg;
  34. unsigned long TXWindow::disabledFg, TXWindow::disabledBg;
  35. unsigned long TXWindow::enabledBg;
  36. unsigned long TXWindow::scrollbarBg;
  37. Colormap TXWindow::cmap = 0;
  38. GC TXWindow::defaultGC = 0;
  39. Font TXWindow::defaultFont = 0;
  40. XFontStruct* TXWindow::defaultFS = 0;
  41. Time TXWindow::cutBufferTime = 0;
  42. Pixmap TXWindow::dot = 0, TXWindow::tick = 0;
  43. const int TXWindow::dotSize = 4, TXWindow::tickSize = 8;
  44. char* TXWindow::defaultWindowClass;
  45. TXGlobalEventHandler* TXWindow::globalEventHandler = NULL;
  46. void TXWindow::init(Display* dpy, const char* defaultWindowClass_)
  47. {
  48. cmap = DefaultColormap(dpy,DefaultScreen(dpy));
  49. wmProtocols = XInternAtom(dpy, "WM_PROTOCOLS", False);
  50. wmDeleteWindow = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  51. wmTakeFocus = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
  52. xaTIMESTAMP = XInternAtom(dpy, "TIMESTAMP", False);
  53. xaTARGETS = XInternAtom(dpy, "TARGETS", False);
  54. xaSELECTION_TIME = XInternAtom(dpy, "SELECTION_TIME", False);
  55. xaSELECTION_STRING = XInternAtom(dpy, "SELECTION_STRING", False);
  56. xaCLIPBOARD = XInternAtom(dpy, "CLIPBOARD", False);
  57. XColor cols[6];
  58. cols[0].red = cols[0].green = cols[0].blue = 0x0000;
  59. cols[1].red = cols[1].green = cols[1].blue = 0xbbbb;
  60. cols[2].red = cols[2].green = cols[2].blue = 0xeeee;
  61. cols[3].red = cols[3].green = cols[3].blue = 0x5555;
  62. cols[4].red = cols[4].green = cols[4].blue = 0x8888;
  63. cols[5].red = cols[5].green = cols[5].blue = 0xffff;
  64. getColours(dpy, cols, 6);
  65. black = defaultFg = cols[0].pixel;
  66. defaultBg = disabledBg = cols[1].pixel;
  67. lightBg = cols[2].pixel;
  68. darkBg = disabledFg = cols[3].pixel;
  69. scrollbarBg = cols[4].pixel;
  70. white = enabledBg = cols[5].pixel;
  71. defaultGC = XCreateGC(dpy, DefaultRootWindow(dpy), 0, 0);
  72. defaultFS
  73. = XLoadQueryFont(dpy, "-*-helvetica-medium-r-*-*-12-*-*-*-*-*-*-*");
  74. if (!defaultFS) {
  75. defaultFS = XLoadQueryFont(dpy, "fixed");
  76. if (!defaultFS) {
  77. fprintf(stderr,"Failed to load any font\n");
  78. exit(1);
  79. }
  80. }
  81. defaultFont = defaultFS->fid;
  82. XSetForeground(dpy, defaultGC, defaultFg);
  83. XSetBackground(dpy, defaultGC, defaultBg);
  84. XSetFont(dpy, defaultGC, defaultFont);
  85. XSelectInput(dpy, DefaultRootWindow(dpy), PropertyChangeMask);
  86. static unsigned char dotBits[] = { 0x06, 0x0f, 0x0f, 0x06};
  87. dot = XCreateBitmapFromData(dpy, DefaultRootWindow(dpy), (char*)dotBits,
  88. dotSize, dotSize);
  89. static unsigned char tickBits[] = { 0x80, 0xc0, 0xe2, 0x76, 0x3e, 0x1c, 0x08, 0x00};
  90. tick = XCreateBitmapFromData(dpy, DefaultRootWindow(dpy), (char*)tickBits,
  91. tickSize, tickSize);
  92. defaultWindowClass = rfb::strDup(defaultWindowClass_);
  93. }
  94. void TXWindow::handleXEvents(Display* dpy)
  95. {
  96. while (XPending(dpy)) {
  97. XEvent ev;
  98. XNextEvent(dpy, &ev);
  99. if (globalEventHandler) {
  100. if (globalEventHandler->handleGlobalEvent(&ev))
  101. continue;
  102. }
  103. if (ev.type == MappingNotify) {
  104. XRefreshKeyboardMapping(&ev.xmapping);
  105. } else if (ev.type == PropertyNotify &&
  106. ev.xproperty.window == DefaultRootWindow(dpy) &&
  107. ev.xproperty.atom == XA_CUT_BUFFER0) {
  108. cutBufferTime = ev.xproperty.time;
  109. } else {
  110. std::list<TXWindow*>::iterator i;
  111. for (i = windows.begin(); i != windows.end(); i++) {
  112. if ((*i)->win() == ev.xany.window)
  113. (*i)->handleXEvent(&ev);
  114. }
  115. }
  116. }
  117. }
  118. TXGlobalEventHandler* TXWindow::setGlobalEventHandler(TXGlobalEventHandler* h)
  119. {
  120. TXGlobalEventHandler* old = globalEventHandler;
  121. globalEventHandler = h;
  122. return old;
  123. }
  124. void TXWindow::getColours(Display* dpy, XColor* cols, int nCols)
  125. {
  126. bool* got = new bool[nCols];
  127. bool failed = false;
  128. int i;
  129. for (i = 0; i < nCols; i++) {
  130. if (XAllocColor(dpy, cmap, &cols[i])) {
  131. got[i] = true;
  132. } else {
  133. got[i] = false;
  134. failed = true;
  135. }
  136. }
  137. if (!failed) {
  138. delete [] got;
  139. return;
  140. }
  141. // AllocColor has failed. This is because the colormap is full. So the
  142. // only thing we can do is use the "shared" pixels in the colormap. The
  143. // code below is designed to work even when the colormap isn't full so is
  144. // more complex than it needs to be in this case. However it would be
  145. // useful one day to be able to restrict the number of colours allocated by
  146. // an application so I'm leaving it in here.
  147. // For each pixel in the colormap, try to allocate exactly its RGB values.
  148. // If this returns a different pixel then it must be a private or
  149. // unallocated pixel, so we can't use it. If it returns us the same pixel
  150. // again, it's almost certainly a shared colour, so we can use it (actually
  151. // it is possible that it was an unallocated pixel which we've now
  152. // allocated - by going through the pixels in reverse order we make this
  153. // unlikely except for the lowest unallocated pixel - this works because of
  154. // the way the X server allocates new pixels).
  155. int cmapSize = DisplayCells(dpy,DefaultScreen(dpy));
  156. XColor* cm = new XColor[cmapSize];
  157. bool* shared = new bool[cmapSize];
  158. bool* usedAsNearest = new bool[cmapSize];
  159. for (i = 0; i < cmapSize; i++) {
  160. cm[i].pixel = i;
  161. shared[i] = usedAsNearest[i] = false;
  162. }
  163. XQueryColors(dpy, cmap, cm, cmapSize);
  164. for (i = cmapSize-1; i >= 0; i--) {
  165. if (XAllocColor(dpy, cmap, &cm[i])) {
  166. if (cm[i].pixel == (unsigned long)i) {
  167. shared[i] = true;
  168. } else {
  169. XFreeColors(dpy, cmap, &cm[i].pixel, 1, 0);
  170. }
  171. }
  172. }
  173. for (int j = 0; j < nCols; j++) {
  174. unsigned long minDistance = ULONG_MAX;
  175. unsigned long nearestPixel = 0;
  176. if (!got[j]) {
  177. for (i = 0; i < cmapSize; i++) {
  178. if (shared[i]) {
  179. unsigned long rd = (cm[i].red - cols[j].red)/2;
  180. unsigned long gd = (cm[i].green - cols[j].green)/2;
  181. unsigned long bd = (cm[i].blue - cols[j].blue)/2;
  182. unsigned long distance = (rd*rd + gd*gd + bd*bd);
  183. if (distance < minDistance) {
  184. minDistance = distance;
  185. nearestPixel = i;
  186. }
  187. }
  188. }
  189. cols[j].pixel = nearestPixel;
  190. usedAsNearest[nearestPixel] = true;
  191. }
  192. }
  193. for (i = 0; i < cmapSize; i++) {
  194. if (shared[i] && !usedAsNearest[i]) {
  195. unsigned long p = i;
  196. XFreeColors(dpy, cmap, &p, 1, 0);
  197. }
  198. }
  199. }
  200. Window TXWindow::windowWithName(Display* dpy, Window top, const char* name)
  201. {
  202. char* windowName;
  203. if (XFetchName(dpy, top, &windowName)) {
  204. if (strcmp(windowName, name) == 0) {
  205. XFree(windowName);
  206. return top;
  207. }
  208. XFree(windowName);
  209. }
  210. Window* children;
  211. Window dummy;
  212. unsigned int nchildren;
  213. if (!XQueryTree(dpy, top, &dummy, &dummy, &children,&nchildren) || !children)
  214. return 0;
  215. for (int i = 0; i < (int)nchildren; i++) {
  216. Window w = windowWithName(dpy, children[i], name);
  217. if (w) {
  218. XFree((char*)children);
  219. return w;
  220. }
  221. }
  222. XFree((char*)children);
  223. return 0;
  224. }
  225. TXWindow::TXWindow(Display* dpy_, int w, int h, TXWindow* parent_,
  226. int borderWidth)
  227. : dpy(dpy_), xPad(3), yPad(3), bevel(2), parent(parent_), width_(w),
  228. height_(h), eventHandler(0), dwc(0), eventMask(0), toplevel_(false)
  229. {
  230. sizeHints.flags = 0;
  231. XSetWindowAttributes attr;
  232. attr.background_pixel = defaultBg;
  233. attr.border_pixel = 0;
  234. Window par = parent ? parent->win() : DefaultRootWindow(dpy);
  235. win_ = XCreateWindow(dpy, par, 0, 0, width_, height_, borderWidth,
  236. CopyFromParent, CopyFromParent, CopyFromParent,
  237. CWBackPixel | CWBorderPixel, &attr);
  238. if (parent) map();
  239. windows.push_back(this);
  240. }
  241. TXWindow::~TXWindow()
  242. {
  243. windows.remove(this);
  244. XDestroyWindow(dpy, win());
  245. }
  246. void TXWindow::toplevel(const char* name, TXDeleteWindowCallback* dwc_,
  247. int argc, char** argv, const char* windowClass,
  248. bool iconic)
  249. {
  250. toplevel_ = true;
  251. XWMHints wmHints;
  252. wmHints.flags = InputHint|StateHint;
  253. wmHints.input = True;
  254. wmHints.initial_state = iconic ? IconicState : NormalState;
  255. XClassHint classHint;
  256. if (!windowClass) windowClass = defaultWindowClass;
  257. classHint.res_name = (char*)name;
  258. classHint.res_class = (char*)windowClass;
  259. XSetWMProperties(dpy, win(), 0, 0, argv, argc,
  260. &sizeHints, &wmHints, &classHint);
  261. XStoreName(dpy, win(), name);
  262. XSetIconName(dpy, win(), name);
  263. Atom protocols[10];
  264. int nProtocols = 0;
  265. protocols[nProtocols++] = wmTakeFocus;
  266. dwc = dwc_;
  267. if (dwc)
  268. protocols[nProtocols++] = wmDeleteWindow;
  269. XSetWMProtocols(dpy, win(), protocols, nProtocols);
  270. addEventMask(StructureNotifyMask);
  271. }
  272. void TXWindow::setName(const char* name)
  273. {
  274. XClassHint classHint;
  275. XGetClassHint(dpy, win(), &classHint);
  276. XFree(classHint.res_name);
  277. classHint.res_name = (char*)name;
  278. XSetClassHint(dpy, win(), &classHint);
  279. XFree(classHint.res_class);
  280. XStoreName(dpy, win(), name);
  281. XSetIconName(dpy, win(), name);
  282. }
  283. void TXWindow::setMaxSize(int w, int h)
  284. {
  285. sizeHints.flags |= PMaxSize;
  286. sizeHints.max_width = w;
  287. sizeHints.max_height = h;
  288. XSetWMNormalHints(dpy, win(), &sizeHints);
  289. }
  290. void TXWindow::setUSPosition(int x, int y)
  291. {
  292. sizeHints.flags |= USPosition;
  293. sizeHints.x = x;
  294. sizeHints.y = y;
  295. XSetWMNormalHints(dpy, win(), &sizeHints);
  296. move(x, y);
  297. }
  298. void TXWindow::setGeometry(const char* geom, int x, int y, int w, int h)
  299. {
  300. char defGeom[256];
  301. sprintf(defGeom,"%dx%d+%d+%d",w,h,x,y);
  302. XWMGeometry(dpy, DefaultScreen(dpy), strEmptyToNull((char*)geom), defGeom,
  303. 0, &sizeHints, &x, &y, &w, &h, &sizeHints.win_gravity);
  304. sizeHints.flags |= PWinGravity;
  305. setUSPosition(x, y);
  306. resize(w, h);
  307. }
  308. TXEventHandler* TXWindow::setEventHandler(TXEventHandler* h)
  309. {
  310. TXEventHandler* old = eventHandler;
  311. eventHandler = h;
  312. return old;
  313. }
  314. void TXWindow::addEventMask(long mask)
  315. {
  316. eventMask |= mask;
  317. XSelectInput(dpy, win(), eventMask);
  318. }
  319. void TXWindow::removeEventMask(long mask)
  320. {
  321. eventMask &= ~mask;
  322. XSelectInput(dpy, win(), eventMask);
  323. }
  324. void TXWindow::unmap()
  325. {
  326. XUnmapWindow(dpy, win());
  327. if (toplevel_) {
  328. XUnmapEvent ue;
  329. ue.type = UnmapNotify;
  330. ue.display = dpy;
  331. ue.event = DefaultRootWindow(dpy);
  332. ue.window = win();
  333. ue.from_configure = False;
  334. XSendEvent(dpy, DefaultRootWindow(dpy), False,
  335. (SubstructureRedirectMask|SubstructureNotifyMask),
  336. (XEvent*)&ue);
  337. }
  338. }
  339. void TXWindow::resize(int w, int h)
  340. {
  341. //if (w == width_ && h == height_) return;
  342. XResizeWindow(dpy, win(), w, h);
  343. width_ = w;
  344. height_ = h;
  345. resizeNotify();
  346. }
  347. void TXWindow::setBorderWidth(int bw)
  348. {
  349. XWindowChanges c;
  350. c.border_width = bw;
  351. XConfigureWindow(dpy, win(), CWBorderWidth, &c);
  352. }
  353. void TXWindow::ownSelection(Atom selection, Time time)
  354. {
  355. XSetSelectionOwner(dpy, selection, win(), time);
  356. if (XGetSelectionOwner(dpy, selection) == win()) {
  357. selectionOwner_[selection] = true;
  358. selectionOwnTime[selection] = time;
  359. }
  360. }
  361. void TXWindow::handleXEvent(XEvent* ev)
  362. {
  363. switch (ev->type) {
  364. case ClientMessage:
  365. if (ev->xclient.message_type == wmProtocols) {
  366. if ((Atom)ev->xclient.data.l[0] == wmDeleteWindow) {
  367. if (dwc) dwc->deleteWindow(this);
  368. } else if ((Atom)ev->xclient.data.l[0] == wmTakeFocus) {
  369. takeFocus(ev->xclient.data.l[1]);
  370. }
  371. }
  372. break;
  373. case ConfigureNotify:
  374. if (ev->xconfigure.width != width_ || ev->xconfigure.height != height_) {
  375. width_ = ev->xconfigure.width;
  376. height_ = ev->xconfigure.height;
  377. resizeNotify();
  378. }
  379. break;
  380. case SelectionNotify:
  381. if (ev->xselection.property != None) {
  382. Atom type;
  383. int format;
  384. unsigned long nitems, after;
  385. unsigned char *data;
  386. XGetWindowProperty(dpy, win(), ev->xselection.property, 0, 16384, True,
  387. AnyPropertyType, &type, &format,
  388. &nitems, &after, &data);
  389. if (type != None) {
  390. selectionNotify(&ev->xselection, type, format, nitems, data);
  391. XFree(data);
  392. break;
  393. }
  394. }
  395. selectionNotify(&ev->xselection, 0, 0, 0, 0);
  396. break;
  397. case SelectionRequest:
  398. {
  399. XSelectionEvent se;
  400. se.type = SelectionNotify;
  401. se.display = ev->xselectionrequest.display;
  402. se.requestor = ev->xselectionrequest.requestor;
  403. se.selection = ev->xselectionrequest.selection;
  404. se.time = ev->xselectionrequest.time;
  405. se.target = ev->xselectionrequest.target;
  406. if (ev->xselectionrequest.property == None)
  407. ev->xselectionrequest.property = ev->xselectionrequest.target;
  408. if (!selectionOwner_[se.selection]) {
  409. se.property = None;
  410. } else {
  411. se.property = ev->xselectionrequest.property;
  412. if (se.target == xaTARGETS) {
  413. Atom targets[2];
  414. targets[0] = xaTIMESTAMP;
  415. targets[1] = XA_STRING;
  416. XChangeProperty(dpy, se.requestor, se.property, XA_ATOM, 32,
  417. PropModeReplace, (unsigned char*)targets, 2);
  418. } else if (se.target == xaTIMESTAMP) {
  419. rdr::U32 t = selectionOwnTime[se.selection];
  420. XChangeProperty(dpy, se.requestor, se.property, XA_INTEGER, 32,
  421. PropModeReplace, (unsigned char*)&t, 1);
  422. } else if (se.target == XA_STRING) {
  423. if (!selectionRequest(se.requestor, se.selection, se.property))
  424. se.property = None;
  425. } else {
  426. se.property = None;
  427. }
  428. }
  429. XSendEvent(dpy, se.requestor, False, 0, (XEvent*)&se);
  430. break;
  431. }
  432. case SelectionClear:
  433. selectionOwner_[ev->xselectionclear.selection] = false;
  434. break;
  435. }
  436. if (eventHandler) eventHandler->handleEvent(this, ev);
  437. }
  438. void TXWindow::drawBevel(GC gc, int x, int y, int w, int h, int b,
  439. unsigned long middle, unsigned long tl,
  440. unsigned long br, bool round)
  441. {
  442. if (round) {
  443. XGCValues gcv;
  444. gcv.line_width = b;
  445. XChangeGC(dpy, gc, GCLineWidth, &gcv);
  446. XSetForeground(dpy, gc, middle);
  447. XFillArc(dpy, win(), gc, x, y, w-b/2, h-b/2, 0, 360*64);
  448. XSetForeground(dpy, gc, tl);
  449. XDrawArc(dpy, win(), gc, x, y, w-b/2, h-b/2, 45*64, 180*64);
  450. XSetForeground(dpy, gc, br);
  451. XDrawArc(dpy, win(), gc, x, y, w-b/2, h-b/2, 225*64, 180*64);
  452. } else {
  453. XSetForeground(dpy, gc, middle);
  454. if (w-2*b > 0 && h-2*b > 0)
  455. XFillRectangle(dpy, win(), gc, x+b, y+b, w-2*b, h-2*b);
  456. XSetForeground(dpy, gc, tl);
  457. XFillRectangle(dpy, win(), gc, x, y, w, b);
  458. XFillRectangle(dpy, win(), gc, x, y, b, h);
  459. XSetForeground(dpy, gc, br);
  460. for (int i = 0; i < b; i++) {
  461. if (w-i > 0) XFillRectangle(dpy, win(), gc, x+i, y+h-1-i, w-i, 1);
  462. if (h-1-i > 0) XFillRectangle(dpy, win(), gc, x+w-1-i, y+i+1, 1, h-1-i);
  463. }
  464. }
  465. }