client_impl.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. Copyright (c) 2017, Feral Interactive
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of Feral Interactive nor the names of its contributors
  12. may be used to endorse or promote products derived from this software
  13. without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <dlfcn.h>
  30. #include <errno.h>
  31. #include <string.h>
  32. #include <systemd/sd-bus.h>
  33. // Storage for error strings
  34. static char error_string[512] = {};
  35. // Simple requestor function for a gamemode
  36. static int gamemode_request( const char* function )
  37. {
  38. sd_bus_message* msg = NULL;
  39. sd_bus* bus = NULL;
  40. int result = -1;
  41. // Open the user bus
  42. int ret = sd_bus_open_user(&bus);
  43. if( ret < 0 )
  44. snprintf( error_string, sizeof(error_string), "Could not connect to bus: %s", strerror(-ret) );
  45. else
  46. {
  47. // Attempt to send the requested function
  48. ret = sd_bus_call_method( bus,
  49. "com.feralinteractive.GameMode",
  50. "/com/feralinteractive/GameMode",
  51. "com.feralinteractive.GameMode",
  52. function,
  53. NULL,
  54. &msg,
  55. "i",
  56. getpid() );
  57. if( ret < 0 )
  58. snprintf( error_string, sizeof(error_string), "Could not call method on bus: %s", strerror(-ret) );
  59. else
  60. {
  61. // Read the reply
  62. ret = sd_bus_message_read( msg, "i", &result );
  63. if( ret < 0 )
  64. snprintf( error_string, sizeof(error_string), "Failure to parse response: %s", strerror(-ret) );
  65. }
  66. }
  67. return result;
  68. }
  69. // Get the error string
  70. extern const char* real_gamemode_error_string()
  71. {
  72. return error_string;
  73. }
  74. // Wrapper to call RegisterGame
  75. extern int real_gamemode_request_start()
  76. {
  77. return gamemode_request( "RegisterGame" );
  78. }
  79. // Wrapper to call UnregisterGame
  80. extern int real_gamemode_request_end()
  81. {
  82. return gamemode_request( "UnregisterGame" );
  83. }