summarylogtreecommitdiffstats
path: root/wrap.c
blob: ca868071e54294a7d514ecbe4039a35ed3f62247 (plain)
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
// by https://aur.archlinux.org/account/AvianaCruz
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <X11/Xlibint.h>
#include <dlfcn.h>
#include <openssl/ssl.h>
#include <stdlib.h>

#ifdef WRAP_FORCE_SINK_HARDWARE
#include <pulse/introspect.h>
#include <pulse/subscribe.h>
#include <string.h>
#endif

#ifdef SSL_get_peer_certificate
#undef SSL_get_peer_certificate
#endif

X509 *SSL_get_peer_certificate(const SSL *s) {
  return SSL_get1_peer_certificate(s);
}

// temporary hack for Wayland screen sharing crash
typedef int (*wrap_XMapWindow_t)(register Display *dpy, Window w);
static wrap_XMapWindow_t wrap_orig_XMapWindow;

int XMapWindow(register Display *dpy, Window w) {
  const char *env = getenv("XDG_SESSION_TYPE");
  if (env == NULL || strcmp(env, "wayland") != 0) {
    if (!wrap_orig_XMapWindow) {
      wrap_orig_XMapWindow = dlsym(RTLD_NEXT, "XMapWindow");
    }

    return wrap_orig_XMapWindow(dpy, w);
  }

  return 1;
}

#ifdef WRAP_FORCE_SINK_HARDWARE
typedef pa_operation *(*wrap_pa_get_sink_by_name_t)(pa_context *c,
                                                    const char *name,
                                                    pa_sink_info_cb_t cb,
                                                    void *userdata);
typedef pa_operation *(*wrap_pa_get_sink_by_index_t)(pa_context *c,
                                                     uint32_t idx,
                                                     pa_sink_info_cb_t cb,
                                                     void *userdata);
typedef pa_operation *(*wrap_pa_get_sink_list_t)(pa_context *c,
                                                 pa_sink_info_cb_t cb,
                                                 void *userdata);

static wrap_pa_get_sink_by_name_t wrap_orig_get_sink_info_by_name;
static pa_sink_info_cb_t wrap_orig_sink_info_by_name_cb;

static wrap_pa_get_sink_by_index_t wrap_orig_get_sink_info_by_index;
static pa_sink_info_cb_t wrap_orig_sink_info_by_index_cb;

static wrap_pa_get_sink_list_t wrap_orig_get_sink_list;
static pa_sink_info_cb_t wrap_orig_sink_info_list_cb;

static void wrap_get_sink_info_callback(pa_context *c, const pa_sink_info *i,
                                        int eol, void *userdata,
                                        pa_sink_info_cb_t orig_cb) {
  if (i && !(i->flags & PA_SINK_HARDWARE)) {
    pa_sink_info sink_info[sizeof(pa_sink_info)];
    memcpy(sink_info, i, sizeof(pa_sink_info));
    sink_info->flags |= PA_SINK_HARDWARE;

    orig_cb(c, sink_info, eol, userdata);
  } else {
    orig_cb(c, i, eol, userdata);
  }
}

static void wrap_get_sink_info_by_name_callback(pa_context *c,
                                                const pa_sink_info *i, int eol,
                                                void *userdata) {
  wrap_get_sink_info_callback(c, i, eol, userdata,
                              wrap_orig_sink_info_by_name_cb);
}

static void wrap_get_sink_info_by_index_callback(pa_context *c,
                                                 const pa_sink_info *i, int eol,
                                                 void *userdata) {
  wrap_get_sink_info_callback(c, i, eol, userdata,
                              wrap_orig_sink_info_by_index_cb);
}

static void wrap_get_sink_info_list_callback(pa_context *c,
                                             const pa_sink_info *i, int eol,
                                             void *userdata) {
  wrap_get_sink_info_callback(c, i, eol, userdata, wrap_orig_sink_info_list_cb);
}

pa_operation *pa_context_get_sink_info_list(pa_context *c, pa_sink_info_cb_t cb,
                                            void *userdata) {
  if (!wrap_orig_get_sink_list) {
    wrap_orig_get_sink_list = dlsym(RTLD_NEXT, "pa_context_get_sink_info_list");
  }
  wrap_orig_sink_info_list_cb = cb;

  return wrap_orig_get_sink_list(c, wrap_get_sink_info_list_callback, userdata);
}

pa_operation *pa_context_get_sink_info_by_index(pa_context *c, uint32_t idx,
                                                pa_sink_info_cb_t cb,
                                                void *userdata) {
  if (!wrap_orig_get_sink_info_by_index) {
    wrap_orig_get_sink_info_by_index =
        dlsym(RTLD_NEXT, "pa_context_get_sink_info_by_index");
  }
  wrap_orig_sink_info_by_index_cb = cb;

  return wrap_orig_get_sink_info_by_index(
      c, idx, wrap_get_sink_info_by_index_callback, userdata);
}

pa_operation *pa_context_get_sink_info_by_name(pa_context *c, const char *name,
                                               pa_sink_info_cb_t cb,
                                               void *userdata) {
  if (!wrap_orig_get_sink_info_by_name) {
    wrap_orig_get_sink_info_by_name =
        dlsym(RTLD_NEXT, "pa_context_get_sink_info_by_name");
  }
  wrap_orig_sink_info_by_name_cb = cb;

  return wrap_orig_get_sink_info_by_name(
      c, name, wrap_get_sink_info_by_name_callback, userdata);
}
#endif