summaryrefslogtreecommitdiff
path: root/src/modules/sysinfo/cpuclock/cpuclock.c
blob: e786fe613842e3d99a91698b336fb1d5cccc9228 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
#include "cpuclock.h"
#if defined(__OpenBSD__) || defined(__NetBSD__)
#include <sys/param.h>
#include <sys/sysctl.h>
#endif

#if defined(__FreeBSD__) || defined(__DragonFly__)
 #include <sys/types.h>
 #include <sys/sysctl.h>
#endif

EINTERN void _cpuclock_poll_interval_update(Instance *inst);

typedef struct _Thread_Config Thread_Config;
struct _Thread_Config
{
   int                  interval;
   Instance            *inst;
   E_Powersave_Sleeper *sleeper;
};

typedef struct _Pstate_Config Pstate_Config;
struct _Pstate_Config
{
   Instance *inst;
   int       min;
   int       max;
   int       turbo;
};

static Cpu_Status *
_cpuclock_status_new(void)
{
   Cpu_Status *s;

   s = E_NEW(Cpu_Status, 1);
   if (!s) return NULL;
   s->active = -1;
   return s;
}

static void
_cpuclock_status_free(Cpu_Status *s)
{
   Eina_List *l;

   if (eina_list_count(s->frequencies)) eina_list_free(s->frequencies);
   if (s->governors)
     {
        for (l = s->governors; l; l = l->next)
          E_FREE(l->data);
        eina_list_free(s->governors);
     }
   E_FREE(s->cur_governor);
   if (s->orig_governor) eina_stringshare_del(s->orig_governor);
   E_FREE(s);
}

static int
_cpuclock_cb_sort(const void *item1, const void *item2)
{
   int a, b;

   a = (long)item1;
   b = (long)item2;
   if (a < b) return -1;
   else if (a > b)
     return 1;
   return 0;
}

#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
static void
_cpuclock_set_thread_frequency(void *data, Ecore_Thread *th EINA_UNUSED)
{
   const char *freq = data;
   int frequency = atoi(freq);
   _cpuclock_sysctl_frequency(frequency);
}

static void
_cpuclock_set_thread_done(void *data EINA_UNUSED, Ecore_Thread *th EINA_UNUSED)
{
   return;
}

#endif

void
_cpuclock_set_governor(const char *governor)
{
#if defined(__FreeBSD__) || defined(__OpenBSD__)
   return;
#endif
   char buf[4096], exe[4096];
   struct stat st;

   snprintf(exe, 4096, "%s/%s/cpuclock_sysfs",
            e_module_dir_get(sysinfo_config->module), MODULE_ARCH);
   if (stat(exe, &st) < 0) return;

   snprintf(buf, sizeof(buf),
            "%s %s %s", exe, "governor", governor);
   system(buf);
}

void
_cpuclock_set_frequency(int frequency)
{
   char buf[4096];

#if defined(__FreeBSD__)
   frequency /= 1000;
#endif

   snprintf(buf, sizeof(buf), "%i", frequency);
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
   const char *freq;
   freq = eina_stringshare_add(buf);
   ecore_thread_run(_cpuclock_set_thread_frequency, _cpuclock_set_thread_done, NULL, freq);
#else
   struct stat st;
   char exe[4096];
   snprintf(exe, 4096, "%s/%s/cpuclock_sysfs",
            e_module_dir_get(sysinfo_config->module), MODULE_ARCH);
   if (stat(exe, &st) < 0) return;
   snprintf(buf, sizeof(buf),
            "%s %s %i", exe, "frequency", frequency);
   system(buf);
#endif
}

void
_cpuclock_set_pstate(int min, int max, int turbo)
{
#if defined(__FreeBSD__) || defined(__OpenBSD__)
   return;
#endif
   char buf[4096], exe[4096];
   struct stat st;

   snprintf(exe, 4096, "%s/%s/cpuclock_sysfs",
            e_module_dir_get(sysinfo_config->module), MODULE_ARCH);
   if (stat(exe, &st) < 0) return;
   snprintf(buf, sizeof(buf),
            "%s %s %i %i %i", exe, "pstate", min, max, turbo);
   system(buf);
}

static void
_cpuclock_face_cb_set_frequency(void *data, Evas_Object *obj EINA_UNUSED, const char *emission, const char *src EINA_UNUSED)
{
   Eina_List *l;
   int next_frequency = 0;
   Instance *inst = data;

   for (l = inst->cfg->cpuclock.status->frequencies; l; l = l->next)
     {
        if (inst->cfg->cpuclock.status->cur_frequency == (long)l->data)
          {
             if (!strcmp(emission, "e,action,frequency,increase"))
               {
                  if (l->next) next_frequency = (long)l->next->data;
                  break;
               }
             else if (!strcmp(emission, "e,action,frequency,decrease"))
               {
                  if (l->prev) next_frequency = (long)l->prev->data;
                  break;
               }
             else
               break;
          }
     }
   if (inst->cfg->cpuclock.status->can_set_frequency && next_frequency)
     _cpuclock_set_frequency(next_frequency);
}

static void
_cpuclock_face_cb_set_governor(void *data, Evas_Object *obj EINA_UNUSED, const char *emission EINA_UNUSED, const char *src EINA_UNUSED)
{
   Eina_List *l;
   char *next_governor = NULL;
   Instance *inst = data;

   for (l = inst->cfg->cpuclock.status->governors; l; l = l->next)
     {
        if (!strcmp(l->data, inst->cfg->cpuclock.status->cur_governor))
          {
             if (l->next)
               next_governor = l->next->data;
             else
               next_governor = inst->cfg->cpuclock.status->governors->data;
             break;
          }
     }
   if (next_governor) _cpuclock_set_governor(next_governor);
}

static Eina_Bool
_cpuclock_event_cb_powersave(void *data, int type, void *event)
{
   Instance *inst = data;
   E_Event_Powersave_Update *ev;
   Eina_List *l;
   Eina_Bool has_powersave = EINA_FALSE;
   Eina_Bool has_conservative = EINA_FALSE;

   if (type != E_EVENT_POWERSAVE_UPDATE) return ECORE_CALLBACK_PASS_ON;
   if (!inst->cfg->cpuclock.auto_powersave) return ECORE_CALLBACK_PASS_ON;

   ev = event;
   if (!inst->cfg->cpuclock.status->orig_governor)
     inst->cfg->cpuclock.status->orig_governor = eina_stringshare_add(inst->cfg->cpuclock.status->cur_governor);

   for (l = inst->cfg->cpuclock.status->governors; l; l = l->next)
     {
        if (!strcmp(l->data, "conservative"))
          has_conservative = EINA_TRUE;
        else if (!strcmp(l->data, "powersave"))
          has_powersave = EINA_TRUE;
        else if (!strcmp(l->data, "interactive"))
          has_powersave = EINA_TRUE;
     }

   switch (ev->mode)
     {
      case E_POWERSAVE_MODE_NONE:
      case E_POWERSAVE_MODE_LOW:
        _cpuclock_set_governor(inst->cfg->cpuclock.status->orig_governor);
        eina_stringshare_del(inst->cfg->cpuclock.status->orig_governor);
        inst->cfg->cpuclock.status->orig_governor = NULL;
        break;

      case E_POWERSAVE_MODE_MEDIUM:
      case E_POWERSAVE_MODE_HIGH:
        if ((inst->cfg->cpuclock.powersave_governor) || (has_conservative))
          {
             if (inst->cfg->cpuclock.powersave_governor)
               _cpuclock_set_governor(inst->cfg->cpuclock.powersave_governor);
             else
               _cpuclock_set_governor("conservative");
             break;
          }
        EINA_FALLTHROUGH;
      /* no break */

      case E_POWERSAVE_MODE_EXTREME:
      default:
        if (has_powersave)
          _cpuclock_set_governor("powersave");
        break;
     }

   return ECORE_CALLBACK_PASS_ON;
}

static Evas_Object *
_cpuclock_configure_cb(Evas_Object *g)
{
   Instance *inst = evas_object_data_get(g, "Instance");

   if (!sysinfo_config) return NULL;
   if (inst->cfg->cpuclock.popup) return NULL;
   return cpuclock_configure(inst);
}

static void
_cpuclock_popup_dismissed(void *data, Evas_Object *obj, void *event_info EINA_UNUSED)
{
   Instance *inst = data;
   E_FREE_FUNC(obj, evas_object_del);

   inst->cfg->cpuclock.popup = NULL;
   inst->cfg->cpuclock.popup_label = NULL;
}

static void
_cpuclock_popup_deleted(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Instance *inst = data;
   inst->cfg->cpuclock.popup = NULL;
}

static Evas_Object *
_cpuclock_popup_create(Instance *inst)
{
   Evas_Object *popup, *box, *label;
   double f = inst->cfg->cpuclock.status->cur_frequency;
   char buf[100], *u;

   if (f < 1000000)
     {
        f += 500;
        f /= 1000;
        u = _("MHz");
     }
   else
     {
        f += 50000;
        f /= 1000000;
        u = _("GHz");
     }

   popup = elm_ctxpopup_add(e_comp->elm);
   elm_object_style_set(popup, "noblock");
   evas_object_smart_callback_add(popup, "dismissed",
                                  _cpuclock_popup_dismissed, inst);
   evas_object_event_callback_add(popup, EVAS_CALLBACK_DEL,
                                  _cpuclock_popup_deleted, inst);

   box = elm_box_add(popup);
   elm_box_horizontal_set(box, EINA_FALSE);
   E_EXPAND(box); E_FILL(box);
   elm_object_content_set(popup, box);
   evas_object_show(box);

   label = elm_label_add(box);
   elm_object_style_set(label, "marker");
   snprintf(buf, 100, "%s: %1.1f %s", _("Frequency"), f, u);
   elm_object_text_set(label, buf);
   elm_box_pack_end(box, label);
   evas_object_show(label);
   inst->cfg->cpuclock.popup_label = label;

   e_gadget_util_ctxpopup_place(inst->o_main, popup,
                                inst->cfg->cpuclock.o_gadget);
   evas_object_show(popup);

   return popup;
}

static void
_cpuclock_mouse_down_cb(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_data)
{
   Evas_Event_Mouse_Down *ev = event_data;
   Instance *inst = data;

   if (ev->event_flags & EVAS_EVENT_FLAG_ON_HOLD) return;
   if (ev->button != 3)
     {
        if (inst->cfg->cpuclock.popup)
          elm_ctxpopup_dismiss(inst->cfg->cpuclock.popup);
        else
          inst->cfg->cpuclock.popup = _cpuclock_popup_create(inst);
     }
   else
     {
        if (inst->cfg->cpuclock.popup)
          elm_ctxpopup_dismiss(inst->cfg->cpuclock.popup);
        if (!sysinfo_config) return;
        ev->event_flags |= EVAS_EVENT_FLAG_ON_HOLD;
        if (inst->cfg->esm != E_SYSINFO_MODULE_CPUCLOCK)
          cpuclock_configure(inst);
        else
          e_gadget_configure(inst->o_main);
     }
}

void
_cpuclock_config_updated(Instance *inst)
{
   Edje_Message_Int_Set *frequency_msg;
   Edje_Message_String_Set *governor_msg;
   Eina_List *l;
   int i;
   unsigned int count;

   if (inst->cfg->id == -1)
     {
        inst->cfg->cpuclock.status->cur_frequency = 3.0;
        inst->cfg->cpuclock.status->can_set_frequency = 1;
        inst->cfg->cpuclock.status->cur_min_frequency = 0.5;
        inst->cfg->cpuclock.status->cur_max_frequency = 3.5;
        return;
     }
   if (inst->cfg->cpuclock.status->frequencies)
     {
        count = eina_list_count(inst->cfg->cpuclock.status->frequencies);
        frequency_msg = malloc(sizeof(Edje_Message_Int_Set) + (count - 1) * sizeof(int));
        EINA_SAFETY_ON_NULL_RETURN(frequency_msg);
        frequency_msg->count = count;
        for (l = inst->cfg->cpuclock.status->frequencies, i = 0; l; l = l->next, i++)
          frequency_msg->val[i] = (long)l->data;
        edje_object_message_send(elm_layout_edje_get(inst->cfg->cpuclock.o_gadget), EDJE_MESSAGE_INT_SET, 1, frequency_msg);
        E_FREE(frequency_msg);
     }

   if (inst->cfg->cpuclock.status->governors)
     {
        count = eina_list_count(inst->cfg->cpuclock.status->governors);
        governor_msg = malloc(sizeof(Edje_Message_String_Set) + (count - 1) * sizeof(char *));
        governor_msg->count = count;
        for (l = inst->cfg->cpuclock.status->governors, i = 0; l; l = l->next, i++)
          governor_msg->str[i] = (char *)l->data;
        edje_object_message_send(elm_layout_edje_get(inst->cfg->cpuclock.o_gadget), EDJE_MESSAGE_STRING_SET, 2, governor_msg);
        E_FREE(governor_msg);
     }
   _cpuclock_poll_interval_update(inst);
}

static void
_cpuclock_face_update_current(Instance *inst)
{
   Edje_Message_Int_Set *frequency_msg;
   Edje_Message_String governor_msg;

   frequency_msg = malloc(sizeof(Edje_Message_Int_Set) + (sizeof(int) * 4));
   EINA_SAFETY_ON_NULL_RETURN(frequency_msg);
   frequency_msg->count = 5;
   frequency_msg->val[0] = inst->cfg->cpuclock.status->cur_frequency;
   frequency_msg->val[1] = inst->cfg->cpuclock.status->can_set_frequency;
   frequency_msg->val[2] = inst->cfg->cpuclock.status->cur_min_frequency;
   frequency_msg->val[3] = inst->cfg->cpuclock.status->cur_max_frequency;
   frequency_msg->val[4] = 0; // pad
   edje_object_message_send(elm_layout_edje_get(inst->cfg->cpuclock.o_gadget), EDJE_MESSAGE_INT_SET, 3,
                            frequency_msg);
   E_FREE(frequency_msg);

   /* BSD crashes here without the if-condition
    * since it has no governors (yet) */
   if (inst->cfg->cpuclock.status->cur_governor)
     {
        governor_msg.str = inst->cfg->cpuclock.status->cur_governor;
        edje_object_message_send(elm_layout_edje_get(inst->cfg->cpuclock.o_gadget), EDJE_MESSAGE_STRING, 4,
                                 &governor_msg);
     }

   if (inst->cfg->cpuclock.popup)
     {
        double f = inst->cfg->cpuclock.status->cur_frequency;
        char buf[100], *u;

        if (f < 1000000)
          {
             f += 500;
             f /= 1000;
             u = _("MHz");
          }
        else
          {
             f += 50000;
             f /= 1000000;
             u = _("GHz");
          }
        snprintf(buf, 100, "%s: %1.1f %s", _("Frequency"), f, u);
        elm_object_text_set(inst->cfg->cpuclock.popup_label, buf);
     }
}

static void
_cpuclock_status_check_available(Cpu_Status *s)
{
#if !defined(__OpenBSD__)
   char buf[4096];
   Eina_List *l;
#endif
   // FIXME: this assumes all cores accept the same freqs/ might be wrong

#if defined(__OpenBSD__)
   int p;

   if (s->frequencies)
     {
        eina_list_free(s->frequencies);
        s->frequencies = NULL;
     }

   /* storing percents */
   p = 100;
   s->frequencies = eina_list_append(s->frequencies, (void *)(long int)p);
   p = 75;
   s->frequencies = eina_list_append(s->frequencies, (void *)(long int)p);
   p = 50;
   s->frequencies = eina_list_append(s->frequencies, (void *)(long int)p);
   p = 25;
   s->frequencies = eina_list_append(s->frequencies, (void *)(long int)p);
#elif defined(__FreeBSD__)
   int freq;
   size_t len = sizeof(buf);
   char *pos, *q;

   /* read freq_levels sysctl and store it in freq */
   if (sysctlbyname("dev.cpu.0.freq_levels", buf, &len, NULL, 0) == 0)
     {
        /* sysctl returns 0 on success */
        if (s->frequencies)
          {
             eina_list_free(s->frequencies);
             s->frequencies = NULL;
          }

        /* parse freqs and store the frequencies in s->frequencies */
        pos = buf;
        while (pos)
          {
             q = strchr(pos, '/');
             if (!q) break;

             *q = '\0';
             freq = atoi(pos);
             freq *= 1000;
             s->frequencies = eina_list_append(s->frequencies, (void *)(long)freq);

             pos = q + 1;
             pos = strchr(pos, ' ');
          }
     }

   /* sort is not necessary because freq_levels is already sorted */
   /* freebsd doesn't have governors */
   if (s->governors)
     {
        for (l = s->governors; l; l = l->next)
          E_FREE(l->data);
        eina_list_free(s->governors);
        s->governors = NULL;
     }
#else
   FILE *f;

   f = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies", "r");
   if (f)
     {
        char *freq;

        if (s->frequencies)
          {
             eina_list_free(s->frequencies);
             s->frequencies = NULL;
          }

        if (fgets(buf, sizeof(buf), f) == NULL)
          {
             fclose(f);
             return;
          }
        fclose(f);

        freq = strtok(buf, " ");
        do
          {
             if (atoi(freq) != 0)
               {
                  s->frequencies = eina_list_append(s->frequencies,
                                                    (void *)(long)atoi(freq));
               }
             freq = strtok(NULL, " ");
          }
        while (freq);

        s->frequencies = eina_list_sort(s->frequencies,
                                        eina_list_count(s->frequencies),
                                        _cpuclock_cb_sort);
     }
   else
     do
       {
#define CPUFREQ_SYSFSDIR "/sys/devices/system/cpu/cpu0/cpufreq"
          f = fopen(CPUFREQ_SYSFSDIR "/scaling_cur_freq", "r");
          if (!f) break;
          fclose(f);

          f = fopen(CPUFREQ_SYSFSDIR "/scaling_driver", "r");
          if (!f) break;
          if (fgets(buf, sizeof(buf), f) == NULL)
            {
               fclose(f);
               break;
            }
          fclose(f);
          if (strcmp(buf, "intel_pstate\n")) break;

          if (s->frequencies)
            {
               eina_list_free(s->frequencies);
               s->frequencies = NULL;
            }
#define CPUFREQ_ADDF(filename)                                          \
  f = fopen(CPUFREQ_SYSFSDIR filename, "r");                            \
  if (f)                                                                \
    {                                                                   \
       if (fgets(buf, sizeof(buf), f) != NULL)                          \
         s->frequencies = eina_list_append(s->frequencies,              \
                                           (void *)(long)(atoi(buf)));  \
       fclose(f);                                                       \
    }
          CPUFREQ_ADDF("/cpuinfo_min_freq");
          CPUFREQ_ADDF("/cpuinfo_max_freq");
       }
     while (0);

   f = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors", "r");
   if (f)
     {
        char *gov;
        int len;

        if (s->governors)
          {
             for (l = s->governors; l; l = l->next)
               E_FREE(l->data);
             eina_list_free(s->governors);
             s->governors = NULL;
          }

        if (fgets(buf, sizeof(buf), f) == NULL)
          {
             fclose(f);
             return;
          }
        fclose(f);
        len = strlen(buf);
        if (len > 0)
          {
             gov = buf + len - 1;
             while ((gov > buf) && (isspace(*gov)))
               {
                  *gov = 0;
                  gov--;
               }
          }
        gov = strtok(buf, " ");
        do
          {
             while ((*gov) && (isspace(*gov)))
               gov++;
             if (strlen(gov) != 0)
               s->governors = eina_list_append(s->governors, strdup(gov));
             gov = strtok(NULL, " ");
          }
        while (gov);

        s->governors =
          eina_list_sort(s->governors, eina_list_count(s->governors),
                         (int (*)(const void *, const void *))strcmp);
     }
#endif
}

static int
_cpuclock_status_check_current(Cpu_Status *s)
{
   int ret = 0;
   int frequency = 0;

#if defined(__OpenBSD__)
   size_t len = sizeof(frequency);
   int percent, mib[] = {CTL_HW, HW_CPUSPEED};
   s->active = 0;

   _cpuclock_status_check_available(s);

   if (sysctl(mib, 2, &frequency, &len, NULL, 0) == 0)
     {
        frequency *= 1000;
        if (frequency != s->cur_frequency) ret = 1;
        s->cur_frequency = frequency;
        s->active = 1;
     }

   mib[1] = HW_SETPERF;

   if (sysctl(mib, 2, &percent, &len, NULL, 0) == 0)
     {
        s->cur_percent = percent;
     }

   s->can_set_frequency = 1;
   s->cur_governor = NULL;

#elif defined(__FreeBSD__)
   size_t len = sizeof(frequency);
   s->active = 0;

   /* frequency is stored in dev.cpu.0.freq */
   if (sysctlbyname("dev.cpu.0.freq", &frequency, &len, NULL, 0) == 0)
     {
        frequency *= 1000;
        if (frequency != s->cur_frequency) ret = 1;
        s->cur_frequency = frequency;
        s->active = 1;
     }

   /* hardcoded for testing */
   s->can_set_frequency = 1;
   s->cur_governor = NULL;

#else
   char buf[4096];
   FILE *f;
   int frequency_min = 0x7fffffff;
   int frequency_max = 0;
   int freqtot = 0;
   int i;

   s->active = 0;

   _cpuclock_status_check_available(s);
   // average out frequencies of all cores
   for (i = 0; i < 64; i++)
     {
        snprintf(buf, sizeof(buf), "/sys/devices/system/cpu/cpu%i/cpufreq/scaling_cur_freq", i);
        f = fopen(buf, "r");
        if (f)
          {
             if (fgets(buf, sizeof(buf), f) == NULL)
               {
                  fclose(f);
                  continue;
               }
             fclose(f);

             frequency = atoi(buf);
             if (frequency > frequency_max) frequency_max = frequency;
             if (frequency < frequency_min) frequency_min = frequency;
             freqtot += frequency;
             s->active = 1;
          }
        else
          break;
     }
   if (i < 1) i = 1;
   frequency = freqtot / i;
   if (frequency != s->cur_frequency) ret = 1;
   if (frequency_min != s->cur_min_frequency) ret = 1;
   if (frequency_max != s->cur_max_frequency) ret = 1;
   s->cur_frequency = frequency;
   s->cur_min_frequency = frequency_min;
   s->cur_max_frequency = frequency_max;

//  printf("%i | %i %i\n", frequency, frequency_min, frequency_max);

   // FIXME: this assumes all cores are on the same governor
   f = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed", "r");
   if (f)
     {
        s->can_set_frequency = 1;
        fclose(f);
     }
   else
     {
        s->can_set_frequency = 0;
     }

   f = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor", "r");
   if (f)
     {
        char *p;

        if (fgets(buf, sizeof(buf), f) == NULL)
          {
             fclose(f);
             return ret;
          }
        fclose(f);

        for (p = buf; (*p != 0) && (isalnum(*p)); p++) ;
        *p = 0;

        if ((!s->cur_governor) || (strcmp(buf, s->cur_governor)))
          {
             ret = 1;

             E_FREE(s->cur_governor);
             s->cur_governor = strdup(buf);

             for (i = strlen(s->cur_governor) - 1; i >= 0; i--)
               {
                  if (isspace(s->cur_governor[i]))
                    s->cur_governor[i] = 0;
                  else
                    break;
               }
          }
     }
   f = fopen("/sys/devices/system/cpu/intel_pstate/min_perf_pct", "r");
   if (f)
     {
        if (fgets(buf, sizeof(buf), f) != NULL)
          {
             s->pstate_min = atoi(buf);
             s->pstate = 1;
          }
        fclose(f);
     }
   f = fopen("/sys/devices/system/cpu/intel_pstate/max_perf_pct", "r");
   if (f)
     {
        if (fgets(buf, sizeof(buf), f) != NULL)
          {
             s->pstate_max = atoi(buf);
             s->pstate = 1;
          }
        fclose(f);
     }
   f = fopen("/sys/devices/system/cpu/intel_pstate/no_turbo", "r");
   if (f)
     {
        if (fgets(buf, sizeof(buf), f) != NULL)
          {
             s->pstate_turbo = atoi(buf);
             if (s->pstate_turbo) s->pstate_turbo = 0;
             else s->pstate_turbo = 1;
             s->pstate = 1;
          }
        fclose(f);
     }
#endif
   return ret;
}

static void
_cpuclock_resize_cb(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_data EINA_UNUSED)
{
   Evas_Coord w, h;
   Instance *inst = data;

   edje_object_parts_extends_calc(elm_layout_edje_get(inst->cfg->cpuclock.o_gadget), 0, 0, &w, &h);
   if (w < 1) w = 1;
   if (h < 1) h = 1;
   if (inst->cfg->esm == E_SYSINFO_MODULE_CPUCLOCK)
     evas_object_size_hint_aspect_set(inst->o_main, EVAS_ASPECT_CONTROL_BOTH, w, h);
   else
     evas_object_size_hint_aspect_set(inst->cfg->cpuclock.o_gadget, EVAS_ASPECT_CONTROL_BOTH, w, h);
}

static void
_cpuclock_cb_frequency_check_main(void *data, Ecore_Thread *th)
{
   Thread_Config *thc = data;
   for (;; )
     {
        Cpu_Status *status;

        if (ecore_thread_check(th)) break;
        status = _cpuclock_status_new();
        if (_cpuclock_status_check_current(status))
          ecore_thread_feedback(th, status);
        else
          _cpuclock_status_free(status);
        if (ecore_thread_check(th)) break;
        e_powersave_sleeper_sleep(thc->sleeper, thc->interval);
        if (ecore_thread_check(th)) break;
     }
}

static void
_cpuclock_cb_frequency_check_notify(void *data,
                                    Ecore_Thread *th EINA_UNUSED,
                                    void *msg)
{
   Cpu_Status *status = msg;
   Eina_Bool freq_changed = EINA_FALSE;
   Eina_Bool init_set = EINA_FALSE;
   Thread_Config *thc = data;

   if (!thc->inst) return;
   if (!thc->inst->cfg) return;
   if (thc->inst->cfg->esm != E_SYSINFO_MODULE_CPUCLOCK && thc->inst->cfg->esm != E_SYSINFO_MODULE_SYSINFO) return;

   if ((thc->inst->cfg->cpuclock.status) && (status) &&
       (
#if defined(__OpenBSD__)
         (status->cur_percent != thc->inst->cfg->cpuclock.status->cur_percent) ||
#endif
         (status->cur_frequency != thc->inst->cfg->cpuclock.status->cur_frequency) ||
         (status->cur_min_frequency != thc->inst->cfg->cpuclock.status->cur_min_frequency) ||
         (status->cur_max_frequency != thc->inst->cfg->cpuclock.status->cur_max_frequency) ||
         (status->can_set_frequency != thc->inst->cfg->cpuclock.status->can_set_frequency)))
     freq_changed = EINA_TRUE;
   E_FREE_FUNC(thc->inst->cfg->cpuclock.status, _cpuclock_status_free);
   thc->inst->cfg->cpuclock.status = status;
   if (freq_changed)
     {
        _cpuclock_face_update_current(thc->inst);
     }
   if (thc->inst->cfg->cpuclock.status->active == 0)
     elm_layout_signal_emit(thc->inst->cfg->cpuclock.o_gadget, "e,state,disabled", "e");
   else if (thc->inst->cfg->cpuclock.status->active == 1)
     elm_layout_signal_emit(thc->inst->cfg->cpuclock.o_gadget, "e,state,enabled", "e");

   if (!init_set)
     {
        _cpuclock_set_pstate(thc->inst->cfg->cpuclock.pstate_min - 1,
                             thc->inst->cfg->cpuclock.pstate_max - 1,
                             thc->inst->cfg->cpuclock.status->pstate_turbo);
        init_set = EINA_TRUE;
     }
}

static void
_cpuclock_cb_frequency_check_end(void *data, Ecore_Thread *th EINA_UNUSED)
{
   Thread_Config *thc = data;

   e_powersave_sleeper_free(thc->sleeper);
   E_FREE(thc);
}

EINTERN void
_cpuclock_poll_interval_update(Instance *inst)
{
   Thread_Config *thc;

   if (inst->cfg->cpuclock.frequency_check_thread)
     {
        ecore_thread_cancel(inst->cfg->cpuclock.frequency_check_thread);
        inst->cfg->cpuclock.frequency_check_thread = NULL;
     }
   thc = E_NEW(Thread_Config, 1);
   if (thc)
     {
        thc->inst = inst;
        thc->sleeper = e_powersave_sleeper_new();
        thc->interval = inst->cfg->cpuclock.poll_interval;
        inst->cfg->cpuclock.frequency_check_thread =
          ecore_thread_feedback_run(_cpuclock_cb_frequency_check_main,
                                    _cpuclock_cb_frequency_check_notify,
                                    _cpuclock_cb_frequency_check_end,
                                    _cpuclock_cb_frequency_check_end, thc, EINA_TRUE);
     }
   e_config_save_queue();
}

static Eina_Bool
_screensaver_on(void *data)
{
   Instance *inst = data;

   if (inst->cfg->cpuclock.frequency_check_thread)
     {
        ecore_thread_cancel(inst->cfg->cpuclock.frequency_check_thread);
        inst->cfg->cpuclock.frequency_check_thread = NULL;
     }
   return ECORE_CALLBACK_RENEW;
}

static Eina_Bool
_screensaver_off(void *data)
{
   Instance *inst = data;

   _cpuclock_config_updated(inst);

   return ECORE_CALLBACK_RENEW;
}

static void
_cpuclock_removed_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_data)
{
   Instance *inst = data;
   Ecore_Event_Handler *handler;

   if (inst->o_main != event_data) return;

   if (inst->cfg->cpuclock.popup_label)
     E_FREE_FUNC(inst->cfg->cpuclock.popup, evas_object_del);
   if (inst->cfg->cpuclock.popup)
     E_FREE_FUNC(inst->cfg->cpuclock.popup, evas_object_del);
   if (inst->cfg->cpuclock.configure)
     E_FREE_FUNC(inst->cfg->cpuclock.configure, evas_object_del);
   EINA_LIST_FREE(inst->cfg->cpuclock.handlers, handler)
     ecore_event_handler_del(handler);
   evas_object_event_callback_del_full(inst->o_main, EVAS_CALLBACK_DEL, sysinfo_cpuclock_remove, data);
   evas_object_smart_callback_del_full(e_gadget_site_get(inst->o_main), "gadget_removed",
                                       _cpuclock_removed_cb, inst);
   if (inst->cfg->cpuclock.frequency_check_thread)
     {
        ecore_thread_cancel(inst->cfg->cpuclock.frequency_check_thread);
        inst->cfg->cpuclock.frequency_check_thread = NULL;
        return;
     }
   if (inst->cfg->cpuclock.governor)
     eina_stringshare_del(inst->cfg->cpuclock.governor);
   E_FREE_FUNC(inst->cfg->cpuclock.status, _cpuclock_status_free);

   sysinfo_config->items = eina_list_remove(sysinfo_config->items, inst->cfg);
   if (inst->cfg->id >= 0)
     sysinfo_instances = eina_list_remove(sysinfo_instances, inst);
   E_FREE(inst->cfg);
   E_FREE(inst);
}

void
sysinfo_cpuclock_remove(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_data EINA_UNUSED)
{
   Instance *inst = data;
   Ecore_Event_Handler *handler;

   if (inst->cfg->cpuclock.popup_label)
     E_FREE_FUNC(inst->cfg->cpuclock.popup, evas_object_del);
   if (inst->cfg->cpuclock.popup)
     E_FREE_FUNC(inst->cfg->cpuclock.popup, evas_object_del);
   if (inst->cfg->cpuclock.configure)
     E_FREE_FUNC(inst->cfg->cpuclock.configure, evas_object_del);
   EINA_LIST_FREE(inst->cfg->cpuclock.handlers, handler)
     ecore_event_handler_del(handler);
   if (inst->cfg->cpuclock.frequency_check_thread)
     {
        ecore_thread_cancel(inst->cfg->cpuclock.frequency_check_thread);
        inst->cfg->cpuclock.frequency_check_thread = NULL;
     }
   if (inst->cfg->cpuclock.governor)
     eina_stringshare_del(inst->cfg->cpuclock.governor);
   E_FREE_FUNC(inst->cfg->cpuclock.status, _cpuclock_status_free);
}

static void
_cpuclock_created_cb(void *data, Evas_Object *obj, void *event_data EINA_UNUSED)
{
   Instance *inst = data;
   E_Gadget_Site_Orient orient = e_gadget_site_orient_get(e_gadget_site_get(inst->o_main));
   Eina_List *l = NULL;

   e_gadget_configure_cb_set(inst->o_main, _cpuclock_configure_cb);

   if (inst->cfg->cpuclock.pstate_min == 0) inst->cfg->cpuclock.pstate_min = 1;
   if (inst->cfg->cpuclock.pstate_max == 0) inst->cfg->cpuclock.pstate_max = 101;

   inst->cfg->cpuclock.o_gadget = elm_layout_add(inst->o_main);
   if (orient == E_GADGET_SITE_ORIENT_VERTICAL)
     e_theme_edje_object_set(inst->cfg->cpuclock.o_gadget,
                             "base/theme/gadget/cpuclock",
                             "e/gadget/cpuclock/main_vert");
   else
     e_theme_edje_object_set(inst->cfg->cpuclock.o_gadget, "base/theme/gadget/cpuclock",
                             "e/gadget/cpuclock/main");
   E_EXPAND(inst->cfg->cpuclock.o_gadget);
   E_FILL(inst->cfg->cpuclock.o_gadget);
   edje_object_signal_callback_add(elm_layout_edje_get(inst->cfg->cpuclock.o_gadget), "e,action,governor,next", "*",
                                   _cpuclock_face_cb_set_governor, inst);
   edje_object_signal_callback_add(elm_layout_edje_get(inst->cfg->cpuclock.o_gadget), "e,action,frequency,increase", "*",
                                   _cpuclock_face_cb_set_frequency, inst);
   edje_object_signal_callback_add(elm_layout_edje_get(inst->cfg->cpuclock.o_gadget), "e,action,frequency,decrease", "*",
                                   _cpuclock_face_cb_set_frequency, inst);
   evas_object_event_callback_add(inst->cfg->cpuclock.o_gadget, EVAS_CALLBACK_RESIZE, _cpuclock_resize_cb, inst);
   elm_box_pack_end(inst->o_main, inst->cfg->cpuclock.o_gadget);
   evas_object_event_callback_add(inst->cfg->cpuclock.o_gadget,
                                  EVAS_CALLBACK_MOUSE_DOWN,
                                  _cpuclock_mouse_down_cb, inst);
   evas_object_show(inst->cfg->cpuclock.o_gadget);
   evas_object_smart_callback_del_full(obj, "gadget_created", _cpuclock_created_cb, data);

   inst->cfg->cpuclock.status = _cpuclock_status_new();
   _cpuclock_status_check_available(inst->cfg->cpuclock.status);

   E_LIST_HANDLER_APPEND(inst->cfg->cpuclock.handlers, E_EVENT_SCREENSAVER_ON, _screensaver_on, inst);
   E_LIST_HANDLER_APPEND(inst->cfg->cpuclock.handlers, E_EVENT_SCREENSAVER_OFF, _screensaver_off, inst);
   E_LIST_HANDLER_APPEND(inst->cfg->cpuclock.handlers, E_EVENT_POWERSAVE_UPDATE, _cpuclock_event_cb_powersave, inst);

   _cpuclock_config_updated(inst);
   if ((inst->cfg->cpuclock.restore_governor) && (inst->cfg->cpuclock.governor))
     {
        for (l = inst->cfg->cpuclock.status->governors; l; l = l->next)
          {
             if (!strcmp(l->data, inst->cfg->cpuclock.governor))
               {
                  _cpuclock_set_governor(inst->cfg->cpuclock.governor);
                  break;
               }
          }
     }
}

Evas_Object *
sysinfo_cpuclock_create(Evas_Object *parent, Instance *inst)
{
   Eina_List *l = NULL;

   if (inst->cfg->cpuclock.pstate_min == 0) inst->cfg->cpuclock.pstate_min = 1;
   if (inst->cfg->cpuclock.pstate_max == 0) inst->cfg->cpuclock.pstate_max = 101;

   inst->cfg->cpuclock.o_gadget = elm_layout_add(parent);
   e_theme_edje_object_set(inst->cfg->cpuclock.o_gadget, "base/theme/gadget/cpuclock",
                           "e/gadget/cpuclock/main");
   E_EXPAND(inst->cfg->cpuclock.o_gadget);
   E_FILL(inst->cfg->cpuclock.o_gadget);
   evas_object_event_callback_add(inst->cfg->cpuclock.o_gadget,
                                  EVAS_CALLBACK_MOUSE_DOWN,
                                  _cpuclock_mouse_down_cb, inst);
   edje_object_signal_callback_add(elm_layout_edje_get(inst->cfg->cpuclock.o_gadget), "e,action,governor,next", "*",
                                   _cpuclock_face_cb_set_governor, inst);
   edje_object_signal_callback_add(elm_layout_edje_get(inst->cfg->cpuclock.o_gadget), "e,action,frequency,increase", "*",
                                   _cpuclock_face_cb_set_frequency, inst);
   edje_object_signal_callback_add(elm_layout_edje_get(inst->cfg->cpuclock.o_gadget), "e,action,frequency,decrease", "*",
                                   _cpuclock_face_cb_set_frequency, inst);
   evas_object_event_callback_add(inst->cfg->cpuclock.o_gadget, EVAS_CALLBACK_RESIZE, _cpuclock_resize_cb, inst);
   evas_object_show(inst->cfg->cpuclock.o_gadget);

   inst->cfg->cpuclock.status = _cpuclock_status_new();
   _cpuclock_status_check_available(inst->cfg->cpuclock.status);

   E_LIST_HANDLER_APPEND(inst->cfg->cpuclock.handlers, E_EVENT_SCREENSAVER_ON, _screensaver_on, inst);
   E_LIST_HANDLER_APPEND(inst->cfg->cpuclock.handlers, E_EVENT_SCREENSAVER_OFF, _screensaver_off, inst);
   E_LIST_HANDLER_APPEND(inst->cfg->cpuclock.handlers, E_EVENT_POWERSAVE_UPDATE, _cpuclock_event_cb_powersave, inst);

   _cpuclock_config_updated(inst);
   if ((inst->cfg->cpuclock.restore_governor) && (inst->cfg->cpuclock.governor))
     {
        for (l = inst->cfg->cpuclock.status->governors; l; l = l->next)
          {
             if (!strcmp(l->data, inst->cfg->cpuclock.governor))
               {
                  _cpuclock_set_governor(inst->cfg->cpuclock.governor);
                  break;
               }
          }
     }

   return inst->cfg->cpuclock.o_gadget;
}

static Config_Item *
_conf_item_get(int *id)
{
   Config_Item *ci;
   Eina_List *l;

   if (*id > 0)
     {
        EINA_LIST_FOREACH(sysinfo_config->items, l, ci)
          if (*id == ci->id && ci->esm == E_SYSINFO_MODULE_CPUCLOCK) return ci;
     }

   ci = E_NEW(Config_Item, 1);

   if (*id != -1)
     ci->id = eina_list_count(sysinfo_config->items) + 1;
   else
     ci->id = -1;

   ci->esm = E_SYSINFO_MODULE_CPUCLOCK;
   ci->cpuclock.poll_interval = 32;
   ci->cpuclock.restore_governor = 0;
   ci->cpuclock.auto_powersave = 1;
   ci->cpuclock.powersave_governor = NULL;
   ci->cpuclock.governor = NULL;
   ci->cpuclock.pstate_min = 1;
   ci->cpuclock.pstate_max = 101;
   ci->cpuclock.configure = NULL;

   sysinfo_config->items = eina_list_append(sysinfo_config->items, ci);

   return ci;
}

Evas_Object *
cpuclock_create(Evas_Object *parent, int *id, E_Gadget_Site_Orient orient EINA_UNUSED)
{
   Instance *inst;

   inst = E_NEW(Instance, 1);
   inst->cfg = _conf_item_get(id);
   *id = inst->cfg->id;
   inst->o_main = elm_box_add(parent);
   evas_object_data_set(inst->o_main, "Instance", inst);
   evas_object_smart_callback_add(parent, "gadget_created", _cpuclock_created_cb, inst);
   evas_object_smart_callback_add(parent, "gadget_removed", _cpuclock_removed_cb, inst);
   evas_object_event_callback_add(inst->o_main, EVAS_CALLBACK_DEL, sysinfo_cpuclock_remove, inst);
   evas_object_show(inst->o_main);

   if (inst->cfg->id < 0) return inst->o_main;

   sysinfo_instances =
     eina_list_append(sysinfo_instances, inst);

   return inst->o_main;
}