作者:BurntSush
项目:go-wayland-simple-sh
func (w *window) newBuffer(width, height int,
format uint32) *C.struct_wl_buffer {
stride := width * 4
size := stride * height
fd := C.os_create_anonymous_file(C.off_t(size))
if fd < 0 {
panic("Could not create buffer file.")
}
data := C.mmap(nil, C.size_t(size), C.PROT_READ|C.PROT_WRITE,
C.MAP_SHARED, C.int(fd), 0)
if *(*int)(data) == -1 {
panic("mmap failed")
C.close(fd)
return nil
}
pool := C.wl_shm_create_pool(w.display.shm,
C.int32_t(fd), C.int32_t(size))
buffer := C.wl_shm_pool_create_buffer(pool, 0,
C.int32_t(width), C.int32_t(height),
C.int32_t(stride), C.uint32_t(format))
C.wl_shm_pool_destroy(pool)
C.close(fd)
w.shmData = data
return buffer
}
作者:lamproa
项目:audio-
func (s *SystemPort) Open() error {
if s.isOpen && s.stream == nil {
return errors.New("Underlying portmidi port is already opened, " +
"but stream is not connected to this SystemPort.")
}
if s.id == -1 || s.isOpen { // Fake port or opened already, ignore.
return nil
}
var errNum C.PmError
if s.IsInputPort {
// The input / output naming LOOKS backwards, but we're opening a
// portmidi "output stream" for input Ports and vice versa.
errNum = C.Pm_OpenOutput(&(s.stream), C.PmDeviceID(s.id),
nil, C.int32_t(512), nil, nil, 0)
} else {
errNum = C.Pm_OpenInput(&(s.stream), C.PmDeviceID(s.id),
nil, C.int32_t(512), nil, nil)
}
if errNum == 0 {
s.isOpen = true
s.stop = make(chan bool, 1)
s.noteOns = make(chan Note, BufferSize)
s.noteOffs = make(chan Note, BufferSize)
s.controlChanges = make(chan ControlChange, BufferSize)
}
return makePortMidiError(errNum)
}
作者:sgichoh
项目:go-strea
func (hll *Hll) AddInt32(value int32) {
cValue := C.int32_t(value)
cSeed := C.int32_t(0)
cHashKey := C.hll_hash_int32(cValue, cSeed)
C.multiset_add(hll.ms, cHashKey)
}
作者:mikkelosca
项目:go-wl
func (g *Geometry) c() *C.struct_wlc_geometry {
return C.init_geometry(
C.int32_t(g.Origin.X),
C.int32_t(g.Origin.Y),
C.uint32_t(g.Size.W),
C.uint32_t(g.Size.H),
)
}
作者:sgichoh
项目:go-strea
func New(log2m int, regwidth int, expthresh int64, sparseon int) (*Hll, error) {
cLog2m := C.int32_t(log2m)
cRegwidth := C.int32_t(regwidth)
cExpthresh := C.int64_t(expthresh)
cSparseon := C.int32_t(sparseon)
hll := &Hll{ms: C.hll_empty4(cLog2m, cRegwidth, cExpthresh, cSparseon)}
return hll, nil
}
作者:BurntSush
项目:go-wayland-simple-sh
func redraw(data unsafe.Pointer, callback *C.struct_wl_callback,
time C.uint32_t) {
win := (*window)(data)
win.paintPixels(uint32(time))
C.wl_surface_attach(win.surface, win.buffer, 0, 0)
C.wl_surface_damage(win.surface, 0, 0,
C.int32_t(win.width), C.int32_t(win.height))
}
作者:andar
项目:portmid
// Initializes a new output stream.
func NewOutputStream(deviceId DeviceId, bufferSize int64, latency int64) (stream *Stream, err error) {
var str *C.PmStream
errCode := C.Pm_OpenOutput(
(*unsafe.Pointer)(unsafe.Pointer(&str)),
C.PmDeviceID(deviceId), nil, C.int32_t(bufferSize), nil, nil, C.int32_t(latency))
if errCode != 0 {
return nil, convertToError(errCode)
}
return &Stream{deviceId: deviceId, pmStream: str}, nil
}
作者:rawlings
项目:gofabric
func doICU(tag, caser, input string) string {
err := C.UErrorCode(0)
loc := C.CString(tag)
cm := C.ucasemap_open(loc, C.uint32_t(0), &err)
buf := make([]byte, len(input)*4)
dst := (*C.char)(unsafe.Pointer(&buf[0]))
src := C.CString(input)
cn := C.int32_t(0)
switch caser {
case "fold":
cn = C.ucasemap_utf8FoldCase(cm,
dst, C.int32_t(len(buf)),
src, C.int32_t(len(input)),
&err)
case "lower":
cn = C.ucasemap_utf8ToLower(cm,
dst, C.int32_t(len(buf)),
src, C.int32_t(len(input)),
&err)
case "upper":
cn = C.ucasemap_utf8ToUpper(cm,
dst, C.int32_t(len(buf)),
src, C.int32_t(len(input)),
&err)
case "title":
cn = C.ucasemap_utf8ToTitle(cm,
dst, C.int32_t(len(buf)),
src, C.int32_t(len(input)),
&err)
}
return string(buf[:cn])
}
作者:nuod
项目:go-nuod
func (stmt *Stmt) bind(args []driver.Value) error {
c := stmt.c
parameterCount := int(stmt.parameterCount)
if parameterCount == 0 || len(args) == 0 {
return nil
}
parameters := make([]C.struct_nuodb_value, parameterCount)
for i, v := range args {
if i >= parameterCount {
break // go1.0.3 allowed extra args; ignore
}
var vt C.enum_nuodb_value_type
var i32 C.int32_t
var i64 C.int64_t
switch v := v.(type) {
case int64:
vt = C.NUODB_TYPE_INT64
i64 = C.int64_t(v)
case float64:
vt = C.NUODB_TYPE_FLOAT64
i64 = *(*C.int64_t)(unsafe.Pointer(&v))
case bool:
vt = C.NUODB_TYPE_BOOL
if v {
i64 = 1
} else {
i64 = 0
}
case string:
vt = C.NUODB_TYPE_STRING
b := []byte(v)
args[i] = b // ensure the b is not GC'ed before the _bind
i32 = C.int32_t(len(v))
i64 = C.int64_t(uintptr(unsafe.Pointer(&b[0])))
case []byte:
vt = C.NUODB_TYPE_BYTES
i32 = C.int32_t(len(v))
i64 = C.int64_t(uintptr(unsafe.Pointer(&v[0])))
case time.Time:
vt = C.NUODB_TYPE_TIME
i32 = C.int32_t(v.Nanosecond())
i64 = C.int64_t(v.Unix()) // seconds
default:
vt = C.NUODB_TYPE_NULL
}
parameters[i].i64 = i64
parameters[i].i32 = i32
parameters[i].vt = vt
}
if C.nuodb_statement_bind(c.db, stmt.st,
(*C.struct_nuodb_value)(unsafe.Pointer(¶meters[0]))) != 0 {
return c.lastError()
}
return nil
}
作者:wi
项目:git2g
func populateCIndexEntry(source *IndexEntry, dest *C.git_index_entry) {
dest.ctime.seconds = C.int32_t(source.Ctime.seconds)
dest.ctime.nanoseconds = C.uint32_t(source.Ctime.nanoseconds)
dest.mtime.seconds = C.int32_t(source.Mtime.seconds)
dest.mtime.nanoseconds = C.uint32_t(source.Mtime.nanoseconds)
dest.mode = C.uint32_t(source.Mode)
dest.uid = C.uint32_t(source.Uid)
dest.gid = C.uint32_t(source.Gid)
dest.file_size = C.uint32_t(source.Size)
dest.id = *source.Id.toC()
dest.path = C.CString(source.Path)
}
作者:TriangleG
项目:golang.or
// Recv is called by Java in a loop and blocks until Go requests a callback
// be executed by the JVM. Then a request object is returned, along with a
// handle for the host to respond via RecvRes.
//export Recv
func Recv(in **C.uint8_t, inlen *C.size_t) (ref, code, handle C.int32_t) {
recv.Lock()
for len(recv.req) == 0 {
recv.cond.Wait()
}
req := recv.req[0]
recv.req = recv.req[1:]
seqToBuf(in, inlen, req.in)
recv.Unlock()
return C.int32_t(req.ref.Num), C.int32_t(req.code), C.int32_t(req.handle)
}
作者:rakyl
项目:portmid
// NewOutputStream initializes a new output stream.
func NewOutputStream(id DeviceID, bufferSize int64, latency int64) (stream *Stream, err error) {
var str *C.PmStream
errCode := C.Pm_OpenOutput(
(*unsafe.Pointer)(unsafe.Pointer(&str)),
C.PmDeviceID(id), nil, C.int32_t(bufferSize), nil, nil, C.int32_t(latency))
if errCode != 0 {
return nil, convertToError(errCode)
}
if info := Info(id); !info.IsOutputAvailable {
return nil, ErrOutputUnavailable
}
return &Stream{deviceID: id, pmStream: str}, nil
}
作者:rlugoj
项目:luc
func (d *DataWriterIMP) addInvertedDoc(inverter Inverter, docId int32) error {
return clownfish.TrapErr(func() {
self := (*C.lucy_DataWriter)(clownfish.Unwrap(d, "d"))
inverterCF := (*C.lucy_Inverter)(clownfish.Unwrap(inverter, "inverter"))
C.LUCY_DataWriter_Add_Inverted_Doc(self, inverterCF, C.int32_t(docId))
})
}
作者:petemoor
项目:runli
func CreateCloneParams(filename string, args []string, env *[]string, cwd *string, suid int, stdhandles StdHandles) (*CloneParams, error) {
result := &CloneParams{}
var err error
result.CommReader, result.CommWriter, err = os.Pipe()
if err != nil {
return nil, err
}
result.tls = tools.AlignedBuffer(4096, 16)
result.stack = tools.AlignedBuffer(4096, 16)
result.repr.tls = (*C.char)(unsafe.Pointer(&result.tls[0]))
result.repr.stack = (*C.char)(unsafe.Pointer(&result.stack[len(result.stack)-1]))
result.repr.commfd = C.int32_t(result.CommWriter.Fd())
result.repr.filename = C.CString(filename)
if cwd != nil {
result.repr.cwd = C.CString(*cwd)
}
if args != nil {
result.args = stringsToCchars(args)
result.repr.argv = &result.args[0]
}
if env != nil {
result.env = stringsToCchars(*env)
result.repr.envp = &result.env[0]
}
result.repr.suid = C.uint32_t(suid)
result.stdhandles = stdhandles
result.repr.stdhandles[0] = getFd(result.stdhandles.StdIn)
result.repr.stdhandles[1] = getFd(result.stdhandles.StdOut)
result.repr.stdhandles[2] = getFd(result.stdhandles.StdErr)
runtime.SetFinalizer(result, freeCloneParams)
return result, nil
}
作者:surma-dum
项目:gol
// Serializes a message to OSC wire format.
// If a unsupported type is encountered, serialization
// will be stopped.
func Serialize(m *Message) ([]byte, error) {
msg := C.lo_message_new()
for i, param := range m.Params {
switch x := param.(type) {
case int32:
C.lo_message_add_int32(msg, C.int32_t(x))
case int64:
C.lo_message_add_int64(msg, C.int64_t(x))
case float32:
C.lo_message_add_float(msg, C.float(x))
case float64:
C.lo_message_add_double(msg, C.double(x))
case string:
cstr := C.CString(x)
defer C.free(unsafe.Pointer(cstr))
C.lo_message_add_string(msg, cstr)
default:
return nil, fmt.Errorf("Parameter %d has invalid type", i)
}
}
cpath := C.CString(m.Path)
defer C.free(unsafe.Pointer(cpath))
var size int
tmpbuffer := C.lo_message_serialise(msg, cpath, unsafe.Pointer(nil), (*C.size_t)(unsafe.Pointer((&size))))
defer C.free(unsafe.Pointer(tmpbuffer))
longbuffer := C.GoBytes(tmpbuffer, C.int(size))
shortbuffer := make([]byte, size)
copy(shortbuffer, longbuffer)
return shortbuffer, nil
}
作者:272
项目:lanter
// init inits the manager and creates a goroutine to proxy the CGO calls.
// All actions related to an ALooper needs to be performed from the same
// OS thread. The goroutine proxy locks itself to an OS thread and handles the
// CGO traffic on the same thread.
func init() {
go func() {
runtime.LockOSThread()
C.GoAndroid_createManager()
for {
v := <-inout
switch s := v.in.(type) {
case enableSignal:
usecsDelay := s.delay.Nanoseconds() / 1000
code := int(C.GoAndroid_enableSensor(typeToInt(s.t), C.int32_t(usecsDelay)))
if code != 0 {
*s.err = fmt.Errorf("sensor: no default %v sensor on the device", s.t)
}
case disableSignal:
C.GoAndroid_disableSensor(typeToInt(s.t))
case readSignal:
n := readEvents(s.dst)
*s.n = n
case closeSignal:
C.GoAndroid_destroyManager()
close(v.out)
return // we don't need this goroutine anymore
}
close(v.out)
}
}()
}
作者:samue
项目:go-accelerat
// VImageRichardsonLucyDeConvolve_ARGB8888 sharpens an ARGB8888 image by undoing a previous convolution that blurred the image, such as diffraction effects in a camera lens.
func VImageRichardsonLucyDeConvolve_ARGB8888(src, dst *VImageBuffer, tempBuffer []byte, roiX, roiY int, kernel, kernel2 []int16, kernelHeight, kernelWidth, kernelHeight2, kernelWidth2, divisor, divisor2 int, backgroundColor [4]uint8, iterationCount int, flags VImageFlag) error {
var tmpBuf unsafe.Pointer
if tempBuffer != nil {
tmpBuf = unsafe.Pointer(&tempBuffer[0])
}
var kernel2Ptr *C.int16_t
if kernel2 != nil {
kernel2Ptr = (*C.int16_t)(&kernel2[0])
}
srcC := src.toC()
dstC := dst.toC()
return toError(C.vImageRichardsonLucyDeConvolve_ARGB8888(&srcC, &dstC, tmpBuf, C.vImagePixelCount(roiX),
C.vImagePixelCount(roiY), (*C.int16_t)(&kernel[0]), kernel2Ptr, C.uint32_t(kernelHeight),
C.uint32_t(kernelWidth), C.uint32_t(kernelHeight2), C.uint32_t(kernelWidth2), C.int32_t(divisor),
C.int32_t(divisor2), (*C.uint8_t)(&backgroundColor[0]), C.uint32_t(iterationCount), C.vImage_Flags(flags)))
}
作者:andreinechae
项目:mobil
// transact calls a method on an Objective-C object instance.
// It blocks until the call is complete.
//
// Code (>0) is the method id assigned by gobind.
// Code -1 is used to instruct Objective-C to decrement the ref count of
// the Objective-Co object.
func transact(ref *seq.Ref, descriptor string, code int, in *seq.Buffer) *seq.Buffer {
var (
res *C.uint8_t = nil
resLen C.size_t = 0
req *C.uint8_t = nil
reqLen C.size_t = 0
)
if len(in.Data) > 0 {
req = (*C.uint8_t)(unsafe.Pointer(&in.Data[0]))
reqLen = C.size_t(len(in.Data))
}
if debug {
fmt.Printf("transact: ref.Num = %d code = %d\n", ref.Num, code)
}
desc := cstrings.get(descriptor)
C.go_seq_recv(C.int32_t(ref.Num), desc, C.int(code), req, reqLen, &res, &resLen)
if resLen > 0 {
goSlice := (*[maxSliceLen]byte)(unsafe.Pointer(res))[:resLen]
out := new(seq.Buffer)
out.Data = make([]byte, int(resLen))
copy(out.Data, goSlice)
C.free(unsafe.Pointer(res))
// TODO: own or copy []bytes whose addresses were passed in.
return out
}
return nil
}
作者:liugangnh
项目:cockroac
func goToCKey(key MVCCKey) C.DBKey {
return C.DBKey{
key: goToCSlice(key.Key),
wall_time: C.int64_t(key.Timestamp.WallTime),
logical: C.int32_t(key.Timestamp.Logical),
}
}
作者:hirochachach
项目:edetec
func Convert(input []byte, from string, to string) ([]byte, error) {
cfrom := C.CString(from)
defer C.free(unsafe.Pointer(cfrom))
cto := C.CString(to)
defer C.free(unsafe.Pointer(cto))
src := (*C.char)(unsafe.Pointer(&input[0]))
srcLen := C.int32_t(len(input))
uErr := C.UErrorCode(C.U_ZERO_ERROR)
// get dstLen
// ignore ENOENT
dstLen, _ := C.ucnv_convert(cto, cfrom, nil, 0, src, srcLen, &uErr)
if uErr != C.U_BUFFER_OVERFLOW_ERROR {
return nil, uErrorToGoError(uErr)
}
uErr = C.UErrorCode(C.U_ZERO_ERROR)
output := make([]byte, int(dstLen))
dst := (*C.char)(unsafe.Pointer(&output[0]))
dstLen, err := C.ucnv_convert(cto, cfrom, dst, dstLen, src, srcLen, &uErr)
if err != nil {
return nil, err
}
if err = uErrorToGoError(uErr); err != nil {
return nil, err
}
return output, nil
}