~ubuntu-branches/ubuntu/oneiric/cairo-dock-plug-ins/oneiric-updates

« back to all changes in this revision

Viewing changes to nVidia/src/applet-nvidia.c

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2009-08-26 21:07:39 UTC
  • Revision ID: james.westby@ubuntu.com-20090826210739-gyjuuqezrzuluao4
Tags: upstream-2.0.8.1
Import upstream version 2.0.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
* This file is a part of the Cairo-Dock project
 
3
*
 
4
* Copyright : (C) see the 'copyright' file.
 
5
* E-mail    : see the 'copyright' file.
 
6
*
 
7
* This program is free software; you can redistribute it and/or
 
8
* modify it under the terms of the GNU General Public License
 
9
* as published by the Free Software Foundation; either version 3
 
10
* of the License, or (at your option) any later version.
 
11
*
 
12
* This program is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
* GNU General Public License for more details.
 
16
* You should have received a copy of the GNU General Public License
 
17
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
/******************************************************************************
 
21
 
 
22
This file is a part of the cairo-dock program, 
 
23
released under the terms of the GNU General Public License.
 
24
 
 
25
Written by Rémy Robertson (for any bug report, please mail me to changfu@cairo-dock.org)
 
26
Fabrice Rey <fabounet@users.berlios.de>
 
27
 
 
28
******************************************************************************/
 
29
#define _BSD_SOURCE
 
30
#include <stdlib.h>
 
31
#include <stdio.h>
 
32
#include <string.h>
 
33
#include <unistd.h>
 
34
#include <glib/gi18n.h>
 
35
#include <glib/gstdio.h>
 
36
 
 
37
#include "applet-struct.h"
 
38
#include "applet-notifications.h"
 
39
#include "applet-draw.h"
 
40
#include "applet-nvidia.h"
 
41
 
 
42
 
 
43
static char  *s_cTmpFileConfig = NULL;
 
44
static char  *s_cTmpFile = NULL;
 
45
 
 
46
 
 
47
//Récupération de la température
 
48
void cd_nvidia_acquisition (void) {
 
49
        s_cTmpFile = g_strdup ("/tmp/nvidia.XXXXXX");
 
50
        int fds =mkstemp (s_cTmpFile);
 
51
        if (fds == -1) {
 
52
                g_free (s_cTmpFile);
 
53
                s_cTmpFile = NULL;
 
54
                return;
 
55
        }
 
56
        gchar *cCommand = g_strdup_printf ("bash %s/nvidia %s", MY_APPLET_SHARE_DATA_DIR, s_cTmpFile);
 
57
        system (cCommand);
 
58
        g_free (cCommand);
 
59
        close(fds);
 
60
}
 
61
 
 
62
void cd_nvidia_read_data (void) {
 
63
        cd_nvidia_acquisition ();
 
64
        if (s_cTmpFile == NULL)
 
65
                return ;
 
66
        gchar *cContent = NULL;
 
67
        gsize length=0;
 
68
        GError *erreur = NULL;
 
69
        gint iGpuTemp;
 
70
        g_file_get_contents(s_cTmpFile, &cContent, &length, &erreur);
 
71
        if (erreur != NULL) {
 
72
                cd_warning("nVidia : %s", erreur->message);
 
73
                g_error_free(erreur);
 
74
                erreur = NULL;
 
75
                myData.bAcquisitionOK = FALSE;
 
76
        }
 
77
        else {
 
78
                iGpuTemp = atoi (cContent);
 
79
                if (iGpuTemp == 0) {
 
80
                        cd_warning("nVidia : couldn't acquire GPU temperature\n is 'nvidia-settings' installed on your system and its version >= 1.0 ?");
 
81
                        myData.bAcquisitionOK = FALSE;
 
82
                }
 
83
                else {
 
84
                        myData.bAcquisitionOK = TRUE;
 
85
                        myData.pGPUData.iGPUTemp = iGpuTemp;
 
86
                }
 
87
        }
 
88
        g_remove (s_cTmpFile);
 
89
        g_free (s_cTmpFile);
 
90
        s_cTmpFile = NULL;
 
91
}
 
92
 
 
93
gboolean cd_nvidia_update_from_data (void) {
 
94
        if (myData.bAcquisitionOK) {
 
95
                cd_nvidia_draw_icon ();
 
96
                return TRUE;
 
97
        }
 
98
        else {
 
99
                cd_nvidia_draw_no_data ();
 
100
                cd_warning ("Couldn't get infos from nvidia setting (may not be installed or too old), halt.");
 
101
                return FALSE;  // pas la peine d'insister.
 
102
        }
 
103
        
 
104
}
 
105
 
 
106
 
 
107
 
 
108
//Récupération de la config
 
109
void cd_nvidia_config_acquisition (void) {
 
110
        s_cTmpFileConfig = g_strdup ("/tmp/nvidia-config.XXXXXX");
 
111
        int fds =mkstemp (s_cTmpFileConfig);
 
112
        if (fds == -1) {
 
113
                g_free (s_cTmpFileConfig);
 
114
                s_cTmpFileConfig = NULL;
 
115
                return;
 
116
        }
 
117
        gchar *cCommand = g_strdup_printf ("bash %s/nvidia-config %s", MY_APPLET_SHARE_DATA_DIR, s_cTmpFileConfig);
 
118
        system (cCommand);
 
119
        g_free (cCommand);
 
120
        close(fds);
 
121
}
 
122
 
 
123
static gboolean _nvidia_get_values_from_file (gchar *cContent) {
 
124
        cd_nvidia_config_acquisition ();
 
125
        gchar **cInfopipesList = g_strsplit (cContent, "\n", -1);
 
126
        gchar *cOneInfopipe;
 
127
        gint i=0;
 
128
        //g_print ("%s ()\n", __func__);
 
129
        
 
130
        g_free (myData.pGPUData.cGPUName);
 
131
        myData.pGPUData.cGPUName = NULL;
 
132
        g_free (myData.pGPUData.cDriverVersion);
 
133
        myData.pGPUData.cDriverVersion = NULL;
 
134
        
 
135
        for (i = 0; cInfopipesList[i] != NULL; i ++) {
 
136
                cOneInfopipe = cInfopipesList[i];
 
137
                if (*cOneInfopipe == '\0')
 
138
                        continue;
 
139
                
 
140
                if ((i == 0) && (strcmp (cOneInfopipe,"nvidia") == 0)) {
 
141
                        g_strfreev (cInfopipesList);
 
142
                        return FALSE;
 
143
                }
 
144
                else {
 
145
                        if (i == 0) {
 
146
                                gchar *str = g_strstr_len (cOneInfopipe, strlen (cOneInfopipe), "version");
 
147
                                if (str != NULL) {
 
148
                                        str += 7;
 
149
                                        while (*str == ' ')
 
150
                                                str ++;
 
151
                                        gchar *str2 = strchr (str, ' ');
 
152
                                        if (str2 != NULL)
 
153
                                                *str2 = '\0';
 
154
                                        int iMajorVersion=0, iMinorVersion=0, iMicroVersion=0;
 
155
                                        cairo_dock_get_version_from_string (str, &iMajorVersion, &iMinorVersion, &iMicroVersion);
 
156
                                        /*if (iMajorVersion == 0 || (iMajorVersion == 1 && iMinorVersion < 0)) { /// A confirmer ...
 
157
                                                myData.bSettingsTooOld == TRUE;
 
158
                                                cd_warning ("Attention : your nvidia-settings's version is too old (%d.%d.%d)", iMajorVersion, iMinorVersion, iMicroVersion);
 
159
                                                break ;
 
160
                                        }*/
 
161
                                }
 
162
                        }
 
163
                        else if (i == 1) { //GPU Name
 
164
                                myData.pGPUData.cGPUName = g_strdup (cOneInfopipe);
 
165
                                gchar *str = strchr (myData.pGPUData.cGPUName, ')');
 
166
                                if (str != NULL)
 
167
                                        *str = '\0';
 
168
                        }
 
169
                        else if (i == 2) { //Video Ram
 
170
                                myData.pGPUData.iVideoRam = atoi (cOneInfopipe);
 
171
                                myData.pGPUData.iVideoRam = myData.pGPUData.iVideoRam >> 10;  // passage en Mo.
 
172
                        }
 
173
                        else if (i == 3) { //Driver Version
 
174
                                myData.pGPUData.cDriverVersion = g_strdup (cOneInfopipe);
 
175
                        }
 
176
                }
 
177
        }
 
178
        
 
179
        cd_debug ("nVidia %s %dMB %sV %d°C", myData.pGPUData.cGPUName, myData.pGPUData.iVideoRam, myData.pGPUData.cDriverVersion, myData.pGPUData.iGPUTemp);
 
180
        
 
181
        g_strfreev (cInfopipesList);
 
182
        return TRUE;
 
183
}
 
184
 
 
185
void cd_nvidia_config_read_data (void) {
 
186
        //g_print ("%s ()\n", __func__);
 
187
        if (s_cTmpFileConfig == NULL)
 
188
                return ;
 
189
        gchar *cContent = NULL;
 
190
        gsize length=0;
 
191
        GError *erreur = NULL;
 
192
        g_file_get_contents(s_cTmpFileConfig, &cContent, &length, &erreur);
 
193
        if (erreur != NULL) {
 
194
                cd_warning("nVidia : %s", erreur->message);
 
195
                g_error_free(erreur);
 
196
                erreur = NULL;
 
197
                myData.bAcquisitionOK = FALSE;
 
198
        }
 
199
        else {
 
200
                gboolean bAcquisitionOK = _nvidia_get_values_from_file (cContent);
 
201
                g_free (cContent);
 
202
        }
 
203
        g_remove (s_cTmpFileConfig);
 
204
        g_free (s_cTmpFileConfig);
 
205
        s_cTmpFileConfig = NULL;
 
206
}
 
207
 
 
208
gboolean cd_nvidia_config_update_from_data (void) {
 
209
        //g_print ("%s ()\n", __func__);
 
210
        if (myConfig.bCardName) {
 
211
                CD_APPLET_SET_NAME_FOR_MY_ICON (myData.pGPUData.cGPUName);
 
212
        }
 
213
        return TRUE;
 
214
}