~ubuntu-branches/ubuntu/warty/dasher/warty

« back to all changes in this revision

Viewing changes to Src/Gtk2/gpesettings_store.cc

  • Committer: Bazaar Package Importer
  • Author(s): Matthew Garrett
  • Date: 2003-06-05 11:10:04 UTC
  • Revision ID: james.westby@ubuntu.com-20030605111004-kqiutbrlvs7td9ic
Tags: upstream-3.2.10
ImportĀ upstreamĀ versionĀ 3.2.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "gpesettings_store.h"
 
2
 
 
3
XSettingsClient *client;
 
4
 
 
5
static GdkFilterReturn xsettings_event_filter (GdkXEvent *xevp, GdkEvent *ev, gpointer p)
 
6
{
 
7
  if (xsettings_client_process_event (client, (XEvent *)xevp))
 
8
    return GDK_FILTER_REMOVE;
 
9
 
 
10
  return GDK_FILTER_CONTINUE;
 
11
}
 
12
 
 
13
static void notify_func (const char *name, XSettingsAction action, 
 
14
                         XSettingsSetting *setting, void *cb_data)
 
15
{
 
16
  size_t length;
 
17
  char* settingname;
 
18
  if (strncmp(name,"dasher/",7)==0) {
 
19
    length = strlen(name);
 
20
    settingname = (char *)malloc(length*sizeof(char));
 
21
    for (size_t i=0; i<length; i++) {
 
22
      settingname[i]=name[i+7];
 
23
    }
 
24
    free(settingname);
 
25
  }
 
26
}
 
27
      
 
28
static void
 
29
watch_func (Window window,
 
30
            Bool   is_start,
 
31
            long   mask,
 
32
            void  *cb_data)
 
33
{
 
34
  GdkWindow *gdkwin;
 
35
 
 
36
  gdkwin = gdk_window_lookup (window);
 
37
 
 
38
  if (is_start)
 
39
    {
 
40
      if (!gdkwin)
 
41
        gdkwin = gdk_window_foreign_new (window);
 
42
      else
 
43
        g_object_ref (gdkwin);
 
44
 
 
45
      gdk_window_add_filter (gdkwin, xsettings_event_filter, NULL);
 
46
    }
 
47
  else
 
48
    {
 
49
      g_object_unref (gdkwin);
 
50
      gdk_window_remove_filter (gdkwin, xsettings_event_filter, NULL);
 
51
    }
 
52
}
 
53
 
 
54
 
 
55
void init_xsettings()
 
56
{
 
57
  client=xsettings_client_new(GDK_DISPLAY(),DefaultScreen(GDK_DISPLAY()), notify_func, watch_func, NULL);
 
58
}
 
59
 
 
60
bool get_long_option_callback(const std::string& Key, long *value)
 
61
{
 
62
  char keypath[1024];
 
63
 
 
64
  snprintf( keypath, 1024, "dasher/%s", Key.c_str() );
 
65
 
 
66
  XSettingsSetting *setting;
 
67
 
 
68
  if (xsettings_client_get_setting (client,keypath, &setting) != XSETTINGS_SUCCESS) {
 
69
    return false;
 
70
  }
 
71
 
 
72
  if (setting->type==XSETTINGS_TYPE_INT) {
 
73
    *value=setting->data.v_int;
 
74
  }
 
75
 
 
76
  return( true );
 
77
}
 
78
 
 
79
bool get_bool_option_callback(const std::string& Key, bool *value)
 
80
{
 
81
  long tmp;
 
82
  if(get_long_option_callback(Key,&tmp)==false) {
 
83
    return false;
 
84
  }
 
85
  if (tmp) {
 
86
    *value=true;
 
87
  } else {
 
88
    *value=false;
 
89
  }
 
90
  return true;
 
91
}
 
92
 
 
93
bool get_string_option_callback(const std::string& Key, std::string *value)
 
94
{
 
95
  char keypath[1024];
 
96
 
 
97
  snprintf( keypath, 1024, "dasher/%s", Key.c_str() );
 
98
 
 
99
  XSettingsSetting *setting;
 
100
 
 
101
  if (xsettings_client_get_setting (client,keypath, &setting) != XSETTINGS_SUCCESS) {
 
102
    return false;
 
103
  }
 
104
 
 
105
  if (setting->type==XSETTINGS_TYPE_STRING) {
 
106
    *value=setting->data.v_string;
 
107
  }
 
108
 
 
109
  return( true );
 
110
}
 
111
  
 
112
void set_long_option_callback(const std::string& Key, long Value)
 
113
{
 
114
  int realvalue=Value;
 
115
  long currentvalue;
 
116
  Atom gpe_settings_update_atom = XInternAtom (GDK_DISPLAY(), "_GPE_SETTINGS_UPDATE", 0);
 
117
  Window manager = XGetSelectionOwner (GDK_DISPLAY(), gpe_settings_update_atom);
 
118
  XSettingsType type;
 
119
  size_t length, name_len;
 
120
  gchar *buffer;
 
121
  XClientMessageEvent ev;
 
122
  gboolean done = FALSE;
 
123
  Window win;
 
124
  char keypath[1024];
 
125
 
 
126
  get_long_option_callback(Key,&currentvalue);
 
127
 
 
128
  if (currentvalue==Value)
 
129
    return;
 
130
 
 
131
  snprintf( keypath, 1024, "dasher/%s", Key.c_str() );
 
132
 
 
133
  if (manager == None) {
 
134
    fprintf (stderr, "gpe-confd not running.\n");
 
135
  }
 
136
  
 
137
  win = XCreateSimpleWindow (GDK_DISPLAY(), DefaultRootWindow (GDK_DISPLAY()), 1, 1, 1, 1, 0, 0, 0);
 
138
 
 
139
  type = XSETTINGS_TYPE_INT;
 
140
  length = 4;
 
141
 
 
142
  name_len = strlen(keypath);
 
143
  // This crack rounds up to the nearest 4
 
144
  name_len = (name_len +3) & ~3;
 
145
  buffer = (gchar *)g_malloc(length + 4 + name_len);
 
146
  *buffer = type;
 
147
  buffer[1] = 0;
 
148
  buffer[2] = name_len & 0xff;
 
149
  buffer[3] = (name_len >> 8) & 0xff;
 
150
  memcpy (buffer + 4, keypath, name_len);
 
151
  *((unsigned long *)(buffer + 4 + name_len)) = realvalue;
 
152
 
 
153
  XChangeProperty (GDK_DISPLAY(), win, gpe_settings_update_atom, 
 
154
                   gpe_settings_update_atom, 8, PropModeReplace, (const unsigned char*)buffer, 
 
155
                   length + 4 + name_len);
 
156
  g_free (buffer);
 
157
  XSelectInput (GDK_DISPLAY(), win, PropertyChangeMask);
 
158
  ev.type = ClientMessage;
 
159
  ev.window = win;
 
160
  ev.message_type = gpe_settings_update_atom;
 
161
  ev.format = 32;
 
162
  ev.data.l[0] = gpe_settings_update_atom;
 
163
  XSendEvent (GDK_DISPLAY(), manager, FALSE, 0, (XEvent *)&ev);
 
164
 
 
165
  while (! done)
 
166
    {
 
167
      XEvent ev;
 
168
      XNextEvent (GDK_DISPLAY(), &ev);
 
169
      switch (ev.xany.type)
 
170
        {
 
171
        case PropertyNotify:
 
172
          if (ev.xproperty.window == win
 
173
              && ev.xproperty.atom == gpe_settings_update_atom)
 
174
            done = TRUE;
 
175
          break;
 
176
        }
 
177
    }
 
178
}
 
179
 
 
180
void set_bool_option_callback(const std::string& Key, bool Value)
 
181
{
 
182
  set_long_option_callback(Key, long(Value));
 
183
}
 
184
 
 
185
void set_string_option_callback(const std::string& Key, const std::string& Value)
 
186
{
 
187
  Atom gpe_settings_update_atom = XInternAtom (GDK_DISPLAY(), "_GPE_SETTINGS_UPDATE", 0);
 
188
  Window manager = XGetSelectionOwner (GDK_DISPLAY(), gpe_settings_update_atom);
 
189
  XSettingsType type;
 
190
  std::string currentvalue;
 
191
  size_t length, name_len;
 
192
  gchar *buffer;
 
193
  XClientMessageEvent ev;
 
194
  gboolean done = FALSE;
 
195
  Window win;
 
196
  char keypath[1024];
 
197
 
 
198
  get_string_option_callback(Key,&currentvalue);
 
199
  if (currentvalue==Value) 
 
200
    return;
 
201
 
 
202
  snprintf( keypath, 1024, "dasher/%s", Key.c_str() );
 
203
 
 
204
  if (manager == None) {
 
205
    fprintf (stderr, "gpe-confd not running.\n");
 
206
  }
 
207
  
 
208
  win = XCreateSimpleWindow (GDK_DISPLAY(), DefaultRootWindow (GDK_DISPLAY()), 1, 1, 1, 1, 0, 0, 0);
 
209
 
 
210
  type = XSETTINGS_TYPE_STRING;
 
211
  length = 4+(strlen(Value.c_str()) +3) & ~3;
 
212
 
 
213
  name_len = strlen(keypath);
 
214
  // As does this crack
 
215
  name_len = (name_len+3) & ~3;
 
216
  buffer = (gchar *)g_malloc(length + 4 + name_len);
 
217
  *buffer = type;
 
218
  buffer[1] = 0;
 
219
  buffer[2] = name_len & 0xff;
 
220
  buffer[3] = (name_len >> 8) & 0xff;
 
221
  memcpy (buffer + 4, keypath, name_len);
 
222
  *((unsigned long *)(buffer + 4 + name_len)) = strlen(Value.c_str());
 
223
  memcpy (buffer + 8 + name_len, Value.c_str(), strlen (Value.c_str()));
 
224
  XChangeProperty (GDK_DISPLAY(), win, gpe_settings_update_atom, 
 
225
                   gpe_settings_update_atom, 8, PropModeReplace, (const unsigned char*)buffer, 
 
226
                   length + 4 + name_len);
 
227
  g_free (buffer);
 
228
  XSelectInput (GDK_DISPLAY(), win, PropertyChangeMask);
 
229
  ev.type = ClientMessage;
 
230
  ev.window = win;
 
231
  ev.message_type = gpe_settings_update_atom;
 
232
  ev.format = 32;
 
233
  ev.data.l[0] = gpe_settings_update_atom;
 
234
  XSendEvent (GDK_DISPLAY(), manager, FALSE, 0, (XEvent *)&ev);
 
235
  while (! done)
 
236
    {
 
237
      XEvent ev;                                                
 
238
      XNextEvent (GDK_DISPLAY(), &ev);
 
239
 
 
240
      switch (ev.xany.type)
 
241
        {
 
242
        case PropertyNotify:
 
243
          if (ev.xproperty.window == win
 
244
              && ev.xproperty.atom == gpe_settings_update_atom)
 
245
            done = TRUE;
 
246
          break;
 
247
        }
 
248
    }
 
249
}
 
250
 
 
251
 
 
252