2016/01/18 - start and baseApi

*2016/01/18*

Began to implement UI based on an open source graphics library which is called “Cairo”.

The first time I knew this graphics library was from Polish consumer. They were using Cairo to implement the display module, and it seems good, fast refresh speed, and flexible.

These days I decided to try to encapsulate a set of UI interfaces in my spare time. I believe I will have a great harvest in this project.

Structure:

This UI interface will contains input sections, including keyboard and touch screen.
First step, I will implement the input device base interfaces.
After that, implement the UI interfaces with Cairo and base interfaces.

Working Record

  1. Build a new C project, and delete the default files in the “src” folder.
  2. Add the relevant files which Maciek sent to me , including liblib_cairo.so and some header of cairo as the following:cairo.h, cairo-deprecated.h, cairo-features.h, cairo-ft.h, cairo-version.h.
  3. Implement the basement layer functions for working with Cairo, divided into three parts: screen, touch screen and keyboard.
    (1). Screen Module(screen.h, screen.c). Get the information of frame buffer (Get the width and height of screen, the size of buffer, the bits number of pixel, frame buffer memory mapping),check whether the screen is rotated, and provide interfaces for other files to get the these information.
//use struct save info
static struct {
    int scrHandle;
    int scrWidth;
    int scrHeight;
    int fbSize;
    short rotateVal;
    short bitsPerPixel;
    unsigned short mmapFB;
} screenInfo = {-1, -1, -1, -1, -1, -1, 0};

screenInfo.scrHandle = open(PATH_FRAMEBUFFER, O_RDWR);
if (screenInfo.scrHandle < 0) {
    return KEVINTESTUIDEF_OPENFAILED;
}
//get framebuffer info
if (ioctl(screenInfo.scrHandle, FBIOGET_FSCREENINFO, &fbInfo)) {
    return KEVINTESTUIDEF_OPENFAILED;
}
//set frame buffer info to struct
screenInfo.scrWidth = screenInfo.rotateVal ? fbInfo.yres : fbInfo.xres;
screenInfo.scrHeight = screenInfo.rotateVal ? fbInfo.xres : fbInfo.yres;
screenInfo.fbSize = fbInfo.xres * fbInfo.yres * fbInfo.bits_per_pixel;
screenInfo.bitsPerPixel = fbInfo.bits_per_pixel;
//map frame buffer
screenInfo.mmapFB = mmap(0, screenInfo.fbSize, PROT_READ | PROT_WRITE, 
MAP_SHARED, screenInfo.scrHandle, 0);
if (screenInfo.mmapFB == MAP_FAILED) {
    return KEVINTESTUIDEF_OPENFAILED;
}
//APIs
int KevinTestUIFunc_scrOpen(void);
void KevinTestUIFunc_scrClose(void);
unsigned short *KevinTestUIFunc_scrGetFb(void);
int KevinTestUIFunc_scrGetWidth(void);
int KevinTestUIFunc_scrGetHeight(void);
int KevinTestUIFunc_scrGetFbSize(void);
int KevinTestUIFunc_scrGetRotateVal(void);
int KevinTestUIFunc_scrGetBitsPerPixel(void);
(2). TouchScreen Module(touchScreen.h、touchScreen.c).Check whether the screen is touch screen, get touch screen events and touch points by reading touch screen device node.
//use struct save info
static struct {
    int tsHandle;
    int isTsHit;
    int tsXpos;
    int tsYpos;
} tsInfo = {-1, -1, -1, -1};

//functions
int KevinTestUIFunc_tsIsTouchScreen(void);
int KevinTestUIFunc_tsOpen(void);
void KevinTestUIFunc_tsClose(void);
int KevinTestUIFunc_tsHit(void);
int KevinTestUIFunc_tsGetPosition(int *x, int *y);
(3). Keyboard Module(keyboard.h、keyboard.c). Get keyboard events and key value by reading keyboard device node. After that, convert the key value with my macro definition.
//use struct save info
static struct {
    int keyHandle;
    int isKeyPressed;
    int keyValue;
} keyInfo = {-1, 0, -1};

//functions
int KevinTestUIFunc_kbOpen(void);
void KevinTestUIFunc_kbClose(void);
void KevinTestUIFunc_kbFlush(void);
int KevinTestUIFunc_kbPressed(void);
int KevinTestUIFunc_kbGetKey(void);

Listening the device input events by using input sub system and poll() function, the typical using method as below:

int KevinTestUIFunc_kbPressed(void) {
    int ret=0, i=0;
    struct pollfd event;
    struct input_event inputEvent[64];

    memset(&event, 0, sizeof(event));
    memset(inputEvent, 0, sizeof(inputEvent));

    if (keyInfo.keyHandle < 0) {
        return KEVINTESTUIDEF_NOTOPENED;
    }
    event.fd = keyInfo.keyHandle;
    event.events = POLLIN | POLLERR;
    ret = poll(&event, 1, 0);
    if ((ret < 0) || (event.revents & POLLERR) || !(event.revents & POLLIN)) {
        return keyInfo.isKeyPressed;
    }
    ret = read(keyInfo.keyHandle, inputEvent, sizeof(inputEvent));
    if (ret < sizeof(struct input_event)) {
        return keyInfo.isKeyPressed;
    }
    for (i = 0; i < ret / sizeof(struct input_event); i++) {
        if ((inputEvent[i].type == EV_KEY) && (inputEvent[i].value == 1)) {
            keyInfo.keyValue = inputEvent[i].code;
            keyInfo.isKeyPressed = KEVINTESTUIDEF_HASPRESSED;
        }
    }
    return keyInfo.isKeyPressed;
}

Notes:

  1. When need to save a set of data for a event, store in a struct is a good choice, and provide interfaces for other function to get or modify these data.
  2. Use macro definition flexible, it will help a lot when modified and maintained.
  3. After use close() function close the file handle, remember set file handle to default value, otherwise it probable lead wrong operate. Free memory is the same.
  4. Initialize value should be notice don’t conflict with the real operate. Especially init with 0 or -1.
  5. At the beginning of the functions, should check the validity of incoming parameters and dependencies.
  6. Implement interfaces should be taken into consideration. Every invoke conditions should be considered.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值