File: conn.go

package info (click to toggle)
golang-github-linuxdeepin-go-x11-client 0.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,656 kB
  • sloc: python: 944; sh: 38; makefile: 17
file content (112 lines) | stat: -rw-r--r-- 1,886 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package x

import (
	"bufio"
	"errors"
	"fmt"
	"log"
	"net"
	"os"
	"sync"
	"sync/atomic"
)

var Logger *log.Logger
var debugEnabled bool

func init() {
	if os.Getenv("DEBUG_X11_CLIENT") == "1" {
		debugEnabled = true
		Logger = log.New(os.Stderr, "[x] ", log.Lshortfile)
	}
}

func logPrintln(v ...interface{}) {
	if debugEnabled {
		_ = Logger.Output(2, fmt.Sprintln(v...))
	}
}
func logPrintf(format string, v ...interface{}) {
	if debugEnabled {
		_ = Logger.Output(2, fmt.Sprintf(format, v...))
	}
}

type Conn struct {
	conn      net.Conn
	closed    int32
	bufReader *bufio.Reader

	host          string
	display       string
	DisplayNumber int
	ScreenNumber  int
	setup         *Setup

	ioMu sync.Mutex
	in   in
	out  out

	ext          ext
	ridAllocator resourceIdAllocator
	atomCache    *AtomCache
	atomCacheMu  sync.Mutex
	errorCb      func(err *Error)
}

func (c *Conn) GetSetup() *Setup {
	return c.setup
}

func (c *Conn) GetDefaultScreen() *Screen {
	return &c.setup.Roots[c.ScreenNumber]
}

func (c *Conn) isClosed() bool {
	return 1 == atomic.LoadInt32(&c.closed)
}

func (c *Conn) markClosed() {
	atomic.StoreInt32(&c.closed, 1)
}

func (c *Conn) Close() {
	if c.isClosed() {
		return
	}
	c.markClosed()
	c.conn.Close()

	c.in.eventsCond.Signal()
	go func() {
		c.ioMu.Lock()
		c.in.wakeUpAllReaders()
		c.ioMu.Unlock()
	}()
}

var errConnClosed = errors.New("conn closed")

func (c *Conn) AddEventChan(eventChan chan<- GenericEvent) {
	if c.isClosed() || eventChan == nil {
		return
	}
	c.in.addEventChan(eventChan)
}

func (c *Conn) RemoveEventChan(eventChan chan<- GenericEvent) {
	if eventChan == nil {
		return
	}
	c.in.removeEventChan(eventChan)
}

func (c *Conn) SetErrorCallback(fn func(err *Error)) {
	c.errorCb = fn
}

func (c *Conn) MakeAndAddEventChan(bufSize int) chan GenericEvent {
	ch := make(chan GenericEvent, bufSize)
	c.AddEventChan(ch)
	return ch
}