Commit f711588e3433e00736fb4b4b37ebabc1247568d7

Authored by Georg Hopp
1 parent 4edbecaf

Add C xcb shm examples

  1 +all: xcbshm xcbshm2
  2 +
  3 +xcbshm2: xcbshm2.c
  4 + gcc -o $@ -lxcb -lxcb-shm -lxcb-image $<
  5 +
  6 +xcbshm: xcbshm.c
  7 + gcc -o $@ -lxcb -lxcb-shm -lxcb-image $<
  8 +
  9 +.PHONY: clean
  10 +
  11 +clean:
  12 + @rm -f xcbshm xcbshm2
... ...
  1 +/**
  2 + * \file
  3 + * [: description :]
  4 + *
  5 + * \author
  6 + * Georg Hopp <georg@steffers.org>
  7 + *
  8 + * \copyright
  9 + * Copyright © 2019 Georg Hopp
  10 + *
  11 + * This program is free software: you can redistribute it and/or modify
  12 + * it under the terms of the GNU General Public License as published by
  13 + * the Free Software Foundation, either version 3 of the License, or
  14 + * (at your option) any later version.
  15 + *
  16 + * This program is distributed in the hope that it will be useful,
  17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19 + * GNU General Public License for more details.
  20 + *
  21 + * You should have received a copy of the GNU General Public License
  22 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23 + */
  24 +// http://stackoverflow.com/questions/27745131
  25 +
  26 +#include <sys/ipc.h>
  27 +#include <sys/shm.h>
  28 +
  29 +#include <xcb/xcb.h>
  30 +#include <xcb/xcb_image.h>
  31 +#include <xcb/shm.h>
  32 +
  33 +#include <stdio.h>
  34 +
  35 +#define W 512
  36 +#define H 512
  37 +
  38 +
  39 +int main(){
  40 + //connect to the X server and get screen
  41 + xcb_connection_t* connection = xcb_connect(NULL, NULL);
  42 + xcb_screen_t* screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data;
  43 +
  44 + //create a window
  45 + uint32_t value_mask = XCB_CW_BACK_PIXEL|XCB_CW_EVENT_MASK;
  46 + uint32_t value_list[2] = {screen->black_pixel, XCB_EVENT_MASK_EXPOSURE};
  47 + xcb_window_t window = xcb_generate_id(connection);
  48 + xcb_create_window(connection, screen->root_depth, window, screen->root, 0, 0, W, H, 0,
  49 + XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual, value_mask, value_list);
  50 +
  51 + //create a graphic context
  52 + value_mask = XCB_GC_FOREGROUND|XCB_GC_GRAPHICS_EXPOSURES;
  53 + value_list[0] = screen->black_pixel;
  54 + value_list[1] = 0;
  55 + xcb_gcontext_t gcontext = xcb_generate_id(connection);
  56 + xcb_create_gc(connection, gcontext, window, value_mask, value_list);
  57 +
  58 + //map the window onto the screen
  59 + xcb_map_window(connection, window);
  60 + xcb_flush(connection);
  61 +
  62 + //Shm test
  63 + xcb_shm_segment_info_t info;
  64 + xcb_shm_query_version_reply(connection, xcb_shm_query_version(connection), NULL);
  65 +
  66 + info.shmid = shmget(IPC_PRIVATE, W*H*4, IPC_CREAT|0777);
  67 + info.shmaddr = shmat(info.shmid, 0, 0);
  68 + info.shmseg = xcb_generate_id(connection);
  69 + xcb_shm_attach(connection, info.shmseg, info.shmid, 0);
  70 + shmctl(info.shmid, IPC_RMID, 0);
  71 + uint8_t* data = info.shmaddr;
  72 +
  73 + xcb_pixmap_t pix = xcb_generate_id(connection);
  74 + xcb_shm_create_pixmap(connection, pix, window, W, H, screen->root_depth, info.shmseg, 0);
  75 +
  76 +
  77 + uint8_t lala[W*H*4] = {0};
  78 + const xcb_setup_t* setup = xcb_get_setup(connection);
  79 + xcb_format_t* fmt = xcb_setup_pixmap_formats(setup);
  80 + printf("scanline_pad %u depth %u bpp %u byte_order %u\n",
  81 + fmt->scanline_pad, fmt->depth, fmt->bits_per_pixel, setup->image_byte_order);
  82 + xcb_image_t* img = xcb_image_create(W,H, XCB_IMAGE_FORMAT_Z_PIXMAP,
  83 + fmt->scanline_pad, 24, 32, 0, setup->image_byte_order, XCB_IMAGE_ORDER_LSB_FIRST, lala, W*H*4, lala);
  84 +
  85 + // xcb_image_shm_get(connection, window, img, info, 0,0, XCB_IMAGE_FORMAT_Z_PIXMAP);
  86 + // xcb_image_shm_put(connection, window, img, info, 0,0, XCB_IMAGE_FORMAT_Z_PIXMAP);
  87 +
  88 +
  89 + // --------------------------------------------------------------
  90 + uint i = 0;
  91 + while(1){
  92 + data[i++] = 0xff;
  93 + xcb_copy_area(connection, pix, window, gcontext, 0, 0, 0, 0, W, H);
  94 + xcb_flush(connection);
  95 + }
  96 +
  97 + xcb_shm_detach(connection, info.shmseg);
  98 + shmdt(info.shmaddr);
  99 +
  100 + xcb_free_pixmap(connection, pix);
  101 + xcb_destroy_window(connection, window);
  102 + xcb_disconnect(connection);
  103 +}
  104 +
  105 +// vim: set ts=4 sw=4:
... ...
  1 +/**
  2 + * \file
  3 + * [: description :]
  4 + *
  5 + * \author
  6 + * Georg Hopp <georg@steffers.org>
  7 + *
  8 + * \copyright
  9 + * Copyright © 2019 Georg Hopp
  10 + *
  11 + * This program is free software: you can redistribute it and/or modify
  12 + * it under the terms of the GNU General Public License as published by
  13 + * the Free Software Foundation, either version 3 of the License, or
  14 + * (at your option) any later version.
  15 + *
  16 + * This program is distributed in the hope that it will be useful,
  17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19 + * GNU General Public License for more details.
  20 + *
  21 + * You should have received a copy of the GNU General Public License
  22 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23 + */
  24 +#include <stdlib.h>
  25 +#include <stdio.h>
  26 +#include <unistd.h>
  27 +
  28 +#include <sys/ipc.h>
  29 +#include <sys/shm.h>
  30 +
  31 +#include <xcb/xcb.h>
  32 +#include <xcb/shm.h>
  33 +#include <xcb/xcb_image.h>
  34 +
  35 +
  36 +#define WID 512
  37 +#define HEI 512
  38 +
  39 +
  40 +int main(){
  41 + xcb_connection_t* connection;
  42 + xcb_window_t window;
  43 + xcb_screen_t* screen;
  44 + xcb_gcontext_t gcontext;
  45 + xcb_generic_event_t* event;
  46 +
  47 + uint32_t value_mask;
  48 + uint32_t value_list[2];
  49 +
  50 + //connect to the X server and get screen
  51 +
  52 + connection = xcb_connect(NULL, NULL);
  53 + screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data;
  54 +
  55 + //create a window
  56 +
  57 + value_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
  58 + value_list[0] = screen->black_pixel;
  59 + value_list[1] = XCB_EVENT_MASK_EXPOSURE;
  60 +
  61 + window = xcb_generate_id(connection);
  62 +
  63 + xcb_create_window(
  64 + connection,
  65 + screen->root_depth,
  66 + window,
  67 + screen->root,
  68 + 0, 0,
  69 + WID, HEI,
  70 + 0,
  71 + XCB_WINDOW_CLASS_INPUT_OUTPUT,
  72 + screen->root_visual,
  73 + value_mask, value_list
  74 + );
  75 +
  76 + //create a graphic context
  77 +
  78 + value_mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
  79 + value_list[0] = screen->black_pixel;
  80 + value_list[1] = 0;
  81 +
  82 + gcontext = xcb_generate_id(connection);
  83 + xcb_create_gc(connection, gcontext, window, value_mask, value_list);
  84 +
  85 + //map the window onto the screen
  86 +
  87 + xcb_map_window(connection, window);
  88 + xcb_flush(connection);
  89 +
  90 +
  91 + //Shm test
  92 + xcb_shm_query_version_reply_t* reply;
  93 + xcb_shm_segment_info_t info;
  94 +
  95 + reply = xcb_shm_query_version_reply(
  96 + connection,
  97 + xcb_shm_query_version(connection),
  98 + NULL
  99 + );
  100 +
  101 + if(!reply || !reply->shared_pixmaps){
  102 + printf("Shm error...\n");
  103 + exit(0);
  104 + }
  105 +
  106 + info.shmid = shmget(IPC_PRIVATE, WID*HEI*4, IPC_CREAT | 0777);
  107 + info.shmaddr = shmat(info.shmid, 0, 0);
  108 +
  109 + info.shmseg = xcb_generate_id(connection);
  110 + xcb_shm_attach(connection, info.shmseg, info.shmid, 0);
  111 + shmctl(info.shmid, IPC_RMID, 0);
  112 +
  113 + uint32_t* data = (uint32_t*)info.shmaddr;
  114 +
  115 + xcb_pixmap_t pix = xcb_generate_id(connection);
  116 + xcb_shm_create_pixmap(
  117 + connection,
  118 + pix,
  119 + window,
  120 + WID, HEI,
  121 + screen->root_depth,
  122 + info.shmseg,
  123 + 0
  124 + );
  125 +
  126 + int i = 0;
  127 + while(1){
  128 + usleep(10000);
  129 +
  130 + data[i] = 0xFFFFFF;
  131 + i++;
  132 +
  133 + xcb_copy_area(
  134 + connection,
  135 + pix,
  136 + window,
  137 + gcontext,
  138 + 0, 0, 0, 0,
  139 + WID, HEI
  140 + );
  141 +
  142 + xcb_flush(connection);
  143 + }
  144 +
  145 + xcb_shm_detach(connection, info.shmseg);
  146 + shmdt(info.shmaddr);
  147 +
  148 + xcb_free_pixmap(connection, pix);
  149 +
  150 + xcb_destroy_window(connection, window);
  151 + xcb_disconnect(connection);
  152 +
  153 + return 0;
  154 +}
  155 +
  156 +// vim: set ts=4 sw=4:
... ...
Please register or login to post a comment