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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
/*
* Copyright © 2006 Novell, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose is hereby granted without
* fee, provided that the above copyright notice appear in all copies
* and that both that copyright notice and this permission notice
* appear in supporting documentation, and that the name of
* Novell, Inc. not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior permission.
* Novell, Inc. makes no representations about the suitability of this
* software for any purpose. It is provided "as is" without express or
* implied warranty.
*
* NOVELL, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
* NO EVENT SHALL NOVELL, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Author: David Reveman <davidr@novell.com>
*/
#include <string.h>
#include <stdlib.h>
#include <poll.h>
#define DBUS_API_SUBJECT_TO_CHANGE
#include <dbus/dbus.h>
#include <compiz.h>
#define COMPIZ_DBUS_SERVICE_NAME "org.freedesktop.compiz"
#define COMPIZ_DBUS_ACTIVATE_MEMBER_NAME "activate"
static int displayPrivateIndex;
typedef struct _DbusDisplay {
DBusConnection *connection;
CompWatchFdHandle watchFdHandle;
} DbusDisplay;
#define GET_DBUS_DISPLAY(d) \
((DbusDisplay *) (d)->privates[displayPrivateIndex].ptr)
#define DBUS_DISPLAY(d) \
DbusDisplay *dd = GET_DBUS_DISPLAY (d)
/*
* Activate can be used to trigger any existing action. Arguments
* should be a pair of { string, bool|int32|double|string }.
*
* Example (rotate to face 1):
*
* dbus-send --type=method_call --dest=org.freedesktop.compiz \
* /org/freedesktop/compiz/rotate/allscreens/rotate_to \
* org.freedesktop.compiz.activate \
* string:'root' int32:0x52 string:'face' int32:1
*/
static Bool
dbusHandleActivateMessage (DBusConnection *connection,
DBusMessage *message,
CompDisplay *d,
char **path)
{
CompScreen *s = NULL;
CompOption *option = NULL;
int nOption = 0;
if (strcmp (path[1], "allscreens"))
{
int screenNum;
if (sscanf (path[1], "screen%d", &screenNum) != 1)
return FALSE;
for (s = d->screens; s; s = s->next)
if (s->screenNum == screenNum)
break;
if (!s)
return FALSE;
}
if (strcmp (path[0], "core") == 0)
{
if (s)
option = compGetScreenOptions (s, &nOption);
else
option = compGetDisplayOptions (d, &nOption);
}
else
{
CompPlugin *p;
for (p = getPlugins (); p; p = p->next)
if (strcmp (p->vTable->name, path[0]) == 0)
break;
if (!p)
return FALSE;
if (s)
{
if (p->vTable->getScreenOptions)
option = (*p->vTable->getScreenOptions) (s, &nOption);
}
else
{
if (p->vTable->getDisplayOptions)
option = (*p->vTable->getDisplayOptions) (d, &nOption);
}
}
while (nOption--)
{
if (strcmp (option->name, path[2]) == 0)
{
CompOption *argument = NULL;
int nArgument = 0;
DBusMessageIter iter;
if (option->type != CompOptionTypeAction)
return FALSE;
if (!option->value.action.initiate)
return FALSE;
if (dbus_message_iter_init (message, &iter))
{
CompOptionValue value;
CompOptionType type = 0;
char *name;
Bool hasValue;
do
{
name = NULL;
hasValue = FALSE;
while (!name)
{
switch (dbus_message_iter_get_arg_type (&iter)) {
case DBUS_TYPE_STRING:
dbus_message_iter_get_basic (&iter, &name);
default:
break;
}
if (!dbus_message_iter_next (&iter))
break;
}
while (!hasValue)
{
double tmp;
switch (dbus_message_iter_get_arg_type (&iter)) {
case DBUS_TYPE_BOOLEAN:
hasValue = TRUE;
type = CompOptionTypeBool;
dbus_message_iter_get_basic (&iter, &value.b);
break;
case DBUS_TYPE_INT32:
hasValue = TRUE;
type = CompOptionTypeInt;
dbus_message_iter_get_basic (&iter, &value.i);
break;
case DBUS_TYPE_DOUBLE:
hasValue = TRUE;
type = CompOptionTypeFloat;
dbus_message_iter_get_basic (&iter, &tmp);
value.f = tmp;
break;
case DBUS_TYPE_STRING:
hasValue = TRUE;
type = CompOptionTypeString;
dbus_message_iter_get_basic (&iter, &value.s);
default:
break;
}
if (!dbus_message_iter_next (&iter))
break;
}
if (name && hasValue)
{
CompOption *a;
a = realloc (argument,
sizeof (CompOption) * (nArgument + 1));
if (a)
{
argument = a;
argument[nArgument].name = name;
argument[nArgument].type = type;
argument[nArgument].value = value;
nArgument++;
}
}
} while (dbus_message_iter_has_next (&iter));
}
(*option->value.action.initiate) (d,
&option->value.action,
0,
argument, nArgument);
if (argument)
free (argument);
return TRUE;
}
option++;
}
return FALSE;
}
static DBusHandlerResult
dbusHandleMessage (DBusConnection *connection,
DBusMessage *message,
void *userData)
{
CompDisplay *d = (CompDisplay *) userData;
Bool status;
char **path;
const char *service, *interface, *member;
service = dbus_message_get_destination (message);
interface = dbus_message_get_interface (message);
member = dbus_message_get_member (message);
if (!service || !interface || !member)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (!dbus_message_is_method_call (message, interface, member))
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (!dbus_message_has_destination (message, COMPIZ_DBUS_SERVICE_NAME))
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (!dbus_message_has_member (message, COMPIZ_DBUS_ACTIVATE_MEMBER_NAME))
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (!dbus_message_get_path_decomposed (message, &path))
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (!path[0] || !path[1] || !path[2] || !path[3] || !path[4] || !path[5])
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
if (strcmp (path[0], "org") ||
strcmp (path[1], "freedesktop")||
strcmp (path[2], "compiz"))
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
status = dbusHandleActivateMessage (connection, message, d, &path[3]);
dbus_free_string_array (path);
if (status)
return DBUS_HANDLER_RESULT_HANDLED;
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
static Bool
dbusProcessMessages (void *data)
{
CompDisplay *d = (CompDisplay *) data;
DBusDispatchStatus status;
DBUS_DISPLAY (d);
do
{
dbus_connection_read_write_dispatch (dd->connection, 0);
status = dbus_connection_get_dispatch_status (dd->connection);
}
while (status == DBUS_DISPATCH_DATA_REMAINS);
return TRUE;
}
static Bool
dbusInitDisplay (CompPlugin *p,
CompDisplay *d)
{
DbusDisplay *dd;
DBusError error;
dbus_bool_t status;
int fd, ret;
dd = malloc (sizeof (DbusDisplay));
if (!dd)
return FALSE;
dbus_error_init (&error);
dd->connection = dbus_bus_get (DBUS_BUS_SESSION, &error);
if (dbus_error_is_set (&error))
{
fprintf (stderr, "%s: dbus_bus_get error: %s\n",
programName, error.message);
dbus_error_free (&error);
return FALSE;
}
ret = dbus_bus_request_name (dd->connection,
COMPIZ_DBUS_SERVICE_NAME,
DBUS_NAME_FLAG_REPLACE_EXISTING |
DBUS_NAME_FLAG_ALLOW_REPLACEMENT,
&error);
if (dbus_error_is_set (&error))
{
fprintf (stderr, "%s: dbus_bus_request_name error: %s\n",
programName, error.message);
dbus_connection_close (dd->connection);
dbus_error_free (&error);
return FALSE;
}
dbus_error_free (&error);
if (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
{
fprintf (stderr, "%s: dbus_bus_request_name reply is not "
"primary owner\n", programName);
dbus_connection_close (dd->connection);
return FALSE;
}
status = dbus_connection_add_filter (dd->connection,
dbusHandleMessage,
d, NULL);
if (!status)
{
fprintf (stderr, "%s: dbus_connection_add_filter failed\n",
programName);
dbus_connection_close (dd->connection);
return FALSE;
}
status = dbus_connection_get_unix_fd (dd->connection, &fd);
if (!status)
{
fprintf (stderr, "%s: dbus_connection_get_unix_fd failed\n",
programName);
dbus_connection_close (dd->connection);
return FALSE;
}
dd->watchFdHandle = compAddWatchFd (fd,
POLLIN | POLLPRI | POLLHUP | POLLERR,
dbusProcessMessages,
d);
d->privates[displayPrivateIndex].ptr = dd;
return TRUE;
}
static void
dbusFiniDisplay (CompPlugin *p,
CompDisplay *d)
{
DBUS_DISPLAY (d);
compRemoveWatchFd (dd->watchFdHandle);
dbus_bus_release_name (dd->connection, COMPIZ_DBUS_SERVICE_NAME, NULL);
dbus_connection_close (dd->connection);
free (dd);
}
static Bool
dbusInit (CompPlugin *p)
{
displayPrivateIndex = allocateDisplayPrivateIndex ();
if (displayPrivateIndex < 0)
return FALSE;
return TRUE;
}
static void
dbusFini (CompPlugin *p)
{
if (displayPrivateIndex >= 0)
freeDisplayPrivateIndex (displayPrivateIndex);
}
static int
dbusGetVersion (CompPlugin *plugin,
int version)
{
return ABIVERSION;
}
CompPluginVTable dbusVTable = {
"dbus",
"Dbus",
"Dbus Control Backend",
dbusGetVersion,
dbusInit,
dbusFini,
dbusInitDisplay,
dbusFiniDisplay,
0, /* InitScreen */
0, /* FiniScreen */
0, /* InitWindow */
0, /* FiniWindow */
0, /* GetDisplayOptions */
0, /* SetDisplayOption */
0, /* GetScreenOptions */
0, /* SetScreenOption */
NULL,
0
};
CompPluginVTable *
getCompPluginInfo (void)
{
return &dbusVTable;
}
|