The repository formerly known as dotfiles
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.

x11.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Copyright (c) 2018, Curtis McEnroe <programble@gmail.com>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program 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 Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <X11/Xlib.h>
  17. #include <err.h>
  18. #include <sysexits.h>
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <stdbool.h>
  22. #include "gfx.h"
  23. static size_t width = 800;
  24. static size_t height = 600;
  25. static Display *display;
  26. static Window window;
  27. static Atom WM_DELETE_WINDOW;
  28. static GC windowGc;
  29. static XImage *image;
  30. static size_t bufSize;
  31. static uint32_t *buf;
  32. static size_t pixmapWidth;
  33. static size_t pixmapHeight;
  34. static Pixmap pixmap;
  35. static void createWindow(void) {
  36. display = XOpenDisplay(NULL);
  37. if (!display) errx(EX_UNAVAILABLE, "XOpenDisplay: %s", XDisplayName(NULL));
  38. Window root = DefaultRootWindow(display);
  39. window = XCreateSimpleWindow(display, root, 0, 0, width, height, 0, 0, 0);
  40. WM_DELETE_WINDOW = XInternAtom(display, "WM_DELETE_WINDOW", false);
  41. XSetWMProtocols(display, window, &WM_DELETE_WINDOW, 1);
  42. windowGc = XCreateGC(display, window, 0, NULL);
  43. image = XCreateImage(display, NULL, 24, ZPixmap, 0, NULL, width, height, 32, 0);
  44. }
  45. static void resizePixmap(void) {
  46. size_t newSize = 4 * width * height;
  47. if (newSize > bufSize) {
  48. bufSize = newSize;
  49. free(buf);
  50. buf = malloc(bufSize);
  51. if (!buf) err(EX_OSERR, "malloc(%zu)", bufSize);
  52. }
  53. image->data = (char *)buf;
  54. image->width = width;
  55. image->height = height;
  56. image->bytes_per_line = 4 * width;
  57. if (width > pixmapWidth || height > pixmapHeight) {
  58. pixmapWidth = width;
  59. pixmapHeight = height;
  60. if (pixmap) XFreePixmap(display, pixmap);
  61. pixmap = XCreatePixmap(display, window, pixmapWidth, pixmapHeight, 24);
  62. }
  63. }
  64. static void drawWindow(void) {
  65. draw(buf, width, height);
  66. XPutImage(display, pixmap, windowGc, image, 0, 0, 0, 0, width, height);
  67. XCopyArea(display, pixmap, window, windowGc, 0, 0, width, height, 0, 0);
  68. }
  69. int main(int argc, char *argv[]) {
  70. int error = init(argc, argv);
  71. if (error) return error;
  72. createWindow();
  73. resizePixmap();
  74. drawWindow();
  75. XStoreName(display, window, status());
  76. XMapWindow(display, window);
  77. XEvent event;
  78. XSelectInput(display, window, ExposureMask | StructureNotifyMask | KeyPressMask);
  79. for (;;) {
  80. XNextEvent(display, &event);
  81. switch (event.type) {
  82. case KeyPress: {
  83. XKeyEvent key = event.xkey;
  84. KeySym sym = XLookupKeysym(&key, key.state & ShiftMask);
  85. if (sym > 0x80) break;
  86. if (!input(sym)) return EX_OK;
  87. drawWindow();
  88. XStoreName(display, window, status());
  89. } break;
  90. case ConfigureNotify: {
  91. XConfigureEvent configure = event.xconfigure;
  92. width = configure.width;
  93. height = configure.height;
  94. resizePixmap();
  95. drawWindow();
  96. } break;
  97. case Expose: {
  98. XExposeEvent expose = event.xexpose;
  99. XCopyArea(
  100. display, pixmap, window, windowGc,
  101. expose.x, expose.y,
  102. expose.width, expose.height,
  103. expose.x, expose.y
  104. );
  105. } break;
  106. case ClientMessage: {
  107. XClientMessageEvent client = event.xclient;
  108. if ((Atom)client.data.l[0] == WM_DELETE_WINDOW) return EX_OK;
  109. } break;
  110. }
  111. }
  112. }