Chinaunix首页 | 论坛 | 博客
  • 博客访问: 849026
  • 博文数量: 168
  • 博客积分: 5431
  • 博客等级: 大校
  • 技术积分: 1560
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-22 11:56
文章存档

2015年(2)

2014年(1)

2013年(12)

2012年(12)

2011年(15)

2010年(5)

2009年(16)

2008年(41)

2007年(64)

分类: LINUX

2007-11-14 19:22:52

Linux下share memory的访问方法
前一段时间公司的项目需要我来完成一个简单的API通过访问share memory的方法来控制硬件设备,现把代码共享出来一起学习:
/*
 *************************************************************
 *      This is for swithc on/off asus A8E notebook touchpad
 *      Before using this program the SHMconfig option need
 *      to on for accessing the share memory,else my program
 *      won't work!
 *                              Author  :       Stephen Du
 *                              Date    :       07-11-8
 *************************************************************
 */

#include
#include
#include
#include

#include
#include
#include

#define SHMEM_SYNAP 23947

using namespace std;

typedef enum {
    RT_TAP = 0,                             /* Right top corner */
    RB_TAP,                                 /* Right bottom corner */
    LT_TAP,                                 /* Left top corner */
    LB_TAP,                                 /* Left bottom corner */
    F1_TAP,                                 /* Non-corner tap, one finger */
    F2_TAP,                                 /* Non-corner tap, two fingers */
    F3_TAP,                                 /* Non-corner tap, three fingers */
    MAX_TAP
} TapEvent;

struct SynapticsHwInfo
{
    unsigned int model_id;                  /* Model-ID */
    unsigned int capabilities;              /* Capabilities */
    unsigned int ext_cap;                   /* Extended Capabilities */
    unsigned int identity;                  /* Identification */
    int hasGuest;                          /* Has a guest mouse */
};

struct cSynapSHMem{
    int version;                            /* Driver version */

    /* Current device state */
    int x, y;                               /* actual x, y coordinates */
    int z;                                  /* pressure value */
    int numFingers;                         /* number of fingers */
    int fingerWidth;                        /* finger width value */
    int left, right, up, down;              /* left/right/up/down buttons */
    int multi[8];
    int middle;
    int guest_left, guest_mid, guest_right; /* guest device buttons */
    int guest_dx, guest_dy;                 /* guest device movement */

    /* Probed hardware properties */
    struct SynapticsHwInfo synhw;

    /* Parameter data */
    int left_edge, right_edge, top_edge, bottom_edge; /* edge coordinates absolute */
    int finger_low, finger_high;            /* finger detection values in Z-values */
    int tap_time;
    int tap_move;                           /* max. tapping time and movement in packets and coord. */
    int single_tap_timeout;                 /* timeout to recognize a single tap */
    int tap_time_2;                         /* max. tapping time for double taps */
    int click_time;                         /* The duration of a single click */
    int fast_taps;                         /* Faster reaction to single taps */
    int emulate_mid_button_time;            /* Max time between left and right button presses to
                                               emulate a middle button press. */
    int scroll_dist_vert;                   /* Scrolling distance in absolute coordinates */
    int scroll_dist_horiz;                  /* Scrolling distance in absolute coordinates */
    int scroll_edge_vert;                  /* Enable/disable vertical scrolling on right edge */
    int scroll_edge_horiz;                 /* Enable/disable horizontal scrolling on left edge */
    int scroll_twofinger_vert;             /* Enable/disable vertical two-finger scrolling */
    int scroll_twofinger_horiz;            /* Enable/disable horizontal two-finger scrolling */
    double min_speed, max_speed, accl;      /* movement parameters */
    int edge_motion_min_z;                  /* finger pressure at which minimum edge motion speed is set */
    int edge_motion_max_z;                  /* finger pressure at which maximum edge motion speed is set */
    int edge_motion_min_speed;              /* slowest setting for edge motion speed */
    int edge_motion_max_speed;              /* fastest setting for edge motion speed */
    int edge_motion_use_always;            /* If false, egde motion is used only when dragging */

    int updown_button_scrolling;           /* Up/Down-Button scrolling or middle/double-click */
    int leftright_button_scrolling;        /* Left/right-button scrolling, or two lots of middle button */
    int updown_button_repeat;              /* If up/down button being used to scroll, auto-repeat?*/
    int leftright_button_repeat;           /* If left/right button being used to scroll, auto-repeat? */
    int scroll_button_repeat;               /* time, in milliseconds, between scroll events being
                                             * sent when holding down scroll buttons */
    int touchpad_off;                       /* Switches the touchpad off
                                             * 0 : Not off
                                             *                                              * 1 : Off
                                             *                                                                                           * 2 : Only tapping and scrolling off
                                             *                                                                                                                                        */
    int guestmouse_off;                    /* Switches the guest mouse off */
    int locked_drags;                      /* Enable locked drags */
    int tap_action[MAX_TAP];                /* Button to report on tap events */
    int circular_scrolling;                /* Enable circular scrolling */
    double scroll_dist_circ;                /* Scrolling angle radians */
    double scroll_dist_circ;                /* Scrolling angle radians */
    int circular_trigger;                   /* Trigger area for circular scrolling */
    int circular_pad;                      /* Edge has an oval or circular shape */
    int palm_detect;                       /* Enable Palm Detection */
    int palm_min_width;                     /* Palm detection width */
    int palm_min_z;                         /* Palm detection depth */
    double coasting_speed;                  /* Coasting threshold scrolling speed */
    int press_motion_min_z;                 /* finger pressure at which minimum pressure motion factor is applied */
    int press_motion_max_z;                 /* finger pressure at which maximum pressure motion factor is applied */
    double press_motion_min_factor;         /* factor applied on speed when finger pressure is at minimum */
    double press_motion_max_factor;         /* factor applied on speed when finger pressure is at minimum */
};


int
main()
{

/*
 *      We need to map the keyboard and touchpad related share memory to our process
 *      So that control the touchapd and keyboard!
 */

static cSynapSHMem* p_shmem;// = static_cast(::operator new (sizeof(cSynapSHMem)));
key_t shmem_id;

if((shmem_id = shmget(SHMEM_SYNAP, sizeof(cSynapSHMem), 0)) == -1){
        if((shmem_id = shmget(SHMEM_SYNAP, 0, 0)) == -1){
                fprintf(stderr, "Can't get share memory!\nYour share memory might be off(SHMconfig option)!\n");
                exit(-1);
        }else{
                fprintf(stderr, "The sizeof memory you allocate might be incorrect!\n");
                exit(-1);
        }
}

if((p_shmem=static_cast(shmat(shmem_id, NULL, 0))) == NULL){
        fprintf(stderr, "Map share memory failed!\n");
        exit(-1);
}

/*
 *      Now we firstly get the status of touchpad I mean on/off/tap off
 *      then set it to status we need is to be!
 */

int swch;
while(1){
        cin>>swch;
        swch = swch % 3;
        p_shmem->touchpad_off = swch;
        usleep(30);
}
}

阅读(6652) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~