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

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