Source merge for Tizen open and remove samsung patent. This uses DPI based scaling.
[platform/core/uifw/e17-extra-config-modules.git] / config-slp / src / e_mod_main.c
1 #include "e.h"
2 #include "e_mod_main.h"
3 #include "Elementary.h"
4
5 #define PI                    3.141592
6 #define BASE_LAYOUT_INCH      4.65
7 #define BASE_LAYOUT_WIDTH_PX  720
8 #define BASE_LAYOUT_HEIGHT_PX 1280
9 #define ROUND_DOUBLE(x)       (round((x)*100)/100)
10 #define MOBILE_PROFILE        "mobile"
11
12 static int _e_elm_config_scale_update(void);
13 static double _e_elm_config_dpi_get(double d_inch, int w_px, int h_px);
14
15 static char fullpath[1024];
16
17 /* this is needed to advertise a label for the module IN the code (not just
18  * the .desktop file) but more specifically the api version it was compiled
19  * for so E can skip modules that are compiled for an incorrect API version
20  * safely) */
21 EAPI E_Module_Api e_modapi =
22 {
23    E_MODULE_API_VERSION, "Elementary Profile Module of Window Manager"
24 };
25
26 EAPI void*
27 e_modapi_init(E_Module* m)
28 {
29    _e_elm_config_scale_update();
30    return m;
31 }
32
33 EAPI int
34 e_modapi_shutdown(E_Module* m)
35 {
36    return 1;
37 }
38
39 EAPI int
40 e_modapi_save(E_Module* m)
41 {
42    /* Do Something */
43    return 1;
44 }
45
46 static double
47 _e_elm_config_dpi_get(double d_inch, int w_px, int h_px)
48 {
49    double dpi;
50
51    dpi = (sqrt((w_px * w_px) + (h_px * h_px))) / d_inch;
52
53    return dpi;
54 }
55
56 static int
57 _file_owner_change(uid_t uid, gid_t gid)
58 {
59    struct stat    statbuf;
60    struct dirent  *dirp;
61    DIR            *dp;
62    char           *ptr;
63    int            ret;
64
65    if (lstat(fullpath, &statbuf) < 0)
66      {
67         fprintf(stderr, "%s : stat error \n", fullpath);
68         return 0;
69      }
70    if (S_ISDIR(statbuf.st_mode) == 0)
71      return 0;
72
73    ptr = fullpath + strlen(fullpath);
74    *ptr++ = '/';
75    *ptr = 0;
76
77    if ((dp = opendir(fullpath)) == NULL)
78      {
79         fprintf(stderr, "can't read %s directory \n", fullpath);
80         return 0;
81      }
82
83    while ((dirp = readdir(dp)) != NULL)
84      {
85         if (strcmp(dirp->d_name, ".") == 0  ||
86             strcmp(dirp->d_name, "..") == 0)
87           continue;
88
89         strcpy(ptr, dirp->d_name);
90         if ((chown(fullpath, uid, gid) == -1))
91           fprintf(stderr, "%s chown error \n", fullpath);
92
93         if ((ret = _file_owner_change(uid, gid)) != 0)
94           break;
95    }
96    *(ptr-1) = 0;
97
98    if (closedir(dp) < 0)
99      fprintf(stderr, "can't close directory %s", fullpath);
100
101    return ret;
102 }
103
104
105 static int
106 _e_mod_config_elm_profile_save(char *profile_name, double scale)
107 {
108    struct dirent *directory;
109    struct stat file_info;
110    DIR *dp;
111    char buf[256];
112    int ret;
113
114    setenv("HOME", "/home/app", 1);
115    elm_init(0, NULL);
116
117    elm_scale_set(scale);
118    elm_finger_size_set(scale * 60);
119
120    dp = opendir("/home/");
121    if (!dp) return 0;
122
123    while((directory = readdir(dp)))
124      {
125         if ((!strcmp(directory->d_name, ".")) || (!strcmp(directory->d_name, ".."))) continue;
126         snprintf(buf, sizeof(buf), "/home/%s", directory->d_name);
127         if ((ret = lstat(buf, &file_info) == -1))
128           {
129              printf("error : can't get file stat \n");
130              continue;
131           }
132         if (S_ISDIR(file_info.st_mode))
133           {
134              setenv("HOME", buf, 1);
135              elm_config_save();
136              snprintf(buf, sizeof(buf), "/home/%s/.elementary", directory->d_name);
137              strcpy(fullpath, buf);
138              if ((chown(fullpath, file_info.st_uid, file_info.st_gid)) == -1)
139                fprintf(stderr, "%s chown error \n", fullpath);
140              _file_owner_change(file_info.st_uid, file_info.st_gid);
141           }
142      }
143    closedir(dp);
144    elm_shutdown();
145
146    return 1;
147 }
148
149 static int
150 _e_elm_config_scale_update (void)
151 {
152    int target_width, target_height, target_width_mm, target_height_mm;
153    double target_inch, scale, target_dpi, base_dpi;
154
155    target_width = 0;
156    target_height = 0;
157    target_width_mm = 0;
158    target_height_mm = 0;
159
160    ecore_x_randr_screen_current_size_get(ecore_x_window_root_first_get(), &target_width, &target_height, &target_width_mm, &target_height_mm);
161    target_inch = ROUND_DOUBLE(sqrt(target_width_mm * target_width_mm + target_height_mm * target_height_mm) / 25.4);
162
163    // Calculate DPI
164    base_dpi = ROUND_DOUBLE(_e_elm_config_dpi_get(BASE_LAYOUT_INCH, BASE_LAYOUT_WIDTH_PX, BASE_LAYOUT_HEIGHT_PX));
165    target_dpi = ROUND_DOUBLE(_e_elm_config_dpi_get(target_inch, target_width, target_height));
166
167      // Calculate Scale factor
168    scale = ROUND_DOUBLE(target_dpi / base_dpi);
169
170    e_config->scale.factor = scale;
171
172    // calculate elementray scale factor
173    _e_mod_config_elm_profile_save(MOBILE_PROFILE, scale);
174
175    system ("/bin/touch /opt/etc/.profile_ready");
176    return 1;
177 }