yutani.c 31 KB

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