How to store the data generated by an event in X11

2019-09-15 13:03发布

I am working on an event-driven project that draws shape on an x-window. Whenever I click a mouse on the screen, new values of x and y are generated. My question is: how can I store the different values of x and y in the code below assuming each time you click a mouse, new values of x and y are generated.

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);
}

标签: c x11
1条回答
SAY GOODBYE
2楼-- · 2019-09-15 13:24

One version of the code might be:

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;
}

Then your code would do:

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);
}

where somewhere you have a variable:

PosnList positions = { 0, 0, 0 };

Note that the add_point() function uses realloc() to do both the initial memory allocation and incremental memory allocations. The code uses a C99 compound literal to assign the values x and y to the next Position in the array. If you don't have C99, you'll need to do two separate assignments.

The zap_posnlist() function releases a previously initialized PosnList. You might still need a formal initializer function — unless you're happy to use the PosnList xxx = { 0, 0, 0 }; notation everywhere.

This code has now been sanitized by GCC; the original edition was not and there were bugs in it — bugs that generated compiler errors.


Tested code — note that "stderr.h" is not a standard header but is the error reporting code I habitually use. It provides the err_error() and err_setarg0() functions.

#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);
}

Contact me (see my profile) if you want source for stderr.h and stderr.c.

查看更多
登录 后发表回答