File: device-state.c

package info (click to toggle)
phoc 0.51.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,948 kB
  • sloc: ansic: 106,056; xml: 3,765; sh: 138; makefile: 33; javascript: 5
file content (133 lines) | stat: -rw-r--r-- 3,650 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2023 Purism SPC
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 * Author: Guido Günther <agx@sigxcpu.org>
 */

#define G_LOG_DOMAIN "phoc-example"

#include "phoc-device-state-unstable-v1-client-protocol.h"

#include <glib.h>

static struct wl_display *display;
static struct zphoc_device_state_v1 *phoc_device_state;
struct zphoc_tablet_mode_switch_v1  *tablet_mode_switch;
struct zphoc_lid_switch_v1          *lid_switch;

static void
tablet_mode_switch_disabled (void *data,
                             struct zphoc_tablet_mode_switch_v1 *zphoc_tablet_mode_switch_v1)
{
  g_message ("Tablet mode disabled");
}

static void
tablet_mode_switch_enabled (void *data,
                            struct zphoc_tablet_mode_switch_v1 *zphoc_tablet_mode_switch_v1)
{
  g_message ("Tablet mode enabled");
}


const struct zphoc_tablet_mode_switch_v1_listener tablet_mode_switch_listener = {
  .disabled = tablet_mode_switch_disabled,
  .enabled = tablet_mode_switch_enabled,
};

static void
lid_switch_closed (void *data,
                   struct zphoc_lid_switch_v1 *zphoc_lid_switch_v1)
{
  g_message ("Lid closed");
}

static void
lid_switch_opened (void *data,
                   struct zphoc_lid_switch_v1 *zphoc_lid_switch_v1)
{
  g_message ("Lid opened");
}

const struct zphoc_lid_switch_v1_listener lid_switch_listener = {
  .closed = lid_switch_closed,
  .opened = lid_switch_opened,
};


static void
device_state_handle_capabilities (void *data,
                                  struct zphoc_device_state_v1 *zphoc_device_state_v1,
                                  uint32_t capabilities)
{
  g_message("Got capabilities: %d", capabilities);

  if (capabilities & ZPHOC_DEVICE_STATE_V1_CAPABILITY_TABLET_MODE_SWITCH) {
    tablet_mode_switch = zphoc_device_state_v1_get_tablet_mode_switch (phoc_device_state);
    zphoc_tablet_mode_switch_v1_add_listener (tablet_mode_switch, &tablet_mode_switch_listener, NULL);
  }
  if (capabilities & ZPHOC_DEVICE_STATE_V1_CAPABILITY_LID_SWITCH) {
    lid_switch = zphoc_device_state_v1_get_lid_switch (phoc_device_state);
    zphoc_lid_switch_v1_add_listener (lid_switch, &lid_switch_listener, NULL);
  }
}


const struct zphoc_device_state_v1_listener device_state_listener = {
  .capabilities = device_state_handle_capabilities,
};


static void
handle_global (void *data, struct wl_registry *registry,
               uint32_t name, const char *interface, uint32_t version)
{
  if (strcmp (interface, zphoc_device_state_v1_interface.name) == 0) {
    phoc_device_state = wl_registry_bind (registry, name,
                                          &zphoc_device_state_v1_interface, 2);
    zphoc_device_state_v1_add_listener (phoc_device_state, &device_state_listener, NULL);
  }
}

static void
handle_global_remove (void *data, struct wl_registry *registry,
                      uint32_t name)
{
  // who cares
}

static const struct wl_registry_listener registry_listener = {
  .global = handle_global,
  .global_remove = handle_global_remove,
};

int
main (int argc, char **argv)
{
  struct wl_registry *registry;

  display = wl_display_connect (NULL);
  if (display == NULL) {
    g_critical ("Failed to create display");
    return 1;
  }

  registry = wl_display_get_registry (display);
  wl_registry_add_listener (registry, &registry_listener, NULL);
  wl_display_roundtrip (display);

  if (phoc_device_state == NULL) {
    g_critical ("device_state protocol not available");
    return 1;
  }

  g_message ("Press CTRL-C to quit");
  wl_display_roundtrip (display);

  while (wl_display_dispatch (display)) {
    // This space intentionally left blank
  }

  return 0;
}