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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* CDK - The GIMP Drawing Kit
 * Copyright (C) 2019 Руслан Ижбулатов <lrn1986@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#define COBJMACROS
#include <msctf.h>

#include <cdk/cdk.h>

#include "cdkprivate-win32.h"

struct _CdkWin32ALPNSink
{
  ITfActiveLanguageProfileNotifySink itf_alpn_sink;

  gint ref_count;
};

typedef struct _CdkWin32ALPNSink CdkWin32ALPNSink;

static CdkWin32ALPNSink *actlangchangenotify = NULL;
static ITfSource *itf_source = NULL;
static DWORD actlangchangenotify_id = 0;

static ULONG STDMETHODCALLTYPE
alpn_sink_addref (ITfActiveLanguageProfileNotifySink *This)
{
  CdkWin32ALPNSink *alpn_sink = (CdkWin32ALPNSink *) This;
  int ref_count = ++alpn_sink->ref_count;

  return ref_count;
}

static HRESULT STDMETHODCALLTYPE
alpn_sink_queryinterface (ITfActiveLanguageProfileNotifySink *This,
                          REFIID                              riid,
                          LPVOID                             *ppvObject)
{
  *ppvObject = NULL;

  if (IsEqualGUID (riid, &IID_IUnknown))
    {
      ITfActiveLanguageProfileNotifySink_AddRef (This);
      *ppvObject = This;
      return S_OK;
    }

  if (IsEqualGUID (riid, &IID_ITfActiveLanguageProfileNotifySink))
    {
      ITfActiveLanguageProfileNotifySink_AddRef (This);
      *ppvObject = This;
      return S_OK;
    }

  return E_NOINTERFACE;
}

static ULONG STDMETHODCALLTYPE
alpn_sink_release (ITfActiveLanguageProfileNotifySink *This)
{
  CdkWin32ALPNSink *alpn_sink = (CdkWin32ALPNSink *) This;
  int ref_count = --alpn_sink->ref_count;

  if (ref_count == 0)
    {
      g_free (This);
    }

  return ref_count;
}

static HRESULT STDMETHODCALLTYPE
alpn_sink_on_activated (ITfActiveLanguageProfileNotifySink *This,<--- Parameter 'This' can be declared as pointer to const
                        REFCLSID                            clsid,
                        REFGUID                             guidProfile,
                        BOOL                                fActivated)
{
  _cdk_input_locale_is_ime = fActivated;
  return S_OK;
}

static ITfActiveLanguageProfileNotifySinkVtbl alpn_sink_vtbl = {
  alpn_sink_queryinterface,
  alpn_sink_addref,
  alpn_sink_release,
  alpn_sink_on_activated,<--- You might need to cast the function pointer here
};

static CdkWin32ALPNSink *
alpn_sink_new ()
{
  CdkWin32ALPNSink *result;

  result = g_new0 (CdkWin32ALPNSink, 1);
  result->itf_alpn_sink.lpVtbl = &alpn_sink_vtbl;
  result->ref_count = 0;

  ITfActiveLanguageProfileNotifySink_AddRef (&result->itf_alpn_sink);

  return result;
}


void
_cdk_win32_lang_notification_init ()
{
  HRESULT hr;
  ITfThreadMgr *itf_threadmgr;

  CoInitializeEx (NULL, COINIT_APARTMENTTHREADED);

  if (actlangchangenotify != NULL)
    return;

  hr = CoCreateInstance (&CLSID_TF_ThreadMgr,
                         NULL,
                         CLSCTX_INPROC_SERVER,
                         &IID_ITfThreadMgr,
                         (LPVOID *) &itf_threadmgr);

  if (!SUCCEEDED (hr))
    return;

  hr = ITfThreadMgr_QueryInterface (itf_threadmgr, &IID_ITfSource, (VOID **) &itf_source);
  ITfThreadMgr_Release (itf_threadmgr);

  if (!SUCCEEDED (hr))
    return;

  actlangchangenotify = alpn_sink_new ();

  hr = ITfSource_AdviseSink (itf_source,
                             &IID_ITfActiveLanguageProfileNotifySink,
                             (IUnknown *) actlangchangenotify,
                             &actlangchangenotify_id);

  if (!SUCCEEDED (hr))
    {
      ITfActiveLanguageProfileNotifySink_Release (&actlangchangenotify->itf_alpn_sink);
      actlangchangenotify = NULL;
      ITfSource_Release (itf_source);
      itf_source = NULL;
    }
}

void
_cdk_win32_lang_notification_exit ()
{
  if (actlangchangenotify != NULL && itf_source != NULL)
    {
      ITfSource_UnadviseSink (itf_source, actlangchangenotify_id);
      ITfSource_Release (itf_source);
      ITfActiveLanguageProfileNotifySink_Release (&actlangchangenotify->itf_alpn_sink);
    }

  CoUninitialize ();
}