File: gksserver.cxx

package info (click to toggle)
gr-framework 0.73.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 11,600 kB
  • sloc: ansic: 87,413; cpp: 45,348; objc: 3,057; javascript: 2,647; makefile: 1,129; python: 1,000; sh: 991; yacc: 855; pascal: 554; fortran: 228
file content (305 lines) | stat: -rw-r--r-- 9,731 bytes parent folder | download
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include <stdio.h>
#include <sstream>

#include <iostream>

#include <QApplication>
#include <QtNetwork>
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <QScreen>
#else
#include <QDesktopWidget>
#endif

#include "gkscore.h"

#include "gksserver.h"

#define DEFAULT_PORT 8410

const int GKSConnection::window_shift = 30;
unsigned int GKSConnection::index = 0;
unsigned int GKSServer::port = DEFAULT_PORT;


#if QT_VERSION < QT_VERSION_CHECK(5, 3, 0)
inline QRect operator-(const QRect &lhs, const QMargins &rhs)
{
  return QRect(QPoint(lhs.left() + rhs.left(), lhs.top() + rhs.top()),
               QPoint(lhs.right() - rhs.right(), lhs.bottom() - rhs.bottom()));
}

inline QRect &operator-=(QRect &rect, const QMargins &margins)
{
  rect = rect - margins;
  return rect;
}
#endif


GKSConnection::GKSConnection(QTcpSocket *socket)
    : socket(socket), widget(NULL), dl(NULL), dl_size(0), socket_function(SocketFunction::unknown)
{
  ++index;
  connect(socket, SIGNAL(readyRead()), this, SLOT(readClient()));
  connect(socket, SIGNAL(disconnected()), this, SLOT(disconnectedSocket()));
  // send information about workstation back to client
  struct
  {
    int nbytes;
    double mwidth;
    double mheight;
    int width;
    int height;
    char name[6];
  } workstation_information = {sizeof(workstation_information), 0, 0, 0, 0, "gksqt"};
  GKSWidget::inqdspsize(&workstation_information.mwidth, &workstation_information.mheight,
                        &workstation_information.width, &workstation_information.height);
  socket->write(reinterpret_cast<const char *>(&workstation_information), workstation_information.nbytes);
}

GKSConnection::~GKSConnection()
{
  socket->close();
  delete socket;
  if (widget != NULL)
    {
      widget->close();
    }
}

void GKSConnection::readClient()
{
  while (socket->bytesAvailable() > 0 || socket_function != SocketFunction::unknown)
    {
      switch (socket_function)
        {
        case SocketFunction::unknown:
          char socket_function_byte;
          socket->read(&socket_function_byte, 1);
          socket_function = static_cast<SocketFunction::Enum>(socket_function_byte);
          break;
        case SocketFunction::create_window:
          if (widget == NULL)
            {
              newWidget();
            }
          socket_function = SocketFunction::unknown;
          break;
        case SocketFunction::draw:
          if (dl_size == 0)
            {
              if (socket->bytesAvailable() < (long)sizeof(int)) return;
              socket->read((char *)&dl_size, sizeof(int));
            }
          if (socket->bytesAvailable() < dl_size) return;
          dl = new char[dl_size + sizeof(int)];
          socket->read(dl, dl_size);
          // The data buffer must be terminated by a zero integer -> `sizeof(int)` zero bytes
          memset(dl + dl_size, 0, sizeof(int));
          if (widget == NULL)
            {
              newWidget();
            }
          emit(data(dl));
          dl_size = 0;
          socket_function = SocketFunction::unknown;
          break;
        case SocketFunction::is_alive:
          {
            char reply[1]{static_cast<char>(SocketFunction::is_alive)};
            socket->write(reply, sizeof(reply));
            socket->flush();
            socket_function = SocketFunction::unknown;
          }
          break;
        case SocketFunction::close_window:
          if (widget != NULL)
            {
              widget->close();
            }
          socket_function = SocketFunction::unknown;
          break;
        case SocketFunction::inq_ws_state:
          {
            char reply[1 + sizeof(gks_ws_state_t)];
            if (widget != NULL)
              {
                reply[0] = static_cast<char>(SocketFunction::inq_ws_state);
                double device_pixel_ratio = (
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
                    widget->devicePixelRatioF()
#elif QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
                    widget->devicePixelRatio()
#else
                    1.0
#endif
                );
                *reinterpret_cast<gks_ws_state_t *>(&reply[1]) =
                    gks_ws_state_t{widget->width(), widget->height(), device_pixel_ratio};
              }
            else
              {
                /* If no widget exists, send back default size and device pixel ratio of primary screen */
                double device_pixel_ratio = (
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
                    QGuiApplication::primaryScreen()->devicePixelRatio()
#else
                    1.0
#endif
                );
                reply[0] = static_cast<char>(SocketFunction::inq_ws_state);
                *reinterpret_cast<gks_ws_state_t *>(&reply[1]) = gks_ws_state_t{0, 0, device_pixel_ratio};
              }
            socket->write(reply, sizeof(reply));
            socket_function = SocketFunction::unknown;
          }
          break;
        case SocketFunction::sample_locator:
          {
            char reply[1 + sizeof(gks_locator_t)];
            double x, y;
            int state;
            reply[0] = static_cast<char>(SocketFunction::sample_locator);
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
            if (widget != NULL)
              {
                QPoint mouse_pos = widget->mapFromGlobal(QCursor::pos());
                x = (double)mouse_pos.x() / widget->width();
                y = 1.0 - (double)mouse_pos.y() / widget->height();
                state = QGuiApplication::mouseButtons();
              }
            else
#endif
              {
                x = y = state = 0;
              }
            *reinterpret_cast<gks_locator_t *>(&reply[1]) = gks_locator_t{x, y, state};
            socket->write(reply, sizeof(reply));
            socket_function = SocketFunction::unknown;
          }
          break;
        default:
          break;
        }
    }
}

void GKSConnection::destroyedWidget()
{
  widget = NULL;
  socket->close();
}

void GKSConnection::disconnectedSocket()
{
  if (widget != NULL)
    {
      widget->close();
      widget = NULL;
    }
  emit(close(*this));
}

void GKSConnection::updateWindowTitle(QString renderer)
{
  std::stringstream window_title_stream;
  window_title_stream << "GKS QtTerm";
  if (widget_index > 1 && !renderer.isEmpty())
    {
      window_title_stream << " (" << widget_index << ", " << renderer.toStdString() << ")";
    }
  else if (widget_index > 1)
    {
      window_title_stream << " (" << widget_index << ")";
    }
  else if (!renderer.isEmpty())
    {
      window_title_stream << " (" << renderer.toStdString() << ")";
    }
  widget->setWindowTitle(window_title_stream.str().c_str());
}

void GKSConnection::newWidget()
{
  widget = new GKSWidget();
  widget_index = index;
  updateWindowTitle();
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
  QRect screen_geometry = QGuiApplication::primaryScreen()->availableGeometry();
#else
  QDesktopWidget *desktop = QApplication::desktop();
  QRect screen_geometry = desktop->screenGeometry(desktop->primaryScreen());
#endif
  QPoint screen_center = screen_geometry.center();
  QRect valid_position_area = screen_geometry - QMargins(0, 0, widget->width(), widget->height());
  if (GKSWidget::frame_decoration_size().isValid())
    {
      valid_position_area -=
          QMargins(0, 0, GKSWidget::frame_decoration_size().width(), GKSWidget::frame_decoration_size().height());
    }
  QPoint widget_position =
      QPoint((screen_center.x() - widget->width() / 2 - valid_position_area.left() + index * window_shift) %
                     valid_position_area.width() +
                 valid_position_area.left(),
             (screen_center.y() - widget->height() / 2 - valid_position_area.top() + index * window_shift) %
                     valid_position_area.height() +
                 valid_position_area.top());
  widget->move(widget_position);
  connect(this, SIGNAL(data(char *)), widget, SLOT(interpret(char *)));

  widget->setAttribute(Qt::WA_QuitOnClose, false);
  widget->setAttribute(Qt::WA_DeleteOnClose);
  connect(widget, SIGNAL(destroyed(QObject *)), this, SLOT(destroyedWidget()));
  connect(widget, SIGNAL(rendererChanged(QString)), this, SLOT(updateWindowTitle(QString)));
}

GKSServer::GKSServer(QObject *parent) : QTcpServer(parent)
{
  QHostAddress host_address = QHostAddress::LocalHost;
  port = DEFAULT_PORT;
  QString gks_display = QProcessEnvironment::systemEnvironment().value("GKS_DISPLAY");
  if (!gks_display.isEmpty())
    {
      QStringList str = gks_display.split(':');
      if (str.size() > 1)
        {
          host_address.setAddress(str[0].size() > 0 ? str[0] : "127.0.0.1");
          port = str[1].size() > 0 ? str[1].toInt() : DEFAULT_PORT;
        }
    }
  connect(this, SIGNAL(newConnection()), this, SLOT(connectSocket()));
  if (!listen(host_address, port))
    {
      qWarning("GKSserver: Failed to listen to port %d", port);
      exit(1);
    }
}

GKSServer::~GKSServer()
{
  for (std::list<const GKSConnection *>::iterator it = connections.begin(); it != connections.end(); ++it)
    {
      delete *it;
    }
}

void GKSServer::connectSocket()
{
  QTcpSocket *socket = this->nextPendingConnection();
  GKSConnection *connection = new GKSConnection(socket);
  connect(connection, SIGNAL(close(GKSConnection &)), this, SLOT(closeConnection(GKSConnection &)));
  connect(connection, SIGNAL(requestApplicationShutdown(GKSConnection &)), this,
          SLOT(closeConnection(GKSConnection &)));
  connections.push_back(connection);
}

void GKSServer::closeConnection(GKSConnection &connection)
{
  connections.remove(&connection);
  connection.deleteLater();
  if (connections.empty())
    {
      QApplication::quit();
    }
}