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

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