/experiments/4/main.c

https://github.com/ereslibre/x11 · C · 86 lines · 66 code · 13 blank · 7 comment · 6 complexity · 8b17bce409c97f22c39bae48361cb6d9 MD5 · raw file

  1. #include <X11/Xlib.h>
  2. #include <X11/extensions/Xrender.h>
  3. #include <iostream>
  4. #include <functional>
  5. #include <unistd.h>
  6. int main(int argc, char **argv)
  7. {
  8. Display *dpy = XOpenDisplay(0);
  9. if (!dpy) {
  10. return -1;
  11. }
  12. const int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
  13. const int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
  14. Window window = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 400, 400, 0, whiteColor, whiteColor);
  15. XSelectInput(dpy, window, StructureNotifyMask | VisibilityChangeMask | KeyPressMask | KeyReleaseMask | PointerMotionMask | ExposureMask);
  16. XMapWindow(dpy, window);
  17. //Window window2 = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 400, 400, 0, whiteColor, whiteColor);
  18. //XSelectInput(dpy, window2, StructureNotifyMask | VisibilityChangeMask | KeyPressMask | KeyReleaseMask | PointerMotionMask | ExposureMask);
  19. //XMapWindow(dpy, window2);
  20. XGCValues gcv;
  21. gcv.line_style = LineOnOffDash;
  22. gcv.line_width = 10;
  23. GC gc = XCreateGC(dpy, window, GCLineWidth | GCLineStyle, &gcv);
  24. XSetForeground(dpy, gc, blackColor);
  25. int eventBase;
  26. int errorBase;
  27. const bool renderExt = XRenderQueryExtension(dpy, &eventBase, &errorBase);
  28. if (renderExt) {
  29. std::cout << "there is render extension enabled :)" << std::endl;
  30. }
  31. while (true) {
  32. XEvent e;
  33. XNextEvent(dpy, &e);
  34. std::cout << "receiving event " << e.type << " on window " << e.xany.window << std::endl;
  35. if (e.type == VisibilityNotify) {
  36. XClearWindow(dpy, e.xany.window);
  37. //XDrawLine(dpy, e.xany.window, gc, 10, 60, 180, 20);
  38. //XDrawLine(dpy, e.xany.window, gc, 10, 20, 180, 60);
  39. //XDrawRectangle(dpy, e.xany.window, gc, 100, 100, 200, 200);
  40. if (renderExt) {
  41. XRenderColor color = { 0xffff, 0x0000, 0x0000, 0x5fff };
  42. Picture cyan = XRenderCreateSolidFill(dpy, &color);
  43. XRenderPictFormat *fmt = XRenderFindStandardFormat(dpy, PictStandardRGB24);
  44. XRenderPictureAttributes pict_attr;
  45. pict_attr.poly_edge = PolyEdgeSmooth;
  46. pict_attr.poly_mode = PolyModeImprecise;
  47. Picture picture = XRenderCreatePicture(dpy, window, fmt, CPPolyEdge | CPPolyMode, &pict_attr);
  48. XTriangle tr;
  49. tr.p1.x = 200 << 16;
  50. tr.p1.y = 50 << 16;
  51. tr.p2.x = 350 << 16;
  52. tr.p2.y = 350 << 16;
  53. tr.p3.x = 50 << 16;
  54. tr.p3.y = 350 << 16;
  55. //XRenderCompositeTriangles(dpy, PictOpOver, cyan, picture, 0, 0, 0, &tr, 1);
  56. XRenderPictFormat *fmt2 = XRenderFindStandardFormat(dpy, PictStandardA8);
  57. XPointDouble line[4];
  58. line[0].x = 60;
  59. line[0].y = 30;
  60. line[1].x = 370;
  61. line[1].y = 60;
  62. line[2].x = 120;
  63. line[2].y = 380;
  64. line[3].x = 20;
  65. line[3].y = 320;
  66. XRenderCompositeDoublePoly(dpy, PictOpOver, cyan, picture, fmt2, 0, 0, 0, 0, line, 4, 0);
  67. XRenderFillRectangle(dpy, PictOpOver, picture, &color, 10, 10, 300, 300);
  68. }
  69. XFlush(dpy);
  70. }
  71. }
  72. return 0;
  73. }