// Convert an RGB color value into a pixel using this colormap. // This assumes that standard colors have already been expanded. internal XPixel RGBToPixel(Color color) { try { // Acquire the display lock. IntPtr display = dpy.Lock(); // Do we already have a cached pixel value for this color? Object cached = cachedPixels[color]; if (cached != null) { return((XPixel)(cached)); } // Try to allocate the color from the X display server. XColor xcolor; int red = color.Red; int green = color.Green; int blue = color.Blue; xcolor.pixel = XPixel.Zero; xcolor.red = (ushort)(red << 8); xcolor.green = (ushort)(green << 8); xcolor.blue = (ushort)(blue << 8); xcolor.flags = (sbyte)(XColor.DoRed | XColor.DoGreen | XColor.DoBlue); xcolor.pad = (sbyte)0; if (Xlib.XAllocColor(display, colormap, ref xcolor) != 0) { // Cache the value for next time. cachedPixels[color] = xcolor.pixel; return(xcolor.pixel); } // TODO: do a closest color match for the color. // Last ditch: return black or white depending upon // the intensity of the color. if (((color.Red * 54 + color.Green * 183 + color.Blue * 19) / 256) < 128) { return(Xlib.XBlackPixelOfScreen(screen.screen)); } else { return(Xlib.XWhitePixelOfScreen(screen.screen)); } } finally { dpy.Unlock(); } }
// Create a new application group for embedding a child application. private unsafe void CreateApplicationGroup() { String displayName; Xauth *auth; Xauth *authReturn; XSecurityAuthorizationAttributes xsa; Xlib.XSecurityAuthorization xs; // Check that we can create application groups. if (!CanEmbed(dpy, true, out displayName)) { return; } try { // Lock down the display while we do this. IntPtr display = dpy.Lock(); // Create the application group identifier. if (Xlib.XagCreateEmbeddedApplicationGroup (display, XVisualID.Zero, Xlib.XDefaultColormapOfScreen(screen.screen), Xlib.XBlackPixelOfScreen(screen.screen), Xlib.XWhitePixelOfScreen(screen.screen), out group) == XStatus.Zero) { return; } // Generate an authentication token for the group. auth = Xlib.XSecurityAllocXauth(); if (auth == null) { return; } auth->name = Marshal.StringToHGlobalAnsi ("MIT-MAGIC-COOKIE-1"); auth->name_length = 18; xsa = new XSecurityAuthorizationAttributes(); xsa.timeout = 300; xsa.trust_level = 0; // XSecurityClientTrusted xsa.group = (XID)group; xsa.event_mask = 0; authReturn = Xlib.XSecurityGenerateAuthorization (display, auth, (uint)(XSecurityAttributeMask.XSecurityTimeout | XSecurityAttributeMask.XSecurityTrustLevel | XSecurityAttributeMask.XSecurityGroup), ref xsa, out xs); if (authReturn == null) { Xlib.XSecurityFreeXauth(auth); return; } // Write the credentials to a temporary X authority file. String authFile = Path.GetTempFileName(); FileStream stream = new FileStream (authFile, FileMode.Create, FileAccess.Write); WriteShort(stream, 65535); // family = FamilyWild WriteShort(stream, 0); // address_length WriteShort(stream, 0); // number_length WriteShort(stream, authReturn->name_length); WriteBytes(stream, authReturn->name, authReturn->name_length); WriteShort(stream, authReturn->data_length); WriteBytes(stream, authReturn->data, authReturn->data_length); stream.Close(); // Free the Xauth structures that we don't need any more. Xlib.XSecurityFreeXauth(auth); Xlib.XSecurityFreeXauth(authReturn); // Record the app group information. redirectDisplay = displayName; authorityFile = authFile; // Create a wrapper around the appgroup to get events. groupWrapper = new AppGroupWidget(dpy, screen, group, this); } catch (MissingMethodException) { return; } catch (DllNotFoundException) { return; } catch (EntryPointNotFoundException) { return; } finally { dpy.Unlock(); } }