package xgbsel import "io" import "strings" // Data represents X selection data. type Data interface { // Convert converts the data to the specified target and returns it. If // the target is not supported, this behavior will return false for ok. Convert (Target) (reader io.ReadSeekCloser, ok bool) // Supported returns a slice of targets that Convert can accept. This // can just be the result of MimeToTargets. Supported () []Target } // Target represents an X selection target. It defines the type of data stored // within an X selection. This data may be a MIME type, or a more specific name // that is unique to X. A list of these names can be found here: // https://tronche.com/gui/x/icccm/sec-2.html#s-2.6.2 type Target string // Confidence represents how accurate a conversion from a target to a MIME type // is. type Confidence int; const ( ConfidenceNone Confidence = iota ConfidencePartial ConfidenceFull ) // ToMime converts the specified target to a MIME type. Because a single MIME // type may correspond to several targets, a confidence value is returned // representing how one-to-one of a match it is. If some data is represented by // multiple targets, they can each be checked individually and the one with the // highest confidence value can be chosen. If a target cannot be converted to a // MIME type, ("", ConfidenceNone) is returned. func (target Target) ToMime () (string, Confidence) { // TODO: add other stuff. reference this table: // https://tronche.com/gui/x/icccm/sec-2.html#s-2.6.2 // perhaps we should also have parameters for mime types so we can // return an encoding here for things like STRING? switch target { case "ADOBE_PORTABLE_DOCUMENT_FORMAT": return "application/pdf", ConfidenceFull case "APPLE_PICT": return "image/pict", ConfidenceFull case "POSTSCRIPT", "ENCAPSULATED_POSTSCRIPT", "ENCAPSULATED_POSTSCRIPT_INTERCHANGE": return "application/postscript", ConfidenceFull case "FILE_NAME": return "text/uri-list", ConfidenceFull case "UTF8_STRING": return "text/plain", ConfidenceFull case "TEXT": return "text/plain", ConfidencePartial case "STRING": return "text/plain", ConfidencePartial default: if strings.Count(string(target), "/") == 1 { return string(target), ConfidenceFull } else { return "", ConfidenceNone } } } // MimeToTargets returns a slice of targets that correspond to a specified MIME // type. The MIME type itself is always the first item of the slice. All targets // returned by this function are guaranteed to convert to the given MIME type // when ToMime is called on them. func MimeToTargets (mime string) []Target { targets := []Target { Target(mime) } switch mime { case "application/pdf": targets = append(targets, "ADOBE_PORTABLE_DOCUMENT_FORMAT") case "image/pict": targets = append(targets, "APPLE_PICT") case "application/postscript": targets = append(targets, "POSTSCRIPT") case "text/uri-list": targets = append(targets, "FILE_NAME") case "text/plain": targets = append(targets, "UTF8_STRING", "TEXT", "STRING") } return targets }