Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************************************************[libaroma]*
- * Copyright (C) 2011-2015 Ahmad Amarullah (http://amarullz.com/)
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *______________________________________________________________________________
- *
- * Filename : aromavm.h
- * Description : AROMA Virtual Machine Header
- *
- * + This is part of libaroma, an embedded ui toolkit.
- * + 27/06/15 - Author(s): Ahmad Amarullah
- *
- */
- #ifndef __libaroma_vm_com_h__
- #define __libaroma_vm_com_h__
- /*
- *
- * Macros
- *
- */
- /* error log */
- #define AVMLOGTAG "AROMAVM"
- #define AVMLOGE(...) printf(AVMLOGTAG "[E] " __VA_ARGS__); printf("\n");
- #define AVMLOGI(...) printf(AVMLOGTAG "[I] " __VA_ARGS__); printf("\n");
- #define AVMLOGS(...) printf(AVMLOGTAG "[S] " __VA_ARGS__); printf("\n");
- #define AVMLOGV(...) printf(AVMLOGTAG "[S] " __VA_ARGS__); printf("\n");
- /* Request command */
- #define AVM_REQ_CMD_FB_SYNC 0x01 /* sync framebuffer */
- #define AVM_REQ_CMD_SET_PRIMARY_COLOR 0x02 /* update primary color */
- #define AVM_REQ_CMD_SB_SYNC 0x03 /* sync statusbar canvas */
- #define AVM_REQ_CMD_READY 0x04 /* app is ready to show */
- #define AVM_REQ_CMD_SETNAME 0x05 /* set application name */
- #define AVM_REQ_CMD_EXIT 0xcc /* app is exited */
- /* Respond status */
- #define AVM_RES_ERR 0x00 /* error status */
- #define AVM_RES_OK 0x01 /* ok status */
- /* event message */
- #define AVM_EV_NEEDSYNC 0x01 /* should sync in next thread */
- #define AVM_EV_HID 0x02 /* hid message */
- #define AVM_EV_PAUSE 0x03 /* pause message */
- #define AVM_EV_RESUME 0x04 /* resume message */
- #define AVM_EV_EXIT 0xcc /* exit message */
- /*
- *
- * Structures
- *
- */
- /* vm application */
- typedef struct{
- int aid; /* application id */
- pid_t pid; /* application pid */
- int wfd; /* write fd - for request */
- int rfd; /* read fd - for respond */
- int efd; /* event fd */
- LIBAROMA_CANVASP cfb; /* app framebuffer canvas */
- LIBAROMA_CANVASP csb; /* overflow status bar canvas */
- int dpi; /* display dpi */
- char program[256]; /* program path */
- } AVMAPP;
- /* request message */
- typedef struct{
- int aid; /* application id */
- byte cmd; /* command id */
- dword param; /* message param */
- size_t len; /* data length */
- } AVM_REQUEST;
- /* respond message */
- typedef struct{
- byte status; /* respond status */
- dword param; /* respond param */
- size_t len; /* data length */
- } AVM_RESPOND;
- /* hid event message */
- typedef struct{
- byte type;
- int key;
- byte state;
- int x;
- int y;
- byte ret;
- } AVM_HID_EVENT;
- /*
- *
- * Functions
- *
- */
- /* get application instance */
- AVMAPP * avm_app();
- /* api request */
- byte avm_request(
- byte cmd,
- dword param,
- voidp data,
- size_t data_len,
- dwordp res_param,
- voidp * res_data,
- size_t * res_data_len);
- /* post hid event to app hid driver */
- void avm_hid_post(
- byte type,
- int key,
- byte state,
- int x,
- int y,
- byte ret);
- #endif /* __libaroma_vm_com_h__ */
- /************************************** SOURCE ****************************************************/
- /********************************************************************[libaroma]*
- * Copyright (C) 2011-2015 Ahmad Amarullah (http://amarullz.com/)
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *______________________________________________________________________________
- *
- * Filename : recovery.c
- * Description : AROMA Virtual Machine Source
- *
- * + This is part of libaroma, an embedded ui toolkit.
- * + 27/06/15 - Author(s): Ahmad Amarullah
- *
- */
- #ifndef __libaroma_vm_com_c__
- #define __libaroma_vm_com_c__
- #include "aromavm.h"
- /*
- *
- * Application Functions
- *
- */
- /* current application instance */
- static AVMAPP * _avm_app = NULL;
- /* get application instance */
- AVMAPP * avm_app(){
- return _avm_app;
- }
- /*
- *
- * Communication Handler
- *
- */
- /* api request */
- byte avm_request(
- byte cmd,
- dword param,
- voidp data,
- size_t data_len,
- dwordp res_param,
- voidp * res_data,
- size_t * res_data_len
- ){
- if (!app){
- return AVM_RES_ERR;
- }
- /* compose request */
- AVM_REQUEST req;
- req.aid = avm_app()->aid;
- req.cmd = cmd;
- req.param = param;
- req.data_len = data?data_len:0;
- /* send request */
- write(avm_app()->wfd,&req,sizeof(AVM_REQUEST));
- if (req.data_len>0){
- write(avm_app()->wfd,data,req.data_len);
- }
- /* read for respond */
- AVM_RESPOND res={0};
- read(avm_app()->rfd,&res,sizeof(AVM_RESPOND));
- if (res.len>0){
- /* read for data */
- voidp rd = calloc(res.len,1);
- read(avm_app()->rfd,rd,res.len);
- if (res_data != NULL){
- *res_data = rd;
- }
- else{
- free(rd);
- }
- }
- /* pass the len & param */
- if (res_data_len != NULL){
- *res_data_len = res.len;
- }
- if (res_param != NULL){
- *res_param = res.param;
- }
- /* return respond status */
- return res.status;
- }
- /*
- *
- * Framebuffer Driver
- *
- */
- byte avm_fb_release(LIBAROMA_FBP me){ return 1; }
- byte avm_fb_start_post(LIBAROMA_FBP me){ return 1; }
- byte avm_fb_post(
- LIBAROMA_FBP me, wordp __restrict src,
- int dx, int dy, int dw, int dh,
- int sx, int sy, int sw, int sh
- ){
- return 1;
- }
- byte avm_fb_end_post(LIBAROMA_FBP me){
- return avm_request(AVM_REQ_CMD_FB_SYNC,0,NULL,0,NULL,NULL,NULL);
- }
- byte avm_fb_init(LIBAROMA_FBP me){
- me->internal = NULL;
- me->release = &avm_fb_release;
- me->w = avm_app()->cfb->w; /* width */
- me->h = avm_app()->cfb->h; /* height */
- me->sz = me->w*me->h;
- me->start_post = &avm_fb_start_post;
- me->end_post = &avm_fb_end_post;
- me->post = &avm_fb_post;
- me->snapshoot = NULL;
- me->config = NULL;
- me->dpi = avm_app()->dpi;
- me->double_buffer = 1;
- me->internal_canvas = 1;
- me->canvas = avm_app()->cfb;
- return 1;
- }
- /*
- *
- * HID Driver
- *
- */
- static int _avm_hid_rfd =0;
- static int _avm_hid_wfd =0;
- static int _avm_hid_active=0;
- void avm_hid_post(
- byte type,
- int key,
- byte state,
- int x,
- int y,
- byte ret){
- if (_avm_hid_active){
- AVM_HID_EVENT ev={0};
- ev.type=type;
- ev.key=key;
- ev.state=state;
- ev.x=x;
- ev.y=y;
- ev.ret=ret;
- write(_avm_hid_wfd,&ev,sizeof(AVM_HID_EVENT));
- }
- }
- void avm_hid_release(LIBAROMA_HIDP me){
- if (_avm_hid_active){
- _avm_hid_active=0;
- avm_hid_post(0,0,0,0,0,LIBAROMA_HID_EV_RET_EXIT);
- write(_avm_hid_wfd,&exit_event,sizeof(AVM_HID_EVENT));
- close(_avm_hid_wfd);
- }
- }
- byte avm_hid_getinput(LIBAROMA_HIDP me, LIBAROMA_HID_EVENTP dest_ev){
- AVM_HID_EVENT ev;
- while(_avm_hid_active){
- if (read(_avm_hid_rfd,&ev,sizeof(AVM_HID_EVENT))==sizeof(AVM_HID_EVENT)){
- if (ev.ret==LIBAROMA_HID_EV_RET_EXIT){
- break;
- }
- dest_ev->type=ev.type;
- dest_ev->key=ev.key;
- dest_ev->state=ev.state;
- dest_ev->x=ev.x;
- dest_ev->y=ev.y;
- return ev.ret;
- }
- }
- close(_avm_hid_rfd);
- return LIBAROMA_HID_EV_RET_EXIT;
- }
- byte avm_hid_init(LIBAROMA_HIDP me){
- if (_avm_hid_active){
- return 0;
- }
- int pipes[2];
- pipe(pipes);
- me->internal = NULL;
- me->release = &avm_hid_release;
- me->getinput = &avm_hid_getinput;
- _avm_hid_rfd = pipes[0];
- _avm_hid_wfd = pipes[1];
- _avm_hid_active = 1;
- return 1;
- }
- /*
- *
- * New Application
- *
- */
- pid_t avm_new_app(
- int appid,
- int wfd,
- int rfd,
- int efd,
- int dpi){
- pid_t apid = fork();
- if (apid==0){
- _avm_app = (AVMAPP *) calloc(sizeof(AVMAPP),1);
- _avm_app->aid = appid;
- _avm_app->pid = getpid();
- _avm_app->wfd = wfd;
- _avm_app->rfd = rfd;
- _avm_app->efd = efd;
- _avm_app->dpi = dpi;
- char canvas_name[256];
- snprintf(canvas_name,256,"@appfb-%i",appid);
- _avm_app->cfb = libaroma_canvas_shmem_open(canvas_name);
- snprintf(canvas_name,256,"@appsb-%i",appid);
- _avm_app->csb = libaroma_canvas_shmem_open(canvas_name);
- if (_avm_app->cfb&&_avm_app->csb){
- AVMLOGI("Init new application (id:%i,pid:%i)",
- _avm_app->aid, _avm_app->pid
- );
- }
- else{
- AVMLOGE("New application error - Cannot open shared canvas");
- }
- if (_avm_app->cfb){
- libaroma_canvas_free(_avm_app->cfb);
- }
- if (_avm_app->csb){
- libaroma_canvas_free(_avm_app->csb);
- }
- /* send exit message */
- avm_request(AVM_REQ_CMD_EXIT,0,NULL,0,NULL,NULL,NULL);
- exit(0);
- }
- else if (apid>0){
- }
- return apid;
- }
- #endif /* __libaroma_vm_com_c__ */
Advertisement
Add Comment
Please, Sign In to add comment