如何存储在X11的事件产生的数据?(How to store the data generated

2019-10-30 05:15发布

我的工作是绘制一个X窗口形状的事件驱动的项目。 每当我在屏幕上点击鼠标,产生x和y的新值。 我的问题是:我怎么能存储在下面的每个时间假设你点击鼠标的码x和y的值不同,会产生x和y的新值。

int x, y;
x = report.xbutton.x;
y = report.xbutton.y;

if (report.xbutton.button == Button1) {
    XFillArc(display_ptr, win, gc_red, 
             x - win_height/80, y - win_height/80,
             win_height/60, win_height/60, 0, 360*64);
}

Answer 1:

代码的一个版本可能是:

typedef struct Position
{
    int x;
    int y;
} Position;

typedef struct PosnList
{
    size_t     num_pts;
    size_t     max_pts;
    Position  *points;
} PosnList;

void add_point(int x, int y, PosnList *p)
{
    if (p->num_pts >= p->max_pts)
    {
        size_t new_num = (p->max_pts + 2) * 2;
        Position *new_pts = realloc(p->points, new_num * sizeof(Position));
        if (new_pts == 0)
            ...handle out of memory error...
        p->max_pts = new_num;
        p->points  = new_pts;
    }
    p->points[p->num_pts++] = (Position){ x, y };
}

void zap_posnlist(PosnList *p)
{
     free(p->points);
     p->num_pts = 0;
     p->max_pts = 0;
     p->points  = 0;
}

那么你的代码会做:

int x, y;
x = report.xbutton.x;
y = report.xbutton.y;

if (report.xbutton.button == Button1) {
    XFillArc(display_ptr, win, gc_red, 
             x - win_height/80, y - win_height/80,
             win_height/60, win_height/60, 0, 360*64);
    add_point(x, y, &positions);
}

其中,地方你有一个变量:

PosnList positions = { 0, 0, 0 };

需要注意的是add_point()函数使用realloc()做了初始的内存分配和增量分配内存。 代码使用C99化合物字面到分配值xy到下一Position在数组中。 如果你没有C99,你需要做两个独立的任务。

zap_posnlist()函数释放先前初始化PosnList 。 你可能还需要一个正式的初始化函数-除非你乐于使用PosnList xxx = { 0, 0, 0 }; 符号无处不在。

此代码现在已经消毒的GCC; 原来的版本是不是和有它的错误 - 生成编译器错误的bug。


测试的代码-请注意, "stderr.h"是不是一个标准的头,而是错误报告代码,我习惯性地使用。 它提供了err_error()err_setarg0()函数。

#include <stdlib.h>
#include "stderr.h"

typedef struct Position
{
    int x;
    int y;
} Position;

typedef struct PosnList
{
    size_t     num_pts;
    size_t     max_pts;
    Position  *points;
} PosnList;

extern void add_point(int x, int y, PosnList *p);
extern void zap_posnlist(PosnList *p);

void add_point(int x, int y, PosnList *p)
{
    if (p->num_pts >= p->max_pts)
    {
        size_t new_num = (p->max_pts + 2) * 2;
        Position *new_pts = realloc(p->points, new_num * sizeof(Position));
        if (new_pts == 0)
            err_error("Out of memory (%s:%d - %zu bytes)\n",
                       __FILE__, __LINE__, new_num * sizeof(Position));
        p->max_pts = new_num;
        p->points  = new_pts;
    }
    p->points[p->num_pts++] = (Position){ x, y };
}

void zap_posnlist(PosnList *p)
{
    free(p->points);
    p->num_pts = 0;
    p->max_pts = 0;
    p->points  = 0;
}

#include <stdio.h>

int main(int argc, char **argv)
{
    PosnList positions = { 0, 0, 0 };

    err_setarg0(argv[0]);

    if (argc > 1)
        srand(atoi(argv[1]));

    for (size_t i = 0; i < 37; i++)
        add_point(rand(), rand(), &positions);

    for (size_t i = 0; i < positions.num_pts; i++)
        printf("%2zu: (%5d, %5d)\n", i, positions.points[i].x, positions.points[i].y);

    zap_posnlist(&positions);
    return(0);
}

联系我(见我的配置文件),如果你想源stderr.hstderr.c



文章来源: How to store the data generated by an event in X11?
标签: c x11