PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/experiments/2/main.c

https://github.com/ereslibre/x11
C | 75 lines | 65 code | 10 blank | 0 comment | 9 complexity | d598c5772c8fdc202f8508fc96d58dfa MD5 | raw file
  1. #include <X11/Xlib.h>
  2. #include <X11/extensions/Xrender.h>
  3. #include <iostream>
  4. #include <unistd.h>
  5. Picture create_pen(int red, int green, int blue, int alpha, Display *dpy, Window window)
  6. {
  7. XRenderColor color;
  8. color.red = red;
  9. color.green = green;
  10. color.blue = blue;
  11. color.alpha = alpha;
  12. XRenderPictFormat *fmt = XRenderFindStandardFormat(dpy, PictStandardARGB32);
  13. Pixmap pm = XCreatePixmap(dpy, window, 1, 1, 32);
  14. XRenderPictureAttributes pict_attr;
  15. pict_attr.repeat = 1;
  16. Picture picture = XRenderCreatePicture(dpy, pm, fmt, CPRepeat, &pict_attr);
  17. XRenderFillRectangle(dpy, PictOpOver, picture, &color, 0, 0, 1, 1);
  18. return picture;
  19. }
  20. int main(int argc, char **argv)
  21. {
  22. Display *dpy = XOpenDisplay(0);
  23. if (!dpy) {
  24. return -1;
  25. }
  26. const int blackColor = BlackPixel(dpy, DefaultScreen(dpy));
  27. const int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
  28. Window window = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 400, 400, 0, whiteColor, whiteColor);
  29. XSelectInput(dpy, window, StructureNotifyMask | VisibilityChangeMask | KeyPressMask | KeyReleaseMask | ExposureMask);
  30. XMapWindow(dpy, window);
  31. GC gc = XCreateGC(dpy, window, 0, 0);
  32. XSetForeground(dpy, gc, blackColor);
  33. int eventBase;
  34. int errorBase;
  35. const bool renderExt = XRenderQueryExtension(dpy, &eventBase, &errorBase);
  36. while (true) {
  37. XEvent e;
  38. XNextEvent(dpy, &e);
  39. std::cout << "receiving event " << e.type << std::endl;
  40. if (e.type == ConfigureNotify || e.type == VisibilityNotify || e.type == Expose) {
  41. XDrawLine(dpy, window, gc, 10, 60, 180, 20);
  42. XDrawLine(dpy, window, gc, 10, 20, 180, 60);
  43. XDrawRectangle(dpy, window, gc, 100, 100, 200, 200);
  44. if (renderExt) {
  45. std::cout << "there is render extension enabled :)" << std::endl;
  46. Picture cyan = create_pen(0, 0, 0xffff, 0xffff, dpy, window);
  47. XRenderPictFormat *fmt = XRenderFindStandardFormat(dpy, PictStandardRGB24);
  48. XRenderPictureAttributes pict_attr;
  49. pict_attr.poly_edge = PolyEdgeSmooth;
  50. pict_attr.poly_mode = PolyModeImprecise;
  51. Picture picture = XRenderCreatePicture(dpy, window, fmt, CPPolyEdge | CPPolyMode, &pict_attr);
  52. XTriangle tr;
  53. tr.p1.x = 50 << 16;
  54. tr.p1.y = 50 << 16;
  55. tr.p2.x = 200 << 16;
  56. tr.p2.y = 100 << 16;
  57. tr.p3.x = 100 << 16;
  58. tr.p3.y = 200 << 16;
  59. XRenderCompositeTriangles(dpy, PictOpOver, cyan, picture, 0, 0, 0, &tr, 1);
  60. }
  61. XFlush(dpy);
  62. }
  63. }
  64. return 0;
  65. }