rtgui_application.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. * File : rtgui_application.c
  3. * This file is part of RTGUI in RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-01-13 Grissiom first version(just a prototype of application API)
  13. */
  14. #include <rtgui/rtgui_system.h>
  15. #include <rtgui/rtgui_application.h>
  16. #include <rtgui/widgets/window.h>
  17. #ifdef _WIN32
  18. #define RTGUI_EVENT_DEBUG
  19. #endif
  20. #ifdef RTGUI_EVENT_DEBUG
  21. const char *event_string[] =
  22. {
  23. /* window event */
  24. "WIN_CREATE", /* create a window */
  25. "WIN_DESTROY", /* destroy a window */
  26. "WIN_SHOW", /* show a window */
  27. "WIN_HIDE", /* hide a window */
  28. "WIN_ACTIVATE", /* activate a window */
  29. "WIN_DEACTIVATE", /* deactivate a window */
  30. "WIN_CLOSE", /* close a window */
  31. "WIN_MOVE", /* move a window */
  32. "WIN_RESIZE", /* resize a window */
  33. "WIN_MODAL_ENTER", /* a window modals */
  34. "SET_WM", /* set window manager */
  35. "UPDATE_BEGIN", /* begin of update rect */
  36. "UPDATE_END", /* end of update rect */
  37. "MONITOR_ADD", /* add a monitor rect */
  38. "MONITOR_REMOVE", /* remove a monitor rect*/
  39. "PAINT", /* paint on screen */
  40. "TIMER", /* timer */
  41. /* clip rect information */
  42. "CLIP_INFO", /* clip rect info */
  43. /* mouse and keyboard event */
  44. "MOUSE_MOTION", /* mouse motion */
  45. "MOUSE_BUTTON", /* mouse button info */
  46. "KBD", /* keyboard info */
  47. /* user command event */
  48. "COMMAND", /* user command */
  49. /* request's status event */
  50. "STATUS", /* request result */
  51. "SCROLLED", /* scroll bar scrolled */
  52. "RESIZE", /* widget resize */
  53. };
  54. #define DBG_MSG(x) rt_kprintf x
  55. static void rtgui_event_dump(rt_thread_t tid, rtgui_event_t* event)
  56. {
  57. char* sender = "(unknown)";
  58. if ((event->type == RTGUI_EVENT_TIMER) ||
  59. (event->type == RTGUI_EVENT_UPDATE_BEGIN) ||
  60. (event->type == RTGUI_EVENT_MOUSE_MOTION) ||
  61. (event->type == RTGUI_EVENT_UPDATE_END))
  62. {
  63. /* don't dump timer event */
  64. return ;
  65. }
  66. if (event->sender != RT_NULL)
  67. sender = event->sender->name;
  68. rt_kprintf("%s -- %s --> %s ", sender, event_string[event->type], tid->name);
  69. switch (event->type)
  70. {
  71. case RTGUI_EVENT_PAINT:
  72. {
  73. struct rtgui_event_paint *paint = (struct rtgui_event_paint *)event;
  74. if(paint->wid != RT_NULL)
  75. rt_kprintf("win: %s", paint->wid->title);
  76. }
  77. break;
  78. case RTGUI_EVENT_KBD:
  79. {
  80. struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd*) event;
  81. if (ekbd->wid != RT_NULL)
  82. rt_kprintf("win: %s", ekbd->wid->title);
  83. if (RTGUI_KBD_IS_UP(ekbd)) rt_kprintf(", up");
  84. else rt_kprintf(", down");
  85. }
  86. break;
  87. case RTGUI_EVENT_CLIP_INFO:
  88. {
  89. struct rtgui_event_clip_info *info = (struct rtgui_event_clip_info *)event;
  90. if(info->wid != RT_NULL)
  91. rt_kprintf("win: %s", info->wid->title);
  92. }
  93. break;
  94. case RTGUI_EVENT_WIN_CREATE:
  95. {
  96. struct rtgui_event_win_create *create = (struct rtgui_event_win_create*)event;
  97. rt_kprintf(" win: %s at (x1:%d, y1:%d, x2:%d, y2:%d), addr: %p",
  98. #ifdef RTGUI_USING_SMALL_SIZE
  99. create->wid->title,
  100. RTGUI_WIDGET(create->wid)->extent.x1,
  101. RTGUI_WIDGET(create->wid)->extent.y1,
  102. RTGUI_WIDGET(create->wid)->extent.x2,
  103. RTGUI_WIDGET(create->wid)->extent.y2,
  104. #else
  105. create->title,
  106. create->extent.x1,
  107. create->extent.y1,
  108. create->extent.x2,
  109. create->extent.y2,
  110. #endif
  111. create->wid
  112. );
  113. }
  114. break;
  115. case RTGUI_EVENT_UPDATE_END:
  116. {
  117. struct rtgui_event_update_end* update_end = (struct rtgui_event_update_end*)event;
  118. rt_kprintf("(x:%d, y1:%d, x2:%d, y2:%d)", update_end->rect.x1,
  119. update_end->rect.y1,
  120. update_end->rect.x2,
  121. update_end->rect.y2);
  122. }
  123. break;
  124. case RTGUI_EVENT_WIN_ACTIVATE:
  125. case RTGUI_EVENT_WIN_DEACTIVATE:
  126. case RTGUI_EVENT_WIN_SHOW:
  127. case RTGUI_EVENT_WIN_MODAL_ENTER:
  128. case RTGUI_EVENT_WIN_HIDE:
  129. {
  130. struct rtgui_event_win *win = (struct rtgui_event_win *)event;
  131. if(win->wid != RT_NULL)
  132. rt_kprintf("win: %s", win->wid->title);
  133. }
  134. break;
  135. case RTGUI_EVENT_WIN_MOVE:
  136. {
  137. struct rtgui_event_win_move *win = (struct rtgui_event_win_move *)event;
  138. if(win->wid != RT_NULL)
  139. {
  140. rt_kprintf("win: %s", win->wid->title);
  141. rt_kprintf(" to (x:%d, y:%d)", win->x, win->y);
  142. }
  143. }
  144. break;
  145. case RTGUI_EVENT_WIN_RESIZE:
  146. {
  147. struct rtgui_event_win_resize* win = (struct rtgui_event_win_resize *)event;
  148. if (win->wid != RT_NULL)
  149. {
  150. rt_kprintf("win: %s, rect(x1:%d, y1:%d, x2:%d, y2:%d)", win->wid->title,
  151. RTGUI_WIDGET(win->wid)->extent.x1,
  152. RTGUI_WIDGET(win->wid)->extent.y1,
  153. RTGUI_WIDGET(win->wid)->extent.x2,
  154. RTGUI_WIDGET(win->wid)->extent.y2);
  155. }
  156. }
  157. break;
  158. case RTGUI_EVENT_MOUSE_BUTTON:
  159. case RTGUI_EVENT_MOUSE_MOTION:
  160. {
  161. struct rtgui_event_mouse *mouse = (struct rtgui_event_mouse*)event;
  162. if (mouse->button & RTGUI_MOUSE_BUTTON_LEFT) rt_kprintf("left ");
  163. else rt_kprintf("right ");
  164. if (mouse->button & RTGUI_MOUSE_BUTTON_DOWN) rt_kprintf("down ");
  165. else rt_kprintf("up ");
  166. if (mouse->wid != RT_NULL)
  167. rt_kprintf("win: %s at (%d, %d)", mouse->wid->title,
  168. mouse->x, mouse->y);
  169. else
  170. rt_kprintf("(%d, %d)", mouse->x, mouse->y);
  171. }
  172. break;
  173. case RTGUI_EVENT_MONITOR_ADD:
  174. {
  175. struct rtgui_event_monitor *monitor = (struct rtgui_event_monitor*)event;
  176. if (monitor->wid != RT_NULL)
  177. {
  178. rt_kprintf("win: %s, the rect is:(%d, %d) - (%d, %d)", monitor->wid->title,
  179. monitor->rect.x1, monitor->rect.y1,
  180. monitor->rect.x2, monitor->rect.y2);
  181. }
  182. }
  183. break;
  184. default:
  185. break;
  186. }
  187. rt_kprintf("\n");
  188. }
  189. #else
  190. #define DBG_MSG(x)
  191. #define rtgui_event_dump(tid, event)
  192. #endif
  193. rt_bool_t rtgui_application_event_handler(struct rtgui_object* obj, rtgui_event_t* event);
  194. static void _rtgui_application_constructor(struct rtgui_application *app)
  195. {
  196. /* set event handler */
  197. rtgui_object_set_event_handler(RTGUI_OBJECT(app),
  198. rtgui_application_event_handler);
  199. app->name = RT_NULL;
  200. /* set EXITED so we can destroy an application that just created */
  201. app->state_flag = RTGUI_APPLICATION_FLAG_EXITED;
  202. app->ref_count = 0;
  203. app->exit_code = 0;
  204. app->tid = RT_NULL;
  205. app->server = RT_NULL;
  206. app->mq = RT_NULL;
  207. app->modal_object = RT_NULL;
  208. app->on_idle = RT_NULL;
  209. }
  210. static void _rtgui_application_destructor(struct rtgui_application *app)
  211. {
  212. RT_ASSERT(app != RT_NULL);
  213. rt_free(app->name);
  214. app->name = RT_NULL;
  215. }
  216. DEFINE_CLASS_TYPE(application, "application",
  217. RTGUI_OBJECT_TYPE,
  218. _rtgui_application_constructor,
  219. _rtgui_application_destructor,
  220. sizeof(struct rtgui_application));
  221. struct rtgui_application* rtgui_application_create(
  222. rt_thread_t tid,
  223. const char *myname)
  224. {
  225. struct rtgui_application *app;
  226. RT_ASSERT(tid != RT_NULL);
  227. RT_ASSERT(myname != RT_NULL);
  228. /* create application */
  229. app = RTGUI_APPLICATION(rtgui_object_create(RTGUI_APPLICATION_TYPE));
  230. if (app == RT_NULL)
  231. return RT_NULL;
  232. DBG_MSG(("register a rtgui application(%s) on thread %s\n", myname, tid->name));
  233. app->tid = tid;
  234. /* set user thread */
  235. tid->user_data = (rt_uint32_t)app;
  236. app->mq = rt_mq_create("rtgui", sizeof(union rtgui_event_generic), 32, RT_IPC_FLAG_FIFO);
  237. if (app->mq == RT_NULL)
  238. {
  239. rt_kprintf("mq err\n");
  240. goto __mq_err;
  241. }
  242. /* set application title */
  243. app->name = (unsigned char*)rt_strdup((char*)myname);
  244. if (app->name != RT_NULL)
  245. return app;
  246. __mq_err:
  247. rtgui_object_destroy(RTGUI_OBJECT(app));
  248. tid->user_data = 0;
  249. return RT_NULL;
  250. }
  251. #define _rtgui_application_check(app) \
  252. do { \
  253. RT_ASSERT(app != RT_NULL); \
  254. RT_ASSERT(app->tid != RT_NULL); \
  255. RT_ASSERT(app->tid->user_data != 0); \
  256. RT_ASSERT(app->mq != RT_NULL); \
  257. } while (0)
  258. void rtgui_application_destroy(struct rtgui_application *app)
  259. {
  260. _rtgui_application_check(app);
  261. if (!(app->state_flag & RTGUI_APPLICATION_FLAG_EXITED))
  262. {
  263. rt_kprintf("cannot destroy a running application: %s.\n",
  264. app->name);
  265. return;
  266. }
  267. app->tid->user_data = 0;
  268. rt_mq_delete(app->mq);
  269. rtgui_object_destroy(RTGUI_OBJECT(app));
  270. }
  271. struct rtgui_application* rtgui_application_self(void)
  272. {
  273. struct rtgui_application *app;
  274. rt_thread_t self;
  275. /* get current thread */
  276. self = rt_thread_self();
  277. app = (struct rtgui_application*)(self->user_data);
  278. return app;
  279. }
  280. void rtgui_application_set_onidle(rtgui_idle_func onidle)
  281. {
  282. struct rtgui_application *app;
  283. app = rtgui_application_self();
  284. if (app != RT_NULL)
  285. app->on_idle = onidle;
  286. }
  287. rtgui_idle_func rtgui_application_get_onidle(void)
  288. {
  289. struct rtgui_application *app;
  290. app = rtgui_application_self();
  291. if (app != RT_NULL)
  292. return app->on_idle;
  293. else
  294. return RT_NULL;
  295. }
  296. extern rt_thread_t rt_thread_find(char* name);
  297. rt_thread_t rtgui_application_get_server(void)
  298. {
  299. return rt_thread_find("rtgui");
  300. }
  301. rt_err_t rtgui_application_send(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
  302. {
  303. rt_err_t result;
  304. struct rtgui_application *app;
  305. RT_ASSERT(tid != RT_NULL);
  306. RT_ASSERT(event != RT_NULL);
  307. RT_ASSERT(event_size != 0);
  308. rtgui_event_dump(tid, event);
  309. /* find struct rtgui_application */
  310. app = (struct rtgui_application*) (tid->user_data);
  311. if (app == RT_NULL)
  312. return -RT_ERROR;
  313. result = rt_mq_send(app->mq, event, event_size);
  314. if (result != RT_EOK)
  315. {
  316. if (event->type != RTGUI_EVENT_TIMER)
  317. rt_kprintf("send event to %s failed\n", app->tid->name);
  318. }
  319. return result;
  320. }
  321. rt_err_t rtgui_application_send_urgent(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
  322. {
  323. rt_err_t result;
  324. struct rtgui_application *app;
  325. RT_ASSERT(tid != RT_NULL);
  326. RT_ASSERT(event != RT_NULL);
  327. RT_ASSERT(event_size != 0);
  328. rtgui_event_dump(tid, event);
  329. /* find rtgui_application */
  330. app = (struct rtgui_application*) (tid->user_data);
  331. if (app == RT_NULL)
  332. return -RT_ERROR;
  333. result = rt_mq_urgent(app->mq, event, event_size);
  334. if (result != RT_EOK)
  335. rt_kprintf("send ergent event failed\n");
  336. return result;
  337. }
  338. rt_err_t rtgui_application_send_sync(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
  339. {
  340. rt_err_t r;
  341. struct rtgui_application *app;
  342. rt_int32_t ack_buffer, ack_status;
  343. struct rt_mailbox ack_mb;
  344. RT_ASSERT(tid != RT_NULL);
  345. RT_ASSERT(event != RT_NULL);
  346. RT_ASSERT(event_size != 0);
  347. rtgui_event_dump(tid, event);
  348. /* init ack mailbox */
  349. r = rt_mb_init(&ack_mb, "ack", &ack_buffer, 1, 0);
  350. if (r!= RT_EOK)
  351. goto __return;
  352. app = (struct rtgui_application*) (tid->user_data);
  353. if (app == RT_NULL)
  354. {
  355. r = -RT_ERROR;
  356. goto __return;
  357. }
  358. event->ack = &ack_mb;
  359. r = rt_mq_send(app->mq, event, event_size);
  360. if (r != RT_EOK)
  361. {
  362. rt_kprintf("send sync event failed\n");
  363. goto __return;
  364. }
  365. r = rt_mb_recv(&ack_mb, (rt_uint32_t*)&ack_status, RT_WAITING_FOREVER);
  366. if (r!= RT_EOK)
  367. goto __return;
  368. if (ack_status != RTGUI_STATUS_OK)
  369. r = -RT_ERROR;
  370. else
  371. r = RT_EOK;
  372. __return:
  373. /* fini ack mailbox */
  374. rt_mb_detach(&ack_mb);
  375. return r;
  376. }
  377. rt_err_t rtgui_application_ack(rtgui_event_t* event, rt_int32_t status)
  378. {
  379. RT_ASSERT(event != RT_NULL);
  380. RT_ASSERT(event->ack != RT_NULL);
  381. rt_mb_send(event->ack, status);
  382. return RT_EOK;
  383. }
  384. rt_err_t rtgui_application_recv(rtgui_event_t* event, rt_size_t event_size)
  385. {
  386. struct rtgui_application* app;
  387. rt_err_t r;
  388. RT_ASSERT(event != RT_NULL);
  389. RT_ASSERT(event_size != 0);
  390. app = (struct rtgui_application*) (rt_thread_self()->user_data);
  391. if (app == RT_NULL)
  392. return -RT_ERROR;
  393. r = rt_mq_recv(app->mq, event, event_size, RT_WAITING_FOREVER);
  394. return r;
  395. }
  396. rt_err_t rtgui_application_recv_nosuspend(rtgui_event_t* event, rt_size_t event_size)
  397. {
  398. struct rtgui_application *app;
  399. rt_err_t r;
  400. RT_ASSERT(event != RT_NULL);
  401. RT_ASSERT(event != 0);
  402. app = (struct rtgui_application*) (rt_thread_self()->user_data);
  403. if (app == RT_NULL)
  404. return -RT_ERROR;
  405. r = rt_mq_recv(app->mq, event, event_size, 0);
  406. return r;
  407. }
  408. rt_err_t rtgui_application_recv_filter(rt_uint32_t type, rtgui_event_t* event, rt_size_t event_size)
  409. {
  410. struct rtgui_application *app;
  411. RT_ASSERT(event != RT_NULL);
  412. RT_ASSERT(event_size != 0);
  413. app = (struct rtgui_application*) (rt_thread_self()->user_data);
  414. if (app == RT_NULL)
  415. return -RT_ERROR;
  416. while (rt_mq_recv(app->mq, event, event_size, RT_WAITING_FOREVER) == RT_EOK)
  417. {
  418. if (event->type == type)
  419. {
  420. return RT_EOK;
  421. }
  422. else
  423. {
  424. if (RTGUI_OBJECT(app)->event_handler != RT_NULL)
  425. {
  426. RTGUI_OBJECT(app)->event_handler(RTGUI_OBJECT(app), event);
  427. }
  428. }
  429. }
  430. return -RT_ERROR;
  431. }
  432. rt_inline rt_bool_t _rtgui_application_dest_handle(
  433. struct rtgui_application *app,
  434. struct rtgui_event *event)
  435. {
  436. struct rtgui_event_win* wevent = (struct rtgui_event_win*)event;
  437. struct rtgui_object* dest_object = RTGUI_OBJECT(wevent->wid);
  438. if (dest_object != RT_NULL)
  439. {
  440. if (dest_object->event_handler != RT_NULL)
  441. return dest_object->event_handler(RTGUI_OBJECT(dest_object), event);
  442. else
  443. return RT_FALSE;
  444. }
  445. else
  446. {
  447. rt_kprintf("RTGUI ERROR:server sent a event(%d) without wid\n", event->type);
  448. return RT_FALSE;
  449. }
  450. }
  451. rt_bool_t rtgui_application_event_handler(struct rtgui_object* object, rtgui_event_t* event)
  452. {
  453. struct rtgui_application* app;
  454. RT_ASSERT(object != RT_NULL);
  455. RT_ASSERT(event != RT_NULL);
  456. app = RTGUI_APPLICATION(object);
  457. switch (event->type)
  458. {
  459. case RTGUI_EVENT_PAINT:
  460. case RTGUI_EVENT_CLIP_INFO:
  461. case RTGUI_EVENT_WIN_ACTIVATE:
  462. case RTGUI_EVENT_WIN_DEACTIVATE:
  463. case RTGUI_EVENT_WIN_CLOSE:
  464. case RTGUI_EVENT_WIN_MOVE:
  465. case RTGUI_EVENT_KBD:
  466. _rtgui_application_dest_handle(app, event);
  467. break;
  468. case RTGUI_EVENT_MOUSE_BUTTON:
  469. case RTGUI_EVENT_MOUSE_MOTION:
  470. {
  471. struct rtgui_event_win* wevent = (struct rtgui_event_win*)event;
  472. struct rtgui_object* dest_object = RTGUI_OBJECT(wevent->wid);
  473. // FIXME: let application determine the dest_wiget but not in sever
  474. // so we can combine this handler with above one
  475. if (app->modal_object != RT_NULL &&
  476. dest_object != app->modal_object)
  477. {
  478. // rt_kprintf("discard event %s that is not sent to modal object\n",
  479. // event_string[event->type]);
  480. }
  481. else
  482. {
  483. _rtgui_application_dest_handle(app, event);
  484. }
  485. }
  486. break;
  487. case RTGUI_EVENT_TIMER:
  488. {
  489. struct rtgui_timer* timer;
  490. struct rtgui_event_timer* etimer = (struct rtgui_event_timer*) event;
  491. timer = etimer->timer;
  492. if (timer->timeout != RT_NULL)
  493. {
  494. /* call timeout function */
  495. timer->timeout(timer, timer->user_data);
  496. }
  497. }
  498. break;
  499. case RTGUI_EVENT_COMMAND:
  500. {
  501. struct rtgui_event_command *ecmd = (struct rtgui_event_command *)event;
  502. if (ecmd->wid != RT_NULL)
  503. return _rtgui_application_dest_handle(app, event);
  504. }
  505. default:
  506. return rtgui_object_event_handler(object, event);
  507. }
  508. return RT_TRUE;
  509. }
  510. rt_inline void _rtgui_application_event_loop(struct rtgui_application *app)
  511. {
  512. rt_err_t result;
  513. rt_uint16_t current_ref;
  514. struct rtgui_event *event;
  515. _rtgui_application_check(app);
  516. /* point to event buffer */
  517. event = (struct rtgui_event*)app->event_buffer;
  518. current_ref = ++app->ref_count;
  519. while (current_ref <= app->ref_count)
  520. {
  521. RT_ASSERT(current_ref == app->ref_count);
  522. if (app->on_idle != RT_NULL)
  523. {
  524. result = rtgui_application_recv_nosuspend(event, sizeof(union rtgui_event_generic));
  525. if (result == RT_EOK)
  526. RTGUI_OBJECT(app)->event_handler(RTGUI_OBJECT(app), event);
  527. else if (result == -RT_ETIMEOUT)
  528. app->on_idle(RTGUI_OBJECT(app), RT_NULL);
  529. }
  530. else
  531. {
  532. result = rtgui_application_recv(event, sizeof(union rtgui_event_generic));
  533. if (result == RT_EOK)
  534. RTGUI_OBJECT(app)->event_handler(RTGUI_OBJECT(app), event);
  535. }
  536. }
  537. }
  538. rt_base_t rtgui_application_run(struct rtgui_application *app)
  539. {
  540. _rtgui_application_check(app);
  541. app->state_flag &= ~RTGUI_APPLICATION_FLAG_EXITED;
  542. _rtgui_application_event_loop(app);
  543. if (app->ref_count == 0)
  544. app->state_flag |= RTGUI_APPLICATION_FLAG_EXITED;
  545. return app->exit_code;
  546. }
  547. void rtgui_application_exit(struct rtgui_application* app, rt_uint16_t code)
  548. {
  549. --app->ref_count;
  550. app->exit_code = code;
  551. }