📄 memory.cxx
字号:
q->box(FL_FLAT_BOX); q->align(FL_ALIGN_INSIDE | FL_ALIGN_LEFT); _storage.used = o; _storage.free = p; _storage.total = q; cury += (3 * BUTTON_HEIGHT); } // end of labels _mainw->end(); _mainw->hide(); return;} // end of NxMemory::MakeWindow(void)/*******************************************************************************\**** Function: void GetMemory()** Desc: Gets the memory values from /proc/meminfo and stores them** into the widgets for display** Accepts: Nothing (void)** Returns: Nothing (void)**\*******************************************************************************/voidNxMemory::GetMemory(void){ char buf[255]; // Buffer int tag_idx = 0; // Tag index double val; // Value unsigned long mbuf = 0, // Memory buffered mcache = 0, // Memory cached mfree = 0, // Memory free mtot = 0, // Memory total mused = 0, // Memory used sfree = 0, // Swap free stot = 0, // Swap total sused = 0; // Swap used FILE *memf; // Memory file ptr if ((memf = fopen("/proc/meminfo", "r")) != NULL) { // Discard the first line fgets(buf, sizeof(buf), memf); fscanf(memf, "%*s %ld %ld %ld %*d %ld %ld", &mtot, &mused, &mfree, &mbuf, &mcache); fscanf(memf, "%*s %ld %ld %ld", &stot, &sused, &sfree); mused = sused + mused - mcache - mbuf; mfree = mtot - mused; fclose(memf); } // end of if // Get the In use value tag_idx = 0; val = getsztags(mused, &tag_idx); sprintf(buf, "%9.2f %s", val, size_tags[tag_idx]); delete[]_memory.u_str; _memory.u_str = new char[strlen(buf) + 1]; strcpy(_memory.u_str, buf); _memory.used->label(_memory.u_str); _memory.used->redraw(); tag_idx = 0; val = getsztags(mfree, &tag_idx); sprintf(buf, "%9.2f %s", val, size_tags[tag_idx]); delete[]_memory.f_str; _memory.f_str = new char[strlen(buf) + 1]; strcpy(_memory.f_str, buf); _memory.free->label(_memory.f_str); _memory.free->redraw(); tag_idx = 0; val = getsztags(mtot, &tag_idx); sprintf(buf, "%9.2f %s", val, size_tags[tag_idx]); delete[]_memory.t_str; _memory.t_str = new char[strlen(buf) + 1]; strcpy(_memory.t_str, buf); _memory.total->label(_memory.t_str); _memory.total->redraw(); val = ((double) mused / mtot) * 100.0; _memsl->value(val); _memsl->redraw(); return;} // end of NxMemory::GetMemory(void)/*******************************************************************************\**** Function: void GetStorage()** Desc: Determines the storage capacity and what is in use for the current** storage medium (as determined by f_curDev)** Accepts: Nothing (void)** Returns: Nothing (void)**\*******************************************************************************/voidNxMemory::GetStorage(void){ char buf[64]; double pcnt_used, // Percentage used sadjtot = 0.0, sfree = 0.0, stot = 0.0, sused = 0.0, val; // Value int tag_idx = 1; // Idx into size_tags struct statfs stfs; // Filesystem stat info // Stat the filesystem if (statfs(f_curDev, &stfs)) return; stot = ((double) stfs.f_blocks * stfs.f_bsize) / 1024; // In KB sadjtot = stfs.f_blocks - (stfs.f_bfree - stfs.f_bavail); sfree = ((double) stfs.f_bavail * stfs.f_bsize) / 1024; sused = ((double) (sadjtot - stfs.f_bavail) * stfs.f_bsize) / 1024; sadjtot = sadjtot * stfs.f_bsize / 1024; pcnt_used = ((sused) / sadjtot) * 100.0 + 0.5; // Set tag_idx to reference values in KB already tag_idx = 1; val = getsztags((long) sfree, &tag_idx); sprintf(buf, "%9.2f %s", val, size_tags[tag_idx]); delete[]_storage.f_str; _storage.f_str = new char[strlen(buf) + 1]; strcpy(_storage.f_str, buf); _storage.free->label(_storage.f_str); _storage.free->redraw(); tag_idx = 1; val = getsztags((long) sused, &tag_idx); sprintf(buf, "%9.2f %s", val, size_tags[tag_idx]); delete[]_storage.u_str; _storage.u_str = new char[strlen(buf) + 1]; strcpy(_storage.u_str, buf); _storage.used->label(_storage.u_str); _storage.used->redraw(); tag_idx = 1; val = getsztags((long) stot, &tag_idx); sprintf(buf, "%9.2f %s", val, size_tags[tag_idx]); delete[]_storage.t_str; _storage.t_str = new char[strlen(buf) + 1]; strcpy(_storage.t_str, buf); _storage.total->label(_storage.t_str); _storage.total->redraw(); // Set the slider value _storsl->value(pcnt_used); _storsl->redraw(); return;} // end of NxMemory::GetStorage()/*******************************************************************************\**** Function: void GetValues()** Desc: Sets the widgets values based upon the current state of the ** application (_mode)** Accpets: int flag = Determines if the values are to be retrieved** MEM_GV_UPDMNT = Updates the mount list.** MEM_GV_UPDMEM = Updates the memory info** MEM_GV_UPDSTO = Updates the storage info** Returns: Nothing (void)**\*******************************************************************************/voidNxMemory::GetValues(int flag){ char buf[255], // Buffer *tmpDev = NULL; // Temp device name int idx; // Index FILE *mntf; // Mount file struct NxMenuItem *mi; // Menu item ptr if (flag & MEM_GV_UPDMNT) { // Save off the current storage device if (f_curDev != NULL) { tmpDev = new char[strlen(f_curDev) + 1]; strcpy(tmpDev, f_curDev); } // end of if if ((mntf = fopen("/proc/mounts", "r")) != NULL) { if (_stormb->size()) { _stormb->clear(); _stormb->menu((NxMenuItem *) NULL); } while (fgets(buf, sizeof(buf), mntf)) { /* Field 1 is the device */ char *cp = buf; for (; *cp && *cp != ' '; cp++); if (!*cp) continue; *cp++ = 0; char *start = cp; int slash = 0; for (; *start && *start != ' '; start++) slash++; if (*start) *start = 0; char *ptr, *str; ptr = str = (char *) calloc(strlen(cp) + slash + 1, 1); for (start = cp; *start; *start++) { if (*start == '/') *ptr++ = '\\'; *ptr++ = *start; } _stormb->add(str); free(str); } _stormb->value(0); fclose(mntf); } // end of if } // end of if mi = const_cast < struct NxMenuItem *>(_stormb->menu()); // Update the label if (tmpDev) { int fnd = 0; for (int i = 0; i < _stormb->size() - 1; i++) { if (!mi[i].text) continue; if (!strcmp(tmpDev, mi[i].text)) { fnd = 1; _stormb->value(i); break; } // end of if } // end of for if (!fnd) flag |= MEM_GV_UPDSTO; } // end of if idx = _stormb->value(); f_curDev = const_cast < char *>(mi[idx].text); _stormb->label(f_curDev); _stormb->redraw(); // Update the memory (if desired) if (flag & MEM_GV_UPDMEM) GetMemory(); if (flag & MEM_GV_UPDSTO) GetStorage(); delete[]tmpDev; return;} // end of NxMemory::GetValues(void)//-------------------------------------------------------------------------------//// Private static callback methods// void stor_mb_cb(Fl_Widget *, void *)////-------------------------------------------------------------------------------/*******************************************************************************\**** Function: void stor_mb_cb()** Desc: Callback to select info on the type of storage media currently** available** Accepts: Fl_Widget *w = Ptr to the widget responsible for the branch** void *d = Ptr to any ancillary data** Returns: Nothing (void)**\*******************************************************************************/voidNxMemory::stor_mb_cb(Fl_Widget * w, void *d){ NxMemory *me = (NxMemory *) d; NxMenuButton *mb = (NxMenuButton *) w; // The menu button struct NxMenuItem *mi; // Ptr to the menu items mi = const_cast < struct NxMenuItem *>(mb->menu()); f_curDev = const_cast < char *>(mi[mb->value()].text); mb->label(f_curDev); me->GetStorage(); mb->redraw(); return;} // end of NxMemory::stor_mb_cb(Fl_Widget *, void *)//------------------------------------------------------------------------------//// Static timer callbacks////------------------------------------------------------------------------------/*******************************************************************************\**** Function: void proc_tmr()** Desc: Timer callback to continually (re)read /proc/mounts and get all** of the devices mounted into the main filesystem** Accepts: void *d = Any ancillary data** Returns: Nothing (void)**\*******************************************************************************/voidNxMemory::proc_tmr(void *d){ NxMemory *me = (NxMemory *) d; static int mem_upd = 0; // Static flag int upd_flag = MEM_GV_UPDMNT; // Default update flags if (mem_upd++ == 15) { upd_flag |= MEM_GV_UPDMEM; mem_upd = 0; } // end of if me->GetValues(upd_flag); Fl::add_timeout(2.0, proc_tmr, d);} // end of proc_tmr(void *)//------------------------------------------------------------------------------//// Non-Class static functions////------------------------------------------------------------------------------/*******************************************************************************\**** Function: double getsztags()** Desc: Converts the value into the lowest form of BYTES/KB/MB/GB that is** >= 1.0 and < 1024.0 units** Accepts: int value = Value to convert (if *idx == 0, values are assumed bytes)** int *idx = Current index level** Returns: Double a value >= 1.0 && < 1024.0**\*******************************************************************************/static doublegetsztags(int value, int *idx){ int new_idx = *idx; // New idx double retval = value; // Value if (new_idx < 0 || new_idx > SZ_TAGS) new_idx = 0; while (retval >= 1.0 && retval >= (new_idx == 0 ? 1024 : 1000) && new_idx < SZ_TAGS) { retval /= (new_idx == 0 ? 1024.0 : 1000.0); new_idx++; } // end of while *idx = new_idx; return (retval);} // end of getsztags(int, int *)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -