yutani.c 31 KB

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