File indexing completed on 2022-09-27 13:43:49

0001 /********************************************************************
0002  KolorServer - color server based on the X Color Management Specification
0003  This file is part of the KDE project.
0004 
0005 Copyright (C) 2012 Casian Andrei <skeletk13@gmail.com>
0006 
0007 Redistribution and use in source and binary forms, with or without
0008 modification, are permitted provided that the following conditions
0009 are met:
0010 
0011 1. Redistributions of source code must retain the above copyright
0012    notice, this list of conditions and the following disclaimer.
0013 2. Redistributions in binary form must reproduce the above copyright
0014    notice, this list of conditions and the following disclaimer in the
0015    documentation and/or other materials provided with the distribution.
0016 
0017 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0018 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0019 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0020 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0021 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0022 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0023 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0024 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0025 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0026 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0027 *********************************************************************/
0028 
0029 #ifndef X11_HELPERS_H_
0030 #define X11_HELPERS_H_
0031 
0032 #include <stdint.h> /* uint32_t */
0033 #include <arpa/inet.h> /* ntohl() */
0034 
0035 namespace X11 {
0036 #include <X11/Xlib.h>
0037 #include <X11/Xatom.h>
0038 #include <X11/Xdefs.h>
0039 
0040 #include <X11/extensions/Xfixes.h>
0041 #include <X11/extensions/Xrandr.h>
0042 
0043 #include <X11/Xcm/Xcm.h>
0044 #include <X11/Xcm/XcmEvents.h>
0045 
0046 } // X11 namespace
0047 
0048 using X11::Atom;
0049 using X11::XcolorProfile;
0050 
0051 
0052 /*
0053  * Helper functions
0054  */
0055 
0056 namespace X11 {
0057 
0058 /**
0059  * Generic function to fetch a window property.
0060  */
0061 static inline void *fetchProperty(
0062     Display *dpy,
0063     Window w,
0064     Atom prop,
0065     Atom type,
0066     unsigned long *n,
0067     Bool del)
0068 {
0069     Atom actual;
0070     int format;
0071     unsigned long left;
0072     unsigned char *data;
0073 
0074     XFlush(dpy);
0075 
0076     int result = XGetWindowProperty(dpy, w, prop, 0, ~0, del, type, &actual, &format, n, &left, &data);
0077     kDebug() << "looking for " << X11::XGetAtomName( dpy,prop ) << " found:" << *n;
0078     if (result == Success)
0079         return (void *) data;
0080 
0081     return NULL;
0082 }
0083 
0084 /**
0085  * Generic function to change a root window property
0086  */
0087 static inline void changeProperty(
0088     Display *display,
0089     Atom target_atom,
0090     int type,
0091     const unsigned char *data,
0092     unsigned long size)
0093 {
0094     XChangeProperty(display, RootWindow(display, 0),
0095         target_atom, type, 8, PropModeReplace,
0096         data, size);
0097     kDebug() << "Change " << X11::XGetAtomName( display,target_atom ) << " size:" << size;
0098 }
0099 
0100 /**
0101  * Generic function to remove a property
0102  */
0103 static inline void deleteProperty(
0104     Display *display,
0105     Atom     atom )
0106 {
0107     XDeleteProperty(display, RootWindow(display, 0), atom);
0108     kDebug() << "Remove " << X11::XGetAtomName( display,atom );
0109 }
0110 
0111 static inline Window rootWindow(Display *display, int screen)
0112 {
0113     return RootWindow(display, screen);
0114 }
0115 
0116 static inline int defaultScreen(Display *display)
0117 {
0118     return DefaultScreen(display);
0119 }
0120 
0121 static inline void setupXRandR(Display *display, int screen)
0122 {
0123     XRRSelectInput(display, RootWindow(display, screen), RROutputPropertyNotifyMask);
0124     /*
0125      * TODO other notify masks? RROutputChangeMask?
0126      * http://cgit.freedesktop.org/xorg/proto/randrproto/tree/randrproto.txt
0127      */
0128 }
0129 
0130 // TODO FIXME? shouldn't these be exported by XColor in it's headers?
0131 /* code from Tomas Carnecky */
0132 static inline XcolorProfile *XcolorProfileNext(XcolorProfile *profile)
0133 {
0134   unsigned char *ptr = (unsigned char *) profile;
0135   return (XcolorProfile *) (ptr + sizeof(XcolorProfile) + ntohl(profile->length));
0136 }
0137 
0138 static inline unsigned long XcolorProfileCount(void *data, unsigned long nBytes)
0139 {
0140   unsigned long count = 0;
0141   XcolorProfile * ptr;
0142 
0143   for (ptr = (XcolorProfile*)data;
0144        (uintptr_t)ptr < (uintptr_t)data + nBytes;
0145        ptr = XcolorProfileNext(ptr))
0146     ++count;
0147 
0148   return count;
0149 }
0150 /* end of code from Tomas Carnecky */
0151 
0152 } // X11 namespace
0153 
0154 #endif // X11_HELPERS_H_