amarullz

aromart

Jul 3rd, 2015
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.74 KB | None | 0 0
  1. /********************************************************************[libaroma]*
  2.  * Copyright (C) 2011-2015 Ahmad Amarullah (http://amarullz.com/)
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *______________________________________________________________________________
  16.  *
  17.  * Filename    : aromart_core.c
  18.  * Description : AROMA Runtime core source
  19.  *
  20.  * + This is part of libaroma, an embedded ui toolkit.
  21.  * + 27/06/15 - Author(s): Ahmad Amarullah
  22.  *
  23.  */
  24. #ifndef __libaromart_core_c__
  25. #define __libaromart_core_c__
  26. #include "aromart_internal.h"
  27.  
  28. static LART _lart={0};
  29.  
  30. /* get aroma runtime instance */
  31. LART * lart(){
  32.   return &_lart;
  33. }
  34.  
  35. /* init runtime */
  36. byte lart_init_runtime(){
  37.   int host_pipes[2];
  38.   int remote_pipes[2];
  39.   pipe(host_pipes);
  40.   pipe(remote_pipes);
  41.  
  42.   lart()->mpid  = getpid();
  43.   pid_t pid     = fork();
  44.   if (pid==0){
  45.     /* system ui */
  46.     lart()->spid = getpid();
  47.     lart()->rfd  = remote_pipes[0];
  48.     lart()->wfd  = host_pipes[1];
  49.     close(remote_pipes[1]);
  50.     close(host_pipes[0]);
  51.     return 2; /* sysui process */
  52.   }
  53.   else if (pid){
  54.     lart()->spid = pid;
  55.     lart()->rfd  = host_pipes[0];
  56.     lart()->wfd  = remote_pipes[1];
  57.     close(host_pipes[1]);
  58.     close(remote_pipes[0]);
  59.     return 1; /* manager process */
  60.   }
  61.   return 0; /* failed */
  62. }
  63.  
  64. /* start runtime */
  65. int lart_start(
  66.     LART_APP_RUN_HANDLER run_handler,
  67.     LART_SYSTEM_UI_HANDLER sysui_handler
  68.   ){
  69.   byte process_type = lart_init_runtime();
  70.   if (process_type==2){
  71.     /* sys ui */
  72.     int ret=lart_sysui(sysui_handler);
  73.     exit(ret);
  74.     return 0;
  75.   }
  76.   else if (process_type==1){
  77.     /* app manager */
  78.     return lart_app_manager(run_handler);
  79.   }
  80.   return 0;
  81. }
  82.  
  83.  
  84. #endif /* __libaromart_core_c__ */
  85.  
  86.  
  87.  
  88. /********************************************************************[libaroma]*
  89.  * Copyright (C) 2011-2015 Ahmad Amarullah (http://amarullz.com/)
  90.  *
  91.  * Licensed under the Apache License, Version 2.0 (the "License");
  92.  * you may not use this file except in compliance with the License.
  93.  * You may obtain a copy of the License at
  94.  *
  95.  *      http://www.apache.org/licenses/LICENSE-2.0
  96.  *
  97.  * Unless required by applicable law or agreed to in writing, software
  98.  * distributed under the License is distributed on an "AS IS" BASIS,
  99.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  100.  * See the License for the specific language governing permissions and
  101.  * limitations under the License.
  102.  *______________________________________________________________________________
  103.  *
  104.  * Filename    : aromart_app.c
  105.  * Description : AROMA Runtime application handler
  106.  *
  107.  * + This is part of libaroma, an embedded ui toolkit.
  108.  * + 27/06/15 - Author(s): Ahmad Amarullah
  109.  *
  110.  */
  111. #ifndef __libaromart_app_c__
  112. #define __libaromart_app_c__
  113. #include "aromart_internal.h"
  114.  
  115. /* application instance */
  116. static LARTAPP * _lart_app = NULL;
  117.  
  118. /* run callback */
  119. static LART_APP_RUN_HANDLER _lart_app_run_cb=NULL;
  120.  
  121. /* get application instance */
  122. LARTAPP * lart_app(){
  123.   return _lart_app;
  124. }
  125.  
  126. /* run application */
  127. int lart_app_run(char * program, char * param){
  128.   if (_lart_app_run_cb){
  129.     return _lart_app_run_cb(program, param);
  130.   }
  131.   LARTLOGE("No Application Callback...");
  132.   return -1;
  133. }
  134.  
  135. /* application command */
  136. byte lart_app_command(
  137.   byte cmd,
  138.   dword param,
  139.   voidp data,
  140.   size_t data_len,
  141.   dwordp    res_param,
  142.   voidp  *  res_data,
  143.   size_t *  res_data_len
  144. ){
  145.   if (!lart_app()){
  146.     return LART_RES_ERR;
  147.   }
  148.   return lart_command(
  149.     lart_app()->wfd,
  150.     lart_app()->rfd,
  151.     cmd,
  152.     param,
  153.     data,
  154.     data_len,
  155.     res_param,
  156.     res_data,
  157.     res_data_len
  158.   );
  159. }
  160.  
  161. /* create new application */
  162. pid_t lart_app_create(
  163.   int appid,
  164.   char * program,
  165.   char * param,
  166.   int wfd,
  167.   int rfd,
  168.   int efd,
  169.   int dpi
  170. ){
  171.   pid_t apid = fork();
  172.   if (apid==0){
  173.     int app_ret = -1;
  174.     _lart_app = (LARTAPP *) calloc(sizeof(LARTAPP),1);
  175.     _lart_app->aid = appid;
  176.     _lart_app->pid = getpid();
  177.     _lart_app->wfd = wfd;
  178.     _lart_app->rfd = rfd;
  179.     _lart_app->efd = efd;
  180.     _lart_app->dpi = dpi;
  181.    
  182.     {
  183.       char canvas_name[256];
  184.       snprintf(canvas_name,256,"@appfb-%i",appid);
  185.       _lart_app->cfb = libaroma_canvas_shmem_open(canvas_name);
  186.       snprintf(canvas_name,256,"@appsb-%i",appid);
  187.       _lart_app->csb = libaroma_canvas_shmem_open(canvas_name);
  188.     }
  189.    
  190.     if (_lart_app->cfb&&_lart_app->csb){
  191.       LARTLOGI("INIT NEW APPLICATION (id:%i,pid:%i,path:%s)",
  192.         _lart_app->aid, _lart_app->pid, program?program:""
  193.       );
  194.       if (lart_libaroma_start()){ /* now init app libaroma */
  195.         /* run application */
  196.         app_ret = lart_app_run(program, param);
  197.       }
  198.       lart_libaroma_end();
  199.     }
  200.     else{
  201.       LARTLOGE("NEW APPLICATION ERROR - Cannot open shared canvas "
  202.         "(id:%i,pid:%i,path:%s)",
  203.         _lart_app->aid, _lart_app->pid, program?program:""
  204.       );
  205.     }
  206.    
  207.     /* close canvases */
  208.     if (_lart_app->cfb){
  209.       libaroma_canvas_free(_lart_app->cfb);
  210.     }
  211.     if (_lart_app->csb){
  212.       libaroma_canvas_free(_lart_app->csb);
  213.     }
  214.    
  215.     /* send exit message */
  216.     lart_app_command(LART_REQ_CMD_EXIT,0,NULL,0,NULL,NULL,NULL);
  217.    
  218.     /* exit log */
  219.     LARTLOGI("APPLICATION EXITED (id:%i,pid:%i,path:%s)",
  220.       _lart_app->aid, _lart_app->pid, _lart_app->program
  221.     );
  222.    
  223.     /* free application instance */
  224.     free(_lart_app);
  225.     _lart_app=NULL;
  226.     exit(app_ret);
  227.   }
  228.   return apid;
  229. }
  230.  
  231. /* start app manager */
  232. int lart_app_manager(
  233.   LART_APP_RUN_HANDLER run_handler
  234. ){
  235.   byte running = 1;
  236.   int res=0;
  237.   _lart_app_run_cb = run_handler;
  238.  
  239.   while (running){
  240.     dword param = 0;
  241.     voidp data  = NULL;
  242.     size_t len  = 0;
  243.     byte status = lart_rrecv(&param, data, &len);
  244.    
  245.     switch (status){
  246.       case LART_ROOT_MSG_CREATE_APP:
  247.         {
  248.           LART_NEW_APP_RES_DATA res={0};
  249.           res.aid = 0;
  250.           res.pid = -1;
  251.           if ((len==sizeof(LART_NEW_APP_DATA))&&(data)){
  252.             LART_NEW_APP_DATA * reqapp = (LART_NEW_APP_DATA *) data;
  253.             res.aid = reqapp->appid;
  254.             res.pid = lart_app_create(
  255.               reqapp->appid,
  256.               reqapp->program,
  257.               reqapp->param,
  258.               reqapp->wfd,
  259.               reqapp->rfd,
  260.               reqapp->efd,
  261.               reqapp->dpi
  262.             );
  263.             lart_rsend(
  264.               LART_ROOT_MSG_CREATE_APP_RES,
  265.               1 /* processed */,  &res, sizeof(LART_NEW_APP_RES_DATA)
  266.             );
  267.           }
  268.           else{
  269.             lart_rsend(
  270.               LART_ROOT_MSG_CREATE_APP_RES,
  271.               0, NULL, 0 /* failed - bad message */
  272.             );
  273.           }
  274.         }
  275.         break;
  276.       case LART_ROOT_MSG_TERMINATED:
  277.         {
  278.           /* terminate runtime */
  279.           running = 0;
  280.           res = (int) param;
  281.         }
  282.         break;
  283.     }
  284.    
  285.     if (len>0){
  286.       free(data);
  287.       data=NULL;
  288.       len=0;
  289.     }
  290.   }
  291.   return res;
  292. }
  293.  
  294. #endif /* __libaromart_app_c__ */
  295.  
  296.  
  297. /********************************************************************[libaroma]*
  298.  * Copyright (C) 2011-2015 Ahmad Amarullah (http://amarullz.com/)
  299.  *
  300.  * Licensed under the Apache License, Version 2.0 (the "License");
  301.  * you may not use this file except in compliance with the License.
  302.  * You may obtain a copy of the License at
  303.  *
  304.  *      http://www.apache.org/licenses/LICENSE-2.0
  305.  *
  306.  * Unless required by applicable law or agreed to in writing, software
  307.  * distributed under the License is distributed on an "AS IS" BASIS,
  308.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  309.  * See the License for the specific language governing permissions and
  310.  * limitations under the License.
  311.  *______________________________________________________________________________
  312.  *
  313.  * Filename    : aromart_sysui.c
  314.  * Description : AROMA Runtime system ui
  315.  *
  316.  * + This is part of libaroma, an embedded ui toolkit.
  317.  * + 27/06/15 - Author(s): Ahmad Amarullah
  318.  *
  319.  */
  320. #ifndef __libaromart_sysui_c__
  321. #define __libaromart_sysui_c__
  322. #include "aromart_internal.h"
  323.  
  324. /* start sysui */
  325. int lart_sysui(LART_SYSTEM_UI_HANDLER sysui_handler){
  326.   int res=0;
  327.   if (libaroma_start()){
  328.     ... sys ui stuff ...
  329.   }
  330.   libaroma_end();
  331.   return res;
  332. }
  333.  
  334. #endif /* __libaromart_sysui_c__ */
  335.  
  336.  
  337.  
  338.  
  339. /********************************************************************[libaroma]*
  340.  * Copyright (C) 2011-2015 Ahmad Amarullah (http://amarullz.com/)
  341.  *
  342.  * Licensed under the Apache License, Version 2.0 (the "License");
  343.  * you may not use this file except in compliance with the License.
  344.  * You may obtain a copy of the License at
  345.  *
  346.  *      http://www.apache.org/licenses/LICENSE-2.0
  347.  *
  348.  * Unless required by applicable law or agreed to in writing, software
  349.  * distributed under the License is distributed on an "AS IS" BASIS,
  350.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  351.  * See the License for the specific language governing permissions and
  352.  * limitations under the License.
  353.  *______________________________________________________________________________
  354.  *
  355.  * Filename    : aromart_driver.c
  356.  * Description : AROMA Runtime client driver
  357.  *
  358.  * + This is part of libaroma, an embedded ui toolkit.
  359.  * + 27/06/15 - Author(s): Ahmad Amarullah
  360.  *
  361.  */
  362. #ifndef __libaromart_driver_c__
  363. #define __libaromart_driver_c__
  364. #include "aromart_internal.h"
  365.  
  366. /* hid event message */
  367. typedef struct{
  368.   byte  type;
  369.   int   key;
  370.   byte  state;
  371.   int   x;
  372.   int   y;
  373.   byte ret;
  374. } LART_HID_EVENT;
  375.  
  376. /* static driver variable */
  377. static int _lart_hid_rfd   =0;
  378. static int _lart_hid_wfd   =0;
  379. static int _lart_hid_active=0;
  380.  
  381. /* Framebuffer Driver */
  382. byte lart_fb_release(LIBAROMA_FBP me){ return 1; }
  383. byte lart_fb_start_post(LIBAROMA_FBP me){ return 1; }
  384. byte lart_fb_post(
  385.   LIBAROMA_FBP me, wordp __restrict src,
  386.   int dx, int dy, int dw, int dh,
  387.   int sx, int sy, int sw, int sh
  388.   ){
  389.   return 1;
  390. }
  391. byte lart_fb_end_post(LIBAROMA_FBP me){
  392.   return lart_app_command(LART_REQ_CMD_FB_SYNC,0,NULL,0,NULL,NULL,NULL);
  393. }
  394. byte lart_fb_init(LIBAROMA_FBP me){
  395.   me->internal        = NULL;
  396.   me->release         = &lart_fb_release;
  397.   me->w               = lart_app()->cfb->w;
  398.   me->h               = lart_app()->cfb->h;
  399.   me->sz              = me->w*me->h;
  400.   me->start_post      = &lart_fb_start_post;
  401.   me->end_post        = &lart_fb_end_post;
  402.   me->post            = &lart_fb_post;
  403.   me->snapshoot       = NULL;
  404.   me->config          = NULL;
  405.   me->dpi             = lart_app()->dpi;
  406.   me->double_buffer   = 1;
  407.   me->internal_canvas = 1;
  408.   me->canvas          = lart_app()->cfb;
  409.   return 1;
  410. }
  411.  
  412. /* HID Driver */
  413. void lart_hid_post(
  414.   byte  type,
  415.   int   key,
  416.   byte  state,
  417.   int   x,
  418.   int   y,
  419.   byte ret){
  420.   if (_lart_hid_active){
  421.     LART_HID_EVENT ev={0};
  422.     ev.type=type;
  423.     ev.key=key;
  424.     ev.state=state;
  425.     ev.x=x;
  426.     ev.y=y;
  427.     ev.ret=ret;
  428.     write(_lart_hid_wfd,&ev,sizeof(LART_HID_EVENT));
  429.   }
  430. }
  431. void lart_hid_release(LIBAROMA_HIDP me){
  432.   if (_lart_hid_active){
  433.     _lart_hid_active=0;
  434.     lart_hid_post(0,0,0,0,0,LIBAROMA_HID_EV_RET_EXIT);
  435.     close(_lart_hid_wfd);
  436.   }
  437. }
  438. byte lart_hid_getinput(LIBAROMA_HIDP me, LIBAROMA_HID_EVENTP dest_ev){
  439.   LART_HID_EVENT ev;
  440.   while(_lart_hid_active){
  441.     if (read(_lart_hid_rfd,&ev,sizeof(LART_HID_EVENT))==sizeof(LART_HID_EVENT)){
  442.       if (ev.ret==LIBAROMA_HID_EV_RET_EXIT){
  443.         break;
  444.       }
  445.       dest_ev->type=ev.type;
  446.       dest_ev->key=ev.key;
  447.       dest_ev->state=ev.state;
  448.       dest_ev->x=ev.x;
  449.       dest_ev->y=ev.y;
  450.       return ev.ret;
  451.     }
  452.   }
  453.   close(_lart_hid_rfd);
  454.   return LIBAROMA_HID_EV_RET_EXIT;
  455. }
  456. byte lart_hid_init(LIBAROMA_HIDP me){
  457.   if (_lart_hid_active){
  458.     return 0;
  459.   }
  460.   int pipes[2];
  461.   pipe(pipes);
  462.   me->internal    = NULL;
  463.   me->release     = &lart_hid_release;
  464.   me->getinput    = &lart_hid_getinput;
  465.   _lart_hid_rfd    = pipes[0];
  466.   _lart_hid_wfd    = pipes[1];
  467.   _lart_hid_active = 1;
  468.   return 1;
  469. }
  470.  
  471. /* libaroma app scope start & end function */
  472. byte lart_libaroma_start(){
  473.   /* set custom initializer */
  474.   libaroma_fb_set_initializer(lart_fb_init);
  475.   libaroma_hid_set_initializer(lart_hid_init);
  476.  
  477.   if (!libaroma_fb_init()) {
  478.     LARTLOGE("lart_libaroma_start cannot start framebuffer...");
  479.     return 0;
  480.   }
  481.   if (!libaroma_font_init()) {
  482.     LARTLOGE("lart_libaroma_start cannot start font engine...");
  483.     return 0;
  484.   }
  485.   if (!libaroma_hid_init()) {
  486.     LARTLOGE("lart_libaroma_start cannot start hid engine...");
  487.     return 0;
  488.   }
  489.   if (!libaroma_msg_init()) {
  490.     LARTLOGE("lart_libaroma_start cannot start message queue...");
  491.     return 0;
  492.   }
  493.   if (!libaroma_lang_init()) {
  494.     LARTLOGE("lart_libaroma_start cannot start language engine...");
  495.     return 0;
  496.   }
  497.   if (!libaroma_timer_init()) {
  498.     LARTLOGE("lart_libaroma_start cannot start timer engine...");
  499.     return 0;
  500.   }
  501.   if (!libaroma_wm_init()){
  502.     LARTLOGE("lart_libaroma_start cannot start window manager...");
  503.     return 0;
  504.   }
  505.   return 1;
  506. }
  507. void lart_libaroma_end(){
  508.   libaroma_wm_release();
  509.   libaroma_timer_release();
  510.   libaroma_lang_release();
  511.   libaroma_msg_release();
  512.   libaroma_hid_release();
  513.   libaroma_font_release();
  514.   libaroma_fb_release();
  515. }
  516.  
  517. #endif /* __libaromart_driver_c__ */
Advertisement
Add Comment
Please, Sign In to add comment