drm/resources.rs
1// Copyright 2016 The libdrm-rs project developers
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4// and associated documentation files (the "Software"), to deal in the Software without
5// restriction, including without limitation the rights to use, copy, modify, merge, publish,
6// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7// Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17
18use ffi;
19use encoder;
20use connector;
21use crtc;
22
23/// Resources structure.
24/// Can be obtained by call to `drm_mode::get_mode_resources`
25pub struct Resources {
26 resources: ffi::xf86drm_mode::drmModeResPtr,
27}
28
29/// General methods
30impl Resources {
31 /// `Resources` constructor.
32 /// Does not check if passed arguments are valid.
33 pub fn new(resources: ffi::xf86drm_mode::drmModeResPtr) -> Self {
34 Resources { resources: resources }
35 }
36}
37
38/// Getters for original members
39impl Resources {
40 /// Returns count of frame buffers.
41 #[inline]
42 pub fn get_count_fbs(&self) -> i32 {
43 unsafe { (*self.resources).count_fbs }
44 }
45
46 /// Returns count of CRTCs
47 #[inline]
48 pub fn get_count_crtcs(&self) -> i32 {
49 unsafe { (*self.resources).count_crtcs }
50 }
51
52 /// Return count of connectors.
53 #[inline]
54 pub fn get_count_connectors(&self) -> i32 {
55 unsafe { (*self.resources).count_connectors }
56 }
57
58 /// Return count of encoders.
59 #[inline]
60 pub fn get_count_encoders(&self) -> i32 {
61 unsafe { (*self.resources).count_encoders }
62 }
63
64 /// Return vector of CRTC ids.
65 pub fn get_crtcs(&self) -> Vec<crtc::CrtcId> {
66 let count = self.get_count_crtcs();
67 let mut vec = Vec::with_capacity(count as usize);
68 for pos in 0..count as isize {
69 vec.push(unsafe { *(*self.resources).crtcs.offset(pos) });
70 }
71 vec
72 }
73
74 /// Return vector of encoder ids.
75 pub fn get_encoders(&self) -> Vec<encoder::EncoderId> {
76 let count = self.get_count_encoders();
77 let mut vec = Vec::with_capacity(count as usize);
78 for pos in 0..count as isize {
79 vec.push(unsafe { *(*self.resources).encoders.offset(pos) });
80 }
81 vec
82 }
83
84 /// Return vector of connector ids.
85 pub fn get_connectors(&self) -> Vec<connector::ConnectorId> {
86 let count = self.get_count_connectors();
87 let mut vec = Vec::with_capacity(count as usize);
88 for pos in 0..count as isize {
89 vec.push(unsafe { *(*self.resources).connectors.offset(pos) });
90 }
91 vec
92 }
93}
94
95impl Drop for Resources {
96 fn drop(&mut self) {
97 unsafe { ffi::xf86drm_mode::drmModeFreeResources(self.resources) };
98 }
99}