Design and Implementation of ftk for Embedded GUI-ftksource)

Source: Internet
Author: User

Indicate the source and author's contact information during reprinting.
Article Source: http://www.limodev.cn/blog
Author contact: Li xianjing <xianjimli@gmail.com>

In the main loop section, we introduce how mainloop processes various event sources. It waits for an event to occur on the event source, and then calls the processing function of the event source to process the event. The event source (ftksource) is an abstraction of the event source. The event source may be an input device (such as a keyboard or touch screen), or a timer, it may also be a network socket or pipe. In short, as long as the interface required by ftksource is implemented, mainloop can be processed.

Ftksource requires the following interface functions:

* Ftk_source_get_fd is used to obtain the file descriptor. This file descriptor is not necessarily a real file descriptor, as long as it is a handle that can be hung on the mainloop.

* Ftk_source_check is used to check the time required by the event source. -1 indicates that you do not care about the waiting time. 0 indicates that an event will occur immediately, and a positive number indicates that an event will occur within the specified time.

* Ftk_source_dispatch is used to process events. Each event source has its own processing function, which simplifies the implementation of the Program.

Currently, ftk uses the following event sources:

1. ftk_source_input (. c/. h)

In Linux, all input device files are in the/dev/input/directory, and the events are reported to the application using the consistent event structure (input_event, ftk_source_input is an event source for these device files.

Because the input events are read from the device files, the main loop can be hung on these device files to wait for the event to occur. Therefore, ftk_source_get_fd only needs to return the file descriptor, while ftk_source_check returns-1.

Static int ftk_source_input_get_fd (ftksource * thiz)
{
Decl_priv (thiz, priv );
 
Return priv-> FD;
}
 
Static int ftk_source_input_check (ftksource * thiz)
{
Return-1;
}

Read the event (input_event) in ftk_source_dispatch, convert it to the ftk event structure, and dispatch the event (call ftk_wnd_manager_queue_event.

Key-value ing is placed in the s_key_mapp table:

Static unsigned short s_key_map [0x100] =
{
[Key_1] = ftk_key_1,
[Key_2] = ftk_key_2,
[Key_3] = ftk_key_3,
[Key_4] = ftk_key_4,
[Key_5] = ftk_key_5,
[Key_6] = ftk_key_6,
[Key_7] = ftk_key_7,
...
};

If you have special key values or other requirements, modify this structure.

2. ftk_source_dfb (. c/. h)

Ftk can use directfb as backend, and ftk_source_dfb is the event source for directfb input events. Directfb can be directly read from eventbuffer, or get the file descriptor of an MTS queue from eventbuffer, and then read events from this MTS queue. For convenience, we use the latter to implement ftk_source_dfb. The main loop can be mounted to this file descriptor to wait for the event. Therefore, ftk_source_get_fd only needs to return the file descriptor, while ftk_source_check returns-1.

Static int ftk_source_dfb_get_fd (ftksource * thiz)
{
Decl_priv (thiz, priv );
 
Return priv-> FD;
}
 
Static int ftk_source_dfb_check (ftksource * thiz)
{
Return-1;
}

Read the event (dfbevent) in ftk_source_dispatch, convert it to the ftk event structure, and dispatch the event (call ftk_wnd_manager_queue_event.

Key-value ing is placed in the s_key_mapp table:

Static const int s_key_map [] =
{
[DIKI_A-DIKI_UNKNOWN] = ftk_key_a,
[DIKI_B-DIKI_UNKNOWN] = maid,
[DIKI_C-DIKI_UNKNOWN] = ftk_key_c,
[DIKI_D-DIKI_UNKNOWN] = ftk_key_d,
[DIKI_E-DIKI_UNKNOWN] = ftk_key_e,
[DIKI_F-DIKI_UNKNOWN] = ftk_key_f,
[DIKI_G-DIKI_UNKNOWN] = ftk_key_g,
[DIKI_H-DIKI_UNKNOWN] = ftk_key_h,
...
};

If you have special key values or other requirements, modify this structure.

3. ftk_source_tslib (. c/. h)

For a resistive touch screen, although it is usually reported by a device file under (/dev/input) in Linux, however, it is necessary to deshake, filter, and correct input events before they can be used. tslib is dedicated to these tasks. Therefore, it is more wise to use tslib to read events.

Tslib provides a function to obtain the file descriptor. The main loop can be mounted to this file descriptor to wait for the event. Therefore, ftk_source_get_fd only needs to return the file descriptor, while ftk_source_check returns-1.

Static int ftk_source_tslib_get_fd (ftksource * thiz)
{
Decl_priv (thiz, priv );
Return_val_if_fail (priv! = NULL & priv-> TS! = NULL,-1 );
 
Return ts_fd (priv-> TS );
}
 
Static int ftk_source_tslib_check (ftksource * thiz)
{
Return-1;
}

Read the event (ts_sample) in ftk_source_dispatch, convert it to the ftk event structure, and dispatch the event (call ftk_wnd_manager_queue_event.

4. ftk_source_primary (. c/. h)

Ftk_source_primary has a special position, which is equivalent to event queues in other guis.

Ftk_source_primary uses pipelines to implement the first-in-first-out (FIFO) feature of queues. This prevents the introduction of mutex to protect queues. Pipelines have their own file descriptors, the main loop can be mounted on this file descriptor to wait for the event. Therefore, ftk_source_get_fd only needs to return the file descriptor, while ftk_source_check returns-1.

Static int ftk_source_primary_get_fd (ftksource * thiz)
{
Decl_priv (thiz, priv );
Return_val_if_fail (priv! = NULL,-1 );
 
Return ftk_pipe_get_read_handle (priv-> pipe );
}
 
Static int ftk_source_primary_check (ftksource * thiz)
{
Return-1;
}

In the ftk_source_primary_dispatch function, the ftk_evt_add_source/ftk_evt_remove_source events are specially processed to add and remove event sources. For other events, the event distribution function of the window manager is called to process the events.

Ftk_source_primary provides ftk_source_queue_event for writing events to pipelines, writing events to pipelines, and adding events to event queues in other guis.

5. ftk_source_timer

Ftk_source_timer is mainly used to regularly execute an action, such as flashing the cursor and updating the time. Unlike the previous event source, it does not have the corresponding file descriptor, and the main loop cannot wait for the event to occur by hanging on the file descriptor. Therefore, ftk_source_get_fd always returns-1:

Static int ftk_source_timer_get_fd (ftksource * thiz)
{
Return-1;
}

Ftk_source_check returns the interval of the next event, telling mainloop to wake up at this time and execute the processing function:

Static int ftk_source_timer_check (ftksource * thiz)
{
Decl_priv (thiz, priv );
Int T = priv-> next_time-ftk_get_relative_time ();
 
T = T <0? 0: T;
 
Return T;
}

The implementation of ftk_source_timer_dispatch is very simple. It calculates the next timer time and then calls the callback function set by the user.

Tatic RET ftk_source_timer_dispatch (ftksource * thiz)
{
RET ret = ret_fail;
Decl_priv (thiz, priv );
Return_val_if_fail (priv-> action! = NULL, ret_remove );
 
If (thiz-> disable> 0)
{
Ftk_source_timer_calc_timer (priv );
Return ret_ OK;
}
 
Ret = priv-> action (priv-> user_data );
Ftk_source_timer_calc_timer (priv );
 
Return ret;
}

5. ftk_source_idle (. c/. h)

The main functions of idle include:

* Perform low-priority tasks when idle. Some tasks have a low priority, but the cost is relatively long, such as screen refresh and other operations. To avoid interfering with the current operation for too long, we will put it in idle.

* Asynchronization of synchronization operations. Some operations may not require synchronous execution in the current processing function. In this case, you can also use idle for Asynchronization so that it can be executed in the following dispatch.

* Serialize access to the GUI. In ftk, for efficiency reasons, Gui objects are not locked, that is, only GUI threads can access these objects. If other threads want to access GUI objects, idle must be used for serialization. Idle is executed in the GUI thread (main thread), so it can access GUI objects.

The implementation of idle is a timer with a timeout of 0. It is mainly made more clear in concept:

Static int ftk_source_idle_get_fd (ftksource * thiz)
{
Return-1;
}
 
Static int ftk_source_idle_check (ftksource * thiz)
{
Return 0;
}

Ftk_source_idle_dispatch is just a simple callback function called by the user.

Static RET ftk_source_idle_dispatch (ftksource * thiz)
{
Decl_priv (thiz, priv );
 
Return_val_if_fail (priv-> action! = NULL, ret_remove );
 
If (thiz-> disable> 0)
{
Return ret_ OK;
}
 
Return priv-> action (priv-> user_data );
}

There are other event sources in ftk, such as the event sources for X11 simulation. Their implementation is similar, so we will not talk about them here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.