compositor.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398
  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) 2013-2017 Kevin Lange
  5. *
  6. * Yutani - The ToaruOS Compositor.
  7. *
  8. * Yutani is a canvas-based window compositor and manager.
  9. * It employs shared memory to provide clients access to
  10. * canvases in which they may render, while using a packet-based
  11. * socket interface to communicate actions between the server
  12. * and client such as keyboard activity, mouse movement, responses
  13. * to client events, etc., as well as to communicate requests from
  14. * the client to the server, such as creation of new windows,
  15. * movement, resizing, and display updates.
  16. */
  17. #include <stdio.h>
  18. #include <stdint.h>
  19. #include <syscall.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <signal.h>
  23. #include <fcntl.h>
  24. #include <unistd.h>
  25. #include <time.h>
  26. #include <math.h>
  27. #include <assert.h>
  28. #include <getopt.h>
  29. #include <errno.h>
  30. #include <sys/stat.h>
  31. #include <sys/time.h>
  32. #include <toaru/graphics.h>
  33. #include <toaru/pthread.h>
  34. #include <toaru/mouse.h>
  35. #include <toaru/kbd.h>
  36. #include <toaru/pex.h>
  37. #include <toaru/yutani.h>
  38. #include <toaru/hashmap.h>
  39. #include <toaru/list.h>
  40. #include <toaru/spinlock.h>
  41. #define _DEBUG_YUTANI
  42. #ifdef _DEBUG_YUTANI
  43. #include <toaru/trace.h>
  44. #define TRACE_APP_NAME "yutani"
  45. #else
  46. #define TRACE(msg,...)
  47. #endif
  48. #include "yutani_int.h"
  49. #define YUTANI_DEBUG_WINDOW_BOUNDS 1
  50. #define YUTANI_DEBUG_WINDOW_SHAPES 1
  51. #define YUTANI_RESIZE_RIGHT 0
  52. #define YUTANI_INCOMING_MOUSE_SCALE * 3
  53. #define MOUSE_WIDTH 64
  54. #define MOUSE_HEIGHT 64
  55. #define UNTILE_SENSITIVITY (MOUSE_SCALE * 5)
  56. struct {
  57. int nested;
  58. int nest_width;
  59. int nest_height;
  60. } yutani_options = {
  61. .nested = 0,
  62. .nest_width = 640,
  63. .nest_height = 480,
  64. };
  65. /**
  66. * Print usage information.
  67. */
  68. static int usage(char * argv[]) {
  69. fprintf(stderr,
  70. "Yutani - Window Compositor\n"
  71. "\n"
  72. "usage: %s [-n [-g WxH]] [-h]\n"
  73. "\n"
  74. " -n --nested \033[3mRun in a window.\033[0m\n"
  75. " -h --help \033[3mShow this help message.\033[0m\n"
  76. " -g --geometry \033[3mSet the size of the server framebuffer.\033[0m\n"
  77. "\n"
  78. " Yutani is the standard system compositor.\n"
  79. "\n",
  80. argv[0]);
  81. return 1;
  82. }
  83. /**
  84. * Parse arguments
  85. */
  86. static int parse_args(int argc, char * argv[], int * out) {
  87. *out = 1;
  88. for (int i = 1; i < argc; ++i) {
  89. if (argv[i][0] == '-') {
  90. char *c = &argv[i][1];
  91. *out = *out + 1;
  92. while (*c) {
  93. switch (*c) {
  94. case 'n':
  95. yutani_options.nested = 1;
  96. break;
  97. }
  98. c++;
  99. }
  100. }
  101. }
  102. #if 0
  103. static struct option long_opts[] = {
  104. {"nest", no_argument, 0, 'n'},
  105. {"geometry", required_argument, 0, 'g'},
  106. {"help", no_argument, 0, 'h'},
  107. {0,0,0,0}
  108. };
  109. int index, c;
  110. while ((c = getopt_long(argc, argv, "hg:n", long_opts, &index)) != -1) {
  111. if (!c) {
  112. if (long_opts[index].flag == 0) {
  113. c = long_opts[index].val;
  114. }
  115. }
  116. switch (c) {
  117. case 'h':
  118. return usage(argv);
  119. case 'n':
  120. yutani_options.nested = 1;
  121. break;
  122. case 'g':
  123. {
  124. char * c = strstr(optarg, "x");
  125. if (c) {
  126. *c = '\0';
  127. c++;
  128. yutani_options.nest_width = atoi(optarg);
  129. yutani_options.nest_height = atoi(c);
  130. }
  131. }
  132. break;
  133. default:
  134. fprintf(stderr, "Unrecognized option: %c\n", c);
  135. break;
  136. }
  137. }
  138. *out = optind;
  139. #endif
  140. return 0;
  141. }
  142. int32_t min(int32_t a, int32_t b) {
  143. return (a < b) ? a : b;
  144. }
  145. int32_t max(int32_t a, int32_t b) {
  146. return (a > b) ? a : b;
  147. }
  148. static int next_buf_id(void) {
  149. static int _next = 1;
  150. return _next++;
  151. }
  152. static int next_wid(void) {
  153. static int _next = 1;
  154. return _next++;
  155. }
  156. static uint32_t yutani_current_time(yutani_globals_t * yg) {
  157. struct timeval t;
  158. gettimeofday(&t, NULL);
  159. uint32_t sec_diff = t.tv_sec - yg->start_time;
  160. uint32_t usec_diff = t.tv_usec - yg->start_subtime;
  161. if (t.tv_usec < yg->start_subtime) {
  162. sec_diff -= 1;
  163. usec_diff = (1000000 + t.tv_usec) - yg->start_subtime;
  164. }
  165. return (uint32_t)(sec_diff * 1000 + usec_diff / 1000);
  166. }
  167. static uint32_t yutani_time_since(yutani_globals_t * yg, uint32_t start_time) {
  168. uint32_t now = yutani_current_time(yg);
  169. uint32_t diff = now - start_time; /* Milliseconds */
  170. return diff;
  171. }
  172. /**
  173. * Translate and transform coordinate from screen-relative to window-relative.
  174. */
  175. static void device_to_window(yutani_server_window_t * window, int32_t x, int32_t y, int32_t * out_x, int32_t * out_y) {
  176. if (!window) return;
  177. *out_x = x - window->x;
  178. *out_y = y - window->y;
  179. if (!window->rotation) return;
  180. #if 0
  181. double t_x = *out_x - (window->width / 2);
  182. double t_y = *out_y - (window->height / 2);
  183. double s = __builtin_sin(-M_PI * (window->rotation/ 180.0));
  184. double c = __builtin_cos(-M_PI * (window->rotation/ 180.0));
  185. double n_x = t_x * c - t_y * s;
  186. double n_y = t_x * s + t_y * c;
  187. *out_x = (int32_t)n_x + (window->width / 2);
  188. *out_y = (int32_t)n_y + (window->height / 2);
  189. #endif
  190. }
  191. /**
  192. * Translate and transform coordinate from window-relative to screen-relative.
  193. */
  194. static void window_to_device(yutani_server_window_t * window, int32_t x, int32_t y, int32_t * out_x, int32_t * out_y) {
  195. if (!window->rotation) {
  196. *out_x = window->x + x;
  197. *out_y = window->y + y;
  198. return;
  199. }
  200. #if 0
  201. double t_x = x - (window->width / 2);
  202. double t_y = y - (window->height / 2);
  203. double s = __builtin_sin(M_PI * (window->rotation/ 180.0));
  204. double c = __builtin_cos(M_PI * (window->rotation/ 180.0));
  205. double n_x = t_x * c - t_y * s;
  206. double n_y = t_x * s + t_y * c;
  207. *out_x = (int32_t)n_x + (window->width / 2) + window->x;
  208. *out_y = (int32_t)n_y + (window->height / 2) + window->y;
  209. #endif
  210. }
  211. /**
  212. * Remove a window from the z stack.
  213. */
  214. static void unorder_window(yutani_globals_t * yg, yutani_server_window_t * w) {
  215. unsigned short index = w->z;
  216. w->z = -1;
  217. if (index == YUTANI_ZORDER_BOTTOM) {
  218. yg->bottom_z = NULL;
  219. return;
  220. }
  221. if (index == YUTANI_ZORDER_TOP) {
  222. yg->top_z = NULL;
  223. return;
  224. }
  225. node_t * n = list_find(yg->mid_zs, w);
  226. if (!n) return;
  227. list_delete(yg->mid_zs, n);
  228. free(n);
  229. }
  230. /**
  231. * Move a window to a new stack order.
  232. */
  233. static void reorder_window(yutani_globals_t * yg, yutani_server_window_t * window, uint16_t new_zed) {
  234. if (!window) {
  235. return;
  236. }
  237. int z = window->z;
  238. spin_lock(&yg->redraw_lock);
  239. unorder_window(yg, window);
  240. spin_unlock(&yg->redraw_lock);
  241. window->z = new_zed;
  242. if (new_zed != YUTANI_ZORDER_TOP && new_zed != YUTANI_ZORDER_BOTTOM) {
  243. spin_lock(&yg->redraw_lock);
  244. list_insert(yg->mid_zs, window);
  245. spin_unlock(&yg->redraw_lock);
  246. return;
  247. }
  248. if (new_zed == YUTANI_ZORDER_TOP) {
  249. if (yg->top_z) {
  250. spin_lock(&yg->redraw_lock);
  251. unorder_window(yg, yg->top_z);
  252. spin_unlock(&yg->redraw_lock);
  253. }
  254. yg->top_z = window;
  255. return;
  256. }
  257. if (new_zed == YUTANI_ZORDER_BOTTOM) {
  258. if (yg->bottom_z) {
  259. spin_lock(&yg->redraw_lock);
  260. unorder_window(yg, yg->bottom_z);
  261. spin_unlock(&yg->redraw_lock);
  262. }
  263. yg->bottom_z = window;
  264. return;
  265. }
  266. }
  267. /**
  268. * Move a window to the top of the basic z stack, if valid.
  269. */
  270. static void make_top(yutani_globals_t * yg, yutani_server_window_t * w) {
  271. unsigned short index = w->z;
  272. if (index == YUTANI_ZORDER_BOTTOM) return;
  273. if (index == YUTANI_ZORDER_TOP) return;
  274. node_t * n = list_find(yg->mid_zs, w);
  275. if (!n) return; /* wat */
  276. list_delete(yg->mid_zs, n);
  277. list_append(yg->mid_zs, n);
  278. }
  279. /**
  280. * Set a window as the focused window.
  281. *
  282. * Currently, we only support one focused window.
  283. * In the future, we should support multiple windows as "focused" to account
  284. * for multiple "seats" on a single display.
  285. */
  286. static void set_focused_window(yutani_globals_t * yg, yutani_server_window_t * w) {
  287. if (w == yg->focused_window) {
  288. return; /* Already focused */
  289. }
  290. if (yg->focused_window) {
  291. /* Send focus change to old focused window */
  292. yutani_msg_t * response = yutani_msg_build_window_focus_change(yg->focused_window->wid, 0);
  293. pex_send(yg->server, yg->focused_window->owner, response->size, (char *)response);
  294. free(response);
  295. }
  296. yg->focused_window = w;
  297. if (w) {
  298. /* Send focus change to new focused window */
  299. yutani_msg_t * response = yutani_msg_build_window_focus_change(w->wid, 1);
  300. pex_send(yg->server, w->owner, response->size, (char *)response);
  301. free(response);
  302. make_top(yg, w);
  303. mark_window(yg, w);
  304. } else {
  305. /*
  306. * There is no window to focus (we're unsetting focus);
  307. * default to the bottom window (background)
  308. */
  309. yg->focused_window = yg->bottom_z;
  310. }
  311. /* Notify all subscribers of window changes */
  312. notify_subscribers(yg);
  313. }
  314. /**
  315. * Get the focused window.
  316. *
  317. * In case there is no focused window, we return the bottom window.
  318. */
  319. static yutani_server_window_t * get_focused(yutani_globals_t * yg) {
  320. if (yg->focused_window) return yg->focused_window;
  321. return yg->bottom_z;
  322. }
  323. /**
  324. * Create a server window object.
  325. *
  326. * Initializes a window of the particular size for a given client.
  327. */
  328. static yutani_server_window_t * server_window_create(yutani_globals_t * yg, int width, int height, uint32_t owner, uint32_t flags) {
  329. yutani_server_window_t * win = malloc(sizeof(yutani_server_window_t));
  330. win->wid = next_wid();
  331. win->owner = owner;
  332. list_insert(yg->windows, win);
  333. hashmap_set(yg->wids_to_windows, (void*)win->wid, win);
  334. list_t * client_list = hashmap_get(yg->clients_to_windows, (void *)owner);
  335. list_insert(client_list, win);
  336. win->x = 0;
  337. win->y = 0;
  338. win->z = 1;
  339. win->width = width;
  340. win->height = height;
  341. win->bufid = next_buf_id();
  342. win->rotation = 0;
  343. win->newbufid = 0;
  344. win->client_flags = 0;
  345. win->client_offsets[0] = 0;
  346. win->client_offsets[1] = 0;
  347. win->client_offsets[2] = 0;
  348. win->client_offsets[3] = 0;
  349. win->client_offsets[4] = 0;
  350. win->client_length = 0;
  351. win->client_strings = NULL;
  352. win->anim_mode = YUTANI_EFFECT_FADE_IN;
  353. win->anim_start = yutani_current_time(yg);
  354. win->alpha_threshold = 0;
  355. win->show_mouse = 1;
  356. win->tiled = 0;
  357. win->untiled_width = 0;
  358. win->untiled_height = 0;
  359. win->default_mouse = 1;
  360. win->server_flags = flags;
  361. win->opacity = 255;
  362. char key[1024];
  363. YUTANI_SHMKEY(yg->server_ident, key, 1024, win);
  364. size_t size = (width * height * 4);
  365. win->buffer = (uint8_t *)syscall_shm_obtain(key, &size);
  366. memset(win->buffer, 0, size);
  367. list_insert(yg->mid_zs, win);
  368. return win;
  369. }
  370. /**
  371. * Update the shape threshold for a window.
  372. *
  373. * A shaping threshold is a byte representing the minimum
  374. * required alpha for a window to be considered "solid".
  375. * Eg., a value of 0 says all windows are solid, while a value of
  376. * 1 requires a window to have at least some opacity to it,
  377. * and a value of 255 requires fully opaque pixels.
  378. *
  379. * Not actually stored as a byte, so a value over 255 can be used.
  380. * This results in a window that passes through all clicks.
  381. */
  382. static void server_window_update_shape(yutani_globals_t * yg, yutani_server_window_t * window, int set) {
  383. window->alpha_threshold = set;
  384. }
  385. /**
  386. * Start resizing a window.
  387. *
  388. * Resizing a multi-stage process.
  389. * The client and server agree on a size and the server prepares a buffer.
  390. * The client then needs to accept the resize, fill the buffer, and then
  391. * inform the server that it is ready, at which point we'll swap the
  392. * buffer we are rendering from.
  393. */
  394. static uint32_t server_window_resize(yutani_globals_t * yg, yutani_server_window_t * win, int width, int height) {
  395. /* A client has accepted our offer, let's make a buffer for them */
  396. if (win->newbufid) {
  397. /* Already in the middle of an accept/done, bail */
  398. return win->newbufid;
  399. }
  400. win->newbufid = next_buf_id();
  401. {
  402. char key[1024];
  403. YUTANI_SHMKEY_EXP(yg->server_ident, key, 1024, win->newbufid);
  404. size_t size = (width * height * 4);
  405. win->newbuffer = (uint8_t *)syscall_shm_obtain(key, &size);
  406. }
  407. return win->newbufid;
  408. }
  409. /**
  410. * Finish the resize process.
  411. *
  412. * We delete the unlink the old buffer and then swap the pointers
  413. * for the new buffer.
  414. */
  415. static void server_window_resize_finish(yutani_globals_t * yg, yutani_server_window_t * win, int width, int height) {
  416. if (!win->newbufid) {
  417. return;
  418. }
  419. int oldbufid = win->bufid;
  420. mark_window(yg, win);
  421. spin_lock(&yg->redraw_lock);
  422. win->width = width;
  423. win->height = height;
  424. win->bufid = win->newbufid;
  425. win->buffer = win->newbuffer;
  426. win->newbuffer = NULL;
  427. win->newbufid = 0;
  428. {
  429. char key[1024];
  430. YUTANI_SHMKEY_EXP(yg->server_ident, key, 1024, oldbufid);
  431. syscall_shm_release(key);
  432. }
  433. spin_unlock(&yg->redraw_lock);
  434. mark_window(yg, win);
  435. }
  436. /**
  437. * Add a clip region from a rectangle.
  438. */
  439. static void yutani_add_clip(yutani_globals_t * yg, double x, double y, double w, double h) {
  440. gfx_add_clip(yg->backend_ctx, (int)x, (int)y, (int)w, (int)h);
  441. }
  442. typedef struct {
  443. int32_t start;
  444. int32_t end;
  445. void * next;
  446. } _clip_t;
  447. /**
  448. * Mark a screen region as damaged.
  449. */
  450. static void mark_screen(yutani_globals_t * yg, int32_t x, int32_t y, int32_t width, int32_t height) {
  451. yutani_damage_rect_t * rect = malloc(sizeof(yutani_damage_rect_t));
  452. rect->x = x;
  453. rect->y = y;
  454. rect->width = width;
  455. rect->height = height;
  456. spin_lock(&yg->update_list_lock);
  457. list_insert(yg->update_list, rect);
  458. spin_unlock(&yg->update_list_lock);
  459. }
  460. /**
  461. * Draw the cursor sprite.
  462. */
  463. static void draw_cursor(yutani_globals_t * yg, int x, int y, int cursor) {
  464. sprite_t * sprite = &yg->mouse_sprite;
  465. static sprite_t * previous = NULL;
  466. if (yg->resizing_window) {
  467. switch (yg->resizing_direction) {
  468. case SCALE_UP:
  469. case SCALE_DOWN:
  470. sprite = &yg->mouse_sprite_resize_v;
  471. break;
  472. case SCALE_LEFT:
  473. case SCALE_RIGHT:
  474. sprite = &yg->mouse_sprite_resize_h;
  475. break;
  476. case SCALE_DOWN_RIGHT:
  477. case SCALE_UP_LEFT:
  478. sprite = &yg->mouse_sprite_resize_da;
  479. break;
  480. case SCALE_DOWN_LEFT:
  481. case SCALE_UP_RIGHT:
  482. sprite = &yg->mouse_sprite_resize_db;
  483. break;
  484. }
  485. } else if (yg->mouse_state == YUTANI_MOUSE_STATE_MOVING) {
  486. sprite = &yg->mouse_sprite_drag;
  487. } else {
  488. switch (cursor) {
  489. case YUTANI_CURSOR_TYPE_DRAG: sprite = &yg->mouse_sprite_drag; break;
  490. case YUTANI_CURSOR_TYPE_RESIZE_VERTICAL: sprite = &yg->mouse_sprite_resize_v; break;
  491. case YUTANI_CURSOR_TYPE_RESIZE_HORIZONTAL: sprite = &yg->mouse_sprite_resize_h; break;
  492. case YUTANI_CURSOR_TYPE_RESIZE_UP_DOWN: sprite = &yg->mouse_sprite_resize_da; break;
  493. case YUTANI_CURSOR_TYPE_RESIZE_DOWN_UP: sprite = &yg->mouse_sprite_resize_db; break;
  494. }
  495. }
  496. if (sprite != previous) {
  497. mark_screen(yg, x / MOUSE_SCALE - MOUSE_OFFSET_X, y / MOUSE_SCALE - MOUSE_OFFSET_Y, MOUSE_WIDTH, MOUSE_HEIGHT);
  498. previous = sprite;
  499. }
  500. draw_sprite(yg->backend_ctx, sprite, x / MOUSE_SCALE - MOUSE_OFFSET_X, y / MOUSE_SCALE - MOUSE_OFFSET_Y);
  501. }
  502. /**
  503. * Determine if a window has a solid pixel at a given screen-space coordinate.
  504. *
  505. * This is where we evaluate alpha thresholds. We only do this underneath
  506. * the cursor, and only when we move the cursor. It's reasonably fast
  507. * in those circumstances, but shouldn't be used for large regions. We
  508. * do have one debug method that indicates the top window in a box
  509. * around the cursor, but it is relatively slow.
  510. */
  511. yutani_server_window_t * check_top_at(yutani_globals_t * yg, yutani_server_window_t * w, uint16_t x, uint16_t y){
  512. if (!w) return NULL;
  513. int32_t _x = -1, _y = -1;
  514. device_to_window(w, x, y, &_x, &_y);
  515. if (_x < 0 || _x >= w->width || _y < 0 || _y >= w->height) return NULL;
  516. uint32_t c = ((uint32_t *)w->buffer)[(w->width * _y + _x)];
  517. uint8_t a = _ALP(c);
  518. if (a >= w->alpha_threshold) {
  519. return w;
  520. }
  521. return NULL;
  522. }
  523. /**
  524. * Find the window that is at the top at a particular screen-space coordinate.
  525. *
  526. * This walks through each window from top to bottom (foreachr - reverse foreach)
  527. * until it finds one with a pixel at this coordinate. Again, we only call this
  528. * at the cursor coordinates, and it is not particularly fast, so don't use it
  529. * anywhere that needs to hit a lot of coordinates.
  530. */
  531. yutani_server_window_t * top_at(yutani_globals_t * yg, uint16_t x, uint16_t y) {
  532. if (check_top_at(yg, yg->top_z, x, y)) return yg->top_z;
  533. foreachr(node, yg->mid_zs) {
  534. yutani_server_window_t * w = node->value;
  535. if (check_top_at(yg, w, x, y)) return w;
  536. }
  537. if (check_top_at(yg, yg->bottom_z, x, y)) return yg->bottom_z;
  538. return NULL;
  539. }
  540. /**
  541. * Get the window at a coordinate and focus it.
  542. *
  543. * See the docs for the proceeding functions, but added to this
  544. * focusing windows is also not particular fast as the reshuffle
  545. * is complicated.
  546. */
  547. static void set_focused_at(yutani_globals_t * yg, int x, int y) {
  548. yutani_server_window_t * n_focused = top_at(yg, x, y);
  549. set_focused_window(yg, n_focused);
  550. }
  551. /*
  552. * Convenience functions for checking if a window is in the top/bottom stack.
  553. *
  554. * In the future, these single-item "stacks" will be replaced with dedicated stacks
  555. * so we can have multiple background windows and multiple panels / always-top windows.
  556. */
  557. static int window_is_top(yutani_globals_t * yg, yutani_server_window_t * window) {
  558. /* For now, just use simple z-order */
  559. return window->z == YUTANI_ZORDER_TOP;
  560. }
  561. static int window_is_bottom(yutani_globals_t * yg, yutani_server_window_t * window) {
  562. /* For now, just use simple z-order */
  563. return window->z == YUTANI_ZORDER_BOTTOM;
  564. }
  565. /**
  566. * Get a color for a wid for debugging.
  567. *
  568. * Makes a pretty rainbow pattern.
  569. */
  570. static uint32_t color_for_wid(yutani_wid_t wid) {
  571. static uint32_t colors[] = {
  572. 0xFF19aeff,
  573. 0xFFff4141,
  574. 0xFFffff3e,
  575. 0xFFff6600,
  576. 0xFF9ade00,
  577. 0xFFd76cff,
  578. 0xFF364e59,
  579. 0xFF0084c8,
  580. 0xFFdc0000,
  581. 0xFFff9900,
  582. 0xFF009100,
  583. 0xFFba00ff,
  584. 0xFFb88100,
  585. 0xFF9eabb0
  586. };
  587. int i = wid % (sizeof(colors) / sizeof(uint32_t));
  588. return colors[i];
  589. }
  590. /**
  591. * Blit a window to the framebuffer.
  592. *
  593. * Applies transformations (rotation, animations) and then renders
  594. * the window through alpha blitting.
  595. */
  596. static int yutani_blit_window(yutani_globals_t * yg, yutani_server_window_t * window, int x, int y) {
  597. sprite_t _win_sprite;
  598. _win_sprite.width = window->width;
  599. _win_sprite.height = window->height;
  600. _win_sprite.bitmap = (uint32_t *)window->buffer;
  601. _win_sprite.masks = NULL;
  602. _win_sprite.blank = 0;
  603. _win_sprite.alpha = ALPHA_EMBEDDED;
  604. if (window->anim_mode) {
  605. int frame = yutani_time_since(yg, window->anim_start);
  606. if (frame >= yutani_animation_lengths[window->anim_mode]) {
  607. /* XXX handle animation-end things like cleanup of closing windows */
  608. if (window->anim_mode == YUTANI_EFFECT_FADE_OUT) {
  609. list_insert(yg->windows_to_remove, window);
  610. goto draw_finish;
  611. }
  612. window->anim_mode = 0;
  613. window->anim_start = 0;
  614. goto draw_window;
  615. } else {
  616. switch (window->anim_mode) {
  617. case YUTANI_EFFECT_FADE_OUT:
  618. {
  619. frame = yutani_animation_lengths[window->anim_mode] - frame;
  620. }
  621. case YUTANI_EFFECT_FADE_IN:
  622. {
  623. double time_diff = ((double)frame / (float)yutani_animation_lengths[window->anim_mode]);
  624. double x = 0.75 + time_diff * 0.25;
  625. int t_x = (window->width * (1.0 - x)) / 2;
  626. int t_y = (window->height * (1.0 - x)) / 2;
  627. double opacity = time_diff * (double)(window->opacity) / 255.0;
  628. if (!window_is_top(yg, window) && !window_is_bottom(yg, window) &&
  629. !(window->server_flags & YUTANI_WINDOW_FLAG_ALT_ANIMATION)) {
  630. draw_sprite_scaled_alpha(yg->backend_ctx, &_win_sprite, window->x + t_x, window->y + t_y, window->width * x, window->height * x, opacity);
  631. } else {
  632. draw_sprite_alpha(yg->backend_ctx, &_win_sprite, window->x, window->y, opacity);
  633. }
  634. }
  635. break;
  636. default:
  637. goto draw_window;
  638. break;
  639. }
  640. }
  641. } else {
  642. draw_window:
  643. if (window->opacity != 255) {
  644. double opacity = (double)(window->opacity) / 255.0;
  645. if (window == yg->resizing_window) {
  646. draw_sprite_scaled_alpha(yg->backend_ctx, &_win_sprite, window->x + (int)yg->resizing_offset_x, window->y + (int)yg->resizing_offset_y, yg->resizing_w, yg->resizing_h, opacity);
  647. } else {
  648. draw_sprite_alpha(yg->backend_ctx, &_win_sprite, window->x, window->y, opacity);
  649. }
  650. } else {
  651. if (window == yg->resizing_window) {
  652. draw_sprite_scaled(yg->backend_ctx, &_win_sprite, window->x + (int)yg->resizing_offset_x, window->y + (int)yg->resizing_offset_y, yg->resizing_w, yg->resizing_h);
  653. } else {
  654. draw_sprite(yg->backend_ctx, &_win_sprite, window->x, window->y);
  655. }
  656. }
  657. }
  658. draw_finish:
  659. return 0;
  660. }
  661. /**
  662. * Blit all windows into the given context.
  663. *
  664. * This is called for rendering and for screenshots.
  665. */
  666. static void yutani_blit_windows(yutani_globals_t * yg) {
  667. if (yg->bottom_z) yutani_blit_window(yg, yg->bottom_z, yg->bottom_z->x, yg->bottom_z->y);
  668. foreach (node, yg->mid_zs) {
  669. yutani_server_window_t * w = node->value;
  670. if (w) yutani_blit_window(yg, w, w->x, w->y);
  671. }
  672. if (yg->top_z) yutani_blit_window(yg, yg->top_z, yg->top_z->x, yg->top_z->y);
  673. }
  674. /**
  675. * Take a screenshot
  676. */
  677. static void yutani_screenshot(yutani_globals_t * yg) {
  678. /* TODO Render bitmap screenshot */
  679. }
  680. /**
  681. * Redraw all windows, as well as the mouse cursor.
  682. *
  683. * This is the main redraw function.
  684. */
  685. static void redraw_windows(yutani_globals_t * yg) {
  686. int has_updates = 0;
  687. /* We keep our own temporary mouse coordinates as they may change while we're drawing. */
  688. int tmp_mouse_x = yg->mouse_x;
  689. int tmp_mouse_y = yg->mouse_y;
  690. /* If the mouse has moved, that counts as two damage regions */
  691. if ((yg->last_mouse_x != tmp_mouse_x) || (yg->last_mouse_y != tmp_mouse_y)) {
  692. has_updates = 2;
  693. yutani_add_clip(yg, yg->last_mouse_x / MOUSE_SCALE - MOUSE_OFFSET_X, yg->last_mouse_y / MOUSE_SCALE - MOUSE_OFFSET_Y, MOUSE_WIDTH, MOUSE_HEIGHT);
  694. yutani_add_clip(yg, tmp_mouse_x / MOUSE_SCALE - MOUSE_OFFSET_X, tmp_mouse_y / MOUSE_SCALE - MOUSE_OFFSET_Y, MOUSE_WIDTH, MOUSE_HEIGHT);
  695. }
  696. yg->last_mouse_x = tmp_mouse_x;
  697. yg->last_mouse_y = tmp_mouse_y;
  698. if (yg->bottom_z && yg->bottom_z->anim_mode) mark_window(yg, yg->bottom_z);
  699. if (yg->top_z && yg->top_z->anim_mode) mark_window(yg, yg->top_z);
  700. foreach (node, yg->mid_zs) {
  701. yutani_server_window_t * w = node->value;
  702. if (w && w->anim_mode) mark_window(yg, w);
  703. }
  704. /* Calculate damage regions from currently queued updates */
  705. spin_lock(&yg->update_list_lock);
  706. while (yg->update_list->length) {
  707. node_t * win = list_dequeue(yg->update_list);
  708. yutani_damage_rect_t * rect = (void *)win->value;
  709. /* We add a clip region for each window in the update queue */
  710. has_updates = 1;
  711. yutani_add_clip(yg, rect->x, rect->y, rect->width, rect->height);
  712. free(rect);
  713. free(win);
  714. }
  715. spin_unlock(&yg->update_list_lock);
  716. /* Render */
  717. if (has_updates) {
  718. yg->windows_to_remove = list_create();
  719. /*
  720. * In theory, we should restrict this to windows within the clip region,
  721. * but calculating that may be more trouble than it's worth;
  722. * we also need to render windows in stacking order...
  723. */
  724. spin_lock(&yg->redraw_lock);
  725. yutani_blit_windows(yg);
  726. #if YUTANI_DEBUG_WINDOW_SHAPES
  727. #define WINDOW_SHAPE_VIEWER_SIZE 20
  728. /*
  729. * Debugging window shapes: draw a box around the mouse cursor
  730. * showing which window is at the top and will accept mouse events.
  731. */
  732. if (yg->debug_shapes) {
  733. int _ly = max(0,tmp_mouse_y/MOUSE_SCALE - WINDOW_SHAPE_VIEWER_SIZE);
  734. int _hy = min(yg->height,tmp_mouse_y/MOUSE_SCALE + WINDOW_SHAPE_VIEWER_SIZE);
  735. int _lx = max(0,tmp_mouse_x/MOUSE_SCALE - 20);
  736. int _hx = min(yg->width,tmp_mouse_x/MOUSE_SCALE + WINDOW_SHAPE_VIEWER_SIZE);
  737. for (int y = _ly; y < _hy; ++y) {
  738. for (int x = _lx; x < _hx; ++x) {
  739. yutani_server_window_t * w = top_at(yg, x, y);
  740. if (w) { GFX(yg->backend_ctx, x, y) = color_for_wid(w->wid); }
  741. }
  742. }
  743. }
  744. #endif
  745. if (yutani_options.nested) {
  746. flip(yg->backend_ctx);
  747. /*
  748. * We should be able to flip only the places we need to flip, but
  749. * instead we're going to flip the whole thing.
  750. *
  751. * TODO: Do a better job of this.
  752. */
  753. yutani_flip(yg->host_context, yg->host_window);
  754. yutani_server_window_t * tmp_window = top_at(yg, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE);
  755. if (yg->mouse_state == YUTANI_MOUSE_STATE_MOVING) {
  756. yutani_window_show_mouse(yg->host_context, yg->host_window, YUTANI_CURSOR_TYPE_DRAG);
  757. } else if (!tmp_window || tmp_window->show_mouse) {
  758. yutani_window_show_mouse(yg->host_context, yg->host_window, tmp_window ? tmp_window->show_mouse : 1);
  759. }
  760. } else {
  761. /*
  762. * Draw the cursor.
  763. * We may also want to draw other compositor elements, like effects, but those
  764. * can also go in the stack order of the windows.
  765. */
  766. yutani_server_window_t * tmp_window = top_at(yg, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE);
  767. if (!tmp_window || tmp_window->show_mouse) {
  768. draw_cursor(yg, tmp_mouse_x, tmp_mouse_y, tmp_window ? tmp_window->show_mouse : 1);
  769. }
  770. /*
  771. * Flip the updated areas. This minimizes writes to video memory,
  772. * which is very important on real hardware where these writes are slow.
  773. */
  774. flip(yg->backend_ctx);
  775. }
  776. gfx_clear_clip(yg->backend_ctx);
  777. spin_unlock(&yg->redraw_lock);
  778. /*
  779. * If any windows were marked for removal,
  780. * then remove them.
  781. */
  782. while (yg->windows_to_remove->head) {
  783. node_t * node = list_pop(yg->windows_to_remove);
  784. window_actually_close(yg, node->value);
  785. free(node);
  786. }
  787. free(yg->windows_to_remove);
  788. }
  789. if (yg->screenshot_frame) {
  790. yutani_screenshot(yg);
  791. }
  792. if (yg->resize_on_next) {
  793. spin_lock(&yg->redraw_lock);
  794. if (!yutani_options.nested) {
  795. reinit_graphics_fullscreen(yg->backend_ctx);
  796. yg->stride = framebuffer_stride();
  797. } else {
  798. reinit_graphics_yutani(yg->backend_ctx, yg->host_window);
  799. yg->stride = yg->backend_ctx->width * 4;
  800. yutani_window_resize_done(yg->host_context, yg->host_window);
  801. }
  802. yg->width = yg->backend_ctx->width;
  803. yg->height = yg->backend_ctx->height;
  804. yg->backend_framebuffer = yg->backend_ctx->backbuffer;
  805. yg->resize_on_next = 0;
  806. mark_screen(yg, 0, 0, yg->width, yg->height);
  807. yutani_msg_t * response = yutani_msg_build_welcome(yg->width, yg->height);
  808. pex_broadcast(yg->server, response->size, (char *)response);
  809. free(response);
  810. spin_unlock(&yg->redraw_lock);
  811. }
  812. }
  813. /**
  814. * Initialize clipping regions.
  815. */
  816. void yutani_clip_init(yutani_globals_t * yg) {
  817. yg->update_list = list_create();
  818. yg->update_list_lock = 0;
  819. }
  820. /**
  821. * Redraw thread.
  822. *
  823. * Calls the redraw functions in a loop, with some
  824. * additional yielding and sleeping.
  825. */
  826. void * redraw(void * in) {
  827. syscall_system_function(11,(char *[]){"compositor","render thread",NULL});
  828. yutani_globals_t * yg = in;
  829. while (1) {
  830. /*
  831. * Perform whatever redraw work is required.
  832. */
  833. redraw_windows(yg);
  834. /*
  835. * Attempt to run at about 60fps...
  836. * we should actually see how long it took to render so
  837. * we can sleep *less* if it took a long time to render
  838. * this particular frame. We are definitely not
  839. * going to run at 60fps unless there's nothing to do
  840. * (and even then we've wasted cycles checking).
  841. */
  842. usleep(16666);
  843. }
  844. }
  845. /**
  846. * Mark a region within a window as damaged.
  847. *
  848. * If the window is rotated, we calculate the minimum rectangle that covers
  849. * the whole region specified and then mark that.
  850. */
  851. static void mark_window_relative(yutani_globals_t * yg, yutani_server_window_t * window, int32_t x, int32_t y, int32_t width, int32_t height) {
  852. yutani_damage_rect_t * rect = malloc(sizeof(yutani_damage_rect_t));
  853. if (window == yg->resizing_window) {
  854. double x_scale = (double)yg->resizing_w / (double)yg->resizing_window->width;
  855. double y_scale = (double)yg->resizing_h / (double)yg->resizing_window->height;
  856. x *= x_scale;
  857. x += yg->resizing_offset_x - 1;
  858. y *= y_scale;
  859. y += yg->resizing_offset_y - 1;
  860. width *= x_scale;
  861. height *= y_scale;
  862. width += 2;
  863. height += 2;
  864. }
  865. if (window->rotation == 0) {
  866. rect->x = window->x + x;
  867. rect->y = window->y + y;
  868. rect->width = width;
  869. rect->height = height;
  870. } else {
  871. int32_t ul_x, ul_y;
  872. int32_t ll_x, ll_y;
  873. int32_t ur_x, ur_y;
  874. int32_t lr_x, lr_y;
  875. window_to_device(window, x, y, &ul_x, &ul_y);
  876. window_to_device(window, x, y + height, &ll_x, &ll_y);
  877. window_to_device(window, x + width, y, &ur_x, &ur_y);
  878. window_to_device(window, x + width, y + height, &lr_x, &lr_y);
  879. /* Calculate bounds */
  880. int32_t left_bound = min(min(ul_x, ll_x), min(ur_x, lr_x));
  881. int32_t top_bound = min(min(ul_y, ll_y), min(ur_y, lr_y));
  882. int32_t right_bound = max(max(ul_x, ll_x), max(ur_x, lr_x));
  883. int32_t bottom_bound = max(max(ul_y, ll_y), max(ur_y, lr_y));
  884. rect->x = left_bound;
  885. rect->y = top_bound;
  886. rect->width = right_bound - left_bound;
  887. rect->height = bottom_bound - top_bound;
  888. }
  889. spin_lock(&yg->update_list_lock);
  890. list_insert(yg->update_list, rect);
  891. spin_unlock(&yg->update_list_lock);
  892. }
  893. /**
  894. * (Convenience function) Mark a whole a window as damaged.
  895. */
  896. static void mark_window(yutani_globals_t * yg, yutani_server_window_t * window) {
  897. mark_window_relative(yg, window, 0, 0, window->width, window->height);
  898. }
  899. /**
  900. * Set a window as closed. It will be removed after rendering has completed.
  901. */
  902. static void window_mark_for_close(yutani_globals_t * yg, yutani_server_window_t * w) {
  903. w->anim_mode = YUTANI_EFFECT_FADE_OUT;
  904. w->anim_start = yutani_current_time(yg);
  905. }
  906. /**
  907. * Remove a window from its owner's child set.
  908. */
  909. static void window_remove_from_client(yutani_globals_t * yg, yutani_server_window_t * w) {
  910. list_t * client_list = hashmap_get(yg->clients_to_windows, (void *)w->owner);
  911. if (client_list) {
  912. node_t * n = list_find(client_list, w);
  913. if (n) {
  914. list_delete(client_list, n);
  915. free(n);
  916. }
  917. }
  918. }
  919. /**
  920. * Actually remove a window and free the associated resources.
  921. */
  922. static void window_actually_close(yutani_globals_t * yg, yutani_server_window_t * w) {
  923. /* Remove from the wid -> window mapping */
  924. hashmap_remove(yg->wids_to_windows, (void *)w->wid);
  925. /* Remove from the general list of windows. */
  926. list_remove(yg->windows, list_index_of(yg->windows, w));
  927. /* Unstack the window */
  928. unorder_window(yg, w);
  929. /* Mark the region where the window was */
  930. mark_window(yg, w);
  931. /* And if it was focused, unfocus it. */
  932. if (w == yg->focused_window) {
  933. /* find the top z-ordered window */
  934. yg->focused_window = NULL;
  935. if (yg->mid_zs->tail && yg->mid_zs->tail->value) {
  936. set_focused_window(yg, yg->mid_zs->tail->value);
  937. }
  938. }
  939. {
  940. char key[1024];
  941. YUTANI_SHMKEY_EXP(yg->server_ident, key, 1024, w->bufid);
  942. /*
  943. * Normally we would acquire a lock before doing this, but the render
  944. * thread holds that lock already and we are only called from the
  945. * render thread, so we don't bother.
  946. */
  947. syscall_shm_release(key);
  948. }
  949. /* Notify subscribers that there are changes to windows */
  950. notify_subscribers(yg);
  951. }
  952. /**
  953. * Generate flags for client advertisements.
  954. *
  955. * Currently, we only have one flag (focused).
  956. */
  957. static uint32_t ad_flags(yutani_globals_t * yg, yutani_server_window_t * win) {
  958. uint32_t flags = win->client_flags;
  959. if (win == yg->focused_window) {
  960. flags |= 1;
  961. }
  962. return flags;
  963. }
  964. /**
  965. * Send a result for a window query.
  966. */
  967. static void yutani_query_result(yutani_globals_t * yg, uint32_t dest, yutani_server_window_t * win) {
  968. if (win && win->client_length) {
  969. yutani_msg_t * response = yutani_msg_build_window_advertise(win->wid, ad_flags(yg, win), win->client_offsets, win->client_length, win->client_strings);
  970. pex_send(yg->server, dest, response->size, (char *)response);
  971. free(response);
  972. }
  973. }
  974. /**
  975. * Send a notice to all subscribed clients that windows have updated.
  976. */
  977. static void notify_subscribers(yutani_globals_t * yg) {
  978. yutani_msg_t * response = yutani_msg_build_notify();
  979. list_t * remove = NULL;
  980. foreach(node, yg->window_subscribers) {
  981. uint32_t subscriber = (uint32_t)node->value;
  982. if (!hashmap_has(yg->clients_to_windows, (void *)subscriber)) {
  983. if (!remove) {
  984. remove = list_create();
  985. }
  986. list_insert(remove, node);
  987. } else {
  988. pex_send(yg->server, subscriber, response->size, (char *)response);
  989. }
  990. }
  991. if (remove) {
  992. while (remove->length) {
  993. node_t * n = list_pop(remove);
  994. list_delete(yg->window_subscribers, n->value);
  995. free(n);
  996. }
  997. free(remove);
  998. }
  999. free(response);
  1000. }
  1001. static void window_move(yutani_globals_t * yg, yutani_server_window_t * window, int x, int y) {
  1002. mark_window(yg, window);
  1003. window->x = x;
  1004. window->y = y;
  1005. mark_window(yg, window);
  1006. yutani_msg_t * response = yutani_msg_build_window_move(window->wid, x, y);
  1007. pex_send(yg->server, window->owner, response->size, (char *)response);
  1008. }
  1009. /**
  1010. * Move and resize a window to fit a particular tiling pattern.
  1011. *
  1012. * x and y are 0-based
  1013. * width_div and height_div are the number of cells in each dimension
  1014. */
  1015. static void window_tile(yutani_globals_t * yg, yutani_server_window_t * window, int width_div, int height_div, int x, int y) {
  1016. int panel_h = 0;
  1017. yutani_server_window_t * panel = yg->top_z;
  1018. if (panel) {
  1019. panel_h = panel->height;
  1020. if (panel->y < 1) {
  1021. panel_h += panel->y; /* We can move the panel up to "hide" it. */
  1022. }
  1023. }
  1024. if (!window->tiled) {
  1025. window->untiled_width = window->width;
  1026. window->untiled_height = window->height;
  1027. window->tiled = 1;
  1028. }
  1029. int w = yg->width / width_div;
  1030. int h = (yg->height - panel_h) / height_div;
  1031. /* Calculate, move, etc. */
  1032. window_move(yg, window, w * x, panel_h + h * y);
  1033. if (x == width_div - 1) {
  1034. w = yg->width - w * x;
  1035. }
  1036. if (y == height_div - 1) {
  1037. h = (yg->height - panel_h) - h * y;
  1038. }
  1039. yutani_msg_t * response = yutani_msg_build_window_resize(YUTANI_MSG_RESIZE_OFFER, window->wid, w, h, 0);
  1040. pex_send(yg->server, window->owner, response->size, (char *)response);
  1041. free(response);
  1042. return;
  1043. }
  1044. /**
  1045. * Process a key event.
  1046. *
  1047. * These are mostly compositor shortcuts and bindings.
  1048. * We also process key bindings for other applications.
  1049. */
  1050. static void handle_key_event(yutani_globals_t * yg, struct yutani_msg_key_event * ke) {
  1051. yutani_server_window_t * focused = get_focused(yg);
  1052. memcpy(&yg->kbd_state, &ke->state, sizeof(key_event_state_t));
  1053. if (focused) {
  1054. if ((ke->event.action == KEY_ACTION_DOWN) &&
  1055. (ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  1056. (ke->event.modifiers & KEY_MOD_LEFT_SHIFT) &&
  1057. (ke->event.keycode == 'z')) {
  1058. mark_window(yg,focused);
  1059. focused->rotation -= 5;
  1060. mark_window(yg,focused);
  1061. return;
  1062. }
  1063. if ((ke->event.action == KEY_ACTION_DOWN) &&
  1064. (ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  1065. (ke->event.modifiers & KEY_MOD_LEFT_SHIFT) &&
  1066. (ke->event.keycode == 'x')) {
  1067. mark_window(yg,focused);
  1068. focused->rotation += 5;
  1069. mark_window(yg,focused);
  1070. return;
  1071. }
  1072. if ((ke->event.action == KEY_ACTION_DOWN) &&
  1073. (ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  1074. (ke->event.modifiers & KEY_MOD_LEFT_SHIFT) &&
  1075. (ke->event.keycode == 'c')) {
  1076. mark_window(yg,focused);
  1077. focused->rotation = 0;
  1078. mark_window(yg,focused);
  1079. return;
  1080. }
  1081. if ((ke->event.action == KEY_ACTION_DOWN) &&
  1082. (ke->event.modifiers & KEY_MOD_LEFT_ALT) &&
  1083. (ke->event.keycode == KEY_F10)) {
  1084. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1085. window_tile(yg, focused, 1, 1, 0, 0);
  1086. return;
  1087. }
  1088. }
  1089. if ((ke->event.action == KEY_ACTION_DOWN) &&
  1090. (ke->event.modifiers & KEY_MOD_LEFT_ALT) &&
  1091. (ke->event.keycode == KEY_F4)) {
  1092. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1093. yutani_msg_t * response = yutani_msg_build_session_end();
  1094. pex_send(yg->server, focused->owner, response->size, (char *)response);
  1095. free(response);
  1096. return;
  1097. }
  1098. }
  1099. #if YUTANI_DEBUG_WINDOW_SHAPES
  1100. if ((ke->event.action == KEY_ACTION_DOWN) &&
  1101. (ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  1102. (ke->event.modifiers & KEY_MOD_LEFT_SHIFT) &&
  1103. (ke->event.keycode == 'v')) {
  1104. yg->debug_shapes = (1-yg->debug_shapes);
  1105. return;
  1106. }
  1107. #endif
  1108. #if YUTANI_DEBUG_WINDOW_BOUNDS
  1109. if ((ke->event.action == KEY_ACTION_DOWN) &&
  1110. (ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  1111. (ke->event.modifiers & KEY_MOD_LEFT_SHIFT) &&
  1112. (ke->event.keycode == 'b')) {
  1113. yg->debug_bounds = (1-yg->debug_bounds);
  1114. return;
  1115. }
  1116. #endif
  1117. /*
  1118. * Tiling hooks.
  1119. * These are based on the compiz grid plugin.
  1120. */
  1121. if ((ke->event.action == KEY_ACTION_DOWN) &&
  1122. (ke->event.modifiers & KEY_MOD_LEFT_SUPER)) {
  1123. if ((ke->event.modifiers & KEY_MOD_LEFT_SHIFT) &&
  1124. (ke->event.keycode == KEY_ARROW_LEFT)) {
  1125. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1126. window_tile(yg, focused, 2, 2, 0, 0);
  1127. return;
  1128. }
  1129. }
  1130. if ((ke->event.modifiers & KEY_MOD_LEFT_SHIFT) &&
  1131. (ke->event.keycode == KEY_ARROW_RIGHT)) {
  1132. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1133. window_tile(yg, focused, 2, 2, 1, 0);
  1134. return;
  1135. }
  1136. }
  1137. if ((ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  1138. (ke->event.keycode == KEY_ARROW_LEFT)) {
  1139. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1140. window_tile(yg, focused, 2, 2, 0, 1);
  1141. return;
  1142. }
  1143. }
  1144. if ((ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  1145. (ke->event.keycode == KEY_ARROW_RIGHT)) {
  1146. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1147. window_tile(yg, focused, 2, 2, 1, 1);
  1148. return;
  1149. }
  1150. }
  1151. if ((ke->event.keycode == KEY_ARROW_LEFT)) {
  1152. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1153. window_tile(yg, focused, 2, 1, 0, 0);
  1154. return;
  1155. }
  1156. }
  1157. if ((ke->event.keycode == KEY_ARROW_RIGHT)) {
  1158. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1159. window_tile(yg, focused, 2, 1, 1, 0);
  1160. return;
  1161. }
  1162. }
  1163. if ((ke->event.keycode == KEY_ARROW_UP)) {
  1164. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1165. window_tile(yg, focused, 1, 2, 0, 0);
  1166. return;
  1167. }
  1168. }
  1169. if ((ke->event.keycode == KEY_ARROW_DOWN)) {
  1170. if (focused->z != YUTANI_ZORDER_BOTTOM && focused->z != YUTANI_ZORDER_TOP) {
  1171. window_tile(yg, focused, 1, 2, 0, 1);
  1172. return;
  1173. }
  1174. }
  1175. if ((ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  1176. (ke->event.keycode == 's')) {
  1177. yg->screenshot_frame = YUTANI_SCREENSHOT_FULL;
  1178. return;
  1179. }
  1180. if ((ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  1181. (ke->event.keycode == 'w')) {
  1182. yg->screenshot_frame = YUTANI_SCREENSHOT_WINDOW;
  1183. return;
  1184. }
  1185. }
  1186. }
  1187. /*
  1188. * External bindings registered by clients.
  1189. */
  1190. uint32_t key_code = ((ke->event.modifiers << 24) | (ke->event.keycode));
  1191. if (hashmap_has(yg->key_binds, (void*)key_code)) {
  1192. struct key_bind * bind = hashmap_get(yg->key_binds, (void*)key_code);
  1193. yutani_msg_t * response = yutani_msg_build_key_event(focused ? focused->wid : -1, &ke->event, &ke->state);
  1194. pex_send(yg->server, bind->owner, response->size, (char *)response);
  1195. free(response);
  1196. if (bind->response == YUTANI_BIND_STEAL) {
  1197. /* If this keybinding was registered as "steal", we'll stop here. */
  1198. return;
  1199. }
  1200. }
  1201. /* Finally, send the key to the focused client. */
  1202. if (focused) {
  1203. yutani_msg_t * response = yutani_msg_build_key_event(focused->wid, &ke->event, &ke->state);
  1204. pex_send(yg->server, focused->owner, response->size, (char *)response);
  1205. free(response);
  1206. }
  1207. }
  1208. /**
  1209. * Register a new keybinding.
  1210. *
  1211. * req - bind message
  1212. * owner - client to assign the binding to
  1213. */
  1214. static void add_key_bind(yutani_globals_t * yg, struct yutani_msg_key_bind * req, unsigned int owner) {
  1215. uint32_t key_code = (((uint8_t)req->modifiers << 24) | ((uint32_t)req->key & 0xFFFFFF));
  1216. struct key_bind * bind = hashmap_get(yg->key_binds, (void*)key_code);
  1217. if (!bind) {
  1218. bind = malloc(sizeof(struct key_bind));
  1219. bind->owner = owner;
  1220. bind->response = req->response;
  1221. hashmap_set(yg->key_binds, (void*)key_code, bind);
  1222. } else {
  1223. bind->owner = owner;
  1224. bind->response = req->response;
  1225. }
  1226. }
  1227. static void adjust_window_opacity(yutani_globals_t * yg, int direction) {
  1228. yutani_server_window_t * window = top_at(yg, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE);
  1229. if (window && window->z != YUTANI_ZORDER_BOTTOM) {
  1230. window->opacity += direction;
  1231. if (window->opacity < 0) {
  1232. window->opacity = 0;
  1233. }
  1234. if (window->opacity > 255) {
  1235. window->opacity = 255;
  1236. }
  1237. mark_window(yg, window);
  1238. }
  1239. }
  1240. static void mouse_start_drag(yutani_globals_t * yg, yutani_server_window_t * w) {
  1241. set_focused_at(yg, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE);
  1242. if (!w) {
  1243. yg->mouse_window = get_focused(yg);
  1244. } else {
  1245. yg->mouse_window = w;
  1246. }
  1247. if (yg->mouse_window) {
  1248. if (yg->mouse_window->z == YUTANI_ZORDER_BOTTOM || yg->mouse_window->z == YUTANI_ZORDER_TOP
  1249. || yg->mouse_window->server_flags & YUTANI_WINDOW_FLAG_DISALLOW_DRAG) {
  1250. yg->mouse_state = YUTANI_MOUSE_STATE_NORMAL;
  1251. yg->mouse_window = NULL;
  1252. } else {
  1253. yg->mouse_state = YUTANI_MOUSE_STATE_MOVING;
  1254. yg->mouse_init_x = yg->mouse_x;
  1255. yg->mouse_init_y = yg->mouse_y;
  1256. yg->mouse_win_x = yg->mouse_window->x;
  1257. yg->mouse_win_y = yg->mouse_window->y;
  1258. yg->mouse_drag_button = yg->last_mouse_buttons;
  1259. mark_screen(yg, yg->mouse_x / MOUSE_SCALE - MOUSE_OFFSET_X, yg->mouse_y / MOUSE_SCALE - MOUSE_OFFSET_Y, MOUSE_WIDTH, MOUSE_HEIGHT);
  1260. make_top(yg, yg->mouse_window);
  1261. }
  1262. }
  1263. }
  1264. static void mouse_start_rotate(yutani_globals_t * yg) {
  1265. #if 0
  1266. set_focused_at(yg, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE);
  1267. yg->mouse_window = get_focused(yg);
  1268. if (yg->mouse_window) {
  1269. if (yg->mouse_window->z == YUTANI_ZORDER_BOTTOM || yg->mouse_window->z == YUTANI_ZORDER_TOP) {
  1270. /* Prevent rotating panel and wallpaper */
  1271. yg->mouse_state = YUTANI_MOUSE_STATE_NORMAL;
  1272. yg->mouse_window = NULL;
  1273. return;
  1274. }
  1275. yg->mouse_state = YUTANI_MOUSE_STATE_ROTATING;
  1276. yg->mouse_init_x = yg->mouse_x;
  1277. yg->mouse_init_y = yg->mouse_y;
  1278. int32_t x_diff = yg->mouse_x / MOUSE_SCALE - (yg->mouse_window->x + yg->mouse_window->width / 2);
  1279. int32_t y_diff = yg->mouse_y / MOUSE_SCALE - (yg->mouse_window->y + yg->mouse_window->height / 2);
  1280. int new_r = __builtin_atan2(x_diff, y_diff) * 180.0 / (-M_PI);
  1281. yg->mouse_init_r = yg->mouse_window->rotation - new_r;
  1282. make_top(yg, yg->mouse_window);
  1283. }
  1284. #endif
  1285. }
  1286. static void mouse_start_resize(yutani_globals_t * yg, yutani_scale_direction_t direction) {
  1287. set_focused_at(yg, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE);
  1288. yg->mouse_window = get_focused(yg);
  1289. if (yg->mouse_window) {
  1290. if (yg->mouse_window->z == YUTANI_ZORDER_BOTTOM || yg->mouse_window->z == YUTANI_ZORDER_TOP
  1291. || yg->mouse_window->server_flags & YUTANI_WINDOW_FLAG_DISALLOW_RESIZE) {
  1292. /* Prevent resizing panel and wallpaper */
  1293. yg->mouse_state = YUTANI_MOUSE_STATE_NORMAL;
  1294. yg->mouse_window = NULL;
  1295. yg->resizing_window = NULL;
  1296. } else {
  1297. TRACE("resize starting for wid=%d", yg->mouse_window -> wid);
  1298. yg->mouse_state = YUTANI_MOUSE_STATE_RESIZING;
  1299. yg->mouse_init_x = yg->mouse_x;
  1300. yg->mouse_init_y = yg->mouse_y;
  1301. yg->mouse_win_x = yg->mouse_window->x;
  1302. yg->mouse_win_y = yg->mouse_window->y;
  1303. yg->resizing_window = yg->mouse_window;
  1304. yg->resizing_w = yg->mouse_window->width;
  1305. yg->resizing_h = yg->mouse_window->height;
  1306. yg->resizing_offset_x = 0;
  1307. yg->resizing_offset_y = 0;
  1308. if (direction == SCALE_AUTO) {
  1309. /* Determine the best direction to scale in based on simple 9-cell system. */
  1310. int32_t x, y;
  1311. device_to_window(yg->resizing_window, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE, &x, &y);
  1312. int h_d = 0;
  1313. int v_d = 0;
  1314. if (y <= yg->resizing_h / 3) {
  1315. v_d = -1;
  1316. } else if (y >= (yg->resizing_h / 3) * 2) {
  1317. v_d = 1;
  1318. }
  1319. if (x <= yg->resizing_w / 3) {
  1320. h_d = -1;
  1321. } else if (x >= (yg->resizing_w / 3) * 2) {
  1322. h_d = 1;
  1323. }
  1324. /* Fall back */
  1325. if (h_d == 0 && v_d == 0) direction = SCALE_DOWN_RIGHT;
  1326. else if (h_d == 1 && v_d == 1) direction = SCALE_DOWN_RIGHT;
  1327. else if (h_d == 1 && v_d == -1) direction = SCALE_UP_RIGHT;
  1328. else if (h_d == -1 && v_d == 1) direction = SCALE_DOWN_LEFT;
  1329. else if (h_d == -1 && v_d == -1) direction = SCALE_UP_LEFT;
  1330. else if (h_d == 1 && v_d == 0) direction = SCALE_RIGHT;
  1331. else if (h_d == -1 && v_d == 0) direction = SCALE_LEFT;
  1332. else if (h_d == 0 && v_d == 1) direction = SCALE_DOWN;
  1333. else if (h_d == 0 && v_d == -1) direction = SCALE_UP;
  1334. }
  1335. yg->resizing_direction = direction;
  1336. make_top(yg, yg->mouse_window);
  1337. mark_window(yg, yg->resizing_window);
  1338. }
  1339. }
  1340. }
  1341. static void handle_mouse_event(yutani_globals_t * yg, struct yutani_msg_mouse_event * me) {
  1342. if (me->type == YUTANI_MOUSE_EVENT_TYPE_RELATIVE) {
  1343. yg->mouse_x += me->event.x_difference YUTANI_INCOMING_MOUSE_SCALE;
  1344. yg->mouse_y -= me->event.y_difference YUTANI_INCOMING_MOUSE_SCALE;
  1345. } else if (me->type == YUTANI_MOUSE_EVENT_TYPE_ABSOLUTE) {
  1346. yg->mouse_x = me->event.x_difference * MOUSE_SCALE;
  1347. yg->mouse_y = me->event.y_difference * MOUSE_SCALE;
  1348. }
  1349. if (yg->mouse_x < 0) yg->mouse_x = 0;
  1350. if (yg->mouse_y < 0) yg->mouse_y = 0;
  1351. if (yg->mouse_x > (yg->width) * MOUSE_SCALE) yg->mouse_x = (yg->width) * MOUSE_SCALE;
  1352. if (yg->mouse_y > (yg->height) * MOUSE_SCALE) yg->mouse_y = (yg->height) * MOUSE_SCALE;
  1353. switch (yg->mouse_state) {
  1354. case YUTANI_MOUSE_STATE_NORMAL:
  1355. {
  1356. if ((me->event.buttons & YUTANI_MOUSE_BUTTON_LEFT) && (yg->kbd_state.k_alt)) {
  1357. mouse_start_drag(yg, NULL);
  1358. } else if ((me->event.buttons & YUTANI_MOUSE_SCROLL_UP) && (yg->kbd_state.k_alt)) {
  1359. adjust_window_opacity(yg, 8);
  1360. } else if ((me->event.buttons & YUTANI_MOUSE_SCROLL_DOWN) && (yg->kbd_state.k_alt)) {
  1361. adjust_window_opacity(yg, -8);
  1362. } else if ((me->event.buttons & YUTANI_MOUSE_BUTTON_RIGHT) && (yg->kbd_state.k_alt)) {
  1363. mouse_start_rotate(yg);
  1364. } else if ((me->event.buttons & YUTANI_MOUSE_BUTTON_MIDDLE) && (yg->kbd_state.k_alt)) {
  1365. yg->resizing_button = YUTANI_MOUSE_BUTTON_MIDDLE;
  1366. mouse_start_resize(yg, SCALE_AUTO);
  1367. } else if ((me->event.buttons & YUTANI_MOUSE_BUTTON_LEFT) && (!yg->kbd_state.k_alt)) {
  1368. yg->mouse_state = YUTANI_MOUSE_STATE_DRAGGING;
  1369. set_focused_at(yg, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE);
  1370. yg->mouse_window = get_focused(yg);
  1371. yg->mouse_moved = 0;
  1372. yg->mouse_drag_button = YUTANI_MOUSE_BUTTON_LEFT;
  1373. if (yg->mouse_window) {
  1374. device_to_window(yg->mouse_window, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE, &yg->mouse_click_x, &yg->mouse_click_y);
  1375. yutani_msg_t * response = yutani_msg_build_window_mouse_event(yg->mouse_window->wid, yg->mouse_click_x, yg->mouse_click_y, -1, -1, me->event.buttons, YUTANI_MOUSE_EVENT_DOWN);
  1376. yg->mouse_click_x_orig = yg->mouse_click_x;
  1377. yg->mouse_click_y_orig = yg->mouse_click_y;
  1378. pex_send(yg->server, yg->mouse_window->owner, response->size, (char *)response);
  1379. free(response);
  1380. }
  1381. } else {
  1382. yg->mouse_window = get_focused(yg);
  1383. yutani_server_window_t * tmp_window = top_at(yg, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE);
  1384. if (yg->mouse_window) {
  1385. int32_t x, y;
  1386. device_to_window(yg->mouse_window, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE, &x, &y);
  1387. yutani_msg_t * response = yutani_msg_build_window_mouse_event(yg->mouse_window->wid, x, y, -1, -1, me->event.buttons, YUTANI_MOUSE_EVENT_MOVE);
  1388. pex_send(yg->server, yg->mouse_window->owner, response->size, (char *)response);
  1389. free(response);
  1390. }
  1391. if (tmp_window) {
  1392. int32_t x, y;
  1393. yutani_msg_t * response;
  1394. if (tmp_window != yg->old_hover_window) {
  1395. device_to_window(tmp_window, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE, &x, &y);
  1396. response = yutani_msg_build_window_mouse_event(tmp_window->wid, x, y, -1, -1, me->event.buttons, YUTANI_MOUSE_EVENT_ENTER);
  1397. pex_send(yg->server, tmp_window->owner, response->size, (char *)response);
  1398. free(response);
  1399. if (yg->old_hover_window) {
  1400. device_to_window(yg->old_hover_window, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE, &x, &y);
  1401. response = yutani_msg_build_window_mouse_event(yg->old_hover_window->wid, x, y, -1, -1, me->event.buttons, YUTANI_MOUSE_EVENT_LEAVE);
  1402. pex_send(yg->server, yg->old_hover_window->owner, response->size, (char *)response);
  1403. free(response);
  1404. }
  1405. yg->old_hover_window = tmp_window;
  1406. }
  1407. if (tmp_window != yg->mouse_window) {
  1408. device_to_window(tmp_window, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE, &x, &y);
  1409. response = yutani_msg_build_window_mouse_event(tmp_window->wid, x, y, -1, -1, me->event.buttons, YUTANI_MOUSE_EVENT_MOVE);
  1410. pex_send(yg->server, tmp_window->owner, response->size, (char *)response);
  1411. free(response);
  1412. }
  1413. }
  1414. }
  1415. }
  1416. break;
  1417. case YUTANI_MOUSE_STATE_MOVING:
  1418. {
  1419. int button_down = (me->event.buttons & YUTANI_MOUSE_BUTTON_LEFT);
  1420. int drag_stop = yg->mouse_drag_button != 0 ? (!button_down) : (button_down);
  1421. if (drag_stop) {
  1422. yg->mouse_window = NULL;
  1423. yg->mouse_state = YUTANI_MOUSE_STATE_NORMAL;
  1424. mark_screen(yg, yg->mouse_x / MOUSE_SCALE - MOUSE_OFFSET_X, yg->mouse_y / MOUSE_SCALE - MOUSE_OFFSET_Y, MOUSE_WIDTH, MOUSE_HEIGHT);
  1425. } else {
  1426. if (yg->mouse_y / MOUSE_SCALE < 2) {
  1427. window_tile(yg, yg->mouse_window, 1, 1, 0, 0);
  1428. yg->mouse_window = NULL;
  1429. yg->mouse_state = YUTANI_MOUSE_STATE_NORMAL;
  1430. break;
  1431. }
  1432. if (yg->mouse_window->tiled) {
  1433. if ((abs(yg->mouse_x - yg->mouse_init_x) > UNTILE_SENSITIVITY) || (abs(yg->mouse_y - yg->mouse_init_y) > UNTILE_SENSITIVITY)) {
  1434. /* Untile it */
  1435. yg->mouse_window->tiled = 0;
  1436. /* Position the window such that it's representative of where it was, percentage-wise, in the untiled window */
  1437. float percent_x = (float)(yg->mouse_x / MOUSE_SCALE - yg->mouse_window->x) / (float)yg->mouse_window->width;
  1438. float percent_y = (float)(yg->mouse_y / MOUSE_SCALE - yg->mouse_window->y) / (float)yg->mouse_window->height;
  1439. window_move(yg, yg->mouse_window,
  1440. yg->mouse_x / MOUSE_SCALE - yg->mouse_window->untiled_width * percent_x,
  1441. yg->mouse_y / MOUSE_SCALE - yg->mouse_window->untiled_height * percent_y);
  1442. yutani_msg_t * response = yutani_msg_build_window_resize(YUTANI_MSG_RESIZE_OFFER, yg->mouse_window->wid, yg->mouse_window->untiled_width, yg->mouse_window->untiled_height, 0);
  1443. pex_send(yg->server, yg->mouse_window->owner, response->size, (char *)response);
  1444. free(response);
  1445. /* reset init_x / init_y */
  1446. yg->mouse_init_x = yg->mouse_x;
  1447. yg->mouse_init_y = yg->mouse_y;
  1448. yg->mouse_win_x = yg->mouse_window->x;
  1449. yg->mouse_win_y = yg->mouse_window->y;
  1450. }
  1451. } else {
  1452. int x, y;
  1453. x = yg->mouse_win_x + (yg->mouse_x - yg->mouse_init_x) / MOUSE_SCALE;
  1454. y = yg->mouse_win_y + (yg->mouse_y - yg->mouse_init_y) / MOUSE_SCALE;
  1455. window_move(yg, yg->mouse_window, x, y);
  1456. }
  1457. }
  1458. }
  1459. break;
  1460. case YUTANI_MOUSE_STATE_ROTATING:
  1461. {
  1462. #if 0
  1463. if (!(me->event.buttons & YUTANI_MOUSE_BUTTON_RIGHT)) {
  1464. yg->mouse_window = NULL;
  1465. yg->mouse_state = YUTANI_MOUSE_STATE_NORMAL;
  1466. mark_screen(yg, yg->mouse_x / MOUSE_SCALE - MOUSE_OFFSET_X, yg->mouse_y / MOUSE_SCALE - MOUSE_OFFSET_Y, MOUSE_WIDTH, MOUSE_HEIGHT);
  1467. } else if (yg->mouse_window) {
  1468. /* Calculate rotation and make relative to initial rotation */
  1469. int32_t x_diff = yg->mouse_x / MOUSE_SCALE - (yg->mouse_window->x + yg->mouse_window->width / 2);
  1470. int32_t y_diff = yg->mouse_y / MOUSE_SCALE - (yg->mouse_window->y + yg->mouse_window->height / 2);
  1471. int new_r = __builtin_atan2(x_diff, y_diff) * 180.0 / (-M_PI);
  1472. mark_window(yg, yg->mouse_window);
  1473. yg->mouse_window->rotation = new_r + yg->mouse_init_r;
  1474. mark_window(yg, yg->mouse_window);
  1475. }
  1476. #endif
  1477. }
  1478. break;
  1479. case YUTANI_MOUSE_STATE_DRAGGING:
  1480. {
  1481. if (!(me->event.buttons & yg->mouse_drag_button)) {
  1482. /* Mouse released */
  1483. yg->mouse_state = YUTANI_MOUSE_STATE_NORMAL;
  1484. int32_t old_x = yg->mouse_click_x_orig;
  1485. int32_t old_y = yg->mouse_click_y_orig;
  1486. if (yg->mouse_window) {
  1487. device_to_window(yg->mouse_window, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE, &yg->mouse_click_x, &yg->mouse_click_y);
  1488. if (!yg->mouse_moved) {
  1489. yutani_msg_t * response = yutani_msg_build_window_mouse_event(yg->mouse_window->wid, yg->mouse_click_x, yg->mouse_click_y, -1, -1, me->event.buttons, YUTANI_MOUSE_EVENT_CLICK);
  1490. pex_send(yg->server, yg->mouse_window->owner, response->size, (char *)response);
  1491. free(response);
  1492. } else {
  1493. yutani_msg_t * response = yutani_msg_build_window_mouse_event(yg->mouse_window->wid, yg->mouse_click_x, yg->mouse_click_y, old_x, old_y, me->event.buttons, YUTANI_MOUSE_EVENT_RAISE);
  1494. pex_send(yg->server, yg->mouse_window->owner, response->size, (char *)response);
  1495. free(response);
  1496. }
  1497. }
  1498. } else {
  1499. yg->mouse_state = YUTANI_MOUSE_STATE_DRAGGING;
  1500. yg->mouse_moved = 1;
  1501. int32_t old_x = yg->mouse_click_x;
  1502. int32_t old_y = yg->mouse_click_y;
  1503. if (yg->mouse_window) {
  1504. device_to_window(yg->mouse_window, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE, &yg->mouse_click_x, &yg->mouse_click_y);
  1505. if (old_x != yg->mouse_click_x || old_y != yg->mouse_click_y) {
  1506. yutani_msg_t * response = yutani_msg_build_window_mouse_event(yg->mouse_window->wid, yg->mouse_click_x, yg->mouse_click_y, old_x, old_y, me->event.buttons, YUTANI_MOUSE_EVENT_DRAG);
  1507. pex_send(yg->server, yg->mouse_window->owner, response->size, (char *)response);
  1508. free(response);
  1509. }
  1510. }
  1511. }
  1512. }
  1513. break;
  1514. case YUTANI_MOUSE_STATE_RESIZING:
  1515. {
  1516. int32_t relative_x, relative_y;
  1517. int32_t relative_init_x, relative_init_y;
  1518. device_to_window(yg->resizing_window, yg->mouse_init_x / MOUSE_SCALE, yg->mouse_init_y / MOUSE_SCALE, &relative_init_x, &relative_init_y);
  1519. device_to_window(yg->resizing_window, yg->mouse_x / MOUSE_SCALE, yg->mouse_y / MOUSE_SCALE, &relative_x, &relative_y);
  1520. int width_diff = (relative_x - relative_init_x);
  1521. int height_diff = (relative_y - relative_init_y);
  1522. mark_window(yg, yg->resizing_window);
  1523. if (yg->resizing_direction == SCALE_UP || yg->resizing_direction == SCALE_DOWN) {
  1524. width_diff = 0;
  1525. yg->resizing_offset_x = 0;
  1526. }
  1527. if (yg->resizing_direction == SCALE_LEFT || yg->resizing_direction == SCALE_RIGHT) {
  1528. height_diff = 0;
  1529. yg->resizing_offset_y = 0;
  1530. }
  1531. if (yg->resizing_direction == SCALE_LEFT ||
  1532. yg->resizing_direction == SCALE_UP_LEFT ||
  1533. yg->resizing_direction == SCALE_DOWN_LEFT) {
  1534. yg->resizing_offset_x = width_diff;
  1535. width_diff = -width_diff;
  1536. } else if (yg->resizing_direction == SCALE_RIGHT ||
  1537. yg->resizing_direction == SCALE_UP_RIGHT ||
  1538. yg->resizing_direction == SCALE_DOWN_RIGHT) {
  1539. yg->resizing_offset_x = 0;
  1540. }
  1541. if (yg->resizing_direction == SCALE_UP ||
  1542. yg->resizing_direction == SCALE_UP_LEFT ||
  1543. yg->resizing_direction == SCALE_UP_RIGHT) {
  1544. yg->resizing_offset_y = height_diff;
  1545. height_diff = -height_diff;
  1546. } else if (yg->resizing_direction == SCALE_DOWN ||
  1547. yg->resizing_direction == SCALE_DOWN_LEFT ||
  1548. yg->resizing_direction == SCALE_DOWN_RIGHT) {
  1549. yg->resizing_offset_y = 0;
  1550. }
  1551. yg->resizing_w = yg->resizing_window->width + width_diff;
  1552. yg->resizing_h = yg->resizing_window->height + height_diff;
  1553. /* Enforce logical boundaries */
  1554. if (yg->resizing_w < 1) {
  1555. yg->resizing_w = 1;
  1556. }
  1557. if (yg->resizing_h < 1) {
  1558. yg->resizing_h = 1;
  1559. }
  1560. if (yg->resizing_offset_x > yg->resizing_window->width) {
  1561. yg->resizing_offset_x = yg->resizing_window->width;
  1562. }
  1563. if (yg->resizing_offset_y > yg->resizing_window->height) {
  1564. yg->resizing_offset_y = yg->resizing_window->height;
  1565. }
  1566. mark_window(yg, yg->resizing_window);
  1567. if (!(me->event.buttons & yg->resizing_button)) {
  1568. int32_t x, y;
  1569. if (yg->resizing_window->rotation) {
  1570. /* If the window is rotated, we need to move the center to be where the new center should be, but x/y are based on the unrotated upper left corner. */
  1571. /* The center always moves by one-half the resize dimensions */
  1572. int32_t center_x, center_y;
  1573. window_to_device(yg->resizing_window, yg->resizing_offset_x + yg->resizing_w / 2, yg->resizing_offset_y + yg->resizing_h / 2, &center_x, &center_y);
  1574. x = center_x - yg->resizing_w / 2;
  1575. y = center_y - yg->resizing_h / 2;
  1576. } else {
  1577. window_to_device(yg->resizing_window, yg->resizing_offset_x, yg->resizing_offset_y, &x, &y);
  1578. }
  1579. TRACE("resize complete, now %d x %d", yg->resizing_w, yg->resizing_h);
  1580. window_move(yg, yg->resizing_window, x,y);
  1581. yutani_msg_t * response = yutani_msg_build_window_resize(YUTANI_MSG_RESIZE_OFFER, yg->resizing_window->wid, yg->resizing_w, yg->resizing_h, 0);
  1582. pex_send(yg->server, yg->resizing_window->owner, response->size, (char *)response);
  1583. free(response);
  1584. yg->resizing_window = NULL;
  1585. yg->mouse_window = NULL;
  1586. yg->mouse_state = YUTANI_MOUSE_STATE_NORMAL;
  1587. }
  1588. }
  1589. break;
  1590. default:
  1591. /* XXX ? */
  1592. break;
  1593. }
  1594. }
  1595. static yutani_globals_t * _static_yg;
  1596. static void yutani_display_resize_handle(int signum) {
  1597. (void)signum;
  1598. TRACE("Display change request, one moment.");
  1599. _static_yg->resize_on_next = 1;
  1600. }
  1601. /**
  1602. * main
  1603. */
  1604. int main(int argc, char * argv[]) {
  1605. int argx = 0;
  1606. int results = parse_args(argc, argv, &argx);
  1607. if (results) return results;
  1608. yutani_globals_t * yg = malloc(sizeof(yutani_globals_t));
  1609. memset(yg, 0x00, sizeof(yutani_globals_t));
  1610. if (yutani_options.nested) {
  1611. yg->host_context = yutani_init();
  1612. yg->host_window = yutani_window_create(yg->host_context, yutani_options.nest_width, yutani_options.nest_height);
  1613. yutani_window_move(yg->host_context, yg->host_window, 50, 50);
  1614. yutani_window_advertise_icon(yg->host_context, yg->host_window, "Compositor", "compositor");
  1615. yg->backend_ctx = init_graphics_yutani_double_buffer(yg->host_window);
  1616. yg->stride = yg->backend_ctx->width * 4;
  1617. } else {
  1618. _static_yg = yg;
  1619. signal(SIGWINEVENT, yutani_display_resize_handle);
  1620. yg->backend_ctx = init_graphics_fullscreen_double_buffer();
  1621. yg->stride = framebuffer_stride();
  1622. }
  1623. if (!yg->backend_ctx) {
  1624. free(yg);
  1625. TRACE("Failed to open framebuffer, bailing.");
  1626. return 1;
  1627. }
  1628. {
  1629. struct timeval t;
  1630. gettimeofday(&t, NULL);
  1631. yg->start_time = t.tv_sec;
  1632. yg->start_subtime = t.tv_usec;
  1633. }
  1634. yg->width = yg->backend_ctx->width;
  1635. yg->height = yg->backend_ctx->height;
  1636. draw_fill(yg->backend_ctx, rgb(110,110,110));
  1637. flip(yg->backend_ctx);
  1638. yg->backend_framebuffer = yg->backend_ctx->backbuffer;
  1639. if (yutani_options.nested) {
  1640. char * name = malloc(sizeof(char) * 64);
  1641. sprintf(name, "compositor-nest-%d", getpid());
  1642. yg->server_ident = name;
  1643. } else {
  1644. /* XXX check if this already exists? */
  1645. yg->server_ident = "compositor";
  1646. }
  1647. // XXX need setenv to get display variable
  1648. //setenv("DISPLAY", yg->server_ident, 1);
  1649. char * _env = malloc(sizeof(char) * 64);
  1650. sprintf(_env, "DISPLAY=%s", yg->server_ident);
  1651. /* count environ */
  1652. size_t env_c = 0;
  1653. for (char ** env = environ; *env; env++, env_c++);
  1654. char ** env_new = malloc(sizeof(char *) * (env_c + 2));
  1655. int set_env = 0;
  1656. for (size_t i = 0; i < env_c; ++i) {
  1657. if (strstr(environ[i], "DISPLAY=") == environ[i]) {
  1658. TRACE("Display already set, replacing.\n");
  1659. env_new[i] = _env;
  1660. set_env = 1;
  1661. } else {
  1662. env_new[i] = environ[i];
  1663. }
  1664. }
  1665. if (!set_env) {
  1666. env_new[env_c] = _env;
  1667. env_c++;
  1668. }
  1669. env_new[env_c] = NULL;
  1670. environ = env_new;
  1671. TRACE("environment has %d items", env_c);
  1672. FILE * server = pex_bind(yg->server_ident);
  1673. TRACE("pex bound? %d", server);
  1674. yg->server = server;
  1675. #if 0
  1676. load_sprite_png(&yg->mouse_sprite, "/usr/share/cursor/normal.png");
  1677. load_sprite_png(&yg->mouse_sprite_drag, "/usr/share/cursor/drag.png");
  1678. load_sprite_png(&yg->mouse_sprite_resize_v, "/usr/share/cursor/resize-vertical.png");
  1679. load_sprite_png(&yg->mouse_sprite_resize_h, "/usr/share/cursor/resize-horizontal.png");
  1680. load_sprite_png(&yg->mouse_sprite_resize_da, "/usr/share/cursor/resize-uldr.png");
  1681. load_sprite_png(&yg->mouse_sprite_resize_db, "/usr/share/cursor/resize-dlur.png");
  1682. #endif
  1683. TRACE("Loading sprites...");
  1684. #define MOUSE_DIR "/usr/share/cursor/"
  1685. load_sprite(&yg->mouse_sprite, MOUSE_DIR "mouse.bmp");
  1686. yg->mouse_sprite.alpha = ALPHA_EMBEDDED;
  1687. load_sprite(&yg->mouse_sprite_drag, MOUSE_DIR "drag.bmp");
  1688. yg->mouse_sprite_drag.alpha = ALPHA_EMBEDDED;
  1689. load_sprite(&yg->mouse_sprite_resize_v, MOUSE_DIR "resize-vertical.bmp");
  1690. yg->mouse_sprite_resize_v.alpha = ALPHA_EMBEDDED;
  1691. load_sprite(&yg->mouse_sprite_resize_h, MOUSE_DIR "resize-horizontal.bmp");
  1692. yg->mouse_sprite_resize_h.alpha = ALPHA_EMBEDDED;
  1693. load_sprite(&yg->mouse_sprite_resize_da, MOUSE_DIR "resize-uldr.bmp");
  1694. yg->mouse_sprite_resize_da.alpha = ALPHA_EMBEDDED;
  1695. load_sprite(&yg->mouse_sprite_resize_db, MOUSE_DIR "resize-dlur.bmp");
  1696. yg->mouse_sprite_resize_db.alpha = ALPHA_EMBEDDED;
  1697. TRACE("Done.");
  1698. TRACE("Initializing variables...");
  1699. yg->last_mouse_x = 0;
  1700. yg->last_mouse_y = 0;
  1701. yg->mouse_x = yg->width * MOUSE_SCALE / 2;
  1702. yg->mouse_y = yg->height * MOUSE_SCALE / 2;
  1703. yg->windows = list_create();
  1704. yg->wids_to_windows = hashmap_create_int(10);
  1705. yg->key_binds = hashmap_create_int(10);
  1706. yg->clients_to_windows = hashmap_create_int(10);
  1707. yg->mid_zs = list_create();
  1708. yg->window_subscribers = list_create();
  1709. yg->last_mouse_buttons = 0;
  1710. TRACE("Done.");
  1711. yutani_clip_init(yg);
  1712. pthread_t render_thread;
  1713. TRACE("Starting render thread.");
  1714. pthread_create(&render_thread, NULL, redraw, yg);
  1715. if (!fork()) {
  1716. if (argx < argc) {
  1717. TRACE("Starting alternate startup app: %s", argv[argx]);
  1718. execvp(argv[argx], &argv[argx]);
  1719. } else {
  1720. TRACE("Starting application");
  1721. char * args[] = {"/bin/session", NULL};
  1722. execvp(args[0], args);
  1723. TRACE("Failed to start app?");
  1724. }
  1725. }
  1726. int fds[4], mfd, kfd, amfd;
  1727. int vmmouse = 0;
  1728. mouse_device_packet_t packet;
  1729. key_event_t event;
  1730. key_event_state_t state = {0};
  1731. fds[0] = fileno(server);
  1732. if (yutani_options.nested) {
  1733. fds[1] = fileno(yg->host_context->sock);
  1734. } else {
  1735. mfd = open("/dev/mouse", O_RDONLY);
  1736. kfd = open("/dev/kbd", O_RDONLY);
  1737. amfd = open("/dev/absmouse", O_RDONLY);
  1738. if (amfd == -1) {
  1739. amfd = open("/dev/vmmouse", O_RDONLY);
  1740. vmmouse = 1;
  1741. }
  1742. fds[1] = mfd;
  1743. fds[2] = kfd;
  1744. fds[3] = amfd;
  1745. }
  1746. while (1) {
  1747. if (yutani_options.nested) {
  1748. int index = syscall_fswait(2, fds);
  1749. if (index == 1) {
  1750. yutani_msg_t * m = yutani_poll(yg->host_context);
  1751. if (m) {
  1752. switch (m->type) {
  1753. case YUTANI_MSG_KEY_EVENT:
  1754. {
  1755. struct yutani_msg_key_event * ke = (void*)m->data;
  1756. yutani_msg_t * m_ = yutani_msg_build_key_event(0, &ke->event, &ke->state);
  1757. handle_key_event(yg, (struct yutani_msg_key_event *)m_->data);
  1758. free(m_);
  1759. }
  1760. break;
  1761. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  1762. {
  1763. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  1764. mouse_device_packet_t packet;
  1765. packet.buttons = me->buttons;
  1766. packet.x_difference = me->new_x;
  1767. packet.y_difference = me->new_y;
  1768. yg->last_mouse_buttons = packet.buttons;
  1769. yutani_msg_t * m_ = yutani_msg_build_mouse_event(0, &packet, YUTANI_MOUSE_EVENT_TYPE_ABSOLUTE);
  1770. handle_mouse_event(yg, (struct yutani_msg_mouse_event *)m_->data);
  1771. free(m_);
  1772. }
  1773. break;
  1774. case YUTANI_MSG_RESIZE_OFFER:
  1775. {
  1776. struct yutani_msg_window_resize * wr = (void*)m->data;
  1777. TRACE("Resize request from host compositor for size %dx%d", wr->width, wr->height);
  1778. yutani_window_resize_accept(yg->host_context, yg->host_window, wr->width, wr->height);
  1779. yg->resize_on_next = 1;
  1780. }
  1781. break;
  1782. case YUTANI_MSG_SESSION_END:
  1783. {
  1784. TRACE("Host session ended. Should exit.");
  1785. yutani_msg_t * response = yutani_msg_build_session_end();
  1786. pex_broadcast(server, response->size, (char *)response);
  1787. free(response);
  1788. kill(render_thread.id, SIGINT);
  1789. exit(0);
  1790. }
  1791. break;
  1792. default:
  1793. break;
  1794. }
  1795. }
  1796. free(m);
  1797. continue;
  1798. }
  1799. } else {
  1800. int index = syscall_fswait(amfd == -1 ? 3 : 4, fds);
  1801. if (index == 2) {
  1802. char buf[1];
  1803. int r = read(kfd, buf, 1);
  1804. if (r > 0) {
  1805. kbd_scancode(&state, buf[0], &event);
  1806. yutani_msg_t * m = yutani_msg_build_key_event(0, &event, &state);
  1807. handle_key_event(yg, (struct yutani_msg_key_event *)m->data);
  1808. free(m);
  1809. }
  1810. continue;
  1811. } else if (index == 1) {
  1812. int r = read(mfd, (char *)&packet, sizeof(mouse_device_packet_t));
  1813. if (r > 0) {
  1814. yg->last_mouse_buttons = packet.buttons;
  1815. yutani_msg_t * m = yutani_msg_build_mouse_event(0, &packet, YUTANI_MOUSE_EVENT_TYPE_RELATIVE);
  1816. handle_mouse_event(yg, (struct yutani_msg_mouse_event *)m->data);
  1817. free(m);
  1818. }
  1819. continue;
  1820. } else if (index == 3) {
  1821. int r = read(amfd, (char *)&packet, sizeof(mouse_device_packet_t));
  1822. if (r > 0) {
  1823. if (!vmmouse) {
  1824. packet.buttons = yg->last_mouse_buttons & 0xF;
  1825. } else {
  1826. yg->last_mouse_buttons = packet.buttons;
  1827. }
  1828. yutani_msg_t * m = yutani_msg_build_mouse_event(0, &packet, YUTANI_MOUSE_EVENT_TYPE_ABSOLUTE);
  1829. handle_mouse_event(yg, (struct yutani_msg_mouse_event *)m->data);
  1830. free(m);
  1831. }
  1832. continue;
  1833. }
  1834. }
  1835. pex_packet_t * p = calloc(PACKET_SIZE, 1);
  1836. pex_listen(server, p);
  1837. yutani_msg_t * m = (yutani_msg_t *)p->data;
  1838. if (p->size == 0) {
  1839. /* Connection closed for client */
  1840. TRACE("Connection closed for client %x", p->source);
  1841. list_t * client_list = hashmap_get(yg->clients_to_windows, (void *)p->source);
  1842. if (client_list) {
  1843. foreach(node, client_list) {
  1844. yutani_server_window_t * win = node->value;
  1845. TRACE("Killing window %d", win->wid);
  1846. window_mark_for_close(yg, win);
  1847. }
  1848. hashmap_remove(yg->clients_to_windows, (void *)p->source);
  1849. list_free(client_list);
  1850. free(client_list);
  1851. }
  1852. free(p);
  1853. continue;
  1854. }
  1855. if (m->magic != YUTANI_MSG__MAGIC) {
  1856. TRACE("Message has bad magic. (Should eject client, but will instead skip this message.) 0x%x", m->magic);
  1857. free(p);
  1858. continue;
  1859. }
  1860. switch(m->type) {
  1861. case YUTANI_MSG_HELLO:
  1862. {
  1863. TRACE("And hello to you, %08x!", p->source);
  1864. list_t * client_list = hashmap_get(yg->clients_to_windows, (void *)p->source);
  1865. if (!client_list) {
  1866. TRACE("Client is new: %x", p->source);
  1867. client_list = list_create();
  1868. hashmap_set(yg->clients_to_windows, (void *)p->source, client_list);
  1869. }
  1870. yutani_msg_t * response = yutani_msg_build_welcome(yg->width, yg->height);
  1871. pex_send(server, p->source, response->size, (char *)response);
  1872. free(response);
  1873. }
  1874. break;
  1875. case YUTANI_MSG_WINDOW_NEW:
  1876. case YUTANI_MSG_WINDOW_NEW_FLAGS:
  1877. {
  1878. struct yutani_msg_window_new_flags * wn = (void *)m->data;
  1879. TRACE("Client %08x requested a new window (%dx%d).", p->source, wn->width, wn->height);
  1880. yutani_server_window_t * w = server_window_create(yg, wn->width, wn->height, p->source, m->type != YUTANI_MSG_WINDOW_NEW ? wn->flags : 0);
  1881. yutani_msg_t * response = yutani_msg_build_window_init(w->wid, w->width, w->height, w->bufid);
  1882. pex_send(server, p->source, response->size, (char *)response);
  1883. free(response);
  1884. if (!(w->server_flags & YUTANI_WINDOW_FLAG_NO_STEAL_FOCUS)) {
  1885. set_focused_window(yg, w);
  1886. }
  1887. notify_subscribers(yg);
  1888. }
  1889. break;
  1890. case YUTANI_MSG_FLIP:
  1891. {
  1892. struct yutani_msg_flip * wf = (void *)m->data;
  1893. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wf->wid);
  1894. if (w) {
  1895. mark_window(yg, w);
  1896. }
  1897. }
  1898. break;
  1899. case YUTANI_MSG_FLIP_REGION:
  1900. {
  1901. struct yutani_msg_flip_region * wf = (void *)m->data;
  1902. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wf->wid);
  1903. if (w) {
  1904. mark_window_relative(yg, w, wf->x, wf->y, wf->width, wf->height);
  1905. }
  1906. }
  1907. break;
  1908. case YUTANI_MSG_KEY_EVENT:
  1909. {
  1910. /* XXX Verify this is from a valid device client */
  1911. struct yutani_msg_key_event * ke = (void *)m->data;
  1912. handle_key_event(yg, ke);
  1913. }
  1914. break;
  1915. case YUTANI_MSG_MOUSE_EVENT:
  1916. {
  1917. /* XXX Verify this is from a valid device client */
  1918. struct yutani_msg_mouse_event * me = (void *)m->data;
  1919. handle_mouse_event(yg, me);
  1920. }
  1921. break;
  1922. case YUTANI_MSG_WINDOW_MOVE:
  1923. {
  1924. struct yutani_msg_window_move * wm = (void *)m->data;
  1925. TRACE("%08x wanted to move window %d to %d, %d", p->source, wm->wid, (int)wm->x, (int)wm->y);
  1926. if (wm->x > (int)yg->width + 100 || wm->x < -(int)yg->width || wm->y > (int)yg->height + 100 || wm->y < -(int)yg->height) {
  1927. TRACE("Refusing to move window to these coordinates.");
  1928. break;
  1929. }
  1930. yutani_server_window_t * win = hashmap_get(yg->wids_to_windows, (void*)wm->wid);
  1931. if (win) {
  1932. window_move(yg, win, wm->x, wm->y);
  1933. } else {
  1934. TRACE("%08x wanted to move window %d, but I can't find it?", p->source, wm->wid);
  1935. }
  1936. }
  1937. break;
  1938. case YUTANI_MSG_WINDOW_CLOSE:
  1939. {
  1940. struct yutani_msg_window_close * wc = (void *)m->data;
  1941. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wc->wid);
  1942. if (w) {
  1943. window_mark_for_close(yg, w);
  1944. window_remove_from_client(yg, w);
  1945. }
  1946. }
  1947. break;
  1948. case YUTANI_MSG_WINDOW_STACK:
  1949. {
  1950. struct yutani_msg_window_stack * ws = (void *)m->data;
  1951. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)ws->wid);
  1952. if (w) {
  1953. reorder_window(yg, w, ws->z);
  1954. }
  1955. }
  1956. break;
  1957. case YUTANI_MSG_RESIZE_REQUEST:
  1958. {
  1959. struct yutani_msg_window_resize * wr = (void *)m->data;
  1960. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wr->wid);
  1961. if (w) {
  1962. yutani_msg_t * response = yutani_msg_build_window_resize(YUTANI_MSG_RESIZE_OFFER, w->wid, wr->width, wr->height, 0);
  1963. pex_send(server, p->source, response->size, (char *)response);
  1964. free(response);
  1965. }
  1966. }
  1967. break;
  1968. case YUTANI_MSG_RESIZE_OFFER:
  1969. {
  1970. struct yutani_msg_window_resize * wr = (void *)m->data;
  1971. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wr->wid);
  1972. if (w) {
  1973. yutani_msg_t * response = yutani_msg_build_window_resize(YUTANI_MSG_RESIZE_OFFER, w->wid, wr->width, wr->height, 0);
  1974. pex_send(server, p->source, response->size, (char *)response);
  1975. free(response);
  1976. }
  1977. }
  1978. break;
  1979. case YUTANI_MSG_RESIZE_ACCEPT:
  1980. {
  1981. struct yutani_msg_window_resize * wr = (void *)m->data;
  1982. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wr->wid);
  1983. if (w) {
  1984. uint32_t newbufid = server_window_resize(yg, w, wr->width, wr->height);
  1985. yutani_msg_t * response = yutani_msg_build_window_resize(YUTANI_MSG_RESIZE_BUFID, w->wid, wr->width, wr->height, newbufid);
  1986. pex_send(server, p->source, response->size, (char *)response);
  1987. free(response);
  1988. }
  1989. }
  1990. break;
  1991. case YUTANI_MSG_RESIZE_DONE:
  1992. {
  1993. struct yutani_msg_window_resize * wr = (void *)m->data;
  1994. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wr->wid);
  1995. if (w) {
  1996. server_window_resize_finish(yg, w, wr->width, wr->height);
  1997. }
  1998. }
  1999. break;
  2000. case YUTANI_MSG_QUERY_WINDOWS:
  2001. {
  2002. yutani_query_result(yg, p->source, yg->bottom_z);
  2003. foreach (node, yg->mid_zs) {
  2004. yutani_query_result(yg, p->source, node->value);
  2005. }
  2006. yutani_query_result(yg, p->source, yg->top_z);
  2007. yutani_msg_t * response = yutani_msg_build_window_advertise(0, 0, NULL, 0, NULL);
  2008. pex_send(server, p->source, response->size, (char *)response);
  2009. free(response);
  2010. }
  2011. break;
  2012. case YUTANI_MSG_SUBSCRIBE:
  2013. {
  2014. foreach(node, yg->window_subscribers) {
  2015. if ((uint32_t)node->value == p->source) {
  2016. break;
  2017. }
  2018. }
  2019. list_insert(yg->window_subscribers, (void*)p->source);
  2020. }
  2021. break;
  2022. case YUTANI_MSG_UNSUBSCRIBE:
  2023. {
  2024. node_t * node = list_find(yg->window_subscribers, (void*)p->source);
  2025. if (node) {
  2026. list_delete(yg->window_subscribers, node);
  2027. }
  2028. }
  2029. break;
  2030. case YUTANI_MSG_WINDOW_ADVERTISE:
  2031. {
  2032. struct yutani_msg_window_advertise * wa = (void *)m->data;
  2033. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wa->wid);
  2034. if (w) {
  2035. if (w->client_strings) free(w->client_strings);
  2036. for (int i = 0; i < 5; ++i) {
  2037. w->client_offsets[i] = wa->offsets[i];
  2038. }
  2039. w->client_flags = wa->flags;
  2040. w->client_length = wa->size;
  2041. w->client_strings = malloc(wa->size);
  2042. memcpy(w->client_strings, wa->strings, wa->size);
  2043. notify_subscribers(yg);
  2044. }
  2045. }
  2046. break;
  2047. case YUTANI_MSG_SESSION_END:
  2048. {
  2049. yutani_msg_t * response = yutani_msg_build_session_end();
  2050. pex_broadcast(server, response->size, (char *)response);
  2051. free(response);
  2052. }
  2053. break;
  2054. case YUTANI_MSG_WINDOW_FOCUS:
  2055. {
  2056. struct yutani_msg_window_focus * wa = (void *)m->data;
  2057. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wa->wid);
  2058. if (w) {
  2059. set_focused_window(yg, w);
  2060. }
  2061. }
  2062. break;
  2063. case YUTANI_MSG_KEY_BIND:
  2064. {
  2065. struct yutani_msg_key_bind * wa = (void *)m->data;
  2066. add_key_bind(yg, wa, p->source);
  2067. }
  2068. break;
  2069. case YUTANI_MSG_WINDOW_DRAG_START:
  2070. {
  2071. struct yutani_msg_window_drag_start * wa = (void *)m->data;
  2072. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wa->wid);
  2073. if (w) {
  2074. /* Start dragging */
  2075. mouse_start_drag(yg, w);
  2076. }
  2077. }
  2078. break;
  2079. case YUTANI_MSG_WINDOW_UPDATE_SHAPE:
  2080. {
  2081. struct yutani_msg_window_update_shape * wa = (void *)m->data;
  2082. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wa->wid);
  2083. if (w) {
  2084. /* Set shape parameter */
  2085. server_window_update_shape(yg, w, wa->set_shape);
  2086. }
  2087. }
  2088. break;
  2089. case YUTANI_MSG_WINDOW_WARP_MOUSE:
  2090. {
  2091. struct yutani_msg_window_warp_mouse * wa = (void *)m->data;
  2092. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wa->wid);
  2093. if (w) {
  2094. if (yg->focused_window == w) {
  2095. int32_t x, y;
  2096. window_to_device(w, wa->x, wa->y, &x, &y);
  2097. struct yutani_msg_mouse_event me;
  2098. me.event.x_difference = x;
  2099. me.event.y_difference = y;
  2100. me.event.buttons = 0;
  2101. me.type = YUTANI_MOUSE_EVENT_TYPE_ABSOLUTE;
  2102. me.wid = wa->wid;
  2103. handle_mouse_event(yg, &me);
  2104. }
  2105. }
  2106. }
  2107. break;
  2108. case YUTANI_MSG_WINDOW_SHOW_MOUSE:
  2109. {
  2110. struct yutani_msg_window_show_mouse * wa = (void *)m->data;
  2111. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wa->wid);
  2112. if (w) {
  2113. if (wa->show_mouse == -1) {
  2114. w->show_mouse = w->default_mouse;
  2115. } else if (wa->show_mouse < 2) {
  2116. w->default_mouse = wa->show_mouse;
  2117. w->show_mouse = wa->show_mouse;
  2118. } else {
  2119. w->show_mouse = wa->show_mouse;
  2120. }
  2121. if (yg->focused_window == w) {
  2122. mark_screen(yg, yg->mouse_x / MOUSE_SCALE - MOUSE_OFFSET_X, yg->mouse_y / MOUSE_SCALE - MOUSE_OFFSET_Y, MOUSE_WIDTH, MOUSE_HEIGHT);
  2123. }
  2124. }
  2125. }
  2126. break;
  2127. case YUTANI_MSG_WINDOW_RESIZE_START:
  2128. {
  2129. struct yutani_msg_window_resize_start * wa = (void *)m->data;
  2130. yutani_server_window_t * w = hashmap_get(yg->wids_to_windows, (void *)wa->wid);
  2131. if (w) {
  2132. if (yg->focused_window == w && !yg->resizing_window) {
  2133. yg->resizing_window = w;
  2134. yg->resizing_button = YUTANI_MOUSE_BUTTON_LEFT; /* XXX Uh, what if we used something else */
  2135. mouse_start_resize(yg, wa->direction);
  2136. }
  2137. }
  2138. }
  2139. break;
  2140. default:
  2141. {
  2142. TRACE("Unknown type: 0x%8x", m->type);
  2143. }
  2144. break;
  2145. }
  2146. free(p);
  2147. }
  2148. return 0;
  2149. }