Tizen 2.1 base
[adaptation/xorg/driver/xserver-xorg-video-emulfb.git] / src / util / fbdev_util.c
1 /**************************************************************************
2
3 xserver-xorg-video-emulfb
4
5 Copyright 2010 - 2011 Samsung Electronics co., Ltd. All Rights Reserved.
6
7 Contact: YoungHoon Jung <yhoon.jung@samsung.com>
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sub license, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice (including the
18 next paragraph) shall be included in all copies or substantial portions
19 of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
24 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
25 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 **************************************************************************/
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/shm.h>
35 #include <sys/ipc.h>
36
37 #include "X11/XWDFile.h"
38 #include "fbdev.h"
39 #include "fbdev_util.h"
40
41 int fbdev_util_dump_raw(const char * file, const void * data, int width, int height)
42 {
43         unsigned int * blocks;
44
45         FILE * fp = fopen(file, "w+");
46         if (fp == NULL)
47                 return 0;
48
49         blocks = (unsigned int *) data;
50         fwrite(blocks, 4, width*height, fp);
51
52         fclose(fp);
53
54         return 1;
55 }
56
57 #ifndef RR_Rotate_All
58 #define RR_Rotate_All   (RR_Rotate_0|RR_Rotate_90|RR_Rotate_180|RR_Rotate_270)
59 #endif
60
61 int fbdev_util_degree_to_rotate(int degree)
62 {
63         int rotate;
64
65         switch(degree)
66         {
67         case 0:
68                 rotate = RR_Rotate_0;
69                 break;
70         case 90:
71                 rotate = RR_Rotate_90;
72                 break;
73         case 180:
74                 rotate = RR_Rotate_180;
75                 break;
76         case 270:
77                 rotate = RR_Rotate_270;
78                 break;
79         default:
80                 rotate = 0;     /* ERROR */
81                 break;
82         }
83
84         return rotate;
85 }
86
87 int fbdev_util_rotate_to_degree(int rotate)
88 {
89         int degree;
90
91         switch(rotate & RR_Rotate_All)
92         {
93         case RR_Rotate_0:
94                 degree = 0;
95                 break;
96         case RR_Rotate_90:
97                 degree = 90;
98                 break;
99         case RR_Rotate_180:
100                 degree = 180;
101                 break;
102         case RR_Rotate_270:
103                 degree = 270;
104                 break;
105         default:
106                 degree = -1;    /* ERROR */
107                 break;
108         }
109
110         return degree;
111 }
112
113 static int
114 _fbdev_util_rotate_to_int(int rot)
115 {
116         switch(rot & RR_Rotate_All)
117         {
118         case RR_Rotate_0:
119                 return 0;
120         case RR_Rotate_90:
121                 return 1;
122         case RR_Rotate_180:
123                 return 2;
124         case RR_Rotate_270:
125                 return 3;
126         }
127
128         return 0;
129 }
130
131 int fbdev_util_rotate_add(int rot_a, int rot_b)
132 {
133         int a = _fbdev_util_rotate_to_int(rot_a);
134         int b = _fbdev_util_rotate_to_int(rot_b);
135
136         return (int)((1 << ((a+b)%4))&RR_Rotate_All);
137 }
138
139 const PropertyPtr
140 fbdev_util_get_window_property(WindowPtr pWin, const char* prop_name)
141 {
142         int rc;
143         Mask prop_mode = DixReadAccess;
144         Atom property;
145         PropertyPtr pProp;
146
147         property = MakeAtom(prop_name, strlen(prop_name), FALSE);
148         if(property == None)
149                 return NULL;
150
151         rc = dixLookupProperty(&pProp, pWin, property, serverClient, prop_mode);
152         if (rc == Success && pProp->data)
153         {
154                 return pProp;
155         }
156
157         return NULL;
158 }
159
160
161 void
162 fbdev_util_rotate_rect (int xres,
163                         int yres,
164                         int src_rot,
165                         int dst_rot,
166                         xRectangle *src)
167 {
168         int diff;
169         xRectangle temp;
170
171         return_if_fail (src != NULL);
172
173         if (src_rot == dst_rot)
174                 return;
175
176         diff = (dst_rot - src_rot);
177         if (diff < 0)
178                 diff = 360 + diff;
179
180         if (src_rot % 180 && diff % 180)
181                 SWAP (xres, yres);
182
183         switch (diff)
184         {
185         case 270:
186                 temp.x = yres - (src->y + src->height);
187                 temp.y = src->x;
188                 temp.width  = src->height;
189                 temp.height = src->width;
190                 break;
191         case 180:
192                 temp.x = xres  - (src->x + src->width);
193                 temp.y = yres - (src->y + src->height);
194                 temp.width  = src->width;
195                 temp.height = src->height;
196                 break;
197         case 90:
198                 temp.x = src->y;
199                 temp.y = xres - (src->x + src->width);
200                 temp.width  = src->height;
201                 temp.height = src->width;
202                 break;
203         default:
204                 temp.x = src->x;
205                 temp.y = src->y;
206                 temp.width  = src->width;
207                 temp.height = src->height;
208                 break;
209         }
210
211         *src = temp;
212 }
213
214 void
215 fbdev_util_align_rect (int src_w, int src_h, int dst_w, int dst_h, xRectangle *fit, Bool hw)
216 {
217         int fit_width;
218         int fit_height;
219         float rw, rh, max;
220
221         if (!fit)
222                 return;
223
224         return_if_fail (src_w > 0 && src_h > 0);
225         return_if_fail (dst_w > 0 && dst_h > 0);
226
227         rw = (float)src_w / dst_w;
228         rh = (float)src_h / dst_h;
229         max = MAX (rw, rh);
230
231         fit_width = src_w / max;
232         fit_height = src_h / max;
233
234         if (hw)
235                 fit_width &= (~0x3);
236
237         fit->x = (dst_w - fit_width) / 2;
238         fit->y = (dst_h - fit_height) / 2;
239         fit->width = fit_width;
240         fit->height = fit_height;
241 }
242
243
244 void
245 drvlog (const char * f, ...)
246 {
247     va_list args;
248     char temp[1024];
249
250     va_start (args, f);
251     vsnprintf (temp, sizeof (temp), f, args);
252     va_end (args);
253
254     fwrite (temp, strlen (temp), 1, stderr);
255 }