File: session.c

package info (click to toggle)
sawfish 1:1.5.3-2.4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 18,160 kB
  • ctags: 1,428
  • sloc: lisp: 23,542; ansic: 15,824; sh: 10,268; makefile: 615; perl: 19
file content (287 lines) | stat: -rw-r--r-- 6,624 bytes parent folder | download | duplicates (5)
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
/* session.c -- session manager skeleton
   $Id$

   Copyright (C) 1999 John Harper <john@dcs.warwick.ac.uk>

   This file is part of sawfish.

   sawfish 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, or (at your option)
   any later version.

   sawfish 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 sawfish; see the file COPYING.   If not, write to
   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */

/* AIX requires this to be the first thing in the file.  */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifndef __GNUC__
# if HAVE_ALLOCA_H
#  include <alloca.h>
# else
#  ifdef _AIX
 #pragma alloca
#  else
#   ifndef alloca /* predefined by HP cc +Olibcalls */
   char *alloca ();
#   endif
#  endif
# endif
#endif
   
#include "sawfish.h"

#ifdef HAVE_X11_SM_SMLIB_H
# include <X11/SM/SMlib.h>
#endif

DEFSYM(sm_save_yourself, "sm-save-yourself");

#ifdef HAVE_X11_SM_SMLIB_H
static IceConn ice_conn;
static SmcConn sm_conn;
#endif

/* Lisp functions */

DEFUN("sm-set-property", Fsm_set_property, Ssm_set_property,
      (repv prop, repv value), rep_Subr2) /*
::doc:sawfish.wm.session.util#sm-set-property::
sm-set-property PROPERTY-NAME VALUE

Sets the session manager property called PROPERTY-NAME (a string) to
VALUE. VALUE may be either an integer, a string, or a list of strings.
::end:: */
{
#ifdef HAVE_X11_SM_SMLIB_H
    SmProp sm_prop, *sm_ptr = &sm_prop;
    rep_DECLARE1 (prop, rep_STRINGP);

    if (sm_conn == 0)
	return Qnil;

    sm_prop.name = rep_STR(prop);
    if (rep_INTP(value))
    {
	unsigned char val = rep_INT(value);
	sm_prop.type = SmCARD8;
	sm_prop.num_vals = 1;
	sm_prop.vals = alloca (sizeof (SmPropValue));
	sm_prop.vals->length = 1;
	sm_prop.vals->value = &val;
    }
    else if (rep_STRINGP(value))
    {
	sm_prop.type = SmARRAY8;
	sm_prop.num_vals = 1;
	sm_prop.vals = alloca (sizeof (SmPropValue));
	sm_prop.vals->length = rep_STRING_LEN(value);
	sm_prop.vals->value = rep_STR(value);
    }
    else if (rep_CONSP(value))
    {
	repv len = Flength (value), tem;
	if (len && rep_INTP(len))
	{
	    int i;
	    sm_prop.type = SmLISTofARRAY8;
	    sm_prop.num_vals = rep_INT(len);
	    sm_prop.vals = alloca (sizeof (SmPropValue) * sm_prop.num_vals);
	    tem = value;
	    for (i = 0; i < sm_prop.num_vals; i++)
	    {
		if (!rep_STRINGP(rep_CAR(tem)))
		    return rep_signal_arg_error (value, 2);
		sm_prop.vals[i].length = rep_STRING_LEN(rep_CAR(tem));
		sm_prop.vals[i].value = rep_STR(rep_CAR(tem));
		tem = rep_CDR(tem);
	    }
	}
    }
    else
	return rep_signal_arg_error (value, 2);
    SmcSetProperties (sm_conn, 1, &sm_ptr);
#endif
    return Qt;
}

DEFUN("sm-delete-property", Fsm_delete_property, Ssm_delete_property,
      (repv prop), rep_Subr1) /*
::doc:sawfish.wm.session.util#sm-delete-property::
sm-delete-property PROPERTY-NAME

Deletes the session manager property called PROPERTY-NAME (a string).
::end:: */
{
#ifdef HAVE_X11_SM_SMLIB_H
    char *name;
    rep_DECLARE1(prop, rep_STRING);

    if (sm_conn == 0)
	return Qnil;

    name = rep_STR(prop);
    SmcDeleteProperties (sm_conn, 1, &name);
#endif
    return Qt;
}

/* SM callbacks */

#ifdef HAVE_X11_SM_SMLIB_H

static bool outstanding_save_done;

static void
save_yourself_2 (SmcConn conn, SmPointer data)
{
    repv ret = rep_call_lisp0 (global_symbol_value (Qsm_save_yourself));
    SmcSaveYourselfDone (conn, (ret && ret != Qnil) ? True : False);
    outstanding_save_done = FALSE;
}

static void
save_yourself (SmcConn conn, SmPointer data, int save_type, Bool shutdown,
	       int interact_style, Bool fast)
{
    if (save_type == SmSaveLocal || save_type == SmSaveBoth)
    {
	outstanding_save_done = TRUE;
	SmcRequestSaveYourselfPhase2 (conn, &save_yourself_2, 0);
    }
    else
	SmcSaveYourselfDone (conn, True);
}

static void
die__ (void)
{
    exit_code = ec_session_died;
    Fquit ();
}

static void
die (SmcConn conn, SmPointer data)
{
    die__ ();
}

static void
save_complete (SmcConn conn, SmPointer data)
{
}

static void
shutdown_cancelled (SmcConn conn, SmPointer data)
{
    if (outstanding_save_done)
    {
	SmcSaveYourselfDone (conn, True);
	outstanding_save_done = FALSE;
    }
}

#endif /* HAVE_X11_SM_SMLIB_H */

/* ICE hooks */

#ifdef HAVE_X11_SM_SMLIB_H

static void
ICE_input_ready (int fd)
{
    if (IceProcessMessages (ice_conn, 0, 0) != IceProcessMessagesSuccess)
    {
	fputs ("ICE has broken?\n", stderr);
	die__ ();
    }
}

static void
ICE_watch_callback (IceConn ice, IcePointer client_data,
		    Bool open, IcePointer *watch_data)
{
    if (open)
	rep_register_input_fd (IceConnectionNumber(ice), ICE_input_ready);
    else
	rep_deregister_input_fd (IceConnectionNumber(ice));
}

#endif /* HAVE_X11_SM_SMLIB_H */

/* initialisation */

DEFUN("sm-connect", Fsm_connect, Ssm_connect, (repv id), rep_Subr1)
{
#ifdef HAVE_X11_SM_SMLIB_H
    SmcCallbacks call;
    char *ret_id;
    char err[256];

    IceAddConnectionWatch (ICE_watch_callback, 0);

    call.save_yourself.callback = save_yourself;
    call.die.callback = die;
    call.save_complete.callback = save_complete;
    call.shutdown_cancelled.callback = shutdown_cancelled;

    sm_conn = SmcOpenConnection (0, 0, 1, 0,
				 SmcSaveYourselfProcMask
				 | SmcDieProcMask
				 | SmcSaveCompleteProcMask
				 | SmcShutdownCancelledProcMask,
				 &call,
				 rep_STRINGP(id) ? rep_STR(id) : 0,
				 &ret_id, sizeof (err), err);
    if (sm_conn != 0)
    {
	ice_conn = SmcGetIceConnection (sm_conn);
	return rep_string_dup (ret_id);
    }
    else
    {
	return Fsignal (Qerror,
			rep_list_2 (rep_string_dup ("sm-open-connection"),
				    rep_string_dup (err)));
    }
#else
    return Qnil;
#endif
}

DEFUN("sm-disconnect", Fsm_disconnect, Ssm_disconnect, (void), rep_Subr0)
{
#ifdef HAVE_X11_SM_SMLIB_H
    if (sm_conn != 0)
	SmcCloseConnection (sm_conn, 0, 0);
    sm_conn = 0;
    ice_conn = 0;
#endif
    return Qt;
}

void
session_init (void)
{
    repv tem = rep_push_structure ("sawfish.wm.session.util");
    rep_ADD_SUBR(Ssm_set_property);
    rep_ADD_SUBR(Ssm_delete_property);
    rep_ADD_SUBR(Ssm_connect);
    rep_ADD_SUBR(Ssm_disconnect);
    rep_INTERN(sm_save_yourself);
    rep_pop_structure (tem);
}

void
session_kill (void)
{
    Fsm_disconnect ();
}