/** * up_backend_init: **/ static void up_backend_init (UpBackend *backend) { GError *err = NULL; GTimeVal timeval; UpDeviceClass *device_class; backend->priv = UP_BACKEND_GET_PRIVATE (backend); backend->priv->daemon = NULL; backend->priv->is_laptop = up_native_is_laptop(); g_debug("is_laptop:%d",backend->priv->is_laptop); if (backend->priv->is_laptop) { backend->priv->ac = UP_DEVICE(up_device_new()); backend->priv->battery = UP_DEVICE(up_device_new ()); device_class = UP_DEVICE_GET_CLASS (backend->priv->battery); device_class->get_on_battery = up_apm_device_get_on_battery; device_class->get_low_battery = up_apm_device_get_low_battery; device_class->get_online = up_apm_device_get_online; device_class->refresh = up_apm_device_refresh; device_class = UP_DEVICE_GET_CLASS (backend->priv->ac); device_class->get_on_battery = up_apm_device_get_on_battery; device_class->get_low_battery = up_apm_device_get_low_battery; device_class->get_online = up_apm_device_get_online; device_class->refresh = up_apm_device_refresh; g_thread_init (NULL); /* creates thread */ if((backend->priv->apm_thread = (GThread*) g_thread_create((GThreadFunc)up_backend_apm_event_thread, (void*) backend, FALSE, &err) == NULL)) { g_warning("Thread create failed: %s", err->message); g_error_free (err); } /* setup dummy */ g_get_current_time (&timeval); g_object_set (backend->priv->battery, "type", UP_DEVICE_KIND_BATTERY, "power-supply", TRUE, "is-present", TRUE, "is-rechargeable", TRUE, "has-history", TRUE, "state", UP_DEVICE_STATE_UNKNOWN, "percentage", 0.0f, "time-to-empty", (gint64) 0, "update-time", (guint64) timeval.tv_sec, (void*) NULL); g_object_set (backend->priv->ac, "type", UP_DEVICE_KIND_LINE_POWER, "online", TRUE, "power-supply", TRUE, "update-time", (guint64) timeval.tv_sec, (void*) NULL); } else { backend->priv->ac = NULL; backend->priv->battery = NULL; } }
/** * up_device_refresh_internal: * * NOTE: if you're calling this function you have to ensure you're doing the * the changed signals on the right interfaces, although by monitoring * notify::update-time this should be mostly done. **/ gboolean up_device_refresh_internal (UpDevice *device) { gboolean ret = FALSE; UpDeviceClass *klass = UP_DEVICE_GET_CLASS (device); /* not implemented */ if (klass->refresh == NULL) goto out; /* do the refresh */ ret = klass->refresh (device); if (!ret) { egg_debug ("no changes"); goto out; } /* the first time, print all properties */ if (!device->priv->has_ever_refresh) { egg_debug ("added native-path: %s\n", device->priv->native_path); device->priv->has_ever_refresh = TRUE; goto out; } out: return ret; }
/** * up_device_coldplug: * * Return %TRUE on success, %FALSE if we failed to get data and should be removed **/ gboolean up_device_coldplug (UpDevice *device, UpDaemon *daemon, GObject *native) { gboolean ret; const gchar *native_path; UpDeviceClass *klass = UP_DEVICE_GET_CLASS (device); gchar *id = NULL; g_return_val_if_fail (UP_IS_DEVICE (device), FALSE); /* save */ device->priv->native = g_object_ref (native); device->priv->daemon = g_object_ref (daemon); native_path = up_native_get_native_path (native); device->priv->native_path = g_strdup (native_path); /* stop signals and callbacks */ g_object_freeze_notify (G_OBJECT(device)); device->priv->during_coldplug = TRUE; /* coldplug source */ if (klass->coldplug != NULL) { ret = klass->coldplug (device); if (!ret) { egg_debug ("failed to coldplug %s", device->priv->native_path); goto out; } } /* only put on the bus if we succeeded */ ret = up_device_register_device (device); if (!ret) { egg_warning ("failed to register device %s", device->priv->native_path); goto out; } /* force a refresh, although failure isn't fatal */ ret = up_device_refresh_internal (device); if (!ret) { egg_debug ("failed to refresh %s", device->priv->native_path); /* TODO: refresh should really have seporate * success _and_ changed parameters */ ret = TRUE; goto out; } /* get the id so we can load the old history */ id = up_device_get_id (device); if (id != NULL) up_history_set_id (device->priv->history, id); out: /* start signals and callbacks */ g_object_thaw_notify (G_OBJECT(device)); device->priv->during_coldplug = FALSE; g_free (id); return ret; }
/** * up_device_get_online: * * Note: Only implement for system devices, i.e. devices supplying the system **/ gboolean up_device_get_online (UpDevice *device, gboolean *online) { UpDeviceClass *klass = UP_DEVICE_GET_CLASS (device); g_return_val_if_fail (UP_IS_DEVICE (device), FALSE); /* no support */ if (klass->get_online == NULL) return FALSE; return klass->get_online (device, online); }
/** * up_device_get_low_battery: * * Note: Only implement for system devices, i.e. ones supplying the system **/ gboolean up_device_get_low_battery (UpDevice *device, gboolean *low_battery) { UpDeviceClass *klass = UP_DEVICE_GET_CLASS (device); g_return_val_if_fail (UP_IS_DEVICE (device), FALSE); /* no support */ if (klass->get_low_battery == NULL) return FALSE; return klass->get_low_battery (device, low_battery); }