diff --git a/backend.go b/backend.go index 5d28c31..c85dd5e 100644 --- a/backend.go +++ b/backend.go @@ -1,7 +1,6 @@ package tomo import "errors" -import "git.tebibyte.media/sashakoshka/tomo/data" import "git.tebibyte.media/sashakoshka/tomo/theme" import "git.tebibyte.media/sashakoshka/tomo/config" import "git.tebibyte.media/sashakoshka/tomo/elements" @@ -24,12 +23,6 @@ type Backend interface { // and returns a struct representing it that fulfills the MainWindow // interface. NewWindow (width, height int) (window elements.MainWindow, err error) - - // Copy puts data into the clipboard. - Copy (data.Data) - - // Paste returns the data currently in the clipboard. - Paste (accept []data.Mime) (data.Data) // SetTheme sets the theme of all open windows. SetTheme (theme.Theme) diff --git a/backends/x/event.go b/backends/x/event.go index ad05e97..53552d4 100644 --- a/backends/x/event.go +++ b/backends/x/event.go @@ -235,6 +235,39 @@ func (window *window) handleMotionNotify ( } } +func (window *window) handleSelectionNotify ( + connection *xgbutil.XUtil, + event xevent.SelectionNotifyEvent, +) { + if window.selectionRequest == nil { return } + window.selectionRequest.handleSelectionNotify(connection, event) + if !window.selectionRequest.open() { window.selectionRequest = nil } +} + +func (window *window) handlePropertyNotify ( + connection *xgbutil.XUtil, + event xevent.PropertyNotifyEvent, +) { + if window.selectionRequest == nil { return } + window.selectionRequest.handlePropertyNotify(connection, event) + if !window.selectionRequest.open() { window.selectionRequest = nil } +} + +func (window *window) handleSelectionClear ( + connection *xgbutil.XUtil, + event xevent.SelectionClearEvent, +) { + window.selectionClaim = nil +} + +func (window *window) handleSelectionRequest ( + connection *xgbutil.XUtil, + event xevent.SelectionRequestEvent, +) { + if window.selectionClaim == nil { return } + window.selectionClaim.handleSelectionRequest(connection, event) +} + func (window *window) compressExpose ( firstEvent xproto.ExposeEvent, ) ( diff --git a/backends/x/selection.go b/backends/x/selection.go new file mode 100644 index 0000000..886dc2c --- /dev/null +++ b/backends/x/selection.go @@ -0,0 +1,363 @@ +package x + +import "errors" +import "strings" +import "github.com/jezek/xgb" +import "github.com/jezek/xgbutil" +import "github.com/jezek/xgb/xproto" +import "github.com/jezek/xgbutil/xprop" +import "github.com/jezek/xgbutil/xevent" +import "git.tebibyte.media/sashakoshka/tomo/data" + +const clipboardName = "CLIPBOARD" + +type selReqState int; const ( + selReqStateClosed selReqState = iota + selReqStateAwaitTargets + selReqStateAwaitValue + selReqStateAwaitChunk +) + +type selectionRequest struct { + state selReqState + window *window + source xproto.Atom + destination xproto.Atom + accept []data.Mime + incrBuffer []byte + incrMime data.Mime + callback func (data.Data, error) +} + +func (window *window) newSelectionRequest ( + source, destination xproto.Atom, + callback func (data.Data, error), + accept ...data.Mime, +) ( + request *selectionRequest, +) { + request = &selectionRequest { + source: source, + destination: destination, + window: window, + accept: accept, + callback: callback, + } + + targets, err := xprop.Atm(window.backend.connection, "TARGETS") + if err != nil { request.die(err); return } + request.convertSelection(targets, selReqStateAwaitTargets) + return +} + +func (request *selectionRequest) convertSelection ( + target xproto.Atom, switchTo selReqState, +) { + // The requestor should set the property argument to the name of a + // property that the owner can use to report the value of the selection. + // Requestors should ensure that the named property does not exist on + // the window before issuing the ConvertSelection. The exception to this + // rule is when the requestor intends to pass parameters with the + // request. Some targets may be defined such that requestors can pass + // parameters along with the request. If the requestor wishes to provide + // parameters to a request, they should be placed in the specified + // property on the requestor window before the requestor issues the + // ConvertSelection request, and this property should be named in the + // request. + err := xproto.DeletePropertyChecked ( + request.window.backend.connection.Conn(), + request.window.xWindow.Id, + request.destination).Check() + if err != nil { request.die(err); return } + + // The selection argument specifies the particular selection involved, + // and the target argument specifies the required form of the + // information. For information about the choice of suitable atoms to + // use, see section 2.6. The requestor should set the requestor argument + // to a window that it created; the owner will place the reply property + // there. The requestor should set the time argument to the timestamp on + // the event that triggered the request for the selection value. Note + // that clients should not specify CurrentTime*. + err = xproto.ConvertSelectionChecked ( + request.window.backend.connection.Conn(), + request.window.xWindow.Id, + request.source, + target, + request.destination, + // TODO: *possibly replace this zero with an actual timestamp + // received from the server. this is non-trivial as we cannot + // rely on the timestamp of the last received event, because + // there is a possibility that this method is invoked + // asynchronously from within tomo.Do(). + 0).Check() + if err != nil { request.die(err); return } + + request.state = switchTo +} + +func (request *selectionRequest) die (err error) { + request.callback(nil, err) + request.state = selReqStateClosed +} + +func (request *selectionRequest) finalize (data data.Data) { + request.callback(data, nil) + request.state = selReqStateClosed +} + +func (request *selectionRequest) open () bool { + return request.state != selReqStateClosed +} + +type confidence int; const ( + confidenceNone confidence = iota + confidencePartial + confidenceFull +) + +func targetToMime (name string) (data.Mime, 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 name { + case "ADOBE_PORTABLE_DOCUMENT_FORMAT": + return data.M("application", "pdf"), confidenceFull + case "APPLE_PICT": + return data.M("image", "pict"), confidenceFull + case + "POSTSCRIPT", + "ENCAPSULATED_POSTSCRIPT", + "ENCAPSULATED_POSTSCRIPT_INTERCHANGE": + return data.M("application", "postscript"), confidenceFull + case "FILE_NAME": + return data.MimeFile, confidenceFull + case "UTF8_STRING": + return data.MimePlain, confidenceFull + case "TEXT": + return data.MimePlain, confidencePartial + case "STRING": + return data.MimePlain, confidencePartial + default: + if strings.Count(name, "/") == 1 { + ty, subtype, _ := strings.Cut(name, "/") + return data.M(ty, subtype), confidenceFull + } else { + return data.Mime { }, confidenceNone + } + } +} + +func mimeToTargets (mime data.Mime) (names []string) { + names = append(names, mime.String()) + switch mime { + case data.M("application", "pdf"): + names = append(names, "ADOBE_PORTABLE_DOCUMENT_FORMAT") + case data.M("image", "pict"): + names = append(names, "APPLE_PICT") + case data.M("application", "postscript"): + names = append(names, "POSTSCRIPT") + case data.MimeFile: + names = append(names, "FILE_NAME") + case data.MimePlain: + names = append(names, "UTF8_STRING", "TEXT", "STRING") + } + return +} + +func (request *selectionRequest) handleSelectionNotify ( + connection *xgbutil.XUtil, + event xevent.SelectionNotifyEvent, +) { + // the only valid states that we can process a SelectionNotify event in + invalidState := + request.state != selReqStateAwaitValue && + request.state != selReqStateAwaitTargets + if invalidState { return } + + // Follow: + // https://tronche.com/gui/x/icccm/sec-2.html#s-2.4 + + // If the property argument is None, the conversion has been refused. + // This can mean either that there is no owner for the selection, that + // the owner does not support the conversion implied by the target, or + // that the server did not have sufficient space to accommodate the + // data. + if event.Property == 0 { request.die(nil); return } + + // When using GetProperty to retrieve the value of a selection, the + // property argument should be set to the corresponding value in the + // SelectionNotify event. Because the requestor has no way of knowing + // beforehand what type the selection owner will use, the type argument + // should be set to AnyPropertyType. Several GetProperty requests may be + // needed to retrieve all the data in the selection; each should set the + // long-offset argument to the amount of data received so far, and the + // size argument to some reasonable buffer size (see section 2.5). If + // the returned value of bytes-after is zero, the whole property has + // been transferred. + reply, err := xproto.GetProperty ( + connection.Conn(), false, request.window.xWindow.Id, + event.Property, xproto.GetPropertyTypeAny, + 0, (1 << 32) - 1).Reply() + if err != nil { request.die(err); return } + if reply.Format == 0 { + request.die(errors.New("x: missing selection property")) + return + } + + // https://tronche.com/gui/x/icccm/sec-2.html#s-2.7.2 + // Requestors may receive a property of type INCR9 in response to any + // target that results in selection data. This indicates that the owner + // will send the actual data incrementally. The contents of the INCR + // property will be an integer, which represents a lower bound on the + // number of bytes of data in the selection. The requestor and the + // selection owner transfer the data in the selection in the following + // manner. The selection requestor starts the transfer process by + // deleting the (type==INCR) property forming the reply to the + // selection. + incr, err := xprop.Atm(request.window.backend.connection, "INCR") + if err != nil { request.die(err); return } + if reply.Type == incr { + // reply to the INCR selection + err = xproto.DeletePropertyChecked ( + request.window.backend.connection.Conn(), + request.window.xWindow.Id, + request.destination).Check() + if err != nil { request.die(err); return } + + // await the first chunk + request.state = selReqStateAwaitChunk + return + } + + // Once all the data in the selection has been retrieved (which may + // require getting the values of several properties &emdash; see section + // 2.7), the requestor should delete the property in the SelectionNotify + // request by using a GetProperty request with the delete argument set + // to True. As previously discussed, the owner has no way of knowing + // when the data has been transferred to the requestor unless the + // property is removed. + err = xproto.DeletePropertyChecked ( + request.window.backend.connection.Conn(), + request.window.xWindow.Id, + request.destination).Check() + if err != nil { request.die(err); return } + + // depending on which state the selection request is in, do something + // different with the property's value + switch request.state { + case selReqStateAwaitValue: + // get the type from the property and convert that to the mime + // value to pass to the application. + targetName, err := xprop.AtomName ( + request.window.backend.connection, reply.Type) + if err != nil { request.die(err); return } + mime, _ := targetToMime(targetName) + + // we now have the full selection data in the property, so we + // finalize the request and are done. + request.finalize(data.Bytes(mime, reply.Value)) + + case selReqStateAwaitTargets: + // make a list of the atoms we got + buffer := reply.Value + atoms := make([]xproto.Atom, len(buffer) / 4) + for index := range atoms { + atoms[index] = xproto.Atom(xgb.Get32(buffer[index * 4:])) + } + + // choose the best match out of all targets using a confidence + // system + confidentMatchFound := false + var chosenTarget xproto.Atom + for _, atom := range atoms { + targetName, err := xprop.AtomName ( + request.window.backend.connection, atom) + if err != nil { request.die(err); return } + + mime, confidence := targetToMime(targetName) + if confidence == confidenceNone { continue } + + // if the accepted types list is nil, just choose this + // one. however, if we are not 100% confident that this + // target can be directly converted into a mime type, + // don't mark it as the final match. we still want the + // mime type we give to the application to be as + // accurate as possible. + if request.accept == nil { + chosenTarget = atom + if confidence == confidenceFull { + confidentMatchFound = true + } + } + + // run through the accepted types list if it exists, + // looking for a match. if one is found, then choose + // this target. however, if we are not 100% confident + // that this target directly corresponds to the mime + // type, don't mark it as the final match, because there + // may be a better target in the list. + for _, accept := range request.accept { + if accept == mime { + chosenTarget = atom + if confidence == confidenceFull { + confidentMatchFound = true + } + break + }} + + if confidentMatchFound { break } + } + + // if we didn't find a match, finalize the request with an empty + // data map to inform the application that, although there were + // no errors, there wasn't a suitable target to choose from. + if chosenTarget == 0 { + request.finalize(data.Data { }) + return + } + + // await the selection value + request.convertSelection(chosenTarget, selReqStateAwaitValue) + } +} + +func (request *selectionRequest) handlePropertyNotify ( + connection *xgbutil.XUtil, + event xevent.PropertyNotifyEvent, +) { + // the only valid state that we can process a PropertyNotify event in + if request.state != selReqStateAwaitChunk { return } + if event.State != xproto.PropertyNewValue { return } + + request.handleINCRProperty(event.Atom) +} + +func (request *selectionRequest) handleINCRProperty (property xproto.Atom) { + // Retrieving data using GetProperty with the delete argument True. + reply, err := xproto.GetProperty ( + request.window.backend.connection.Conn(), true, + request.window.xWindow.Id, property, xproto.GetPropertyTypeAny, + 0, (1 << 32) - 1).Reply() + if err != nil { request.die(err); return } + + if len(reply.Value) == 0 { + // a zero length property means the transfer has finished. we + // finalize the request with the data we have, and don't wait + // for more. + request.finalize(data.Bytes(request.incrMime, request.incrBuffer)) + + // we want to be extra sure we aren't holding onto this memory + request.incrBuffer = nil + } else { + // a property with content means the transfer is still ongoing. + // we append the data we got and wait for more. + request.state = selReqStateAwaitChunk + request.incrBuffer = append(request.incrBuffer, reply.Value...) + + targetName, err := xprop.AtomName ( + request.window.backend.connection, reply.Type) + if err != nil { request.die(err); return } + request.incrMime, _ = targetToMime(targetName) + } +} diff --git a/backends/x/selectionclaim.go b/backends/x/selectionclaim.go new file mode 100644 index 0000000..c5fefb7 --- /dev/null +++ b/backends/x/selectionclaim.go @@ -0,0 +1,164 @@ +package x + +import "io" +import "github.com/jezek/xgb" +import "github.com/jezek/xgbutil" +import "github.com/jezek/xgb/xproto" +import "github.com/jezek/xgbutil/xprop" +import "github.com/jezek/xgbutil/xevent" +import "git.tebibyte.media/sashakoshka/tomo/data" + +type selectionClaim struct { + window *window + data data.Data + name xproto.Atom +} + +func (window *window) claimSelection (name xproto.Atom, data data.Data) *selectionClaim { + // Follow: + // https://tronche.com/gui/x/icccm/sec-2.html#s-2.1 + + // A client wishing to acquire ownership of a particular selection + // should call SetSelectionOwner. The client should set the specified + // selection to the atom that represents the selection, set the + // specified owner to some window that the client created, and set the + // specified time to some time between the current last-change time of + // the selection concerned and the current server time. This time value + // usually will be obtained from the timestamp of the event that + // triggers the acquisition of the selection. Clients should not set the + // time value to CurrentTime, because if they do so, they have no way of + // finding when they gained ownership of the selection. Clients must use + // a window they created so that requestors can route events to the + // owner of the selection. + err := xproto.SetSelectionOwnerChecked ( + window.backend.connection.Conn(), + window.xWindow.Id, name, 0).Check() // FIXME: should not be zero + if err != nil { return nil } + + ownerReply, err := xproto.GetSelectionOwner ( + window.backend.connection.Conn(), name).Reply() + if err != nil { return nil } + if ownerReply.Owner != window.xWindow.Id { return nil } + + return &selectionClaim { + window: window, + data: data, + name: name, + } +} + +func (window *window) refuseSelectionRequest (request xevent.SelectionRequestEvent) { + // ... refuse the SelectionRequest by sending the requestor window a + // SelectionNotify event with the property set to None (by means of a + // SendEvent request with an empty event mask). + event := xproto.SelectionNotifyEvent { + Requestor: request.Requestor, + Selection: request.Selection, + Target: request.Target, + Property: 0, + }.Bytes() + xproto.SendEvent ( + window.backend.connection.Conn(), + false, request.Requestor, 0, string(event)) +} + +func (window *window) fulfillSelectionRequest ( + data []byte, + format byte, + ty xproto.Atom, + request xevent.SelectionRequestEvent, +) { + die := func () { window.refuseSelectionRequest(request) } + + // If the specified property is not None, the owner should place the + // data resulting from converting the selection into the specified + // property on the requestor window and should set the property's type + // to some appropriate value, which need not be the same as the + // specified target. + err := xproto.ChangePropertyChecked ( + window.backend.connection.Conn(), + xproto.PropModeReplace, request.Requestor, + request.Property, + ty, format, + uint32(len(data) / (int(format) / 8)), data).Check() + if err != nil { die() } + + // If the property is successfully stored, the owner should acknowledge + // the successful conversion by sending the requestor window a + // SelectionNotify event (by means of a SendEvent request with an empty + // mask). + event := xproto.SelectionNotifyEvent { + Requestor: request.Requestor, + Selection: request.Selection, + Target: request.Target, + Property: request.Property, + }.Bytes() + xproto.SendEvent ( + window.backend.connection.Conn(), + false, request.Requestor, 0, string(event)) +} + +func (claim *selectionClaim) handleSelectionRequest ( + connection *xgbutil.XUtil, + event xevent.SelectionRequestEvent, +) { + // Follow: + // https://tronche.com/gui/x/icccm/sec-2.html#s-2.2 + + die := func () { claim.window.refuseSelectionRequest(event) } + + // When a requestor wants the value of a selection, the owner receives a + // SelectionRequest event. The specified owner and selection will be the + // values that were specified in the SetSelectionOwner request. The + // owner should compare the timestamp with the period it has owned the + // selection and, if the time is outside, refuse the SelectionRequest. + if event.Selection != claim.name { die(); return } + + // If the specified property is None , the requestor is an obsolete + // client. Owners are encouraged to support these clients by using the + // specified target atom as the property name to be used for the reply. + if event.Property == 0 { + event.Property = event.Target + } + + // Otherwise, the owner should use the target to decide the form into + // which the selection should be converted. Some targets may be defined + // such that requestors can pass parameters along with the request. The + // owner will find these parameters in the property named in the + // selection request. The type, format, and contents of this property + // are dependent upon the definition of the target. If the target is not + // defined to have parameters, the owner should ignore the property if + // it is present. If the selection cannot be converted into a form based + // on the target (and parameters, if any), the owner should refuse the + // SelectionRequest as previously described. + targetName, err := xprop.AtomName ( + claim.window.backend.connection, event.Target) + if err != nil { die(); return } + + switch targetName { + case "TARGETS": + targetNames := []string { "TARGETS", } + for mime := range claim.data { + targetNames = append(targetNames, mimeToTargets(mime)...) + } + data := make([]byte, len(targetNames) * 4) + for index, name := range targetNames { + atom, err := xprop.Atm(claim.window.backend.connection, name) + if err != nil { die(); return } + xgb.Put32(data[(index) * 4:], uint32(atom)) + } + atomAtom, err := xprop.Atm(claim.window.backend.connection, "ATOM") + if err != nil { die(); return } + claim.window.fulfillSelectionRequest(data, 32, atomAtom, event) + + default: + mime, confidence := targetToMime(targetName) + if confidence == confidenceNone { die(); return } + reader, ok := claim.data[mime] + if !ok { die(); return } + reader.Seek(0, io.SeekStart) + data, err := io.ReadAll(reader) + if err != nil { die() } + claim.window.fulfillSelectionRequest(data, 8, event.Target, event) + } +} diff --git a/backends/x/window.go b/backends/x/window.go index e1edd8f..d8a273a 100644 --- a/backends/x/window.go +++ b/backends/x/window.go @@ -1,12 +1,15 @@ package x import "image" +import "errors" import "github.com/jezek/xgb/xproto" import "github.com/jezek/xgbutil/ewmh" import "github.com/jezek/xgbutil/icccm" +import "github.com/jezek/xgbutil/xprop" import "github.com/jezek/xgbutil/xevent" import "github.com/jezek/xgbutil/xwindow" import "github.com/jezek/xgbutil/xgraphics" +import "git.tebibyte.media/sashakoshka/tomo/data" import "git.tebibyte.media/sashakoshka/tomo/input" import "git.tebibyte.media/sashakoshka/tomo/theme" import "git.tebibyte.media/sashakoshka/tomo/config" @@ -30,6 +33,9 @@ type window struct { theme theme.Theme config config.Config + selectionRequest *selectionRequest + selectionClaim *selectionClaim + metrics struct { width int height int @@ -64,6 +70,7 @@ func (backend *Backend) newWindow ( err = window.xWindow.Listen ( xproto.EventMaskExposure, xproto.EventMaskStructureNotify, + xproto.EventMaskPropertyChange, xproto.EventMaskPointerMotion, xproto.EventMaskKeyPress, xproto.EventMaskKeyRelease, @@ -89,6 +96,14 @@ func (backend *Backend) newWindow ( Connect(backend.connection, window.xWindow.Id) xevent.MotionNotifyFun(window.handleMotionNotify). Connect(backend.connection, window.xWindow.Id) + xevent.SelectionNotifyFun(window.handleSelectionNotify). + Connect(backend.connection, window.xWindow.Id) + xevent.PropertyNotifyFun(window.handlePropertyNotify). + Connect(backend.connection, window.xWindow.Id) + xevent.SelectionClearFun(window.handleSelectionClear). + Connect(backend.connection, window.xWindow.Id) + xevent.SelectionRequestFun(window.handleSelectionRequest). + Connect(backend.connection, window.xWindow.Id) window.SetTheme(backend.theme) window.SetConfig(backend.config) @@ -246,7 +261,6 @@ func (window *window) setType (ty string) error { } func (window *window) setClientLeader (leader *window) error { - // FIXME: doe not fucking work hints, _ := icccm.WmHintsGet(window.backend.connection, window.xWindow.Id) if hints == nil { hints = &icccm.Hints { } @@ -275,6 +289,35 @@ func (window *window) Hide () { window.xWindow.Unmap() } +func (window *window) Copy (data data.Data) { + selectionAtom, err := xprop.Atm(window.backend.connection, clipboardName) + if err != nil { return } + window.selectionClaim = window.claimSelection(selectionAtom, data) +} + +func (window *window) Paste (callback func (data.Data, error), accept ...data.Mime) { + // Follow: + // https://tronche.com/gui/x/icccm/sec-2.html#s-2.4 + die := func (err error) { callback(nil, err) } + if window.selectionRequest != nil { + // TODO: add the request to a queue and take care of it when the + // current selection has completed + die(errors.New("there is already a selection request")) + return + } + + propertyName := "TOMO_SELECTION" + selectionAtom, err := xprop.Atm(window.backend.connection, clipboardName) + if err != nil { die(err); return } + propertyAtom, err := xprop.Atm(window.backend.connection, propertyName) + if err != nil { die(err); return } + + window.selectionRequest = window.newSelectionRequest ( + selectionAtom, propertyAtom, callback, accept...) + if !window.selectionRequest.open() { window.selectionRequest = nil } + return +} + func (window *window) Close () { if window.onClose != nil { window.onClose() } if window.modalParent != nil { diff --git a/backends/x/x.go b/backends/x/x.go index d72ce1f..8e25ac4 100644 --- a/backends/x/x.go +++ b/backends/x/x.go @@ -1,7 +1,6 @@ package x import "git.tebibyte.media/sashakoshka/tomo" -import "git.tebibyte.media/sashakoshka/tomo/data" import "git.tebibyte.media/sashakoshka/tomo/theme" import "git.tebibyte.media/sashakoshka/tomo/config" @@ -39,7 +38,7 @@ type Backend struct { func NewBackend () (output tomo.Backend, err error) { backend := &Backend { windows: map[xproto.Window] *window { }, - doChannel: make(chan func (), 0), + doChannel: make(chan func (), 32), theme: theme.Default { }, config: config.Default { }, open: true, @@ -97,22 +96,6 @@ func (backend *Backend) Do (callback func ()) { backend.doChannel <- callback } -// Copy puts data into the clipboard. This method is not yet implemented and -// will do nothing! -func (backend *Backend) Copy (data data.Data) { - backend.assert() - // TODO -} - -// Paste returns the data currently in the clipboard. This method may -// return nil. This method is not yet implemented and will do nothing! -func (backend *Backend) Paste (accept []data.Mime) (data data.Data) { - backend.assert() - // TODO - return -} - - // SetTheme sets the theme of all open windows. func (backend *Backend) SetTheme (theme theme.Theme) { backend.assert() diff --git a/data/data.go b/data/data.go index 8055711..9e99b3b 100644 --- a/data/data.go +++ b/data/data.go @@ -1,10 +1,11 @@ package data import "io" +import "bytes" // Data represents arbitrary polymorphic data that can be used for data transfer // between applications. -type Data map[Mime] io.ReadCloser +type Data map[Mime] io.ReadSeekCloser // Mime represents a MIME type. type Mime struct { @@ -15,6 +16,41 @@ type Mime struct { Type, Subtype string } +// M is shorthand for creating a MIME type. +func M (ty, subtype string) Mime { + return Mime { ty, subtype } +} + +// String returns the string representation of the MIME type. +func (mime Mime) String () string { + return mime.Type + "/" + mime.Subtype +} + var MimePlain = Mime { "text", "plain" } var MimeFile = Mime { "text", "uri-list" } + +type byteReadCloser struct { *bytes.Reader } +func (byteReadCloser) Close () error { return nil } + +// Text returns plain text Data given a string. +func Text (text string) Data { + return Bytes(MimePlain, []byte(text)) +} + +// Bytes constructs a Data given a buffer and a mime type. +func Bytes (mime Mime, buffer []byte) Data { + return Data { + mime: byteReadCloser { bytes.NewReader(buffer) }, + } +} + +// Merge combines several Datas together. If multiple Datas provide a reader for +// the same mime type, the ones further on in the list will take precedence. +func Merge (individual ...Data) (combined Data) { + for _, data := range individual { + for mime, reader := range data { + combined[mime] = reader + }} + return +} diff --git a/elements/window.go b/elements/window.go index 7b432c3..4a7a167 100644 --- a/elements/window.go +++ b/elements/window.go @@ -1,13 +1,12 @@ package elements import "image" +import "git.tebibyte.media/sashakoshka/tomo/data" // Window represents a top-level container generated by the currently running // backend. It can contain a single element. It is hidden by default, and must // be explicitly shown with the Show() method. type Window interface { - Parent - // Adopt sets the root element of the window. There can only be one of // these at one time. Adopt (Element) @@ -27,6 +26,15 @@ type Window interface { // NewModal creates a new modal dialog window. NewModal (width, height int) (window Window, err error) + + // Copy puts data into the clipboard. + Copy (data.Data) + + // Paste requests the data currently in the clipboard. When the data is + // available, the callback is called with the clipboard data. If there + // was no data matching the requested mime type found, nil is passed to + // the callback instead. + Paste (callback func (data.Data, error), accept ...data.Mime) // Show shows the window. The window starts off hidden, so this must be // called after initial setup to make sure it is visible. diff --git a/examples/clipboard/main.go b/examples/clipboard/main.go new file mode 100644 index 0000000..65bcc25 --- /dev/null +++ b/examples/clipboard/main.go @@ -0,0 +1,136 @@ +package main + +import "io" +import "image" +import _ "image/png" +import _ "image/gif" +import _ "image/jpeg" +import "git.tebibyte.media/sashakoshka/tomo" +import "git.tebibyte.media/sashakoshka/tomo/data" +import "git.tebibyte.media/sashakoshka/tomo/theme" +import "git.tebibyte.media/sashakoshka/tomo/popups" +import "git.tebibyte.media/sashakoshka/tomo/layouts/basic" +import "git.tebibyte.media/sashakoshka/tomo/elements/basic" +import _ "git.tebibyte.media/sashakoshka/tomo/backends/all" +import "git.tebibyte.media/sashakoshka/tomo/elements/containers" + +func main () { + tomo.Run(run) +} + +var validImageTypes = []data.Mime { + data.M("image", "png"), + data.M("image", "gif"), + data.M("image", "jpeg"), +} + +func run () { + window, _ := tomo.NewWindow(256, 2) + window.SetTitle("Clipboard") + + container := containers.NewContainer(basicLayouts.Vertical { true, true }) + textInput := basicElements.NewTextBox("", "") + controlRow := containers.NewContainer(basicLayouts.Horizontal { true, false }) + copyButton := basicElements.NewButton("Copy") + copyButton.SetIcon(theme.IconCopy) + pasteButton := basicElements.NewButton("Paste") + pasteButton.SetIcon(theme.IconPaste) + pasteImageButton := basicElements.NewButton("Image") + pasteImageButton.SetIcon(theme.IconPictures) + + imageClipboardCallback := func (clipboard data.Data, err error) { + if err != nil { + popups.NewDialog ( + popups.DialogKindError, + window, + "Error", + "Cannot get clipboard:\n" + err.Error()) + return + } + + var imageData io.Reader + var ok bool + for mime, reader := range clipboard { + for _, mimeCheck := range validImageTypes { + if mime == mimeCheck { + imageData = reader + ok = true + }}} + + if !ok { + popups.NewDialog ( + popups.DialogKindError, + window, + "Clipboard Empty", + "No image data in clipboard") + return + } + + img, _, err := image.Decode(imageData) + if err != nil { + popups.NewDialog ( + popups.DialogKindError, + window, + "Error", + "Cannot decode image:\n" + err.Error()) + return + } + imageWindow(img) + } + clipboardCallback := func (clipboard data.Data, err error) { + if err != nil { + popups.NewDialog ( + popups.DialogKindError, + window, + "Error", + "Cannot get clipboard:\n" + err.Error()) + return + } + + textData, ok := clipboard[data.MimePlain] + if !ok { + popups.NewDialog ( + popups.DialogKindError, + window, + "Clipboard Empty", + "No text data in clipboard") + return + } + + text, _ := io.ReadAll(textData) + textInput.SetValue(string(text)) + } + copyButton.OnClick (func () { + window.Copy(data.Text(textInput.Value())) + }) + pasteButton.OnClick (func () { + window.Paste(clipboardCallback, data.MimePlain) + }) + pasteImageButton.OnClick (func () { + window.Paste(imageClipboardCallback, validImageTypes...) + }) + + container.Adopt(textInput, true) + controlRow.Adopt(copyButton, true) + controlRow.Adopt(pasteButton, true) + controlRow.Adopt(pasteImageButton, true) + container.Adopt(controlRow, false) + window.Adopt(container) + + window.OnClose(tomo.Stop) + window.Show() +} + +func imageWindow (image image.Image) { + window, _ := tomo.NewWindow(2, 2) + window.SetTitle("Clipboard Image") + container := containers.NewContainer(basicLayouts.Vertical { true, true }) + closeButton := basicElements.NewButton("Ok") + closeButton.SetIcon(theme.IconYes) + closeButton.OnClick(window.Close) + + container.Adopt(basicElements.NewImage(image), true) + container.Adopt(closeButton, false) + window.Adopt(container) + window.Show() +} diff --git a/examples/panels/main.go b/examples/panels/main.go index a9110f7..425c807 100644 --- a/examples/panels/main.go +++ b/examples/panels/main.go @@ -13,7 +13,7 @@ func main () { } func run () { - window, _ := tomo.NewWindow(2, 2) + window, _ := tomo.NewWindow(256, 256) window.SetTitle("Main") container := containers.NewContainer(basicLayouts.Vertical { true, true }) @@ -24,9 +24,9 @@ func run () { window.Show() createPanel(window, 0) - // createPanel(window, 1) - // createPanel(window, 2) - // createPanel(window, 3) + createPanel(window, 1) + createPanel(window, 2) + createPanel(window, 3) } func createPanel (parent elements.MainWindow, id int) { diff --git a/go.mod b/go.mod index 20559ab..2363371 100644 --- a/go.mod +++ b/go.mod @@ -22,3 +22,5 @@ require ( github.com/BurntSushi/graphics-go v0.0.0-20160129215708-b43f31a4a966 // indirect github.com/jezek/xgb v1.1.0 ) + +replace github.com/jezek/xgbutil => github.com/sashakoshka/xgbutil v0.0.0-20230330215824-bebecf813e8b diff --git a/go.sum b/go.sum index 6cf2167..362be1c 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,3 @@ -git.tebibyte.media/sashakoshka/ezprof v0.0.0-20230309011813-4cd4374e3830 h1:McIAkTzD4y0tS7YprTOwRu8a8NTsMKtomQnvxnCdOmg= -git.tebibyte.media/sashakoshka/ezprof v0.0.0-20230309011813-4cd4374e3830/go.mod h1:cpXX8SAUDEvZX5m7scoyruavUhEqQ1SByfWzPFHkTbg= -git.tebibyte.media/sashakoshka/ezprof v0.0.0-20230309013013-f7ee80c8f908 h1:kFdch6JQ/gWESn/vLeKzImzf3Xx1it2MkZqBwrSnR1Q= -git.tebibyte.media/sashakoshka/ezprof v0.0.0-20230309013013-f7ee80c8f908/go.mod h1:cpXX8SAUDEvZX5m7scoyruavUhEqQ1SByfWzPFHkTbg= -git.tebibyte.media/sashakoshka/ezprof v0.0.0-20230309013201-fc0de8121523 h1:1KaoiGetWYIDQKts6yas1hW+4ObkuTm6+TkFpl6jZxg= -git.tebibyte.media/sashakoshka/ezprof v0.0.0-20230309013201-fc0de8121523/go.mod h1:cpXX8SAUDEvZX5m7scoyruavUhEqQ1SByfWzPFHkTbg= git.tebibyte.media/sashakoshka/ezprof v0.0.0-20230309044548-401cba83602b h1:vPFKR7vjN1VrMdMtpATMrKQobz/cqbPiRrA1EbtG6PM= git.tebibyte.media/sashakoshka/ezprof v0.0.0-20230309044548-401cba83602b/go.mod h1:cpXX8SAUDEvZX5m7scoyruavUhEqQ1SByfWzPFHkTbg= github.com/BurntSushi/freetype-go v0.0.0-20160129220410-b763ddbfe298 h1:1qlsVAQJXZHsaM8b6OLVo6muQUQd4CwkH/D3fnnbHXA= @@ -27,8 +21,6 @@ github.com/icza/bitio v1.0.0/go.mod h1:0jGnlLAx8MKMr9VGnn/4YrvZiprkvBelsVIbA9Jjr github.com/icza/mighty v0.0.0-20180919140131-cfd07d671de6/go.mod h1:xQig96I1VNBDIWGCdTt54nHt6EeI639SmHycLYL7FkA= github.com/jezek/xgb v1.1.0 h1:wnpxJzP1+rkbGclEkmwpVFQWpuE2PUGNUzP8SbfFobk= github.com/jezek/xgb v1.1.0/go.mod h1:nrhwO0FX/enq75I7Y7G8iN1ubpSGZEiA3v9e9GyRFlk= -github.com/jezek/xgbutil v0.0.0-20210302171758-530099784e66 h1:+wPhoJD8EH0/bXipIq8Lc2z477jfox9zkXPCJdhvHj8= -github.com/jezek/xgbutil v0.0.0-20210302171758-530099784e66/go.mod h1:KACeV+k6b+aoLTVrrurywEbu3UpqoQcQywj4qX8aQKM= github.com/jfreymuth/oggvorbis v1.0.1/go.mod h1:NqS+K+UXKje0FUYUPosyQ+XTVvjmVjps1aEZH1sumIk= github.com/jfreymuth/vorbis v1.0.0/go.mod h1:8zy3lUAm9K/rJJk223RKy6vjCZTWC61NA2QD06bfOE0= github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s= @@ -38,6 +30,8 @@ github.com/mewkiz/pkg v0.0.0-20190919212034-518ade7978e2/go.mod h1:3E2FUC/qYUfM8 github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/sashakoshka/xgbutil v0.0.0-20230330215824-bebecf813e8b h1:jkUYWTFGCavJ5I4/lkireJ9KxOxEWKLHB9SPoieIHro= +github.com/sashakoshka/xgbutil v0.0.0-20230330215824-bebecf813e8b/go.mod h1:AHecLyFNy6AN9f/+0AH/h1MI7X1+JL5bmCz4XlVZk7Y= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= diff --git a/textmanip/textmanip.go b/textmanip/textmanip.go index e86d6b7..5f2d181 100644 --- a/textmanip/textmanip.go +++ b/textmanip/textmanip.go @@ -42,6 +42,15 @@ func (dot Dot) Constrain (length int) Dot { return dot } +func (dot Dot) Width () int { + dot = dot.Canon() + return dot.End - dot.Start +} + +func (dot Dot) Slice (text []rune) []rune { + return text[dot.Start:dot.End] +} + func WordToLeft (text []rune, position int) (length int) { if position < 1 { return } if position > len(text) { position = len(text) } @@ -121,6 +130,22 @@ func Delete (text []rune, dot Dot, word bool) (result []rune, moved Dot) { } } +func Lift (text []rune, dot Dot) (result []rune, moved Dot, lifted []rune) { + dot = dot.Constrain(len(text)) + if dot.Empty() { + moved = dot + return + } + + dot = dot.Canon() + lifted = make([]rune, dot.Width()) + copy(lifted, dot.Slice(text)) + result = append(result, text[:dot.Start]...) + result = append(result, text[dot.End:]...) + moved = EmptyDot(dot.Start) + return +} + func Type (text []rune, dot Dot, character rune) (result []rune, moved Dot) { dot = dot.Constrain(len(text)) if dot.Empty() { diff --git a/theme/theme.go b/theme/theme.go index 44a45ac..fd1f00c 100644 --- a/theme/theme.go +++ b/theme/theme.go @@ -63,10 +63,12 @@ type Color int; const ( ) // Icon lists a number of cannonical icons, each with its own ID. -type Icon int; const ( - // IconNone specifies no icon. - IconNone = -1 +type Icon int +// IconNone specifies no icon. +const IconNone = -1 + +const ( // Place icons IconHome Icon = iota Icon3DObjects diff --git a/tomo.go b/tomo.go index eb36c8e..33780cb 100644 --- a/tomo.go +++ b/tomo.go @@ -4,7 +4,6 @@ import "os" import "io" import "path/filepath" import "git.tebibyte.media/sashakoshka/tomo/dirs" -import "git.tebibyte.media/sashakoshka/tomo/data" import "git.tebibyte.media/sashakoshka/tomo/theme" import "git.tebibyte.media/sashakoshka/tomo/config" import "git.tebibyte.media/sashakoshka/tomo/elements" @@ -47,19 +46,6 @@ func NewWindow (width, height int) (window elements.MainWindow, err error) { return backend.NewWindow(width, height) } -// Copy puts data into the clipboard. -func Copy (data data.Data) { - assertBackend() - backend.Copy(data) -} - -// Paste returns the data currently in the clipboard. This method may -// return nil. -func Paste (accept []data.Mime) (data.Data) { - assertBackend() - return backend.Paste(accept) -} - // SetTheme sets the theme of all open windows. func SetTheme (theme theme.Theme) { backend.SetTheme(theme)