linux_system.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  1. #include "linux_system.h"
  2. #include <iron_gpu.h>
  3. #include <iron_video.h>
  4. #include <iron_system.h>
  5. #include <assert.h>
  6. #include <limits.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <errno.h>
  11. #include <pwd.h>
  12. #include <unistd.h>
  13. #include <ctype.h>
  14. #include <time.h>
  15. #include <dlfcn.h>
  16. #include <X11/Xlib.h>
  17. #include <sys/stat.h>
  18. #include <sys/time.h>
  19. #include <vulkan/vulkan.h>
  20. #include <vulkan/vulkan_xlib.h>
  21. #define Button6 6
  22. #define Button7 7
  23. struct iron_x11_procs xlib = {0};
  24. struct x11_context x11_ctx = {0};
  25. static size_t clipboardStringSize = 1024;
  26. static char *clipboardString = NULL;
  27. static char buffer[1024];
  28. static char save[2000];
  29. static bool saveInitialized = false;
  30. static const char *videoFormats[] = {"ogv", NULL};
  31. static struct timeval start;
  32. static bool mouse_hidden = false;
  33. static void init_pen_device(XDeviceInfo *info, struct x11_pen_device *pen, bool eraser);
  34. bool iron_x11_init();
  35. void iron_display_init() {
  36. static bool display_initialized = false;
  37. if (display_initialized) {
  38. return;
  39. }
  40. iron_x11_init();
  41. Window root_window = RootWindow(x11_ctx.display, DefaultScreen(x11_ctx.display));
  42. XRRScreenResources *screen_resources = xlib.XRRGetScreenResourcesCurrent(x11_ctx.display, root_window);
  43. RROutput primary_output = xlib.XRRGetOutputPrimary(x11_ctx.display, root_window);
  44. for (int i = 0; i < screen_resources->noutput; i++) {
  45. if (i >= MAXIMUM_DISPLAYS) {
  46. iron_error("Too many screens (maximum %i)", MAXIMUM_DISPLAYS);
  47. break;
  48. }
  49. XRROutputInfo *output_info = xlib.XRRGetOutputInfo(x11_ctx.display, screen_resources, screen_resources->outputs[i]);
  50. if (output_info->connection != RR_Connected || output_info->crtc == None) {
  51. xlib.XRRFreeOutputInfo(output_info);
  52. continue;
  53. }
  54. XRRCrtcInfo *crtc_info = xlib.XRRGetCrtcInfo(x11_ctx.display, screen_resources, output_info->crtc);
  55. struct iron_x11_display *display = &x11_ctx.displays[x11_ctx.num_displays++];
  56. display->index = i;
  57. display->x = crtc_info->x;
  58. display->y = crtc_info->y;
  59. display->width = crtc_info->width;
  60. display->height = crtc_info->height;
  61. display->primary = screen_resources->outputs[i] == primary_output;
  62. display->crtc = output_info->crtc;
  63. display->output = screen_resources->outputs[i];
  64. xlib.XRRFreeOutputInfo(output_info);
  65. xlib.XRRFreeCrtcInfo(crtc_info);
  66. }
  67. xlib.XRRFreeScreenResources(screen_resources);
  68. display_initialized = true;
  69. }
  70. iron_display_mode_t iron_display_current_mode(int display_index) {
  71. struct iron_x11_display *display = &x11_ctx.displays[display_index];
  72. iron_display_mode_t mode;
  73. mode.x = 0;
  74. mode.y = 0;
  75. mode.width = display->width;
  76. mode.height = display->height;
  77. mode.frequency = 60;
  78. mode.bits_per_pixel = 32;
  79. mode.pixels_per_inch = 96;
  80. Window root_window = DefaultRootWindow(x11_ctx.display);
  81. XRRScreenResources *screen_resources = xlib.XRRGetScreenResourcesCurrent(x11_ctx.display, root_window);
  82. XRROutputInfo *output_info = xlib.XRRGetOutputInfo(x11_ctx.display, screen_resources, screen_resources->outputs[display->index]);
  83. if (output_info->connection != RR_Connected || output_info->crtc == None) {
  84. xlib.XRRFreeOutputInfo(output_info);
  85. xlib.XRRFreeScreenResources(screen_resources);
  86. return mode;
  87. }
  88. XRRCrtcInfo *crtc_info = xlib.XRRGetCrtcInfo(x11_ctx.display, screen_resources, output_info->crtc);
  89. for (int j = 0; j < output_info->nmode; j++) {
  90. RRMode rr_mode = crtc_info->mode;
  91. XRRModeInfo *mode_info = NULL;
  92. for (int k = 0; k < screen_resources->nmode; k++) {
  93. if (screen_resources->modes[k].id == rr_mode) {
  94. mode_info = &screen_resources->modes[k];
  95. break;
  96. }
  97. }
  98. if (mode_info == NULL) {
  99. continue;
  100. }
  101. mode.x = display->x;
  102. mode.y = display->y;
  103. mode.width = mode_info->width;
  104. mode.height = mode_info->height;
  105. mode.pixels_per_inch = 96;
  106. mode.bits_per_pixel = 32;
  107. if (mode_info->hTotal && mode_info->vTotal) {
  108. mode.frequency = (mode_info->dotClock / (mode_info->hTotal * mode_info->vTotal));
  109. }
  110. }
  111. xlib.XRRFreeOutputInfo(output_info);
  112. xlib.XRRFreeCrtcInfo(crtc_info);
  113. xlib.XRRFreeScreenResources(screen_resources);
  114. return mode;
  115. }
  116. int iron_primary_display(void) {
  117. for (int i = 0; i < x11_ctx.num_displays; i++) {
  118. if (x11_ctx.displays[i].primary) {
  119. return i;
  120. }
  121. }
  122. return 0;
  123. }
  124. int iron_count_displays(void) {
  125. return x11_ctx.num_displays;
  126. }
  127. int iron_window_x() {
  128. return 0;
  129. }
  130. int iron_window_y() {
  131. return 0;
  132. }
  133. int iron_window_width() {
  134. return x11_ctx.windows[0].width;
  135. }
  136. int iron_window_height() {
  137. return x11_ctx.windows[0].height;
  138. }
  139. void iron_window_resize(int width, int height) {
  140. struct iron_x11_window *window = &x11_ctx.windows[0];
  141. xlib.XResizeWindow(x11_ctx.display, window->window, width, height);
  142. }
  143. void iron_window_move(int x, int y) {
  144. struct iron_x11_window *window = &x11_ctx.windows[0];
  145. xlib.XMoveWindow(x11_ctx.display, window->window, x, y);
  146. }
  147. void iron_window_change_mode(iron_window_mode_t mode) {
  148. struct iron_x11_window *window = &x11_ctx.windows[0];
  149. if (mode == window->mode) {
  150. return;
  151. }
  152. window->mode = mode;
  153. XEvent xev;
  154. memset(&xev, 0, sizeof(xev));
  155. xev.type = ClientMessage;
  156. xev.xclient.window = window->window;
  157. xev.xclient.message_type = x11_ctx.atoms.NET_WM_STATE;
  158. xev.xclient.format = 32;
  159. xev.xclient.data.l[0] = mode == IRON_WINDOW_MODE_FULLSCREEN ? 1 : 0;
  160. xev.xclient.data.l[1] = x11_ctx.atoms.NET_WM_STATE_FULLSCREEN;
  161. xev.xclient.data.l[2] = 0;
  162. xlib.XMapWindow(x11_ctx.display, window->window);
  163. xlib.XSendEvent(x11_ctx.display, DefaultRootWindow(x11_ctx.display), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
  164. xlib.XFlush(x11_ctx.display);
  165. }
  166. int iron_window_display() {
  167. struct iron_x11_window *window = &x11_ctx.windows[0];
  168. return window->display_index;
  169. }
  170. void iron_window_destroy() {
  171. xlib.XFlush(x11_ctx.display);
  172. struct iron_x11_window *window = &x11_ctx.windows[0];
  173. xlib.XDestroyIC(window->xInputContext);
  174. xlib.XCloseIM(window->xInputMethod);
  175. xlib.XDestroyWindow(x11_ctx.display, window->window);
  176. xlib.XFlush(x11_ctx.display);
  177. *window = (struct iron_x11_window){0};
  178. }
  179. void iron_window_show() {
  180. struct iron_x11_window *window = &x11_ctx.windows[0];
  181. xlib.XMapWindow(x11_ctx.display, window->window);
  182. }
  183. void iron_window_hide() {
  184. struct iron_x11_window *window = &x11_ctx.windows[0];
  185. xlib.XUnmapWindow(x11_ctx.display, window->window);
  186. }
  187. void iron_window_set_title(const char *title) {
  188. struct iron_x11_window *window = &x11_ctx.windows[0];
  189. xlib.XChangeProperty(x11_ctx.display, window->window, x11_ctx.atoms.NET_WM_NAME, x11_ctx.atoms.UTF8_STRING, 8, PropModeReplace, (unsigned char *)title, strlen(title));
  190. xlib.XChangeProperty(x11_ctx.display, window->window, x11_ctx.atoms.NET_WM_ICON_NAME, x11_ctx.atoms.UTF8_STRING, 8, PropModeReplace, (unsigned char *)title, strlen(title));
  191. xlib.XFlush(x11_ctx.display);
  192. }
  193. void iron_window_create(iron_window_options_t *win) {
  194. struct iron_x11_window *window = &x11_ctx.windows[0];
  195. window->width = win->width;
  196. window->height = win->height;
  197. Visual *visual = NULL;
  198. XSetWindowAttributes set_window_attribs = {0};
  199. XColor color;
  200. Colormap colormap = DefaultColormap(x11_ctx.display, DefaultScreen(x11_ctx.display));
  201. color.red = (32 * 65535) / 255;
  202. color.green = (32 * 65535) / 255;
  203. color.blue = (32 * 65535) / 255;
  204. xlib.XAllocColor(x11_ctx.display, colormap, &color);
  205. set_window_attribs.background_pixel = color.pixel;
  206. set_window_attribs.border_pixel = 0;
  207. set_window_attribs.event_mask =
  208. KeyPressMask | KeyReleaseMask | ExposureMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask | FocusChangeMask;
  209. int screen = DefaultScreen(x11_ctx.display);
  210. visual = DefaultVisual(x11_ctx.display, screen);
  211. int depth = DefaultDepth(x11_ctx.display, screen);
  212. set_window_attribs.colormap = xlib.XCreateColormap(x11_ctx.display, RootWindow(x11_ctx.display, screen), visual, AllocNone);
  213. window->window = xlib.XCreateWindow(x11_ctx.display, RootWindow(x11_ctx.display, DefaultScreen(x11_ctx.display)), 0, 0, win->width, win->height, 0, depth,
  214. InputOutput, visual, CWBackPixel | CWBorderPixel | CWColormap | CWEventMask, &set_window_attribs);
  215. static char nameClass[256];
  216. static const char *nameClassAddendum = "_IronApplication";
  217. strncpy(nameClass, iron_application_name(), sizeof(nameClass) - strlen(nameClassAddendum) - 1);
  218. strcat(nameClass, nameClassAddendum);
  219. char resNameBuffer[256];
  220. strncpy(resNameBuffer, iron_application_name(), 256);
  221. XClassHint classHint = {.res_name = resNameBuffer, .res_class = nameClass};
  222. xlib.XSetClassHint(x11_ctx.display, window->window, &classHint);
  223. xlib.XSetLocaleModifiers("@im=none");
  224. window->xInputMethod = xlib.XOpenIM(x11_ctx.display, NULL, NULL, NULL);
  225. window->xInputContext = xlib.XCreateIC(window->xInputMethod, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, window->window, NULL);
  226. xlib.XSetICFocus(window->xInputContext);
  227. window->mode = IRON_WINDOW_MODE_WINDOW;
  228. iron_window_change_mode(win->mode);
  229. xlib.XMapWindow(x11_ctx.display, window->window);
  230. Atom XdndVersion = 5;
  231. xlib.XChangeProperty(x11_ctx.display, window->window, x11_ctx.atoms.XdndAware, XA_ATOM, 32, PropModeReplace, (unsigned char *)&XdndVersion, 1);
  232. xlib.XSetWMProtocols(x11_ctx.display, window->window, &x11_ctx.atoms.WM_DELETE_WINDOW, 1);
  233. iron_window_set_title(win->title);
  234. if (x11_ctx.pen.id != -1) {
  235. xlib.XSelectExtensionEvent(x11_ctx.display, window->window, &x11_ctx.pen.motionClass, 1);
  236. }
  237. if (x11_ctx.eraser.id != -1) {
  238. xlib.XSelectExtensionEvent(x11_ctx.display, window->window, &x11_ctx.eraser.motionClass, 1);
  239. }
  240. gpu_init(win->depth_bits, win->vsync);
  241. }
  242. static struct {
  243. void (*resize_callback)(int width, int height, void *data);
  244. void *resize_data;
  245. void *ppi_data;
  246. bool (*close_callback)(void *data);
  247. void *close_data;
  248. } iron_internal_window_callbacks[1];
  249. void iron_window_set_resize_callback(void (*callback)(int width, int height, void *data), void *data) {
  250. iron_internal_window_callbacks[0].resize_callback = callback;
  251. iron_internal_window_callbacks[0].resize_data = data;
  252. }
  253. void iron_internal_call_resize_callback(int width, int height) {
  254. if (iron_internal_window_callbacks[0].resize_callback != NULL) {
  255. iron_internal_window_callbacks[0].resize_callback(width, height, iron_internal_window_callbacks[0].resize_data);
  256. }
  257. }
  258. void iron_window_set_close_callback(bool (*callback)(void *data), void *data) {
  259. iron_internal_window_callbacks[0].close_callback = callback;
  260. iron_internal_window_callbacks[0].close_data = data;
  261. }
  262. bool iron_internal_call_close_callback() {
  263. if (iron_internal_window_callbacks[0].close_callback != NULL) {
  264. return iron_internal_window_callbacks[0].close_callback(iron_internal_window_callbacks[0].close_data);
  265. }
  266. else {
  267. return true;
  268. }
  269. }
  270. iron_window_mode_t iron_window_get_mode() {
  271. return x11_ctx.windows[0].mode;
  272. }
  273. static void load_lib(void **lib, const char *name) {
  274. char libname[64];
  275. sprintf(libname, "lib%s.so", name);
  276. *lib = dlopen(libname, RTLD_LAZY);
  277. if (*lib != NULL) {
  278. return;
  279. }
  280. // Ubuntu and Fedora only ship libFoo.so.major by default, so look for those.
  281. for (int i = 0; i < 10; i++) {
  282. sprintf(libname, "lib%s.so.%i", name, i);
  283. *lib = dlopen(libname, RTLD_LAZY);
  284. if (*lib != NULL) {
  285. return;
  286. }
  287. }
  288. iron_error("Failed to load lib%s.so", name);
  289. }
  290. int iron_x11_error_handler(Display *display, XErrorEvent *error_event) {
  291. xlib.XGetErrorText(display, error_event->error_code, buffer, 1024);
  292. iron_error("X Error: %s", buffer);
  293. return 0;
  294. }
  295. bool iron_x11_init() {
  296. load_lib(&x11_ctx.libs.X11, "X11");
  297. load_lib(&x11_ctx.libs.Xi, "Xi");
  298. load_lib(&x11_ctx.libs.Xcursor, "Xcursor");
  299. load_lib(&x11_ctx.libs.Xrandr, "Xrandr");
  300. xlib.XOpenDisplay = dlsym(x11_ctx.libs.X11, "XOpenDisplay");
  301. xlib.XCloseDisplay = dlsym(x11_ctx.libs.X11, "XCloseDisplay");
  302. xlib.XSetErrorHandler = dlsym(x11_ctx.libs.X11, "XSetErrorHandler");
  303. xlib.XGetErrorText = dlsym(x11_ctx.libs.X11, "XGetErrorText");
  304. xlib.XInternAtoms = dlsym(x11_ctx.libs.X11, "XInternAtoms");
  305. xlib.XPending = dlsym(x11_ctx.libs.X11, "XPending");
  306. xlib.XFlush = dlsym(x11_ctx.libs.X11, "XFlush");
  307. xlib.XNextEvent = dlsym(x11_ctx.libs.X11, "XNextEvent");
  308. xlib.XRefreshKeyboardMapping = dlsym(x11_ctx.libs.X11, "XRefreshKeyboardMapping");
  309. xlib.XwcLookupString = dlsym(x11_ctx.libs.X11, "XwcLookupString");
  310. xlib.XFilterEvent = dlsym(x11_ctx.libs.X11, "XFilterEvent");
  311. xlib.XConvertSelection = dlsym(x11_ctx.libs.X11, "XConvertSelection");
  312. xlib.XSetSelectionOwner = dlsym(x11_ctx.libs.X11, "XSetSelectionOwner");
  313. xlib.XLookupString = dlsym(x11_ctx.libs.X11, "XLookupString");
  314. xlib.XkbKeycodeToKeysym = dlsym(x11_ctx.libs.X11, "XkbKeycodeToKeysym");
  315. xlib.XSendEvent = dlsym(x11_ctx.libs.X11, "XSendEvent");
  316. xlib.XGetWindowProperty = dlsym(x11_ctx.libs.X11, "XGetWindowProperty");
  317. xlib.XFree = dlsym(x11_ctx.libs.X11, "XFree");
  318. xlib.XChangeProperty = dlsym(x11_ctx.libs.X11, "XChangeProperty");
  319. xlib.XDefineCursor = dlsym(x11_ctx.libs.X11, "XDefineCursor");
  320. xlib.XUndefineCursor = dlsym(x11_ctx.libs.X11, "XUndefineCursor");
  321. xlib.XCreateBitmapFromData = dlsym(x11_ctx.libs.X11, "XCreateBitmapFromData");
  322. xlib.XCreatePixmapCursor = dlsym(x11_ctx.libs.X11, "XCreatePixmapCursor");
  323. xlib.XFreePixmap = dlsym(x11_ctx.libs.X11, "XFreePixmap");
  324. xlib.XWarpPointer = dlsym(x11_ctx.libs.X11, "XWarpPointer");
  325. xlib.XQueryPointer = dlsym(x11_ctx.libs.X11, "XQueryPointer");
  326. xlib.XCreateColormap = dlsym(x11_ctx.libs.X11, "XCreateColormap");
  327. xlib.XCreateWindow = dlsym(x11_ctx.libs.X11, "XCreateWindow");
  328. xlib.XMoveWindow = dlsym(x11_ctx.libs.X11, "XMoveWindow");
  329. xlib.XResizeWindow = dlsym(x11_ctx.libs.X11, "XResizeWindow");
  330. xlib.XDestroyWindow = dlsym(x11_ctx.libs.X11, "XDestroyWindow");
  331. xlib.XSetClassHint = dlsym(x11_ctx.libs.X11, "XSetClassHint");
  332. xlib.XSetLocaleModifiers = dlsym(x11_ctx.libs.X11, "XSetLocaleModifiers");
  333. xlib.XOpenIM = dlsym(x11_ctx.libs.X11, "XOpenIM");
  334. xlib.XCloseIM = dlsym(x11_ctx.libs.X11, "XCloseIM");
  335. xlib.XCreateIC = dlsym(x11_ctx.libs.X11, "XCreateIC");
  336. xlib.XDestroyIC = dlsym(x11_ctx.libs.X11, "XDestroyIC");
  337. xlib.XSetICFocus = dlsym(x11_ctx.libs.X11, "XSetICFocus");
  338. xlib.XMapWindow = dlsym(x11_ctx.libs.X11, "XMapWindow");
  339. xlib.XUnmapWindow = dlsym(x11_ctx.libs.X11, "XUnmapWindow");
  340. xlib.XSetWMProtocols = dlsym(x11_ctx.libs.X11, "XSetWMProtocols");
  341. xlib.XPeekEvent = dlsym(x11_ctx.libs.X11, "XPeekEvent");
  342. xlib.XAllocColor = dlsym(x11_ctx.libs.X11, "XAllocColor");
  343. xlib.XListInputDevices = dlsym(x11_ctx.libs.Xi, "XListInputDevices");
  344. xlib.XFreeDeviceList = dlsym(x11_ctx.libs.Xi, "XFreeDeviceList");
  345. xlib.XOpenDevice = dlsym(x11_ctx.libs.Xi, "XOpenDevice");
  346. xlib.XCloseDevice = dlsym(x11_ctx.libs.Xi, "XCloseDevice");
  347. xlib.XSelectExtensionEvent = dlsym(x11_ctx.libs.Xi, "XSelectExtensionEvent");
  348. xlib.XcursorLibraryLoadCursor = dlsym(x11_ctx.libs.Xcursor, "XcursorLibraryLoadCursor");
  349. xlib.XRRGetScreenResourcesCurrent = dlsym(x11_ctx.libs.Xrandr, "XRRGetScreenResourcesCurrent");
  350. xlib.XRRGetOutputPrimary = dlsym(x11_ctx.libs.Xrandr, "XRRGetOutputPrimary");
  351. xlib.XRRGetOutputInfo = dlsym(x11_ctx.libs.Xrandr, "XRRGetOutputInfo");
  352. xlib.XRRFreeOutputInfo = dlsym(x11_ctx.libs.Xrandr, "XRRFreeOutputInfo");
  353. xlib.XRRGetCrtcInfo = dlsym(x11_ctx.libs.Xrandr, "XRRGetCrtcInfo");
  354. xlib.XRRFreeCrtcInfo = dlsym(x11_ctx.libs.Xrandr, "XRRFreeCrtcInfo");
  355. xlib.XRRFreeScreenResources = dlsym(x11_ctx.libs.Xrandr, "XRRFreeScreenResources");
  356. x11_ctx.display = xlib.XOpenDisplay(NULL);
  357. if (!x11_ctx.display) {
  358. return false;
  359. }
  360. xlib.XSetErrorHandler(iron_x11_error_handler);
  361. // this should be kept in sync with the x11_atoms struct
  362. static char *atom_names[] = {
  363. "XdndAware",
  364. "XdndDrop",
  365. "XdndEnter",
  366. "text/uri-list",
  367. "XdndStatus",
  368. "XdndActionCopy",
  369. "XdndSelection",
  370. "CLIPBOARD",
  371. "UTF8_STRING",
  372. "XSEL_DATA",
  373. "TARGETS",
  374. "MULTIPLE",
  375. "text/plain;charset=utf-8",
  376. "WM_DELETE_WINDOW",
  377. "_MOTIF_WM_HINTS",
  378. "_NET_WM_NAME",
  379. "_NET_WM_ICON_NAME",
  380. "_NET_WM_STATE",
  381. "_NET_WM_STATE_FULLSCREEN",
  382. XI_MOUSE,
  383. XI_TABLET,
  384. XI_KEYBOARD,
  385. XI_TOUCHSCREEN,
  386. XI_TOUCHPAD,
  387. XI_BUTTONBOX,
  388. XI_BARCODE,
  389. XI_TRACKBALL,
  390. XI_QUADRATURE,
  391. XI_ID_MODULE,
  392. XI_ONE_KNOB,
  393. XI_NINE_KNOB,
  394. XI_KNOB_BOX,
  395. XI_SPACEBALL,
  396. XI_DATAGLOVE,
  397. XI_EYETRACKER,
  398. XI_CURSORKEYS,
  399. XI_FOOTMOUSE,
  400. XI_JOYSTICK,
  401. };
  402. assert((sizeof atom_names / sizeof atom_names[0]) == (sizeof(struct iron_x11_atoms) / sizeof(Atom)));
  403. xlib.XInternAtoms(x11_ctx.display, atom_names, sizeof atom_names / sizeof atom_names[0], False, (Atom *)&x11_ctx.atoms);
  404. clipboardString = (char *)malloc(clipboardStringSize);
  405. x11_ctx.pen.id = -1;
  406. x11_ctx.eraser.id = -1;
  407. int count;
  408. XDeviceInfoPtr devices = (XDeviceInfoPtr)xlib.XListInputDevices(x11_ctx.display, &count);
  409. for (int i = 0; i < count; i++) {
  410. strncpy(buffer, devices[i].name, 1023);
  411. buffer[1023] = 0;
  412. for (int j = 0; buffer[j]; j++) {
  413. buffer[j] = tolower(buffer[j]);
  414. }
  415. if (strstr(buffer, "stylus") || strstr(buffer, "pen") || strstr(buffer, "wacom")) {
  416. init_pen_device(&devices[i], &x11_ctx.pen, false);
  417. }
  418. if (strstr(buffer, "eraser")) {
  419. init_pen_device(&devices[i], &x11_ctx.eraser, true);
  420. }
  421. }
  422. if (devices != NULL) {
  423. xlib.XFreeDeviceList(devices);
  424. }
  425. return true;
  426. }
  427. static void init_pen_device(XDeviceInfo *info, struct x11_pen_device *pen, bool eraser) {
  428. XDevice *device = xlib.XOpenDevice(x11_ctx.display, info->id);
  429. XAnyClassPtr c = info->inputclassinfo;
  430. for (int j = 0; j < device->num_classes; j++) {
  431. if (c->class == ValuatorClass) {
  432. XValuatorInfo *valuator_info = (XValuatorInfo *)c;
  433. if (valuator_info->num_axes > 2) {
  434. pen->maxPressure = valuator_info->axes[2].max_value;
  435. }
  436. pen->id = info->id;
  437. DeviceMotionNotify(device, pen->motionEvent, pen->motionClass);
  438. if (eraser) {
  439. pen->press = iron_internal_eraser_trigger_press;
  440. pen->move = iron_internal_eraser_trigger_move;
  441. pen->release = iron_internal_eraser_trigger_release;
  442. }
  443. else {
  444. pen->press = iron_internal_pen_trigger_press;
  445. pen->move = iron_internal_pen_trigger_move;
  446. pen->release = iron_internal_pen_trigger_release;
  447. }
  448. return;
  449. }
  450. c = (XAnyClassPtr)((uint8_t *)c + c->length);
  451. }
  452. xlib.XCloseDevice(x11_ctx.display, device);
  453. }
  454. static void check_pen_device(struct iron_x11_window *window, XEvent *event, struct x11_pen_device *pen) {
  455. if (event->type == pen->motionEvent) {
  456. XDeviceMotionEvent *motion = (XDeviceMotionEvent *)(event);
  457. if (motion->deviceid == pen->id) {
  458. float p = (float)motion->axis_data[2] / (float)pen->maxPressure;
  459. if (p > 0 && x11_ctx.pen.current_pressure == 0) {
  460. pen->press(motion->x, motion->y, p);
  461. }
  462. else if (p == 0 && pen->current_pressure > 0) {
  463. pen->release(motion->x, motion->y, p);
  464. }
  465. else if (p > 0) {
  466. pen->move(motion->x, motion->y, p);
  467. }
  468. pen->current_pressure = p;
  469. }
  470. }
  471. }
  472. struct iron_x11_window *window_from_window(Window window) {
  473. for (int i = 0; i < 1; i++) {
  474. if (x11_ctx.windows[i].window == window) {
  475. return &x11_ctx.windows[i];
  476. }
  477. }
  478. return NULL;
  479. }
  480. void iron_vulkan_get_instance_extensions(const char **names, int *index) {
  481. names[(*index)++] = VK_KHR_XLIB_SURFACE_EXTENSION_NAME;
  482. }
  483. VkBool32 iron_vulkan_get_physical_device_presentation_support(VkPhysicalDevice physicalDevice, uint32_t queueFamilyIndex) {
  484. return vkGetPhysicalDeviceXlibPresentationSupportKHR(physicalDevice, queueFamilyIndex, x11_ctx.display,
  485. DefaultVisual(x11_ctx.display, DefaultScreen(x11_ctx.display))->visualid);
  486. }
  487. VkResult iron_vulkan_create_surface(VkInstance instance, VkSurfaceKHR *surface) {
  488. VkXlibSurfaceCreateInfoKHR info = {0};
  489. info.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
  490. info.pNext = NULL;
  491. info.flags = 0;
  492. info.dpy = x11_ctx.display;
  493. info.window = x11_ctx.windows[0].window;
  494. return vkCreateXlibSurfaceKHR(instance, &info, NULL, surface);
  495. }
  496. static int xk_to_iron(KeySym symbol) {
  497. if (symbol == XK_Right) return IRON_KEY_RIGHT;
  498. if (symbol == XK_Left) return IRON_KEY_LEFT;
  499. if (symbol == XK_Up) return IRON_KEY_UP;
  500. if (symbol == XK_Down) return IRON_KEY_DOWN;
  501. if (symbol == XK_space) return IRON_KEY_SPACE;
  502. if (symbol == XK_BackSpace) return IRON_KEY_BACKSPACE;
  503. if (symbol == XK_Tab) return IRON_KEY_TAB;
  504. if (symbol == XK_Return) return IRON_KEY_RETURN;
  505. if (symbol == XK_Shift_L) return IRON_KEY_SHIFT;
  506. if (symbol == XK_Shift_R) return IRON_KEY_SHIFT;
  507. if (symbol == XK_Control_L) return IRON_KEY_CONTROL;
  508. if (symbol == XK_Control_R) return IRON_KEY_CONTROL;
  509. if (symbol == XK_Alt_L) return IRON_KEY_ALT;
  510. if (symbol == XK_Alt_R) return IRON_KEY_ALT;
  511. if (symbol == XK_Delete) return IRON_KEY_DELETE;
  512. if (symbol == XK_comma) return IRON_KEY_COMMA;
  513. if (symbol == XK_period) return IRON_KEY_PERIOD;
  514. if (symbol == XK_bracketleft) return IRON_KEY_OPEN_BRACKET;
  515. if (symbol == XK_bracketright) return IRON_KEY_CLOSE_BRACKET;
  516. if (symbol == XK_braceleft) return IRON_KEY_OPEN_CURLY_BRACKET;
  517. if (symbol == XK_braceright) return IRON_KEY_CLOSE_CURLY_BRACKET;
  518. if (symbol == XK_parenleft) return IRON_KEY_OPEN_PAREN;
  519. if (symbol == XK_parenright) return IRON_KEY_CLOSE_PAREN;
  520. if (symbol == XK_backslash) return IRON_KEY_BACK_SLASH;
  521. if (symbol == XK_apostrophe) return IRON_KEY_QUOTE;
  522. if (symbol == XK_colon) return IRON_KEY_COLON;
  523. if (symbol == XK_semicolon) return IRON_KEY_SEMICOLON;
  524. if (symbol == XK_minus) return IRON_KEY_HYPHEN_MINUS;
  525. if (symbol == XK_underscore) return IRON_KEY_UNDERSCORE;
  526. if (symbol == XK_slash) return IRON_KEY_SLASH;
  527. if (symbol == XK_bar) return IRON_KEY_PIPE;
  528. if (symbol == XK_question) return IRON_KEY_QUESTIONMARK;
  529. if (symbol == XK_less) return IRON_KEY_LESS_THAN;
  530. if (symbol == XK_greater) return IRON_KEY_GREATER_THAN;
  531. if (symbol == XK_asterisk) return IRON_KEY_ASTERISK;
  532. if (symbol == XK_ampersand) return IRON_KEY_AMPERSAND;
  533. if (symbol == XK_asciicircum) return IRON_KEY_CIRCUMFLEX;
  534. if (symbol == XK_percent) return IRON_KEY_PERCENT;
  535. if (symbol == XK_dollar) return IRON_KEY_DOLLAR;
  536. if (symbol == XK_numbersign) return IRON_KEY_HASH;
  537. if (symbol == XK_at) return IRON_KEY_AT;
  538. if (symbol == XK_exclam) return IRON_KEY_EXCLAMATION;
  539. if (symbol == XK_equal) return IRON_KEY_EQUALS;
  540. if (symbol == XK_plus) return IRON_KEY_ADD;
  541. if (symbol == XK_quoteleft) return IRON_KEY_BACK_QUOTE;
  542. if (symbol == XK_quotedbl) return IRON_KEY_DOUBLE_QUOTE;
  543. if (symbol == XK_asciitilde) return IRON_KEY_TILDE;
  544. if (symbol == XK_Pause) return IRON_KEY_PAUSE;
  545. if (symbol == XK_Scroll_Lock) return IRON_KEY_SCROLL_LOCK;
  546. if (symbol == XK_Home) return IRON_KEY_HOME;
  547. if (symbol == XK_Page_Up) return IRON_KEY_PAGE_UP;
  548. if (symbol == XK_Page_Down) return IRON_KEY_PAGE_DOWN;
  549. if (symbol == XK_End) return IRON_KEY_END;
  550. if (symbol == XK_Insert) return IRON_KEY_INSERT;
  551. if (symbol == XK_KP_Enter) return IRON_KEY_RETURN;
  552. if (symbol == XK_KP_Multiply) return IRON_KEY_MULTIPLY;
  553. if (symbol == XK_KP_Add) return IRON_KEY_ADD;
  554. if (symbol == XK_KP_Subtract) return IRON_KEY_SUBTRACT;
  555. if (symbol == XK_KP_Decimal) return IRON_KEY_DECIMAL;
  556. if (symbol == XK_KP_Divide) return IRON_KEY_DIVIDE;
  557. if (symbol == XK_KP_0) return IRON_KEY_NUMPAD_0;
  558. if (symbol == XK_KP_1) return IRON_KEY_NUMPAD_1;
  559. if (symbol == XK_KP_2) return IRON_KEY_NUMPAD_2;
  560. if (symbol == XK_KP_3) return IRON_KEY_NUMPAD_3;
  561. if (symbol == XK_KP_4) return IRON_KEY_NUMPAD_4;
  562. if (symbol == XK_KP_5) return IRON_KEY_NUMPAD_5;
  563. if (symbol == XK_KP_6) return IRON_KEY_NUMPAD_6;
  564. if (symbol == XK_KP_7) return IRON_KEY_NUMPAD_7;
  565. if (symbol == XK_KP_8) return IRON_KEY_NUMPAD_8;
  566. if (symbol == XK_KP_9) return IRON_KEY_NUMPAD_9;
  567. if (symbol == XK_KP_Insert) return IRON_KEY_INSERT;
  568. if (symbol == XK_KP_Delete) return IRON_KEY_DELETE;
  569. if (symbol == XK_KP_End) return IRON_KEY_END;
  570. if (symbol == XK_KP_Home) return IRON_KEY_HOME;
  571. if (symbol == XK_KP_Left) return IRON_KEY_LEFT;
  572. if (symbol == XK_KP_Up) return IRON_KEY_UP;
  573. if (symbol == XK_KP_Right) return IRON_KEY_RIGHT;
  574. if (symbol == XK_KP_Down) return IRON_KEY_DOWN;
  575. if (symbol == XK_KP_Page_Up) return IRON_KEY_PAGE_UP;
  576. if (symbol == XK_KP_Page_Down) return IRON_KEY_PAGE_DOWN;
  577. if (symbol == XK_Menu) return IRON_KEY_CONTEXT_MENU;
  578. if (symbol == XK_a) return IRON_KEY_A;
  579. if (symbol == XK_b) return IRON_KEY_B;
  580. if (symbol == XK_c) return IRON_KEY_C;
  581. if (symbol == XK_d) return IRON_KEY_D;
  582. if (symbol == XK_e) return IRON_KEY_E;
  583. if (symbol == XK_f) return IRON_KEY_F;
  584. if (symbol == XK_g) return IRON_KEY_G;
  585. if (symbol == XK_h) return IRON_KEY_H;
  586. if (symbol == XK_i) return IRON_KEY_I;
  587. if (symbol == XK_j) return IRON_KEY_J;
  588. if (symbol == XK_k) return IRON_KEY_K;
  589. if (symbol == XK_l) return IRON_KEY_L;
  590. if (symbol == XK_m) return IRON_KEY_M;
  591. if (symbol == XK_n) return IRON_KEY_N;
  592. if (symbol == XK_o) return IRON_KEY_O;
  593. if (symbol == XK_p) return IRON_KEY_P;
  594. if (symbol == XK_q) return IRON_KEY_Q;
  595. if (symbol == XK_r) return IRON_KEY_R;
  596. if (symbol == XK_s) return IRON_KEY_S;
  597. if (symbol == XK_t) return IRON_KEY_T;
  598. if (symbol == XK_u) return IRON_KEY_U;
  599. if (symbol == XK_v) return IRON_KEY_V;
  600. if (symbol == XK_w) return IRON_KEY_W;
  601. if (symbol == XK_x) return IRON_KEY_X;
  602. if (symbol == XK_y) return IRON_KEY_Y;
  603. if (symbol == XK_z) return IRON_KEY_Z;
  604. if (symbol == XK_1) return IRON_KEY_1;
  605. if (symbol == XK_2) return IRON_KEY_2;
  606. if (symbol == XK_3) return IRON_KEY_3;
  607. if (symbol == XK_4) return IRON_KEY_4;
  608. if (symbol == XK_5) return IRON_KEY_5;
  609. if (symbol == XK_6) return IRON_KEY_6;
  610. if (symbol == XK_7) return IRON_KEY_7;
  611. if (symbol == XK_8) return IRON_KEY_8;
  612. if (symbol == XK_9) return IRON_KEY_9;
  613. if (symbol == XK_0) return IRON_KEY_0;
  614. if (symbol == XK_Escape) return IRON_KEY_ESCAPE;
  615. if (symbol == XK_F1) return IRON_KEY_F1;
  616. if (symbol == XK_F2) return IRON_KEY_F2;
  617. if (symbol == XK_F3) return IRON_KEY_F3;
  618. if (symbol == XK_F4) return IRON_KEY_F4;
  619. if (symbol == XK_F5) return IRON_KEY_F5;
  620. if (symbol == XK_F6) return IRON_KEY_F6;
  621. if (symbol == XK_F7) return IRON_KEY_F7;
  622. if (symbol == XK_F8) return IRON_KEY_F8;
  623. if (symbol == XK_F9) return IRON_KEY_F9;
  624. if (symbol == XK_F10) return IRON_KEY_F10;
  625. if (symbol == XK_F11) return IRON_KEY_F11;
  626. if (symbol == XK_F12) return IRON_KEY_F12;
  627. return IRON_KEY_UNKNOWN;
  628. }
  629. static bool _handle_messages() {
  630. static bool controlDown = false;
  631. static int ignoreKeycode = 0;
  632. static bool preventNextKeyDownEvent = false;
  633. while (xlib.XPending(x11_ctx.display)) {
  634. XEvent event;
  635. xlib.XNextEvent(x11_ctx.display, &event);
  636. Window window = event.xclient.window;
  637. struct iron_x11_window *k_window = window_from_window(window);
  638. if (k_window == NULL) {
  639. continue;
  640. }
  641. check_pen_device(k_window, &event, &x11_ctx.pen);
  642. check_pen_device(k_window, &event, &x11_ctx.eraser);
  643. switch (event.type) {
  644. case MappingNotify: {
  645. xlib.XRefreshKeyboardMapping(&event.xmapping);
  646. break;
  647. }
  648. case KeyPress: {
  649. XKeyEvent *key = (XKeyEvent *)&event;
  650. KeySym keysym;
  651. wchar_t wchar;
  652. bool wcConverted = xlib.XwcLookupString(k_window->xInputContext, key, &wchar, 1, &keysym, NULL);
  653. bool isIgnoredKeySym = keysym == XK_Escape || keysym == XK_BackSpace || keysym == XK_Delete;
  654. if (!controlDown && !xlib.XFilterEvent(&event, window) && !isIgnoredKeySym) {
  655. if (wcConverted) {
  656. iron_internal_keyboard_trigger_key_press(wchar);
  657. }
  658. }
  659. if (preventNextKeyDownEvent) {
  660. // this keypress is a repeated keystroke and should not lead to a keydown-event
  661. preventNextKeyDownEvent = false;
  662. continue;
  663. }
  664. KeySym ksKey = xlib.XkbKeycodeToKeysym(x11_ctx.display, event.xkey.keycode, 0, 0);
  665. if (ksKey == XK_Control_L || ksKey == XK_Control_R) {
  666. controlDown = true;
  667. }
  668. else if (controlDown && (ksKey == XK_v || ksKey == XK_V)) {
  669. xlib.XConvertSelection(x11_ctx.display, x11_ctx.atoms.CLIPBOARD, x11_ctx.atoms.UTF8_STRING, x11_ctx.atoms.XSEL_DATA, window, CurrentTime);
  670. }
  671. else if (controlDown && (ksKey == XK_c || ksKey == XK_C)) {
  672. xlib.XSetSelectionOwner(x11_ctx.display, x11_ctx.atoms.CLIPBOARD, window, CurrentTime);
  673. char *text = iron_internal_copy_callback();
  674. if (text != NULL)
  675. iron_copy_to_clipboard(text);
  676. }
  677. else if (controlDown && (ksKey == XK_x || ksKey == XK_X)) {
  678. xlib.XSetSelectionOwner(x11_ctx.display, x11_ctx.atoms.CLIPBOARD, window, CurrentTime);
  679. char *text = iron_internal_cut_callback();
  680. if (text != NULL)
  681. iron_copy_to_clipboard(text);
  682. }
  683. if (event.xkey.keycode == ignoreKeycode) {
  684. break;
  685. }
  686. else {
  687. ignoreKeycode = event.xkey.keycode;
  688. }
  689. if (ksKey < 97 || ksKey > 122) {
  690. ksKey = keysym;
  691. }
  692. int key_code = xk_to_iron(ksKey);
  693. if (key_code != IRON_KEY_UNKNOWN) {
  694. iron_internal_keyboard_trigger_key_down(key_code);
  695. }
  696. break;
  697. }
  698. case KeyRelease: {
  699. XKeyEvent *key = (XKeyEvent *)&event;
  700. // peek next-event to determine if this a repeated-keystroke
  701. XEvent nev;
  702. if (xlib.XPending(x11_ctx.display)) {
  703. xlib.XPeekEvent(x11_ctx.display, &nev);
  704. if (nev.type == KeyPress && nev.xkey.time == event.xkey.time && nev.xkey.keycode == event.xkey.keycode) {
  705. // repeated keystroke! prevent this keyup-event and next keydown-event from being fired
  706. preventNextKeyDownEvent = true;
  707. continue;
  708. }
  709. }
  710. KeySym keysym;
  711. char c;
  712. xlib.XLookupString(key, &c, 1, &keysym, NULL);
  713. KeySym ksKey = xlib.XkbKeycodeToKeysym(x11_ctx.display, event.xkey.keycode, 0, 0);
  714. if (ksKey == XK_Control_L || ksKey == XK_Control_R) {
  715. controlDown = false;
  716. }
  717. if (event.xkey.keycode == ignoreKeycode) {
  718. ignoreKeycode = 0;
  719. }
  720. if (ksKey < 97 || ksKey > 122) {
  721. ksKey = keysym;
  722. }
  723. int key_code = xk_to_iron(ksKey);
  724. if (key_code != IRON_KEY_UNKNOWN) {
  725. iron_internal_keyboard_trigger_key_up(key_code);
  726. }
  727. break;
  728. }
  729. case ButtonPress: {
  730. XButtonEvent *button = (XButtonEvent *)&event;
  731. switch (button->button) {
  732. case Button1:
  733. iron_internal_mouse_trigger_press(0, button->x, button->y);
  734. break;
  735. case Button2:
  736. iron_internal_mouse_trigger_press(2, button->x, button->y);
  737. break;
  738. case Button3:
  739. iron_internal_mouse_trigger_press(1, button->x, button->y);
  740. break;
  741. // buttons 4-7 are for mouse wheel events because why not
  742. case Button4:
  743. case Button5:
  744. case Button6:
  745. case Button7:
  746. break;
  747. default:
  748. iron_internal_mouse_trigger_press(button->button - Button1 - 4, button->x, button->y);
  749. break;
  750. }
  751. break;
  752. }
  753. case ButtonRelease: {
  754. XButtonEvent *button = (XButtonEvent *)&event;
  755. switch (button->button) {
  756. case Button1:
  757. iron_internal_mouse_trigger_release(0, button->x, button->y);
  758. break;
  759. case Button2:
  760. iron_internal_mouse_trigger_release(2, button->x, button->y);
  761. break;
  762. case Button3:
  763. iron_internal_mouse_trigger_release(1, button->x, button->y);
  764. break;
  765. // Button4 and Button5 provide mouse wheel events because why not
  766. case Button4:
  767. iron_internal_mouse_trigger_scroll(-1);
  768. break;
  769. case Button5:
  770. iron_internal_mouse_trigger_scroll(1);
  771. break;
  772. // button 6 and 7 seem to be horizontal scrolling, which is not exposed in Iron's api at the moment
  773. case Button6:
  774. case Button7:
  775. break;
  776. default:
  777. iron_internal_mouse_trigger_release(button->button - Button1 - 4, button->x, button->y);
  778. break;
  779. }
  780. break;
  781. }
  782. case MotionNotify: {
  783. XMotionEvent *motion = (XMotionEvent *)&event;
  784. iron_internal_mouse_trigger_move(motion->x, motion->y);
  785. break;
  786. }
  787. case ConfigureNotify: {
  788. if (event.xconfigure.width != k_window->width || event.xconfigure.height != k_window->height) {
  789. k_window->width = event.xconfigure.width;
  790. k_window->height = event.xconfigure.height;
  791. gpu_resize(event.xconfigure.width, event.xconfigure.height);
  792. iron_internal_call_resize_callback(event.xconfigure.width, event.xconfigure.height);
  793. }
  794. break;
  795. }
  796. case ClientMessage: {
  797. if (event.xclient.message_type == x11_ctx.atoms.XdndEnter) {
  798. Window source_window = event.xclient.data.l[0];
  799. XEvent m;
  800. memset(&m, 0, sizeof(m));
  801. m.type = ClientMessage;
  802. m.xclient.window = event.xclient.data.l[0];
  803. m.xclient.message_type = x11_ctx.atoms.XdndStatus;
  804. m.xclient.format = 32;
  805. m.xclient.data.l[0] = window;
  806. m.xclient.data.l[2] = 0;
  807. m.xclient.data.l[3] = 0;
  808. m.xclient.data.l[1] = 1;
  809. m.xclient.data.l[4] = x11_ctx.atoms.XdndActionCopy;
  810. xlib.XSendEvent(x11_ctx.display, source_window, false, NoEventMask, (XEvent *)&m);
  811. xlib.XFlush(x11_ctx.display);
  812. }
  813. else if (event.xclient.message_type == x11_ctx.atoms.XdndDrop) {
  814. xlib.XConvertSelection(x11_ctx.display, x11_ctx.atoms.XdndSelection, x11_ctx.atoms.XdndTextUriList, x11_ctx.atoms.XdndSelection, window,
  815. event.xclient.data.l[2]);
  816. }
  817. else if (event.xclient.data.l[0] == x11_ctx.atoms.WM_DELETE_WINDOW) {
  818. if (iron_internal_call_close_callback()) {
  819. iron_window_destroy();
  820. iron_stop();
  821. }
  822. }
  823. break;
  824. }
  825. case SelectionNotify: {
  826. if (event.xselection.selection == x11_ctx.atoms.CLIPBOARD) {
  827. char *result;
  828. unsigned long ressize, restail;
  829. int resbits;
  830. xlib.XGetWindowProperty(x11_ctx.display, window, x11_ctx.atoms.XSEL_DATA, 0, LONG_MAX / 4, False, AnyPropertyType, &x11_ctx.atoms.UTF8_STRING,
  831. &resbits, &ressize, &restail, (unsigned char **)&result);
  832. iron_internal_paste_callback(result);
  833. xlib.XFree(result);
  834. }
  835. else if (event.xselection.property == x11_ctx.atoms.XdndSelection) {
  836. Atom type;
  837. int format;
  838. unsigned long numItems;
  839. unsigned long bytesAfter = 1;
  840. unsigned char *data = 0;
  841. xlib.XGetWindowProperty(x11_ctx.display, event.xselection.requestor, event.xselection.property, 0, LONG_MAX, False, event.xselection.target,
  842. &type, &format, &numItems, &bytesAfter, &data);
  843. size_t pos = 0;
  844. size_t len = 0;
  845. while (pos < numItems) {
  846. if (data[pos] == '\r') { // Found a file
  847. wchar_t filePath[len + 1];
  848. mbstowcs(filePath, buffer, len);
  849. filePath[len] = 0;
  850. iron_internal_drop_files_callback(filePath + 7); // Strip file://
  851. pos += 2; // Avoid \n
  852. len = 0;
  853. }
  854. buffer[len++] = data[pos++];
  855. }
  856. xlib.XFree(data);
  857. }
  858. break;
  859. }
  860. case SelectionRequest: {
  861. if (event.xselectionrequest.target == x11_ctx.atoms.TARGETS) {
  862. XEvent send;
  863. send.xselection.type = SelectionNotify;
  864. send.xselection.requestor = event.xselectionrequest.requestor;
  865. send.xselection.selection = event.xselectionrequest.selection;
  866. send.xselection.target = event.xselectionrequest.target;
  867. send.xselection.property = event.xselectionrequest.property;
  868. send.xselection.time = event.xselectionrequest.time;
  869. Atom available[] = {x11_ctx.atoms.TARGETS, x11_ctx.atoms.MULTIPLE, x11_ctx.atoms.TEXT_PLAIN, x11_ctx.atoms.UTF8_STRING};
  870. xlib.XChangeProperty(x11_ctx.display, send.xselection.requestor, send.xselection.property, XA_ATOM, 32, PropModeReplace,
  871. (unsigned char *)&available[0], 4);
  872. xlib.XSendEvent(x11_ctx.display, send.xselection.requestor, True, 0, &send);
  873. }
  874. if (event.xselectionrequest.target == x11_ctx.atoms.TEXT_PLAIN || event.xselectionrequest.target == x11_ctx.atoms.UTF8_STRING) {
  875. XEvent send;
  876. send.xselection.type = SelectionNotify;
  877. send.xselection.requestor = event.xselectionrequest.requestor;
  878. send.xselection.selection = event.xselectionrequest.selection;
  879. send.xselection.target = event.xselectionrequest.target;
  880. send.xselection.property = event.xselectionrequest.property;
  881. send.xselection.time = event.xselectionrequest.time;
  882. xlib.XChangeProperty(x11_ctx.display, send.xselection.requestor, send.xselection.property, send.xselection.target, 8, PropModeReplace,
  883. (const unsigned char *)clipboardString, strlen(clipboardString));
  884. xlib.XSendEvent(x11_ctx.display, send.xselection.requestor, True, 0, &send);
  885. }
  886. break;
  887. }
  888. case Expose: {
  889. break;
  890. }
  891. case FocusIn: {
  892. iron_internal_foreground_callback();
  893. break;
  894. }
  895. case FocusOut: {
  896. controlDown = false;
  897. ignoreKeycode = 0;
  898. iron_internal_background_callback();
  899. break;
  900. }
  901. case LeaveNotify: {
  902. break;
  903. }
  904. case EnterNotify: {
  905. break;
  906. }
  907. }
  908. }
  909. return true;
  910. }
  911. bool iron_internal_handle_messages() {
  912. if (!_handle_messages()) {
  913. return false;
  914. }
  915. #ifdef WITH_GAMEPAD
  916. iron_linux_updateHIDGamepads();
  917. #endif
  918. return true;
  919. }
  920. const char *iron_system_id() {
  921. return "Linux";
  922. }
  923. void iron_set_keep_screen_on(bool on) {}
  924. void iron_keyboard_show() {}
  925. void iron_keyboard_hide() {}
  926. bool iron_keyboard_active() {
  927. return true;
  928. }
  929. void iron_load_url(const char *url) {
  930. if (strncmp(url, "http://", sizeof("http://") - 1) == 0 || strncmp(url, "https://", sizeof("https://") - 1) == 0) {
  931. char openUrlCommand[256];
  932. snprintf(openUrlCommand, 256, "xdg-open %s", url);
  933. int err = system(openUrlCommand);
  934. if (err != 0) {
  935. iron_log("Error opening url %s", url);
  936. }
  937. }
  938. }
  939. const char *iron_language() {
  940. return "en";
  941. }
  942. const char *iron_internal_save_path() {
  943. // first check for an existing directory in $HOME
  944. // if one exists, use it, else create one in $XDG_DATA_HOME
  945. // See: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
  946. if (!saveInitialized) {
  947. const char *homedir;
  948. if ((homedir = getenv("HOME")) == NULL) {
  949. homedir = getpwuid(getuid())->pw_dir;
  950. }
  951. strcpy(save, homedir);
  952. strcat(save, "/.");
  953. strcat(save, iron_application_name());
  954. strcat(save, "/");
  955. struct stat st;
  956. if (stat(save, &st) == 0) {
  957. // use existing folder in $HOME
  958. }
  959. else {
  960. // use XDG folder
  961. const char *data_home;
  962. if ((data_home = getenv("XDG_DATA_HOME")) == NULL) {
  963. // $XDG_DATA_HOME is not defined, fall back to the default, $HOME/.local/share
  964. strcpy(save, homedir);
  965. strcat(save, "/.local/share/");
  966. }
  967. else {
  968. // use $XDG_DATA_HOME
  969. strcpy(save, data_home);
  970. if (data_home[strlen(data_home) - 1] != '/') {
  971. strcat(save, "/");
  972. }
  973. }
  974. strcat(save, iron_application_name());
  975. strcat(save, "/");
  976. int res = mkdir(save, 0700);
  977. if (res != 0 && errno != EEXIST) {
  978. iron_error("Could not create save directory '%s'. Error %d", save, errno);
  979. }
  980. }
  981. saveInitialized = true;
  982. }
  983. return save;
  984. }
  985. const char **iron_video_formats() {
  986. return videoFormats;
  987. }
  988. double iron_frequency(void) {
  989. return 1000000.0;
  990. }
  991. uint64_t iron_timestamp(void) {
  992. struct timeval now;
  993. gettimeofday(&now, NULL);
  994. now.tv_sec -= start.tv_sec;
  995. now.tv_usec -= start.tv_usec;
  996. return (uint64_t)now.tv_sec * 1000000 + (uint64_t)now.tv_usec;
  997. }
  998. void iron_init(iron_window_options_t *win) {
  999. gettimeofday(&start, NULL);
  1000. #ifdef WITH_GAMEPAD
  1001. iron_linux_initHIDGamepads();
  1002. #endif
  1003. iron_x11_init();
  1004. iron_display_init();
  1005. iron_set_app_name(win->title);
  1006. iron_window_create(win);
  1007. }
  1008. void iron_internal_shutdown() {
  1009. gpu_destroy();
  1010. #ifdef WITH_GAMEPAD
  1011. iron_linux_closeHIDGamepads();
  1012. #endif
  1013. free(clipboardString);
  1014. xlib.XCloseDisplay(x11_ctx.display);
  1015. iron_internal_shutdown_callback();
  1016. }
  1017. #ifndef IRON_NO_MAIN
  1018. int main(int argc, char **argv) {
  1019. return kickstart(argc, argv);
  1020. }
  1021. #endif
  1022. void iron_copy_to_clipboard(const char *text) {
  1023. size_t textLength = strlen(text);
  1024. if (textLength >= clipboardStringSize) {
  1025. free(clipboardString);
  1026. clipboardStringSize = textLength + 1;
  1027. clipboardString = (char *)malloc(clipboardStringSize);
  1028. }
  1029. strcpy(clipboardString, text);
  1030. }
  1031. static int parse_number_at_end_of_line(char *line) {
  1032. char *end = &line[strlen(line) - 2];
  1033. int num = 0;
  1034. int multi = 1;
  1035. while (*end >= '0' && *end <= '9') {
  1036. num += (*end - '0') * multi;
  1037. multi *= 10;
  1038. --end;
  1039. }
  1040. return num;
  1041. }
  1042. int iron_hardware_threads(void) {
  1043. return sysconf(_SC_NPROCESSORS_ONLN);
  1044. }
  1045. void iron_internal_mouse_lock() {
  1046. iron_mouse_hide();
  1047. int width = iron_window_width();
  1048. int height = iron_window_height();
  1049. int x, y;
  1050. iron_mouse_get_position(&x, &y);
  1051. int newX = x;
  1052. int newY = y;
  1053. if (x < 0) {
  1054. newX -= x;
  1055. }
  1056. else if (x > width) {
  1057. newX -= x - width;
  1058. }
  1059. if (y < 0) {
  1060. newY -= y;
  1061. }
  1062. else if (y > height) {
  1063. newY -= y - height;
  1064. }
  1065. iron_mouse_set_position(newX, newY);
  1066. }
  1067. void iron_internal_mouse_unlock() {
  1068. iron_mouse_show();
  1069. }
  1070. bool iron_mouse_can_lock(void) {
  1071. return true;
  1072. }
  1073. void iron_mouse_show() {
  1074. struct iron_x11_window *window = &x11_ctx.windows[0];
  1075. if (mouse_hidden) {
  1076. xlib.XUndefineCursor(x11_ctx.display, window->window);
  1077. mouse_hidden = false;
  1078. }
  1079. }
  1080. void iron_mouse_hide() {
  1081. struct iron_x11_window *window = &x11_ctx.windows[0];
  1082. if (!mouse_hidden) {
  1083. XColor col;
  1084. col.pixel = 0;
  1085. col.red = 0;
  1086. col.green = 0;
  1087. col.blue = 0;
  1088. col.flags = DoRed | DoGreen | DoBlue;
  1089. col.pad = 0;
  1090. char data[1] = {'\0'};
  1091. Pixmap blank = xlib.XCreateBitmapFromData(x11_ctx.display, window->window, data, 1, 1);
  1092. Cursor cursor = xlib.XCreatePixmapCursor(x11_ctx.display, blank, blank, &col, &col, 0, 0);
  1093. xlib.XDefineCursor(x11_ctx.display, window->window, cursor);
  1094. xlib.XFreePixmap(x11_ctx.display, blank);
  1095. mouse_hidden = true;
  1096. }
  1097. }
  1098. void iron_mouse_set_cursor(int cursor_index) {
  1099. struct iron_x11_window *window = &x11_ctx.windows[0];
  1100. if (!mouse_hidden) {
  1101. Cursor cursor;
  1102. if (cursor_index == 0) {
  1103. cursor = xlib.XcursorLibraryLoadCursor(x11_ctx.display, "left_ptr"); // "arrow"
  1104. }
  1105. else if (cursor_index == 1) {
  1106. cursor = xlib.XcursorLibraryLoadCursor(x11_ctx.display, "hand1");
  1107. }
  1108. else if (cursor_index == 2) {
  1109. cursor = xlib.XcursorLibraryLoadCursor(x11_ctx.display, "xterm");
  1110. }
  1111. else if (cursor_index == 3) {
  1112. cursor = xlib.XcursorLibraryLoadCursor(x11_ctx.display, "sb_h_double_arrow");
  1113. }
  1114. else if (cursor_index == 4) {
  1115. cursor = xlib.XcursorLibraryLoadCursor(x11_ctx.display, "sb_v_double_arrow");
  1116. }
  1117. else if (cursor_index == 5) {
  1118. cursor = xlib.XcursorLibraryLoadCursor(x11_ctx.display, "top_right_corner");
  1119. }
  1120. else if (cursor_index == 6) {
  1121. cursor = xlib.XcursorLibraryLoadCursor(x11_ctx.display, "bottom_right_corner");
  1122. }
  1123. else if (cursor_index == 7) {
  1124. cursor = xlib.XcursorLibraryLoadCursor(x11_ctx.display, "top_left_corner");
  1125. }
  1126. else if (cursor_index == 8) {
  1127. cursor = xlib.XcursorLibraryLoadCursor(x11_ctx.display, "bottom_left_corner");
  1128. }
  1129. else if (cursor_index == 9) {
  1130. cursor = xlib.XcursorLibraryLoadCursor(x11_ctx.display, "grab");
  1131. }
  1132. else if (cursor_index == 10) {
  1133. cursor = xlib.XcursorLibraryLoadCursor(x11_ctx.display, "grabbing");
  1134. }
  1135. else if (cursor_index == 11) {
  1136. cursor = xlib.XcursorLibraryLoadCursor(x11_ctx.display, "not-allowed");
  1137. }
  1138. else if (cursor_index == 12) {
  1139. cursor = xlib.XcursorLibraryLoadCursor(x11_ctx.display, "watch");
  1140. }
  1141. else if (cursor_index == 13) {
  1142. cursor = xlib.XcursorLibraryLoadCursor(x11_ctx.display, "crosshair");
  1143. }
  1144. else {
  1145. cursor = xlib.XcursorLibraryLoadCursor(x11_ctx.display, "arrow");
  1146. }
  1147. xlib.XDefineCursor(x11_ctx.display, window->window, cursor);
  1148. }
  1149. }
  1150. void iron_mouse_set_position(int x, int y) {
  1151. struct iron_x11_window *window = &x11_ctx.windows[0];
  1152. xlib.XWarpPointer(x11_ctx.display, None, window->window, 0, 0, 0, 0, x, y);
  1153. xlib.XFlush(x11_ctx.display);
  1154. }
  1155. void iron_mouse_get_position(int *x, int *y) {
  1156. struct iron_x11_window *window = &x11_ctx.windows[0];
  1157. Window inwin;
  1158. Window inchildwin;
  1159. int rootx, rooty;
  1160. unsigned int mask;
  1161. xlib.XQueryPointer(x11_ctx.display, window->window, &inwin, &inchildwin, &rootx, &rooty, x, y, &mask);
  1162. }
  1163. #ifdef WITH_GAMEPAD
  1164. #include <fcntl.h>
  1165. #include <libudev.h>
  1166. #include <linux/joystick.h>
  1167. struct HIDGamepad {
  1168. int idx;
  1169. char gamepad_dev_name[256];
  1170. char name[385];
  1171. int file_descriptor;
  1172. bool connected;
  1173. struct js_event gamepadEvent;
  1174. };
  1175. static void HIDGamepad_open(struct HIDGamepad *pad) {
  1176. pad->file_descriptor = open(pad->gamepad_dev_name, O_RDONLY | O_NONBLOCK);
  1177. if (pad->file_descriptor < 0) {
  1178. pad->connected = false;
  1179. }
  1180. else {
  1181. pad->connected = true;
  1182. char buf[128];
  1183. if (ioctl(pad->file_descriptor, JSIOCGNAME(sizeof(buf)), buf) < 0) {
  1184. strncpy(buf, "Unknown", sizeof(buf));
  1185. }
  1186. pad->name[0] = 0;
  1187. // snprintf(pad->name, sizeof(pad->name), "%s(%s)", buf, pad->gamepad_dev_name); // TODO: valgrind error
  1188. iron_internal_gamepad_trigger_connect(pad->idx);
  1189. }
  1190. }
  1191. static void HIDGamepad_init(struct HIDGamepad *pad, int index) {
  1192. pad->file_descriptor = -1;
  1193. pad->connected = false;
  1194. pad->gamepad_dev_name[0] = 0;
  1195. if (index >= 0 && index < 12) {
  1196. pad->idx = index;
  1197. snprintf(pad->gamepad_dev_name, sizeof(pad->gamepad_dev_name), "/dev/input/js%d", pad->idx);
  1198. HIDGamepad_open(pad);
  1199. }
  1200. }
  1201. static void HIDGamepad_close(struct HIDGamepad *pad) {
  1202. if (pad->connected) {
  1203. iron_internal_gamepad_trigger_disconnect(pad->idx);
  1204. close(pad->file_descriptor);
  1205. pad->file_descriptor = -1;
  1206. pad->connected = false;
  1207. }
  1208. }
  1209. void HIDGamepad_processEvent(struct HIDGamepad *pad, struct js_event e) {
  1210. switch (e.type) {
  1211. case JS_EVENT_BUTTON:
  1212. iron_internal_gamepad_trigger_button(pad->idx, e.number, e.value);
  1213. break;
  1214. case JS_EVENT_AXIS: {
  1215. float value = e.number % 2 == 0 ? e.value : -e.value;
  1216. iron_internal_gamepad_trigger_axis(pad->idx, e.number, value / 32767.0f);
  1217. break;
  1218. }
  1219. default:
  1220. break;
  1221. }
  1222. }
  1223. void HIDGamepad_update(struct HIDGamepad *pad) {
  1224. if (pad->connected) {
  1225. while (read(pad->file_descriptor, &pad->gamepadEvent, sizeof(pad->gamepadEvent)) > 0) {
  1226. HIDGamepad_processEvent(pad, pad->gamepadEvent);
  1227. }
  1228. }
  1229. }
  1230. struct HIDGamepadUdevHelper {
  1231. struct udev *udevPtr;
  1232. struct udev_monitor *udevMonitorPtr;
  1233. int udevMonitorFD;
  1234. };
  1235. static struct HIDGamepadUdevHelper udev_helper;
  1236. static struct HIDGamepad gamepads[IRON_GAMEPAD_MAX_COUNT];
  1237. static void HIDGamepadUdevHelper_openOrCloseGamepad(struct HIDGamepadUdevHelper *helper, struct udev_device *dev) {
  1238. const char *action = udev_device_get_action(dev);
  1239. if (!action)
  1240. action = "add";
  1241. const char *joystickDevnodeName = strstr(udev_device_get_devnode(dev), "js");
  1242. if (joystickDevnodeName) {
  1243. int joystickDevnodeIndex;
  1244. sscanf(joystickDevnodeName, "js%d", &joystickDevnodeIndex);
  1245. if (!strcmp(action, "add")) {
  1246. HIDGamepad_open(&gamepads[joystickDevnodeIndex]);
  1247. }
  1248. if (!strcmp(action, "remove")) {
  1249. HIDGamepad_close(&gamepads[joystickDevnodeIndex]);
  1250. }
  1251. }
  1252. }
  1253. static void HIDGamepadUdevHelper_processDevice(struct HIDGamepadUdevHelper *helper, struct udev_device *dev) {
  1254. if (dev) {
  1255. if (udev_device_get_devnode(dev))
  1256. HIDGamepadUdevHelper_openOrCloseGamepad(helper, dev);
  1257. udev_device_unref(dev);
  1258. }
  1259. }
  1260. static void HIDGamepadUdevHelper_init(struct HIDGamepadUdevHelper *helper) {
  1261. struct udev *udevPtrNew = udev_new();
  1262. // enumerate
  1263. struct udev_enumerate *enumerate = udev_enumerate_new(udevPtrNew);
  1264. udev_enumerate_add_match_subsystem(enumerate, "input");
  1265. udev_enumerate_scan_devices(enumerate);
  1266. struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate);
  1267. struct udev_list_entry *entry;
  1268. udev_list_entry_foreach(entry, devices) {
  1269. const char *path = udev_list_entry_get_name(entry);
  1270. struct udev_device *dev = udev_device_new_from_syspath(udevPtrNew, path);
  1271. HIDGamepadUdevHelper_processDevice(helper, dev);
  1272. }
  1273. udev_enumerate_unref(enumerate);
  1274. // setup mon
  1275. helper->udevMonitorPtr = udev_monitor_new_from_netlink(udevPtrNew, "udev");
  1276. udev_monitor_filter_add_match_subsystem_devtype(helper->udevMonitorPtr, "input", NULL);
  1277. udev_monitor_enable_receiving(helper->udevMonitorPtr);
  1278. helper->udevMonitorFD = udev_monitor_get_fd(helper->udevMonitorPtr);
  1279. helper->udevPtr = udevPtrNew;
  1280. }
  1281. static void HIDGamepadUdevHelper_update(struct HIDGamepadUdevHelper *helper) {
  1282. fd_set fds;
  1283. FD_ZERO(&fds);
  1284. FD_SET(helper->udevMonitorFD, &fds);
  1285. if (FD_ISSET(helper->udevMonitorFD, &fds)) {
  1286. struct udev_device *dev = udev_monitor_receive_device(helper->udevMonitorPtr);
  1287. HIDGamepadUdevHelper_processDevice(helper, dev);
  1288. }
  1289. }
  1290. static void HIDGamepadUdevHelper_close(struct HIDGamepadUdevHelper *helper) {
  1291. udev_unref(helper->udevPtr);
  1292. }
  1293. void iron_linux_initHIDGamepads() {
  1294. for (int i = 0; i < IRON_GAMEPAD_MAX_COUNT; ++i) {
  1295. HIDGamepad_init(&gamepads[i], i);
  1296. }
  1297. HIDGamepadUdevHelper_init(&udev_helper);
  1298. }
  1299. void iron_linux_updateHIDGamepads() {
  1300. HIDGamepadUdevHelper_update(&udev_helper);
  1301. for (int i = 0; i < IRON_GAMEPAD_MAX_COUNT; ++i) {
  1302. HIDGamepad_update(&gamepads[i]);
  1303. }
  1304. }
  1305. void iron_linux_closeHIDGamepads() {
  1306. HIDGamepadUdevHelper_close(&udev_helper);
  1307. }
  1308. const char *iron_gamepad_vendor(int gamepad) {
  1309. return "Linux gamepad";
  1310. }
  1311. const char *iron_gamepad_product_name(int gamepad) {
  1312. return gamepad >= 0 && gamepad < IRON_GAMEPAD_MAX_COUNT ? gamepads[gamepad].name : "";
  1313. }
  1314. bool iron_gamepad_connected(int gamepad) {
  1315. return gamepad >= 0 && gamepad < IRON_GAMEPAD_MAX_COUNT && gamepads[gamepad].connected;
  1316. }
  1317. void iron_gamepad_rumble(int gamepad, float left, float right) {}
  1318. #endif