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
|
/* SLiM - Simple Login Manager
* Copyright (C) 2011 David Hauweele
*
* 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.
*/
#include <cstdio>
#include <iostream>
#include <ck-connector.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <stdarg.h>
#include "Ck.h"
namespace Ck
{
Exception::Exception(const std::string &func, const std::string &errstr)
: func(func), errstr(errstr)
{
}
dbus_bool_t Session::ck_connector_open_graphic_session(
const std::string &display,
uid_t uid)
{
dbus_bool_t local = true;
const char *session_type = "x11";
const char *x11_display = display.c_str();
const char *x11_device = get_x11_device(display);
const char *remote_host = "";
const char *display_dev = "";
return ck_connector_open_session_with_parameters(ckc, &error,
"unix-user", &uid,
"session-type", &session_type,
"x11-display", &x11_display,
"x11-display-device", &x11_device,
"display-device", &display_dev,
"remote-host-name", &remote_host,
"is-local", &local,
NULL);
}
const char * Session::get_x11_device(const std::string &display)
{
static char device[32];
Display *xdisplay = XOpenDisplay(display.c_str());
if(!xdisplay)
throw Exception(__func__, "cannot open display");
Window root;
Atom xfree86_vt_atom;
Atom return_type_atom;
int return_format;
unsigned long return_count;
unsigned long bytes_left;
unsigned char *return_value;
long vt;
xfree86_vt_atom = XInternAtom(xdisplay, "XFree86_VT", true);
if(xfree86_vt_atom == None)
throw Exception(__func__, "cannot get XFree86_VT");
root = DefaultRootWindow(xdisplay);
if(XGetWindowProperty(xdisplay, root, xfree86_vt_atom,
0L, 1L, false, XA_INTEGER,
&return_type_atom, &return_format,
&return_count, &bytes_left,
&return_value) != Success)
throw Exception(__func__, "cannot get root window property");
if(return_type_atom != XA_INTEGER)
throw Exception(__func__, "bad atom type");
if(return_format != 32)
throw Exception(__func__, "invalid return format");
if(return_count != 1)
throw Exception(__func__, "invalid count");
if(bytes_left != 0)
throw Exception(__func__, "invalid bytes left");
vt = *((long *)return_value);
std::sprintf(device, "/dev/tty%ld", vt);
if(return_value)
XFree(return_value);
return device;
}
void Session::open_session(const std::string &display, uid_t uid)
{
ckc = ck_connector_new();
if(!ckc)
throw Exception(__func__, "error setting up connection to ConsoleKit");
if (!ck_connector_open_graphic_session(display, uid)) {
if(dbus_error_is_set(&error))
throw Exception(__func__, error.message);
else
throw Exception(__func__, "cannot open ConsoleKit session: OOM,"
" DBus system bus not available or insufficient"
" privileges");
}
}
const char * Session::get_xdg_session_cookie()
{
return ck_connector_get_cookie(ckc);
}
void Session::close_session()
{
if(!ck_connector_close_session(ckc, &error)) {
if(dbus_error_is_set(&error))
throw Exception(__func__, error.message);
else
throw Exception(__func__, "cannot close ConsoleKit session: OOM,"
" DBus system bus not available or insufficient"
" privileges");
}
}
Session::Session()
{
dbus_error_init(&error);
}
Session::~Session()
{
dbus_error_free(&error);
}
}
std::ostream& operator<<( std::ostream& os, const Ck::Exception& e)
{
os << e.func << ": " << e.errstr;
return os;
}
|