コード例 #1
0
ファイル: event.c プロジェクト: Krlos0208/Sintetizador_Voz
static gboolean get_adapter_and_device(bdaddr_t *src, bdaddr_t *dst,
					struct btd_adapter **adapter,
					struct btd_device **device,
					gboolean create)
{
	DBusConnection *conn = get_dbus_connection();
	char peer_addr[18];

	*adapter = manager_find_adapter(src);
	if (!*adapter) {
		error("Unable to find matching adapter");
		return FALSE;
	}

	ba2str(dst, peer_addr);

	if (create)
		*device = adapter_get_device(conn, *adapter, peer_addr);
	else
		*device = adapter_find_device(*adapter, peer_addr);

	if (create && !*device) {
		error("Unable to get device object!");
		return FALSE;
	}

	return TRUE;
}
コード例 #2
0
ファイル: dbus-common.c プロジェクト: writefaruq/bluez-oob
void hcid_dbus_unregister(void)
{
    DBusConnection *conn = get_dbus_connection();
    char **children;
    int i;
    uint16_t dev_id;

    if (!conn || !dbus_connection_get_is_connected(conn))
        return;

    /* Unregister all paths in Adapter path hierarchy */
    if (!dbus_connection_list_registered(conn, "/", &children))
        return;

    for (i = 0; children[i]; i++) {
        char path[MAX_PATH_LENGTH];
        struct btd_adapter *adapter;

        if (children[i][0] != 'h')
            continue;

        snprintf(path, sizeof(path), "/%s", children[i]);

        adapter = manager_find_adapter_by_path(path);
        if (!adapter)
            continue;

        dev_id = adapter_get_dev_id(adapter);
        manager_unregister_adapter(dev_id);
    }

    dbus_free_string_array(children);
}
コード例 #3
0
static int dbusoob_init(void)
{
	DBG("Setup dbusoob plugin");

	connection = get_dbus_connection();

	oob_register_cb(read_local_data_complete);

	return btd_register_adapter_driver(&oob_driver);
}
コード例 #4
0
ファイル: dbus-common.c プロジェクト: writefaruq/bluez-oob
static gboolean system_bus_reconnect(void *data)
{
    DBusConnection *conn = get_dbus_connection();
    struct hci_dev_list_req *dl = NULL;
    struct hci_dev_req *dr;
    int sk, i;
    gboolean ret_val = TRUE;

    if (conn) {
        if (dbus_connection_get_is_connected(conn))
            return FALSE;
    }

    if (hcid_dbus_init() < 0)
        return TRUE;

    /* Create and bind HCI socket */
    sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
    if (sk < 0) {
        error("Can't open HCI socket: %s (%d)",
              strerror(errno), errno);
        return TRUE;
    }

    dl = g_malloc0(HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl));

    dl->dev_num = HCI_MAX_DEV;
    dr = dl->dev_req;

    if (ioctl(sk, HCIGETDEVLIST, (void *) dl) < 0) {
        info("Can't get device list: %s (%d)",
             strerror(errno), errno);
        goto failed;
    }

    /* reset the default device */
    manager_set_default_adapter(-1);

    /* FIXME: it shouldn't be needed to register adapters again */
    for (i = 0; i < dl->dev_num; i++, dr++)
        manager_register_adapter(dr->dev_id, TRUE);

    ret_val = FALSE;

failed:
    if (sk >= 0)
        close(sk);

    g_free(dl);

    return ret_val;
}
コード例 #5
0
ファイル: dbus-common.c プロジェクト: writefaruq/bluez-oob
void hcid_dbus_exit(void)
{
    DBusConnection *conn = get_dbus_connection();

    if (!conn || !dbus_connection_get_is_connected(conn))
        return;

    manager_cleanup(conn, "/");

    set_dbus_connection(NULL);

    dbus_connection_unref(conn);
}
コード例 #6
0
ファイル: manager.c プロジェクト: morphis/samsung-modem-mgr
void manager_cleanup(struct manager *mgr)
{
	DBusConnection *conn = get_dbus_connection();

	if (mgr->state == ONLINE)
		set_powered(conn, mgr, FALSE);

	g_dbus_unregister_interface(conn, SAMSUNG_MODEM_MANAGER_PATH,
					SAMSUNG_MODEM_MANAGER_INTERFACE);

	ipc_client_free(mgr->client);
	rfs_manager_free(mgr->rfs);

	g_free(mgr);
}
コード例 #7
0
ファイル: caja-file-utilities.c プロジェクト: eyelash/caja
/**
 * caja_inhibit_power_manager:
 * @message: a human readable message for the reason why power management
 *       is being suspended.
 *
 * Inhibits the power manager from logging out or suspending the machine
 * (e.g. whenever Caja is doing file operations).
 *
 * Returns: an integer cookie, which must be passed to
 *    caja_uninhibit_power_manager() to resume
 *    normal power management.
 */
int
caja_inhibit_power_manager (const char *message)
{
    GDBusConnection *connection;
    GVariant *result;
    GError *error = NULL;
    guint cookie = 0;

    g_return_val_if_fail (message != NULL, -1);

    connection = get_dbus_connection ();

    if (connection == NULL)
    {
        return -1;
    }

    result = g_dbus_connection_call_sync (connection,
                                          GSM_NAME,
                                          GSM_PATH,
                                          GSM_INTERFACE,
                                          "Inhibit",
                                          g_variant_new ("(susu)",
                                                  "Caja",
                                                  (guint) 0,
                                                  message,
                                                  (guint) (INHIBIT_LOGOUT | INHIBIT_SUSPEND)),
                                          G_VARIANT_TYPE ("(u)"),
                                          G_DBUS_CALL_FLAGS_NO_AUTO_START,
                                          -1,
                                          NULL,
                                          &error);

    if (error != NULL)
    {
        g_warning ("Could not inhibit power management: %s", error->message);
        g_error_free (error);
        return -1;
    }

    g_variant_get (result, "(u)", &cookie);
    g_variant_unref (result);

    return (int) cookie;
}
コード例 #8
0
ファイル: manager.c プロジェクト: morphis/samsung-modem-mgr
int manager_init(struct manager *mgr)
{
	DBusConnection *conn;
	gboolean ret;

	conn = get_dbus_connection();

	ret = g_dbus_register_interface(conn, SAMSUNG_MODEM_MANAGER_PATH,
					SAMSUNG_MODEM_MANAGER_INTERFACE,
					manager_methods, manager_signals,
					NULL, mgr, NULL);

	if (ret == FALSE)
		return -1;

	g_debug("Initialized DBus manager interface");

	return 0;
}
コード例 #9
0
void send_notification(const std::string & owner, const std::string & message, bool with_history)
{
	DBusConnection *connection = get_dbus_connection();
	if (!connection) return;

	wnotify_by_owner & noticias = notifications.get<by_owner>();

	wnotify_owner_it i = noticias.find(owner);

	if (i != noticias.end()) {
		if (with_history) {
			i->message = message + "\n" + i->message;

			size_t endl_pos = i->message.find('\n');
			size_t ctr = 1;

			while (ctr < MAX_MSG_LINES && endl_pos != std::string::npos) {
				endl_pos = i->message.find('\n', endl_pos+1);
				ctr++;
			}

			i->message = i->message.substr(0,endl_pos);
		} else {
			i->message = message;
		}

		send_dbus_notification(connection, i->id, owner, i->message);
		return;
	} else {
		uint32_t id = send_dbus_notification(connection, 0, owner, message);
		if (!id) return;
		wnotify visual(id,owner,message);
		std::pair<wnotify_owner_it, bool> result = noticias.insert(visual);
		if (!result.second) {
			ERR_DU << "Failed to insert a dbus notification message:\n"
				<< "New Item:\n" << "\tid=" << id << "\n\towner=" << owner << "\n\tmessage=" << message << "\n"
				<< "Old Item:\n" << "\tid=" << result.first->id << "\n\towner=" << result.first->owner << "\n\tmessage=" << result.first->message << "\n";
		}
	}
}
コード例 #10
0
ファイル: caja-file-utilities.c プロジェクト: eyelash/caja
/**
 * caja_uninhibit_power_manager:
 * @cookie: the cookie value returned by caja_inhibit_power_manager()
 *
 * Uninhibits power management. This function must be called after the task
 * which inhibited power management has finished, or the system will not
 * return to normal power management.
 */
void
caja_uninhibit_power_manager (gint cookie)
{
    GDBusConnection *connection;
    GVariant *result;
    GError *error = NULL;

    g_return_if_fail (cookie > 0);

    connection = get_dbus_connection ();

    if (connection == NULL)
    {
        return;
    }

    result = g_dbus_connection_call_sync (connection,
                                          GSM_NAME,
                                          GSM_PATH,
                                          GSM_INTERFACE,
                                          "Uninhibit",
                                          g_variant_new ("(u)", (guint) cookie),
                                          NULL,
                                          G_DBUS_CALL_FLAGS_NO_AUTO_START,
                                          -1,
                                          NULL,
                                          &error);

    if (result == NULL)
    {
        g_warning ("Could not uninhibit power management: %s", error->message);
        g_error_free (error);
        return;
    }

    g_variant_unref (result);
}
コード例 #11
0
ファイル: lightum.c プロジェクト: vgeddes/lightum
int
main (int argc, char *argv[])
{

    int screensaver = 0, c, brightness_prev = -1, backlight_prev = -1;
    int light = 0, brightness = 255, backlight = 100;
    int foreground = 0, verbose = 0, debug = 0;
    int brightness_restore, backlight_restore, brightness_restoreflag =
        0, backlight_restoreflag = 0;
    int res, dbus_backend = -1, tmp = -1;
    float idletime = 0;
    pid_t pid;
    conf_data conf;
    Display *display = NULL;
    DBusGConnection *connection;
    DBusGProxy *proxy_manager;
    DBusGProxy *proxy_session;
    uid_t uid, euid;
    int light_aux = -1, light_avg = -1;
    int lightvalues[15] = { 0 };
    int countarray[256] = { 0 };
    unsigned int i, index = 0;

    // make sure we are run as a regular user
    uid = getuid ();
    euid = geteuid ();
    if (uid == 0 || euid == 0) {
        fprintf (stderr, "lightum must NOT be run as root.\n");
        exit (1);
    }
    // overwrite defaults with config file
    conf = config_parse ();

    // overwrite config file with command line arguments
    while ((c = getopt (argc, argv, "hxuUlsvfm:n:M:N:p:I:i:d:w:?")) != EOF) {
        switch (c) {
        case 'h':
            usage ("");
            break;
        case 'x':
            conf.manualmode = 1;
            break;
        case 'u':
            conf.ignoreuser = 0;
            break;
        case 'U':
            conf.ignoresession = 1;
            break;
        case 'l':
            conf.fulldim = 1;
            break;
        case 's':
            conf.queryscreensaver = 1;
            break;
        case 'f':
            foreground = 1;
            break;
        case 'v':
            verbose = 1;
            break;
        case 'd':
            debug = atoi (optarg);
            break;
        case 'm':
            conf.maxbrightness = atoi (optarg);
            break;
        case 'n':
            conf.minbrightness = atoi (optarg);
            break;
        case 'M':
            conf.maxbacklight = atoi (optarg);
            break;
        case 'N':
            conf.minbacklight = atoi (optarg);
            break;
        case 'p':
            conf.polltime = atoi (optarg);
            break;
        case 'i':
            conf.idleoff = atoi (optarg);
            break;
        case 'I':
            conf.screenidle = atoi (optarg);
            break;
        case 'w':
            conf.workmode = atoi (optarg);
            break;
        default:
            usage ("ERROR: Unknown OPTION\n");
            break;
        }
    }

    if (verbose)
        printf ("CONFIG:\n\tmanualmode: %d\n", conf.manualmode);
    if (verbose)
        printf ("\tignoreuser: %d\n", conf.ignoreuser);
    if (verbose)
        printf ("\tignoresession: %d\n", conf.ignoresession);
    if (verbose)
        printf ("\tworkmode: %d\n", conf.workmode);
    if (verbose)
        printf ("\tqueryscreensaver: %d\n", conf.queryscreensaver);
    if (verbose)
        printf ("\tmaxbrightness: %d\n", conf.maxbrightness);
    if (verbose)
        printf ("\tminbrightness: %d\n", conf.minbrightness);
    if (verbose)
        printf ("\tmaxbacklight: %d\n", conf.maxbacklight);
    if (verbose)
        printf ("\tminbacklight: %d\n", conf.minbacklight);
    if (verbose)
        printf ("\tpolltime: %d\n", conf.polltime);
    if (verbose)
        printf ("\tidleoff: %d\n", conf.idleoff);
    if (verbose)
        printf ("\tscreenidle: %d\n", conf.screenidle);
    if (verbose)
        printf ("\tfulldim: %d\n\n", conf.fulldim);

    // make sure all config values are correct
    check_config_values (conf);
    if (debug < 0 || debug > 3)
        usage ("ERROR: Wrong value in config variable 'debug'\n");

    // if debug enabled, force verbose mode too
    if (debug > 0)
        verbose = 1;
    // if verbose enabled, force foreground mode too
    if (verbose)
        foreground = 1;

    if (conf.manualmode)
        printf ("lightum v%s running in manual mode ", VERSION);
    else
        printf ("lightum v%s running in auto mode ", VERSION);
    fflush (stdout);

    if (!foreground) {
        if ((pid = fork ()) < 0)
            exit (1);
        else if (pid != 0)
            exit (0);
        /* daemon running here */
        setsid ();
        res = chdir ("/");
        if (res != 0) {
            perror ("Could not chdir");
            exit (1);
        }
        umask (0);
        printf ("forked into background\n");
    } else
        printf ("\n");

    /* create pid file */
    if (!create_pid_file ())
        exit (1);

    /* start with current brightness values */
    if (conf.workmode == 1 || conf.workmode == 3) {
        brightness_restore = get_keyboard_brightness_value ();
    }

    /* start with current backlight values */
    if (conf.workmode == 2 || conf.workmode == 3) {
        backlight_restore = get_screen_backlight_value ();

        // detect dbus backend: 0: gnome, 1: kde
        tmp =
            dbus_set_screen_backlight_value_gnome (acpi_to_dbus_backlight
                                                   (backlight_restore));
        if (tmp == -1) {
            tmp =
                dbus_set_screen_backlight_value_kde (acpi_to_dbus_backlight
                                                     (backlight_restore));
            if (tmp == -1) {
                tmp =
                    set_screen_xbacklight_value (acpi_to_dbus_backlight
                                                 (backlight_restore));
                if (tmp == -1) {
                    fprintf (stderr,
                             "Can't manage screen backlight on this system.\nPlease disable backlight with config option 'workmode='1' or command line switch '-w 1'.\nIf you believe this is an error, open a bug report: https://github.com/poliva/lightum/issues\n");
                    exit (1);
                } else {
                    dbus_backend = 2;
                }
            } else {
                dbus_backend = 1;
            }

        } else {
            dbus_backend = 0;
        }
    }

    if (conf.idleoff != 0 || conf.screenidle != 0) {
        display = XOpenDisplay (NULL);
        if (display == NULL) {
            fprintf (stderr, "Failed to open display\n");
            exit (1);
        }
    }

    signal_installer ();

    if (!conf.ignoresession) {
        connection = get_dbus_connection ();
        proxy_manager = get_dbus_proxy_manager (connection);
        proxy_session = get_dbus_proxy_session (connection, proxy_manager);
    }
    // initialize the light values array
    if (!conf.manualmode) {
        light = get_light_sensor_value ();
        for (i = 0; i < ARRAY_LENGTH (lightvalues); i++)
            lightvalues[i] = light;
        countarray[light] = ARRAY_LENGTH (lightvalues);
    } else {
        for (i = 0; i < ARRAY_LENGTH (lightvalues); i++)
            lightvalues[i] = 0;
    }

    while (1) {

        if (reloadconfig) {
            conf = config_parse ();
            if (verbose)
                printf ("lightum: SIGUSR1 received, configuration reloaded\n");
            check_config_values (conf);
            reloadconfig = 0;
        }

        if (!conf.ignoresession) {
            if (!get_session_active (proxy_session)) {
                if (verbose)
                    printf
                        ("lightum: user session not active, sleeping %d milliseconds.\nIf you believe this is an error, try running lightum with 'ignoresession=1' or '-U' command line switch.\n",
                         conf.polltime);
                usleep (conf.polltime * 1000);
                continue;
            }
        }

        if (!conf.manualmode) {
            light = get_light_sensor_value ();
            if (verbose)
                printf ("light_sensor: %d ", light);

            // to avoid backlight flickering when the light sensor flaps too frequently
            // between two values, we collect lighting values and use the most common
            // value of the collected values

            if (index == ARRAY_LENGTH (lightvalues))
                index = 0;
            lightvalues[index] = light;

            // get the most repetitive value of lightvalues array
            for (i = 0; i < ARRAY_LENGTH (lightvalues); ++i) {
                countarray[lightvalues[i]]++;
            }

            light_avg = -1;
            light_aux = -1;
            for (i = 0; i < ARRAY_LENGTH (countarray); ++i) {
                if (countarray[i] > light_aux) {
                    light_aux = countarray[i];
                    light_avg = i;
                }
                countarray[i] = 0;
            }

            light = light_avg;

            if (verbose)
                printf ("light_avg: %d ", light);
            index++;
        }

        if (conf.idleoff != 0 || conf.screenidle != 0) {
            idletime = get_session_idle_time (display);
            if (verbose)
                printf ("idle_time: %f ", idletime);
        }

        if (!conf.manualmode) {

            if (conf.workmode == 1 || conf.workmode == 3)
                brightness =
                    calculate_keyboard_brightness_value (light,
                                                         conf.maxbrightness,
                                                         conf.minbrightness);
            if (conf.workmode == 2 || conf.workmode == 3)
                backlight =
                    calculate_screen_backlight_value (light, conf.maxbacklight,
                                                      conf.minbacklight);
            if (verbose)
                printf ("auto mode ");

        } else {

            if (verbose)
                printf ("manual mode ");
            if (conf.workmode == 1 || conf.workmode == 3) {
                if (!screensaver) {
                    if (idletime > conf.idleoff) {
                        if (brightness_restoreflag == 0) {
                            brightness_restore =
                                get_keyboard_brightness_value ();
                            brightness_restoreflag = 1;
                            if (debug == 1 || debug == 3)
                                printf ("brightness_restoreflag(%d) ",
                                        brightness_restore);
                        }
                        brightness = conf.minbrightness;
                    } else {
                        brightness = brightness_restore;
                        brightness_restoreflag = 0;
                        if (debug == 1 || debug == 3)
                            printf ("brightness_restored(%d) ",
                                    brightness_restore);
                    }
                }
            }

            if (conf.workmode == 2 || conf.workmode == 3) {
                if (!screensaver) {
                    if (idletime > conf.screenidle) {
                        if (backlight_restoreflag == 0) {
                            backlight_restore = get_screen_backlight_value ();
                            backlight_restoreflag = 1;
                            if (debug == 2 || debug == 3)
                                printf ("backlight_restoreflag(%d) ",
                                        backlight_restore);
                        }
                        if (conf.fulldim)
                            backlight = 1;
                        else
                            backlight = conf.minbacklight;
                    } else {
                        backlight = backlight_restore;
                        backlight_restoreflag = 0;
                        if (debug == 2 || debug == 3)
                            printf ("backlight_restored(%d) ",
                                    backlight_restore);
                    }
                }
            }

        }

        if (conf.workmode == 1 || conf.workmode == 3) {
            if ((conf.idleoff != 0) && (idletime > conf.idleoff)) {
                brightness = conf.minbrightness;
            }
        }

        if (conf.workmode == 2 || conf.workmode == 3) {
            if ((conf.screenidle != 0) && (idletime > conf.screenidle)) {
                if (conf.fulldim)
                    backlight = 1;
                else
                    backlight = conf.minbacklight;
            }
        }

        if (conf.queryscreensaver) {
            screensaver = get_screensaver_active ();
            if (verbose)
                printf ("screensaver: %d ", screensaver);
            if (screensaver) {
                brightness = 0;
                backlight = conf.minbacklight;
            }
        }

        if (conf.workmode == 1 || conf.workmode == 3)
            if (verbose)
                printf ("brightness: %d/%d ", brightness, conf.maxbrightness);

        if (conf.workmode == 2 || conf.workmode == 3)
            if (verbose)
                printf ("backlight: %d/%d ", backlight, conf.maxbacklight);

        // keyboard brightness
        if (conf.workmode == 1 || conf.workmode == 3) {
            if (brightness != brightness_prev) {
                if (!conf.manualmode) {
                    brightness_restore = get_keyboard_brightness_value ();
                    if (brightness_restore < conf.minbrightness)
                        brightness_restore = conf.minbrightness;
                    if (debug == 1 || debug == 3)
                        printf ("\ncurrent brightness: %d\n",
                                brightness_restore);
                    if ((brightness_restore != brightness_prev)
                        && (brightness_restoreflag)) {
                        if (!conf.ignoreuser) {
                            /* make sure maxbrightness is never <4 */
                            if (brightness_restore < 4)
                                conf.maxbrightness = 4;
                            else
                                conf.maxbrightness = brightness_restore;
                            if (verbose)
                                printf
                                    ("-> Detected user brightness change, setting maxbrightness to %d\n",
                                     conf.maxbrightness);
                            brightness_prev = brightness_restore;
                        } else {
                            if (verbose)
                                printf
                                    ("-> Ignoring user brightness change, wants to set maxbrightness to %d\n",
                                     brightness_restore);
                        }
                        brightness =
                            calculate_keyboard_brightness_value (light,
                                                                 conf.
                                                                 maxbrightness,
                                                                 conf.
                                                                 minbrightness);
                    }
                    brightness_restoreflag = 1;
                }
                if (debug == 1 || debug == 3)
                    printf ("-> set keyboard brightness: %d -> %d\n",
                            brightness_prev, brightness);
                fading (brightness_prev, brightness);
                usleep (1500);
                brightness = get_keyboard_brightness_value ();
                brightness_prev = brightness;
            }
            if (!conf.manualmode) {
                tmp = get_keyboard_brightness_value ();
                if (tmp != brightness) {
                    if (verbose)
                        printf
                            ("-> Detected user brightness change, current brightness is set to %d\n",
                             tmp);
                    if (conf.ignoreuser) {
                        if (debug == 1 || debug == 3)
                            printf ("\n*** forcing brightness from %d to %d\n",
                                    tmp, brightness);
                        fading (tmp, brightness);
                    }
                }
            }
        }
        // screen backlight
        if (conf.workmode == 2 || conf.workmode == 3) {
            if (backlight != backlight_prev) {
                if (!conf.manualmode) {
                    backlight_restore = get_screen_backlight_value ();
                    if (debug == 2 || debug == 3)
                        printf ("\ncurrent backlight: %d\n",
                                backlight_restore);
                    if ((backlight_restore != backlight_prev)
                        && (backlight_restoreflag)) {
                        if (!conf.ignoreuser) {
                            if (verbose)
                                printf
                                    ("-> Detected user backlight change, switching to manualmode\n");
                            conf.manualmode = 1;
                            backlight_prev = backlight_restore;
                            backlight = backlight_restore;
                        } else {
                            if (verbose)
                                printf
                                    ("-> Ignoring user backlight change, wants to set maxbacklight to %d\n",
                                     backlight_restore);
                            backlight =
                                calculate_screen_backlight_value (light,
                                                                  conf.
                                                                  maxbacklight,
                                                                  conf.
                                                                  minbacklight);
                        }
                    }
                    backlight_restoreflag = 1;
                }
                if (debug == 2 || debug == 3)
                    printf ("-> set screen backlight: %d -> %d\n",
                            backlight_prev, backlight);
                backlight_fading (backlight_prev, backlight, dbus_backend);
                usleep (1500);
                backlight = get_screen_backlight_value ();
                backlight_prev = backlight;
            }
            if (!conf.manualmode) {
                tmp = get_screen_backlight_value ();
                if (tmp != backlight) {
                    if (verbose)
                        printf
                            ("-> Detected user backlight change, current backlight is set to %d\n",
                             tmp);
                    if (conf.ignoreuser) {
                        if (debug == 2 || debug == 3)
                            printf ("\n*** forcing backlight from %d to %d\n",
                                    tmp, backlight);
                        backlight_fading (tmp, backlight, dbus_backend);
                    }
                }
            }
        }

        if (verbose)
            printf ("\n");

        usleep (conf.polltime * 1000);
    }

    // we should never reach here.
    //if (conf.idleoff != 0) XCloseDisplay(display);
    //dbus_g_connection_unref(connection);
    exit (1);
}