简体   繁体   中英

Check for X11 extension

I am writing a shell script that needs to differ its behavior and provide different options to called programs based on the presence or absence of particular X11 extensions. I have a working solution, but I am hoping for a cleaner solution. I am open to considering a simple c program to do the test and return the result. Here is what I have working as a minimal functional example:

#!/bin/sh
xdpyinfo |sed -nr '/^number of extensions/,/^[^ ]/s/^  *//p'  | \
    grep -q $EXTENSION && echo present

I think there is a way to simplify the sed,grep but I really would prefer not to parse xdpyinfo .

You have the C-tag, too, so let me suggest to do the xdpyinfo yourself. The following C program prints just the extensions:

#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int compare(const void *a, const void *b)
{
  return strcmp(*(char **) a, *(char **) b);
}

static void print_extension_info(Display * dpy)
{
  int n = 0, i;
  char **extlist = XListExtensions(dpy, &n);


  printf("number of extensions:    %d\n", n);
  if (extlist) {
    qsort(extlist, n, sizeof(char *), compare);
    for (i = 0; i < n; i++) {

      printf("    %s\n", extlist[i]);

    }
  }
  // TODO: it might not be a good idea to free extlist, check
}

int main()
{
  Display *dpy;
  char *displayname = NULL;

  dpy = XOpenDisplay(displayname);
  if (!dpy) {
    fprintf(stderr, "Unable to open display \"%s\".\n",
            XDisplayName(displayname));
    exit(EXIT_FAILURE);
  }

  print_extension_info(dpy);

  XCloseDisplay(dpy);
  exit(EXIT_SUCCESS);
}

Compile with eg: GCC

gcc -O3 -g3  -W -Wall -Wextra  xdpyinfo1.0.2.c  $(pkg-config --cflags --libs x11)  -o xdpyinfo1.0.2

(should give a warning about unused argc but that's harmless)

Just change the printf() 's to the format you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM