gimpinputdevicestore-dx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /* GIMP - The GNU Image Manipulation Program
  2. * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3. *
  4. * gimpinputdevicestore-dx.c
  5. * Input device store based on DirectX.
  6. * Copyright (C) 2007 Sven Neumann <sven@gimp.org>
  7. * Copyright (C) 2007 Tor Lillqvist <tml@novell.com>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. #include "config.h"
  23. #include <string.h>
  24. #include <gtk/gtk.h>
  25. #ifdef HAVE_DX_DINPUT
  26. #include <windows.h>
  27. #include <dinput.h>
  28. #include <rpc.h>
  29. #ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
  30. #define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 0x00000004
  31. #endif
  32. #include <gdk/gdkwin32.h>
  33. #include "libgimpmodule/gimpmodule.h"
  34. #include "gimpinputdevicestore.h"
  35. enum
  36. {
  37. COLUMN_GUID,
  38. COLUMN_LABEL,
  39. COLUMN_IDEVICE,
  40. NUM_COLUMNS
  41. };
  42. enum
  43. {
  44. DEVICE_ADDED,
  45. DEVICE_REMOVED,
  46. LAST_SIGNAL
  47. };
  48. typedef struct _GimpInputDeviceStoreClass GimpInputDeviceStoreClass;
  49. struct _GimpInputDeviceStore
  50. {
  51. GtkListStore parent_instance;
  52. GdkWindow *window;
  53. LPDIRECTINPUT8W directinput8;
  54. GError *error;
  55. };
  56. struct _GimpInputDeviceStoreClass
  57. {
  58. GtkListStoreClass parent_class;
  59. void (* device_added) (GimpInputDeviceStore *store,
  60. const gchar *udi);
  61. void (* device_removed) (GimpInputDeviceStore *store,
  62. const gchar *udi);
  63. };
  64. static void gimp_input_device_store_finalize (GObject *object);
  65. static gboolean gimp_input_device_store_add (GimpInputDeviceStore *store,
  66. const GUID *guid);
  67. static gboolean gimp_input_device_store_remove (GimpInputDeviceStore *store,
  68. const gchar *udi);
  69. G_DEFINE_DYNAMIC_TYPE (GimpInputDeviceStore, gimp_input_device_store,
  70. GTK_TYPE_LIST_STORE)
  71. static guint store_signals[LAST_SIGNAL] = { 0 };
  72. void
  73. gimp_input_device_store_register_types (GTypeModule *module)
  74. {
  75. gimp_input_device_store_register_type (module);
  76. }
  77. static void
  78. gimp_input_device_store_class_init (GimpInputDeviceStoreClass *klass)
  79. {
  80. GObjectClass *object_class = G_OBJECT_CLASS (klass);
  81. store_signals[DEVICE_ADDED] =
  82. g_signal_new ("device-added",
  83. G_TYPE_FROM_CLASS (klass),
  84. G_SIGNAL_RUN_FIRST,
  85. G_STRUCT_OFFSET (GimpInputDeviceStoreClass, device_added),
  86. NULL, NULL, NULL,
  87. G_TYPE_NONE, 1, G_TYPE_STRING);
  88. store_signals[DEVICE_REMOVED] =
  89. g_signal_new ("device-removed",
  90. G_TYPE_FROM_CLASS (klass),
  91. G_SIGNAL_RUN_FIRST,
  92. G_STRUCT_OFFSET (GimpInputDeviceStoreClass, device_removed),
  93. NULL, NULL, NULL,
  94. G_TYPE_NONE, 1, G_TYPE_STRING);
  95. object_class->finalize = gimp_input_device_store_finalize;
  96. klass->device_added = NULL;
  97. klass->device_removed = NULL;
  98. }
  99. static void
  100. gimp_input_device_store_class_finalize (GimpInputDeviceStoreClass *klass)
  101. {
  102. }
  103. static GdkFilterReturn
  104. aux_window_filter (GdkXEvent *xevent,
  105. GdkEvent *event,
  106. gpointer data)
  107. {
  108. #if 0
  109. GimpInputDeviceStore *store = (GimpInputDeviceStore *) data;
  110. const MSG *msg = (MSG *) xevent;
  111. /* Look for deviced being added or removed */
  112. switch (msg->message)
  113. {
  114. }
  115. #endif
  116. return GDK_FILTER_REMOVE;
  117. }
  118. static GdkWindow *
  119. create_aux_window (GimpInputDeviceStore *store)
  120. {
  121. GdkWindowAttr wa;
  122. GdkWindow *retval;
  123. /* Create a dummy window to be associated with DirectInput devices */
  124. wa.wclass = GDK_INPUT_OUTPUT;
  125. wa.event_mask = GDK_ALL_EVENTS_MASK;
  126. wa.width = 2;
  127. wa.height = 2;
  128. wa.x = -100;
  129. wa.y = -100;
  130. wa.window_type = GDK_WINDOW_TOPLEVEL;
  131. if ((retval = gdk_window_new (NULL, &wa, GDK_WA_X|GDK_WA_Y)) == NULL)
  132. return NULL;
  133. g_object_ref (retval);
  134. gdk_window_add_filter (retval, aux_window_filter, store);
  135. return retval;
  136. }
  137. static BOOL CALLBACK
  138. enum_devices (const DIDEVICEINSTANCEW *di,
  139. void *user_data)
  140. {
  141. GimpInputDeviceStore *store = (GimpInputDeviceStore *) user_data;
  142. gimp_input_device_store_add (store, &di->guidInstance);
  143. return DIENUM_CONTINUE;
  144. }
  145. static void
  146. gimp_input_device_store_init (GimpInputDeviceStore *store)
  147. {
  148. GType types[] = { G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER };
  149. HRESULT hresult;
  150. HMODULE thismodule;
  151. HMODULE dinput8;
  152. typedef HRESULT (WINAPI *t_DirectInput8Create) (HINSTANCE, DWORD, REFIID, LPVOID *, LPUNKNOWN);
  153. t_DirectInput8Create p_DirectInput8Create;
  154. g_assert (G_N_ELEMENTS (types) == NUM_COLUMNS);
  155. gtk_list_store_set_column_types (GTK_LIST_STORE (store),
  156. G_N_ELEMENTS (types), types);
  157. if (!GetModuleHandleExW (GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
  158. GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
  159. (LPCTSTR) &gimp_input_device_store_init,
  160. &thismodule))
  161. return;
  162. if ((store->window = create_aux_window (store)) == NULL)
  163. {
  164. g_set_error_literal (&store->error, GIMP_MODULE_ERROR, GIMP_MODULE_FAILED,
  165. "Could not create aux window");
  166. return;
  167. }
  168. if ((dinput8 = LoadLibraryW (L"dinput8.dll")) == NULL)
  169. {
  170. g_set_error_literal (&store->error, GIMP_MODULE_ERROR, GIMP_MODULE_FAILED,
  171. "Could not load dinput8.dll");
  172. return;
  173. }
  174. if ((p_DirectInput8Create = (t_DirectInput8Create) GetProcAddress (dinput8, "DirectInput8Create")) == NULL)
  175. {
  176. g_set_error_literal (&store->error, GIMP_MODULE_ERROR, GIMP_MODULE_FAILED,
  177. "Could not find DirectInput8Create in dinput8.dll");
  178. return;
  179. }
  180. if (FAILED ((hresult = (*p_DirectInput8Create) (thismodule,
  181. DIRECTINPUT_VERSION,
  182. &IID_IDirectInput8W,
  183. (LPVOID *) &store->directinput8,
  184. NULL))))
  185. {
  186. g_set_error (&store->error, GIMP_MODULE_ERROR, GIMP_MODULE_FAILED,
  187. "DirectInput8Create failed: %s",
  188. g_win32_error_message (hresult));
  189. return;
  190. }
  191. if (FAILED ((hresult = IDirectInput8_EnumDevices (store->directinput8,
  192. DI8DEVCLASS_GAMECTRL,
  193. enum_devices,
  194. store,
  195. DIEDFL_ATTACHEDONLY))))
  196. {
  197. g_set_error (&store->error, GIMP_MODULE_ERROR, GIMP_MODULE_FAILED,
  198. "IDirectInput8::EnumDevices failed: %s",
  199. g_win32_error_message (hresult));
  200. return;
  201. }
  202. }
  203. static void
  204. gimp_input_device_store_finalize (GObject *object)
  205. {
  206. GimpInputDeviceStore *store = GIMP_INPUT_DEVICE_STORE (object);
  207. if (store->directinput8)
  208. {
  209. IDirectInput8_Release (store->directinput8);
  210. store->directinput8 = NULL;
  211. }
  212. if (store->error)
  213. {
  214. g_error_free (store->error);
  215. store->error = NULL;
  216. }
  217. G_OBJECT_CLASS (gimp_input_device_store_parent_class)->finalize (object);
  218. }
  219. static gboolean
  220. gimp_input_device_store_lookup (GimpInputDeviceStore *store,
  221. const gchar *guid,
  222. GtkTreeIter *iter)
  223. {
  224. GtkTreeModel *model = GTK_TREE_MODEL (store);
  225. GValue value = G_VALUE_INIT;
  226. gboolean iter_valid;
  227. for (iter_valid = gtk_tree_model_get_iter_first (model, iter);
  228. iter_valid;
  229. iter_valid = gtk_tree_model_iter_next (model, iter))
  230. {
  231. const gchar *str;
  232. gtk_tree_model_get_value (model, iter, COLUMN_GUID, &value);
  233. str = g_value_get_string (&value);
  234. if (strcmp (str, guid) == 0)
  235. {
  236. g_value_unset (&value);
  237. break;
  238. }
  239. g_value_unset (&value);
  240. }
  241. return iter_valid;
  242. }
  243. /* insert in alphabetic order */
  244. static void
  245. gimp_input_device_store_insert (GimpInputDeviceStore *store,
  246. const gchar *guid,
  247. const gchar *label,
  248. LPDIRECTINPUTDEVICE8W didevice8)
  249. {
  250. GtkTreeModel *model = GTK_TREE_MODEL (store);
  251. GtkTreeIter iter;
  252. GValue value = G_VALUE_INIT;
  253. gint pos = 0;
  254. gboolean iter_valid;
  255. for (iter_valid = gtk_tree_model_get_iter_first (model, &iter);
  256. iter_valid;
  257. iter_valid = gtk_tree_model_iter_next (model, &iter), pos++)
  258. {
  259. const gchar *str;
  260. gtk_tree_model_get_value (model, &iter, COLUMN_LABEL, &value);
  261. str = g_value_get_string (&value);
  262. if (g_utf8_collate (label, str) < 0)
  263. {
  264. g_value_unset (&value);
  265. break;
  266. }
  267. g_value_unset (&value);
  268. }
  269. gtk_list_store_insert_with_values (GTK_LIST_STORE (store), &iter, pos,
  270. COLUMN_GUID, guid,
  271. COLUMN_LABEL, label,
  272. COLUMN_IDEVICE, didevice8,
  273. -1);
  274. }
  275. static gboolean
  276. gimp_input_device_store_add (GimpInputDeviceStore *store,
  277. const GUID *guid)
  278. {
  279. HRESULT hresult;
  280. LPDIRECTINPUTDEVICE8W didevice8;
  281. DIDEVICEINSTANCEW di;
  282. gboolean added = FALSE;
  283. unsigned char *s;
  284. gchar *guidstring;
  285. gchar *name;
  286. if (UuidToString (guid, &s) != S_OK)
  287. return FALSE;
  288. guidstring = g_strdup (s);
  289. RpcStringFree (&s);
  290. if (FAILED ((hresult = IDirectInput8_CreateDevice (store->directinput8,
  291. guid,
  292. &didevice8,
  293. NULL))))
  294. {
  295. g_free (guidstring);
  296. return FALSE;
  297. }
  298. if (FAILED ((hresult = IDirectInputDevice8_SetCooperativeLevel (didevice8,
  299. (HWND) gdk_win32_window_get_handle (store->window),
  300. DISCL_NONEXCLUSIVE | DISCL_BACKGROUND))))
  301. {
  302. g_warning ("IDirectInputDevice8::SetCooperativeLevel failed: %s",
  303. g_win32_error_message (hresult));
  304. g_free (guidstring);
  305. return FALSE;
  306. }
  307. di.dwSize = sizeof (DIDEVICEINSTANCEW);
  308. if (FAILED ((hresult = IDirectInputDevice8_GetDeviceInfo (didevice8,
  309. &di))))
  310. {
  311. g_warning ("IDirectInputDevice8::GetDeviceInfo failed: %s",
  312. g_win32_error_message (hresult));
  313. g_free (guidstring);
  314. return FALSE;
  315. }
  316. name = g_utf16_to_utf8 (di.tszInstanceName, -1, NULL, NULL, NULL);
  317. gimp_input_device_store_insert (store, guidstring, name, didevice8);
  318. return added;
  319. }
  320. static gboolean
  321. gimp_input_device_store_remove (GimpInputDeviceStore *store,
  322. const gchar *guid)
  323. {
  324. GtkTreeIter iter;
  325. if (gimp_input_device_store_lookup (store, guid, &iter))
  326. {
  327. gtk_list_store_remove (GTK_LIST_STORE (store), &iter);
  328. return TRUE;
  329. }
  330. return FALSE;
  331. }
  332. #if 0
  333. static void
  334. gimp_input_device_store_device_added (LibHalContext *ctx,
  335. const char *guid)
  336. {
  337. GimpInputDeviceStore *store = libhal_ctx_get_user_data (ctx);
  338. if (gimp_input_device_store_add (store, udi))
  339. {
  340. g_signal_emit (store, store_signals[DEVICE_ADDED], 0, udi);
  341. }
  342. }
  343. static void
  344. gimp_input_device_store_device_removed (LibHalContext *ctx,
  345. const char *udi)
  346. {
  347. GimpInputDeviceStore *store = libhal_ctx_get_user_data (ctx);
  348. if (gimp_input_device_store_remove (store, udi))
  349. {
  350. g_signal_emit (store, store_signals[DEVICE_REMOVED], 0, udi);
  351. }
  352. }
  353. #endif
  354. GimpInputDeviceStore *
  355. gimp_input_device_store_new (void)
  356. {
  357. return g_object_new (GIMP_TYPE_INPUT_DEVICE_STORE, NULL);
  358. }
  359. gchar *
  360. gimp_input_device_store_get_device_file (GimpInputDeviceStore *store,
  361. const gchar *udi)
  362. {
  363. GtkTreeIter iter;
  364. GValue value = G_VALUE_INIT;
  365. g_return_val_if_fail (GIMP_IS_INPUT_DEVICE_STORE (store), NULL);
  366. g_return_val_if_fail (udi != NULL, NULL);
  367. if (! store->directinput8)
  368. return NULL;
  369. if (gimp_input_device_store_lookup (store, udi, &iter))
  370. {
  371. gtk_tree_model_get_value (GTK_TREE_MODEL (store),
  372. &iter, COLUMN_IDEVICE, &value);
  373. return g_value_get_pointer (&value);
  374. }
  375. return NULL;
  376. }
  377. GError *
  378. gimp_input_device_store_get_error (GimpInputDeviceStore *store)
  379. {
  380. g_return_val_if_fail (GIMP_IS_INPUT_DEVICE_STORE (store), NULL);
  381. return store->error ? g_error_copy (store->error) : NULL;
  382. }
  383. #endif /* HAVE_DX_DINPUT */