Showing posts with label source. Show all posts
Showing posts with label source. Show all posts

Monday, December 6, 2010

Convert a string to a long using ' _str2num '

unsigned long long _str2num(const char *string, unsigned base, int sign)
{
  unsigned long long result = 0;
  int length = 0;
  int negative = 0;
  int count = 0;

  if (string == NULL)
    {
      errno = ERR_NULLPARAMETER;
      return (0);
    }

  // Here we measure the string length
  length = strlen(string);
  // Here we check the string sign
  if (sign && (string[0] == '-'))
    {
      negative = 1;
      count += 1;
    }


  // Do a loop to add to the value of 'result'.
  for ( ; count < length; count ++)
    {
      switch (base)
    {
  // DEC
    case 10:
      if (!isdigit(string[count]))
        {
          errno = ERR_INVALID;
          goto out;
        }
      result *= base;
      result += (string[count] - '0');
      break;
  // HEX
    case 16:
      if (!isxdigit(string[count]))
        {
          errno = ERR_INVALID;
          goto out;
        }
      result *= base;
      if ((string[count] >= '0') && (string[count] <= '9'))
        result += (string[count] - '0');
      else if ((string[count] >= 'a') && (string[count] <= 'f'))
        result += ((string[count] - 'a') + 10);
      else
        result += ((string[count] - 'A') + 10);
      break;

    default:
      errno = ERR_NOTIMPLEMENTED;
      goto out;
    }
    }

 out:
 
// If negative is 1 then 'result' = 'result' * -1
  if (negative)
    result = ((long long) result * -1);
 
  return (result);
}

Thursday, November 25, 2010

Vanesika API

static kernelFunctionIndex textFunctionIndex[] = {

  // Text input/output functions (1000-1999 range)

  { _fnum_textGetConsoleInput, kernelTextGetConsoleInput, 0, PRIVILEGE_USER },
  { _fnum_textSetConsoleInput, kernelTextSetConsoleInput,
    1, PRIVILEGE_SUPERVISOR },
  { _fnum_textGetConsoleOutput, kernelTextGetConsoleOutput,
    0, PRIVILEGE_USER },
  { _fnum_textSetConsoleOutput, kernelTextSetConsoleOutput,
    1, PRIVILEGE_SUPERVISOR },
  { _fnum_textGetCurrentInput, kernelTextGetCurrentInput, 0, PRIVILEGE_USER },
  { _fnum_textSetCurrentInput, kernelTextSetCurrentInput, 1, PRIVILEGE_USER },
  { _fnum_textGetCurrentOutput, kernelTextGetCurrentOutput,
    0, PRIVILEGE_USER },
  { _fnum_textSetCurrentOutput, kernelTextSetCurrentOutput,
    1, PRIVILEGE_USER },
  { _fnum_textGetForeground, kernelTextGetForeground, 1, PRIVILEGE_USER },
  { _fnum_textSetForeground, kernelTextSetForeground, 1, PRIVILEGE_USER },
  { _fnum_textGetBackground, kernelTextGetBackground, 1, PRIVILEGE_USER },
  { _fnum_textSetBackground, kernelTextSetBackground, 1, PRIVILEGE_USER },
  { _fnum_textPutc, kernelTextPutc, 1, PRIVILEGE_USER },
  { _fnum_textPrint, (void *) kernelTextPrint, 1, PRIVILEGE_USER },
  { _fnum_textPrintAttrs, (void *) kernelTextPrintAttrs, 2, PRIVILEGE_USER },
  { _fnum_textPrintLine, (void *) kernelTextPrintLine, 1, PRIVILEGE_USER },
  { _fnum_textNewline, kernelTextNewline, 0, PRIVILEGE_USER },
  { _fnum_textBackSpace, kernelTextBackSpace, 0, PRIVILEGE_USER },
  { _fnum_textTab, kernelTextTab, 0, PRIVILEGE_USER },
  { _fnum_textCursorUp, kernelTextCursorUp, 0, PRIVILEGE_USER },
  { _fnum_textCursorDown, kernelTextCursorDown, 0, PRIVILEGE_USER },
  { _fnum_ternelTextCursorLeft, kernelTextCursorLeft, 0, PRIVILEGE_USER },
  { _fnum_textCursorRight, kernelTextCursorRight, 0, PRIVILEGE_USER },
  { _fnum_textEnableScroll, kernelTextEnableScroll, 1, PRIVILEGE_USER },
  { _fnum_textScroll, kernelTextScroll, 1, PRIVILEGE_USER },
  { _fnum_textGetNumColumns, kernelTextGetNumColumns, 0, PRIVILEGE_USER },
  { _fnum_textGetNumRows, kernelTextGetNumRows, 0, PRIVILEGE_USER },
  { _fnum_textGetColumn, kernelTextGetColumn, 0, PRIVILEGE_USER },
  { _fnum_textSetColumn, kernelTextSetColumn, 1, PRIVILEGE_USER },
  { _fnum_textGetRow, kernelTextGetRow, 0, PRIVILEGE_USER },
  { _fnum_textSetRow, kernelTextSetRow, 1, PRIVILEGE_USER },
  { _fnum_textSetCursor, kernelTextSetCursor, 1, PRIVILEGE_USER },
  { _fnum_textScreenClear, kernelTextScreenClear, 0, PRIVILEGE_USER },
  { _fnum_textScreenSave, kernelTextScreenSave, 1, PRIVILEGE_USER },
  { _fnum_textScreenRestore, kernelTextScreenRestore, 1, PRIVILEGE_USER },
  { _fnum_textInputStreamCount, kernelTextInputStreamCount,
    1, PRIVILEGE_USER },
  { _fnum_textInputCount, kernelTextInputCount, 0, PRIVILEGE_USER },
  { _fnum_textInputStreamGetc, kernelTextInputStreamGetc, 2, PRIVILEGE_USER },
  { _fnum_textInputGetc, kernelTextInputGetc, 1, PRIVILEGE_USER },
  { _fnum_textInputStreamReadN, kernelTextInputStreamReadN,
    3, PRIVILEGE_USER },
  { _fnum_textInputReadN, kernelTextInputReadN, 2, PRIVILEGE_USER },
  { _fnum_textInputStreamReadAll, kernelTextInputStreamReadAll,
    2, PRIVILEGE_USER },
  { _fnum_textInputReadAll, kernelTextInputReadAll, 1, PRIVILEGE_USER },
  { _fnum_textInputStreamAppend, kernelTextInputStreamAppend,
    2, PRIVILEGE_USER },
  { _fnum_textInputAppend, kernelTextInputAppend, 1, PRIVILEGE_USER },
  { _fnum_textInputStreamAppendN, kernelTextInputStreamAppendN,
    3, PRIVILEGE_USER },
  { _fnum_textInputAppendN, kernelTextInputAppendN, 2, PRIVILEGE_USER },
  { _fnum_textInputStreamRemove, kernelTextInputStreamRemove,
    1, PRIVILEGE_USER },
  { _fnum_textInputRemove, kernelTextInputRemove, 0, PRIVILEGE_USER },
  { _fnum_textInputStreamRemoveN, kernelTextInputStreamRemoveN,
    2, PRIVILEGE_USER },
  { _fnum_textInputRemoveN, kernelTextInputRemoveN, 1, PRIVILEGE_USER },
  { _fnum_textInputStreamRemoveAll, kernelTextInputStreamRemoveAll,
    1, PRIVILEGE_USER },
  { _fnum_textInputRemoveAll, kernelTextInputRemoveAll, 0, PRIVILEGE_USER },
  { _fnum_textInputStreamSetEcho, kernelTextInputStreamSetEcho,
    2, PRIVILEGE_USER },
  { _fnum_textInputSetEcho, kernelTextInputSetEcho, 1,  PRIVILEGE_USER }
};

static kernelFunctionIndex diskFunctionIndex[] = {

  // Disk functions (2000-2999 range)

  { _fnum_diskReadPartitions, kernelDiskReadPartitions,
    1, PRIVILEGE_SUPERVISOR },
  { _fnum_diskReadPartitionsAll, kernelDiskReadPartitionsAll,
    0, PRIVILEGE_SUPERVISOR },
  { _fnum_diskSync, kernelDiskSync, 1, PRIVILEGE_USER },
  { _fnum_diskSyncAll, kernelDiskSyncAll, 0, PRIVILEGE_USER },
  { _fnum_diskGetBoot, kernelDiskGetBoot, 1, PRIVILEGE_USER },
  { _fnum_diskGetCount, kernelDiskGetCount, 0, PRIVILEGE_USER },
  { _fnum_diskGetPhysicalCount, kernelDiskGetPhysicalCount,
    0, PRIVILEGE_USER },
  { _fnum_diskGet, kernelDiskGet, 2, PRIVILEGE_USER },
  { _fnum_diskGetAll, kernelDiskGetAll, 2, PRIVILEGE_USER },
  { _fnum_diskGetAllPhysical, kernelDiskGetAllPhysical, 2, PRIVILEGE_USER },
  { _fnum_diskGetFilesystemType, kernelDiskGetFilesystemType,
    3, PRIVILEGE_USER },
  { _fnum_diskGetPartType, kernelDiskGetPartType, 2, PRIVILEGE_USER },
  { _fnum_diskGetPartTypes, kernelDiskGetPartTypes, 0, PRIVILEGE_USER },
  { _fnum_diskSetFlags, kernelDiskSetFlags, 3, PRIVILEGE_SUPERVISOR },
  { _fnum_diskSetLockState, kernelDiskSetLockState, 2, PRIVILEGE_USER },
  { _fnum_diskSetDoorState, kernelDiskSetDoorState, 2, PRIVILEGE_USER },
  { _fnum_diskGetMediaState, kernelDiskGetMediaState, 1, PRIVILEGE_USER },
  { _fnum_diskReadSectors, kernelDiskReadSectors, 4, PRIVILEGE_SUPERVISOR },
  { _fnum_diskWriteSectors, kernelDiskWriteSectors, 4, PRIVILEGE_SUPERVISOR },
  { _fnum_diskEraseSectors, kernelDiskEraseSectors, 4, PRIVILEGE_SUPERVISOR },
  { _fnum_diskGetStats, kernelDiskGetStats, 2, PRIVILEGE_USER }
};

static kernelFunctionIndex filesystemFunctionIndex[] = {

  // Filesystem functions (3000-3999 range)

  { _fnum_filesystemFormat, kernelFilesystemFormat, 5, PRIVILEGE_SUPERVISOR },
  { _fnum_filesystemClobber, kernelFilesystemClobber,
    1, PRIVILEGE_SUPERVISOR },
  { _fnum_filesystemCheck, kernelFilesystemCheck, 4, PRIVILEGE_USER },
  { _fnum_filesystemDefragment, kernelFilesystemDefragment,
    2, PRIVILEGE_SUPERVISOR },
  { _fnum_filesystemResizeConstraints, kernelFilesystemResizeConstraints,
    3, PRIVILEGE_USER },
  { _fnum_filesystemResize, kernelFilesystemResize, 3, PRIVILEGE_SUPERVISOR },
  { _fnum_filesystemMount, kernelFilesystemMount, 2, PRIVILEGE_USER },
  { _fnum_filesystemUnmount, kernelFilesystemUnmount, 1, PRIVILEGE_USER },
  { _fnum_filesystemGetFree, kernelFilesystemGetFree, 1, PRIVILEGE_USER },
  { _fnum_filesystemGetBlockSize, kernelFilesystemGetBlockSize,
    1, PRIVILEGE_USER }
};

static kernelFunctionIndex fileFunctionIndex[] = {

  // File functions (4000-4999 range)

  { _fnum_fileFixupPath, kernelFileFixupPath, 2, PRIVILEGE_USER },
  { _fnum_fileGetDisk, kernelFileGetDisk, 2, PRIVILEGE_USER },
  { _fnum_fileCount, kernelFileCount, 1, PRIVILEGE_USER },
  { _fnum_fileFirst, kernelFileFirst, 2, PRIVILEGE_USER },
  { _fnum_fileNext, kernelFileNext, 2, PRIVILEGE_USER },
  { _fnum_fileFind, kernelFileFind, 2, PRIVILEGE_USER },
  { _fnum_fileOpen, kernelFileOpen, 3, PRIVILEGE_USER },
  { _fnum_fileClose, kernelFileClose, 1, PRIVILEGE_USER },
  { _fnum_fileRead, kernelFileRead, 4, PRIVILEGE_USER },
  { _fnum_fileWrite, kernelFileWrite, 4, PRIVILEGE_USER },
  { _fnum_fileDelete, kernelFileDelete, 1, PRIVILEGE_USER },
  { _fnum_fileDeleteRecursive, kernelFileDeleteRecursive, 1, PRIVILEGE_USER },
  { _fnum_fileDeleteSecure, kernelFileDeleteSecure, 2, PRIVILEGE_USER },
  { _fnum_fileMakeDir, kernelFileMakeDir, 1, PRIVILEGE_USER },
  { _fnum_fileRemoveDir, kernelFileRemoveDir, 1, PRIVILEGE_USER },
  { _fnum_fileCopy, kernelFileCopy, 2, PRIVILEGE_USER },
  { _fnum_fileCopyRecursive, kernelFileCopyRecursive, 2, PRIVILEGE_USER },
  { _fnum_fileMove, kernelFileMove, 2, PRIVILEGE_USER },
  { _fnum_fileTimestamp, kernelFileTimestamp, 1, PRIVILEGE_USER },
  { _fnum_fileGetTemp, kernelFileGetTemp, 1, PRIVILEGE_USER },
  { _fnum_fileStreamOpen, kernelFileStreamOpen, 3, PRIVILEGE_USER },
  { _fnum_fileStreamSeek, kernelFileStreamSeek, 2, PRIVILEGE_USER },
  { _fnum_fileStreamRead, kernelFileStreamRead, 3, PRIVILEGE_USER },
  { _fnum_fileStreamReadLine, kernelFileStreamReadLine, 3, PRIVILEGE_USER },
  { _fnum_fileStreamWrite, kernelFileStreamWrite, 3, PRIVILEGE_USER },
  { _fnum_fileStreamWriteStr, kernelFileStreamWriteStr, 2, PRIVILEGE_USER },
  { _fnum_fileStreamWriteLine, kernelFileStreamWriteLine, 2, PRIVILEGE_USER },
  { _fnum_fileStreamFlush, kernelFileStreamFlush, 1, PRIVILEGE_USER },
  { _fnum_fileStreamClose, kernelFileStreamClose, 1, PRIVILEGE_USER }
};

static kernelFunctionIndex memoryFunctionIndex[] = {

  // Memory manager functions (5000-5999 range)

  { _fnum_memoryGet, kernelMemoryGet, 2, PRIVILEGE_USER },
  { _fnum_memoryGetPhysical, kernelMemoryGetPhysical,
    3, PRIVILEGE_SUPERVISOR },
  { _fnum_memoryRelease, kernelMemoryRelease, 1, PRIVILEGE_USER },
  { _fnum_memoryReleaseAllByProcId, kernelMemoryReleaseAllByProcId,
    1, PRIVILEGE_USER },
  { _fnum_memoryChangeOwner, kernelMemoryChangeOwner,
    4, PRIVILEGE_SUPERVISOR },
  { _fnum_memoryGetStats, kernelMemoryGetStats, 2, PRIVILEGE_USER },
  { _fnum_memoryGetBlocks, kernelMemoryGetBlocks, 3, PRIVILEGE_USER },
  { _fnum_memoryBlockInfo, kernelMemoryBlockInfo, 2, PRIVILEGE_USER }
};

static kernelFunctionIndex multitaskerFunctionIndex[] = {

  // Multitasker functions (6000-6999 range)

  { _fnum_multitaskerCreateProcess, kernelMultitaskerCreateProcess,
    3, PRIVILEGE_USER },
  { _fnum_multitaskerSpawn, kernelMultitaskerSpawn, 4, PRIVILEGE_USER },
  { _fnum_multitaskerGetCurrentProcessId, kernelMultitaskerGetCurrentProcessId,
    0, PRIVILEGE_USER },
  { _fnum_multitaskerGetProcess, kernelMultitaskerGetProcess,
    2, PRIVILEGE_USER },
  { _fnum_multitaskerGetProcessByName, kernelMultitaskerGetProcessByName,
    2, PRIVILEGE_USER },
  { _fnum_multitaskerGetProcesses, kernelMultitaskerGetProcesses,
    2, PRIVILEGE_USER },
  { _fnum_multitaskerSetProcessState, kernelMultitaskerSetProcessState,
    2, PRIVILEGE_USER },
  { _fnum_multitaskerProcessIsAlive, kernelMultitaskerProcessIsAlive,
    1, PRIVILEGE_USER },
  { _fnum_multitaskerSetProcessPriority, kernelMultitaskerSetProcessPriority,
    2, PRIVILEGE_USER },
  { _fnum_multitaskerGetProcessPrivilege, kernelMultitaskerGetProcessPrivilege,
    1, PRIVILEGE_USER },
  { _fnum_multitaskerGetCurrentDirectory, kernelMultitaskerGetCurrentDirectory,
    2, PRIVILEGE_USER },
  { _fnum_multitaskerSetCurrentDirectory, kernelMultitaskerSetCurrentDirectory,
    1, PRIVILEGE_USER },
  { _fnum_multitaskerGetTextInput, kernelMultitaskerGetTextInput,
    0, PRIVILEGE_USER },
  { _fnum_multitaskerSetTextInput, kernelMultitaskerSetTextInput,
    2, PRIVILEGE_USER },
  { _fnum_multitaskerGetTextOutput, kernelMultitaskerGetTextOutput,
    0, PRIVILEGE_USER },
  { _fnum_multitaskerSetTextOutput, kernelMultitaskerSetTextOutput,
    2, PRIVILEGE_USER },
  { _fnum_multitaskerDuplicateIO, kernelMultitaskerDuplicateIO,
    3, PRIVILEGE_USER },
  { _fnum_multitaskerGetProcessorTime, kernelMultitaskerGetProcessorTime,
    1, PRIVILEGE_USER },
  { _fnum_multitaskerYield, kernelMultitaskerYield, 0, PRIVILEGE_USER },
  { _fnum_multitaskerWait, kernelMultitaskerWait, 1, PRIVILEGE_USER },
  { _fnum_multitaskerBlock, kernelMultitaskerBlock, 1, PRIVILEGE_USER },
  { _fnum_multitaskerDetach, kernelMultitaskerDetach, 0, PRIVILEGE_USER },
  { _fnum_multitaskerKillProcess, kernelMultitaskerKillProcess,
    2, PRIVILEGE_USER },
  { _fnum_multitaskerKillByName, kernelMultitaskerKillByName, 2,
    PRIVILEGE_USER },
  { _fnum_multitaskerTerminate, kernelMultitaskerTerminate,
    1, PRIVILEGE_USER },
  { _fnum_multitaskerSignalSet, kernelMultitaskerSignalSet,
    3, PRIVILEGE_USER },
  { _fnum_multitaskerSignal, kernelMultitaskerSignal, 2, PRIVILEGE_USER },
  { _fnum_multitaskerSignalRead, kernelMultitaskerSignalRead,
    1, PRIVILEGE_USER },
  { _fnum_multitaskerGetIOPerm, kernelMultitaskerGetIOPerm, 2, PRIVILEGE_USER},
  { _fnum_multitaskerSetIOPerm, kernelMultitaskerSetIOPerm,
    3, PRIVILEGE_SUPERVISOR}
};

static kernelFunctionIndex loaderFunctionIndex[] = {

  // Loader functions (7000-7999 range)

  { _fnum_loaderLoad, kernelLoaderLoad, 2, PRIVILEGE_USER },
  { _fnum_loaderClassify, kernelLoaderClassify, 4, PRIVILEGE_USER },
  { _fnum_loaderClassifyFile, kernelLoaderClassifyFile, 2, PRIVILEGE_USER },
  { _fnum_loaderGetSymbols, kernelLoaderGetSymbols, 2, PRIVILEGE_USER },
  { _fnum_loaderCheckCommand, kernelLoaderCheckCommand, 1, PRIVILEGE_USER },
  { _fnum_loaderLoadProgram, kernelLoaderLoadProgram, 2, PRIVILEGE_USER },
  { _fnum_loaderLoadLibrary, kernelLoaderLoadLibrary, 1, PRIVILEGE_USER },
  { _fnum_loaderExecProgram, kernelLoaderExecProgram, 2, PRIVILEGE_USER },
  { _fnum_loaderLoadAndExec, kernelLoaderLoadAndExec,  3, PRIVILEGE_USER }
};

static kernelFunctionIndex rtcFunctionIndex[] = {

  // Real-time clock functions (8000-8999 range)

  { _fnum_rtcReadSeconds, kernelRtcReadSeconds, 0, PRIVILEGE_USER },
  { _fnum_rtcReadMinutes, kernelRtcReadMinutes, 0, PRIVILEGE_USER },
  { _fnum_rtcReadHours, kernelRtcReadHours, 0, PRIVILEGE_USER },
  { _fnum_rtcDayOfWeek, kernelRtcDayOfWeek, 3, PRIVILEGE_USER },
  { _fnum_rtcReadDayOfMonth, kernelRtcReadDayOfMonth, 0, PRIVILEGE_USER },
  { _fnum_rtcReadMonth, kernelRtcReadMonth, 0, PRIVILEGE_USER },
  { _fnum_rtcReadYear, kernelRtcReadYear, 0, PRIVILEGE_USER },
  { _fnum_rtcUptimeSeconds, kernelRtcUptimeSeconds, 0, PRIVILEGE_USER },
  { _fnum_rtcDateTime, kernelRtcDateTime, 1, PRIVILEGE_USER }
};

static kernelFunctionIndex randomFunctionIndex[] = {

  // Random number functions (9000-9999 range)

  { _fnum_randomUnformatted, kernelRandomUnformatted, 0, PRIVILEGE_USER },
  { _fnum_randomFormatted, kernelRandomFormatted, 2, PRIVILEGE_USER },
  { _fnum_randomSeededUnformatted, kernelRandomSeededUnformatted,
    1, PRIVILEGE_USER },
  { _fnum_randomSeededFormatted, kernelRandomSeededFormatted,
    3, PRIVILEGE_USER }
};

static kernelFunctionIndex environmentFunctionIndex[] = {
 
  // Environment functions (10000-10999 range)

  { _fnum_environmentGet, kernelEnvironmentGet, 3, PRIVILEGE_USER },
  { _fnum_environmentSet, kernelEnvironmentSet, 2, PRIVILEGE_USER },
  { _fnum_environmentUnset, kernelEnvironmentUnset, 1, PRIVILEGE_USER },
  { _fnum_environmentDump, kernelEnvironmentDump, 0, PRIVILEGE_USER }
};

static kernelFunctionIndex graphicFunctionIndex[] = {
 
  // Raw graphics functions (11000-11999 range)

  { _fnum_graphicsAreEnabled, kernelGraphicsAreEnabled, 0, PRIVILEGE_USER },
  { _fnum_graphicGetModes, kernelGraphicGetModes, 2, PRIVILEGE_USER },
  { _fnum_graphicGetMode, kernelGraphicGetMode, 1, PRIVILEGE_USER },
  { _fnum_graphicSetMode, kernelGraphicSetMode, 1, PRIVILEGE_SUPERVISOR },
  { _fnum_graphicGetScreenWidth, kernelGraphicGetScreenWidth,
    0, PRIVILEGE_USER },
  { _fnum_graphicGetScreenHeight, kernelGraphicGetScreenHeight,
    0, PRIVILEGE_USER },
  { _fnum_graphicCalculateAreaBytes, kernelGraphicCalculateAreaBytes,
    2, PRIVILEGE_USER },
  { _fnum_graphicClearScreen, kernelGraphicClearScreen, 1, PRIVILEGE_USER },
  { _fnum_graphicGetColor, kernelGraphicGetColor, 2, PRIVILEGE_USER },
  { _fnum_graphicSetColor, kernelGraphicSetColor, 2, PRIVILEGE_USER },
  { _fnum_graphicDrawPixel, kernelGraphicDrawPixel, 5, PRIVILEGE_USER },
  { _fnum_graphicDrawLine, kernelGraphicDrawLine, 7, PRIVILEGE_USER },
  { _fnum_graphicDrawRect, kernelGraphicDrawRect, 9, PRIVILEGE_USER },
  { _fnum_graphicDrawOval, kernelGraphicDrawOval, 9, PRIVILEGE_USER },
  { _fnum_graphicDrawImage, kernelGraphicDrawImage, 9, PRIVILEGE_USER },
  { _fnum_graphicGetImage, kernelGraphicGetImage, 6, PRIVILEGE_USER },
  { _fnum_graphicDrawText, kernelGraphicDrawText, 8, PRIVILEGE_USER },
  { _fnum_graphicCopyArea, kernelGraphicCopyArea, 7, PRIVILEGE_USER },
  { _fnum_graphicClearArea, kernelGraphicClearArea, 6, PRIVILEGE_USER },
  { _fnum_graphicRenderBuffer, kernelGraphicRenderBuffer, 7, PRIVILEGE_USER }
};

static kernelFunctionIndex windowFunctionIndex[] = {
 
  // Windowing system functions (12000-12999 range)

  { _fnum_windowLogin, kernelWindowLogin, 1, PRIVILEGE_SUPERVISOR },
  { _fnum_windowLogout, kernelWindowLogout, 0, PRIVILEGE_USER },
  { _fnum_windowNew, kernelWindowNew, 2, PRIVILEGE_USER },
  { _fnum_windowNewDialog, kernelWindowNewDialog, 2, PRIVILEGE_USER },
  { _fnum_windowDestroy, kernelWindowDestroy, 1, PRIVILEGE_USER },
  { _fnum_windowUpdateBuffer, kernelWindowUpdateBuffer, 5, PRIVILEGE_USER },
  { _fnum_windowSetTitle, kernelWindowSetTitle, 2, PRIVILEGE_USER },
  { _fnum_windowGetSize, kernelWindowGetSize, 3, PRIVILEGE_USER },
  { _fnum_windowSetSize, kernelWindowSetSize, 3, PRIVILEGE_USER },
  { _fnum_windowGetLocation, kernelWindowGetLocation, 3, PRIVILEGE_USER },
  { _fnum_windowSetLocation, kernelWindowSetLocation, 3, PRIVILEGE_USER },
  { _fnum_windowCenter, kernelWindowCenter, 1, PRIVILEGE_USER },
  { _fnum_windowSnapIcons, kernelWindowSnapIcons, 1, PRIVILEGE_USER },
  { _fnum_windowSetHasBorder, kernelWindowSetHasBorder, 2, PRIVILEGE_USER },
  { _fnum_windowSetHasTitleBar, kernelWindowSetHasTitleBar,
    2, PRIVILEGE_USER },
  { _fnum_windowSetMovable, kernelWindowSetMovable, 2, PRIVILEGE_USER },
  { _fnum_windowSetResizable, kernelWindowSetResizable, 2, PRIVILEGE_USER },
  { _fnum_windowRemoveMinimizeButton, kernelWindowRemoveMinimizeButton,
    1, PRIVILEGE_USER },
  { _fnum_windowRemoveCloseButton, kernelWindowRemoveCloseButton,
    1, PRIVILEGE_USER },
  { _fnum_windowSetColors, kernelWindowSetColors, 2, PRIVILEGE_USER },
  { _fnum_windowSetVisible, kernelWindowSetVisible, 2, PRIVILEGE_USER },
  { _fnum_windowSetMinimized, kernelWindowSetMinimized, 2, PRIVILEGE_USER },
  { _fnum_windowAddConsoleTextArea, kernelWindowAddConsoleTextArea,
    1, PRIVILEGE_USER },
  { _fnum_windowRedrawArea, kernelWindowRedrawArea, 4, PRIVILEGE_USER },
  { _fnum_windowDrawAll, kernelWindowDrawAll, 0, PRIVILEGE_USER },
  { _fnum_windowResetColors, kernelWindowResetColors, 0, PRIVILEGE_USER },
  { _fnum_windowProcessEvent, kernelWindowProcessEvent, 1, PRIVILEGE_USER },
  { _fnum_windowComponentEventGet, kernelWindowComponentEventGet,
    2, PRIVILEGE_USER },
  { _fnum_windowTileBackground, kernelWindowTileBackground,
    1, PRIVILEGE_USER },
  { _fnum_windowCenterBackground, kernelWindowCenterBackground,
    1, PRIVILEGE_USER },
  { _fnum_windowScreenShot, kernelWindowScreenShot, 1, PRIVILEGE_USER },
  { _fnum_windowSaveScreenShot, kernelWindowSaveScreenShot,
    1, PRIVILEGE_USER },
  { _fnum_windowSetTextOutput, kernelWindowSetTextOutput, 1, PRIVILEGE_USER },
  { _fnum_windowLayout, kernelWindowLayout, 1, PRIVILEGE_USER },
  { _fnum_windowDebugLayout, kernelWindowDebugLayout, 1, PRIVILEGE_USER },
  { _fnum_windowContextAdd, kernelWindowContextAdd, 2, PRIVILEGE_USER },
  { _fnum_windowContextSet, kernelWindowContextSet, 2, PRIVILEGE_USER },
  { _fnum_windowSwitchPointer, kernelWindowSwitchPointer, 2, PRIVILEGE_USER },
  { _fnum_windowComponentDestroy, kernelWindowComponentDestroy,
    1, PRIVILEGE_USER },
  { _fnum_windowComponentSetVisible, kernelWindowComponentSetVisible,
    2, PRIVILEGE_USER },
  { _fnum_windowComponentSetEnabled, kernelWindowComponentSetEnabled,
    2, PRIVILEGE_USER },
  { _fnum_windowComponentGetWidth, kernelWindowComponentGetWidth,
    1, PRIVILEGE_USER },
  { _fnum_windowComponentSetWidth, kernelWindowComponentSetWidth,
    2, PRIVILEGE_USER },
  { _fnum_windowComponentGetHeight, kernelWindowComponentGetHeight,
    1, PRIVILEGE_USER },
  { _fnum_windowComponentSetHeight, kernelWindowComponentSetHeight,
    2, PRIVILEGE_USER },
  { _fnum_windowComponentFocus, kernelWindowComponentFocus,
    1, PRIVILEGE_USER },
  { _fnum_windowComponentDraw, kernelWindowComponentDraw, 1, PRIVILEGE_USER },
  { _fnum_windowComponentGetData, kernelWindowComponentGetData,
    3, PRIVILEGE_USER },
  { _fnum_windowComponentSetData, kernelWindowComponentSetData,
    3, PRIVILEGE_USER },
  { _fnum_windowComponentGetSelected, kernelWindowComponentGetSelected,
    2, PRIVILEGE_USER },
  { _fnum_windowComponentSetSelected, kernelWindowComponentSetSelected,
    2, PRIVILEGE_USER },
  { _fnum_windowNewButton, kernelWindowNewButton, 4, PRIVILEGE_USER },
  { _fnum_windowNewCanvas, kernelWindowNewCanvas, 4, PRIVILEGE_USER },
  { _fnum_windowNewCheckbox, kernelWindowNewCheckbox, 3, PRIVILEGE_USER },
  { _fnum_windowNewContainer, kernelWindowNewContainer, 3, PRIVILEGE_USER },
  { _fnum_windowNewIcon, kernelWindowNewIcon, 4, PRIVILEGE_USER },
  { _fnum_windowNewImage, kernelWindowNewImage, 4, PRIVILEGE_USER },
  { _fnum_windowNewList, kernelWindowNewList, 8, PRIVILEGE_USER },
  { _fnum_windowNewListItem, kernelWindowNewListItem, 3, PRIVILEGE_USER },
  { _fnum_windowNewMenu, kernelWindowNewMenu, 4, PRIVILEGE_USER },
  { _fnum_windowNewMenuBar, kernelWindowNewMenuBar, 2, PRIVILEGE_USER },
  { _fnum_windowNewMenuItem, kernelWindowNewMenuItem, 3, PRIVILEGE_USER },
  { _fnum_windowNewPasswordField, kernelWindowNewPasswordField,
    3, PRIVILEGE_USER },
  { _fnum_windowNewProgressBar, kernelWindowNewProgressBar,
    2, PRIVILEGE_USER },
  { _fnum_windowNewRadioButton, kernelWindowNewRadioButton,
    6, PRIVILEGE_USER },
  { _fnum_windowNewScrollBar, kernelWindowNewScrollBar, 5, PRIVILEGE_USER },
  { _fnum_windowNewSlider, kernelWindowNewSlider, 5, PRIVILEGE_USER },
  { _fnum_windowNewTextArea, kernelWindowNewTextArea, 5, PRIVILEGE_USER },
  { _fnum_windowNewTextField, kernelWindowNewTextField, 3, PRIVILEGE_USER },
  { _fnum_windowNewTextLabel, kernelWindowNewTextLabel, 3, PRIVILEGE_USER }
};

static kernelFunctionIndex userFunctionIndex[] = {

  // User functions (13000-13999 range)

  { _fnum_userAuthenticate, kernelUserAuthenticate, 2, PRIVILEGE_USER },
  { _fnum_userLogin, kernelUserLogin, 2, PRIVILEGE_SUPERVISOR },
  { _fnum_userLogout, kernelUserLogout, 1, PRIVILEGE_USER },
  { _fnum_userGetNames, kernelUserGetNames, 2, PRIVILEGE_USER },
  { _fnum_userAdd, kernelUserAdd, 2, PRIVILEGE_SUPERVISOR },
  { _fnum_userDelete, kernelUserDelete, 1, PRIVILEGE_SUPERVISOR },
  { _fnum_userSetPassword, kernelUserSetPassword, 3, PRIVILEGE_USER },
  { _fnum_userGetPrivilege, kernelUserGetPrivilege, 1, PRIVILEGE_USER },
  { _fnum_userGetPid, kernelUserGetPid, 0, PRIVILEGE_USER },
  { _fnum_userSetPid, kernelUserSetPid, 2, PRIVILEGE_SUPERVISOR },
  { _fnum_userFileAdd, kernelUserFileAdd, 3, PRIVILEGE_SUPERVISOR },
  { _fnum_userFileDelete, kernelUserFileDelete, 2, PRIVILEGE_SUPERVISOR },
  { _fnum_userFileSetPassword, kernelUserFileSetPassword, 4, PRIVILEGE_USER }
};

static kernelFunctionIndex networkFunctionIndex[] = {

  // Network functions (14000-14999 range)

  { _fnum_networkDeviceGetCount, kernelNetworkDeviceGetCount,
    0, PRIVILEGE_USER },
  { _fnum_networkDeviceGet, kernelNetworkDeviceGet, 2, PRIVILEGE_USER },
  { _fnum_networkInitialized, kernelNetworkInitialized, 0, PRIVILEGE_USER },
  { _fnum_networkInitialize, kernelNetworkInitialize,
    0, PRIVILEGE_SUPERVISOR },
  { _fnum_networkShutdown, kernelNetworkShutdown, 0, PRIVILEGE_SUPERVISOR },
  { _fnum_networkOpen, kernelNetworkOpen, 3, PRIVILEGE_USER },
  { _fnum_networkClose, kernelNetworkClose, 1, PRIVILEGE_USER },
  { _fnum_networkCount, kernelNetworkCount, 1, PRIVILEGE_USER },
  { _fnum_networkRead, kernelNetworkRead, 3, PRIVILEGE_USER },
  { _fnum_networkWrite, kernelNetworkWrite, 3, PRIVILEGE_USER },
  { _fnum_networkPing, kernelNetworkPing, 4, PRIVILEGE_USER },
  { _fnum_networkGetHostName, kernelNetworkGetHostName, 2, PRIVILEGE_USER },
  { _fnum_networkSetHostName, kernelNetworkSetHostName,
    2, PRIVILEGE_SUPERVISOR },
  { _fnum_networkGetDomainName, kernelNetworkGetDomainName,
    2, PRIVILEGE_USER },
  { _fnum_networkSetDomainName, kernelNetworkSetDomainName,
    2, PRIVILEGE_SUPERVISOR },
};

static kernelFunctionIndex miscFunctionIndex[] = {

  // Miscellaneous functions (99000-99999 range)
 
  { _fnum_fontGetDefault, kernelFontGetDefault, 1, PRIVILEGE_USER },
  { _fnum_fontSetDefault, kernelFontSetDefault, 1, PRIVILEGE_USER },
  { _fnum_fontLoad, kernelFontLoad, 4, PRIVILEGE_USER },
  { _fnum_fontGetPrintedWidth, kernelFontGetPrintedWidth, 2, PRIVILEGE_USER },
  { _fnum_fontGetWidth, kernelFontGetWidth, 1, PRIVILEGE_USER },
  { _fnum_fontGetHeight, kernelFontGetHeight, 1, PRIVILEGE_USER },
  { _fnum_imageLoad, kernelImageLoad, 4, PRIVILEGE_USER },
  { _fnum_imageSave, kernelImageSave, 3, PRIVILEGE_USER },
  { _fnum_shutdown, kernelShutdown, 2, PRIVILEGE_USER },
  { _fnum_getVersion, kernelGetVersion, 2, PRIVILEGE_USER },
  { _fnum_systemInfo, kernelSystemInfo, 1, PRIVILEGE_USER },
  { _fnum_encryptMD5, kernelEncryptMD5, 2, PRIVILEGE_USER },
  { _fnum_lockGet, kernelLockGet, 1, PRIVILEGE_USER },
  { _fnum_lockRelease, kernelLockRelease, 1, PRIVILEGE_USER },
  { _fnum_lockVerify, kernelLockVerify, 1, PRIVILEGE_USER },
  { _fnum_variableListCreate, kernelVariableListCreate, 1, PRIVILEGE_USER },
  { _fnum_variableListDestroy, kernelVariableListDestroy, 1, PRIVILEGE_USER },
  { _fnum_variableListGet, kernelVariableListGet, 4, PRIVILEGE_USER },
  { _fnum_variableListSet, kernelVariableListSet, 3, PRIVILEGE_USER },
  { _fnum_variableListUnset, kernelVariableListUnset, 2, PRIVILEGE_USER },
  { _fnum_configurationReader, kernelConfigurationReader, 2, PRIVILEGE_USER },
  { _fnum_configurationWriter, kernelConfigurationWriter, 2, PRIVILEGE_USER },
  { _fnum_keyboardGetMaps, kernelKeyboardGetMaps, 2, PRIVILEGE_USER },
  { _fnum_keyboardSetMap, kernelKeyboardSetMap, 1, PRIVILEGE_USER },
  { _fnum_deviceTreeGetCount, kernelDeviceTreeGetCount, 0, PRIVILEGE_USER },
  { _fnum_deviceTreeGetRoot, kernelDeviceTreeGetRoot, 1, PRIVILEGE_USER },
  { _fnum_deviceTreeGetChild, kernelDeviceTreeGetChild, 2, PRIVILEGE_USER },
  { _fnum_deviceTreeGetNext, kernelDeviceTreeGetNext, 1, PRIVILEGE_USER },
  { _fnum_mouseLoadPointer, kernelMouseLoadPointer, 2, PRIVILEGE_USER }
};

static kernelFunctionIndex *functionIndex[] = {
  miscFunctionIndex,
  textFunctionIndex,
  diskFunctionIndex,
  filesystemFunctionIndex,
  fileFunctionIndex,
  memoryFunctionIndex,
  multitaskerFunctionIndex,
  loaderFunctionIndex,
  rtcFunctionIndex,
  randomFunctionIndex,
  environmentFunctionIndex,
  graphicFunctionIndex,
  windowFunctionIndex,
  userFunctionIndex,
  networkFunctionIndex
};

Some graphic functions

static int driverDrawPixel(kernelGraphicBuffer *buffer, color *foreground,
               drawMode mode, int xCoord, int yCoord)
{
  // Draws a single pixel to the graphic buffer using the preset foreground
  // color

  int status = 0;
  unsigned char *framebufferPointer = NULL;
  short pix = 0;

  // If the supplied kernelGraphicBuffer is NULL, we draw directly to the
  // whole screen
  if (buffer == NULL)
    buffer = &wholeScreen;

  // Make sure the pixel is in the buffer
  if ((xCoord >= buffer->width) || (yCoord >= buffer->height))
    // Don't make an error condition, just skip it
    return (status = 0);

  // Make sure we're not drawing off the screen
  if ((xCoord < 0) || (xCoord >= buffer->width) ||
      (yCoord < 0) || (yCoord >= buffer->height))
    return (status = 0);

  // Draw the pixel using the supplied color
  framebufferPointer = buffer->data +
    (((buffer->width * yCoord) + xCoord) * adapter->bytesPerPixel);
   
  if ((adapter->bitsPerPixel == 32) || (adapter->bitsPerPixel == 24))
    {
      if (mode == draw_normal)
    {
      framebufferPointer[0] = foreground->blue;
      framebufferPointer[1] = foreground->green;
      framebufferPointer[2] = foreground->red;
    }
      else if (mode == draw_or)
    {
      framebufferPointer[0] |= foreground->blue;
      framebufferPointer[1] |= foreground->green;
      framebufferPointer[2] |= foreground->red;
    }
      else if (mode == draw_xor)
    {
      framebufferPointer[0] ^= foreground->blue;
      framebufferPointer[1] ^= foreground->green;
      framebufferPointer[2] ^= foreground->red;
    }
    }

  else if ((adapter->bitsPerPixel == 16) || (adapter->bitsPerPixel == 15))
    {
      if (adapter->bitsPerPixel == 16)
    pix = (((foreground->red >> 3) << 11) |
           ((foreground->green >> 2) << 5) |
           (foreground->blue >> 3));
      else
    pix = (((foreground->red >> 3) << 10) |
           ((foreground->green >> 3) << 5) |
           (foreground->blue >> 3));
     
      if (mode == draw_normal)
    *((short *) framebufferPointer) = pix;
      else if (mode == draw_or)
    *((short *) framebufferPointer) |= pix;
      else if (mode == draw_xor)
    *((short *) framebufferPointer) ^= pix;
    }

  return (status = 0);
}




// driverDrawMonoImage is used to draw font image.Evert char will be inserted MONOCHROME into memory buffer for later use.
static int driverDrawMonoImage(kernelGraphicBuffer *buffer, image *drawImage,
                   drawMode mode,color *foreground,
                   color *background, int xCoord, int yCoord)
{
  // Draws the supplied image on the screen at the requested coordinates

  int status = 0;
  unsigned lineLength = 0;
  unsigned lineBytes = 0;
  int numberLines = 0;
  unsigned char *framebufferPointer = NULL;
  unsigned char *monoImageData = NULL;
  int lineCounter = 0;
  unsigned pixelCounter = 0;
  short onPixel, offPixel;
  unsigned count;

  // If the supplied kernelGraphicBuffer is NULL, we draw directly to the
  // whole screen
  if (buffer == NULL)
    buffer = &wholeScreen;

  // Check params
  if ((xCoord < 0) || (xCoord >= buffer->width) ||
      (yCoord < 0) || (yCoord >= buffer->height))
    return (status = ERR_BOUNDS);

  // Make sure it's a mono image
  if (drawImage->type != IMAGETYPE_MONO)
    return (status = ERR_INVALID);

  // If the image goes off the right edge of the screen, only attempt to
  // display what will fit
  if ((int)(xCoord + drawImage->width) < buffer->width)
    lineLength = drawImage->width;
  else
    lineLength = (buffer->width - xCoord);

  // If the image goes off the bottom of the screen, only show the
  // lines that will fit
  if ((int)(yCoord + drawImage->height) < buffer->height)
    numberLines = drawImage->height;
  else
    numberLines = (buffer->height - yCoord);
 
  // images are lovely little data structures that give us image
  // data in the most convenient form we can imagine.

  // How many bytes in a line of data?
  lineBytes = (adapter->bytesPerPixel * lineLength);

  framebufferPointer = buffer->data +
    (((buffer->width * yCoord) + xCoord) * adapter->bytesPerPixel);

  // A mono image has a bitmap of 'on' bits and 'off' bits.  We will
  // draw all 'on' bits using the current foreground color.
  monoImageData = (unsigned char *) drawImage->data;

  // Loop for each line

  for (lineCounter = 0; lineCounter < numberLines; lineCounter++)
    {
      // Do a loop through the line, copying either the foreground color
      // value or the background color into framebuffer

      if ((adapter->bitsPerPixel == 32) || (adapter->bitsPerPixel == 24))
    for (count = 0; count < lineBytes; )
      {
        // Isolate the bit from the bitmap
        if ((monoImageData[pixelCounter / 8] &
         (0x80 >> (pixelCounter % 8))) != 0)
          {
        // 'on' bit.
        framebufferPointer[count++] = foreground->blue;
        framebufferPointer[count++] = foreground->green;
        framebufferPointer[count++] = foreground->red;
        if (adapter->bitsPerPixel == 32)
          count++;
          }
        else
          {
        if (mode == draw_translucent)
          count += adapter->bytesPerPixel;
        else
          {
            // 'off' bit.
            framebufferPointer[count++] = background->blue;
            framebufferPointer[count++] = background->green;
            framebufferPointer[count++] = background->red;
            if (adapter->bitsPerPixel == 32)
              count++;
          }
          }

        pixelCounter += 1;
      }

      else if ((adapter->bitsPerPixel == 16) || (adapter->bitsPerPixel == 15))
    {
      if (adapter->bitsPerPixel == 16)
        {
          onPixel = (((foreground->red >> 3) << 11) |
             ((foreground->green >> 2) << 5) |
             (foreground->blue >> 3));
          offPixel = (((background->red >> 3) << 11) |
              ((background->green >> 2) << 5) |
              (background->blue >> 3));
        }
      else
        {
          onPixel = (((foreground->red >> 3) << 10) |
              ((foreground->green >> 3) << 5) |
              (foreground->blue >> 3));
          offPixel = (((background->red >> 3) << 10) |
              ((background->green >> 3) << 5) |
              (background->blue >> 3));
        }

    for (count = 0; count < lineLength; count ++)
      {
        // Isolate the bit from the bitmap
        if ((monoImageData[pixelCounter / 8] &
         (0x80 >> (pixelCounter % 8))) != 0)
          // 'on' bit - means that it will be inserted into Buffer.
          ((short *) framebufferPointer)[count] = onPixel;

        else if (mode != draw_translucent)
          // 'off' bit - means that it will not be inserted into Buffer
          ((short *) framebufferPointer)[count] = offPixel;

        pixelCounter += 1;
      }
    }

      // Move to the next line in the framebuffer
      framebufferPointer += (buffer->width * adapter->bytesPerPixel);
     
      // Are we skipping any because it's off the screen?
      if (drawImage->width > lineLength)
    pixelCounter += (drawImage->width - lineLength);
    }


  // Success :)
  return (status = 0);
}

Wednesday, November 17, 2010

Multitasker scheduler algorithm

Priority level 0 - most highest priority - processes will be REAL TIME scheduled. When there are any processes running and ready at this priority level, they will be serviced to the exclusion all processes not at level 0. No process will have this priority level, not even the kernel.
The last level - lowest priority - will be "BACKGROUND" scheduled. In this case, prcesses will only receive processor time when there are no ready processes at any other level.
There are 8 priority levels, from 0 to 7.

Among all of the processes at other priority levels, there will be a more even-handed approach to scheduling. Among the quantum-ing variables will be following: priority, waiting time.
quantum = ((priority levels - task priority) * ratio ) + waiting time.
This means that the inverse of the process priority will be multiplied by "ratio", and to that will be added the current waiting time.

For example:

TASK1 : priority = 1, waiting time = 4
TASK2 : priority = 2, waiting time = 6
then
TASK1 quantum = ( ( 8 - 1 ) * 3) + 4 = 25   <- winner
TASK2 quantum = ( ( 8 - 2 ) * 3) + 6 = 24
That means TASK1 win



TASK1 : priority = 1, waiting time = 3
TASK2 : priority = 2, waiting time = 10
then
TASK1 quantum = ( ( 8 - 1 ) * 3) + 3 = 24

TASK2 quantum = ( ( 8 - 2 ) * 3) + 10 = 28   <- winner

In the second case, task 2 gets to run since it has been waiting long enough to overcome task 1's higher priority. This possibility helps to ensure that no processes will stop.

Make window movable or unmovable

int kernelWindowSetMovable(kernelWindow *window, int trueFalse)
{
 

  // Sets the 'is movable' attribute
  int status = 0;
  // Make sure we've been initialized
  if (!initialized)
    return (status = ERR_NOTINITIALIZED);

  if (window == NULL)
    return (status = ERR_NOSUCHENTRY);

  if (trueFalse)
    window->flags |= WINFLAG_MOVABLE;
  else
    window->flags &= ~WINFLAG_MOVABLE;

  // Return success
  return (status = 0);
}