Unify basic X11 functionality in test programs

This commit is contained in:
Kim Woelders 2022-08-27 17:42:25 +02:00
parent d30460b39b
commit 0c687ee837
12 changed files with 151 additions and 207 deletions

View File

@ -16,29 +16,31 @@ imlib2_conv \
imlib2_load \
$(X_BASED_PROGS)
SRCS_COMMON_X11 = prog_x11.c prog_x11.h
imlib2_conv_SOURCES = imlib2_conv.c
imlib2_conv_LDADD = $(top_builddir)/src/lib/libImlib2.la
imlib2_load_SOURCES = imlib2_load.c
imlib2_load_LDADD = $(top_builddir)/src/lib/libImlib2.la $(CLOCK_LIBS)
imlib2_show_SOURCES = imlib2_show.c
imlib2_show_SOURCES = imlib2_show.c $(SRCS_COMMON_X11)
imlib2_show_LDADD = $(top_builddir)/src/lib/libImlib2.la -lX11 -lm
imlib2_test_SOURCES = imlib2_test.c
imlib2_test_SOURCES = imlib2_test.c $(SRCS_COMMON_X11)
imlib2_test_LDADD = $(top_builddir)/src/lib/libImlib2.la -lX11
imlib2_bumpmap_SOURCES = imlib2_bumpmap.c
imlib2_bumpmap_SOURCES = imlib2_bumpmap.c $(SRCS_COMMON_X11)
imlib2_bumpmap_LDADD = $(top_builddir)/src/lib/libImlib2.la -lX11
imlib2_poly_SOURCES = imlib2_poly.c
imlib2_poly_SOURCES = imlib2_poly.c $(SRCS_COMMON_X11)
imlib2_poly_LDADD = $(top_builddir)/src/lib/libImlib2.la -lX11
imlib2_colorspace_SOURCES = imlib2_colorspace.c
imlib2_colorspace_SOURCES = imlib2_colorspace.c $(SRCS_COMMON_X11)
imlib2_colorspace_LDADD = $(top_builddir)/src/lib/libImlib2.la -lX11
imlib2_view_SOURCES = imlib2_view.c props.c props.h
imlib2_view_SOURCES = imlib2_view.c $(SRCS_COMMON_X11)
imlib2_view_LDADD = $(top_builddir)/src/lib/libImlib2.la -lX11
imlib2_grab_SOURCES = imlib2_grab.c
imlib2_grab_SOURCES = imlib2_grab.c $(SRCS_COMMON_X11)
imlib2_grab_LDADD = $(top_builddir)/src/lib/libImlib2.la -lX11

View File

@ -6,7 +6,8 @@
#include <stdio.h>
#include <stdlib.h>
Display *disp;
#include "prog_x11.h"
Window win;
int
@ -22,29 +23,13 @@ main(int argc, char **argv)
*/
printf("Initialising\n");
/**
* First tests to determine which rendering task to perform
*/
disp = XOpenDisplay(NULL);
if (!disp)
{
fprintf(stderr, "Cannot open display\n");
return 1;
}
win = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 0, 0, 100, 100,
0, 0, 0);
XSelectInput(disp, win, KeyPressMask |
ButtonPressMask | ButtonReleaseMask | ButtonMotionMask |
PointerMotionMask | ExposureMask);
prog_x11_init();
win = prog_x11_create_window("imlib2_bumpmap", 100, 100);
/**
* Start rendering
*/
printf("Rendering\n");
imlib_context_set_display(disp);
imlib_context_set_visual(DefaultVisual(disp, DefaultScreen(disp)));
imlib_context_set_colormap(DefaultColormap(disp, DefaultScreen(disp)));
imlib_context_set_drawable(win);
imlib_context_set_dither(1);
imlib_context_set_blend(0);
@ -70,6 +55,10 @@ main(int argc, char **argv)
XNextEvent(disp, &ev);
switch (ev.type)
{
default:
if (prog_x11_event(&ev))
goto quit;
break;
case KeyPress:
keysym = XLookupKeysym(&ev.xkey, 0);
if (keysym == XK_q || keysym == XK_Escape)
@ -81,8 +70,6 @@ main(int argc, char **argv)
x = ev.xmotion.x;
y = ev.xmotion.y;
break;
default:
break;
}
}
while (XPending(disp));

View File

@ -6,12 +6,12 @@
#include <stdio.h>
#include <stdlib.h>
Display *disp;
Window win;
#include "prog_x11.h"
int
main(int argc, char **argv)
{
Window win;
int w, h;
Imlib_Image im_bg = NULL;
XEvent ev;
@ -23,21 +23,9 @@ main(int argc, char **argv)
int tw, th;
#endif
/**
* First tests to determine which rendering task to perform
*/
disp = XOpenDisplay(NULL);
if (!disp)
{
fprintf(stderr, "Cannot open display\n");
return 1;
}
prog_x11_init();
win = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 0, 0, 100, 100,
0, 0, 0);
XSelectInput(disp, win,
ButtonPressMask | ButtonReleaseMask | ButtonMotionMask |
PointerMotionMask | ExposureMask | KeyPressMask);
win = prog_x11_create_window("imlib2_colorspace", 100, 100);
/**
* Start rendering
@ -46,9 +34,6 @@ main(int argc, char **argv)
imlib_set_font_cache_size(512 * 1024);
imlib_add_path_to_font_path(PACKAGE_DATA_DIR "/data/fonts");
#endif
imlib_context_set_display(disp);
imlib_context_set_visual(DefaultVisual(disp, DefaultScreen(disp)));
imlib_context_set_colormap(DefaultColormap(disp, DefaultScreen(disp)));
imlib_context_set_drawable(win);
imlib_context_set_blend(0);
imlib_context_set_color_modifier(NULL);
@ -72,6 +57,10 @@ main(int argc, char **argv)
XNextEvent(disp, &ev);
switch (ev.type)
{
default:
if (prog_x11_event(&ev))
goto quit;
break;
case KeyPress:
keysym = XLookupKeysym(&ev.xkey, 0);
if (keysym == XK_q || keysym == XK_Escape)
@ -79,9 +68,6 @@ main(int argc, char **argv)
break;
case ButtonRelease:
goto quit;
default:
break;
}
}
while (XPending(disp));

View File

@ -6,8 +6,7 @@
#include <string.h>
#include <stdlib.h>
Display *disp;
int image_width = 0, image_height = 0;
#include "prog_x11.h"
static void
usage(void)
@ -24,7 +23,6 @@ main(int argc, char **argv)
char *file = NULL;
int verbose;
int get_alpha;
const char *display_name = getenv("DISPLAY");
Drawable draw;
int x, y;
unsigned int w, h, bw;
@ -93,18 +91,7 @@ main(int argc, char **argv)
file = argv[0];
if (!display_name)
display_name = ":0";
disp = XOpenDisplay(display_name);
if (!disp)
{
fprintf(stderr, "Can't open display %s\n", display_name);
return 1;
}
imlib_context_set_display(disp);
imlib_context_set_visual(DefaultVisual(disp, DefaultScreen(disp)));
imlib_context_set_colormap(DefaultColormap(disp, DefaultScreen(disp)));
prog_x11_init();
if (draw == None)
draw = DefaultRootWindow(disp);

View File

@ -6,40 +6,25 @@
#include <stdio.h>
#include <stdlib.h>
Display *disp;
Window win;
#include "prog_x11.h"
int
main(int argc, char **argv)
{
Window win;
int w, h;
Imlib_Image im_bg = NULL;
XEvent ev;
KeySym keysym;
ImlibPolygon poly, poly1, poly2;
/**
* First tests to determine which rendering task to perform
*/
disp = XOpenDisplay(NULL);
if (!disp)
{
fprintf(stderr, "Cannot open display\n");
return 1;
}
prog_x11_init();
win = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 0, 0, 100, 100,
0, 0, 0);
XSelectInput(disp, win,
ButtonPressMask | ButtonReleaseMask | ButtonMotionMask |
PointerMotionMask | ExposureMask | KeyPressMask);
win = prog_x11_create_window("imlib2_poly", 100, 100);
/**
* Start rendering
*/
imlib_context_set_display(disp);
imlib_context_set_visual(DefaultVisual(disp, DefaultScreen(disp)));
imlib_context_set_colormap(DefaultColormap(disp, DefaultScreen(disp)));
imlib_context_set_drawable(win);
imlib_context_set_blend(0);
imlib_context_set_color_modifier(NULL);
@ -79,6 +64,10 @@ main(int argc, char **argv)
XNextEvent(disp, &ev);
switch (ev.type)
{
default:
if (prog_x11_event(&ev))
goto quit;
break;
case KeyPress:
keysym = XLookupKeysym(&ev.xkey, 0);
switch (keysym)
@ -98,9 +87,6 @@ main(int argc, char **argv)
break;
case ButtonRelease:
goto quit;
default:
break;
}
}
while (XPending(disp));

View File

@ -11,8 +11,9 @@
#include <math.h>
#include <locale.h>
Display *disp;
Window win;
#include "prog_x11.h"
static Window win;
void progress(Imlib_Image * im, char percent, int update_x,
int update_y, int update_w, int update_h);
@ -21,7 +22,6 @@ void
progress(Imlib_Image * im, char percent,
int update_x, int update_y, int update_w, int update_h)
{
imlib_context_set_display(disp);
imlib_context_set_drawable(win);
imlib_context_set_dither(0);
imlib_context_set_blend(0);
@ -267,16 +267,7 @@ main(int argc, char **argv)
*/
if (!blendtest)
{
const char *display_name = getenv("DISPLAY");
if (!display_name)
display_name = ":0";
disp = XOpenDisplay(display_name);
if (!disp)
{
fprintf(stderr, "Can't open display %s\n", display_name);
return 1;
}
prog_x11_init();
#if 0
/* nasty - using imlib internal function.. but it makes benchmarks fair */
if (!interactive)
@ -285,14 +276,9 @@ main(int argc, char **argv)
if (root)
win = DefaultRootWindow(disp);
else
{
win =
XCreateSimpleWindow(disp, DefaultRootWindow(disp), 0, 0, 10,
10, 0, 0, 0);
XSelectInput(disp, win, KeyPressMask |
ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
| PointerMotionMask | ExposureMask);
}
win = prog_x11_create_window("imlib2_show", 100, 100);
imlib_context_set_drawable(win);
}
if (!interactive)
@ -338,13 +324,6 @@ main(int argc, char **argv)
*/
printf("rend\n");
if (!blendtest)
{
imlib_context_set_display(disp);
imlib_context_set_visual(DefaultVisual(disp, DefaultScreen(disp)));
imlib_context_set_colormap(DefaultColormap(disp, DefaultScreen(disp)));
imlib_context_set_drawable(win);
}
imlib_context_set_anti_alias(aa);
imlib_context_set_dither(dith);
imlib_context_set_blend(blend);
@ -957,6 +936,10 @@ main(int argc, char **argv)
XNextEvent(disp, &ev);
switch (ev.type)
{
default:
if (prog_x11_event(&ev))
goto quit;
break;
case Expose:
up = imlib_update_append_rect(up,
ev.xexpose.x,
@ -981,9 +964,6 @@ main(int argc, char **argv)
case MotionNotify:
x = ev.xmotion.x;
y = ev.xmotion.y;
default:
break;
}
}
while (XPending(disp));

View File

@ -6,14 +6,13 @@
#include <stdio.h>
#include <stdlib.h>
/* some globals for our window & X display */
Display *disp;
Window win;
#include "prog_x11.h"
/* the program... */
int
main(int argc, char **argv)
{
Window win;
/* events we get from X */
XEvent ev;
KeySym keysym;
@ -30,27 +29,18 @@ main(int argc, char **argv)
/* our mouse x, y coordinates */
int mouse_x = 0, mouse_y = 0;
/* connect to X */
disp = XOpenDisplay(NULL);
if (!disp)
{
fprintf(stderr, "Cannot open display\n");
return 1;
}
prog_x11_init();
/* get default visual , colormap etc. you could ask imlib2 for what it */
/* thinks is the best, but this example is intended to be simple */
/* create a window 640x480 */
win = XCreateSimpleWindow(disp, DefaultRootWindow(disp),
0, 0, 640, 480, 0, 0, 0);
/* tell X what events we are interested in */
XSelectInput(disp, win, KeyPressMask | ButtonPressMask | ButtonReleaseMask |
PointerMotionMask | ExposureMask);
win = prog_x11_create_window("imlib2_test", 640, 480);
/* show the window */
XMapWindow(disp, win);
/* set our cache to 2 Mb so it doesn't have to go hit the disk as long as */
/* the images we use use less than 2Mb of RAM (that is uncompressed) */
imlib_set_cache_size(2048 * 1024);
#if ENABLE_TEXT
/* set the font cache to 512Kb - again to avoid re-loading */
imlib_set_font_cache_size(512 * 1024);
@ -58,14 +48,14 @@ main(int argc, char **argv)
/* in that dir for the text to display */
imlib_add_path_to_font_path(PACKAGE_DATA_DIR "/data/fonts");
#endif
/* set the maximum number of colors to allocate for 8bpp and less to 128 */
imlib_set_color_usage(128);
/* dither for depths < 24bpp */
imlib_context_set_dither(1);
/* set the display , visual, colormap and drawable we are using */
imlib_context_set_display(disp);
imlib_context_set_visual(DefaultVisual(disp, DefaultScreen(disp)));
imlib_context_set_colormap(DefaultColormap(disp, DefaultScreen(disp)));
/* set the drawable we are using */
imlib_context_set_drawable(win);
/* infinite event loop */
@ -91,6 +81,10 @@ main(int argc, char **argv)
XNextEvent(disp, &ev);
switch (ev.type)
{
default:
if (prog_x11_event(&ev))
goto quit;
break;
case Expose:
/* window rectangle was exposed - add it to the list of */
/* rectangles we need to re-render */
@ -160,9 +154,6 @@ main(int argc, char **argv)
}
#endif /* ENABLE_TEXT */
break;
default:
/* any other events - do nothing */
break;
}
}
while (XPending(disp));

View File

@ -9,13 +9,11 @@
#include <stdbool.h>
#include <unistd.h>
#include "props.h"
#include "prog_x11.h"
#define MIN(a, b) ((a < b) ? a : b)
#define MAX(a, b) ((a > b) ? a : b)
Display *disp;
typedef struct {
int x, y; /* Origin */
int w, h; /* Size */
@ -599,23 +597,9 @@ main(int argc, char **argv)
return 1;
}
disp = XOpenDisplay(NULL);
if (!disp)
{
fprintf(stderr, "Cannot open display\n");
return 1;
}
prog_x11_init();
win = XCreateSimpleWindow(disp, DefaultRootWindow(disp), 0, 0, 10, 10,
0, 0, 0);
XSelectInput(disp, win, KeyPressMask | ButtonPressMask | ButtonReleaseMask |
ButtonMotionMask | PointerMotionMask);
props_win_set_proto_quit(win);
imlib_context_set_display(disp);
imlib_context_set_visual(DefaultVisual(disp, DefaultScreen(disp)));
imlib_context_set_colormap(DefaultColormap(disp, DefaultScreen(disp)));
win = prog_x11_create_window("imlib2_view", 10, 10);
if (opt_progr)
{
@ -678,10 +662,7 @@ main(int argc, char **argv)
switch (ev.type)
{
default:
break;
case ClientMessage:
if (props_clientmsg_check_quit(&ev.xclient))
if (prog_x11_event(&ev))
goto quit;
break;
case KeyPress:

73
src/bin/prog_x11.c Normal file
View File

@ -0,0 +1,73 @@
/*
* Common program functionality
*/
#include "config.h"
#include <Imlib2.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include "prog_x11.h"
Display *disp = NULL;
static Atom ATOM_WM_DELETE_WINDOW = None;
static Atom ATOM_WM_PROTOCOLS = None;
int
prog_x11_init(void)
{
disp = XOpenDisplay(NULL);
if (!disp)
{
fprintf(stderr, "Cannot open display\n");
return 1;
}
imlib_context_set_display(disp);
imlib_context_set_visual(DefaultVisual(disp, DefaultScreen(disp)));
imlib_context_set_colormap(DefaultColormap(disp, DefaultScreen(disp)));
return 0;
}
Window
prog_x11_create_window(const char *name, int w, int h)
{
Window win;
int x, y;
x = y = 0;
win = XCreateSimpleWindow(disp, DefaultRootWindow(disp),
x, y, w, h, 0, 0, 0);
XSelectInput(disp, win, KeyPressMask |
ButtonPressMask | ButtonReleaseMask | ButtonMotionMask |
PointerMotionMask | ExposureMask);
XStoreName(disp, win, name);
ATOM_WM_PROTOCOLS = XInternAtom(disp, "WM_PROTOCOLS", False);
ATOM_WM_DELETE_WINDOW = XInternAtom(disp, "WM_DELETE_WINDOW", False);
XSetWMProtocols(disp, win, &ATOM_WM_DELETE_WINDOW, 1);
return win;
}
int
prog_x11_event(XEvent * ev)
{
switch (ev->type)
{
default:
break;
case ClientMessage:
if (ev->xclient.message_type == ATOM_WM_PROTOCOLS &&
ev->xclient.data.l[0] == (long)ATOM_WM_DELETE_WINDOW)
return 1;
break;
}
return 0;
}

12
src/bin/prog_x11.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef PROG_X11_H
#define PROG_X11_H
#include <X11/Xlib.h>
extern Display *disp;
int prog_x11_init(void);
Window prog_x11_create_window(const char *name, int w, int h);
int prog_x11_event(XEvent * ev);
#endif /* PROG_X11_H */

View File

@ -1,25 +0,0 @@
/*
* Property handling
*/
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include "props.h"
static Atom ATOM_WM_DELETE_WINDOW = None;
static Atom ATOM_WM_PROTOCOLS = None;
void
props_win_set_proto_quit(Window win)
{
ATOM_WM_PROTOCOLS = XInternAtom(disp, "WM_PROTOCOLS", False);
ATOM_WM_DELETE_WINDOW = XInternAtom(disp, "WM_DELETE_WINDOW", False);
XSetWMProtocols(disp, win, &ATOM_WM_DELETE_WINDOW, 1);
}
int
props_clientmsg_check_quit(const XClientMessageEvent * ev)
{
return ev->message_type == ATOM_WM_PROTOCOLS &&
(Atom) ev->data.l[0] == ATOM_WM_DELETE_WINDOW;
}

View File

@ -1,16 +0,0 @@
/*
* Property handling
*/
#ifndef PROPS_H
#define PROPS_H
#include <X11/Xatom.h>
#include <X11/Xlib.h>
extern Display *disp;
void props_win_set_proto_quit(Window win);
int props_clientmsg_check_quit(const XClientMessageEvent * ev);
#endif /* PROPS_H */