]> git.lyx.org Git - lyx.git/blob - src/lyxserver.h
The speed patch: redraw only rows that have changed
[lyx.git] / src / lyxserver.h
1 // -*- C++ -*-
2 /**
3  * \file lyxserver.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef LYXSERVER_H
14 #define LYXSERVER_H
15
16 #include <boost/signals/trackable.hpp>
17
18 class LyXFunc;
19 class LyXServer;
20
21 /* --- i/o pipes --------------------------------------------------------- */
22
23 /** This class managed the pipes used for communicating with clients.
24  Usage: Initialize with pipe-filename-base, client class to receive
25  messages, and callback-function that will be called with the messages.
26  When you want to send, use "send()".
27  This class encapsulates all the dirty communication and thus provides
28  a clean string interface.
29  */
30 class LyXComm : public boost::signals::trackable {
31 public:
32         /** When we receive a message, we send it to a client.
33           This is one of the small things that would have been a lot
34           cleaner with a Signal/Slot thing.
35          */
36         typedef void (*ClientCallbackfct)(LyXServer *, std::string const &);
37
38         /// Construct with pipe-basename and callback to receive messages
39         LyXComm(std::string const & pip, LyXServer * cli, ClientCallbackfct ccb = 0)
40                 : pipename(pip), client(cli), clientcb(ccb) {
41                 ready = false;
42                 openConnection();
43         }
44
45         ///
46         ~LyXComm() {
47                 closeConnection();
48         }
49
50         /// clean up in emergency
51         void emergencyCleanup();
52
53         /// Send message
54         void send(std::string const &);
55
56         /// asynch ready-to-be-read notification
57         void read_ready();
58
59 private:
60         /// the filename of the in pipe
61         std::string const inPipeName() const;
62
63         /// the filename of the out pipe
64         std::string const outPipeName() const;
65
66         /// Open pipes
67         void openConnection();
68
69         /// Close pipes
70         void closeConnection();
71
72         /// start a pipe
73         int startPipe(std::string const &, bool);
74
75         /// finish a pipe
76         void endPipe(int &, std::string const &, bool);
77
78         /// This is -1 if not open
79         int infd;
80
81         /// This is -1 if not open
82         int outfd;
83
84         /// Are we up and running?
85         bool ready;
86
87         /// Base of pipename including path
88         std::string pipename;
89
90         /// The client
91         LyXServer * client;
92
93         /// The client callback function
94         ClientCallbackfct clientcb;
95 };
96
97
98 /* --- prototypes -------------------------------------------------------- */
99 ///
100 class LyXServer {
101 public:
102         // FIXME IN 0.13
103         // Hack! This should be changed in 0.13
104
105         // The lyx server should not take an argument "LyXFunc" but this is
106         // how it will be done for 0.12. In 0.13 we must write a non-gui
107         // bufferview.
108         // IMO lyxserver is atypical, and for the moment the only one, non-gui
109         // bufferview. We just have to find a way to handle situations like if
110         // lyxserver is using a buffer that is being edited with a bufferview.
111         // With a common buffer list this is not a problem, maybe. (Alejandro)
112         ///
113         LyXServer(LyXFunc * f, std::string const & pip)
114                 : numclients(0), func(f), pipes(pip, (this), callback) {}
115         ///
116         ~LyXServer();
117         ///
118         void notifyClient(std::string const &);
119
120         /// whilst crashing etc.
121         void emergencyCleanup() {
122                 pipes.emergencyCleanup();
123         }
124
125 private:
126         ///
127         static void callback(LyXServer *, std::string const & msg);
128         /// Names and number of current clients
129         enum {
130                 ///
131                 MAX_CLIENTS = 10
132         };
133         ///
134         std::string clients[MAX_CLIENTS];
135         ///
136         int numclients;
137         ///
138         LyXFunc * func;
139         ///
140         LyXComm pipes;
141 };
142
143 #endif /* _LYXSERVER_H_ */
144
145 /* === End of File: lyxserver.h ========================================== */