From f711588e3433e00736fb4b4b37ebabc1247568d7 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Mon, 16 Dec 2019 19:54:24 +0100 Subject: [PATCH] Add C xcb shm examples --- xcb-test/Makefile | 12 ++++ xcb-test/xcbshm.c | 105 ++++++++++++++++++++++++++++++ xcb-test/xcbshm2.c | 156 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 273 insertions(+) create mode 100644 xcb-test/Makefile create mode 100644 xcb-test/xcbshm.c create mode 100644 xcb-test/xcbshm2.c diff --git a/xcb-test/Makefile b/xcb-test/Makefile new file mode 100644 index 0000000..c17188b --- /dev/null +++ b/xcb-test/Makefile @@ -0,0 +1,12 @@ +all: xcbshm xcbshm2 + +xcbshm2: xcbshm2.c + gcc -o $@ -lxcb -lxcb-shm -lxcb-image $< + +xcbshm: xcbshm.c + gcc -o $@ -lxcb -lxcb-shm -lxcb-image $< + +.PHONY: clean + +clean: + @rm -f xcbshm xcbshm2 diff --git a/xcb-test/xcbshm.c b/xcb-test/xcbshm.c new file mode 100644 index 0000000..c0112b0 --- /dev/null +++ b/xcb-test/xcbshm.c @@ -0,0 +1,105 @@ +/** + * \file + * [: description :] + * + * \author + * Georg Hopp + * + * \copyright + * Copyright © 2019 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +// http://stackoverflow.com/questions/27745131 + +#include +#include + +#include +#include +#include + +#include + +#define W 512 +#define H 512 + + +int main(){ + //connect to the X server and get screen + xcb_connection_t* connection = xcb_connect(NULL, NULL); + xcb_screen_t* screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data; + + //create a window + uint32_t value_mask = XCB_CW_BACK_PIXEL|XCB_CW_EVENT_MASK; + uint32_t value_list[2] = {screen->black_pixel, XCB_EVENT_MASK_EXPOSURE}; + xcb_window_t window = xcb_generate_id(connection); + xcb_create_window(connection, screen->root_depth, window, screen->root, 0, 0, W, H, 0, + XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, value_mask, value_list); + + //create a graphic context + value_mask = XCB_GC_FOREGROUND|XCB_GC_GRAPHICS_EXPOSURES; + value_list[0] = screen->black_pixel; + value_list[1] = 0; + xcb_gcontext_t gcontext = xcb_generate_id(connection); + xcb_create_gc(connection, gcontext, window, value_mask, value_list); + + //map the window onto the screen + xcb_map_window(connection, window); + xcb_flush(connection); + + //Shm test + xcb_shm_segment_info_t info; + xcb_shm_query_version_reply(connection, xcb_shm_query_version(connection), NULL); + + info.shmid = shmget(IPC_PRIVATE, W*H*4, IPC_CREAT|0777); + info.shmaddr = shmat(info.shmid, 0, 0); + info.shmseg = xcb_generate_id(connection); + xcb_shm_attach(connection, info.shmseg, info.shmid, 0); + shmctl(info.shmid, IPC_RMID, 0); + uint8_t* data = info.shmaddr; + + xcb_pixmap_t pix = xcb_generate_id(connection); + xcb_shm_create_pixmap(connection, pix, window, W, H, screen->root_depth, info.shmseg, 0); + + + uint8_t lala[W*H*4] = {0}; + const xcb_setup_t* setup = xcb_get_setup(connection); + xcb_format_t* fmt = xcb_setup_pixmap_formats(setup); + printf("scanline_pad %u depth %u bpp %u byte_order %u\n", + fmt->scanline_pad, fmt->depth, fmt->bits_per_pixel, setup->image_byte_order); + xcb_image_t* img = xcb_image_create(W,H, XCB_IMAGE_FORMAT_Z_PIXMAP, + fmt->scanline_pad, 24, 32, 0, setup->image_byte_order, XCB_IMAGE_ORDER_LSB_FIRST, lala, W*H*4, lala); + + // xcb_image_shm_get(connection, window, img, info, 0,0, XCB_IMAGE_FORMAT_Z_PIXMAP); + // xcb_image_shm_put(connection, window, img, info, 0,0, XCB_IMAGE_FORMAT_Z_PIXMAP); + + + // -------------------------------------------------------------- + uint i = 0; + while(1){ + data[i++] = 0xff; + xcb_copy_area(connection, pix, window, gcontext, 0, 0, 0, 0, W, H); + xcb_flush(connection); + } + + xcb_shm_detach(connection, info.shmseg); + shmdt(info.shmaddr); + + xcb_free_pixmap(connection, pix); + xcb_destroy_window(connection, window); + xcb_disconnect(connection); +} + +// vim: set ts=4 sw=4: diff --git a/xcb-test/xcbshm2.c b/xcb-test/xcbshm2.c new file mode 100644 index 0000000..6242994 --- /dev/null +++ b/xcb-test/xcbshm2.c @@ -0,0 +1,156 @@ +/** + * \file + * [: description :] + * + * \author + * Georg Hopp + * + * \copyright + * Copyright © 2019 Georg Hopp + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include +#include + +#include +#include + +#include +#include +#include + + +#define WID 512 +#define HEI 512 + + +int main(){ + xcb_connection_t* connection; + xcb_window_t window; + xcb_screen_t* screen; + xcb_gcontext_t gcontext; + xcb_generic_event_t* event; + + uint32_t value_mask; + uint32_t value_list[2]; + + //connect to the X server and get screen + + connection = xcb_connect(NULL, NULL); + screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data; + + //create a window + + value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; + value_list[0] = screen->black_pixel; + value_list[1] = XCB_EVENT_MASK_EXPOSURE; + + window = xcb_generate_id(connection); + + xcb_create_window( + connection, + screen->root_depth, + window, + screen->root, + 0, 0, + WID, HEI, + 0, + XCB_WINDOW_CLASS_INPUT_OUTPUT, + screen->root_visual, + value_mask, value_list + ); + + //create a graphic context + + value_mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES; + value_list[0] = screen->black_pixel; + value_list[1] = 0; + + gcontext = xcb_generate_id(connection); + xcb_create_gc(connection, gcontext, window, value_mask, value_list); + + //map the window onto the screen + + xcb_map_window(connection, window); + xcb_flush(connection); + + + //Shm test + xcb_shm_query_version_reply_t* reply; + xcb_shm_segment_info_t info; + + reply = xcb_shm_query_version_reply( + connection, + xcb_shm_query_version(connection), + NULL + ); + + if(!reply || !reply->shared_pixmaps){ + printf("Shm error...\n"); + exit(0); + } + + info.shmid = shmget(IPC_PRIVATE, WID*HEI*4, IPC_CREAT | 0777); + info.shmaddr = shmat(info.shmid, 0, 0); + + info.shmseg = xcb_generate_id(connection); + xcb_shm_attach(connection, info.shmseg, info.shmid, 0); + shmctl(info.shmid, IPC_RMID, 0); + + uint32_t* data = (uint32_t*)info.shmaddr; + + xcb_pixmap_t pix = xcb_generate_id(connection); + xcb_shm_create_pixmap( + connection, + pix, + window, + WID, HEI, + screen->root_depth, + info.shmseg, + 0 + ); + + int i = 0; + while(1){ + usleep(10000); + + data[i] = 0xFFFFFF; + i++; + + xcb_copy_area( + connection, + pix, + window, + gcontext, + 0, 0, 0, 0, + WID, HEI + ); + + xcb_flush(connection); + } + + xcb_shm_detach(connection, info.shmseg); + shmdt(info.shmaddr); + + xcb_free_pixmap(connection, pix); + + xcb_destroy_window(connection, window); + xcb_disconnect(connection); + + return 0; +} + +// vim: set ts=4 sw=4: