amarullz

aromavm

Jul 2nd, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.91 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    : aromavm.h
  18.  * Description : AROMA Virtual Machine Header
  19.  *
  20.  * + This is part of libaroma, an embedded ui toolkit.
  21.  * + 27/06/15 - Author(s): Ahmad Amarullah
  22.  *
  23.  */
  24. #ifndef __libaroma_vm_com_h__
  25. #define __libaroma_vm_com_h__
  26.  
  27. /*
  28.  *
  29.  * Macros
  30.  *
  31.  */
  32. /* error log */
  33. #define AVMLOGTAG "AROMAVM"
  34. #define AVMLOGE(...) printf(AVMLOGTAG "[E] " __VA_ARGS__); printf("\n");
  35. #define AVMLOGI(...) printf(AVMLOGTAG "[I] " __VA_ARGS__); printf("\n");
  36. #define AVMLOGS(...) printf(AVMLOGTAG "[S] " __VA_ARGS__); printf("\n");
  37. #define AVMLOGV(...) printf(AVMLOGTAG "[S] " __VA_ARGS__); printf("\n");
  38.  
  39.  
  40. /* Request command */
  41. #define AVM_REQ_CMD_FB_SYNC           0x01  /* sync framebuffer */
  42. #define AVM_REQ_CMD_SET_PRIMARY_COLOR 0x02  /* update primary color */
  43. #define AVM_REQ_CMD_SB_SYNC           0x03  /* sync statusbar canvas */
  44. #define AVM_REQ_CMD_READY             0x04  /* app is ready to show */
  45. #define AVM_REQ_CMD_SETNAME           0x05  /* set application name */
  46. #define AVM_REQ_CMD_EXIT              0xcc  /* app is exited */
  47.  
  48. /* Respond status */
  49. #define AVM_RES_ERR                   0x00  /* error status */
  50. #define AVM_RES_OK                    0x01  /* ok status */
  51.  
  52. /* event message */
  53. #define AVM_EV_NEEDSYNC               0x01  /* should sync in next thread */
  54. #define AVM_EV_HID                    0x02  /* hid message */
  55. #define AVM_EV_PAUSE                  0x03  /* pause message */
  56. #define AVM_EV_RESUME                 0x04  /* resume message */
  57. #define AVM_EV_EXIT                   0xcc  /* exit message */
  58.  
  59. /*
  60.  *
  61.  * Structures
  62.  *
  63.  */
  64. /* vm application */
  65. typedef struct{
  66.   int     aid;            /* application id */
  67.   pid_t   pid;            /* application pid */
  68.   int     wfd;            /* write fd - for request */
  69.   int     rfd;            /* read fd - for respond */
  70.   int     efd;            /* event fd */
  71.   LIBAROMA_CANVASP  cfb;  /* app framebuffer canvas */
  72.   LIBAROMA_CANVASP  csb;  /* overflow status bar canvas */
  73.   int     dpi;            /* display dpi */
  74.   char    program[256];   /* program path */
  75. } AVMAPP;
  76.  
  77. /* request message */
  78. typedef struct{
  79.   int     aid;      /* application id */
  80.   byte    cmd;      /* command id */
  81.   dword   param;    /* message param */
  82.   size_t  len;      /* data length */
  83. } AVM_REQUEST;
  84.  
  85. /* respond message */
  86. typedef struct{
  87.   byte    status;   /* respond status */
  88.   dword   param;    /* respond param */
  89.   size_t  len;      /* data length */
  90. } AVM_RESPOND;
  91.  
  92. /* hid event message */
  93. typedef struct{
  94.   byte  type;
  95.   int   key;
  96.   byte  state;
  97.   int   x;
  98.   int   y;
  99.   byte ret;
  100. } AVM_HID_EVENT;
  101.  
  102. /*
  103.  *
  104.  * Functions
  105.  *
  106.  */
  107.  
  108. /* get application instance */
  109. AVMAPP * avm_app();
  110.  
  111. /* api request */
  112. byte avm_request(
  113.   byte cmd,
  114.   dword param,
  115.   voidp data,
  116.   size_t data_len,
  117.   dwordp res_param,
  118.   voidp * res_data,
  119.   size_t * res_data_len);
  120.  
  121. /* post hid event to app hid driver */
  122. void avm_hid_post(
  123.   byte  type,
  124.   int   key,
  125.   byte  state,
  126.   int   x,
  127.   int   y,
  128.   byte ret);
  129.  
  130.  
  131. #endif /* __libaroma_vm_com_h__ */
  132.  
  133.  
  134. /************************************** SOURCE ****************************************************/
  135.  
  136.  
  137. /********************************************************************[libaroma]*
  138.  * Copyright (C) 2011-2015 Ahmad Amarullah (http://amarullz.com/)
  139.  *
  140.  * Licensed under the Apache License, Version 2.0 (the "License");
  141.  * you may not use this file except in compliance with the License.
  142.  * You may obtain a copy of the License at
  143.  *
  144.  *      http://www.apache.org/licenses/LICENSE-2.0
  145.  *
  146.  * Unless required by applicable law or agreed to in writing, software
  147.  * distributed under the License is distributed on an "AS IS" BASIS,
  148.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  149.  * See the License for the specific language governing permissions and
  150.  * limitations under the License.
  151.  *______________________________________________________________________________
  152.  *
  153.  * Filename    : recovery.c
  154.  * Description : AROMA Virtual Machine Source
  155.  *
  156.  * + This is part of libaroma, an embedded ui toolkit.
  157.  * + 27/06/15 - Author(s): Ahmad Amarullah
  158.  *
  159.  */
  160. #ifndef __libaroma_vm_com_c__
  161. #define __libaroma_vm_com_c__
  162. #include "aromavm.h"
  163.  
  164. /*
  165.  *
  166.  * Application Functions
  167.  *
  168.  */
  169.  
  170. /* current application instance */
  171. static AVMAPP * _avm_app = NULL;
  172.  
  173. /* get application instance */
  174. AVMAPP * avm_app(){
  175.   return _avm_app;
  176. }
  177.  
  178. /*
  179.  *
  180.  * Communication Handler
  181.  *
  182.  */
  183.  
  184. /* api request */
  185. byte avm_request(
  186.   byte cmd,
  187.   dword param,
  188.   voidp data,
  189.   size_t data_len,
  190.   dwordp res_param,
  191.   voidp * res_data,
  192.   size_t * res_data_len
  193. ){
  194.   if (!app){
  195.     return AVM_RES_ERR;
  196.   }
  197.   /* compose request */
  198.   AVM_REQUEST req;
  199.   req.aid = avm_app()->aid;
  200.   req.cmd = cmd;
  201.   req.param = param;
  202.   req.data_len = data?data_len:0;
  203.   /* send request */
  204.   write(avm_app()->wfd,&req,sizeof(AVM_REQUEST));
  205.   if (req.data_len>0){
  206.     write(avm_app()->wfd,data,req.data_len);
  207.   }
  208.   /* read for respond */
  209.   AVM_RESPOND res={0};
  210.   read(avm_app()->rfd,&res,sizeof(AVM_RESPOND));
  211.   if (res.len>0){
  212.     /* read for data */
  213.     voidp rd = calloc(res.len,1);
  214.     read(avm_app()->rfd,rd,res.len);
  215.     if (res_data != NULL){
  216.       *res_data = rd;
  217.     }
  218.     else{
  219.       free(rd);
  220.     }
  221.   }
  222.   /* pass the len & param */
  223.   if (res_data_len != NULL){
  224.     *res_data_len = res.len;
  225.   }
  226.   if (res_param != NULL){
  227.     *res_param = res.param;
  228.   }
  229.   /* return respond status */
  230.   return res.status;
  231. }
  232.  
  233. /*
  234.  *
  235.  * Framebuffer Driver
  236.  *
  237.  */
  238. byte avm_fb_release(LIBAROMA_FBP me){ return 1; }
  239. byte avm_fb_start_post(LIBAROMA_FBP me){ return 1; }
  240. byte avm_fb_post(
  241.   LIBAROMA_FBP me, wordp __restrict src,
  242.   int dx, int dy, int dw, int dh,
  243.   int sx, int sy, int sw, int sh
  244.   ){
  245.   return 1;
  246. }
  247. byte avm_fb_end_post(LIBAROMA_FBP me){
  248.   return avm_request(AVM_REQ_CMD_FB_SYNC,0,NULL,0,NULL,NULL,NULL);
  249. }
  250. byte avm_fb_init(LIBAROMA_FBP me){
  251.   me->internal        = NULL;
  252.   me->release         = &avm_fb_release;
  253.   me->w               = avm_app()->cfb->w;  /* width */
  254.   me->h               = avm_app()->cfb->h;  /* height */
  255.   me->sz              = me->w*me->h;
  256.   me->start_post      = &avm_fb_start_post;
  257.   me->end_post        = &avm_fb_end_post;
  258.   me->post            = &avm_fb_post;
  259.   me->snapshoot       = NULL;
  260.   me->config          = NULL;
  261.   me->dpi             = avm_app()->dpi;
  262.   me->double_buffer   = 1;
  263.   me->internal_canvas = 1;
  264.   me->canvas          = avm_app()->cfb;
  265.   return 1;
  266. }
  267.  
  268. /*
  269.  *
  270.  * HID Driver
  271.  *
  272.  */
  273. static int _avm_hid_rfd   =0;
  274. static int _avm_hid_wfd   =0;
  275. static int _avm_hid_active=0;
  276. void avm_hid_post(
  277.   byte  type,
  278.   int   key,
  279.   byte  state,
  280.   int   x,
  281.   int   y,
  282.   byte ret){
  283.   if (_avm_hid_active){
  284.     AVM_HID_EVENT ev={0};
  285.     ev.type=type;
  286.     ev.key=key;
  287.     ev.state=state;
  288.     ev.x=x;
  289.     ev.y=y;
  290.     ev.ret=ret;
  291.     write(_avm_hid_wfd,&ev,sizeof(AVM_HID_EVENT));
  292.   }
  293. }
  294. void avm_hid_release(LIBAROMA_HIDP me){
  295.   if (_avm_hid_active){
  296.     _avm_hid_active=0;
  297.     avm_hid_post(0,0,0,0,0,LIBAROMA_HID_EV_RET_EXIT);
  298.     write(_avm_hid_wfd,&exit_event,sizeof(AVM_HID_EVENT));
  299.     close(_avm_hid_wfd);
  300.   }
  301. }
  302. byte avm_hid_getinput(LIBAROMA_HIDP me, LIBAROMA_HID_EVENTP dest_ev){
  303.   AVM_HID_EVENT ev;
  304.   while(_avm_hid_active){
  305.     if (read(_avm_hid_rfd,&ev,sizeof(AVM_HID_EVENT))==sizeof(AVM_HID_EVENT)){
  306.       if (ev.ret==LIBAROMA_HID_EV_RET_EXIT){
  307.         break;
  308.       }
  309.       dest_ev->type=ev.type;
  310.       dest_ev->key=ev.key;
  311.       dest_ev->state=ev.state;
  312.       dest_ev->x=ev.x;
  313.       dest_ev->y=ev.y;
  314.       return ev.ret;
  315.     }
  316.   }
  317.   close(_avm_hid_rfd);
  318.   return LIBAROMA_HID_EV_RET_EXIT;
  319. }
  320. byte avm_hid_init(LIBAROMA_HIDP me){
  321.   if (_avm_hid_active){
  322.     return 0;
  323.   }
  324.   int pipes[2];
  325.   pipe(pipes);
  326.   me->internal    = NULL;
  327.   me->release     = &avm_hid_release;
  328.   me->getinput    = &avm_hid_getinput;
  329.   _avm_hid_rfd    = pipes[0];
  330.   _avm_hid_wfd    = pipes[1];
  331.   _avm_hid_active = 1;
  332.   return 1;
  333. }
  334.  
  335. /*
  336.  *
  337.  * New Application
  338.  *
  339.  */
  340. pid_t avm_new_app(
  341.     int appid,
  342.     int wfd,
  343.     int rfd,
  344.     int efd,
  345.     int dpi){
  346.   pid_t apid = fork();
  347.   if (apid==0){
  348.     _avm_app = (AVMAPP *) calloc(sizeof(AVMAPP),1);
  349.     _avm_app->aid = appid;
  350.     _avm_app->pid = getpid();
  351.     _avm_app->wfd = wfd;
  352.     _avm_app->rfd = rfd;
  353.     _avm_app->efd = efd;
  354.     _avm_app->dpi = dpi;
  355.    
  356.     char canvas_name[256];
  357.     snprintf(canvas_name,256,"@appfb-%i",appid);
  358.     _avm_app->cfb = libaroma_canvas_shmem_open(canvas_name);
  359.     snprintf(canvas_name,256,"@appsb-%i",appid);
  360.     _avm_app->csb = libaroma_canvas_shmem_open(canvas_name);
  361.    
  362.     if (_avm_app->cfb&&_avm_app->csb){
  363.       AVMLOGI("Init new application (id:%i,pid:%i)",
  364.         _avm_app->aid, _avm_app->pid
  365.       );
  366.     }
  367.     else{
  368.       AVMLOGE("New application error - Cannot open shared canvas");
  369.     }
  370.    
  371.     if (_avm_app->cfb){
  372.       libaroma_canvas_free(_avm_app->cfb);
  373.     }
  374.     if (_avm_app->csb){
  375.       libaroma_canvas_free(_avm_app->csb);
  376.     }
  377.    
  378.     /* send exit message */
  379.     avm_request(AVM_REQ_CMD_EXIT,0,NULL,0,NULL,NULL,NULL);
  380.     exit(0);
  381.   }
  382.   else if (apid>0){
  383.   }
  384.   return apid;
  385. }
  386.  
  387. #endif /* __libaroma_vm_com_c__ */
Advertisement
Add Comment
Please, Sign In to add comment