yutani.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2014-2018 K. Lange
  5. *
  6. * Yutani Client Library
  7. *
  8. * Client library for the compositing window system.
  9. */
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <syscall.h>
  13. #include <toaru/pex.h>
  14. #include <toaru/graphics.h>
  15. #include <toaru/kbd.h>
  16. #include <toaru/hashmap.h>
  17. #include <toaru/list.h>
  18. #include <toaru/yutani.h>
  19. #include <toaru/yutani-internal.h>
  20. #include <toaru/mouse.h>
  21. /* We need the flags but don't want the library dep (maybe the flags should be here?) */
  22. #include <toaru/./decorations.h>
  23. /**
  24. * yutani_wait_for
  25. *
  26. * Wait for a particular kind of message, queuing other types
  27. * of messages for processing later.
  28. */
  29. yutani_msg_t * yutani_wait_for(yutani_t * y, uint32_t type) {
  30. do {
  31. yutani_msg_t * out;
  32. size_t size;
  33. {
  34. char tmp[MAX_PACKET_SIZE];
  35. size = pex_recv(y->sock, tmp);
  36. out = malloc(size);
  37. memcpy(out, tmp, size);
  38. }
  39. if (out->type == type) {
  40. return out;
  41. } else {
  42. list_insert(y->queued, out);
  43. }
  44. } while (1); /* XXX: (!y->abort) */
  45. }
  46. /**
  47. * yutani_query
  48. *
  49. * Check if there is an available message, either in the
  50. * internal queue or directly from the server interface.
  51. */
  52. size_t yutani_query(yutani_t * y) {
  53. if (y->queued->length > 0) return 1;
  54. return pex_query(y->sock);
  55. }
  56. /**
  57. * _handle_internal
  58. *
  59. * Some messages are processed internally. They are still
  60. * available to the client application, but some work will
  61. * be done before they are handed off.
  62. *
  63. * WELCOME: Update the display_width and display_height for the connection.
  64. * WINDOW_MOVE: Update the window location.
  65. */
  66. static void _handle_internal(yutani_t * y, yutani_msg_t * out) {
  67. switch (out->type) {
  68. case YUTANI_MSG_WELCOME:
  69. {
  70. struct yutani_msg_welcome * mw = (void *)out->data;
  71. y->display_width = mw->display_width;
  72. y->display_height = mw->display_height;
  73. }
  74. break;
  75. case YUTANI_MSG_WINDOW_MOVE:
  76. {
  77. struct yutani_msg_window_move * wm = (void *)out->data;
  78. yutani_window_t * win = hashmap_get(y->windows, (void *)wm->wid);
  79. if (win) {
  80. win->x = wm->x;
  81. win->y = wm->y;
  82. }
  83. }
  84. break;
  85. case YUTANI_MSG_RESIZE_OFFER:
  86. {
  87. struct yutani_msg_window_resize * wr = (void *)out->data;
  88. yutani_window_t * win = hashmap_get(y->windows, (void *)wr->wid);
  89. if (win) {
  90. win->decorator_flags &= ~(DECOR_FLAG_TILED);
  91. win->decorator_flags |= (wr->flags & YUTANI_RESIZE_TILED) << 2;
  92. }
  93. }
  94. default:
  95. break;
  96. }
  97. }
  98. /**
  99. * yutani_poll
  100. *
  101. * Wait for a message to be available, processing it if
  102. * it has internal processing requirements.
  103. */
  104. yutani_msg_t * yutani_poll(yutani_t * y) {
  105. yutani_msg_t * out;
  106. if (y->queued->length > 0) {
  107. node_t * node = list_dequeue(y->queued);
  108. out = (yutani_msg_t *)node->value;
  109. free(node);
  110. _handle_internal(y, out);
  111. return out;
  112. }
  113. size_t size;
  114. {
  115. char tmp[MAX_PACKET_SIZE];
  116. size = pex_recv(y->sock, tmp);
  117. out = malloc(size);
  118. memcpy(out, tmp, size);
  119. }
  120. _handle_internal(y, out);
  121. return out;
  122. }
  123. /**
  124. * yutani_poll_async
  125. *
  126. * Get the next available message, if there is one, otherwise
  127. * return immediately. Generally should be called in a loop
  128. * after an initial call to yutani_poll in case processing
  129. * caused additional messages to be queued.
  130. */
  131. yutani_msg_t * yutani_poll_async(yutani_t * y) {
  132. if (yutani_query(y) > 0) {
  133. return yutani_poll(y);
  134. }
  135. return NULL;
  136. }
  137. void yutani_msg_buildx_hello(yutani_msg_t * msg) {
  138. msg->magic = YUTANI_MSG__MAGIC;
  139. msg->type = YUTANI_MSG_HELLO;
  140. msg->size = sizeof(struct yutani_message);
  141. }
  142. void yutani_msg_buildx_flip(yutani_msg_t * msg, yutani_wid_t wid) {
  143. msg->magic = YUTANI_MSG__MAGIC;
  144. msg->type = YUTANI_MSG_FLIP;
  145. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_flip);
  146. struct yutani_msg_flip * mw = (void *)msg->data;
  147. mw->wid = wid;
  148. }
  149. void yutani_msg_buildx_welcome(yutani_msg_t * msg, uint32_t width, uint32_t height) {
  150. msg->magic = YUTANI_MSG__MAGIC;
  151. msg->type = YUTANI_MSG_WELCOME;
  152. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_welcome);
  153. struct yutani_msg_welcome * mw = (void *)msg->data;
  154. mw->display_width = width;
  155. mw->display_height = height;
  156. }
  157. void yutani_msg_buildx_window_new(yutani_msg_t * msg, uint32_t width, uint32_t height) {
  158. msg->magic = YUTANI_MSG__MAGIC;
  159. msg->type = YUTANI_MSG_WINDOW_NEW;
  160. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_window_new);
  161. struct yutani_msg_window_new * mw = (void *)msg->data;
  162. mw->width = width;
  163. mw->height = height;
  164. }
  165. void yutani_msg_buildx_window_new_flags(yutani_msg_t * msg, uint32_t width, uint32_t height, uint32_t flags) {
  166. msg->magic = YUTANI_MSG__MAGIC;
  167. msg->type = YUTANI_MSG_WINDOW_NEW_FLAGS;
  168. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_window_new_flags);
  169. struct yutani_msg_window_new_flags * mw = (void *)msg->data;
  170. mw->width = width;
  171. mw->height = height;
  172. mw->flags = flags;
  173. }
  174. void yutani_msg_buildx_window_init(yutani_msg_t * msg, yutani_wid_t wid, uint32_t width, uint32_t height, uint32_t bufid) {
  175. msg->magic = YUTANI_MSG__MAGIC;
  176. msg->type = YUTANI_MSG_WINDOW_INIT;
  177. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_window_init);
  178. struct yutani_msg_window_init * mw = (void *)msg->data;
  179. mw->wid = wid;
  180. mw->width = width;
  181. mw->height = height;
  182. mw->bufid = bufid;
  183. }
  184. void yutani_msg_buildx_window_close(yutani_msg_t * msg, yutani_wid_t wid) {
  185. msg->magic = YUTANI_MSG__MAGIC;
  186. msg->type = YUTANI_MSG_WINDOW_CLOSE;
  187. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_window_close);
  188. struct yutani_msg_window_close * mw = (void *)msg->data;
  189. mw->wid = wid;
  190. }
  191. void yutani_msg_buildx_key_event(yutani_msg_t * msg, yutani_wid_t wid, key_event_t * event, key_event_state_t * state) {
  192. msg->magic = YUTANI_MSG__MAGIC;
  193. msg->type = YUTANI_MSG_KEY_EVENT;
  194. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_key_event);
  195. struct yutani_msg_key_event * mw = (void *)msg->data;
  196. mw->wid = wid;
  197. memcpy(&mw->event, event, sizeof(key_event_t));
  198. memcpy(&mw->state, state, sizeof(key_event_state_t));
  199. }
  200. void yutani_msg_buildx_mouse_event(yutani_msg_t * msg, yutani_wid_t wid, mouse_device_packet_t * event, int32_t type) {
  201. msg->magic = YUTANI_MSG__MAGIC;
  202. msg->type = YUTANI_MSG_MOUSE_EVENT;
  203. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_mouse_event);
  204. struct yutani_msg_mouse_event * mw = (void *)msg->data;
  205. mw->wid = wid;
  206. memcpy(&mw->event, event, sizeof(mouse_device_packet_t));
  207. mw->type = type;
  208. }
  209. void yutani_msg_buildx_window_move(yutani_msg_t * msg, yutani_wid_t wid, int32_t x, int32_t y) {
  210. msg->magic = YUTANI_MSG__MAGIC;
  211. msg->type = YUTANI_MSG_WINDOW_MOVE;
  212. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_window_move);
  213. struct yutani_msg_window_move * mw = (void *)msg->data;
  214. mw->wid = wid;
  215. mw->x = x;
  216. mw->y = y;
  217. }
  218. void yutani_msg_buildx_window_stack(yutani_msg_t * msg, yutani_wid_t wid, int z) {
  219. msg->magic = YUTANI_MSG__MAGIC;
  220. msg->type = YUTANI_MSG_WINDOW_STACK;
  221. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_window_stack);
  222. struct yutani_msg_window_stack * mw = (void *)msg->data;
  223. mw->wid = wid;
  224. mw->z = z;
  225. }
  226. void yutani_msg_buildx_window_focus_change(yutani_msg_t * msg, yutani_wid_t wid, int focused) {
  227. msg->magic = YUTANI_MSG__MAGIC;
  228. msg->type = YUTANI_MSG_WINDOW_FOCUS_CHANGE;
  229. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_window_focus_change);
  230. struct yutani_msg_window_focus_change * mw = (void *)msg->data;
  231. mw->wid = wid;
  232. mw->focused = focused;
  233. }
  234. void yutani_msg_buildx_window_mouse_event(yutani_msg_t * msg, yutani_wid_t wid, int32_t new_x, int32_t new_y, int32_t old_x, int32_t old_y, uint8_t buttons, uint8_t command) {
  235. msg->magic = YUTANI_MSG__MAGIC;
  236. msg->type = YUTANI_MSG_WINDOW_MOUSE_EVENT;
  237. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_window_mouse_event);
  238. struct yutani_msg_window_mouse_event * mw = (void *)msg->data;
  239. mw->wid = wid;
  240. mw->new_x = new_x;
  241. mw->new_y = new_y;
  242. mw->old_x = old_x;
  243. mw->old_y = old_y;
  244. mw->buttons = buttons;
  245. mw->command = command;
  246. }
  247. void yutani_msg_buildx_flip_region(yutani_msg_t * msg, yutani_wid_t wid, int32_t x, int32_t y, int32_t width, int32_t height) {
  248. msg->magic = YUTANI_MSG__MAGIC;
  249. msg->type = YUTANI_MSG_FLIP_REGION;
  250. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_flip_region);
  251. struct yutani_msg_flip_region * mw = (void *)msg->data;
  252. mw->wid = wid;
  253. mw->x = x;
  254. mw->y = y;
  255. mw->width = width;
  256. mw->height = height;
  257. }
  258. void yutani_msg_buildx_window_resize(yutani_msg_t * msg, uint32_t type, yutani_wid_t wid, uint32_t width, uint32_t height, uint32_t bufid, uint32_t flags) {
  259. msg->magic = YUTANI_MSG__MAGIC;
  260. msg->type = type;
  261. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_window_resize);
  262. struct yutani_msg_window_resize * mw = (void *)msg->data;
  263. mw->wid = wid;
  264. mw->width = width;
  265. mw->height = height;
  266. mw->bufid = bufid;
  267. mw->flags = flags;
  268. }
  269. void yutani_msg_buildx_window_advertise(yutani_msg_t * msg, yutani_wid_t wid, uint32_t flags, uint16_t * offsets, size_t length, char * data) {
  270. msg->magic = YUTANI_MSG__MAGIC;
  271. msg->type = YUTANI_MSG_WINDOW_ADVERTISE;
  272. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_window_advertise) + length;
  273. struct yutani_msg_window_advertise * mw = (void *)msg->data;
  274. mw->wid = wid;
  275. mw->flags = flags;
  276. mw->size = length;
  277. if (offsets) {
  278. memcpy(mw->offsets, offsets, sizeof(uint16_t)*5);
  279. } else {
  280. memset(mw->offsets, 0, sizeof(uint16_t)*5);
  281. }
  282. if (data) {
  283. memcpy(mw->strings, data, mw->size);
  284. }
  285. }
  286. void yutani_msg_buildx_subscribe(yutani_msg_t * msg) {
  287. msg->magic = YUTANI_MSG__MAGIC;
  288. msg->type = YUTANI_MSG_SUBSCRIBE;
  289. msg->size = sizeof(struct yutani_message);
  290. }
  291. void yutani_msg_buildx_unsubscribe(yutani_msg_t * msg) {
  292. msg->magic = YUTANI_MSG__MAGIC;
  293. msg->type = YUTANI_MSG_UNSUBSCRIBE;
  294. msg->size = sizeof(struct yutani_message);
  295. }
  296. void yutani_msg_buildx_query_windows(yutani_msg_t * msg) {
  297. msg->magic = YUTANI_MSG__MAGIC;
  298. msg->type = YUTANI_MSG_QUERY_WINDOWS;
  299. msg->size = sizeof(struct yutani_message);
  300. }
  301. void yutani_msg_buildx_notify(yutani_msg_t * msg) {
  302. msg->magic = YUTANI_MSG__MAGIC;
  303. msg->type = YUTANI_MSG_NOTIFY;
  304. msg->size = sizeof(struct yutani_message);
  305. }
  306. void yutani_msg_buildx_session_end(yutani_msg_t * msg) {
  307. msg->magic = YUTANI_MSG__MAGIC;
  308. msg->type = YUTANI_MSG_SESSION_END;
  309. msg->size = sizeof(struct yutani_message);
  310. }
  311. void yutani_msg_buildx_window_focus(yutani_msg_t * msg, yutani_wid_t wid) {
  312. msg->magic = YUTANI_MSG__MAGIC;
  313. msg->type = YUTANI_MSG_WINDOW_FOCUS;
  314. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_window_focus);
  315. struct yutani_msg_window_focus * mw = (void *)msg->data;
  316. mw->wid = wid;
  317. }
  318. void yutani_msg_buildx_key_bind(yutani_msg_t * msg, kbd_key_t key, kbd_mod_t mod, int response) {
  319. msg->magic = YUTANI_MSG__MAGIC;
  320. msg->type = YUTANI_MSG_KEY_BIND;
  321. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_key_bind);
  322. struct yutani_msg_key_bind * mw = (void *)msg->data;
  323. mw->key = key;
  324. mw->modifiers = mod;
  325. mw->response = response;
  326. }
  327. void yutani_msg_buildx_window_drag_start(yutani_msg_t * msg, yutani_wid_t wid) {
  328. msg->magic = YUTANI_MSG__MAGIC;
  329. msg->type = YUTANI_MSG_WINDOW_DRAG_START;
  330. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_window_drag_start);
  331. struct yutani_msg_window_drag_start * mw = (void *)msg->data;
  332. mw->wid = wid;
  333. }
  334. void yutani_msg_buildx_window_update_shape(yutani_msg_t * msg, yutani_wid_t wid, int set_shape) {
  335. msg->magic = YUTANI_MSG__MAGIC;
  336. msg->type = YUTANI_MSG_WINDOW_UPDATE_SHAPE;
  337. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_window_update_shape);
  338. struct yutani_msg_window_update_shape * mw = (void *)msg->data;
  339. mw->wid = wid;
  340. mw->set_shape = set_shape;
  341. }
  342. void yutani_msg_buildx_window_warp_mouse(yutani_msg_t * msg, yutani_wid_t wid, int32_t x, int32_t y) {
  343. msg->magic = YUTANI_MSG__MAGIC;
  344. msg->type = YUTANI_MSG_WINDOW_WARP_MOUSE;
  345. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_window_warp_mouse);
  346. struct yutani_msg_window_warp_mouse * mw = (void *)msg->data;
  347. mw->wid = wid;
  348. mw->x = x;
  349. mw->y = y;
  350. }
  351. void yutani_msg_buildx_window_show_mouse(yutani_msg_t * msg, yutani_wid_t wid, int32_t show_mouse) {
  352. msg->magic = YUTANI_MSG__MAGIC;
  353. msg->type = YUTANI_MSG_WINDOW_SHOW_MOUSE;
  354. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_window_show_mouse);
  355. struct yutani_msg_window_show_mouse * mw = (void *)msg->data;
  356. mw->wid = wid;
  357. mw->show_mouse = show_mouse;
  358. }
  359. void yutani_msg_buildx_window_resize_start(yutani_msg_t * msg, yutani_wid_t wid, yutani_scale_direction_t direction) {
  360. msg->magic = YUTANI_MSG__MAGIC;
  361. msg->type = YUTANI_MSG_WINDOW_RESIZE_START;
  362. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_window_resize_start);
  363. struct yutani_msg_window_resize_start * mw = (void *)msg->data;
  364. mw->wid = wid;
  365. mw->direction = direction;
  366. }
  367. void yutani_msg_buildx_special_request(yutani_msg_t * msg, yutani_wid_t wid, uint32_t request) {
  368. msg->magic = YUTANI_MSG__MAGIC;
  369. msg->type = YUTANI_MSG_SPECIAL_REQUEST;
  370. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_special_request);
  371. struct yutani_msg_special_request * sr = (void *)msg->data;
  372. sr->wid = wid;
  373. sr->request = request;
  374. }
  375. void yutani_msg_buildx_clipboard(yutani_msg_t * msg, char * content) {
  376. msg->magic = YUTANI_MSG__MAGIC;
  377. msg->type = YUTANI_MSG_CLIPBOARD;
  378. msg->size = sizeof(struct yutani_message) + sizeof(struct yutani_msg_clipboard) + strlen(content);
  379. struct yutani_msg_clipboard * cl = (void *)msg->data;
  380. cl->size = strlen(content);
  381. memcpy(cl->content, content, strlen(content));
  382. }
  383. int yutani_msg_send(yutani_t * y, yutani_msg_t * msg) {
  384. return pex_reply(y->sock, msg->size, (char *)msg);
  385. }
  386. yutani_t * yutani_context_create(FILE * socket) {
  387. yutani_t * out = malloc(sizeof(yutani_t));
  388. out->sock = socket;
  389. out->display_width = 0;
  390. out->display_height = 0;
  391. out->windows = hashmap_create_int(10);
  392. out->queued = list_create();
  393. return out;
  394. }
  395. /**
  396. * yutani_init
  397. *
  398. * Connect to the compositor.
  399. *
  400. * Connects and handles the initial welcome message.
  401. */
  402. yutani_t * yutani_init(void) {
  403. char * server_name = getenv("DISPLAY");
  404. if (!server_name) {
  405. server_name = "compositor";
  406. }
  407. FILE * c = pex_connect(server_name);
  408. if (!c) {
  409. return NULL; /* Connection failed. */
  410. }
  411. yutani_t * y = yutani_context_create(c);
  412. yutani_msg_buildx_hello_alloc(m);
  413. yutani_msg_buildx_hello(m);
  414. yutani_msg_send(y, m);
  415. yutani_msg_t * mm = yutani_wait_for(y, YUTANI_MSG_WELCOME);
  416. struct yutani_msg_welcome * mw = (void *)&mm->data;
  417. y->display_width = mw->display_width;
  418. y->display_height = mw->display_height;
  419. y->server_ident = server_name;
  420. free(mm);
  421. return y;
  422. }
  423. /**
  424. * yutani_window_create_flags
  425. *
  426. * Create a window with certain pre-specified properties.
  427. */
  428. yutani_window_t * yutani_window_create_flags(yutani_t * y, int width, int height, uint32_t flags) {
  429. yutani_window_t * win = malloc(sizeof(yutani_window_t));
  430. yutani_msg_buildx_window_new_flags_alloc(m);
  431. yutani_msg_buildx_window_new_flags(m, width, height, flags);
  432. yutani_msg_send(y, m);
  433. yutani_msg_t * mm = yutani_wait_for(y, YUTANI_MSG_WINDOW_INIT);
  434. struct yutani_msg_window_init * mw = (void *)&mm->data;
  435. win->width = mw->width;
  436. win->height = mw->height;
  437. win->bufid = mw->bufid;
  438. win->wid = mw->wid;
  439. win->focused = 0;
  440. win->decorator_flags = 0;
  441. win->x = 0;
  442. win->y = 0;
  443. win->user_data = NULL;
  444. win->ctx = y;
  445. free(mm);
  446. hashmap_set(y->windows, (void*)win->wid, win);
  447. char key[1024];
  448. YUTANI_SHMKEY(y->server_ident, key, 1024, win);
  449. size_t size = (width * height * 4);
  450. win->buffer = (char *)syscall_shm_obtain(key, &size);
  451. return win;
  452. }
  453. /**
  454. * yutani_window_create
  455. *
  456. * Create a basic window.
  457. */
  458. yutani_window_t * yutani_window_create(yutani_t * y, int width, int height) {
  459. return yutani_window_create_flags(y,width,height,0);
  460. }
  461. /**
  462. * yutani_flip
  463. *
  464. * Ask the server to redraw the window.
  465. */
  466. void yutani_flip(yutani_t * y, yutani_window_t * win) {
  467. yutani_msg_buildx_flip_alloc(m);
  468. yutani_msg_buildx_flip(m, win->wid);
  469. yutani_msg_send(y, m);
  470. }
  471. /**
  472. * yutani_flip_region
  473. *
  474. * Ask the server to redraw a region relative the window.
  475. */
  476. void yutani_flip_region(yutani_t * yctx, yutani_window_t * win, int32_t x, int32_t y, int32_t width, int32_t height) {
  477. yutani_msg_buildx_flip_region_alloc(m);
  478. yutani_msg_buildx_flip_region(m, win->wid, x, y, width, height);
  479. yutani_msg_send(yctx, m);
  480. }
  481. /**
  482. * yutani_close
  483. *
  484. * Close a window. A closed window should not be used again,
  485. * and its associated buffers will be freed.
  486. */
  487. void yutani_close(yutani_t * y, yutani_window_t * win) {
  488. yutani_msg_buildx_window_close_alloc(m);
  489. yutani_msg_buildx_window_close(m, win->wid);
  490. yutani_msg_send(y, m);
  491. /* Now destroy our end of the window */
  492. {
  493. char key[1024];
  494. YUTANI_SHMKEY_EXP(y->server_ident, key, 1024, win->bufid);
  495. syscall_shm_release(key);
  496. }
  497. hashmap_remove(y->windows, (void*)win->wid);
  498. free(win);
  499. }
  500. /**
  501. * yutani_window_move
  502. *
  503. * Request a window be moved to new a location on screen.
  504. */
  505. void yutani_window_move(yutani_t * yctx, yutani_window_t * window, int x, int y) {
  506. yutani_msg_buildx_window_move_alloc(m);
  507. yutani_msg_buildx_window_move(m, window->wid, x, y);
  508. yutani_msg_send(yctx, m);
  509. }
  510. /**
  511. * yutani_set_stack
  512. *
  513. * Set the stacking order of the window.
  514. */
  515. void yutani_set_stack(yutani_t * yctx, yutani_window_t * window, int z) {
  516. yutani_msg_buildx_window_stack_alloc(m);
  517. yutani_msg_buildx_window_stack(m, window->wid, z);
  518. yutani_msg_send(yctx, m);
  519. }
  520. /**
  521. * yutani_window_resize
  522. *
  523. * Request that the server resize a window.
  524. */
  525. void yutani_window_resize(yutani_t * yctx, yutani_window_t * window, uint32_t width, uint32_t height) {
  526. yutani_msg_buildx_window_resize_alloc(m);
  527. yutani_msg_buildx_window_resize(m, YUTANI_MSG_RESIZE_REQUEST, window->wid, width, height, 0, 0);
  528. yutani_msg_send(yctx, m);
  529. }
  530. /**
  531. * yutani_window_resize_offer
  532. *
  533. * In a response to a server resize message, offer an alternative size.
  534. * Allows the client to reject a user-provided resize request due to
  535. * size constraints or other reasons.
  536. */
  537. void yutani_window_resize_offer(yutani_t * yctx, yutani_window_t * window, uint32_t width, uint32_t height) {
  538. yutani_msg_buildx_window_resize_alloc(m);
  539. yutani_msg_buildx_window_resize(m, YUTANI_MSG_RESIZE_OFFER, window->wid, width, height, 0, 0);
  540. yutani_msg_send(yctx, m);
  541. }
  542. /**
  543. * yutani_window_resize_accept
  544. *
  545. * Accept the server's resize request, initialize new buffers
  546. * and all the client to draw into the new buffers.
  547. */
  548. void yutani_window_resize_accept(yutani_t * yctx, yutani_window_t * window, uint32_t width, uint32_t height) {
  549. yutani_msg_buildx_window_resize_alloc(m);
  550. yutani_msg_buildx_window_resize(m, YUTANI_MSG_RESIZE_ACCEPT, window->wid, width, height, 0, 0);
  551. yutani_msg_send(yctx, m);
  552. /* Now wait for the new bufid */
  553. yutani_msg_t * mm = yutani_wait_for(yctx, YUTANI_MSG_RESIZE_BUFID);
  554. struct yutani_msg_window_resize * wr = (void*)mm->data;
  555. if (window->wid != wr->wid) {
  556. /* I am not sure what to do here. */
  557. return;
  558. }
  559. /* Update the window */
  560. window->width = wr->width;
  561. window->height = wr->height;
  562. window->oldbufid = window->bufid;
  563. window->bufid = wr->bufid;
  564. free(mm);
  565. /* Allocate the buffer */
  566. {
  567. char key[1024];
  568. YUTANI_SHMKEY(yctx->server_ident, key, 1024, window);
  569. size_t size = (window->width * window->height * 4);
  570. window->buffer = (char *)syscall_shm_obtain(key, &size);
  571. }
  572. }
  573. /**
  574. * yutani_window_resize_done
  575. *
  576. * The client has finished drawing into the new buffers after
  577. * accepting a resize request and the server should now
  578. * discard the old buffer and switch to the new one.
  579. */
  580. void yutani_window_resize_done(yutani_t * yctx, yutani_window_t * window) {
  581. /* Destroy the old buffer */
  582. {
  583. char key[1024];
  584. YUTANI_SHMKEY_EXP(yctx->server_ident, key, 1024, window->oldbufid);
  585. syscall_shm_release(key);
  586. }
  587. yutani_msg_buildx_window_resize_alloc(m);
  588. yutani_msg_buildx_window_resize(m, YUTANI_MSG_RESIZE_DONE, window->wid, window->width, window->height, window->bufid, 0);
  589. yutani_msg_send(yctx, m);
  590. }
  591. /**
  592. * yutani_window_advertise
  593. *
  594. * Provide a title for a window to have it show up
  595. * in the panel window list.
  596. */
  597. void yutani_window_advertise(yutani_t * yctx, yutani_window_t * window, char * name) {
  598. uint32_t flags = 0; /* currently, no client flags */
  599. uint16_t offsets[5] = {0,0,0,0,0};
  600. uint32_t length = 0;
  601. char * strings;
  602. if (!name) {
  603. length = 1;
  604. strings = " ";
  605. } else {
  606. length = strlen(name) + 1;
  607. strings = name;
  608. /* All the other offsets will point to null characters */
  609. offsets[1] = strlen(name);
  610. offsets[2] = strlen(name);
  611. offsets[3] = strlen(name);
  612. offsets[4] = strlen(name);
  613. }
  614. yutani_msg_buildx_window_advertise_alloc(m, length);
  615. yutani_msg_buildx_window_advertise(m, window->wid, flags, offsets, length, strings);
  616. yutani_msg_send(yctx, m);
  617. }
  618. /**
  619. * yutani_window_advertise_icon
  620. *
  621. * Provide a title and an icon for the panel to show.
  622. *
  623. * Note that three additional fields are available in the advertisement
  624. * messages which are not yet used. This is to allow for future expansion.
  625. */
  626. void yutani_window_advertise_icon(yutani_t * yctx, yutani_window_t * window, char * name, char * icon) {
  627. uint32_t flags = 0; /* currently no client flags */
  628. uint16_t offsets[5] = {0,0,0,0,0};
  629. uint32_t length = strlen(name) + strlen(icon) + 2;
  630. char * strings = malloc(length);
  631. if (name) {
  632. memcpy(&strings[0], name, strlen(name)+1);
  633. offsets[0] = 0;
  634. offsets[1] = strlen(name);
  635. offsets[2] = strlen(name);
  636. offsets[3] = strlen(name);
  637. offsets[4] = strlen(name);
  638. }
  639. if (icon) {
  640. memcpy(&strings[offsets[1]+1], icon, strlen(icon)+1);
  641. offsets[1] = strlen(name)+1;
  642. offsets[2] = strlen(name)+1+strlen(icon);
  643. offsets[3] = strlen(name)+1+strlen(icon);
  644. offsets[4] = strlen(name)+1+strlen(icon);
  645. }
  646. yutani_msg_buildx_window_advertise_alloc(m, length);
  647. yutani_msg_buildx_window_advertise(m, window->wid, flags, offsets, length, strings);
  648. yutani_msg_send(yctx, m);
  649. free(strings);
  650. }
  651. /**
  652. * yutani_subscribe_windows
  653. *
  654. * Subscribe to messages about new window advertisements.
  655. * Basically, if you're a panel, you want to do this, so
  656. * you can know when windows move around or change focus.
  657. */
  658. void yutani_subscribe_windows(yutani_t * y) {
  659. yutani_msg_buildx_subscribe_alloc(m);
  660. yutani_msg_buildx_subscribe(m);
  661. yutani_msg_send(y, m);
  662. }
  663. /**
  664. * yutani_unsubscribe_windows
  665. *
  666. * If you no longer wish to receive window change messages,
  667. * you can unsubscribe your client from them.
  668. */
  669. void yutani_unsubscribe_windows(yutani_t * y) {
  670. yutani_msg_buildx_unsubscribe_alloc(m);
  671. yutani_msg_buildx_unsubscribe(m);
  672. yutani_msg_send(y, m);
  673. }
  674. /**
  675. * yutani_query_windows
  676. *
  677. * When notified of changes, call this to request
  678. * the new information.
  679. */
  680. void yutani_query_windows(yutani_t * y) {
  681. yutani_msg_buildx_query_windows_alloc(m);
  682. yutani_msg_buildx_query_windows(m);
  683. yutani_msg_send(y, m);
  684. }
  685. /**
  686. * yutani_session_end
  687. *
  688. * For use by session managers, tell the compositor
  689. * that the session has ended and it should inform
  690. * other clients of this so they can exit.
  691. */
  692. void yutani_session_end(yutani_t * y) {
  693. yutani_msg_buildx_session_end_alloc(m);
  694. yutani_msg_buildx_session_end(m);
  695. yutani_msg_send(y, m);
  696. }
  697. /**
  698. * yutani_focus_window
  699. *
  700. * Change focus to the given window. Mostly used by
  701. * panels and other window management things, but if you
  702. * have a multi-window application, such as one with a
  703. * model dialog, and you want to force focus away from one
  704. * window and onto another, you can use this.
  705. */
  706. void yutani_focus_window(yutani_t * yctx, yutani_wid_t wid) {
  707. yutani_msg_buildx_window_focus_alloc(m);
  708. yutani_msg_buildx_window_focus(m, wid);
  709. yutani_msg_send(yctx, m);
  710. }
  711. /**
  712. * yutani_key_bind
  713. *
  714. * Request a key combination always be sent to this client.
  715. * You can request for the combination to be sent only to
  716. * this client (steal binding) or to also go to other clients
  717. * (spy binding), the latter of which is useful for catching
  718. * changes to modifier keys.
  719. */
  720. void yutani_key_bind(yutani_t * yctx, kbd_key_t key, kbd_mod_t mod, int response) {
  721. yutani_msg_buildx_key_bind_alloc(m);
  722. yutani_msg_buildx_key_bind(m, key,mod,response);
  723. yutani_msg_send(yctx, m);
  724. }
  725. /**
  726. * yutani_window_drag_start
  727. *
  728. * Begin a mouse-driven window movement action.
  729. * Typically used by decorators to start moving the window
  730. * when the user clicks and drags on the title bar.
  731. */
  732. void yutani_window_drag_start(yutani_t * yctx, yutani_window_t * window) {
  733. yutani_msg_buildx_window_drag_start_alloc(m);
  734. yutani_msg_buildx_window_drag_start(m, window->wid);
  735. yutani_msg_send(yctx, m);
  736. }
  737. /**
  738. * yutani_window_drag_start_wid
  739. *
  740. * Same as above, but takes a wid (of a presumably-foreign window)
  741. * instead of a window pointer; used by the panel to initiate
  742. * window movement through a drop-down menu for other clients.
  743. */
  744. void yutani_window_drag_start_wid(yutani_t * yctx, yutani_wid_t wid) {
  745. yutani_msg_buildx_window_drag_start_alloc(m);
  746. yutani_msg_buildx_window_drag_start(m, wid);
  747. yutani_msg_send(yctx, m);
  748. }
  749. /**
  750. * yutani_window_update_shape
  751. *
  752. * Change the window shaping threshold.
  753. * Allows partially-transparent windows to control whether they
  754. * should still receive mouse events in their transparent regions.
  755. */
  756. void yutani_window_update_shape(yutani_t * yctx, yutani_window_t * window, int set_shape) {
  757. yutani_msg_buildx_window_update_shape_alloc(m);
  758. yutani_msg_buildx_window_update_shape(m, window->wid, set_shape);
  759. yutani_msg_send(yctx, m);
  760. }
  761. /**
  762. * yutani_window_warp_mouse
  763. *
  764. * Move the mouse to a locate relative to the window.
  765. * Only works with relative mouse cursor.
  766. * Useful for games.
  767. *
  768. * TODO: We still need a way to lock the cursor to a particular window.
  769. * Even in games where warping happens quickly, we can still
  770. * end up with the cursor outside of the window when a click happens.
  771. */
  772. void yutani_window_warp_mouse(yutani_t * yctx, yutani_window_t * window, int32_t x, int32_t y) {
  773. yutani_msg_buildx_window_warp_mouse_alloc(m);
  774. yutani_msg_buildx_window_warp_mouse(m, window->wid, x, y);
  775. yutani_msg_send(yctx, m);
  776. }
  777. /**
  778. * yutani_window_show_mouse
  779. *
  780. * Set the cursor type. Used to change to risize and drag indicators.
  781. * Could be used to show a text insertion bar, or a link-clicking hand,
  782. * but those cursors need to be added in the server.
  783. *
  784. * TODO: We should add a way to use client-provided cursor textures.
  785. */
  786. void yutani_window_show_mouse(yutani_t * yctx, yutani_window_t * window, int32_t show_mouse) {
  787. yutani_msg_buildx_window_show_mouse_alloc(m);
  788. yutani_msg_buildx_window_show_mouse(m, window->wid, show_mouse);
  789. yutani_msg_send(yctx, m);
  790. }
  791. /**
  792. * yutani_window_resize_start
  793. *
  794. * Start a mouse-driven window resize action.
  795. * Used by decorators.
  796. */
  797. void yutani_window_resize_start(yutani_t * yctx, yutani_window_t * window, yutani_scale_direction_t direction) {
  798. yutani_msg_buildx_window_resize_start_alloc(m);
  799. yutani_msg_buildx_window_resize_start(m, window->wid, direction);
  800. yutani_msg_send(yctx, m);
  801. }
  802. /**
  803. * yutani_special_request
  804. *
  805. * Send one of the special request messages that aren't
  806. * important enough to get their own message types.
  807. *
  808. * (MAXIMIZE, PLEASE_CLOSE, CLIPBOARD)
  809. *
  810. * Note that, especially in the CLIPBOARD case, the
  811. * window does not to be set.
  812. */
  813. void yutani_special_request(yutani_t * yctx, yutani_window_t * window, uint32_t request) {
  814. /* wid isn't necessary; if window is null, set to 0 */
  815. yutani_msg_buildx_special_request_alloc(m);
  816. yutani_msg_buildx_special_request(m, window ? window->wid : 0, request);
  817. yutani_msg_send(yctx, m);
  818. }
  819. /**
  820. * yutani_special_request_wid
  821. *
  822. * Same as above, but takes a wid instead of a window pointer,
  823. * for use with foreign windows.
  824. */
  825. void yutani_special_request_wid(yutani_t * yctx, yutani_wid_t wid, uint32_t request) {
  826. /* For working with other applications' windows */
  827. yutani_msg_buildx_special_request_alloc(m);
  828. yutani_msg_buildx_special_request(m, wid, request);
  829. yutani_msg_send(yctx, m);
  830. }
  831. /**
  832. * yutani_set_clipboard
  833. *
  834. * Set the clipboard content.
  835. *
  836. * If the clipboard content is too large for a message,
  837. * it will be stored in a file and a special clipboard string
  838. * will be set to indicate the real contents are
  839. * in the file.
  840. *
  841. * To get the clipboard contents, send a CLIPBOARD special
  842. * request and wait for the CLIPBOARD response message.
  843. */
  844. void yutani_set_clipboard(yutani_t * yctx, char * content) {
  845. /* Set clipboard contents */
  846. int len = strlen(content);
  847. if (len > 511) {
  848. char tmp_file[100];
  849. sprintf(tmp_file, "/tmp/.clipboard.%s", yctx->server_ident);
  850. FILE * tmp = fopen(tmp_file, "w+");
  851. fwrite(content, len, 1, tmp);
  852. fclose(tmp);
  853. char tmp_data[100];
  854. sprintf(tmp_data, "\002 %d", len);
  855. yutani_msg_buildx_clipboard_alloc(m, strlen(tmp_data));
  856. yutani_msg_buildx_clipboard(m, tmp_data);
  857. yutani_msg_send(yctx, m);
  858. } else {
  859. yutani_msg_buildx_clipboard_alloc(m, len);
  860. yutani_msg_buildx_clipboard(m, content);
  861. yutani_msg_send(yctx, m);
  862. }
  863. }
  864. /**
  865. * yutani_open_clipboard
  866. *
  867. * Open the clipboard contents file.
  868. */
  869. FILE * yutani_open_clipboard(yutani_t * yctx) {
  870. char tmp_file[100];
  871. sprintf(tmp_file, "/tmp/.clipboard.%s", yctx->server_ident);
  872. return fopen(tmp_file, "r");
  873. }
  874. /**
  875. * init_graphics_yutani
  876. *
  877. * Create a graphical context around a Yutani window.
  878. */
  879. gfx_context_t * init_graphics_yutani(yutani_window_t * window) {
  880. gfx_context_t * out = malloc(sizeof(gfx_context_t));
  881. out->width = window->width;
  882. out->height = window->height;
  883. out->stride = window->width * sizeof(uint32_t);
  884. out->depth = 32;
  885. out->size = GFX_H(out) * GFX_W(out) * GFX_B(out);
  886. out->buffer = window->buffer;
  887. out->backbuffer = out->buffer;
  888. out->clips = NULL;
  889. return out;
  890. }
  891. /**
  892. * init_graphics_yutani_double_buffer
  893. *
  894. * Create a graphics context around a Yutani window
  895. * with a separate backing store for double-buffering.
  896. */
  897. gfx_context_t * init_graphics_yutani_double_buffer(yutani_window_t * window) {
  898. gfx_context_t * out = init_graphics_yutani(window);
  899. out->backbuffer = malloc(GFX_B(out) * GFX_W(out) * GFX_H(out));
  900. return out;
  901. }
  902. /**
  903. * reinit_graphics_yutani
  904. *
  905. * Reinitialize a graphics context, such as when
  906. * the window size changes.
  907. */
  908. void reinit_graphics_yutani(gfx_context_t * out, yutani_window_t * window) {
  909. out->width = window->width;
  910. out->height = window->height;
  911. out->stride = window->width * 4;
  912. out->depth = 32;
  913. out->size = GFX_H(out) * GFX_W(out) * GFX_B(out);
  914. if (out->buffer == out->backbuffer) {
  915. out->buffer = window->buffer;
  916. out->backbuffer = out->buffer;
  917. } else {
  918. out->buffer = window->buffer;
  919. out->backbuffer = realloc(out->backbuffer, GFX_B(out) * GFX_W(out) * GFX_H(out));
  920. }
  921. }
  922. /**
  923. * release_graphics_yutani
  924. *
  925. * Release a graphics context.
  926. * XXX: This seems to work generically for any graphics context?
  927. */
  928. void release_graphics_yutani(gfx_context_t * gfx) {
  929. if (gfx->backbuffer != gfx->buffer) {
  930. free(gfx->backbuffer);
  931. }
  932. free(gfx);
  933. }