NVCtrl.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #define NEED_EVENTS
  2. #define NEED_REPLIES
  3. #include <X11/Xlibint.h>
  4. #include <X11/Xutil.h>
  5. #include <X11/extensions/Xext.h>
  6. #include "extutil.h"
  7. #include "NVCtrlLib.h"
  8. #include "nv_control.h"
  9. #if !defined(XTRHEADS)
  10. #warning XTRHEADS not defined -- this libXNVCtrl.a will not be thread safe!
  11. #endif
  12. static XExtensionInfo _nvctrl_ext_info_data;
  13. static XExtensionInfo *nvctrl_ext_info = &_nvctrl_ext_info_data;
  14. static /* const */ char *nvctrl_extension_name = NV_CONTROL_NAME;
  15. #define XNVCTRLCheckExtension(dpy,i,val) \
  16. XextCheckExtension (dpy, i, nvctrl_extension_name, val)
  17. #define XNVCTRLSimpleCheckExtension(dpy,i) \
  18. XextSimpleCheckExtension (dpy, i, nvctrl_extension_name)
  19. static int close_display();
  20. static Bool wire_to_event();
  21. static /* const */ XExtensionHooks nvctrl_extension_hooks = {
  22. NULL, /* create_gc */
  23. NULL, /* copy_gc */
  24. NULL, /* flush_gc */
  25. NULL, /* free_gc */
  26. NULL, /* create_font */
  27. NULL, /* free_font */
  28. close_display, /* close_display */
  29. wire_to_event, /* wire_to_event */
  30. NULL, /* event_to_wire */
  31. NULL, /* error */
  32. NULL, /* error_string */
  33. };
  34. static XEXT_GENERATE_FIND_DISPLAY (find_display, nvctrl_ext_info,
  35. nvctrl_extension_name,
  36. &nvctrl_extension_hooks,
  37. NV_CONTROL_EVENTS, NULL)
  38. static XEXT_GENERATE_CLOSE_DISPLAY (close_display, nvctrl_ext_info)
  39. Bool XNVCTRLQueryExtension (
  40. Display *dpy,
  41. int *event_basep,
  42. int *error_basep
  43. ){
  44. XExtDisplayInfo *info = find_display (dpy);
  45. if (XextHasExtension(info)) {
  46. if (event_basep) *event_basep = info->codes->first_event;
  47. if (error_basep) *error_basep = info->codes->first_error;
  48. return True;
  49. } else {
  50. return False;
  51. }
  52. }
  53. Bool XNVCTRLQueryVersion (
  54. Display *dpy,
  55. int *major,
  56. int *minor
  57. ){
  58. XExtDisplayInfo *info = find_display (dpy);
  59. xnvCtrlQueryExtensionReply rep;
  60. xnvCtrlQueryExtensionReq *req;
  61. if(!XextHasExtension(info))
  62. return False;
  63. XNVCTRLCheckExtension (dpy, info, False);
  64. LockDisplay (dpy);
  65. GetReq (nvCtrlQueryExtension, req);
  66. req->reqType = info->codes->major_opcode;
  67. req->nvReqType = X_nvCtrlQueryExtension;
  68. if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
  69. UnlockDisplay (dpy);
  70. SyncHandle ();
  71. return False;
  72. }
  73. if (major) *major = rep.major;
  74. if (minor) *minor = rep.minor;
  75. UnlockDisplay (dpy);
  76. SyncHandle ();
  77. return True;
  78. }
  79. Bool XNVCTRLIsNvScreen (
  80. Display *dpy,
  81. int screen
  82. ){
  83. XExtDisplayInfo *info = find_display (dpy);
  84. xnvCtrlIsNvReply rep;
  85. xnvCtrlIsNvReq *req;
  86. Bool isnv;
  87. if(!XextHasExtension(info))
  88. return False;
  89. XNVCTRLCheckExtension (dpy, info, False);
  90. LockDisplay (dpy);
  91. GetReq (nvCtrlIsNv, req);
  92. req->reqType = info->codes->major_opcode;
  93. req->nvReqType = X_nvCtrlIsNv;
  94. req->screen = screen;
  95. if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
  96. UnlockDisplay (dpy);
  97. SyncHandle ();
  98. return False;
  99. }
  100. isnv = rep.isnv;
  101. UnlockDisplay (dpy);
  102. SyncHandle ();
  103. return isnv;
  104. }
  105. void XNVCTRLSetAttribute (
  106. Display *dpy,
  107. int screen,
  108. unsigned int display_mask,
  109. unsigned int attribute,
  110. int value
  111. ){
  112. XExtDisplayInfo *info = find_display (dpy);
  113. xnvCtrlSetAttributeReq *req;
  114. XNVCTRLSimpleCheckExtension (dpy, info);
  115. LockDisplay (dpy);
  116. GetReq (nvCtrlSetAttribute, req);
  117. req->reqType = info->codes->major_opcode;
  118. req->nvReqType = X_nvCtrlSetAttribute;
  119. req->screen = screen;
  120. req->display_mask = display_mask;
  121. req->attribute = attribute;
  122. req->value = value;
  123. UnlockDisplay (dpy);
  124. SyncHandle ();
  125. }
  126. Bool XNVCTRLQueryAttribute (
  127. Display *dpy,
  128. int screen,
  129. unsigned int display_mask,
  130. unsigned int attribute,
  131. int *value
  132. ){
  133. XExtDisplayInfo *info = find_display (dpy);
  134. xnvCtrlQueryAttributeReply rep;
  135. xnvCtrlQueryAttributeReq *req;
  136. Bool exists;
  137. if(!XextHasExtension(info))
  138. return False;
  139. XNVCTRLCheckExtension (dpy, info, False);
  140. LockDisplay (dpy);
  141. GetReq (nvCtrlQueryAttribute, req);
  142. req->reqType = info->codes->major_opcode;
  143. req->nvReqType = X_nvCtrlQueryAttribute;
  144. req->screen = screen;
  145. req->display_mask = display_mask;
  146. req->attribute = attribute;
  147. if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
  148. UnlockDisplay (dpy);
  149. SyncHandle ();
  150. return False;
  151. }
  152. if (value) *value = rep.value;
  153. exists = rep.flags;
  154. UnlockDisplay (dpy);
  155. SyncHandle ();
  156. return exists;
  157. }
  158. Bool XNVCTRLQueryStringAttribute (
  159. Display *dpy,
  160. int screen,
  161. unsigned int display_mask,
  162. unsigned int attribute,
  163. char **ptr
  164. ){
  165. XExtDisplayInfo *info = find_display (dpy);
  166. xnvCtrlQueryStringAttributeReply rep;
  167. xnvCtrlQueryStringAttributeReq *req;
  168. Bool exists;
  169. int length, numbytes, slop;
  170. if (!ptr) return False;
  171. if(!XextHasExtension(info))
  172. return False;
  173. XNVCTRLCheckExtension (dpy, info, False);
  174. LockDisplay (dpy);
  175. GetReq (nvCtrlQueryStringAttribute, req);
  176. req->reqType = info->codes->major_opcode;
  177. req->nvReqType = X_nvCtrlQueryStringAttribute;
  178. req->screen = screen;
  179. req->display_mask = display_mask;
  180. req->attribute = attribute;
  181. if (!_XReply (dpy, (xReply *) &rep, 0, False)) {
  182. UnlockDisplay (dpy);
  183. SyncHandle ();
  184. return False;
  185. }
  186. length = rep.length;
  187. numbytes = rep.n;
  188. slop = numbytes & 3;
  189. *ptr = (char *) Xmalloc(numbytes);
  190. if (! *ptr) {
  191. _XEatData(dpy, length);
  192. UnlockDisplay (dpy);
  193. SyncHandle ();
  194. return False;
  195. } else {
  196. _XRead(dpy, (char *) *ptr, numbytes);
  197. if (slop) _XEatData(dpy, 4-slop);
  198. }
  199. exists = rep.flags;
  200. UnlockDisplay (dpy);
  201. SyncHandle ();
  202. return exists;
  203. }
  204. Bool XNVCTRLQueryValidAttributeValues (
  205. Display *dpy,
  206. int screen,
  207. unsigned int display_mask,
  208. unsigned int attribute,
  209. NVCTRLAttributeValidValuesRec *values
  210. ){
  211. XExtDisplayInfo *info = find_display (dpy);
  212. xnvCtrlQueryValidAttributeValuesReply rep;
  213. xnvCtrlQueryValidAttributeValuesReq *req;
  214. Bool exists;
  215. if (!values) return False;
  216. if(!XextHasExtension(info))
  217. return False;
  218. XNVCTRLCheckExtension (dpy, info, False);
  219. LockDisplay (dpy);
  220. GetReq (nvCtrlQueryValidAttributeValues, req);
  221. req->reqType = info->codes->major_opcode;
  222. req->nvReqType = X_nvCtrlQueryValidAttributeValues;
  223. req->screen = screen;
  224. req->display_mask = display_mask;
  225. req->attribute = attribute;
  226. if (!_XReply (dpy, (xReply *) &rep, 0, xTrue)) {
  227. UnlockDisplay (dpy);
  228. SyncHandle ();
  229. return False;
  230. }
  231. exists = rep.flags;
  232. values->type = rep.attr_type;
  233. if (rep.attr_type == ATTRIBUTE_TYPE_RANGE) {
  234. values->u.range.min = rep.min;
  235. values->u.range.max = rep.max;
  236. }
  237. if (rep.attr_type == ATTRIBUTE_TYPE_INT_BITS) {
  238. values->u.bits.ints = rep.bits;
  239. }
  240. values->permissions = rep.perms;
  241. UnlockDisplay (dpy);
  242. SyncHandle ();
  243. return exists;
  244. }
  245. Bool XNVCtrlSelectNotify (
  246. Display *dpy,
  247. int screen,
  248. int type,
  249. Bool onoff
  250. ){
  251. XExtDisplayInfo *info = find_display (dpy);
  252. xnvCtrlSelectNotifyReq *req;
  253. if(!XextHasExtension (info))
  254. return False;
  255. XNVCTRLCheckExtension (dpy, info, False);
  256. LockDisplay (dpy);
  257. GetReq (nvCtrlSelectNotify, req);
  258. req->reqType = info->codes->major_opcode;
  259. req->nvReqType = X_nvCtrlSelectNotify;
  260. req->screen = screen;
  261. req->notifyType = type;
  262. req->onoff = onoff;
  263. UnlockDisplay (dpy);
  264. SyncHandle ();
  265. return True;
  266. }
  267. static Bool wire_to_event (Display *dpy, XEvent *host, xEvent *wire)
  268. {
  269. XExtDisplayInfo *info = find_display (dpy);
  270. XNVCtrlEvent *re = (XNVCtrlEvent *) host;
  271. xnvctrlEvent *event = (xnvctrlEvent *) wire;
  272. XNVCTRLCheckExtension (dpy, info, False);
  273. switch ((event->u.u.type & 0x7F) - info->codes->first_event) {
  274. case ATTRIBUTE_CHANGED_EVENT:
  275. re->attribute_changed.type = event->u.u.type & 0x7F;
  276. re->attribute_changed.serial =
  277. _XSetLastRequestRead(dpy, (xGenericReply*) event);
  278. re->attribute_changed.send_event = ((event->u.u.type & 0x80) != 0);
  279. re->attribute_changed.display = dpy;
  280. re->attribute_changed.time = event->u.attribute_changed.time;
  281. re->attribute_changed.screen = event->u.attribute_changed.screen;
  282. re->attribute_changed.display_mask =
  283. event->u.attribute_changed.display_mask;
  284. re->attribute_changed.attribute = event->u.attribute_changed.attribute;
  285. re->attribute_changed.value = event->u.attribute_changed.value;
  286. break;
  287. default:
  288. return False;
  289. }
  290. return True;
  291. }