summaryrefslogtreecommitdiff
path: root/src/uislave/messages.c
blob: 1fb1df472dc55c06ec8df0c476e6164927ba19d3 (plain)
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
/* Metacity UI Slave Messages */

/* 
 * Copyright (C) 2001 Havoc Pennington
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#include "messages.h"
#include "main.h"

#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <config.h>

typedef enum
{
  READ_FAILED = 0, /* FALSE */
  READ_OK,
  READ_EOF
} ReadResult;

static ReadResult read_data       (GString       *str,
                                   gint           fd);

static void send_message (MetaMessage *message);

#define META_MESSAGE_LENGTH(real_type) \
   (G_STRUCT_OFFSET (real_type, footer) + sizeof (MetaMessageFooter))

void
meta_message_send_check (void)
{
  MetaMessageCheck check;

  memset (&check, 0, META_MESSAGE_LENGTH (MetaMessageCheck));
  check.header.message_code = MetaMessageCheckCode;
  check.header.length = META_MESSAGE_LENGTH (MetaMessageCheck);
  strcpy (check.metacity_version, VERSION);
  strcpy (check.host_alias, HOST_ALIAS);
  check.metacity_version[META_MESSAGE_MAX_VERSION_LEN] = '\0';
  check.host_alias[META_MESSAGE_MAX_HOST_ALIAS_LEN] = '\0';
  check.messages_version = META_MESSAGES_VERSION;

  send_message ((MetaMessage*)&check);
}

static int
write_bytes (void *buf, int bytes)
{
  const char *p;
  int left;

  left = bytes;
  p = (char*) buf;
  while (left > 0)
    {
      int written;

      written = write (1, p, left);

      if (written < 0)
        return -1;

      left -= written;
      p += written;
    }

  g_assert (p == ((char*)buf) + bytes); 
  
  return 0;
}

#if 0
static void
print_mem (int fd, void *mem, int len)
{
  char *p = (char*) mem;
  int i;
  char unknown = 'Z';
  char null = 'n';
  
  i = 0;
  while (i < len)
    {
      if (p[i] == '\0')
        write (fd, &null, 1);
      else if (isascii (p[i]))
        write (fd, p + i, 1);
      else
        write (fd, &unknown, 1);
      
      ++i;
    }
}
#endif

static void
send_message (MetaMessage *message)
{
  static int serial = 0;
  MetaMessageFooter *footer;
  
  message->header.serial = serial;
  footer = META_MESSAGE_FOOTER (message);
  
  footer->checksum = META_MESSAGE_CHECKSUM (message);
  ++serial;

#if 0
  meta_ui_warning ("'");
  print_mem (2, message, message->header.length);
  meta_ui_warning ("'\n");
#endif
  
  if (write_bytes (META_MESSAGE_ESCAPE, META_MESSAGE_ESCAPE_LEN) < 0)
    meta_ui_warning ("Failed to write escape sequence: %s\n",
                     g_strerror (errno));
  if (write_bytes (message, message->header.length) < 0)
    meta_ui_warning ("Failed to write message: %s\n",
                     g_strerror (errno));
}

static ReadResult
read_data (GString *str,
           gint     fd)
{
  gint bytes;
  gchar buf[4096];

 again:
  
  bytes = read (fd, &buf, 4096);

  if (bytes == 0)
    return READ_EOF;
  else if (bytes > 0)
    {
      g_string_append_len (str, buf, bytes);
      return READ_OK;
    }
  else if (bytes < 0 && errno == EINTR)
    goto again;
  else if (bytes < 0)
    {
      meta_ui_warning (_("Failed to read data from window manager (%s)\n"),
                       g_strerror (errno));
      
      return READ_FAILED;
    }
  else
    return READ_OK;
}